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