blob: 17b62bbc20522d5beabfc3b16adb689fbb7db3cb [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
Chris Packham1de16f72018-07-09 21:34:00 +120014#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053016#include <net.h>
17#include <malloc.h>
18#include <miiphy.h>
Chris Packhamf42e5b92018-06-09 20:46:16 +120019#include <wait_bit.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060020#include <asm/global_data.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
Albert Aribaude91d7d32010-07-12 22:24:28 +020039#define MV_PHY_ADR_REQUEST 0xee
40#define MVGBE_SMI_REG (((struct mvgbe_registers *)MVGBE0_BASE)->smi)
Tony Dinh77943da2022-04-12 13:18:19 -070041#define MVGBE_PGADR_REG 22
Simon Kagstromab9ca512009-08-20 10:12:28 +020042
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +010043#if defined(CONFIG_PHYLIB) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Chris Packhamf42e5b92018-06-09 20:46:16 +120044static int smi_wait_ready(struct mvgbe_device *dmvgbe)
45{
46 int ret;
47
48 ret = wait_for_bit_le32(&MVGBE_SMI_REG, MVGBE_PHY_SMI_BUSY_MASK, false,
49 MVGBE_PHY_SMI_TIMEOUT_MS, false);
50 if (ret) {
51 printf("Error: SMI busy timeout\n");
52 return ret;
53 }
54
55 return 0;
56}
57
Chris Packham465f5cf2018-07-09 21:33:59 +120058static int __mvgbe_mdio_read(struct mvgbe_device *dmvgbe, int phy_adr,
59 int devad, int reg_ofs)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053060{
Albert Aribaude91d7d32010-07-12 22:24:28 +020061 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053062 u32 smi_reg;
Simon Kagstrom4d0941c2009-07-08 13:03:18 +020063 u32 timeout;
Chris Packham465f5cf2018-07-09 21:33:59 +120064 u16 data = 0;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053065
66 /* Phyadr read request */
Albert Aribaude91d7d32010-07-12 22:24:28 +020067 if (phy_adr == MV_PHY_ADR_REQUEST &&
68 reg_ofs == MV_PHY_ADR_REQUEST) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053069 /* */
Joe Hershberger1fbcbed2016-08-08 11:28:38 -050070 data = (u16) (MVGBE_REG_RD(regs->phyadr) & PHYADR_MASK);
71 return data;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053072 }
73 /* check parameters */
74 if (phy_adr > PHYADR_MASK) {
75 printf("Err..(%s) Invalid PHY address %d\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -050076 __func__, phy_adr);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053077 return -EFAULT;
78 }
79 if (reg_ofs > PHYREG_MASK) {
80 printf("Err..(%s) Invalid register offset %d\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -050081 __func__, reg_ofs);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053082 return -EFAULT;
83 }
84
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053085 /* wait till the SMI is not busy */
Chris Packhamf42e5b92018-06-09 20:46:16 +120086 if (smi_wait_ready(dmvgbe) < 0)
87 return -EFAULT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053088
89 /* fill the phy address and regiser offset and read opcode */
Albert Aribaude91d7d32010-07-12 22:24:28 +020090 smi_reg = (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS)
91 | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS)
92 | MVGBE_PHY_SMI_OPCODE_READ;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053093
94 /* write the smi register */
Albert Aribaude91d7d32010-07-12 22:24:28 +020095 MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053096
97 /*wait till read value is ready */
Albert Aribaude91d7d32010-07-12 22:24:28 +020098 timeout = MVGBE_PHY_SMI_TIMEOUT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053099
100 do {
101 /* read smi register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200102 smi_reg = MVGBE_REG_RD(MVGBE_SMI_REG);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530103 if (timeout-- == 0) {
104 printf("Err..(%s) SMI read ready timeout\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -0500105 __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530106 return -EFAULT;
107 }
Albert Aribaude91d7d32010-07-12 22:24:28 +0200108 } while (!(smi_reg & MVGBE_PHY_SMI_READ_VALID_MASK));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530109
110 /* Wait for the data to update in the SMI register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200111 for (timeout = 0; timeout < MVGBE_PHY_SMI_TIMEOUT; timeout++)
112 ;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530113
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500114 data = (u16) (MVGBE_REG_RD(MVGBE_SMI_REG) & MVGBE_PHY_SMI_DATA_MASK);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530115
Joe Hershberger9f09a362015-04-08 01:41:06 -0500116 debug("%s:(adr %d, off %d) value= %04x\n", __func__, phy_adr, reg_ofs,
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500117 data);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530118
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500119 return data;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530120}
121
122/*
Chris Packham465f5cf2018-07-09 21:33:59 +1200123 * smi_reg_read - miiphy_read callback function.
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530124 *
Chris Packham465f5cf2018-07-09 21:33:59 +1200125 * Returns 16bit phy register value, or -EFAULT on error
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530126 */
Chris Packham465f5cf2018-07-09 21:33:59 +1200127static int smi_reg_read(struct mii_dev *bus, int phy_adr, int devad,
128 int reg_ofs)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530129{
Chris Packham1de16f72018-07-09 21:34:00 +1200130 struct mvgbe_device *dmvgbe = bus->priv;
Chris Packham465f5cf2018-07-09 21:33:59 +1200131
132 return __mvgbe_mdio_read(dmvgbe, phy_adr, devad, reg_ofs);
133}
134
135static int __mvgbe_mdio_write(struct mvgbe_device *dmvgbe, int phy_adr,
136 int devad, int reg_ofs, u16 data)
137{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200138 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530139 u32 smi_reg;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530140
141 /* Phyadr write request*/
Albert Aribaude91d7d32010-07-12 22:24:28 +0200142 if (phy_adr == MV_PHY_ADR_REQUEST &&
143 reg_ofs == MV_PHY_ADR_REQUEST) {
144 MVGBE_REG_WR(regs->phyadr, data);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530145 return 0;
146 }
147
148 /* check parameters */
149 if (phy_adr > PHYADR_MASK) {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500150 printf("Err..(%s) Invalid phy address\n", __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530151 return -EINVAL;
152 }
153 if (reg_ofs > PHYREG_MASK) {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500154 printf("Err..(%s) Invalid register offset\n", __func__);
Chris Packhamf42e5b92018-06-09 20:46:16 +1200155 return -EFAULT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530156 }
157
158 /* wait till the SMI is not busy */
Chris Packhamf42e5b92018-06-09 20:46:16 +1200159 if (smi_wait_ready(dmvgbe) < 0)
160 return -EFAULT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530161
162 /* fill the phy addr and reg offset and write opcode and data */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200163 smi_reg = (data << MVGBE_PHY_SMI_DATA_OFFS);
164 smi_reg |= (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS)
165 | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS);
166 smi_reg &= ~MVGBE_PHY_SMI_OPCODE_READ;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530167
168 /* write the smi register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200169 MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530170
171 return 0;
172}
Chris Packham465f5cf2018-07-09 21:33:59 +1200173
174/*
175 * smi_reg_write - miiphy_write callback function.
176 *
177 * Returns 0 if write succeed, -EFAULT on error
178 */
179static int smi_reg_write(struct mii_dev *bus, int phy_adr, int devad,
180 int reg_ofs, u16 data)
181{
Chris Packham1de16f72018-07-09 21:34:00 +1200182 struct mvgbe_device *dmvgbe = bus->priv;
Chris Packham465f5cf2018-07-09 21:33:59 +1200183
184 return __mvgbe_mdio_write(dmvgbe, phy_adr, devad, reg_ofs, data);
185}
Stefan Bigler96455292012-03-26 00:02:13 +0000186#endif
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530187
188/* Stop and checks all queues */
189static void stop_queue(u32 * qreg)
190{
191 u32 reg_data;
192
193 reg_data = readl(qreg);
194
195 if (reg_data & 0xFF) {
196 /* Issue stop command for active channels only */
197 writel((reg_data << 8), qreg);
198
199 /* Wait for all queue activity to terminate. */
200 do {
201 /*
202 * Check port cause register that all queues
203 * are stopped
204 */
205 reg_data = readl(qreg);
206 }
207 while (reg_data & 0xFF);
208 }
209}
210
211/*
212 * set_access_control - Config address decode parameters for Ethernet unit
213 *
214 * This function configures the address decode parameters for the Gigabit
215 * Ethernet Controller according the given parameters struct.
216 *
217 * @regs Register struct pointer.
218 * @param Address decode parameter struct.
219 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200220static void set_access_control(struct mvgbe_registers *regs,
221 struct mvgbe_winparam *param)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530222{
223 u32 access_prot_reg;
224
225 /* Set access control register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200226 access_prot_reg = MVGBE_REG_RD(regs->epap);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530227 /* clear window permission */
228 access_prot_reg &= (~(3 << (param->win * 2)));
229 access_prot_reg |= (param->access_ctrl << (param->win * 2));
Albert Aribaude91d7d32010-07-12 22:24:28 +0200230 MVGBE_REG_WR(regs->epap, access_prot_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530231
232 /* Set window Size reg (SR) */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200233 MVGBE_REG_WR(regs->barsz[param->win].size,
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530234 (((param->size / 0x10000) - 1) << 16));
235
236 /* Set window Base address reg (BA) */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200237 MVGBE_REG_WR(regs->barsz[param->win].bar,
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530238 (param->target | param->attrib | param->base_addr));
239 /* High address remap reg (HARR) */
240 if (param->win < 4)
Albert Aribaude91d7d32010-07-12 22:24:28 +0200241 MVGBE_REG_WR(regs->ha_remap[param->win], param->high_addr);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530242
243 /* Base address enable reg (BARER) */
244 if (param->enable == 1)
Albert Aribaude91d7d32010-07-12 22:24:28 +0200245 MVGBE_REG_BITS_RESET(regs->bare, (1 << param->win));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530246 else
Albert Aribaude91d7d32010-07-12 22:24:28 +0200247 MVGBE_REG_BITS_SET(regs->bare, (1 << param->win));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530248}
249
Albert Aribaude91d7d32010-07-12 22:24:28 +0200250static void set_dram_access(struct mvgbe_registers *regs)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530251{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200252 struct mvgbe_winparam win_param;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530253 int i;
254
255 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
256 /* Set access parameters for DRAM bank i */
257 win_param.win = i; /* Use Ethernet window i */
258 /* Window target - DDR */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200259 win_param.target = MVGBE_TARGET_DRAM;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530260 /* Enable full access */
261 win_param.access_ctrl = EWIN_ACCESS_FULL;
262 win_param.high_addr = 0;
Albert Aribauda7564072010-07-05 20:15:25 +0200263 /* Get bank base and size */
264 win_param.base_addr = gd->bd->bi_dram[i].start;
265 win_param.size = gd->bd->bi_dram[i].size;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530266 if (win_param.size == 0)
267 win_param.enable = 0;
268 else
269 win_param.enable = 1; /* Enable the access */
270
271 /* Enable DRAM bank */
272 switch (i) {
273 case 0:
274 win_param.attrib = EBAR_DRAM_CS0;
275 break;
276 case 1:
277 win_param.attrib = EBAR_DRAM_CS1;
278 break;
279 case 2:
280 win_param.attrib = EBAR_DRAM_CS2;
281 break;
282 case 3:
283 win_param.attrib = EBAR_DRAM_CS3;
284 break;
285 default:
Albert Aribauda7564072010-07-05 20:15:25 +0200286 /* invalid bank, disable access */
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530287 win_param.enable = 0;
288 win_param.attrib = 0;
289 break;
290 }
291 /* Set the access control for address window(EPAPR) RD/WR */
292 set_access_control(regs, &win_param);
293 }
294}
295
296/*
297 * port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
298 *
299 * Go through all the DA filter tables (Unicast, Special Multicast & Other
300 * Multicast) and set each entry to 0.
301 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200302static void port_init_mac_tables(struct mvgbe_registers *regs)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530303{
304 int table_index;
305
306 /* Clear DA filter unicast table (Ex_dFUT) */
307 for (table_index = 0; table_index < 4; ++table_index)
Albert Aribaude91d7d32010-07-12 22:24:28 +0200308 MVGBE_REG_WR(regs->dfut[table_index], 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530309
310 for (table_index = 0; table_index < 64; ++table_index) {
311 /* Clear DA filter special multicast table (Ex_dFSMT) */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200312 MVGBE_REG_WR(regs->dfsmt[table_index], 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530313 /* Clear DA filter other multicast table (Ex_dFOMT) */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200314 MVGBE_REG_WR(regs->dfomt[table_index], 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530315 }
316}
317
318/*
319 * port_uc_addr - This function Set the port unicast address table
320 *
321 * This function locates the proper entry in the Unicast table for the
322 * specified MAC nibble and sets its properties according to function
323 * parameters.
324 * This function add/removes MAC addresses from the port unicast address
325 * table.
326 *
327 * @uc_nibble Unicast MAC Address last nibble.
328 * @option 0 = Add, 1 = remove address.
329 *
330 * RETURN: 1 if output succeeded. 0 if option parameter is invalid.
331 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200332static int port_uc_addr(struct mvgbe_registers *regs, u8 uc_nibble,
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530333 int option)
334{
335 u32 unicast_reg;
336 u32 tbl_offset;
337 u32 reg_offset;
338
339 /* Locate the Unicast table entry */
340 uc_nibble = (0xf & uc_nibble);
341 /* Register offset from unicast table base */
342 tbl_offset = (uc_nibble / 4);
343 /* Entry offset within the above register */
344 reg_offset = uc_nibble % 4;
345
346 switch (option) {
347 case REJECT_MAC_ADDR:
348 /*
349 * Clear accepts frame bit at specified unicast
350 * DA table entry
351 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200352 unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530353 unicast_reg &= (0xFF << (8 * reg_offset));
Albert Aribaude91d7d32010-07-12 22:24:28 +0200354 MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530355 break;
356 case ACCEPT_MAC_ADDR:
357 /* Set accepts frame bit at unicast DA filter table entry */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200358 unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530359 unicast_reg &= (0xFF << (8 * reg_offset));
360 unicast_reg |= ((0x01 | (RXUQ << 1)) << (8 * reg_offset));
Albert Aribaude91d7d32010-07-12 22:24:28 +0200361 MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530362 break;
363 default:
364 return 0;
365 }
366 return 1;
367}
368
369/*
370 * port_uc_addr_set - This function Set the port Unicast address.
371 */
Chris Packham465f5cf2018-07-09 21:33:59 +1200372static void port_uc_addr_set(struct mvgbe_device *dmvgbe, u8 *p_addr)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530373{
Chris Packham465f5cf2018-07-09 21:33:59 +1200374 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530375 u32 mac_h;
376 u32 mac_l;
377
378 mac_l = (p_addr[4] << 8) | (p_addr[5]);
379 mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
380 (p_addr[3] << 0);
381
Albert Aribaude91d7d32010-07-12 22:24:28 +0200382 MVGBE_REG_WR(regs->macal, mac_l);
383 MVGBE_REG_WR(regs->macah, mac_h);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530384
385 /* Accept frames of this address */
386 port_uc_addr(regs, p_addr[5], ACCEPT_MAC_ADDR);
387}
388
389/*
Albert Aribaude91d7d32010-07-12 22:24:28 +0200390 * mvgbe_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530391 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200392static void mvgbe_init_rx_desc_ring(struct mvgbe_device *dmvgbe)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530393{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200394 struct mvgbe_rxdesc *p_rx_desc;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530395 int i;
396
397 /* initialize the Rx descriptors ring */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200398 p_rx_desc = dmvgbe->p_rxdesc;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530399 for (i = 0; i < RINGSZ; i++) {
400 p_rx_desc->cmd_sts =
Albert Aribaude91d7d32010-07-12 22:24:28 +0200401 MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530402 p_rx_desc->buf_size = PKTSIZE_ALIGN;
403 p_rx_desc->byte_cnt = 0;
Albert Aribaude91d7d32010-07-12 22:24:28 +0200404 p_rx_desc->buf_ptr = dmvgbe->p_rxbuf + i * PKTSIZE_ALIGN;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530405 if (i == (RINGSZ - 1))
Albert Aribaude91d7d32010-07-12 22:24:28 +0200406 p_rx_desc->nxtdesc_p = dmvgbe->p_rxdesc;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530407 else {
Albert Aribaude91d7d32010-07-12 22:24:28 +0200408 p_rx_desc->nxtdesc_p = (struct mvgbe_rxdesc *)
409 ((u32) p_rx_desc + MV_RXQ_DESC_ALIGNED_SIZE);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530410 p_rx_desc = p_rx_desc->nxtdesc_p;
411 }
412 }
Albert Aribaude91d7d32010-07-12 22:24:28 +0200413 dmvgbe->p_rxdesc_curr = dmvgbe->p_rxdesc;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530414}
415
Chris Packham1de16f72018-07-09 21:34:00 +1200416static int __mvgbe_init(struct mvgbe_device *dmvgbe, u8 *enetaddr,
417 const char *name)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530418{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200419 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530420 /* setup RX rings */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200421 mvgbe_init_rx_desc_ring(dmvgbe);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530422
423 /* Clear the ethernet port interrupts */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200424 MVGBE_REG_WR(regs->ic, 0);
425 MVGBE_REG_WR(regs->ice, 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530426 /* Unmask RX buffer and TX end interrupt */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200427 MVGBE_REG_WR(regs->pim, INT_CAUSE_UNMASK_ALL);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530428 /* Unmask phy and link status changes interrupts */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200429 MVGBE_REG_WR(regs->peim, INT_CAUSE_UNMASK_ALL_EXT);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530430
431 set_dram_access(regs);
432 port_init_mac_tables(regs);
Chris Packham1de16f72018-07-09 21:34:00 +1200433 port_uc_addr_set(dmvgbe, enetaddr);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530434
435 /* Assign port configuration and command. */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200436 MVGBE_REG_WR(regs->pxc, PRT_CFG_VAL);
437 MVGBE_REG_WR(regs->pxcx, PORT_CFG_EXTEND_VALUE);
438 MVGBE_REG_WR(regs->psc0, PORT_SERIAL_CONTROL_VALUE);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530439
440 /* Assign port SDMA configuration */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200441 MVGBE_REG_WR(regs->sdc, PORT_SDMA_CFG_VALUE);
442 MVGBE_REG_WR(regs->tqx[0].qxttbc, QTKNBKT_DEF_VAL);
443 MVGBE_REG_WR(regs->tqx[0].tqxtbc,
444 (QMTBS_DEF_VAL << 16) | QTKNRT_DEF_VAL);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530445 /* Turn off the port/RXUQ bandwidth limitation */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200446 MVGBE_REG_WR(regs->pmtu, 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530447
448 /* Set maximum receive buffer to 9700 bytes */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200449 MVGBE_REG_WR(regs->psc0, MVGBE_MAX_RX_PACKET_9700BYTE
450 | (MVGBE_REG_RD(regs->psc0) & MRU_MASK));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530451
Prafulla Wadaskar60ee8a22010-04-06 21:33:08 +0530452 /* Enable port initially */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200453 MVGBE_REG_BITS_SET(regs->psc0, MVGBE_SERIAL_PORT_EN);
Prafulla Wadaskar60ee8a22010-04-06 21:33:08 +0530454
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530455 /*
456 * Set ethernet MTU for leaky bucket mechanism to 0 - this will
457 * disable the leaky bucket mechanism .
458 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200459 MVGBE_REG_WR(regs->pmtu, 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530460
461 /* Assignment of Rx CRDB of given RXUQ */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200462 MVGBE_REG_WR(regs->rxcdp[RXUQ], (u32) dmvgbe->p_rxdesc_curr);
Albert Aribaudcc2b8e32010-07-10 15:41:29 +0200463 /* ensure previous write is done before enabling Rx DMA */
464 isb();
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530465 /* Enable port Rx. */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200466 MVGBE_REG_WR(regs->rqc, (1 << RXUQ));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530467
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530468 return 0;
469}
Chris Packham465f5cf2018-07-09 21:33:59 +1200470
471static void __mvgbe_halt(struct mvgbe_device *dmvgbe)
472{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200473 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530474
475 /* Disable all gigE address decoder */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200476 MVGBE_REG_WR(regs->bare, 0x3f);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530477
478 stop_queue(&regs->tqc);
479 stop_queue(&regs->rqc);
480
Prafulla Wadaskar60ee8a22010-04-06 21:33:08 +0530481 /* Disable port */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200482 MVGBE_REG_BITS_RESET(regs->psc0, MVGBE_SERIAL_PORT_EN);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530483 /* Set port is not reset */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200484 MVGBE_REG_BITS_RESET(regs->psc1, 1 << 4);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530485#ifdef CONFIG_SYS_MII_MODE
486 /* Set MMI interface up */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200487 MVGBE_REG_BITS_RESET(regs->psc1, 1 << 3);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530488#endif
489 /* Disable & mask ethernet port interrupts */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200490 MVGBE_REG_WR(regs->ic, 0);
491 MVGBE_REG_WR(regs->ice, 0);
492 MVGBE_REG_WR(regs->pim, 0);
493 MVGBE_REG_WR(regs->peim, 0);
Chris Packham465f5cf2018-07-09 21:33:59 +1200494}
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530495
Chris Packham1de16f72018-07-09 21:34:00 +1200496static int mvgbe_write_hwaddr(struct udevice *dev)
497{
Simon Glassfa20e932020-12-03 16:55:20 -0700498 struct eth_pdata *pdata = dev_get_plat(dev);
Chris Packham1de16f72018-07-09 21:34:00 +1200499
500 port_uc_addr_set(dev_get_priv(dev), pdata->enetaddr);
501
502 return 0;
503}
Prafulla Wadaskar7dae2eb2010-04-06 22:21:33 +0530504
Chris Packham465f5cf2018-07-09 21:33:59 +1200505static int __mvgbe_send(struct mvgbe_device *dmvgbe, void *dataptr,
506 int datasize)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530507{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200508 struct mvgbe_registers *regs = dmvgbe->regs;
509 struct mvgbe_txdesc *p_txdesc = dmvgbe->p_txdesc;
Simon Kagstrome9220b32009-08-20 10:14:11 +0200510 void *p = (void *)dataptr;
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200511 u32 cmd_sts;
Anatolij Gustschinda42f242011-11-19 08:59:36 +0000512 u32 txuq0_reg_addr;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530513
Simon Kagstrome9220b32009-08-20 10:14:11 +0200514 /* Copy buffer if it's misaligned */
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530515 if ((u32) dataptr & 0x07) {
Simon Kagstrome9220b32009-08-20 10:14:11 +0200516 if (datasize > PKTSIZE_ALIGN) {
517 printf("Non-aligned data too large (%d)\n",
518 datasize);
519 return -1;
520 }
521
Albert Aribaude91d7d32010-07-12 22:24:28 +0200522 memcpy(dmvgbe->p_aligned_txbuf, p, datasize);
523 p = dmvgbe->p_aligned_txbuf;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530524 }
Simon Kagstrome9220b32009-08-20 10:14:11 +0200525
Albert Aribaude91d7d32010-07-12 22:24:28 +0200526 p_txdesc->cmd_sts = MVGBE_ZERO_PADDING | MVGBE_GEN_CRC;
527 p_txdesc->cmd_sts |= MVGBE_TX_FIRST_DESC | MVGBE_TX_LAST_DESC;
528 p_txdesc->cmd_sts |= MVGBE_BUFFER_OWNED_BY_DMA;
529 p_txdesc->cmd_sts |= MVGBE_TX_EN_INTERRUPT;
Simon Kagstrome9220b32009-08-20 10:14:11 +0200530 p_txdesc->buf_ptr = (u8 *) p;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530531 p_txdesc->byte_cnt = datasize;
532
Albert Aribaudcc2b8e32010-07-10 15:41:29 +0200533 /* Set this tc desc as zeroth TXUQ */
Anatolij Gustschinda42f242011-11-19 08:59:36 +0000534 txuq0_reg_addr = (u32)&regs->tcqdp[TXUQ];
535 writel((u32) p_txdesc, txuq0_reg_addr);
Albert Aribaudcc2b8e32010-07-10 15:41:29 +0200536
537 /* ensure tx desc writes above are performed before we start Tx DMA */
538 isb();
539
540 /* Apply send command using zeroth TXUQ */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200541 MVGBE_REG_WR(regs->tqc, (1 << TXUQ));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530542
543 /*
544 * wait for packet xmit completion
545 */
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200546 cmd_sts = readl(&p_txdesc->cmd_sts);
Albert Aribaude91d7d32010-07-12 22:24:28 +0200547 while (cmd_sts & MVGBE_BUFFER_OWNED_BY_DMA) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530548 /* return fail if error is detected */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200549 if ((cmd_sts & (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME)) ==
550 (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME) &&
551 cmd_sts & (MVGBE_UR_ERROR | MVGBE_RL_ERROR)) {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500552 printf("Err..(%s) in xmit packet\n", __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530553 return -1;
554 }
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200555 cmd_sts = readl(&p_txdesc->cmd_sts);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530556 };
557 return 0;
558}
Chris Packham465f5cf2018-07-09 21:33:59 +1200559
560static int __mvgbe_recv(struct mvgbe_device *dmvgbe, uchar **packetp)
561{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200562 struct mvgbe_rxdesc *p_rxdesc_curr = dmvgbe->p_rxdesc_curr;
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200563 u32 cmd_sts;
564 u32 timeout = 0;
Anatolij Gustschinda42f242011-11-19 08:59:36 +0000565 u32 rxdesc_curr_addr;
Chris Packham465f5cf2018-07-09 21:33:59 +1200566 unsigned char *data;
567 int rx_bytes = 0;
568
569 *packetp = NULL;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530570
571 /* wait untill rx packet available or timeout */
572 do {
Albert Aribaude91d7d32010-07-12 22:24:28 +0200573 if (timeout < MVGBE_PHY_SMI_TIMEOUT)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530574 timeout++;
575 else {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500576 debug("%s time out...\n", __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530577 return -1;
578 }
Albert Aribaude91d7d32010-07-12 22:24:28 +0200579 } while (readl(&p_rxdesc_curr->cmd_sts) & MVGBE_BUFFER_OWNED_BY_DMA);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530580
581 if (p_rxdesc_curr->byte_cnt != 0) {
582 debug("%s: Received %d byte Packet @ 0x%x (cmd_sts= %08x)\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -0500583 __func__, (u32) p_rxdesc_curr->byte_cnt,
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530584 (u32) p_rxdesc_curr->buf_ptr,
585 (u32) p_rxdesc_curr->cmd_sts);
586 }
587
588 /*
589 * In case received a packet without first/last bits on
590 * OR the error summary bit is on,
591 * the packets needs to be dropeed.
592 */
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200593 cmd_sts = readl(&p_rxdesc_curr->cmd_sts);
594
595 if ((cmd_sts &
Albert Aribaude91d7d32010-07-12 22:24:28 +0200596 (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC))
597 != (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC)) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530598
599 printf("Err..(%s) Dropping packet spread on"
Joe Hershberger9f09a362015-04-08 01:41:06 -0500600 " multiple descriptors\n", __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530601
Albert Aribaude91d7d32010-07-12 22:24:28 +0200602 } else if (cmd_sts & MVGBE_ERROR_SUMMARY) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530603
604 printf("Err..(%s) Dropping packet with errors\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -0500605 __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530606
607 } else {
608 /* !!! call higher layer processing */
609 debug("%s: Sending Received packet to"
Joe Hershberger9f09a362015-04-08 01:41:06 -0500610 " upper layer (net_process_received_packet)\n",
611 __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530612
Chris Packham465f5cf2018-07-09 21:33:59 +1200613 data = (p_rxdesc_curr->buf_ptr + RX_BUF_OFFSET);
614 rx_bytes = (int)(p_rxdesc_curr->byte_cnt -
615 RX_BUF_OFFSET);
616
617 *packetp = data;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530618 }
619 /*
620 * free these descriptors and point next in the ring
621 */
622 p_rxdesc_curr->cmd_sts =
Albert Aribaude91d7d32010-07-12 22:24:28 +0200623 MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530624 p_rxdesc_curr->buf_size = PKTSIZE_ALIGN;
625 p_rxdesc_curr->byte_cnt = 0;
626
Anatolij Gustschinda42f242011-11-19 08:59:36 +0000627 rxdesc_curr_addr = (u32)&dmvgbe->p_rxdesc_curr;
628 writel((unsigned)p_rxdesc_curr->nxtdesc_p, rxdesc_curr_addr);
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200629
Chris Packham465f5cf2018-07-09 21:33:59 +1200630 return rx_bytes;
631}
632
Tom Rini5b280922022-11-27 10:25:17 -0500633#if defined(CONFIG_PHYLIB)
Chris Packham1de16f72018-07-09 21:34:00 +1200634static struct phy_device *__mvgbe_phy_init(struct udevice *dev,
635 struct mii_dev *bus,
636 phy_interface_t phy_interface,
637 int phyid)
Chris Packham1de16f72018-07-09 21:34:00 +1200638{
639 struct phy_device *phydev;
640
641 /* Set phy address of the port */
642 miiphy_write(dev->name, MV_PHY_ADR_REQUEST, MV_PHY_ADR_REQUEST,
643 phyid);
644
Tony Dinh77943da2022-04-12 13:18:19 -0700645 /* Make sure the selected PHY page is 0 before connecting */
646 miiphy_write(dev->name, phyid, MVGBE_PGADR_REG, 0);
647
Chris Packham1de16f72018-07-09 21:34:00 +1200648 phydev = phy_connect(bus, phyid, dev, phy_interface);
649 if (!phydev) {
650 printf("phy_connect failed\n");
651 return NULL;
652 }
653
654 phy_config(phydev);
655 phy_startup(phydev);
656
657 return phydev;
658}
Tom Rini5b280922022-11-27 10:25:17 -0500659#endif /* CONFIG_PHYLIB */
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100660
Chris Packham1de16f72018-07-09 21:34:00 +1200661static int mvgbe_alloc_buffers(struct mvgbe_device *dmvgbe)
662{
663 dmvgbe->p_rxdesc = memalign(PKTALIGN,
664 MV_RXQ_DESC_ALIGNED_SIZE * RINGSZ + 1);
665 if (!dmvgbe->p_rxdesc)
666 goto error1;
667
668 dmvgbe->p_rxbuf = memalign(PKTALIGN,
669 RINGSZ * PKTSIZE_ALIGN + 1);
670 if (!dmvgbe->p_rxbuf)
671 goto error2;
672
673 dmvgbe->p_aligned_txbuf = memalign(8, PKTSIZE_ALIGN);
674 if (!dmvgbe->p_aligned_txbuf)
675 goto error3;
676
677 dmvgbe->p_txdesc = memalign(PKTALIGN, sizeof(struct mvgbe_txdesc) + 1);
678 if (!dmvgbe->p_txdesc)
679 goto error4;
680
681 return 0;
682
683error4:
684 free(dmvgbe->p_aligned_txbuf);
685error3:
686 free(dmvgbe->p_rxbuf);
687error2:
688 free(dmvgbe->p_rxdesc);
689error1:
690 return -ENOMEM;
691}
692
Chris Packham1de16f72018-07-09 21:34:00 +1200693static int mvgbe_port_is_fixed_link(struct mvgbe_device *dmvgbe)
694{
695 return dmvgbe->phyaddr > PHY_MAX_ADDR;
696}
697
698static int mvgbe_start(struct udevice *dev)
699{
Simon Glassfa20e932020-12-03 16:55:20 -0700700 struct eth_pdata *pdata = dev_get_plat(dev);
Chris Packham1de16f72018-07-09 21:34:00 +1200701 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
702 int ret;
703
704 ret = __mvgbe_init(dmvgbe, pdata->enetaddr, dev->name);
705 if (ret)
706 return ret;
707
708 if (!mvgbe_port_is_fixed_link(dmvgbe)) {
709 dmvgbe->phydev = __mvgbe_phy_init(dev, dmvgbe->bus,
710 dmvgbe->phy_interface,
711 dmvgbe->phyaddr);
712 if (!dmvgbe->phydev)
713 return -ENODEV;
714 }
715
716 return 0;
717}
718
719static int mvgbe_send(struct udevice *dev, void *packet, int length)
720{
721 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
722
723 return __mvgbe_send(dmvgbe, packet, length);
724}
725
726static int mvgbe_recv(struct udevice *dev, int flags, uchar **packetp)
727{
728 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
729
730 return __mvgbe_recv(dmvgbe, packetp);
731}
732
733static void mvgbe_stop(struct udevice *dev)
734{
735 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
736
737 __mvgbe_halt(dmvgbe);
738}
739
740static int mvgbe_probe(struct udevice *dev)
741{
Simon Glassfa20e932020-12-03 16:55:20 -0700742 struct eth_pdata *pdata = dev_get_plat(dev);
Chris Packham1de16f72018-07-09 21:34:00 +1200743 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
744 struct mii_dev *bus;
745 int ret;
746
747 ret = mvgbe_alloc_buffers(dmvgbe);
748 if (ret)
749 return ret;
750
751 dmvgbe->regs = (void __iomem *)pdata->iobase;
752
753 bus = mdio_alloc();
754 if (!bus) {
755 printf("Failed to allocate MDIO bus\n");
756 return -ENOMEM;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530757 }
Chris Packham1de16f72018-07-09 21:34:00 +1200758
759 bus->read = smi_reg_read;
760 bus->write = smi_reg_write;
761 snprintf(bus->name, sizeof(bus->name), dev->name);
762 bus->priv = dmvgbe;
763 dmvgbe->bus = bus;
764
765 ret = mdio_register(bus);
766 if (ret < 0)
767 return ret;
768
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530769 return 0;
Prafulla Wadaskar12618ef2009-07-01 20:34:51 +0200770}
Chris Packham1de16f72018-07-09 21:34:00 +1200771
772static const struct eth_ops mvgbe_ops = {
773 .start = mvgbe_start,
774 .send = mvgbe_send,
775 .recv = mvgbe_recv,
776 .stop = mvgbe_stop,
777 .write_hwaddr = mvgbe_write_hwaddr,
778};
779
Simon Glassaad29ae2020-12-03 16:55:21 -0700780static int mvgbe_of_to_plat(struct udevice *dev)
Chris Packham1de16f72018-07-09 21:34:00 +1200781{
Simon Glassfa20e932020-12-03 16:55:20 -0700782 struct eth_pdata *pdata = dev_get_plat(dev);
Chris Packham1de16f72018-07-09 21:34:00 +1200783 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
784 void *blob = (void *)gd->fdt_blob;
785 int node = dev_of_offset(dev);
Chris Packham1de16f72018-07-09 21:34:00 +1200786 int fl_node;
787 int pnode;
788 unsigned long addr;
789
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900790 pdata->iobase = dev_read_addr(dev);
Chris Packham1de16f72018-07-09 21:34:00 +1200791 pdata->phy_interface = -1;
792
793 pnode = fdt_node_offset_by_compatible(blob, node,
794 "marvell,kirkwood-eth-port");
795
796 /* Get phy-mode / phy_interface from DT */
Marek BehĂșnbc194772022-04-07 00:33:01 +0200797 pdata->phy_interface = dev_read_phy_mode(dev);
Marek BehĂșn48631e42022-04-07 00:33:03 +0200798 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
Chris Packham7ca5e692018-12-04 19:54:30 +1300799 pdata->phy_interface = PHY_INTERFACE_MODE_GMII;
Chris Packham1de16f72018-07-09 21:34:00 +1200800
801 dmvgbe->phy_interface = pdata->phy_interface;
802
803 /* fetch 'fixed-link' property */
804 fl_node = fdt_subnode_offset(blob, pnode, "fixed-link");
805 if (fl_node != -FDT_ERR_NOTFOUND) {
806 /* set phy_addr to invalid value for fixed link */
807 dmvgbe->phyaddr = PHY_MAX_ADDR + 1;
808 dmvgbe->duplex = fdtdec_get_bool(blob, fl_node, "full-duplex");
809 dmvgbe->speed = fdtdec_get_int(blob, fl_node, "speed", 0);
810 } else {
811 /* Now read phyaddr from DT */
812 addr = fdtdec_lookup_phandle(blob, pnode, "phy-handle");
813 if (addr > 0)
814 dmvgbe->phyaddr = fdtdec_get_int(blob, addr, "reg", 0);
815 }
816
817 return 0;
818}
819
820static const struct udevice_id mvgbe_ids[] = {
821 { .compatible = "marvell,kirkwood-eth" },
822 { }
823};
824
825U_BOOT_DRIVER(mvgbe) = {
826 .name = "mvgbe",
827 .id = UCLASS_ETH,
828 .of_match = mvgbe_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700829 .of_to_plat = mvgbe_of_to_plat,
Chris Packham1de16f72018-07-09 21:34:00 +1200830 .probe = mvgbe_probe,
831 .ops = &mvgbe_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700832 .priv_auto = sizeof(struct mvgbe_device),
Simon Glass71fa5b42020-12-03 16:55:18 -0700833 .plat_auto = sizeof(struct eth_pdata),
Chris Packham1de16f72018-07-09 21:34:00 +1200834};