blob: a1d12f69027c19a591e403410996cb91476e89d7 [file] [log] [blame]
Stefan Roesef9947682018-10-26 14:53:27 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * MediaTek ethernet IP driver for U-Boot
4 *
5 * Copyright (C) 2018 Stefan Roese <sr@denx.de>
6 *
7 * This code is mostly based on the code extracted from this MediaTek
8 * github repository:
9 *
10 * https://github.com/MediaTek-Labs/linkit-smart-uboot.git
11 *
12 * I was not able to find a specific license or other developers
13 * copyrights here, so I can't add them here.
14 */
15
16#include <common.h>
17#include <dm.h>
18#include <malloc.h>
19#include <miiphy.h>
20#include <net.h>
developere4387bd2019-09-25 17:45:32 +080021#include <reset.h>
Stefan Roesef9947682018-10-26 14:53:27 +020022#include <wait_bit.h>
23#include <asm/io.h>
24#include <linux/bitfield.h>
25#include <linux/err.h>
26
Stefan Roesef9947682018-10-26 14:53:27 +020027/* Ethernet frame engine register */
28#define PDMA_RELATED 0x0800
29
30#define TX_BASE_PTR0 (PDMA_RELATED + 0x000)
31#define TX_MAX_CNT0 (PDMA_RELATED + 0x004)
32#define TX_CTX_IDX0 (PDMA_RELATED + 0x008)
33#define TX_DTX_IDX0 (PDMA_RELATED + 0x00c)
34
35#define RX_BASE_PTR0 (PDMA_RELATED + 0x100)
36#define RX_MAX_CNT0 (PDMA_RELATED + 0x104)
37#define RX_CALC_IDX0 (PDMA_RELATED + 0x108)
38
39#define PDMA_GLO_CFG (PDMA_RELATED + 0x204)
40#define PDMA_RST_IDX (PDMA_RELATED + 0x208)
41#define DLY_INT_CFG (PDMA_RELATED + 0x20c)
42
43#define SDM_RELATED 0x0c00
44
45#define SDM_MAC_ADRL (SDM_RELATED + 0x0c) /* MAC address LSB */
46#define SDM_MAC_ADRH (SDM_RELATED + 0x10) /* MAC Address MSB */
47
48#define RST_DTX_IDX0 BIT(0)
49#define RST_DRX_IDX0 BIT(16)
50
51#define TX_DMA_EN BIT(0)
52#define TX_DMA_BUSY BIT(1)
53#define RX_DMA_EN BIT(2)
54#define RX_DMA_BUSY BIT(3)
55#define TX_WB_DDONE BIT(6)
56
57/* Ethernet switch register */
58#define MT7628_SWITCH_FCT0 0x0008
59#define MT7628_SWITCH_PFC1 0x0014
developer33272b62019-09-25 17:45:35 +080060#define MT7628_SWITCH_PVIDC0 0x0040
61#define MT7628_SWITCH_PVIDC1 0x0044
62#define MT7628_SWITCH_PVIDC2 0x0048
63#define MT7628_SWITCH_PVIDC3 0x004c
64#define MT7628_SWITCH_VMSC0 0x0070
Stefan Roesef9947682018-10-26 14:53:27 +020065#define MT7628_SWITCH_FPA 0x0084
66#define MT7628_SWITCH_SOCPC 0x008c
67#define MT7628_SWITCH_POC0 0x0090
68#define MT7628_SWITCH_POC2 0x0098
69#define MT7628_SWITCH_SGC 0x009c
70#define MT7628_SWITCH_PCR0 0x00c0
71#define PCR0_PHY_ADDR GENMASK(4, 0)
72#define PCR0_PHY_REG GENMASK(12, 8)
73#define PCR0_WT_PHY_CMD BIT(13)
74#define PCR0_RD_PHY_CMD BIT(14)
75#define PCR0_WT_DATA GENMASK(31, 16)
76
77#define MT7628_SWITCH_PCR1 0x00c4
78#define PCR1_WT_DONE BIT(0)
79#define PCR1_RD_RDY BIT(1)
80#define PCR1_RD_DATA GENMASK(31, 16)
81
82#define MT7628_SWITCH_FPA1 0x00c8
83#define MT7628_SWITCH_FCT2 0x00cc
84#define MT7628_SWITCH_SGC2 0x00e4
85#define MT7628_SWITCH_BMU_CTRL 0x0110
86
87/* rxd2 */
88#define RX_DMA_DONE BIT(31)
89#define RX_DMA_LSO BIT(30)
90#define RX_DMA_PLEN0 GENMASK(29, 16)
91#define RX_DMA_TAG BIT(15)
92
93struct fe_rx_dma {
94 unsigned int rxd1;
95 unsigned int rxd2;
96 unsigned int rxd3;
97 unsigned int rxd4;
98} __packed __aligned(4);
99
100#define TX_DMA_PLEN0 GENMASK(29, 16)
101#define TX_DMA_LS1 BIT(14)
102#define TX_DMA_LS0 BIT(30)
103#define TX_DMA_DONE BIT(31)
104
105#define TX_DMA_INS_VLAN_MT7621 BIT(16)
106#define TX_DMA_INS_VLAN BIT(7)
107#define TX_DMA_INS_PPPOE BIT(12)
108#define TX_DMA_PN GENMASK(26, 24)
109
110struct fe_tx_dma {
111 unsigned int txd1;
112 unsigned int txd2;
113 unsigned int txd3;
114 unsigned int txd4;
115} __packed __aligned(4);
116
117#define NUM_RX_DESC 256
118#define NUM_TX_DESC 4
developera5644112019-09-25 17:45:33 +0800119#define NUM_PHYS 5
Stefan Roesef9947682018-10-26 14:53:27 +0200120
121#define PADDING_LENGTH 60
122
123#define MTK_QDMA_PAGE_SIZE 2048
124
125#define CONFIG_MDIO_TIMEOUT 100
126#define CONFIG_DMA_STOP_TIMEOUT 100
127#define CONFIG_TX_DMA_TIMEOUT 100
128
Stefan Roesef9947682018-10-26 14:53:27 +0200129struct mt7628_eth_dev {
130 void __iomem *base; /* frame engine base address */
131 void __iomem *eth_sw_base; /* switch base address */
Stefan Roesef9947682018-10-26 14:53:27 +0200132
133 struct mii_dev *bus;
134
135 struct fe_tx_dma *tx_ring;
136 struct fe_rx_dma *rx_ring;
137
138 u8 *rx_buf[NUM_RX_DESC];
139
140 /* Point to the next RXD DMA wants to use in RXD Ring0 */
141 int rx_dma_idx;
142 /* Point to the next TXD in TXD Ring0 CPU wants to use */
143 int tx_dma_idx;
developere4387bd2019-09-25 17:45:32 +0800144
145 struct reset_ctl rst_ephy;
developera5644112019-09-25 17:45:33 +0800146
147 struct phy_device *phy;
developer33272b62019-09-25 17:45:35 +0800148
149 int wan_port;
Stefan Roesef9947682018-10-26 14:53:27 +0200150};
151
developerba57a6b2019-09-25 17:45:34 +0800152static int mt7628_eth_free_pkt(struct udevice *dev, uchar *packet, int length);
153
Stefan Roesef9947682018-10-26 14:53:27 +0200154static int mdio_wait_read(struct mt7628_eth_dev *priv, u32 mask, bool mask_set)
155{
156 void __iomem *base = priv->eth_sw_base;
157 int ret;
158
159 ret = wait_for_bit_le32(base + MT7628_SWITCH_PCR1, mask, mask_set,
160 CONFIG_MDIO_TIMEOUT, false);
161 if (ret) {
162 printf("MDIO operation timeout!\n");
163 return -ETIMEDOUT;
164 }
165
166 return 0;
167}
168
169static int mii_mgr_read(struct mt7628_eth_dev *priv,
170 u32 phy_addr, u32 phy_register, u32 *read_data)
171{
172 void __iomem *base = priv->eth_sw_base;
173 u32 status = 0;
174 u32 ret;
175
176 *read_data = 0xffff;
177 /* Make sure previous read operation is complete */
178 ret = mdio_wait_read(priv, PCR1_RD_RDY, false);
179 if (ret)
180 return ret;
181
182 writel(PCR0_RD_PHY_CMD |
183 FIELD_PREP(PCR0_PHY_REG, phy_register) |
184 FIELD_PREP(PCR0_PHY_ADDR, phy_addr),
185 base + MT7628_SWITCH_PCR0);
186
187 /* Make sure previous read operation is complete */
188 ret = mdio_wait_read(priv, PCR1_RD_RDY, true);
189 if (ret)
190 return ret;
191
192 status = readl(base + MT7628_SWITCH_PCR1);
193 *read_data = FIELD_GET(PCR1_RD_DATA, status);
194
195 return 0;
196}
197
198static int mii_mgr_write(struct mt7628_eth_dev *priv,
199 u32 phy_addr, u32 phy_register, u32 write_data)
200{
201 void __iomem *base = priv->eth_sw_base;
202 u32 data;
203 int ret;
204
205 /* Make sure previous write operation is complete */
206 ret = mdio_wait_read(priv, PCR1_WT_DONE, false);
207 if (ret)
208 return ret;
209
210 data = FIELD_PREP(PCR0_WT_DATA, write_data) |
211 FIELD_PREP(PCR0_PHY_REG, phy_register) |
212 FIELD_PREP(PCR0_PHY_ADDR, phy_addr) |
213 PCR0_WT_PHY_CMD;
214 writel(data, base + MT7628_SWITCH_PCR0);
215
216 return mdio_wait_read(priv, PCR1_WT_DONE, true);
217}
218
219static int mt7628_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
220{
221 u32 val;
222 int ret;
223
224 ret = mii_mgr_read(bus->priv, addr, reg, &val);
225 if (ret)
226 return ret;
227
228 return val;
229}
230
231static int mt7628_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
232 u16 value)
233{
234 return mii_mgr_write(bus->priv, addr, reg, value);
235}
236
237static void mt7628_ephy_init(struct mt7628_eth_dev *priv)
238{
239 int i;
240
241 mii_mgr_write(priv, 0, 31, 0x2000); /* change G2 page */
242 mii_mgr_write(priv, 0, 26, 0x0000);
243
244 for (i = 0; i < 5; i++) {
245 mii_mgr_write(priv, i, 31, 0x8000); /* change L0 page */
246 mii_mgr_write(priv, i, 0, 0x3100);
247
248 /* EEE disable */
249 mii_mgr_write(priv, i, 30, 0xa000);
250 mii_mgr_write(priv, i, 31, 0xa000); /* change L2 page */
251 mii_mgr_write(priv, i, 16, 0x0606);
252 mii_mgr_write(priv, i, 23, 0x0f0e);
253 mii_mgr_write(priv, i, 24, 0x1610);
254 mii_mgr_write(priv, i, 30, 0x1f15);
255 mii_mgr_write(priv, i, 28, 0x6111);
256 }
257
258 /* 100Base AOI setting */
259 mii_mgr_write(priv, 0, 31, 0x5000); /* change G5 page */
260 mii_mgr_write(priv, 0, 19, 0x004a);
261 mii_mgr_write(priv, 0, 20, 0x015a);
262 mii_mgr_write(priv, 0, 21, 0x00ee);
263 mii_mgr_write(priv, 0, 22, 0x0033);
264 mii_mgr_write(priv, 0, 23, 0x020a);
265 mii_mgr_write(priv, 0, 24, 0x0000);
266 mii_mgr_write(priv, 0, 25, 0x024a);
267 mii_mgr_write(priv, 0, 26, 0x035a);
268 mii_mgr_write(priv, 0, 27, 0x02ee);
269 mii_mgr_write(priv, 0, 28, 0x0233);
270 mii_mgr_write(priv, 0, 29, 0x000a);
271 mii_mgr_write(priv, 0, 30, 0x0000);
272
273 /* Fix EPHY idle state abnormal behavior */
274 mii_mgr_write(priv, 0, 31, 0x4000); /* change G4 page */
275 mii_mgr_write(priv, 0, 29, 0x000d);
276 mii_mgr_write(priv, 0, 30, 0x0500);
277}
278
279static void rt305x_esw_init(struct mt7628_eth_dev *priv)
280{
281 void __iomem *base = priv->eth_sw_base;
developer33272b62019-09-25 17:45:35 +0800282 void __iomem *reg;
283 u32 val = 0, pvid;
284 int i;
Stefan Roesef9947682018-10-26 14:53:27 +0200285
286 /*
287 * FC_RLS_TH=200, FC_SET_TH=160
288 * DROP_RLS=120, DROP_SET_TH=80
289 */
290 writel(0xc8a07850, base + MT7628_SWITCH_FCT0);
291 writel(0x00000000, base + MT7628_SWITCH_SGC2);
292 writel(0x00405555, base + MT7628_SWITCH_PFC1);
293 writel(0x00007f7f, base + MT7628_SWITCH_POC0);
294 writel(0x00007f7f, base + MT7628_SWITCH_POC2); /* disable VLAN */
295 writel(0x0002500c, base + MT7628_SWITCH_FCT2);
296 /* hashing algorithm=XOR48, aging interval=300sec */
297 writel(0x0008a301, base + MT7628_SWITCH_SGC);
298 writel(0x02404040, base + MT7628_SWITCH_SOCPC);
299
300 /* Ext PHY Addr=0x1f */
301 writel(0x3f502b28, base + MT7628_SWITCH_FPA1);
302 writel(0x00000000, base + MT7628_SWITCH_FPA);
303 /* 1us cycle number=125 (FE's clock=125Mhz) */
304 writel(0x7d000000, base + MT7628_SWITCH_BMU_CTRL);
305
developer33272b62019-09-25 17:45:35 +0800306 /* LAN/WAN partition, WAN port will be unusable in u-boot network */
307 if (priv->wan_port >= 0 && priv->wan_port < 6) {
308 for (i = 0; i < 8; i++) {
309 pvid = i == priv->wan_port ? 2 : 1;
310 reg = base + MT7628_SWITCH_PVIDC0 + (i / 2) * 4;
311 if (i % 2 == 0) {
312 val = pvid;
313 } else {
314 val |= (pvid << 12);
315 writel(val, reg);
316 }
317 }
318
319 val = 0xffff407f;
320 val |= 1 << (8 + priv->wan_port);
321 val &= ~(1 << priv->wan_port);
322 writel(val, base + MT7628_SWITCH_VMSC0);
323 }
324
Stefan Roesef9947682018-10-26 14:53:27 +0200325 /* Reset PHY */
developere4387bd2019-09-25 17:45:32 +0800326 reset_assert(&priv->rst_ephy);
327 reset_deassert(&priv->rst_ephy);
Stefan Roesef9947682018-10-26 14:53:27 +0200328 mdelay(10);
329
Stefan Roesef9947682018-10-26 14:53:27 +0200330 mt7628_ephy_init(priv);
331}
332
333static void eth_dma_start(struct mt7628_eth_dev *priv)
334{
335 void __iomem *base = priv->base;
336
337 setbits_le32(base + PDMA_GLO_CFG, TX_WB_DDONE | RX_DMA_EN | TX_DMA_EN);
338}
339
340static void eth_dma_stop(struct mt7628_eth_dev *priv)
341{
342 void __iomem *base = priv->base;
343 int ret;
344
345 clrbits_le32(base + PDMA_GLO_CFG, TX_WB_DDONE | RX_DMA_EN | TX_DMA_EN);
346
347 /* Wait for DMA to stop */
348 ret = wait_for_bit_le32(base + PDMA_GLO_CFG,
349 RX_DMA_BUSY | TX_DMA_BUSY, false,
350 CONFIG_DMA_STOP_TIMEOUT, false);
351 if (ret)
352 printf("DMA stop timeout error!\n");
353}
354
355static int mt7628_eth_write_hwaddr(struct udevice *dev)
356{
357 struct mt7628_eth_dev *priv = dev_get_priv(dev);
358 void __iomem *base = priv->base;
359 u8 *addr = ((struct eth_pdata *)dev_get_platdata(dev))->enetaddr;
360 u32 val;
361
362 /* Set MAC address. */
363 val = addr[0];
364 val = (val << 8) | addr[1];
365 writel(val, base + SDM_MAC_ADRH);
366
367 val = addr[2];
368 val = (val << 8) | addr[3];
369 val = (val << 8) | addr[4];
370 val = (val << 8) | addr[5];
371 writel(val, base + SDM_MAC_ADRL);
372
373 return 0;
374}
375
376static int mt7628_eth_send(struct udevice *dev, void *packet, int length)
377{
378 struct mt7628_eth_dev *priv = dev_get_priv(dev);
379 void __iomem *base = priv->base;
380 int ret;
381 int idx;
382 int i;
383
384 idx = priv->tx_dma_idx;
385
386 /* Pad message to a minimum length */
387 if (length < PADDING_LENGTH) {
388 char *p = (char *)packet;
389
390 for (i = 0; i < PADDING_LENGTH - length; i++)
391 p[length + i] = 0;
392 length = PADDING_LENGTH;
393 }
394
395 /* Check if buffer is ready for next TX DMA */
396 ret = wait_for_bit_le32(&priv->tx_ring[idx].txd2, TX_DMA_DONE, true,
397 CONFIG_TX_DMA_TIMEOUT, false);
398 if (ret) {
399 printf("TX: DMA still busy on buffer %d\n", idx);
400 return ret;
401 }
402
403 flush_dcache_range((u32)packet, (u32)packet + length);
404
405 priv->tx_ring[idx].txd1 = CPHYSADDR(packet);
406 priv->tx_ring[idx].txd2 &= ~TX_DMA_PLEN0;
407 priv->tx_ring[idx].txd2 |= FIELD_PREP(TX_DMA_PLEN0, length);
408 priv->tx_ring[idx].txd2 &= ~TX_DMA_DONE;
409
410 idx = (idx + 1) % NUM_TX_DESC;
411
412 /* Make sure the writes executed at this place */
413 wmb();
414 writel(idx, base + TX_CTX_IDX0);
415
416 priv->tx_dma_idx = idx;
417
418 return 0;
419}
420
421static int mt7628_eth_recv(struct udevice *dev, int flags, uchar **packetp)
422{
423 struct mt7628_eth_dev *priv = dev_get_priv(dev);
424 u32 rxd_info;
425 int length;
426 int idx;
427
428 idx = priv->rx_dma_idx;
429
430 rxd_info = priv->rx_ring[idx].rxd2;
431 if ((rxd_info & RX_DMA_DONE) == 0)
432 return -EAGAIN;
433
434 length = FIELD_GET(RX_DMA_PLEN0, priv->rx_ring[idx].rxd2);
435 if (length == 0 || length > MTK_QDMA_PAGE_SIZE) {
436 printf("%s: invalid length (%d bytes)\n", __func__, length);
developerba57a6b2019-09-25 17:45:34 +0800437 mt7628_eth_free_pkt(dev, NULL, 0);
Stefan Roesef9947682018-10-26 14:53:27 +0200438 return -EIO;
439 }
440
441 *packetp = priv->rx_buf[idx];
442 invalidate_dcache_range((u32)*packetp, (u32)*packetp + length);
443
444 priv->rx_ring[idx].rxd4 = 0;
445 priv->rx_ring[idx].rxd2 = RX_DMA_LSO;
446
447 /* Make sure the writes executed at this place */
448 wmb();
449
450 return length;
451}
452
453static int mt7628_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
454{
455 struct mt7628_eth_dev *priv = dev_get_priv(dev);
456 void __iomem *base = priv->base;
457 int idx;
458
459 idx = priv->rx_dma_idx;
460
461 /* Move point to next RXD which wants to alloc */
462 writel(idx, base + RX_CALC_IDX0);
463
464 /* Update to Next packet point that was received */
465 idx = (idx + 1) % NUM_RX_DESC;
466
467 priv->rx_dma_idx = idx;
468
469 return 0;
470}
471
Stefan Roesef9947682018-10-26 14:53:27 +0200472static int mt7628_eth_start(struct udevice *dev)
473{
474 struct mt7628_eth_dev *priv = dev_get_priv(dev);
475 void __iomem *base = priv->base;
476 uchar packet[MTK_QDMA_PAGE_SIZE];
477 uchar *packetp;
developera5644112019-09-25 17:45:33 +0800478 int ret;
Stefan Roesef9947682018-10-26 14:53:27 +0200479 int i;
480
481 for (i = 0; i < NUM_RX_DESC; i++) {
482 memset((void *)&priv->rx_ring[i], 0, sizeof(priv->rx_ring[0]));
483 priv->rx_ring[i].rxd2 |= RX_DMA_LSO;
484 priv->rx_ring[i].rxd1 = CPHYSADDR(priv->rx_buf[i]);
485 }
486
487 for (i = 0; i < NUM_TX_DESC; i++) {
488 memset((void *)&priv->tx_ring[i], 0, sizeof(priv->tx_ring[0]));
489 priv->tx_ring[i].txd2 = TX_DMA_LS0 | TX_DMA_DONE;
490 priv->tx_ring[i].txd4 = FIELD_PREP(TX_DMA_PN, 1);
491 }
492
493 priv->rx_dma_idx = 0;
494 priv->tx_dma_idx = 0;
495
496 /* Make sure the writes executed at this place */
497 wmb();
498
499 /* disable delay interrupt */
500 writel(0, base + DLY_INT_CFG);
501
502 clrbits_le32(base + PDMA_GLO_CFG, 0xffff0000);
503
504 /* Tell the adapter where the TX/RX rings are located. */
505 writel(CPHYSADDR(&priv->rx_ring[0]), base + RX_BASE_PTR0);
506 writel(CPHYSADDR((u32)&priv->tx_ring[0]), base + TX_BASE_PTR0);
507
508 writel(NUM_RX_DESC, base + RX_MAX_CNT0);
509 writel(NUM_TX_DESC, base + TX_MAX_CNT0);
510
511 writel(priv->tx_dma_idx, base + TX_CTX_IDX0);
512 writel(RST_DTX_IDX0, base + PDMA_RST_IDX);
513
514 writel(NUM_RX_DESC - 1, base + RX_CALC_IDX0);
515 writel(RST_DRX_IDX0, base + PDMA_RST_IDX);
516
517 /* Make sure the writes executed at this place */
518 wmb();
519 eth_dma_start(priv);
520
developera5644112019-09-25 17:45:33 +0800521 if (priv->phy) {
522 ret = phy_startup(priv->phy);
523 if (ret)
524 return ret;
Stefan Roesef9947682018-10-26 14:53:27 +0200525
developera5644112019-09-25 17:45:33 +0800526 if (!priv->phy->link)
527 return -EAGAIN;
Stefan Roesef9947682018-10-26 14:53:27 +0200528 }
529
530 /*
531 * The integrated switch seems to queue some received ethernet
532 * packets in some FIFO. Lets read the already queued packets
533 * out by using the receive routine, so that these old messages
534 * are dropped before the new xfer starts.
535 */
536 packetp = &packet[0];
537 while (mt7628_eth_recv(dev, 0, &packetp) != -EAGAIN)
538 mt7628_eth_free_pkt(dev, packetp, 0);
539
540 return 0;
541}
542
543static void mt7628_eth_stop(struct udevice *dev)
544{
545 struct mt7628_eth_dev *priv = dev_get_priv(dev);
546
547 eth_dma_stop(priv);
548}
549
550static int mt7628_eth_probe(struct udevice *dev)
551{
552 struct mt7628_eth_dev *priv = dev_get_priv(dev);
Stefan Roesef9947682018-10-26 14:53:27 +0200553 struct mii_dev *bus;
developera5644112019-09-25 17:45:33 +0800554 int poll_link_phy;
Stefan Roesef9947682018-10-26 14:53:27 +0200555 int ret;
556 int i;
557
558 /* Save frame-engine base address for later use */
559 priv->base = dev_remap_addr_index(dev, 0);
560 if (IS_ERR(priv->base))
561 return PTR_ERR(priv->base);
562
563 /* Save switch base address for later use */
564 priv->eth_sw_base = dev_remap_addr_index(dev, 1);
565 if (IS_ERR(priv->eth_sw_base))
566 return PTR_ERR(priv->eth_sw_base);
567
developere4387bd2019-09-25 17:45:32 +0800568 /* Reset controller */
569 ret = reset_get_by_name(dev, "ephy", &priv->rst_ephy);
Stefan Roesef9947682018-10-26 14:53:27 +0200570 if (ret) {
developere4387bd2019-09-25 17:45:32 +0800571 pr_err("unable to find reset controller for ethernet PHYs\n");
Stefan Roesef9947682018-10-26 14:53:27 +0200572 return ret;
573 }
574
developer33272b62019-09-25 17:45:35 +0800575 /* WAN port will be isolated from LAN ports */
576 priv->wan_port = dev_read_u32_default(dev, "mediatek,wan-port", -1);
577
Stefan Roesef9947682018-10-26 14:53:27 +0200578 /* Put rx and tx rings into KSEG1 area (uncached) */
579 priv->tx_ring = (struct fe_tx_dma *)
580 KSEG1ADDR(memalign(ARCH_DMA_MINALIGN,
581 sizeof(*priv->tx_ring) * NUM_TX_DESC));
582 priv->rx_ring = (struct fe_rx_dma *)
583 KSEG1ADDR(memalign(ARCH_DMA_MINALIGN,
584 sizeof(*priv->rx_ring) * NUM_RX_DESC));
585
586 for (i = 0; i < NUM_RX_DESC; i++)
587 priv->rx_buf[i] = memalign(PKTALIGN, MTK_QDMA_PAGE_SIZE);
588
589 bus = mdio_alloc();
590 if (!bus) {
591 printf("Failed to allocate MDIO bus\n");
592 return -ENOMEM;
593 }
594
595 bus->read = mt7628_mdio_read;
596 bus->write = mt7628_mdio_write;
597 snprintf(bus->name, sizeof(bus->name), dev->name);
598 bus->priv = (void *)priv;
599
600 ret = mdio_register(bus);
601 if (ret)
602 return ret;
603
developera5644112019-09-25 17:45:33 +0800604 poll_link_phy = dev_read_u32_default(dev, "mediatek,poll-link-phy", -1);
605 if (poll_link_phy >= 0) {
606 if (poll_link_phy >= NUM_PHYS) {
607 pr_err("invalid phy %d for poll-link-phy\n",
608 poll_link_phy);
609 return ret;
610 }
611
612 priv->phy = phy_connect(bus, poll_link_phy, dev,
613 PHY_INTERFACE_MODE_MII);
614 if (!priv->phy) {
615 pr_err("failed to probe phy %d\n", poll_link_phy);
616 return -ENODEV;
617 }
618
619 priv->phy->advertising = priv->phy->supported;
620 phy_config(priv->phy);
621 }
622
Stefan Roesef9947682018-10-26 14:53:27 +0200623 /* Switch configuration */
624 rt305x_esw_init(priv);
625
626 return 0;
627}
628
629static const struct eth_ops mt7628_eth_ops = {
630 .start = mt7628_eth_start,
631 .send = mt7628_eth_send,
632 .recv = mt7628_eth_recv,
633 .free_pkt = mt7628_eth_free_pkt,
634 .stop = mt7628_eth_stop,
635 .write_hwaddr = mt7628_eth_write_hwaddr,
636};
637
638static const struct udevice_id mt7628_eth_ids[] = {
639 { .compatible = "mediatek,mt7628-eth" },
640 { }
641};
642
643U_BOOT_DRIVER(mt7628_eth) = {
644 .name = "mt7628_eth",
645 .id = UCLASS_ETH,
646 .of_match = mt7628_eth_ids,
647 .probe = mt7628_eth_probe,
648 .ops = &mt7628_eth_ops,
649 .priv_auto_alloc_size = sizeof(struct mt7628_eth_dev),
650 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
651};