Merge branch 'next' of git://git.denx.de/u-boot-sh into next
[platform/kernel/u-boot.git] / drivers / net / smc911x.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * SMSC LAN9[12]1[567] Network driver
4  *
5  * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
6  */
7
8 #include <common.h>
9 #include <env.h>
10 #include <command.h>
11 #include <malloc.h>
12 #include <net.h>
13 #include <miiphy.h>
14 #include <linux/io.h>
15 #include <linux/delay.h>
16 #include <linux/types.h>
17
18 #include "smc911x.h"
19
20 struct chip_id {
21         u16 id;
22         char *name;
23 };
24
25 struct smc911x_priv {
26 #ifndef CONFIG_DM_ETH
27         struct eth_device       dev;
28 #endif
29         phys_addr_t             iobase;
30         const struct chip_id    *chipid;
31         unsigned char           enetaddr[6];
32 };
33
34 static const struct chip_id chip_ids[] =  {
35         { CHIP_89218, "LAN89218" },
36         { CHIP_9115, "LAN9115" },
37         { CHIP_9116, "LAN9116" },
38         { CHIP_9117, "LAN9117" },
39         { CHIP_9118, "LAN9118" },
40         { CHIP_9211, "LAN9211" },
41         { CHIP_9215, "LAN9215" },
42         { CHIP_9216, "LAN9216" },
43         { CHIP_9217, "LAN9217" },
44         { CHIP_9218, "LAN9218" },
45         { CHIP_9220, "LAN9220" },
46         { CHIP_9221, "LAN9221" },
47         { 0, NULL },
48 };
49
50 #define DRIVERNAME "smc911x"
51
52 #if defined (CONFIG_SMC911X_32_BIT) && \
53         defined (CONFIG_SMC911X_16_BIT)
54 #error "SMC911X: Only one of CONFIG_SMC911X_32_BIT and \
55         CONFIG_SMC911X_16_BIT shall be set"
56 #endif
57
58 #if defined (CONFIG_SMC911X_32_BIT)
59 static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
60 {
61         return readl(priv->iobase + offset);
62 }
63
64 static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
65 {
66         writel(val, priv->iobase + offset);
67 }
68 #elif defined (CONFIG_SMC911X_16_BIT)
69 static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
70 {
71         return (readw(priv->iobase + offset) & 0xffff) |
72                (readw(priv->iobase + offset + 2) << 16);
73 }
74 static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
75 {
76         writew(val & 0xffff, priv->iobase + offset);
77         writew(val >> 16, priv->iobase + offset + 2);
78 }
79 #else
80 #error "SMC911X: undefined bus width"
81 #endif /* CONFIG_SMC911X_16_BIT */
82
83 static u32 smc911x_get_mac_csr(struct smc911x_priv *priv, u8 reg)
84 {
85         while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
86                 ;
87         smc911x_reg_write(priv, MAC_CSR_CMD,
88                         MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_R_NOT_W | reg);
89         while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
90                 ;
91
92         return smc911x_reg_read(priv, MAC_CSR_DATA);
93 }
94
95 static void smc911x_set_mac_csr(struct smc911x_priv *priv, u8 reg, u32 data)
96 {
97         while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
98                 ;
99         smc911x_reg_write(priv, MAC_CSR_DATA, data);
100         smc911x_reg_write(priv, MAC_CSR_CMD, MAC_CSR_CMD_CSR_BUSY | reg);
101         while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
102                 ;
103 }
104
105 static int smc911x_detect_chip(struct smc911x_priv *priv)
106 {
107         unsigned long val, i;
108
109         val = smc911x_reg_read(priv, BYTE_TEST);
110         if (val == 0xffffffff) {
111                 /* Special case -- no chip present */
112                 return -1;
113         } else if (val != 0x87654321) {
114                 printf(DRIVERNAME ": Invalid chip endian 0x%08lx\n", val);
115                 return -1;
116         }
117
118         val = smc911x_reg_read(priv, ID_REV) >> 16;
119         for (i = 0; chip_ids[i].id != 0; i++) {
120                 if (chip_ids[i].id == val) break;
121         }
122         if (!chip_ids[i].id) {
123                 printf(DRIVERNAME ": Unknown chip ID %04lx\n", val);
124                 return -1;
125         }
126
127         priv->chipid = &chip_ids[i];
128
129         return 0;
130 }
131
132 static void smc911x_reset(struct smc911x_priv *priv)
133 {
134         int timeout;
135
136         /*
137          *  Take out of PM setting first
138          *  Device is already wake up if PMT_CTRL_READY bit is set
139          */
140         if ((smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY) == 0) {
141                 /* Write to the bytetest will take out of powerdown */
142                 smc911x_reg_write(priv, BYTE_TEST, 0x0);
143
144                 timeout = 10;
145
146                 while (timeout-- &&
147                         !(smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY))
148                         udelay(10);
149                 if (timeout < 0) {
150                         printf(DRIVERNAME
151                                 ": timeout waiting for PM restore\n");
152                         return;
153                 }
154         }
155
156         /* Disable interrupts */
157         smc911x_reg_write(priv, INT_EN, 0);
158
159         smc911x_reg_write(priv, HW_CFG, HW_CFG_SRST);
160
161         timeout = 1000;
162         while (timeout-- && smc911x_reg_read(priv, E2P_CMD) & E2P_CMD_EPC_BUSY)
163                 udelay(10);
164
165         if (timeout < 0) {
166                 printf(DRIVERNAME ": reset timeout\n");
167                 return;
168         }
169
170         /* Reset the FIFO level and flow control settings */
171         smc911x_set_mac_csr(priv, FLOW, FLOW_FCPT | FLOW_FCEN);
172         smc911x_reg_write(priv, AFC_CFG, 0x0050287F);
173
174         /* Set to LED outputs */
175         smc911x_reg_write(priv, GPIO_CFG, 0x70070000);
176 }
177
178 static void smc911x_handle_mac_address(struct smc911x_priv *priv)
179 {
180         unsigned long addrh, addrl;
181         unsigned char *m = priv->enetaddr;
182
183         addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24);
184         addrh = m[4] | (m[5] << 8);
185         smc911x_set_mac_csr(priv, ADDRL, addrl);
186         smc911x_set_mac_csr(priv, ADDRH, addrh);
187
188         printf(DRIVERNAME ": MAC %pM\n", m);
189         if (!env_get("ethaddr"))
190                 env_set("ethaddr", (const char *)m);
191 }
192
193 static bool smc911x_read_mac_address(struct smc911x_priv *priv)
194 {
195         u32 addrh, addrl;
196
197         /* address is obtained from optional eeprom */
198         addrh = smc911x_get_mac_csr(priv, ADDRH);
199         addrl = smc911x_get_mac_csr(priv, ADDRL);
200         if (addrl == 0xffffffff && addrh == 0x0000ffff)
201                 return false;
202
203         priv->enetaddr[0] = addrl;
204         priv->enetaddr[1] = addrl >>  8;
205         priv->enetaddr[2] = addrl >> 16;
206         priv->enetaddr[3] = addrl >> 24;
207         priv->enetaddr[4] = addrh;
208         priv->enetaddr[5] = addrh >> 8;
209
210         return true;
211 }
212
213 static int smc911x_eth_phy_read(struct smc911x_priv *priv,
214                                 u8 phy, u8 reg, u16 *val)
215 {
216         while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
217                 ;
218
219         smc911x_set_mac_csr(priv, MII_ACC, phy << 11 | reg << 6 |
220                                 MII_ACC_MII_BUSY);
221
222         while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
223                 ;
224
225         *val = smc911x_get_mac_csr(priv, MII_DATA);
226
227         return 0;
228 }
229
230 static int smc911x_eth_phy_write(struct smc911x_priv *priv,
231                                 u8 phy, u8 reg, u16  val)
232 {
233         while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
234                 ;
235
236         smc911x_set_mac_csr(priv, MII_DATA, val);
237         smc911x_set_mac_csr(priv, MII_ACC,
238                 phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
239
240         while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
241                 ;
242         return 0;
243 }
244
245 static int smc911x_phy_reset(struct smc911x_priv *priv)
246 {
247         u32 reg;
248
249         reg = smc911x_reg_read(priv, PMT_CTRL);
250         reg &= ~0xfffff030;
251         reg |= PMT_CTRL_PHY_RST;
252         smc911x_reg_write(priv, PMT_CTRL, reg);
253
254         mdelay(100);
255
256         return 0;
257 }
258
259 static void smc911x_phy_configure(struct smc911x_priv *priv)
260 {
261         int timeout;
262         u16 status;
263
264         smc911x_phy_reset(priv);
265
266         smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_RESET);
267         mdelay(1);
268         smc911x_eth_phy_write(priv, 1, MII_ADVERTISE, 0x01e1);
269         smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_ANENABLE |
270                                 BMCR_ANRESTART);
271
272         timeout = 5000;
273         do {
274                 mdelay(1);
275                 if ((timeout--) == 0)
276                         goto err_out;
277
278                 if (smc911x_eth_phy_read(priv, 1, MII_BMSR, &status) != 0)
279                         goto err_out;
280         } while (!(status & BMSR_LSTATUS));
281
282         printf(DRIVERNAME ": phy initialized\n");
283
284         return;
285
286 err_out:
287         printf(DRIVERNAME ": autonegotiation timed out\n");
288 }
289
290 static void smc911x_enable(struct smc911x_priv *priv)
291 {
292         /* Enable TX */
293         smc911x_reg_write(priv, HW_CFG, 8 << 16 | HW_CFG_SF);
294
295         smc911x_reg_write(priv, GPT_CFG, GPT_CFG_TIMER_EN | 10000);
296
297         smc911x_reg_write(priv, TX_CFG, TX_CFG_TX_ON);
298
299         /* no padding to start of packets */
300         smc911x_reg_write(priv, RX_CFG, 0);
301
302         smc911x_set_mac_csr(priv, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN |
303                                 MAC_CR_HBDIS);
304 }
305
306 static int smc911x_init_common(struct smc911x_priv *priv)
307 {
308         const struct chip_id *id = priv->chipid;
309
310         printf(DRIVERNAME ": detected %s controller\n", id->name);
311
312         smc911x_reset(priv);
313
314         /* Configure the PHY, initialize the link state */
315         smc911x_phy_configure(priv);
316
317         smc911x_handle_mac_address(priv);
318
319         /* Turn on Tx + Rx */
320         smc911x_enable(priv);
321
322         return 0;
323 }
324
325 static int smc911x_send_common(struct smc911x_priv *priv,
326                                void *packet, int length)
327 {
328         u32 *data = (u32*)packet;
329         u32 tmplen;
330         u32 status;
331
332         smc911x_reg_write(priv, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG |
333                                 TX_CMD_A_INT_LAST_SEG | length);
334         smc911x_reg_write(priv, TX_DATA_FIFO, length);
335
336         tmplen = (length + 3) / 4;
337
338         while (tmplen--)
339                 smc911x_reg_write(priv, TX_DATA_FIFO, *data++);
340
341         /* wait for transmission */
342         while (!((smc911x_reg_read(priv, TX_FIFO_INF) &
343                                         TX_FIFO_INF_TSUSED) >> 16));
344
345         /* get status. Ignore 'no carrier' error, it has no meaning for
346          * full duplex operation
347          */
348         status = smc911x_reg_read(priv, TX_STATUS_FIFO) &
349                         (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL |
350                         TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
351
352         if (!status)
353                 return 0;
354
355         printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
356                 status & TX_STS_LOC ? "TX_STS_LOC " : "",
357                 status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
358                 status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
359                 status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
360                 status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
361
362         return -1;
363 }
364
365 static void smc911x_halt_common(struct smc911x_priv *priv)
366 {
367         smc911x_reset(priv);
368         smc911x_handle_mac_address(priv);
369 }
370
371 static int smc911x_recv_common(struct smc911x_priv *priv, u32 *data)
372 {
373         u32 pktlen, tmplen;
374         u32 status;
375
376         status = smc911x_reg_read(priv, RX_FIFO_INF);
377         if (!(status & RX_FIFO_INF_RXSUSED))
378                 return 0;
379
380         status = smc911x_reg_read(priv, RX_STATUS_FIFO);
381         pktlen = (status & RX_STS_PKT_LEN) >> 16;
382
383         smc911x_reg_write(priv, RX_CFG, 0);
384
385         tmplen = (pktlen + 3) / 4;
386         while (tmplen--)
387                 *data++ = smc911x_reg_read(priv, RX_DATA_FIFO);
388
389         if (status & RX_STS_ES) {
390                 printf(DRIVERNAME
391                         ": dropped bad packet. Status: 0x%08x\n",
392                         status);
393                 return 0;
394         }
395
396         return pktlen;
397 }
398
399 #ifndef CONFIG_DM_ETH
400
401 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
402 /* wrapper for smc911x_eth_phy_read */
403 static int smc911x_miiphy_read(struct mii_dev *bus, int phy, int devad,
404                                int reg)
405 {
406         struct eth_device *dev = eth_get_dev_by_name(bus->name);
407         struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
408         u16 val = 0;
409         int ret;
410
411         if (!dev || !priv)
412                 return -ENODEV;
413
414         ret = smc911x_eth_phy_read(priv, phy, reg, &val);
415         if (ret < 0)
416                 return ret;
417
418         return val;
419 }
420
421 /* wrapper for smc911x_eth_phy_write */
422 static int smc911x_miiphy_write(struct mii_dev *bus, int phy, int devad,
423                                 int reg, u16 val)
424 {
425         struct eth_device *dev = eth_get_dev_by_name(bus->name);
426         struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
427
428         if (!dev || !priv)
429                 return -ENODEV;
430
431         return smc911x_eth_phy_write(priv, phy, reg, val);
432 }
433
434 static int smc911x_initialize_mii(struct smc911x_priv *priv)
435 {
436         struct mii_dev *mdiodev = mdio_alloc();
437         int ret;
438
439         if (!mdiodev)
440                 return -ENOMEM;
441
442         strncpy(mdiodev->name, priv->dev.name, MDIO_NAME_LEN);
443         mdiodev->read = smc911x_miiphy_read;
444         mdiodev->write = smc911x_miiphy_write;
445
446         ret = mdio_register(mdiodev);
447         if (ret < 0) {
448                 mdio_free(mdiodev);
449                 return ret;
450         }
451
452         return 0;
453 }
454 #else
455 static int smc911x_initialize_mii(struct smc911x_priv *priv)
456 {
457         return 0;
458 }
459 #endif
460
461 static int smc911x_init(struct eth_device *dev, struct bd_info *bd)
462 {
463         struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
464
465         return smc911x_init_common(priv);
466 }
467
468 static void smc911x_halt(struct eth_device *dev)
469 {
470         struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
471
472         smc911x_halt_common(priv);
473 }
474
475 static int smc911x_send(struct eth_device *dev, void *packet, int length)
476 {
477         struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
478
479         return smc911x_send_common(priv, packet, length);
480 }
481
482 static int smc911x_recv(struct eth_device *dev)
483 {
484         struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
485         u32 *data = (u32 *)net_rx_packets[0];
486         int ret;
487
488         ret = smc911x_recv_common(priv, data);
489         if (ret)
490                 net_process_received_packet(net_rx_packets[0], ret);
491
492         return ret;
493 }
494
495 int smc911x_initialize(u8 dev_num, int base_addr)
496 {
497         struct smc911x_priv *priv;
498         int ret;
499
500         priv = calloc(1, sizeof(*priv));
501         if (!priv)
502                 return -ENOMEM;
503
504         priv->iobase = base_addr;
505         priv->dev.iobase = base_addr;
506
507         /* Try to detect chip. Will fail if not present. */
508         ret = smc911x_detect_chip(priv);
509         if (ret) {
510                 ret = 0;        /* Card not detected is not an error */
511                 goto err_detect;
512         }
513
514         if (smc911x_read_mac_address(priv))
515                 memcpy(priv->dev.enetaddr, priv->enetaddr, 6);
516
517         priv->dev.init = smc911x_init;
518         priv->dev.halt = smc911x_halt;
519         priv->dev.send = smc911x_send;
520         priv->dev.recv = smc911x_recv;
521         sprintf(priv->dev.name, "%s-%hu", DRIVERNAME, dev_num);
522
523         eth_register(&priv->dev);
524
525         ret = smc911x_initialize_mii(priv);
526         if (ret)
527                 goto err_mii;
528
529         return 1;
530
531 err_mii:
532         eth_unregister(&priv->dev);
533 err_detect:
534         free(priv);
535         return ret;
536 }
537
538 #else   /* ifdef CONFIG_DM_ETH */
539
540 static int smc911x_start(struct udevice *dev)
541 {
542         struct eth_pdata *plat = dev_get_platdata(dev);
543         struct smc911x_priv *priv = dev_get_priv(dev);
544
545         memcpy(priv->enetaddr, plat->enetaddr, sizeof(plat->enetaddr));
546
547         return smc911x_init_common(priv);
548 }
549
550 static void smc911x_stop(struct udevice *dev)
551 {
552         struct smc911x_priv *priv = dev_get_priv(dev);
553
554         smc911x_halt_common(priv);
555 }
556
557 static int smc911x_send(struct udevice *dev, void *packet, int length)
558 {
559         struct smc911x_priv *priv = dev_get_priv(dev);
560         int ret;
561
562         ret = smc911x_send_common(priv, packet, length);
563
564         return ret ? 0 : -ETIMEDOUT;
565 }
566
567 static int smc911x_recv(struct udevice *dev, int flags, uchar **packetp)
568 {
569         struct smc911x_priv *priv = dev_get_priv(dev);
570         u32 *data = (u32 *)net_rx_packets[0];
571         int ret;
572
573         ret = smc911x_recv_common(priv, data);
574         if (ret)
575                 *packetp = (void *)data;
576
577         return ret ? ret : -EAGAIN;
578 }
579
580 static int smc911x_read_rom_hwaddr(struct udevice *dev)
581 {
582         struct smc911x_priv *priv = dev_get_priv(dev);
583         struct eth_pdata *pdata = dev_get_platdata(dev);
584
585         if (!smc911x_read_mac_address(priv))
586                 return -ENODEV;
587
588         memcpy(pdata->enetaddr, priv->enetaddr, sizeof(pdata->enetaddr));
589
590         return 0;
591 }
592
593 static int smc911x_bind(struct udevice *dev)
594 {
595         return device_set_name(dev, dev->name);
596 }
597
598 static int smc911x_probe(struct udevice *dev)
599 {
600         struct smc911x_priv *priv = dev_get_priv(dev);
601         int ret;
602
603         /* Try to detect chip. Will fail if not present. */
604         ret = smc911x_detect_chip(priv);
605         if (ret)
606                 return ret;
607
608         smc911x_read_rom_hwaddr(dev);
609
610         return 0;
611 }
612
613 static int smc911x_ofdata_to_platdata(struct udevice *dev)
614 {
615         struct smc911x_priv *priv = dev_get_priv(dev);
616         struct eth_pdata *pdata = dev_get_platdata(dev);
617
618         pdata->iobase = dev_read_addr(dev);
619         priv->iobase = pdata->iobase;
620
621         return 0;
622 }
623
624 static const struct eth_ops smc911x_ops = {
625         .start  = smc911x_start,
626         .send   = smc911x_send,
627         .recv   = smc911x_recv,
628         .stop   = smc911x_stop,
629         .read_rom_hwaddr = smc911x_read_rom_hwaddr,
630 };
631
632 static const struct udevice_id smc911x_ids[] = {
633         { .compatible = "smsc,lan9115" },
634         { }
635 };
636
637 U_BOOT_DRIVER(smc911x) = {
638         .name           = "eth_smc911x",
639         .id             = UCLASS_ETH,
640         .of_match       = smc911x_ids,
641         .bind           = smc911x_bind,
642         .ofdata_to_platdata = smc911x_ofdata_to_platdata,
643         .probe          = smc911x_probe,
644         .ops            = &smc911x_ops,
645         .priv_auto_alloc_size = sizeof(struct smc911x_priv),
646         .platdata_auto_alloc_size = sizeof(struct eth_pdata),
647         .flags          = DM_FLAG_ALLOC_PRIV_DMA,
648 };
649 #endif