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