blob: 604f676bfff862d0daac7eba1862286c77b78ba6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +02002/*
3 * LPC32xx Ethernet MAC interface driver
4 *
5 * (C) Copyright 2014 DENX Software Engineering GmbH
6 * Written-by: Albert ARIBAUD - 3ADEV <albert.aribaud@3adev.fr>
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +02007 */
8
9#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +020011#include <net.h>
12#include <malloc.h>
13#include <miiphy.h>
14#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090015#include <linux/errno.h>
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +020016#include <asm/types.h>
17#include <asm/system.h>
18#include <asm/byteorder.h>
19#include <asm/arch/cpu.h>
20#include <asm/arch/config.h>
21
22/*
23 * Notes:
24 *
25 * 1. Unless specified otherwise, all references to tables or paragraphs
26 * are to UM10326, "LPC32x0 and LPC32x0/01 User manual".
27 *
28 * 2. Only bitfield masks/values which are actually used by the driver
29 * are defined.
30 */
31
32/* a single RX descriptor. The controller has an array of these */
33struct lpc32xx_eth_rxdesc {
34 u32 packet; /* Receive packet pointer */
35 u32 control; /* Descriptor command status */
36};
37
38#define LPC32XX_ETH_RX_DESC_SIZE (sizeof(struct lpc32xx_eth_rxdesc))
39
40/* RX control bitfields/masks (see Table 330) */
41#define LPC32XX_ETH_RX_CTRL_SIZE_MASK 0x000007FF
42#define LPC32XX_ETH_RX_CTRL_UNUSED 0x7FFFF800
43#define LPC32XX_ETH_RX_CTRL_INTERRUPT 0x80000000
44
45/* a single RX status. The controller has an array of these */
46struct lpc32xx_eth_rxstat {
47 u32 statusinfo; /* Transmit Descriptor status */
48 u32 statushashcrc; /* Transmit Descriptor CRCs */
49};
50
51#define LPC32XX_ETH_RX_STAT_SIZE (sizeof(struct lpc32xx_eth_rxstat))
52
53/* RX statusinfo bitfields/masks (see Table 333) */
54#define RX_STAT_RXSIZE 0x000007FF
55/* Helper: OR of all errors except RANGE */
56#define RX_STAT_ERRORS 0x1B800000
57
58/* a single TX descriptor. The controller has an array of these */
59struct lpc32xx_eth_txdesc {
60 u32 packet; /* Transmit packet pointer */
61 u32 control; /* Descriptor control */
62};
63
64#define LPC32XX_ETH_TX_DESC_SIZE (sizeof(struct lpc32xx_eth_txdesc))
65
66/* TX control bitfields/masks (see Table 335) */
67#define TX_CTRL_TXSIZE 0x000007FF
68#define TX_CTRL_LAST 0x40000000
69
70/* a single TX status. The controller has an array of these */
71struct lpc32xx_eth_txstat {
72 u32 statusinfo; /* Transmit Descriptor status */
73};
74
75#define LPC32XX_ETH_TX_STAT_SIZE (sizeof(struct lpc32xx_eth_txstat))
76
77/* Ethernet MAC interface registers (see Table 283) */
78struct lpc32xx_eth_registers {
79 /* MAC registers - 0x3106_0000 to 0x3106_01FC */
80 u32 mac1; /* MAC configuration register 1 */
81 u32 mac2; /* MAC configuration register 2 */
82 u32 ipgt; /* Back-to-back Inter-Packet Gap reg. */
83 u32 ipgr; /* Non-back-to-back IPG register */
84 u32 clrt; /* Collision Window / Retry register */
85 u32 maxf; /* Maximum Frame register */
86 u32 supp; /* Phy Support register */
87 u32 test;
88 u32 mcfg; /* MII management configuration reg. */
89 u32 mcmd; /* MII management command register */
90 u32 madr; /* MII management address register */
91 u32 mwtd; /* MII management wite data register */
92 u32 mrdd; /* MII management read data register */
93 u32 mind; /* MII management indicators register */
94 u32 reserved1[2];
95 u32 sa0; /* Station address register 0 */
96 u32 sa1; /* Station address register 1 */
97 u32 sa2; /* Station address register 2 */
98 u32 reserved2[45];
99 /* Control registers */
100 u32 command;
101 u32 status;
102 u32 rxdescriptor;
103 u32 rxstatus;
104 u32 rxdescriptornumber; /* actually, number MINUS ONE */
105 u32 rxproduceindex; /* head of rx desc fifo */
106 u32 rxconsumeindex; /* tail of rx desc fifo */
107 u32 txdescriptor;
108 u32 txstatus;
109 u32 txdescriptornumber; /* actually, number MINUS ONE */
110 u32 txproduceindex; /* head of rx desc fifo */
111 u32 txconsumeindex; /* tail of rx desc fifo */
112 u32 reserved3[10];
113 u32 tsv0; /* Transmit status vector register 0 */
114 u32 tsv1; /* Transmit status vector register 1 */
115 u32 rsv; /* Receive status vector register */
116 u32 reserved4[3];
117 u32 flowcontrolcounter;
118 u32 flowcontrolstatus;
119 u32 reserved5[34];
120 /* RX filter registers - 0x3106_0200 to 0x3106_0FDC */
121 u32 rxfilterctrl;
122 u32 rxfilterwolstatus;
123 u32 rxfilterwolclear;
124 u32 reserved6;
125 u32 hashfilterl;
126 u32 hashfilterh;
127 u32 reserved7[882];
128 /* Module control registers - 0x3106_0FE0 to 0x3106_0FF8 */
129 u32 intstatus; /* Interrupt status register */
130 u32 intenable;
131 u32 intclear;
132 u32 intset;
133 u32 reserved8;
134 u32 powerdown;
135 u32 reserved9;
136};
137
138/* MAC1 register bitfields/masks and offsets (see Table 283) */
139#define MAC1_RECV_ENABLE 0x00000001
140#define MAC1_PASS_ALL_RX_FRAMES 0x00000002
141#define MAC1_SOFT_RESET 0x00008000
142/* Helper: general reset */
143#define MAC1_RESETS 0x0000CF00
144
145/* MAC2 register bitfields/masks and offsets (see Table 284) */
146#define MAC2_FULL_DUPLEX 0x00000001
147#define MAC2_CRC_ENABLE 0x00000010
148#define MAC2_PAD_CRC_ENABLE 0x00000020
149
150/* SUPP register bitfields/masks and offsets (see Table 290) */
151#define SUPP_SPEED 0x00000100
152
153/* MCFG register bitfields/masks and offsets (see Table 292) */
Vladimir Zapolskiye7d729f2015-07-06 07:22:10 +0300154#define MCFG_RESET_MII_MGMT 0x00008000
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200155/* divide clock by 28 (see Table 293) */
156#define MCFG_CLOCK_SELECT_DIV28 0x0000001C
157
158/* MADR register bitfields/masks and offsets (see Table 295) */
159#define MADR_REG_MASK 0x0000001F
160#define MADR_PHY_MASK 0x00001F00
161#define MADR_REG_OFFSET 0
162#define MADR_PHY_OFFSET 8
163
164/* MIND register bitfields/masks (see Table 298) */
165#define MIND_BUSY 0x00000001
166
167/* COMMAND register bitfields/masks and offsets (see Table 283) */
168#define COMMAND_RXENABLE 0x00000001
169#define COMMAND_TXENABLE 0x00000002
170#define COMMAND_PASSRUNTFRAME 0x00000040
Vladimir Zapolskiy8bffd712015-07-06 07:22:11 +0300171#define COMMAND_RMII 0x00000200
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200172#define COMMAND_FULL_DUPLEX 0x00000400
173/* Helper: general reset */
Vladimir Zapolskiy8884ced2015-06-28 06:03:38 +0300174#define COMMAND_RESETS 0x00000038
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200175
176/* STATUS register bitfields/masks and offsets (see Table 283) */
177#define STATUS_RXSTATUS 0x00000001
178#define STATUS_TXSTATUS 0x00000002
179
180/* RXFILTERCTRL register bitfields/masks (see Table 319) */
181#define RXFILTERCTRL_ACCEPTBROADCAST 0x00000002
182#define RXFILTERCTRL_ACCEPTPERFECT 0x00000020
183
184/* Buffers and descriptors */
185
186#define ATTRS(n) __aligned(n)
187
188#define TX_BUF_COUNT 4
189#define RX_BUF_COUNT 4
190
191struct lpc32xx_eth_buffers {
192 ATTRS(4) struct lpc32xx_eth_txdesc tx_desc[TX_BUF_COUNT];
193 ATTRS(4) struct lpc32xx_eth_txstat tx_stat[TX_BUF_COUNT];
194 ATTRS(PKTALIGN) u8 tx_buf[TX_BUF_COUNT*PKTSIZE_ALIGN];
195 ATTRS(4) struct lpc32xx_eth_rxdesc rx_desc[RX_BUF_COUNT];
196 ATTRS(8) struct lpc32xx_eth_rxstat rx_stat[RX_BUF_COUNT];
197 ATTRS(PKTALIGN) u8 rx_buf[RX_BUF_COUNT*PKTSIZE_ALIGN];
198};
199
200/* port device data struct */
201struct lpc32xx_eth_device {
202 struct eth_device dev;
203 struct lpc32xx_eth_registers *regs;
204 struct lpc32xx_eth_buffers *bufs;
Vladimir Zapolskiy8bffd712015-07-06 07:22:11 +0300205 bool phy_rmii;
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200206};
207
208#define LPC32XX_ETH_DEVICE_SIZE (sizeof(struct lpc32xx_eth_device))
209
210/* generic macros */
211#define to_lpc32xx_eth(_d) container_of(_d, struct lpc32xx_eth_device, dev)
212
213/* timeout for MII polling */
214#define MII_TIMEOUT 10000000
215
216/* limits for PHY and register addresses */
217#define MII_MAX_REG (MADR_REG_MASK >> MADR_REG_OFFSET)
218
219#define MII_MAX_PHY (MADR_PHY_MASK >> MADR_PHY_OFFSET)
220
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200221#if defined(CONFIG_PHYLIB) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
222/*
223 * mii_reg_read - miiphy_read callback function.
224 *
225 * Returns 16bit phy register value, or 0xffff on error
226 */
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500227static int mii_reg_read(struct mii_dev *bus, int phy_adr, int devad,
228 int reg_ofs)
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200229{
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500230 u16 data = 0;
231 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200232 struct lpc32xx_eth_device *dlpc32xx_eth = to_lpc32xx_eth(dev);
233 struct lpc32xx_eth_registers *regs = dlpc32xx_eth->regs;
234 u32 mind_reg;
235 u32 timeout;
236
237 /* check parameters */
238 if (phy_adr > MII_MAX_PHY) {
239 printf("%s:%u: Invalid PHY address %d\n",
240 __func__, __LINE__, phy_adr);
241 return -EFAULT;
242 }
243 if (reg_ofs > MII_MAX_REG) {
244 printf("%s:%u: Invalid register offset %d\n",
245 __func__, __LINE__, reg_ofs);
246 return -EFAULT;
247 }
248
249 /* write the phy and reg addressse into the MII address reg */
250 writel((phy_adr << MADR_PHY_OFFSET) | (reg_ofs << MADR_REG_OFFSET),
251 &regs->madr);
252
253 /* write 1 to the MII command register to cause a read */
254 writel(1, &regs->mcmd);
255
256 /* wait till the MII is not busy */
257 timeout = MII_TIMEOUT;
258 do {
259 /* read MII indicators register */
260 mind_reg = readl(&regs->mind);
261 if (--timeout == 0)
262 break;
263 } while (mind_reg & MIND_BUSY);
264
265 /* write 0 to the MII command register to finish the read */
266 writel(0, &regs->mcmd);
267
268 if (timeout == 0) {
269 printf("%s:%u: MII busy timeout\n", __func__, __LINE__);
270 return -EFAULT;
271 }
272
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500273 data = (u16) readl(&regs->mrdd);
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200274
275 debug("%s:(adr %d, off %d) => %04x\n", __func__, phy_adr,
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500276 reg_ofs, data);
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200277
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500278 return data;
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200279}
280
281/*
282 * mii_reg_write - imiiphy_write callback function.
283 *
284 * Returns 0 if write succeed, -EINVAL on bad parameters
285 * -ETIME on timeout
286 */
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500287static int mii_reg_write(struct mii_dev *bus, int phy_adr, int devad,
288 int reg_ofs, u16 data)
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200289{
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500290 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200291 struct lpc32xx_eth_device *dlpc32xx_eth = to_lpc32xx_eth(dev);
292 struct lpc32xx_eth_registers *regs = dlpc32xx_eth->regs;
293 u32 mind_reg;
294 u32 timeout;
295
296 /* check parameters */
297 if (phy_adr > MII_MAX_PHY) {
298 printf("%s:%u: Invalid PHY address %d\n",
299 __func__, __LINE__, phy_adr);
300 return -EFAULT;
301 }
302 if (reg_ofs > MII_MAX_REG) {
303 printf("%s:%u: Invalid register offset %d\n",
304 __func__, __LINE__, reg_ofs);
305 return -EFAULT;
306 }
307
Vladimir Zapolskiye90f63a2015-12-27 05:12:24 +0200308 /* write the phy and reg addressse into the MII address reg */
309 writel((phy_adr << MADR_PHY_OFFSET) | (reg_ofs << MADR_REG_OFFSET),
310 &regs->madr);
311
312 /* write data to the MII write register */
313 writel(data, &regs->mwtd);
314
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200315 /* wait till the MII is not busy */
316 timeout = MII_TIMEOUT;
317 do {
318 /* read MII indicators register */
319 mind_reg = readl(&regs->mind);
320 if (--timeout == 0)
321 break;
322 } while (mind_reg & MIND_BUSY);
323
324 if (timeout == 0) {
325 printf("%s:%u: MII busy timeout\n", __func__,
326 __LINE__);
327 return -EFAULT;
328 }
329
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200330 /*debug("%s:(adr %d, off %d) <= %04x\n", __func__, phy_adr,
331 reg_ofs, data);*/
332
333 return 0;
334}
335#endif
336
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200337/*
Sylvain Lemieux25624f92015-07-27 13:37:40 -0400338 * Provide default Ethernet buffers base address if target did not.
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200339 * Locate buffers in SRAM at 0x00001000 to avoid cache issues and
340 * maximize throughput.
341 */
Sylvain Lemieux25624f92015-07-27 13:37:40 -0400342#if !defined(CONFIG_LPC32XX_ETH_BUFS_BASE)
343#define CONFIG_LPC32XX_ETH_BUFS_BASE 0x00001000
344#endif
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200345
346static struct lpc32xx_eth_device lpc32xx_eth = {
347 .regs = (struct lpc32xx_eth_registers *)LPC32XX_ETH_BASE,
Sylvain Lemieux25624f92015-07-27 13:37:40 -0400348 .bufs = (struct lpc32xx_eth_buffers *)CONFIG_LPC32XX_ETH_BUFS_BASE,
Vladimir Zapolskiy8bffd712015-07-06 07:22:11 +0300349#if defined(CONFIG_RMII)
350 .phy_rmii = true,
351#endif
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200352};
353
354#define TX_TIMEOUT 10000
355
356static int lpc32xx_eth_send(struct eth_device *dev, void *dataptr, int datasize)
357{
358 struct lpc32xx_eth_device *lpc32xx_eth_device =
359 container_of(dev, struct lpc32xx_eth_device, dev);
360 struct lpc32xx_eth_registers *regs = lpc32xx_eth_device->regs;
361 struct lpc32xx_eth_buffers *bufs = lpc32xx_eth_device->bufs;
362 int timeout, tx_index;
363
364 /* time out if transmit descriptor array remains full too long */
365 timeout = TX_TIMEOUT;
366 while ((readl(&regs->status) & STATUS_TXSTATUS) &&
367 (readl(&regs->txconsumeindex)
368 == readl(&regs->txproduceindex))) {
369 if (timeout-- == 0)
370 return -1;
371 }
372
373 /* determine next transmit packet index to use */
374 tx_index = readl(&regs->txproduceindex);
375
376 /* set up transmit packet */
Gregory CLEMENTe599b542019-04-17 11:41:42 +0200377 memcpy((void *)&bufs->tx_buf[tx_index * PKTSIZE_ALIGN],
378 (void *)dataptr, datasize);
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200379 writel(TX_CTRL_LAST | ((datasize - 1) & TX_CTRL_TXSIZE),
380 &bufs->tx_desc[tx_index].control);
381 writel(0, &bufs->tx_stat[tx_index].statusinfo);
382
383 /* pass transmit packet to DMA engine */
384 tx_index = (tx_index + 1) % TX_BUF_COUNT;
385 writel(tx_index, &regs->txproduceindex);
386
387 /* transmission succeeded */
388 return 0;
389}
390
391#define RX_TIMEOUT 1000000
392
393static int lpc32xx_eth_recv(struct eth_device *dev)
394{
395 struct lpc32xx_eth_device *lpc32xx_eth_device =
396 container_of(dev, struct lpc32xx_eth_device, dev);
397 struct lpc32xx_eth_registers *regs = lpc32xx_eth_device->regs;
398 struct lpc32xx_eth_buffers *bufs = lpc32xx_eth_device->bufs;
399 int timeout, rx_index;
400
401 /* time out if receive descriptor array remains empty too long */
402 timeout = RX_TIMEOUT;
403 while (readl(&regs->rxproduceindex) == readl(&regs->rxconsumeindex)) {
404 if (timeout-- == 0)
405 return -1;
406 }
407
408 /* determine next receive packet index to use */
409 rx_index = readl(&regs->rxconsumeindex);
410
411 /* if data was valid, pass it on */
Joe Hershberger9f09a362015-04-08 01:41:06 -0500412 if (!(bufs->rx_stat[rx_index].statusinfo & RX_STAT_ERRORS)) {
413 net_process_received_packet(
414 &(bufs->rx_buf[rx_index * PKTSIZE_ALIGN]),
415 (bufs->rx_stat[rx_index].statusinfo
416 & RX_STAT_RXSIZE) + 1);
417 }
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200418
419 /* pass receive slot back to DMA engine */
420 rx_index = (rx_index + 1) % RX_BUF_COUNT;
421 writel(rx_index, &regs->rxconsumeindex);
422
423 /* reception successful */
424 return 0;
425}
426
427static int lpc32xx_eth_write_hwaddr(struct eth_device *dev)
428{
429 struct lpc32xx_eth_device *lpc32xx_eth_device =
430 container_of(dev, struct lpc32xx_eth_device, dev);
431 struct lpc32xx_eth_registers *regs = lpc32xx_eth_device->regs;
432
433 /* Save station address */
434 writel((unsigned long) (dev->enetaddr[0] |
435 (dev->enetaddr[1] << 8)), &regs->sa2);
436 writel((unsigned long) (dev->enetaddr[2] |
437 (dev->enetaddr[3] << 8)), &regs->sa1);
438 writel((unsigned long) (dev->enetaddr[4] |
439 (dev->enetaddr[5] << 8)), &regs->sa0);
440
441 return 0;
442}
443
444static int lpc32xx_eth_init(struct eth_device *dev)
445{
446 struct lpc32xx_eth_device *lpc32xx_eth_device =
447 container_of(dev, struct lpc32xx_eth_device, dev);
448 struct lpc32xx_eth_registers *regs = lpc32xx_eth_device->regs;
449 struct lpc32xx_eth_buffers *bufs = lpc32xx_eth_device->bufs;
450 int index;
451
Vladimir Zapolskiye7d729f2015-07-06 07:22:10 +0300452 /* Initial MAC initialization */
453 writel(MAC1_PASS_ALL_RX_FRAMES, &regs->mac1);
454 writel(MAC2_PAD_CRC_ENABLE | MAC2_CRC_ENABLE, &regs->mac2);
455 writel(PKTSIZE_ALIGN, &regs->maxf);
456
457 /* Retries: 15 (0xF). Collision window: 57 (0x37). */
458 writel(0x370F, &regs->clrt);
459
460 /* Set IP gap pt 2 to default 0x12 but pt 1 to non-default 0 */
461 writel(0x0012, &regs->ipgr);
462
463 /* pass runt (smaller than 64 bytes) frames */
Vladimir Zapolskiy8bffd712015-07-06 07:22:11 +0300464 if (lpc32xx_eth_device->phy_rmii)
465 writel(COMMAND_PASSRUNTFRAME | COMMAND_RMII, &regs->command);
466 else
467 writel(COMMAND_PASSRUNTFRAME, &regs->command);
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200468
469 /* Configure Full/Half Duplex mode */
470 if (miiphy_duplex(dev->name, CONFIG_PHY_ADDR) == FULL) {
471 setbits_le32(&regs->mac2, MAC2_FULL_DUPLEX);
472 setbits_le32(&regs->command, COMMAND_FULL_DUPLEX);
473 writel(0x15, &regs->ipgt);
474 } else {
475 writel(0x12, &regs->ipgt);
476 }
477
478 /* Configure 100MBit/10MBit mode */
479 if (miiphy_speed(dev->name, CONFIG_PHY_ADDR) == _100BASET)
480 writel(SUPP_SPEED, &regs->supp);
481 else
482 writel(0, &regs->supp);
483
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200484 /* Save station address */
485 writel((unsigned long) (dev->enetaddr[0] |
486 (dev->enetaddr[1] << 8)), &regs->sa2);
487 writel((unsigned long) (dev->enetaddr[2] |
488 (dev->enetaddr[3] << 8)), &regs->sa1);
489 writel((unsigned long) (dev->enetaddr[4] |
490 (dev->enetaddr[5] << 8)), &regs->sa0);
491
492 /* set up transmit buffers */
493 for (index = 0; index < TX_BUF_COUNT; index++) {
494 bufs->tx_desc[index].control = 0;
495 bufs->tx_stat[index].statusinfo = 0;
496 }
497 writel((u32)(&bufs->tx_desc), (u32 *)&regs->txdescriptor);
498 writel((u32)(&bufs->tx_stat), &regs->txstatus);
499 writel(TX_BUF_COUNT-1, &regs->txdescriptornumber);
500
501 /* set up receive buffers */
502 for (index = 0; index < RX_BUF_COUNT; index++) {
503 bufs->rx_desc[index].packet =
504 (u32) (bufs->rx_buf+index*PKTSIZE_ALIGN);
505 bufs->rx_desc[index].control = PKTSIZE_ALIGN - 1;
506 bufs->rx_stat[index].statusinfo = 0;
507 bufs->rx_stat[index].statushashcrc = 0;
508 }
509 writel((u32)(&bufs->rx_desc), &regs->rxdescriptor);
510 writel((u32)(&bufs->rx_stat), &regs->rxstatus);
511 writel(RX_BUF_COUNT-1, &regs->rxdescriptornumber);
512
Gregory CLEMENTe599b542019-04-17 11:41:42 +0200513 /* set up transmit buffers */
514 for (index = 0; index < TX_BUF_COUNT; index++)
515 bufs->tx_desc[index].packet =
516 (u32)(bufs->tx_buf + index * PKTSIZE_ALIGN);
517
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200518 /* Enable broadcast and matching address packets */
519 writel(RXFILTERCTRL_ACCEPTBROADCAST |
520 RXFILTERCTRL_ACCEPTPERFECT, &regs->rxfilterctrl);
521
522 /* Clear and disable interrupts */
523 writel(0xFFFF, &regs->intclear);
524 writel(0, &regs->intenable);
525
526 /* Enable receive and transmit mode of MAC ethernet core */
527 setbits_le32(&regs->command, COMMAND_RXENABLE | COMMAND_TXENABLE);
528 setbits_le32(&regs->mac1, MAC1_RECV_ENABLE);
529
530 /*
531 * Perform a 'dummy' first send to work around Ethernet.1
532 * erratum (see ES_LPC3250 rev. 9 dated 1 June 2011).
533 * Use zeroed "index" variable as the dummy.
534 */
535
536 index = 0;
537 lpc32xx_eth_send(dev, &index, 4);
538
539 return 0;
540}
541
542static int lpc32xx_eth_halt(struct eth_device *dev)
543{
544 struct lpc32xx_eth_device *lpc32xx_eth_device =
545 container_of(dev, struct lpc32xx_eth_device, dev);
546 struct lpc32xx_eth_registers *regs = lpc32xx_eth_device->regs;
547
548 /* Reset all MAC logic */
549 writel(MAC1_RESETS, &regs->mac1);
550 writel(COMMAND_RESETS, &regs->command);
551 /* Let reset condition settle */
552 udelay(2000);
553
554 return 0;
555}
556
557#if defined(CONFIG_PHYLIB)
558int lpc32xx_eth_phylib_init(struct eth_device *dev, int phyid)
559{
Vladimir Zapolskiy8bffd712015-07-06 07:22:11 +0300560 struct lpc32xx_eth_device *lpc32xx_eth_device =
561 container_of(dev, struct lpc32xx_eth_device, dev);
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200562 struct mii_dev *bus;
563 struct phy_device *phydev;
564 int ret;
565
566 bus = mdio_alloc();
567 if (!bus) {
568 printf("mdio_alloc failed\n");
569 return -ENOMEM;
570 }
Joe Hershbergerece1aa62016-08-08 11:28:40 -0500571 bus->read = mii_reg_read;
572 bus->write = mii_reg_write;
Ben Whitten34fd6c92015-12-30 13:05:58 +0000573 strcpy(bus->name, dev->name);
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200574
575 ret = mdio_register(bus);
576 if (ret) {
577 printf("mdio_register failed\n");
578 free(bus);
579 return -ENOMEM;
580 }
581
Vladimir Zapolskiy8bffd712015-07-06 07:22:11 +0300582 if (lpc32xx_eth_device->phy_rmii)
583 phydev = phy_connect(bus, phyid, dev, PHY_INTERFACE_MODE_RMII);
584 else
585 phydev = phy_connect(bus, phyid, dev, PHY_INTERFACE_MODE_MII);
586
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200587 if (!phydev) {
588 printf("phy_connect failed\n");
589 return -ENODEV;
590 }
591
592 phy_config(phydev);
593 phy_startup(phydev);
594
595 return 0;
596}
597#endif
598
599int lpc32xx_eth_initialize(bd_t *bis)
600{
601 struct eth_device *dev = &lpc32xx_eth.dev;
602 struct lpc32xx_eth_registers *regs = lpc32xx_eth.regs;
603
604 /*
605 * Set RMII management clock rate. With HCLK at 104 MHz and
606 * a divider of 28, this will be 3.72 MHz.
607 */
Vladimir Zapolskiye7d729f2015-07-06 07:22:10 +0300608 writel(MCFG_RESET_MII_MGMT, &regs->mcfg);
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200609 writel(MCFG_CLOCK_SELECT_DIV28, &regs->mcfg);
610
611 /* Reset all MAC logic */
612 writel(MAC1_RESETS, &regs->mac1);
613 writel(COMMAND_RESETS, &regs->command);
614
615 /* wait 10 ms for the whole I/F to reset */
616 udelay(10000);
617
618 /* must be less than sizeof(dev->name) */
619 strcpy(dev->name, "eth0");
620
621 dev->init = (void *)lpc32xx_eth_init;
622 dev->halt = (void *)lpc32xx_eth_halt;
623 dev->send = (void *)lpc32xx_eth_send;
624 dev->recv = (void *)lpc32xx_eth_recv;
625 dev->write_hwaddr = (void *)lpc32xx_eth_write_hwaddr;
626
627 /* Release SOFT reset to let MII talk to PHY */
628 clrbits_le32(&regs->mac1, MAC1_SOFT_RESET);
629
630 /* register driver before talking to phy */
631 eth_register(dev);
632
633#if defined(CONFIG_PHYLIB)
Vladimir Zapolskiyca0af032015-06-29 03:35:12 +0300634 lpc32xx_eth_phylib_init(dev, CONFIG_PHY_ADDR);
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200635#elif defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500636 int retval;
637 struct mii_dev *mdiodev = mdio_alloc();
638 if (!mdiodev)
639 return -ENOMEM;
640 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
641 mdiodev->read = mii_reg_read;
642 mdiodev->write = mii_reg_write;
643
644 retval = mdio_register(mdiodev);
645 if (retval < 0)
646 return retval;
Albert ARIBAUD \(3ADEV\)391e1632015-03-31 11:40:43 +0200647#endif
648
649 return 0;
650}