blob: a77c057432626521d2e75762d5460c0babcd97da [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Prafulla Wadaskar91315892009-06-14 22:33:46 +05302/*
3 * (C) Copyright 2009
4 * Marvell Semiconductor <www.marvell.com>
5 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
6 *
7 * (C) Copyright 2003
8 * Ingo Assmus <ingo.assmus@keymile.com>
9 *
10 * based on - Driver for MV64360X ethernet ports
11 * Copyright (C) 2002 rabeeh@galileo.co.il
Prafulla Wadaskar91315892009-06-14 22:33:46 +053012 */
13
14#include <common.h>
Chris Packhamfb731072018-07-09 21:34:00 +120015#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060016#include <log.h>
Prafulla Wadaskar91315892009-06-14 22:33:46 +053017#include <net.h>
18#include <malloc.h>
19#include <miiphy.h>
Chris Packham5194ed72018-06-09 20:46:16 +120020#include <wait_bit.h>
Simon Glass401d1c42020-10-30 21:38:53 -060021#include <asm/global_data.h>
Lei Wena7efd712011-10-18 20:11:42 +053022#include <asm/io.h>
Simon Glassc05ed002020-05-10 11:40:11 -060023#include <linux/delay.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090024#include <linux/errno.h>
Prafulla Wadaskar91315892009-06-14 22:33:46 +053025#include <asm/types.h>
Lei Wena7efd712011-10-18 20:11:42 +053026#include <asm/system.h>
Prafulla Wadaskar91315892009-06-14 22:33:46 +053027#include <asm/byteorder.h>
Anatolij Gustschin36aaa912011-10-29 10:09:22 +000028#include <asm/arch/cpu.h>
Albert Aribaudd44265a2010-07-12 22:24:28 +020029
Trevor Woernerbb0fb4c2020-05-06 08:02:40 -040030#if defined(CONFIG_ARCH_KIRKWOOD)
Stefan Roese3dc23f72014-10-22 12:13:06 +020031#include <asm/arch/soc.h>
Trevor Woernerb16a3312020-05-06 08:02:38 -040032#elif defined(CONFIG_ARCH_ORION5X)
Albert Aribaudd3c9ffd2010-07-12 22:24:29 +020033#include <asm/arch/orion5x.h>
Albert Aribaudd44265a2010-07-12 22:24:28 +020034#endif
35
Albert Aribaud9b6bcdc2010-07-12 22:24:27 +020036#include "mvgbe.h"
Prafulla Wadaskar91315892009-06-14 22:33:46 +053037
Albert Aribaud49fa6ed2010-07-05 20:15:25 +020038DECLARE_GLOBAL_DATA_PTR;
39
Luka Perkov5aa22972013-11-11 07:27:53 +010040#ifndef CONFIG_MVGBE_PORTS
41# define CONFIG_MVGBE_PORTS {0, 0}
42#endif
43
Albert Aribaudd44265a2010-07-12 22:24:28 +020044#define MV_PHY_ADR_REQUEST 0xee
45#define MVGBE_SMI_REG (((struct mvgbe_registers *)MVGBE0_BASE)->smi)
Tony Dinhf0f98752022-04-12 13:18:19 -070046#define MVGBE_PGADR_REG 22
Simon Kagstrombb1ca3b2009-08-20 10:12:28 +020047
Sebastian Hesselbarthcd3ca3f2012-12-04 09:32:00 +010048#if defined(CONFIG_PHYLIB) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Chris Packham5194ed72018-06-09 20:46:16 +120049static int smi_wait_ready(struct mvgbe_device *dmvgbe)
50{
51 int ret;
52
53 ret = wait_for_bit_le32(&MVGBE_SMI_REG, MVGBE_PHY_SMI_BUSY_MASK, false,
54 MVGBE_PHY_SMI_TIMEOUT_MS, false);
55 if (ret) {
56 printf("Error: SMI busy timeout\n");
57 return ret;
58 }
59
60 return 0;
61}
62
Chris Packhame9bf75c2018-07-09 21:33:59 +120063static int __mvgbe_mdio_read(struct mvgbe_device *dmvgbe, int phy_adr,
64 int devad, int reg_ofs)
Prafulla Wadaskar91315892009-06-14 22:33:46 +053065{
Albert Aribaudd44265a2010-07-12 22:24:28 +020066 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskar91315892009-06-14 22:33:46 +053067 u32 smi_reg;
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +020068 u32 timeout;
Chris Packhame9bf75c2018-07-09 21:33:59 +120069 u16 data = 0;
Prafulla Wadaskar91315892009-06-14 22:33:46 +053070
71 /* Phyadr read request */
Albert Aribaudd44265a2010-07-12 22:24:28 +020072 if (phy_adr == MV_PHY_ADR_REQUEST &&
73 reg_ofs == MV_PHY_ADR_REQUEST) {
Prafulla Wadaskar91315892009-06-14 22:33:46 +053074 /* */
Joe Hershberger5a49f172016-08-08 11:28:38 -050075 data = (u16) (MVGBE_REG_RD(regs->phyadr) & PHYADR_MASK);
76 return data;
Prafulla Wadaskar91315892009-06-14 22:33:46 +053077 }
78 /* check parameters */
79 if (phy_adr > PHYADR_MASK) {
80 printf("Err..(%s) Invalid PHY address %d\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -050081 __func__, phy_adr);
Prafulla Wadaskar91315892009-06-14 22:33:46 +053082 return -EFAULT;
83 }
84 if (reg_ofs > PHYREG_MASK) {
85 printf("Err..(%s) Invalid register offset %d\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -050086 __func__, reg_ofs);
Prafulla Wadaskar91315892009-06-14 22:33:46 +053087 return -EFAULT;
88 }
89
Prafulla Wadaskar91315892009-06-14 22:33:46 +053090 /* wait till the SMI is not busy */
Chris Packham5194ed72018-06-09 20:46:16 +120091 if (smi_wait_ready(dmvgbe) < 0)
92 return -EFAULT;
Prafulla Wadaskar91315892009-06-14 22:33:46 +053093
94 /* fill the phy address and regiser offset and read opcode */
Albert Aribaudd44265a2010-07-12 22:24:28 +020095 smi_reg = (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS)
96 | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS)
97 | MVGBE_PHY_SMI_OPCODE_READ;
Prafulla Wadaskar91315892009-06-14 22:33:46 +053098
99 /* write the smi register */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200100 MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530101
102 /*wait till read value is ready */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200103 timeout = MVGBE_PHY_SMI_TIMEOUT;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530104
105 do {
106 /* read smi register */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200107 smi_reg = MVGBE_REG_RD(MVGBE_SMI_REG);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530108 if (timeout-- == 0) {
109 printf("Err..(%s) SMI read ready timeout\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500110 __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530111 return -EFAULT;
112 }
Albert Aribaudd44265a2010-07-12 22:24:28 +0200113 } while (!(smi_reg & MVGBE_PHY_SMI_READ_VALID_MASK));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530114
115 /* Wait for the data to update in the SMI register */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200116 for (timeout = 0; timeout < MVGBE_PHY_SMI_TIMEOUT; timeout++)
117 ;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530118
Joe Hershberger5a49f172016-08-08 11:28:38 -0500119 data = (u16) (MVGBE_REG_RD(MVGBE_SMI_REG) & MVGBE_PHY_SMI_DATA_MASK);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530120
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500121 debug("%s:(adr %d, off %d) value= %04x\n", __func__, phy_adr, reg_ofs,
Joe Hershberger5a49f172016-08-08 11:28:38 -0500122 data);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530123
Joe Hershberger5a49f172016-08-08 11:28:38 -0500124 return data;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530125}
126
127/*
Chris Packhame9bf75c2018-07-09 21:33:59 +1200128 * smi_reg_read - miiphy_read callback function.
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530129 *
Chris Packhame9bf75c2018-07-09 21:33:59 +1200130 * Returns 16bit phy register value, or -EFAULT on error
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530131 */
Chris Packhame9bf75c2018-07-09 21:33:59 +1200132static int smi_reg_read(struct mii_dev *bus, int phy_adr, int devad,
133 int reg_ofs)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530134{
Chris Packhamfb731072018-07-09 21:34:00 +1200135#ifdef CONFIG_DM_ETH
136 struct mvgbe_device *dmvgbe = bus->priv;
137#else
Joe Hershberger5a49f172016-08-08 11:28:38 -0500138 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Albert Aribaudd44265a2010-07-12 22:24:28 +0200139 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Chris Packhamfb731072018-07-09 21:34:00 +1200140#endif
Chris Packhame9bf75c2018-07-09 21:33:59 +1200141
142 return __mvgbe_mdio_read(dmvgbe, phy_adr, devad, reg_ofs);
143}
144
145static int __mvgbe_mdio_write(struct mvgbe_device *dmvgbe, int phy_adr,
146 int devad, int reg_ofs, u16 data)
147{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200148 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530149 u32 smi_reg;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530150
151 /* Phyadr write request*/
Albert Aribaudd44265a2010-07-12 22:24:28 +0200152 if (phy_adr == MV_PHY_ADR_REQUEST &&
153 reg_ofs == MV_PHY_ADR_REQUEST) {
154 MVGBE_REG_WR(regs->phyadr, data);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530155 return 0;
156 }
157
158 /* check parameters */
159 if (phy_adr > PHYADR_MASK) {
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500160 printf("Err..(%s) Invalid phy address\n", __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530161 return -EINVAL;
162 }
163 if (reg_ofs > PHYREG_MASK) {
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500164 printf("Err..(%s) Invalid register offset\n", __func__);
Chris Packham5194ed72018-06-09 20:46:16 +1200165 return -EFAULT;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530166 }
167
168 /* wait till the SMI is not busy */
Chris Packham5194ed72018-06-09 20:46:16 +1200169 if (smi_wait_ready(dmvgbe) < 0)
170 return -EFAULT;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530171
172 /* fill the phy addr and reg offset and write opcode and data */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200173 smi_reg = (data << MVGBE_PHY_SMI_DATA_OFFS);
174 smi_reg |= (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS)
175 | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS);
176 smi_reg &= ~MVGBE_PHY_SMI_OPCODE_READ;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530177
178 /* write the smi register */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200179 MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530180
181 return 0;
182}
Chris Packhame9bf75c2018-07-09 21:33:59 +1200183
184/*
185 * smi_reg_write - miiphy_write callback function.
186 *
187 * Returns 0 if write succeed, -EFAULT on error
188 */
189static int smi_reg_write(struct mii_dev *bus, int phy_adr, int devad,
190 int reg_ofs, u16 data)
191{
Chris Packhamfb731072018-07-09 21:34:00 +1200192#ifdef CONFIG_DM_ETH
193 struct mvgbe_device *dmvgbe = bus->priv;
194#else
Chris Packhame9bf75c2018-07-09 21:33:59 +1200195 struct eth_device *dev = eth_get_dev_by_name(bus->name);
196 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Chris Packhamfb731072018-07-09 21:34:00 +1200197#endif
Chris Packhame9bf75c2018-07-09 21:33:59 +1200198
199 return __mvgbe_mdio_write(dmvgbe, phy_adr, devad, reg_ofs, data);
200}
Stefan Biglercc796972012-03-26 00:02:13 +0000201#endif
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530202
203/* Stop and checks all queues */
204static void stop_queue(u32 * qreg)
205{
206 u32 reg_data;
207
208 reg_data = readl(qreg);
209
210 if (reg_data & 0xFF) {
211 /* Issue stop command for active channels only */
212 writel((reg_data << 8), qreg);
213
214 /* Wait for all queue activity to terminate. */
215 do {
216 /*
217 * Check port cause register that all queues
218 * are stopped
219 */
220 reg_data = readl(qreg);
221 }
222 while (reg_data & 0xFF);
223 }
224}
225
226/*
227 * set_access_control - Config address decode parameters for Ethernet unit
228 *
229 * This function configures the address decode parameters for the Gigabit
230 * Ethernet Controller according the given parameters struct.
231 *
232 * @regs Register struct pointer.
233 * @param Address decode parameter struct.
234 */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200235static void set_access_control(struct mvgbe_registers *regs,
236 struct mvgbe_winparam *param)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530237{
238 u32 access_prot_reg;
239
240 /* Set access control register */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200241 access_prot_reg = MVGBE_REG_RD(regs->epap);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530242 /* clear window permission */
243 access_prot_reg &= (~(3 << (param->win * 2)));
244 access_prot_reg |= (param->access_ctrl << (param->win * 2));
Albert Aribaudd44265a2010-07-12 22:24:28 +0200245 MVGBE_REG_WR(regs->epap, access_prot_reg);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530246
247 /* Set window Size reg (SR) */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200248 MVGBE_REG_WR(regs->barsz[param->win].size,
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530249 (((param->size / 0x10000) - 1) << 16));
250
251 /* Set window Base address reg (BA) */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200252 MVGBE_REG_WR(regs->barsz[param->win].bar,
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530253 (param->target | param->attrib | param->base_addr));
254 /* High address remap reg (HARR) */
255 if (param->win < 4)
Albert Aribaudd44265a2010-07-12 22:24:28 +0200256 MVGBE_REG_WR(regs->ha_remap[param->win], param->high_addr);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530257
258 /* Base address enable reg (BARER) */
259 if (param->enable == 1)
Albert Aribaudd44265a2010-07-12 22:24:28 +0200260 MVGBE_REG_BITS_RESET(regs->bare, (1 << param->win));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530261 else
Albert Aribaudd44265a2010-07-12 22:24:28 +0200262 MVGBE_REG_BITS_SET(regs->bare, (1 << param->win));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530263}
264
Albert Aribaudd44265a2010-07-12 22:24:28 +0200265static void set_dram_access(struct mvgbe_registers *regs)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530266{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200267 struct mvgbe_winparam win_param;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530268 int i;
269
270 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
271 /* Set access parameters for DRAM bank i */
272 win_param.win = i; /* Use Ethernet window i */
273 /* Window target - DDR */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200274 win_param.target = MVGBE_TARGET_DRAM;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530275 /* Enable full access */
276 win_param.access_ctrl = EWIN_ACCESS_FULL;
277 win_param.high_addr = 0;
Albert Aribaud49fa6ed2010-07-05 20:15:25 +0200278 /* Get bank base and size */
279 win_param.base_addr = gd->bd->bi_dram[i].start;
280 win_param.size = gd->bd->bi_dram[i].size;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530281 if (win_param.size == 0)
282 win_param.enable = 0;
283 else
284 win_param.enable = 1; /* Enable the access */
285
286 /* Enable DRAM bank */
287 switch (i) {
288 case 0:
289 win_param.attrib = EBAR_DRAM_CS0;
290 break;
291 case 1:
292 win_param.attrib = EBAR_DRAM_CS1;
293 break;
294 case 2:
295 win_param.attrib = EBAR_DRAM_CS2;
296 break;
297 case 3:
298 win_param.attrib = EBAR_DRAM_CS3;
299 break;
300 default:
Albert Aribaud49fa6ed2010-07-05 20:15:25 +0200301 /* invalid bank, disable access */
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530302 win_param.enable = 0;
303 win_param.attrib = 0;
304 break;
305 }
306 /* Set the access control for address window(EPAPR) RD/WR */
307 set_access_control(regs, &win_param);
308 }
309}
310
311/*
312 * port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
313 *
314 * Go through all the DA filter tables (Unicast, Special Multicast & Other
315 * Multicast) and set each entry to 0.
316 */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200317static void port_init_mac_tables(struct mvgbe_registers *regs)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530318{
319 int table_index;
320
321 /* Clear DA filter unicast table (Ex_dFUT) */
322 for (table_index = 0; table_index < 4; ++table_index)
Albert Aribaudd44265a2010-07-12 22:24:28 +0200323 MVGBE_REG_WR(regs->dfut[table_index], 0);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530324
325 for (table_index = 0; table_index < 64; ++table_index) {
326 /* Clear DA filter special multicast table (Ex_dFSMT) */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200327 MVGBE_REG_WR(regs->dfsmt[table_index], 0);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530328 /* Clear DA filter other multicast table (Ex_dFOMT) */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200329 MVGBE_REG_WR(regs->dfomt[table_index], 0);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530330 }
331}
332
333/*
334 * port_uc_addr - This function Set the port unicast address table
335 *
336 * This function locates the proper entry in the Unicast table for the
337 * specified MAC nibble and sets its properties according to function
338 * parameters.
339 * This function add/removes MAC addresses from the port unicast address
340 * table.
341 *
342 * @uc_nibble Unicast MAC Address last nibble.
343 * @option 0 = Add, 1 = remove address.
344 *
345 * RETURN: 1 if output succeeded. 0 if option parameter is invalid.
346 */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200347static int port_uc_addr(struct mvgbe_registers *regs, u8 uc_nibble,
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530348 int option)
349{
350 u32 unicast_reg;
351 u32 tbl_offset;
352 u32 reg_offset;
353
354 /* Locate the Unicast table entry */
355 uc_nibble = (0xf & uc_nibble);
356 /* Register offset from unicast table base */
357 tbl_offset = (uc_nibble / 4);
358 /* Entry offset within the above register */
359 reg_offset = uc_nibble % 4;
360
361 switch (option) {
362 case REJECT_MAC_ADDR:
363 /*
364 * Clear accepts frame bit at specified unicast
365 * DA table entry
366 */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200367 unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530368 unicast_reg &= (0xFF << (8 * reg_offset));
Albert Aribaudd44265a2010-07-12 22:24:28 +0200369 MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530370 break;
371 case ACCEPT_MAC_ADDR:
372 /* Set accepts frame bit at unicast DA filter table entry */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200373 unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530374 unicast_reg &= (0xFF << (8 * reg_offset));
375 unicast_reg |= ((0x01 | (RXUQ << 1)) << (8 * reg_offset));
Albert Aribaudd44265a2010-07-12 22:24:28 +0200376 MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530377 break;
378 default:
379 return 0;
380 }
381 return 1;
382}
383
384/*
385 * port_uc_addr_set - This function Set the port Unicast address.
386 */
Chris Packhame9bf75c2018-07-09 21:33:59 +1200387static void port_uc_addr_set(struct mvgbe_device *dmvgbe, u8 *p_addr)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530388{
Chris Packhame9bf75c2018-07-09 21:33:59 +1200389 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530390 u32 mac_h;
391 u32 mac_l;
392
393 mac_l = (p_addr[4] << 8) | (p_addr[5]);
394 mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
395 (p_addr[3] << 0);
396
Albert Aribaudd44265a2010-07-12 22:24:28 +0200397 MVGBE_REG_WR(regs->macal, mac_l);
398 MVGBE_REG_WR(regs->macah, mac_h);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530399
400 /* Accept frames of this address */
401 port_uc_addr(regs, p_addr[5], ACCEPT_MAC_ADDR);
402}
403
404/*
Albert Aribaudd44265a2010-07-12 22:24:28 +0200405 * mvgbe_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530406 */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200407static void mvgbe_init_rx_desc_ring(struct mvgbe_device *dmvgbe)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530408{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200409 struct mvgbe_rxdesc *p_rx_desc;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530410 int i;
411
412 /* initialize the Rx descriptors ring */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200413 p_rx_desc = dmvgbe->p_rxdesc;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530414 for (i = 0; i < RINGSZ; i++) {
415 p_rx_desc->cmd_sts =
Albert Aribaudd44265a2010-07-12 22:24:28 +0200416 MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530417 p_rx_desc->buf_size = PKTSIZE_ALIGN;
418 p_rx_desc->byte_cnt = 0;
Albert Aribaudd44265a2010-07-12 22:24:28 +0200419 p_rx_desc->buf_ptr = dmvgbe->p_rxbuf + i * PKTSIZE_ALIGN;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530420 if (i == (RINGSZ - 1))
Albert Aribaudd44265a2010-07-12 22:24:28 +0200421 p_rx_desc->nxtdesc_p = dmvgbe->p_rxdesc;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530422 else {
Albert Aribaudd44265a2010-07-12 22:24:28 +0200423 p_rx_desc->nxtdesc_p = (struct mvgbe_rxdesc *)
424 ((u32) p_rx_desc + MV_RXQ_DESC_ALIGNED_SIZE);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530425 p_rx_desc = p_rx_desc->nxtdesc_p;
426 }
427 }
Albert Aribaudd44265a2010-07-12 22:24:28 +0200428 dmvgbe->p_rxdesc_curr = dmvgbe->p_rxdesc;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530429}
430
Chris Packhamfb731072018-07-09 21:34:00 +1200431static int __mvgbe_init(struct mvgbe_device *dmvgbe, u8 *enetaddr,
432 const char *name)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530433{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200434 struct mvgbe_registers *regs = dmvgbe->regs;
Sascha Silbe0611c602013-08-11 17:08:23 +0200435#if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \
436 !defined(CONFIG_PHYLIB) && \
Chris Packhamfb731072018-07-09 21:34:00 +1200437 !defined(CONFIG_DM_ETH) && \
Sascha Silbe0611c602013-08-11 17:08:23 +0200438 defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)
Simon Kagstromcad713b2009-08-20 10:13:06 +0200439 int i;
Prafulla Wadaskaraba82372009-09-09 15:59:19 +0530440#endif
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530441 /* setup RX rings */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200442 mvgbe_init_rx_desc_ring(dmvgbe);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530443
444 /* Clear the ethernet port interrupts */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200445 MVGBE_REG_WR(regs->ic, 0);
446 MVGBE_REG_WR(regs->ice, 0);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530447 /* Unmask RX buffer and TX end interrupt */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200448 MVGBE_REG_WR(regs->pim, INT_CAUSE_UNMASK_ALL);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530449 /* Unmask phy and link status changes interrupts */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200450 MVGBE_REG_WR(regs->peim, INT_CAUSE_UNMASK_ALL_EXT);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530451
452 set_dram_access(regs);
453 port_init_mac_tables(regs);
Chris Packhamfb731072018-07-09 21:34:00 +1200454 port_uc_addr_set(dmvgbe, enetaddr);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530455
456 /* Assign port configuration and command. */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200457 MVGBE_REG_WR(regs->pxc, PRT_CFG_VAL);
458 MVGBE_REG_WR(regs->pxcx, PORT_CFG_EXTEND_VALUE);
459 MVGBE_REG_WR(regs->psc0, PORT_SERIAL_CONTROL_VALUE);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530460
461 /* Assign port SDMA configuration */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200462 MVGBE_REG_WR(regs->sdc, PORT_SDMA_CFG_VALUE);
463 MVGBE_REG_WR(regs->tqx[0].qxttbc, QTKNBKT_DEF_VAL);
464 MVGBE_REG_WR(regs->tqx[0].tqxtbc,
465 (QMTBS_DEF_VAL << 16) | QTKNRT_DEF_VAL);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530466 /* Turn off the port/RXUQ bandwidth limitation */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200467 MVGBE_REG_WR(regs->pmtu, 0);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530468
469 /* Set maximum receive buffer to 9700 bytes */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200470 MVGBE_REG_WR(regs->psc0, MVGBE_MAX_RX_PACKET_9700BYTE
471 | (MVGBE_REG_RD(regs->psc0) & MRU_MASK));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530472
Prafulla Wadaskarf0588fd2010-04-06 21:33:08 +0530473 /* Enable port initially */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200474 MVGBE_REG_BITS_SET(regs->psc0, MVGBE_SERIAL_PORT_EN);
Prafulla Wadaskarf0588fd2010-04-06 21:33:08 +0530475
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530476 /*
477 * Set ethernet MTU for leaky bucket mechanism to 0 - this will
478 * disable the leaky bucket mechanism .
479 */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200480 MVGBE_REG_WR(regs->pmtu, 0);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530481
482 /* Assignment of Rx CRDB of given RXUQ */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200483 MVGBE_REG_WR(regs->rxcdp[RXUQ], (u32) dmvgbe->p_rxdesc_curr);
Albert Aribaudc19a20d2010-07-10 15:41:29 +0200484 /* ensure previous write is done before enabling Rx DMA */
485 isb();
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530486 /* Enable port Rx. */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200487 MVGBE_REG_WR(regs->rqc, (1 << RXUQ));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530488
Sebastian Hesselbarthcd3ca3f2012-12-04 09:32:00 +0100489#if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \
490 !defined(CONFIG_PHYLIB) && \
Chris Packhamfb731072018-07-09 21:34:00 +1200491 !defined(CONFIG_DM_ETH) && \
Sebastian Hesselbarthcd3ca3f2012-12-04 09:32:00 +0100492 defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)
Simon Kagstromcad713b2009-08-20 10:13:06 +0200493 /* Wait up to 5s for the link status */
494 for (i = 0; i < 5; i++) {
495 u16 phyadr;
496
Chris Packhamfb731072018-07-09 21:34:00 +1200497 miiphy_read(name, MV_PHY_ADR_REQUEST,
Albert Aribaudd44265a2010-07-12 22:24:28 +0200498 MV_PHY_ADR_REQUEST, &phyadr);
Simon Kagstromcad713b2009-08-20 10:13:06 +0200499 /* Return if we get link up */
Chris Packhamfb731072018-07-09 21:34:00 +1200500 if (miiphy_link(name, phyadr))
Simon Kagstromcad713b2009-08-20 10:13:06 +0200501 return 0;
502 udelay(1000000);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530503 }
Simon Kagstromcad713b2009-08-20 10:13:06 +0200504
Chris Packhamfb731072018-07-09 21:34:00 +1200505 printf("No link on %s\n", name);
Simon Kagstromcad713b2009-08-20 10:13:06 +0200506 return -1;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530507#endif
508 return 0;
509}
510
Chris Packhamfb731072018-07-09 21:34:00 +1200511#ifndef CONFIG_DM_ETH
Chris Packhame9bf75c2018-07-09 21:33:59 +1200512static int mvgbe_init(struct eth_device *dev)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530513{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200514 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Chris Packhame9bf75c2018-07-09 21:33:59 +1200515
Chris Packhamfb731072018-07-09 21:34:00 +1200516 return __mvgbe_init(dmvgbe, dmvgbe->dev.enetaddr, dmvgbe->dev.name);
Chris Packhame9bf75c2018-07-09 21:33:59 +1200517}
Chris Packhamfb731072018-07-09 21:34:00 +1200518#endif
Chris Packhame9bf75c2018-07-09 21:33:59 +1200519
520static void __mvgbe_halt(struct mvgbe_device *dmvgbe)
521{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200522 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530523
524 /* Disable all gigE address decoder */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200525 MVGBE_REG_WR(regs->bare, 0x3f);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530526
527 stop_queue(&regs->tqc);
528 stop_queue(&regs->rqc);
529
Prafulla Wadaskarf0588fd2010-04-06 21:33:08 +0530530 /* Disable port */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200531 MVGBE_REG_BITS_RESET(regs->psc0, MVGBE_SERIAL_PORT_EN);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530532 /* Set port is not reset */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200533 MVGBE_REG_BITS_RESET(regs->psc1, 1 << 4);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530534#ifdef CONFIG_SYS_MII_MODE
535 /* Set MMI interface up */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200536 MVGBE_REG_BITS_RESET(regs->psc1, 1 << 3);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530537#endif
538 /* Disable & mask ethernet port interrupts */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200539 MVGBE_REG_WR(regs->ic, 0);
540 MVGBE_REG_WR(regs->ice, 0);
541 MVGBE_REG_WR(regs->pim, 0);
542 MVGBE_REG_WR(regs->peim, 0);
Chris Packhame9bf75c2018-07-09 21:33:59 +1200543}
544
Chris Packhamfb731072018-07-09 21:34:00 +1200545#ifndef CONFIG_DM_ETH
Chris Packhame9bf75c2018-07-09 21:33:59 +1200546static int mvgbe_halt(struct eth_device *dev)
547{
548 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
549
550 __mvgbe_halt(dmvgbe);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530551
552 return 0;
553}
Chris Packhamfb731072018-07-09 21:34:00 +1200554#endif
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530555
Chris Packhamfb731072018-07-09 21:34:00 +1200556#ifdef CONFIG_DM_ETH
557static int mvgbe_write_hwaddr(struct udevice *dev)
558{
Simon Glassc69cda22020-12-03 16:55:20 -0700559 struct eth_pdata *pdata = dev_get_plat(dev);
Chris Packhamfb731072018-07-09 21:34:00 +1200560
561 port_uc_addr_set(dev_get_priv(dev), pdata->enetaddr);
562
563 return 0;
564}
565#else
Albert Aribaudd44265a2010-07-12 22:24:28 +0200566static int mvgbe_write_hwaddr(struct eth_device *dev)
Prafulla Wadaskarb5ce63e2010-04-06 22:21:33 +0530567{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200568 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Prafulla Wadaskarb5ce63e2010-04-06 22:21:33 +0530569
570 /* Programs net device MAC address after initialization */
Chris Packhame9bf75c2018-07-09 21:33:59 +1200571 port_uc_addr_set(dmvgbe, dmvgbe->dev.enetaddr);
Prafulla Wadaskarb5ce63e2010-04-06 22:21:33 +0530572 return 0;
573}
Chris Packhamfb731072018-07-09 21:34:00 +1200574#endif
Prafulla Wadaskarb5ce63e2010-04-06 22:21:33 +0530575
Chris Packhame9bf75c2018-07-09 21:33:59 +1200576static int __mvgbe_send(struct mvgbe_device *dmvgbe, void *dataptr,
577 int datasize)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530578{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200579 struct mvgbe_registers *regs = dmvgbe->regs;
580 struct mvgbe_txdesc *p_txdesc = dmvgbe->p_txdesc;
Simon Kagstrom477fa632009-08-20 10:14:11 +0200581 void *p = (void *)dataptr;
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +0200582 u32 cmd_sts;
Anatolij Gustschine6e556c2011-11-19 08:59:36 +0000583 u32 txuq0_reg_addr;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530584
Simon Kagstrom477fa632009-08-20 10:14:11 +0200585 /* Copy buffer if it's misaligned */
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530586 if ((u32) dataptr & 0x07) {
Simon Kagstrom477fa632009-08-20 10:14:11 +0200587 if (datasize > PKTSIZE_ALIGN) {
588 printf("Non-aligned data too large (%d)\n",
589 datasize);
590 return -1;
591 }
592
Albert Aribaudd44265a2010-07-12 22:24:28 +0200593 memcpy(dmvgbe->p_aligned_txbuf, p, datasize);
594 p = dmvgbe->p_aligned_txbuf;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530595 }
Simon Kagstrom477fa632009-08-20 10:14:11 +0200596
Albert Aribaudd44265a2010-07-12 22:24:28 +0200597 p_txdesc->cmd_sts = MVGBE_ZERO_PADDING | MVGBE_GEN_CRC;
598 p_txdesc->cmd_sts |= MVGBE_TX_FIRST_DESC | MVGBE_TX_LAST_DESC;
599 p_txdesc->cmd_sts |= MVGBE_BUFFER_OWNED_BY_DMA;
600 p_txdesc->cmd_sts |= MVGBE_TX_EN_INTERRUPT;
Simon Kagstrom477fa632009-08-20 10:14:11 +0200601 p_txdesc->buf_ptr = (u8 *) p;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530602 p_txdesc->byte_cnt = datasize;
603
Albert Aribaudc19a20d2010-07-10 15:41:29 +0200604 /* Set this tc desc as zeroth TXUQ */
Anatolij Gustschine6e556c2011-11-19 08:59:36 +0000605 txuq0_reg_addr = (u32)&regs->tcqdp[TXUQ];
606 writel((u32) p_txdesc, txuq0_reg_addr);
Albert Aribaudc19a20d2010-07-10 15:41:29 +0200607
608 /* ensure tx desc writes above are performed before we start Tx DMA */
609 isb();
610
611 /* Apply send command using zeroth TXUQ */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200612 MVGBE_REG_WR(regs->tqc, (1 << TXUQ));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530613
614 /*
615 * wait for packet xmit completion
616 */
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +0200617 cmd_sts = readl(&p_txdesc->cmd_sts);
Albert Aribaudd44265a2010-07-12 22:24:28 +0200618 while (cmd_sts & MVGBE_BUFFER_OWNED_BY_DMA) {
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530619 /* return fail if error is detected */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200620 if ((cmd_sts & (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME)) ==
621 (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME) &&
622 cmd_sts & (MVGBE_UR_ERROR | MVGBE_RL_ERROR)) {
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500623 printf("Err..(%s) in xmit packet\n", __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530624 return -1;
625 }
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +0200626 cmd_sts = readl(&p_txdesc->cmd_sts);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530627 };
628 return 0;
629}
630
Chris Packhamfb731072018-07-09 21:34:00 +1200631#ifndef CONFIG_DM_ETH
Chris Packhame9bf75c2018-07-09 21:33:59 +1200632static int mvgbe_send(struct eth_device *dev, void *dataptr, int datasize)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530633{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200634 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Chris Packhame9bf75c2018-07-09 21:33:59 +1200635
636 return __mvgbe_send(dmvgbe, dataptr, datasize);
637}
Chris Packhamfb731072018-07-09 21:34:00 +1200638#endif
Chris Packhame9bf75c2018-07-09 21:33:59 +1200639
640static int __mvgbe_recv(struct mvgbe_device *dmvgbe, uchar **packetp)
641{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200642 struct mvgbe_rxdesc *p_rxdesc_curr = dmvgbe->p_rxdesc_curr;
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +0200643 u32 cmd_sts;
644 u32 timeout = 0;
Anatolij Gustschine6e556c2011-11-19 08:59:36 +0000645 u32 rxdesc_curr_addr;
Chris Packhame9bf75c2018-07-09 21:33:59 +1200646 unsigned char *data;
647 int rx_bytes = 0;
648
649 *packetp = NULL;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530650
651 /* wait untill rx packet available or timeout */
652 do {
Albert Aribaudd44265a2010-07-12 22:24:28 +0200653 if (timeout < MVGBE_PHY_SMI_TIMEOUT)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530654 timeout++;
655 else {
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500656 debug("%s time out...\n", __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530657 return -1;
658 }
Albert Aribaudd44265a2010-07-12 22:24:28 +0200659 } while (readl(&p_rxdesc_curr->cmd_sts) & MVGBE_BUFFER_OWNED_BY_DMA);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530660
661 if (p_rxdesc_curr->byte_cnt != 0) {
662 debug("%s: Received %d byte Packet @ 0x%x (cmd_sts= %08x)\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500663 __func__, (u32) p_rxdesc_curr->byte_cnt,
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530664 (u32) p_rxdesc_curr->buf_ptr,
665 (u32) p_rxdesc_curr->cmd_sts);
666 }
667
668 /*
669 * In case received a packet without first/last bits on
670 * OR the error summary bit is on,
671 * the packets needs to be dropeed.
672 */
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +0200673 cmd_sts = readl(&p_rxdesc_curr->cmd_sts);
674
675 if ((cmd_sts &
Albert Aribaudd44265a2010-07-12 22:24:28 +0200676 (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC))
677 != (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC)) {
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530678
679 printf("Err..(%s) Dropping packet spread on"
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500680 " multiple descriptors\n", __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530681
Albert Aribaudd44265a2010-07-12 22:24:28 +0200682 } else if (cmd_sts & MVGBE_ERROR_SUMMARY) {
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530683
684 printf("Err..(%s) Dropping packet with errors\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500685 __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530686
687 } else {
688 /* !!! call higher layer processing */
689 debug("%s: Sending Received packet to"
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500690 " upper layer (net_process_received_packet)\n",
691 __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530692
Chris Packhame9bf75c2018-07-09 21:33:59 +1200693 data = (p_rxdesc_curr->buf_ptr + RX_BUF_OFFSET);
694 rx_bytes = (int)(p_rxdesc_curr->byte_cnt -
695 RX_BUF_OFFSET);
696
697 *packetp = data;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530698 }
699 /*
700 * free these descriptors and point next in the ring
701 */
702 p_rxdesc_curr->cmd_sts =
Albert Aribaudd44265a2010-07-12 22:24:28 +0200703 MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530704 p_rxdesc_curr->buf_size = PKTSIZE_ALIGN;
705 p_rxdesc_curr->byte_cnt = 0;
706
Anatolij Gustschine6e556c2011-11-19 08:59:36 +0000707 rxdesc_curr_addr = (u32)&dmvgbe->p_rxdesc_curr;
708 writel((unsigned)p_rxdesc_curr->nxtdesc_p, rxdesc_curr_addr);
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +0200709
Chris Packhame9bf75c2018-07-09 21:33:59 +1200710 return rx_bytes;
711}
712
Chris Packhamfb731072018-07-09 21:34:00 +1200713#ifndef CONFIG_DM_ETH
Chris Packhame9bf75c2018-07-09 21:33:59 +1200714static int mvgbe_recv(struct eth_device *dev)
715{
716 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
717 uchar *packet;
718 int ret;
719
720 ret = __mvgbe_recv(dmvgbe, &packet);
721 if (ret < 0)
722 return ret;
723
724 net_process_received_packet(packet, ret);
725
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530726 return 0;
727}
Chris Packhamfb731072018-07-09 21:34:00 +1200728#endif
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530729
Chris Packhamfb731072018-07-09 21:34:00 +1200730#if defined(CONFIG_PHYLIB) || defined(CONFIG_DM_ETH)
731#if defined(CONFIG_DM_ETH)
732static struct phy_device *__mvgbe_phy_init(struct udevice *dev,
733 struct mii_dev *bus,
734 phy_interface_t phy_interface,
735 int phyid)
736#else
737static struct phy_device *__mvgbe_phy_init(struct eth_device *dev,
738 struct mii_dev *bus,
739 phy_interface_t phy_interface,
740 int phyid)
741#endif
742{
743 struct phy_device *phydev;
744
745 /* Set phy address of the port */
746 miiphy_write(dev->name, MV_PHY_ADR_REQUEST, MV_PHY_ADR_REQUEST,
747 phyid);
748
Tony Dinhf0f98752022-04-12 13:18:19 -0700749 /* Make sure the selected PHY page is 0 before connecting */
750 miiphy_write(dev->name, phyid, MVGBE_PGADR_REG, 0);
751
Chris Packhamfb731072018-07-09 21:34:00 +1200752 phydev = phy_connect(bus, phyid, dev, phy_interface);
753 if (!phydev) {
754 printf("phy_connect failed\n");
755 return NULL;
756 }
757
758 phy_config(phydev);
759 phy_startup(phydev);
760
761 return phydev;
762}
763#endif /* CONFIG_PHYLIB || CONFIG_DM_ETH */
764
765#if defined(CONFIG_PHYLIB) && !defined(CONFIG_DM_ETH)
Sebastian Hesselbarthcd3ca3f2012-12-04 09:32:00 +0100766int mvgbe_phylib_init(struct eth_device *dev, int phyid)
767{
768 struct mii_dev *bus;
769 struct phy_device *phydev;
770 int ret;
771
772 bus = mdio_alloc();
773 if (!bus) {
774 printf("mdio_alloc failed\n");
775 return -ENOMEM;
776 }
Chris Packham6ecf9e22016-11-01 10:48:32 +1300777 bus->read = smi_reg_read;
778 bus->write = smi_reg_write;
Ben Whitten192bc692015-12-30 13:05:58 +0000779 strcpy(bus->name, dev->name);
Sebastian Hesselbarthcd3ca3f2012-12-04 09:32:00 +0100780
781 ret = mdio_register(bus);
782 if (ret) {
783 printf("mdio_register failed\n");
784 free(bus);
785 return -ENOMEM;
786 }
787
Chris Packhamfb731072018-07-09 21:34:00 +1200788 phydev = __mvgbe_phy_init(dev, bus, PHY_INTERFACE_MODE_RGMII, phyid);
789 if (!phydev)
Sebastian Hesselbarthcd3ca3f2012-12-04 09:32:00 +0100790 return -ENODEV;
Sebastian Hesselbarthcd3ca3f2012-12-04 09:32:00 +0100791
792 return 0;
793}
794#endif
795
Chris Packhamfb731072018-07-09 21:34:00 +1200796static int mvgbe_alloc_buffers(struct mvgbe_device *dmvgbe)
797{
798 dmvgbe->p_rxdesc = memalign(PKTALIGN,
799 MV_RXQ_DESC_ALIGNED_SIZE * RINGSZ + 1);
800 if (!dmvgbe->p_rxdesc)
801 goto error1;
802
803 dmvgbe->p_rxbuf = memalign(PKTALIGN,
804 RINGSZ * PKTSIZE_ALIGN + 1);
805 if (!dmvgbe->p_rxbuf)
806 goto error2;
807
808 dmvgbe->p_aligned_txbuf = memalign(8, PKTSIZE_ALIGN);
809 if (!dmvgbe->p_aligned_txbuf)
810 goto error3;
811
812 dmvgbe->p_txdesc = memalign(PKTALIGN, sizeof(struct mvgbe_txdesc) + 1);
813 if (!dmvgbe->p_txdesc)
814 goto error4;
815
816 return 0;
817
818error4:
819 free(dmvgbe->p_aligned_txbuf);
820error3:
821 free(dmvgbe->p_rxbuf);
822error2:
823 free(dmvgbe->p_rxdesc);
824error1:
825 return -ENOMEM;
826}
827
828#ifndef CONFIG_DM_ETH
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900829int mvgbe_initialize(struct bd_info *bis)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530830{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200831 struct mvgbe_device *dmvgbe;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530832 struct eth_device *dev;
833 int devnum;
Chris Packhamfb731072018-07-09 21:34:00 +1200834 int ret;
Albert Aribaudd44265a2010-07-12 22:24:28 +0200835 u8 used_ports[MAX_MVGBE_DEVS] = CONFIG_MVGBE_PORTS;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530836
Albert Aribaudd44265a2010-07-12 22:24:28 +0200837 for (devnum = 0; devnum < MAX_MVGBE_DEVS; devnum++) {
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530838 /*skip if port is configured not to use */
839 if (used_ports[devnum] == 0)
840 continue;
841
Albert Aribaudd44265a2010-07-12 22:24:28 +0200842 dmvgbe = malloc(sizeof(struct mvgbe_device));
Albert Aribaudd44265a2010-07-12 22:24:28 +0200843 if (!dmvgbe)
Chris Packhamfb731072018-07-09 21:34:00 +1200844 return -ENOMEM;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530845
Albert Aribaudd44265a2010-07-12 22:24:28 +0200846 memset(dmvgbe, 0, sizeof(struct mvgbe_device));
Chris Packhamfb731072018-07-09 21:34:00 +1200847 ret = mvgbe_alloc_buffers(dmvgbe);
848 if (ret) {
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530849 printf("Err.. %s Failed to allocate memory\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500850 __func__);
Chris Packhamfb731072018-07-09 21:34:00 +1200851 free(dmvgbe);
852 return ret;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530853 }
854
Albert Aribaudd44265a2010-07-12 22:24:28 +0200855 dev = &dmvgbe->dev;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530856
Mike Frysingerf6add132011-11-10 14:11:04 +0000857 /* must be less than sizeof(dev->name) */
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530858 sprintf(dev->name, "egiga%d", devnum);
859
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530860 switch (devnum) {
861 case 0:
Albert Aribaudd44265a2010-07-12 22:24:28 +0200862 dmvgbe->regs = (void *)MVGBE0_BASE;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530863 break;
Albert Aribaudd44265a2010-07-12 22:24:28 +0200864#if defined(MVGBE1_BASE)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530865 case 1:
Albert Aribaudd44265a2010-07-12 22:24:28 +0200866 dmvgbe->regs = (void *)MVGBE1_BASE;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530867 break;
Albert Aribaudd44265a2010-07-12 22:24:28 +0200868#endif
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530869 default: /* this should never happen */
870 printf("Err..(%s) Invalid device number %d\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500871 __func__, devnum);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530872 return -1;
873 }
874
Albert Aribaudd44265a2010-07-12 22:24:28 +0200875 dev->init = (void *)mvgbe_init;
876 dev->halt = (void *)mvgbe_halt;
877 dev->send = (void *)mvgbe_send;
878 dev->recv = (void *)mvgbe_recv;
879 dev->write_hwaddr = (void *)mvgbe_write_hwaddr;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530880
881 eth_register(dev);
882
Sebastian Hesselbarthcd3ca3f2012-12-04 09:32:00 +0100883#if defined(CONFIG_PHYLIB)
884 mvgbe_phylib_init(dev, PHY_BASE_ADR + devnum);
885#elif defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Joe Hershberger5a49f172016-08-08 11:28:38 -0500886 int retval;
887 struct mii_dev *mdiodev = mdio_alloc();
888 if (!mdiodev)
889 return -ENOMEM;
Vladimir Oltean77003e52021-09-27 14:21:55 +0300890 strlcpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
Joe Hershberger5a49f172016-08-08 11:28:38 -0500891 mdiodev->read = smi_reg_read;
892 mdiodev->write = smi_reg_write;
893
894 retval = mdio_register(mdiodev);
895 if (retval < 0)
896 return retval;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530897 /* Set phy address of the port */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200898 miiphy_write(dev->name, MV_PHY_ADR_REQUEST,
899 MV_PHY_ADR_REQUEST, PHY_BASE_ADR + devnum);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530900#endif
901 }
902 return 0;
Prafulla Wadaskar0b785dd2009-07-01 20:34:51 +0200903}
Chris Packhamfb731072018-07-09 21:34:00 +1200904#endif
905
906#ifdef CONFIG_DM_ETH
907static int mvgbe_port_is_fixed_link(struct mvgbe_device *dmvgbe)
908{
909 return dmvgbe->phyaddr > PHY_MAX_ADDR;
910}
911
912static int mvgbe_start(struct udevice *dev)
913{
Simon Glassc69cda22020-12-03 16:55:20 -0700914 struct eth_pdata *pdata = dev_get_plat(dev);
Chris Packhamfb731072018-07-09 21:34:00 +1200915 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
916 int ret;
917
918 ret = __mvgbe_init(dmvgbe, pdata->enetaddr, dev->name);
919 if (ret)
920 return ret;
921
922 if (!mvgbe_port_is_fixed_link(dmvgbe)) {
923 dmvgbe->phydev = __mvgbe_phy_init(dev, dmvgbe->bus,
924 dmvgbe->phy_interface,
925 dmvgbe->phyaddr);
926 if (!dmvgbe->phydev)
927 return -ENODEV;
928 }
929
930 return 0;
931}
932
933static int mvgbe_send(struct udevice *dev, void *packet, int length)
934{
935 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
936
937 return __mvgbe_send(dmvgbe, packet, length);
938}
939
940static int mvgbe_recv(struct udevice *dev, int flags, uchar **packetp)
941{
942 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
943
944 return __mvgbe_recv(dmvgbe, packetp);
945}
946
947static void mvgbe_stop(struct udevice *dev)
948{
949 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
950
951 __mvgbe_halt(dmvgbe);
952}
953
954static int mvgbe_probe(struct udevice *dev)
955{
Simon Glassc69cda22020-12-03 16:55:20 -0700956 struct eth_pdata *pdata = dev_get_plat(dev);
Chris Packhamfb731072018-07-09 21:34:00 +1200957 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
958 struct mii_dev *bus;
959 int ret;
960
961 ret = mvgbe_alloc_buffers(dmvgbe);
962 if (ret)
963 return ret;
964
965 dmvgbe->regs = (void __iomem *)pdata->iobase;
966
967 bus = mdio_alloc();
968 if (!bus) {
969 printf("Failed to allocate MDIO bus\n");
970 return -ENOMEM;
971 }
972
973 bus->read = smi_reg_read;
974 bus->write = smi_reg_write;
975 snprintf(bus->name, sizeof(bus->name), dev->name);
976 bus->priv = dmvgbe;
977 dmvgbe->bus = bus;
978
979 ret = mdio_register(bus);
980 if (ret < 0)
981 return ret;
982
983 return 0;
984}
985
986static const struct eth_ops mvgbe_ops = {
987 .start = mvgbe_start,
988 .send = mvgbe_send,
989 .recv = mvgbe_recv,
990 .stop = mvgbe_stop,
991 .write_hwaddr = mvgbe_write_hwaddr,
992};
993
Simon Glassd1998a92020-12-03 16:55:21 -0700994static int mvgbe_of_to_plat(struct udevice *dev)
Chris Packhamfb731072018-07-09 21:34:00 +1200995{
Simon Glassc69cda22020-12-03 16:55:20 -0700996 struct eth_pdata *pdata = dev_get_plat(dev);
Chris Packhamfb731072018-07-09 21:34:00 +1200997 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
998 void *blob = (void *)gd->fdt_blob;
999 int node = dev_of_offset(dev);
Chris Packhamfb731072018-07-09 21:34:00 +12001000 int fl_node;
1001 int pnode;
1002 unsigned long addr;
1003
Masahiro Yamada25484932020-07-17 14:36:48 +09001004 pdata->iobase = dev_read_addr(dev);
Chris Packhamfb731072018-07-09 21:34:00 +12001005 pdata->phy_interface = -1;
1006
1007 pnode = fdt_node_offset_by_compatible(blob, node,
1008 "marvell,kirkwood-eth-port");
1009
1010 /* Get phy-mode / phy_interface from DT */
Marek BehĂșn123ca112022-04-07 00:33:01 +02001011 pdata->phy_interface = dev_read_phy_mode(dev);
Marek BehĂșnffb0f6f2022-04-07 00:33:03 +02001012 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
Chris Packham92f129f2018-12-04 19:54:30 +13001013 pdata->phy_interface = PHY_INTERFACE_MODE_GMII;
Chris Packhamfb731072018-07-09 21:34:00 +12001014
1015 dmvgbe->phy_interface = pdata->phy_interface;
1016
1017 /* fetch 'fixed-link' property */
1018 fl_node = fdt_subnode_offset(blob, pnode, "fixed-link");
1019 if (fl_node != -FDT_ERR_NOTFOUND) {
1020 /* set phy_addr to invalid value for fixed link */
1021 dmvgbe->phyaddr = PHY_MAX_ADDR + 1;
1022 dmvgbe->duplex = fdtdec_get_bool(blob, fl_node, "full-duplex");
1023 dmvgbe->speed = fdtdec_get_int(blob, fl_node, "speed", 0);
1024 } else {
1025 /* Now read phyaddr from DT */
1026 addr = fdtdec_lookup_phandle(blob, pnode, "phy-handle");
1027 if (addr > 0)
1028 dmvgbe->phyaddr = fdtdec_get_int(blob, addr, "reg", 0);
1029 }
1030
1031 return 0;
1032}
1033
1034static const struct udevice_id mvgbe_ids[] = {
1035 { .compatible = "marvell,kirkwood-eth" },
1036 { }
1037};
1038
1039U_BOOT_DRIVER(mvgbe) = {
1040 .name = "mvgbe",
1041 .id = UCLASS_ETH,
1042 .of_match = mvgbe_ids,
Simon Glassd1998a92020-12-03 16:55:21 -07001043 .of_to_plat = mvgbe_of_to_plat,
Chris Packhamfb731072018-07-09 21:34:00 +12001044 .probe = mvgbe_probe,
1045 .ops = &mvgbe_ops,
Simon Glass41575d82020-12-03 16:55:17 -07001046 .priv_auto = sizeof(struct mvgbe_device),
Simon Glasscaa4daa2020-12-03 16:55:18 -07001047 .plat_auto = sizeof(struct eth_pdata),
Chris Packhamfb731072018-07-09 21:34:00 +12001048};
1049#endif /* CONFIG_DM_ETH */