net: smc911x: Pass around driver private data
[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/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         struct eth_device       dev;
25         phys_addr_t             iobase;
26         const struct chip_id    *chipid;
27         unsigned char           enetaddr[6];
28 };
29
30 static const struct chip_id chip_ids[] =  {
31         { CHIP_89218, "LAN89218" },
32         { CHIP_9115, "LAN9115" },
33         { CHIP_9116, "LAN9116" },
34         { CHIP_9117, "LAN9117" },
35         { CHIP_9118, "LAN9118" },
36         { CHIP_9211, "LAN9211" },
37         { CHIP_9215, "LAN9215" },
38         { CHIP_9216, "LAN9216" },
39         { CHIP_9217, "LAN9217" },
40         { CHIP_9218, "LAN9218" },
41         { CHIP_9220, "LAN9220" },
42         { CHIP_9221, "LAN9221" },
43         { 0, NULL },
44 };
45
46 #define DRIVERNAME "smc911x"
47
48 #if defined (CONFIG_SMC911X_32_BIT) && \
49         defined (CONFIG_SMC911X_16_BIT)
50 #error "SMC911X: Only one of CONFIG_SMC911X_32_BIT and \
51         CONFIG_SMC911X_16_BIT shall be set"
52 #endif
53
54 #if defined (CONFIG_SMC911X_32_BIT)
55 static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
56 {
57         return readl(priv->iobase + offset);
58 }
59
60 static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
61 {
62         writel(val, priv->iobase + offset);
63 }
64 #elif defined (CONFIG_SMC911X_16_BIT)
65 static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
66 {
67         return (readw(priv->iobase + offset) & 0xffff) |
68                (readw(priv->iobase + offset + 2) << 16);
69 }
70 static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
71 {
72         writew(val & 0xffff, priv->iobase + offset);
73         writew(val >> 16, priv->iobase + offset + 2);
74 }
75 #else
76 #error "SMC911X: undefined bus width"
77 #endif /* CONFIG_SMC911X_16_BIT */
78
79 static u32 smc911x_get_mac_csr(struct smc911x_priv *priv, u8 reg)
80 {
81         while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
82                 ;
83         smc911x_reg_write(priv, MAC_CSR_CMD,
84                         MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_R_NOT_W | reg);
85         while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
86                 ;
87
88         return smc911x_reg_read(priv, MAC_CSR_DATA);
89 }
90
91 static void smc911x_set_mac_csr(struct smc911x_priv *priv, u8 reg, u32 data)
92 {
93         while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
94                 ;
95         smc911x_reg_write(priv, MAC_CSR_DATA, data);
96         smc911x_reg_write(priv, MAC_CSR_CMD, MAC_CSR_CMD_CSR_BUSY | reg);
97         while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
98                 ;
99 }
100
101 static int smc911x_detect_chip(struct smc911x_priv *priv)
102 {
103         unsigned long val, i;
104
105         val = smc911x_reg_read(priv, BYTE_TEST);
106         if (val == 0xffffffff) {
107                 /* Special case -- no chip present */
108                 return -1;
109         } else if (val != 0x87654321) {
110                 printf(DRIVERNAME ": Invalid chip endian 0x%08lx\n", val);
111                 return -1;
112         }
113
114         val = smc911x_reg_read(priv, ID_REV) >> 16;
115         for (i = 0; chip_ids[i].id != 0; i++) {
116                 if (chip_ids[i].id == val) break;
117         }
118         if (!chip_ids[i].id) {
119                 printf(DRIVERNAME ": Unknown chip ID %04lx\n", val);
120                 return -1;
121         }
122
123         priv->chipid = &chip_ids[i];
124
125         return 0;
126 }
127
128 static void smc911x_reset(struct smc911x_priv *priv)
129 {
130         int timeout;
131
132         /*
133          *  Take out of PM setting first
134          *  Device is already wake up if PMT_CTRL_READY bit is set
135          */
136         if ((smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY) == 0) {
137                 /* Write to the bytetest will take out of powerdown */
138                 smc911x_reg_write(priv, BYTE_TEST, 0x0);
139
140                 timeout = 10;
141
142                 while (timeout-- &&
143                         !(smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY))
144                         udelay(10);
145                 if (timeout < 0) {
146                         printf(DRIVERNAME
147                                 ": timeout waiting for PM restore\n");
148                         return;
149                 }
150         }
151
152         /* Disable interrupts */
153         smc911x_reg_write(priv, INT_EN, 0);
154
155         smc911x_reg_write(priv, HW_CFG, HW_CFG_SRST);
156
157         timeout = 1000;
158         while (timeout-- && smc911x_reg_read(priv, E2P_CMD) & E2P_CMD_EPC_BUSY)
159                 udelay(10);
160
161         if (timeout < 0) {
162                 printf(DRIVERNAME ": reset timeout\n");
163                 return;
164         }
165
166         /* Reset the FIFO level and flow control settings */
167         smc911x_set_mac_csr(priv, FLOW, FLOW_FCPT | FLOW_FCEN);
168         smc911x_reg_write(priv, AFC_CFG, 0x0050287F);
169
170         /* Set to LED outputs */
171         smc911x_reg_write(priv, GPIO_CFG, 0x70070000);
172 }
173
174 static void smc911x_handle_mac_address(struct smc911x_priv *priv)
175 {
176         unsigned long addrh, addrl;
177         unsigned char *m = priv->enetaddr;
178
179         addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24);
180         addrh = m[4] | (m[5] << 8);
181         smc911x_set_mac_csr(priv, ADDRL, addrl);
182         smc911x_set_mac_csr(priv, ADDRH, addrh);
183
184         printf(DRIVERNAME ": MAC %pM\n", m);
185 }
186
187 static int smc911x_eth_phy_read(struct smc911x_priv *priv,
188                                 u8 phy, u8 reg, u16 *val)
189 {
190         while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
191                 ;
192
193         smc911x_set_mac_csr(priv, MII_ACC, phy << 11 | reg << 6 |
194                                 MII_ACC_MII_BUSY);
195
196         while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
197                 ;
198
199         *val = smc911x_get_mac_csr(priv, MII_DATA);
200
201         return 0;
202 }
203
204 static int smc911x_eth_phy_write(struct smc911x_priv *priv,
205                                 u8 phy, u8 reg, u16  val)
206 {
207         while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
208                 ;
209
210         smc911x_set_mac_csr(priv, MII_DATA, val);
211         smc911x_set_mac_csr(priv, MII_ACC,
212                 phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
213
214         while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
215                 ;
216         return 0;
217 }
218
219 static int smc911x_phy_reset(struct smc911x_priv *priv)
220 {
221         u32 reg;
222
223         reg = smc911x_reg_read(priv, PMT_CTRL);
224         reg &= ~0xfffff030;
225         reg |= PMT_CTRL_PHY_RST;
226         smc911x_reg_write(priv, PMT_CTRL, reg);
227
228         mdelay(100);
229
230         return 0;
231 }
232
233 static void smc911x_phy_configure(struct smc911x_priv *priv)
234 {
235         int timeout;
236         u16 status;
237
238         smc911x_phy_reset(priv);
239
240         smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_RESET);
241         mdelay(1);
242         smc911x_eth_phy_write(priv, 1, MII_ADVERTISE, 0x01e1);
243         smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_ANENABLE |
244                                 BMCR_ANRESTART);
245
246         timeout = 5000;
247         do {
248                 mdelay(1);
249                 if ((timeout--) == 0)
250                         goto err_out;
251
252                 if (smc911x_eth_phy_read(priv, 1, MII_BMSR, &status) != 0)
253                         goto err_out;
254         } while (!(status & BMSR_LSTATUS));
255
256         printf(DRIVERNAME ": phy initialized\n");
257
258         return;
259
260 err_out:
261         printf(DRIVERNAME ": autonegotiation timed out\n");
262 }
263
264 static void smc911x_enable(struct smc911x_priv *priv)
265 {
266         /* Enable TX */
267         smc911x_reg_write(priv, HW_CFG, 8 << 16 | HW_CFG_SF);
268
269         smc911x_reg_write(priv, GPT_CFG, GPT_CFG_TIMER_EN | 10000);
270
271         smc911x_reg_write(priv, TX_CFG, TX_CFG_TX_ON);
272
273         /* no padding to start of packets */
274         smc911x_reg_write(priv, RX_CFG, 0);
275
276         smc911x_set_mac_csr(priv, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN |
277                                 MAC_CR_HBDIS);
278 }
279
280 static int smc911x_init(struct eth_device *dev, bd_t * bd)
281 {
282         struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
283         const struct chip_id *id = priv->chipid;
284
285         printf(DRIVERNAME ": detected %s controller\n", id->name);
286
287         smc911x_reset(priv);
288
289         /* Configure the PHY, initialize the link state */
290         smc911x_phy_configure(priv);
291
292         smc911x_handle_mac_address(priv);
293
294         /* Turn on Tx + Rx */
295         smc911x_enable(priv);
296
297         return 0;
298 }
299
300 static int smc911x_send(struct eth_device *dev, void *packet, int length)
301 {
302         struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
303         u32 *data = (u32*)packet;
304         u32 tmplen;
305         u32 status;
306
307         smc911x_reg_write(priv, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG |
308                                 TX_CMD_A_INT_LAST_SEG | length);
309         smc911x_reg_write(priv, TX_DATA_FIFO, length);
310
311         tmplen = (length + 3) / 4;
312
313         while (tmplen--)
314                 smc911x_reg_write(priv, TX_DATA_FIFO, *data++);
315
316         /* wait for transmission */
317         while (!((smc911x_reg_read(priv, TX_FIFO_INF) &
318                                         TX_FIFO_INF_TSUSED) >> 16));
319
320         /* get status. Ignore 'no carrier' error, it has no meaning for
321          * full duplex operation
322          */
323         status = smc911x_reg_read(priv, TX_STATUS_FIFO) &
324                         (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL |
325                         TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
326
327         if (!status)
328                 return 0;
329
330         printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
331                 status & TX_STS_LOC ? "TX_STS_LOC " : "",
332                 status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
333                 status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
334                 status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
335                 status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
336
337         return -1;
338 }
339
340 static void smc911x_halt(struct eth_device *dev)
341 {
342         struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
343
344         smc911x_reset(priv);
345         smc911x_handle_mac_address(priv);
346 }
347
348 static int smc911x_recv(struct eth_device *dev)
349 {
350         struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
351         u32 *data = (u32 *)net_rx_packets[0];
352         u32 pktlen, tmplen;
353         u32 status;
354
355         if ((smc911x_reg_read(priv, RX_FIFO_INF) & RX_FIFO_INF_RXSUSED) >> 16) {
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                 else
370                         net_process_received_packet(net_rx_packets[0], pktlen);
371         }
372
373         return 0;
374 }
375
376 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
377 /* wrapper for smc911x_eth_phy_read */
378 static int smc911x_miiphy_read(struct mii_dev *bus, int phy, int devad,
379                                int reg)
380 {
381         struct eth_device *dev = eth_get_dev_by_name(bus->name);
382         struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
383         u16 val = 0;
384         int ret;
385
386         if (!dev || !priv)
387                 return -ENODEV;
388
389         ret = smc911x_eth_phy_read(priv, phy, reg, &val);
390         if (ret < 0)
391                 return ret;
392
393         return val;
394 }
395
396 /* wrapper for smc911x_eth_phy_write */
397 static int smc911x_miiphy_write(struct mii_dev *bus, int phy, int devad,
398                                 int reg, u16 val)
399 {
400         struct eth_device *dev = eth_get_dev_by_name(bus->name);
401         struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
402
403         if (!dev || !priv)
404                 return -ENODEV;
405
406         return smc911x_eth_phy_write(priv, phy, reg, val);
407 }
408
409 static int smc911x_initialize_mii(struct smc911x_priv *priv)
410 {
411         struct mii_dev *mdiodev = mdio_alloc();
412         int ret;
413
414         if (!mdiodev)
415                 return -ENOMEM;
416
417         strncpy(mdiodev->name, priv->dev.name, MDIO_NAME_LEN);
418         mdiodev->read = smc911x_miiphy_read;
419         mdiodev->write = smc911x_miiphy_write;
420
421         ret = mdio_register(mdiodev);
422         if (ret < 0) {
423                 mdio_free(mdiodev);
424                 return ret;
425         }
426
427         return 0;
428 }
429 #else
430 static int smc911x_initialize_mii(struct smc911x_priv *priv)
431 {
432         return 0;
433 }
434 #endif
435
436 int smc911x_initialize(u8 dev_num, int base_addr)
437 {
438         unsigned long addrl, addrh;
439         struct smc911x_priv *priv;
440         int ret;
441
442         priv = calloc(1, sizeof(*priv));
443         if (!priv)
444                 return -ENOMEM;
445
446         priv->iobase = base_addr;
447         priv->dev.iobase = base_addr;
448
449         /* Try to detect chip. Will fail if not present. */
450         ret = smc911x_detect_chip(priv);
451         if (ret) {
452                 ret = 0;        /* Card not detected is not an error */
453                 goto err_detect;
454         }
455
456         addrh = smc911x_get_mac_csr(priv, ADDRH);
457         addrl = smc911x_get_mac_csr(priv, ADDRL);
458         if (!(addrl == 0xffffffff && addrh == 0x0000ffff)) {
459                 /* address is obtained from optional eeprom */
460                 priv->enetaddr[0] = addrl;
461                 priv->enetaddr[1] = addrl >>  8;
462                 priv->enetaddr[2] = addrl >> 16;
463                 priv->enetaddr[3] = addrl >> 24;
464                 priv->enetaddr[4] = addrh;
465                 priv->enetaddr[5] = addrh >> 8;
466         }
467
468         priv->dev.init = smc911x_init;
469         priv->dev.halt = smc911x_halt;
470         priv->dev.send = smc911x_send;
471         priv->dev.recv = smc911x_recv;
472         sprintf(priv->dev.name, "%s-%hu", DRIVERNAME, dev_num);
473
474         eth_register(&priv->dev);
475
476         ret = smc911x_initialize_mii(priv);
477         if (ret)
478                 goto err_mii;
479
480         return 1;
481
482 err_mii:
483         eth_unregister(&priv->dev);
484 err_detect:
485         free(priv);
486         return ret;
487 }