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