26c75067bf2c834dda6b165194036131e6077533
[oweals/u-boot.git] / drivers / net / sunxi_emac.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * sunxi_emac.c -- Allwinner A10 ethernet driver
4  *
5  * (C) Copyright 2012, Stefan Roese <sr@denx.de>
6  */
7
8 #include <common.h>
9 #include <clk.h>
10 #include <dm.h>
11 #include <log.h>
12 #include <dm/device_compat.h>
13 #include <linux/err.h>
14 #include <malloc.h>
15 #include <miiphy.h>
16 #include <net.h>
17 #include <asm/io.h>
18 #include <asm/arch/clock.h>
19 #include <asm/arch/gpio.h>
20
21 /* EMAC register  */
22 struct emac_regs {
23         u32 ctl;        /* 0x00 */
24         u32 tx_mode;    /* 0x04 */
25         u32 tx_flow;    /* 0x08 */
26         u32 tx_ctl0;    /* 0x0c */
27         u32 tx_ctl1;    /* 0x10 */
28         u32 tx_ins;     /* 0x14 */
29         u32 tx_pl0;     /* 0x18 */
30         u32 tx_pl1;     /* 0x1c */
31         u32 tx_sta;     /* 0x20 */
32         u32 tx_io_data; /* 0x24 */
33         u32 tx_io_data1;/* 0x28 */
34         u32 tx_tsvl0;   /* 0x2c */
35         u32 tx_tsvh0;   /* 0x30 */
36         u32 tx_tsvl1;   /* 0x34 */
37         u32 tx_tsvh1;   /* 0x38 */
38         u32 rx_ctl;     /* 0x3c */
39         u32 rx_hash0;   /* 0x40 */
40         u32 rx_hash1;   /* 0x44 */
41         u32 rx_sta;     /* 0x48 */
42         u32 rx_io_data; /* 0x4c */
43         u32 rx_fbc;     /* 0x50 */
44         u32 int_ctl;    /* 0x54 */
45         u32 int_sta;    /* 0x58 */
46         u32 mac_ctl0;   /* 0x5c */
47         u32 mac_ctl1;   /* 0x60 */
48         u32 mac_ipgt;   /* 0x64 */
49         u32 mac_ipgr;   /* 0x68 */
50         u32 mac_clrt;   /* 0x6c */
51         u32 mac_maxf;   /* 0x70 */
52         u32 mac_supp;   /* 0x74 */
53         u32 mac_test;   /* 0x78 */
54         u32 mac_mcfg;   /* 0x7c */
55         u32 mac_mcmd;   /* 0x80 */
56         u32 mac_madr;   /* 0x84 */
57         u32 mac_mwtd;   /* 0x88 */
58         u32 mac_mrdd;   /* 0x8c */
59         u32 mac_mind;   /* 0x90 */
60         u32 mac_ssrr;   /* 0x94 */
61         u32 mac_a0;     /* 0x98 */
62         u32 mac_a1;     /* 0x9c */
63 };
64
65 /* SRAMC register  */
66 struct sunxi_sramc_regs {
67         u32 ctrl0;
68         u32 ctrl1;
69 };
70
71 /* 0: Disable       1: Aborted frame enable(default) */
72 #define EMAC_TX_AB_M            (0x1 << 0)
73 /* 0: CPU           1: DMA(default) */
74 #define EMAC_TX_TM              (0x1 << 1)
75
76 #define EMAC_TX_SETUP           (0)
77
78 /* 0: DRQ asserted  1: DRQ automatically(default) */
79 #define EMAC_RX_DRQ_MODE        (0x1 << 1)
80 /* 0: CPU           1: DMA(default) */
81 #define EMAC_RX_TM              (0x1 << 2)
82 /* 0: Normal(default)        1: Pass all Frames */
83 #define EMAC_RX_PA              (0x1 << 4)
84 /* 0: Normal(default)        1: Pass Control Frames */
85 #define EMAC_RX_PCF             (0x1 << 5)
86 /* 0: Normal(default)        1: Pass Frames with CRC Error */
87 #define EMAC_RX_PCRCE           (0x1 << 6)
88 /* 0: Normal(default)        1: Pass Frames with Length Error */
89 #define EMAC_RX_PLE             (0x1 << 7)
90 /* 0: Normal                 1: Pass Frames length out of range(default) */
91 #define EMAC_RX_POR             (0x1 << 8)
92 /* 0: Not accept             1: Accept unicast Packets(default) */
93 #define EMAC_RX_UCAD            (0x1 << 16)
94 /* 0: Normal(default)        1: DA Filtering */
95 #define EMAC_RX_DAF             (0x1 << 17)
96 /* 0: Not accept             1: Accept multicast Packets(default) */
97 #define EMAC_RX_MCO             (0x1 << 20)
98 /* 0: Disable(default)       1: Enable Hash filter */
99 #define EMAC_RX_MHF             (0x1 << 21)
100 /* 0: Not accept             1: Accept Broadcast Packets(default) */
101 #define EMAC_RX_BCO             (0x1 << 22)
102 /* 0: Disable(default)       1: Enable SA Filtering */
103 #define EMAC_RX_SAF             (0x1 << 24)
104 /* 0: Normal(default)        1: Inverse Filtering */
105 #define EMAC_RX_SAIF            (0x1 << 25)
106
107 #define EMAC_RX_SETUP           (EMAC_RX_POR | EMAC_RX_UCAD | EMAC_RX_DAF | \
108                                  EMAC_RX_MCO | EMAC_RX_BCO)
109
110 /* 0: Disable                1: Enable Receive Flow Control(default) */
111 #define EMAC_MAC_CTL0_RFC       (0x1 << 2)
112 /* 0: Disable                1: Enable Transmit Flow Control(default) */
113 #define EMAC_MAC_CTL0_TFC       (0x1 << 3)
114
115 #define EMAC_MAC_CTL0_SETUP     (EMAC_MAC_CTL0_RFC | EMAC_MAC_CTL0_TFC)
116
117 /* 0: Disable                1: Enable MAC Frame Length Checking(default) */
118 #define EMAC_MAC_CTL1_FLC       (0x1 << 1)
119 /* 0: Disable(default)       1: Enable Huge Frame */
120 #define EMAC_MAC_CTL1_HF        (0x1 << 2)
121 /* 0: Disable(default)       1: Enable MAC Delayed CRC */
122 #define EMAC_MAC_CTL1_DCRC      (0x1 << 3)
123 /* 0: Disable                1: Enable MAC CRC(default) */
124 #define EMAC_MAC_CTL1_CRC       (0x1 << 4)
125 /* 0: Disable                1: Enable MAC PAD Short frames(default) */
126 #define EMAC_MAC_CTL1_PC        (0x1 << 5)
127 /* 0: Disable(default)       1: Enable MAC PAD Short frames and append CRC */
128 #define EMAC_MAC_CTL1_VC        (0x1 << 6)
129 /* 0: Disable(default)       1: Enable MAC auto detect Short frames */
130 #define EMAC_MAC_CTL1_ADP       (0x1 << 7)
131 /* 0: Disable(default)       1: Enable */
132 #define EMAC_MAC_CTL1_PRE       (0x1 << 8)
133 /* 0: Disable(default)       1: Enable */
134 #define EMAC_MAC_CTL1_LPE       (0x1 << 9)
135 /* 0: Disable(default)       1: Enable no back off */
136 #define EMAC_MAC_CTL1_NB        (0x1 << 12)
137 /* 0: Disable(default)       1: Enable */
138 #define EMAC_MAC_CTL1_BNB       (0x1 << 13)
139 /* 0: Disable(default)       1: Enable */
140 #define EMAC_MAC_CTL1_ED        (0x1 << 14)
141
142 #define EMAC_MAC_CTL1_SETUP     (EMAC_MAC_CTL1_FLC | EMAC_MAC_CTL1_CRC | \
143                                  EMAC_MAC_CTL1_PC)
144
145 #define EMAC_MAC_IPGT           0x15
146
147 #define EMAC_MAC_NBTB_IPG1      0xc
148 #define EMAC_MAC_NBTB_IPG2      0x12
149
150 #define EMAC_MAC_CW             0x37
151 #define EMAC_MAC_RM             0xf
152
153 #define EMAC_MAC_MFL            0x0600
154
155 /* Receive status */
156 #define EMAC_CRCERR             (0x1 << 4)
157 #define EMAC_LENERR             (0x3 << 5)
158
159 #define EMAC_RX_BUFSIZE         2000
160
161 struct emac_eth_dev {
162         struct emac_regs *regs;
163         struct clk clk;
164         struct mii_dev *bus;
165         struct phy_device *phydev;
166         int link_printed;
167 #ifdef CONFIG_DM_ETH
168         uchar rx_buf[EMAC_RX_BUFSIZE];
169 #endif
170 };
171
172 struct emac_rxhdr {
173         s16 rx_len;
174         u16 rx_status;
175 };
176
177 static void emac_inblk_32bit(void *reg, void *data, int count)
178 {
179         int cnt = (count + 3) >> 2;
180
181         if (cnt) {
182                 u32 *buf = data;
183
184                 do {
185                         u32 x = readl(reg);
186                         *buf++ = x;
187                 } while (--cnt);
188         }
189 }
190
191 static void emac_outblk_32bit(void *reg, void *data, int count)
192 {
193         int cnt = (count + 3) >> 2;
194
195         if (cnt) {
196                 const u32 *buf = data;
197
198                 do {
199                         writel(*buf++, reg);
200                 } while (--cnt);
201         }
202 }
203
204 /* Read a word from phyxcer */
205 static int emac_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
206 {
207         struct emac_eth_dev *priv = bus->priv;
208         struct emac_regs *regs = priv->regs;
209
210         /* issue the phy address and reg */
211         writel(addr << 8 | reg, &regs->mac_madr);
212
213         /* pull up the phy io line */
214         writel(0x1, &regs->mac_mcmd);
215
216         /* Wait read complete */
217         mdelay(1);
218
219         /* push down the phy io line */
220         writel(0x0, &regs->mac_mcmd);
221
222         /* And read data */
223         return readl(&regs->mac_mrdd);
224 }
225
226 /* Write a word to phyxcer */
227 static int emac_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
228                           u16 value)
229 {
230         struct emac_eth_dev *priv = bus->priv;
231         struct emac_regs *regs = priv->regs;
232
233         /* issue the phy address and reg */
234         writel(addr << 8 | reg, &regs->mac_madr);
235
236         /* pull up the phy io line */
237         writel(0x1, &regs->mac_mcmd);
238
239         /* Wait write complete */
240         mdelay(1);
241
242         /* push down the phy io line */
243         writel(0x0, &regs->mac_mcmd);
244
245         /* and write data */
246         writel(value, &regs->mac_mwtd);
247
248         return 0;
249 }
250
251 static int sunxi_emac_init_phy(struct emac_eth_dev *priv, void *dev)
252 {
253         int ret, mask = 0xffffffff;
254
255 #ifdef CONFIG_PHY_ADDR
256         mask = 1 << CONFIG_PHY_ADDR;
257 #endif
258
259         priv->bus = mdio_alloc();
260         if (!priv->bus) {
261                 printf("Failed to allocate MDIO bus\n");
262                 return -ENOMEM;
263         }
264
265         priv->bus->read = emac_mdio_read;
266         priv->bus->write = emac_mdio_write;
267         priv->bus->priv = priv;
268         strcpy(priv->bus->name, "emac");
269
270         ret = mdio_register(priv->bus);
271         if (ret)
272                 return ret;
273
274         priv->phydev = phy_find_by_mask(priv->bus, mask,
275                                         PHY_INTERFACE_MODE_MII);
276         if (!priv->phydev)
277                 return -ENODEV;
278
279         phy_connect_dev(priv->phydev, dev);
280         phy_config(priv->phydev);
281
282         return 0;
283 }
284
285 static void emac_setup(struct emac_eth_dev *priv)
286 {
287         struct emac_regs *regs = priv->regs;
288         u32 reg_val;
289
290         /* Set up TX */
291         writel(EMAC_TX_SETUP, &regs->tx_mode);
292
293         /* Set up RX */
294         writel(EMAC_RX_SETUP, &regs->rx_ctl);
295
296         /* Set MAC */
297         /* Set MAC CTL0 */
298         writel(EMAC_MAC_CTL0_SETUP, &regs->mac_ctl0);
299
300         /* Set MAC CTL1 */
301         reg_val = 0;
302         if (priv->phydev->duplex == DUPLEX_FULL)
303                 reg_val = (0x1 << 0);
304         writel(EMAC_MAC_CTL1_SETUP | reg_val, &regs->mac_ctl1);
305
306         /* Set up IPGT */
307         writel(EMAC_MAC_IPGT, &regs->mac_ipgt);
308
309         /* Set up IPGR */
310         writel(EMAC_MAC_NBTB_IPG2 | (EMAC_MAC_NBTB_IPG1 << 8), &regs->mac_ipgr);
311
312         /* Set up Collison window */
313         writel(EMAC_MAC_RM | (EMAC_MAC_CW << 8), &regs->mac_clrt);
314
315         /* Set up Max Frame Length */
316         writel(EMAC_MAC_MFL, &regs->mac_maxf);
317 }
318
319 static void emac_reset(struct emac_eth_dev *priv)
320 {
321         struct emac_regs *regs = priv->regs;
322
323         debug("resetting device\n");
324
325         /* RESET device */
326         writel(0, &regs->ctl);
327         udelay(200);
328
329         writel(1, &regs->ctl);
330         udelay(200);
331 }
332
333 static int _sunxi_write_hwaddr(struct emac_eth_dev *priv, u8 *enetaddr)
334 {
335         struct emac_regs *regs = priv->regs;
336         u32 enetaddr_lo, enetaddr_hi;
337
338         enetaddr_lo = enetaddr[2] | (enetaddr[1] << 8) | (enetaddr[0] << 16);
339         enetaddr_hi = enetaddr[5] | (enetaddr[4] << 8) | (enetaddr[3] << 16);
340
341         writel(enetaddr_hi, &regs->mac_a0);
342         writel(enetaddr_lo, &regs->mac_a1);
343
344         return 0;
345 }
346
347 static int _sunxi_emac_eth_init(struct emac_eth_dev *priv, u8 *enetaddr)
348 {
349         struct emac_regs *regs = priv->regs;
350         int ret;
351
352         /* Init EMAC */
353
354         /* Flush RX FIFO */
355         setbits_le32(&regs->rx_ctl, 0x8);
356         udelay(1);
357
358         /* Init MAC */
359
360         /* Soft reset MAC */
361         clrbits_le32(&regs->mac_ctl0, 0x1 << 15);
362
363         /* Clear RX counter */
364         writel(0x0, &regs->rx_fbc);
365         udelay(1);
366
367         /* Set up EMAC */
368         emac_setup(priv);
369
370         _sunxi_write_hwaddr(priv, enetaddr);
371
372         mdelay(1);
373
374         emac_reset(priv);
375
376         /* PHY POWER UP */
377         ret = phy_startup(priv->phydev);
378         if (ret) {
379                 printf("Could not initialize PHY %s\n",
380                        priv->phydev->dev->name);
381                 return ret;
382         }
383
384         /* Print link status only once */
385         if (!priv->link_printed) {
386                 printf("ENET Speed is %d Mbps - %s duplex connection\n",
387                        priv->phydev->speed,
388                        priv->phydev->duplex ? "FULL" : "HALF");
389                 priv->link_printed = 1;
390         }
391
392         /* Set EMAC SPEED depend on PHY */
393         if (priv->phydev->speed == SPEED_100)
394                 setbits_le32(&regs->mac_supp, 1 << 8);
395         else
396                 clrbits_le32(&regs->mac_supp, 1 << 8);
397
398         /* Set duplex depend on phy */
399         if (priv->phydev->duplex == DUPLEX_FULL)
400                 setbits_le32(&regs->mac_ctl1, 1 << 0);
401         else
402                 clrbits_le32(&regs->mac_ctl1, 1 << 0);
403
404         /* Enable RX/TX */
405         setbits_le32(&regs->ctl, 0x7);
406
407         return 0;
408 }
409
410 static int _sunxi_emac_eth_recv(struct emac_eth_dev *priv, void *packet)
411 {
412         struct emac_regs *regs = priv->regs;
413         struct emac_rxhdr rxhdr;
414         u32 rxcount;
415         u32 reg_val;
416         int rx_len;
417         int rx_status;
418         int good_packet;
419
420         /* Check packet ready or not */
421
422         /* Race warning: The first packet might arrive with
423          * the interrupts disabled, but the second will fix
424          */
425         rxcount = readl(&regs->rx_fbc);
426         if (!rxcount) {
427                 /* Had one stuck? */
428                 rxcount = readl(&regs->rx_fbc);
429                 if (!rxcount)
430                         return -EAGAIN;
431         }
432
433         reg_val = readl(&regs->rx_io_data);
434         if (reg_val != 0x0143414d) {
435                 /* Disable RX */
436                 clrbits_le32(&regs->ctl, 0x1 << 2);
437
438                 /* Flush RX FIFO */
439                 setbits_le32(&regs->rx_ctl, 0x1 << 3);
440                 while (readl(&regs->rx_ctl) & (0x1 << 3))
441                         ;
442
443                 /* Enable RX */
444                 setbits_le32(&regs->ctl, 0x1 << 2);
445
446                 return -EAGAIN;
447         }
448
449         /* A packet ready now
450          * Get status/length
451          */
452         good_packet = 1;
453
454         emac_inblk_32bit(&regs->rx_io_data, &rxhdr, sizeof(rxhdr));
455
456         rx_len = rxhdr.rx_len;
457         rx_status = rxhdr.rx_status;
458
459         /* Packet Status check */
460         if (rx_len < 0x40) {
461                 good_packet = 0;
462                 debug("RX: Bad Packet (runt)\n");
463         }
464
465         /* rx_status is identical to RSR register. */
466         if (0 & rx_status & (EMAC_CRCERR | EMAC_LENERR)) {
467                 good_packet = 0;
468                 if (rx_status & EMAC_CRCERR)
469                         printf("crc error\n");
470                 if (rx_status & EMAC_LENERR)
471                         printf("length error\n");
472         }
473
474         /* Move data from EMAC */
475         if (good_packet) {
476                 if (rx_len > EMAC_RX_BUFSIZE) {
477                         printf("Received packet is too big (len=%d)\n", rx_len);
478                         return -EMSGSIZE;
479                 }
480                 emac_inblk_32bit((void *)&regs->rx_io_data, packet, rx_len);
481                 return rx_len;
482         }
483
484         return -EIO; /* Bad packet */
485 }
486
487 static int _sunxi_emac_eth_send(struct emac_eth_dev *priv, void *packet,
488                                 int len)
489 {
490         struct emac_regs *regs = priv->regs;
491
492         /* Select channel 0 */
493         writel(0, &regs->tx_ins);
494
495         /* Write packet */
496         emac_outblk_32bit((void *)&regs->tx_io_data, packet, len);
497
498         /* Set TX len */
499         writel(len, &regs->tx_pl0);
500
501         /* Start translate from fifo to phy */
502         setbits_le32(&regs->tx_ctl0, 1);
503
504         return 0;
505 }
506
507 static int sunxi_emac_board_setup(struct emac_eth_dev *priv)
508 {
509         struct sunxi_sramc_regs *sram =
510                 (struct sunxi_sramc_regs *)SUNXI_SRAMC_BASE;
511         struct emac_regs *regs = priv->regs;
512         int pin, ret;
513
514         /* Map SRAM to EMAC */
515         setbits_le32(&sram->ctrl1, 0x5 << 2);
516
517         /* Configure pin mux settings for MII Ethernet */
518         for (pin = SUNXI_GPA(0); pin <= SUNXI_GPA(17); pin++)
519                 sunxi_gpio_set_cfgpin(pin, SUNXI_GPA_EMAC);
520
521         /* Set up clock gating */
522         ret = clk_enable(&priv->clk);
523         if (ret) {
524                 dev_err(dev, "failed to enable emac clock\n");
525                 return ret;
526         }
527
528         /* Set MII clock */
529         clrsetbits_le32(&regs->mac_mcfg, 0xf << 2, 0xd << 2);
530
531         return 0;
532 }
533
534 static int sunxi_emac_eth_start(struct udevice *dev)
535 {
536         struct eth_pdata *pdata = dev_get_platdata(dev);
537
538         return _sunxi_emac_eth_init(dev->priv, pdata->enetaddr);
539 }
540
541 static int sunxi_emac_eth_send(struct udevice *dev, void *packet, int length)
542 {
543         struct emac_eth_dev *priv = dev_get_priv(dev);
544
545         return _sunxi_emac_eth_send(priv, packet, length);
546 }
547
548 static int sunxi_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
549 {
550         struct emac_eth_dev *priv = dev_get_priv(dev);
551         int rx_len;
552
553         rx_len = _sunxi_emac_eth_recv(priv, priv->rx_buf);
554         *packetp = priv->rx_buf;
555
556         return rx_len;
557 }
558
559 static void sunxi_emac_eth_stop(struct udevice *dev)
560 {
561         /* Nothing to do here */
562 }
563
564 static int sunxi_emac_eth_probe(struct udevice *dev)
565 {
566         struct eth_pdata *pdata = dev_get_platdata(dev);
567         struct emac_eth_dev *priv = dev_get_priv(dev);
568         int ret;
569
570         priv->regs = (struct emac_regs *)pdata->iobase;
571
572         ret = clk_get_by_index(dev, 0, &priv->clk);
573         if (ret) {
574                 dev_err(dev, "failed to get emac clock\n");
575                 return ret;
576         }
577
578         ret = sunxi_emac_board_setup(priv);
579         if (ret)
580                 return ret;
581
582         return sunxi_emac_init_phy(priv, dev);
583 }
584
585 static const struct eth_ops sunxi_emac_eth_ops = {
586         .start                  = sunxi_emac_eth_start,
587         .send                   = sunxi_emac_eth_send,
588         .recv                   = sunxi_emac_eth_recv,
589         .stop                   = sunxi_emac_eth_stop,
590 };
591
592 static int sunxi_emac_eth_ofdata_to_platdata(struct udevice *dev)
593 {
594         struct eth_pdata *pdata = dev_get_platdata(dev);
595
596         pdata->iobase = devfdt_get_addr(dev);
597
598         return 0;
599 }
600
601 static const struct udevice_id sunxi_emac_eth_ids[] = {
602         { .compatible = "allwinner,sun4i-a10-emac" },
603         { }
604 };
605
606 U_BOOT_DRIVER(eth_sunxi_emac) = {
607         .name   = "eth_sunxi_emac",
608         .id     = UCLASS_ETH,
609         .of_match = sunxi_emac_eth_ids,
610         .ofdata_to_platdata = sunxi_emac_eth_ofdata_to_platdata,
611         .probe  = sunxi_emac_eth_probe,
612         .ops    = &sunxi_emac_eth_ops,
613         .priv_auto_alloc_size = sizeof(struct emac_eth_dev),
614         .platdata_auto_alloc_size = sizeof(struct eth_pdata),
615 };