[efi] Avoid setting direction flag on EFI platforms
[ipxe.git] / src / drivers / net / lan78xx.c
1 /*
2 * Copyright (C) 2017 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <string.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <ipxe/ethernet.h>
30 #include <ipxe/usb.h>
31 #include <ipxe/usbnet.h>
32 #include "lan78xx.h"
33
34 /** @file
35 *
36 * Microchip LAN78xx USB Ethernet driver
37 *
38 */
39
40 /******************************************************************************
41 *
42 * MAC address
43 *
44 ******************************************************************************
45 */
46
47 /**
48 * Fetch MAC address from EEPROM
49 *
50 * @v smscusb SMSC USB device
51 * @ret rc Return status code
52 */
53 static int lan78xx_eeprom_fetch_mac ( struct smscusb_device *smscusb ) {
54 uint32_t hw_cfg;
55 uint32_t orig_hw_cfg;
56 int rc;
57
58 /* Read original HW_CFG value */
59 if ( ( rc = smscusb_readl ( smscusb, LAN78XX_HW_CFG, &hw_cfg ) ) != 0 )
60 goto err_read_hw_cfg;
61 orig_hw_cfg = hw_cfg;
62
63 /* Temporarily disable LED0 and LED1 (which share physical
64 * pins with EEDO and EECLK respectively).
65 */
66 hw_cfg &= ~( LAN78XX_HW_CFG_LED0_EN | LAN78XX_HW_CFG_LED1_EN );
67 if ( ( rc = smscusb_writel ( smscusb, LAN78XX_HW_CFG, hw_cfg ) ) != 0 )
68 goto err_write_hw_cfg;
69
70 /* Fetch MAC address from EEPROM */
71 if ( ( rc = smscusb_eeprom_fetch_mac ( smscusb,
72 LAN78XX_E2P_BASE ) ) != 0 )
73 goto err_fetch_mac;
74
75 err_fetch_mac:
76 smscusb_writel ( smscusb, LAN78XX_HW_CFG, orig_hw_cfg );
77 err_write_hw_cfg:
78 err_read_hw_cfg:
79 return rc;
80 }
81
82 /**
83 * Fetch MAC address
84 *
85 * @v smscusb SMSC USB device
86 * @ret rc Return status code
87 */
88 static int lan78xx_fetch_mac ( struct smscusb_device *smscusb ) {
89 struct net_device *netdev = smscusb->netdev;
90 int rc;
91
92 /* Read MAC address from EEPROM, if present */
93 if ( ( rc = lan78xx_eeprom_fetch_mac ( smscusb ) ) == 0 )
94 return 0;
95
96 /* Read MAC address from OTP, if present */
97 if ( ( rc = smscusb_otp_fetch_mac ( smscusb, LAN78XX_OTP_BASE ) ) == 0 )
98 return 0;
99
100 /* Read MAC address from device tree, if present */
101 if ( ( rc = smscusb_fdt_fetch_mac ( smscusb ) ) == 0 )
102 return 0;
103
104 /* Otherwise, generate a random MAC address */
105 eth_random_addr ( netdev->hw_addr );
106 DBGC ( smscusb, "LAN78XX %p using random MAC %s\n",
107 smscusb, eth_ntoa ( netdev->hw_addr ) );
108 return 0;
109 }
110
111 /******************************************************************************
112 *
113 * Device reset
114 *
115 ******************************************************************************
116 */
117
118 /**
119 * Reset device
120 *
121 * @v smscusb SMSC USB device
122 * @ret rc Return status code
123 */
124 static int lan78xx_reset ( struct smscusb_device *smscusb ) {
125 uint32_t hw_cfg;
126 unsigned int i;
127 int rc;
128
129 /* Reset device */
130 if ( ( rc = smscusb_writel ( smscusb, LAN78XX_HW_CFG,
131 LAN78XX_HW_CFG_LRST ) ) != 0 )
132 return rc;
133
134 /* Wait for reset to complete */
135 for ( i = 0 ; i < LAN78XX_RESET_MAX_WAIT_MS ; i++ ) {
136
137 /* Check if reset has completed */
138 if ( ( rc = smscusb_readl ( smscusb, LAN78XX_HW_CFG,
139 &hw_cfg ) ) != 0 )
140 return rc;
141 if ( ! ( hw_cfg & LAN78XX_HW_CFG_LRST ) )
142 return 0;
143
144 /* Delay */
145 mdelay ( 1 );
146 }
147
148 DBGC ( smscusb, "LAN78XX %p timed out waiting for reset\n",
149 smscusb );
150 return -ETIMEDOUT;
151 }
152
153 /******************************************************************************
154 *
155 * Network device interface
156 *
157 ******************************************************************************
158 */
159
160 /**
161 * Open network device
162 *
163 * @v netdev Network device
164 * @ret rc Return status code
165 */
166 static int lan78xx_open ( struct net_device *netdev ) {
167 struct smscusb_device *smscusb = netdev->priv;
168 uint32_t usb_cfg0;
169 int rc;
170
171 /* Clear stored interrupt status */
172 smscusb->int_sts = 0;
173
174 /* Configure bulk IN empty response */
175 if ( ( rc = smscusb_readl ( smscusb, LAN78XX_USB_CFG0,
176 &usb_cfg0 ) ) != 0 )
177 goto err_usb_cfg0_read;
178 usb_cfg0 |= LAN78XX_USB_CFG0_BIR;
179 if ( ( rc = smscusb_writel ( smscusb, LAN78XX_USB_CFG0,
180 usb_cfg0 ) ) != 0 )
181 goto err_usb_cfg0_write;
182
183 /* Open USB network device */
184 if ( ( rc = usbnet_open ( &smscusb->usbnet ) ) != 0 ) {
185 DBGC ( smscusb, "LAN78XX %p could not open: %s\n",
186 smscusb, strerror ( rc ) );
187 goto err_open;
188 }
189
190 /* Configure interrupt endpoint */
191 if ( ( rc = smscusb_writel ( smscusb, LAN78XX_INT_EP_CTL,
192 ( LAN78XX_INT_EP_CTL_RDFO_EN |
193 LAN78XX_INT_EP_CTL_PHY_EN ) ) ) != 0 )
194 goto err_int_ep_ctl;
195
196 /* Configure bulk IN delay */
197 if ( ( rc = smscusb_writel ( smscusb, LAN78XX_BULK_IN_DLY,
198 LAN78XX_BULK_IN_DLY_SET ( 0 ) ) ) != 0 )
199 goto err_bulk_in_dly;
200
201 /* Enable automatic speed and duplex detection */
202 if ( ( rc = smscusb_writel ( smscusb, LAN78XX_MAC_CR,
203 ( LAN78XX_MAC_CR_ADP |
204 LAN78XX_MAC_CR_ADD |
205 LAN78XX_MAC_CR_ASD ) ) ) != 0 )
206 goto err_mac_cr;
207
208 /* Configure receive filters */
209 if ( ( rc = smscusb_writel ( smscusb, LAN78XX_RFE_CTL,
210 ( LAN78XX_RFE_CTL_AB |
211 LAN78XX_RFE_CTL_AM |
212 LAN78XX_RFE_CTL_AU ) ) ) != 0 )
213 goto err_rfe_ctl;
214
215 /* Configure receive FIFO */
216 if ( ( rc = smscusb_writel ( smscusb, LAN78XX_FCT_RX_CTL,
217 ( LAN78XX_FCT_RX_CTL_EN |
218 LAN78XX_FCT_RX_CTL_BAD ) ) ) != 0 )
219 goto err_fct_rx_ctl;
220
221 /* Configure transmit FIFO */
222 if ( ( rc = smscusb_writel ( smscusb, LAN78XX_FCT_TX_CTL,
223 LAN78XX_FCT_TX_CTL_EN ) ) != 0 )
224 goto err_fct_tx_ctl;
225
226 /* Configure receive datapath */
227 if ( ( rc = smscusb_writel ( smscusb, LAN78XX_MAC_RX,
228 ( LAN78XX_MAC_RX_MAX_SIZE_DEFAULT |
229 LAN78XX_MAC_RX_FCS |
230 LAN78XX_MAC_RX_EN ) ) ) != 0 )
231 goto err_mac_rx;
232
233 /* Configure transmit datapath */
234 if ( ( rc = smscusb_writel ( smscusb, LAN78XX_MAC_TX,
235 LAN78XX_MAC_TX_EN ) ) != 0 )
236 goto err_mac_tx;
237
238 /* Set MAC address */
239 if ( ( rc = smscusb_set_address ( smscusb,
240 LAN78XX_RX_ADDR_BASE ) ) != 0 )
241 goto err_set_address;
242
243 /* Set MAC address perfect filter */
244 if ( ( rc = smscusb_set_filter ( smscusb,
245 LAN78XX_ADDR_FILT_BASE ) ) != 0 )
246 goto err_set_filter;
247
248 /* Enable PHY interrupts and update link status */
249 if ( ( rc = smscusb_mii_open ( smscusb, LAN78XX_MII_PHY_INTR_MASK,
250 ( LAN78XX_PHY_INTR_ENABLE |
251 LAN78XX_PHY_INTR_LINK |
252 LAN78XX_PHY_INTR_ANEG_ERR |
253 LAN78XX_PHY_INTR_ANEG_DONE ) ) ) != 0 )
254 goto err_mii_open;
255
256 return 0;
257
258 err_mii_open:
259 err_set_filter:
260 err_set_address:
261 err_mac_tx:
262 err_mac_rx:
263 err_fct_tx_ctl:
264 err_fct_rx_ctl:
265 err_rfe_ctl:
266 err_mac_cr:
267 err_bulk_in_dly:
268 err_int_ep_ctl:
269 usbnet_close ( &smscusb->usbnet );
270 err_open:
271 err_usb_cfg0_write:
272 err_usb_cfg0_read:
273 lan78xx_reset ( smscusb );
274 return rc;
275 }
276
277 /**
278 * Close network device
279 *
280 * @v netdev Network device
281 */
282 static void lan78xx_close ( struct net_device *netdev ) {
283 struct smscusb_device *smscusb = netdev->priv;
284
285 /* Close USB network device */
286 usbnet_close ( &smscusb->usbnet );
287
288 /* Dump statistics (for debugging) */
289 if ( DBG_LOG )
290 smsc75xx_dump_statistics ( smscusb );
291
292 /* Reset device */
293 lan78xx_reset ( smscusb );
294 }
295
296 /** LAN78xx network device operations */
297 static struct net_device_operations lan78xx_operations = {
298 .open = lan78xx_open,
299 .close = lan78xx_close,
300 .transmit = smsc75xx_transmit,
301 .poll = smsc75xx_poll,
302 };
303
304 /******************************************************************************
305 *
306 * USB interface
307 *
308 ******************************************************************************
309 */
310
311 /**
312 * Probe device
313 *
314 * @v func USB function
315 * @v config Configuration descriptor
316 * @ret rc Return status code
317 */
318 static int lan78xx_probe ( struct usb_function *func,
319 struct usb_configuration_descriptor *config ) {
320 struct net_device *netdev;
321 struct smscusb_device *smscusb;
322 int rc;
323
324 /* Allocate and initialise structure */
325 netdev = alloc_etherdev ( sizeof ( *smscusb ) );
326 if ( ! netdev ) {
327 rc = -ENOMEM;
328 goto err_alloc;
329 }
330 netdev_init ( netdev, &lan78xx_operations );
331 netdev->dev = &func->dev;
332 smscusb = netdev->priv;
333 memset ( smscusb, 0, sizeof ( *smscusb ) );
334 smscusb_init ( smscusb, netdev, func, &smsc75xx_in_operations );
335 smscusb_mii_init ( smscusb, LAN78XX_MII_BASE,
336 LAN78XX_MII_PHY_INTR_SOURCE );
337 usb_refill_init ( &smscusb->usbnet.in, 0, SMSC75XX_IN_MTU,
338 SMSC75XX_IN_MAX_FILL );
339 DBGC ( smscusb, "LAN78XX %p on %s\n", smscusb, func->name );
340
341 /* Describe USB network device */
342 if ( ( rc = usbnet_describe ( &smscusb->usbnet, config ) ) != 0 ) {
343 DBGC ( smscusb, "LAN78XX %p could not describe: %s\n",
344 smscusb, strerror ( rc ) );
345 goto err_describe;
346 }
347
348 /* Reset device */
349 if ( ( rc = lan78xx_reset ( smscusb ) ) != 0 )
350 goto err_reset;
351
352 /* Read MAC address */
353 if ( ( rc = lan78xx_fetch_mac ( smscusb ) ) != 0 )
354 goto err_fetch_mac;
355
356 /* Register network device */
357 if ( ( rc = register_netdev ( netdev ) ) != 0 )
358 goto err_register;
359
360 usb_func_set_drvdata ( func, netdev );
361 return 0;
362
363 unregister_netdev ( netdev );
364 err_register:
365 err_fetch_mac:
366 err_reset:
367 err_describe:
368 netdev_nullify ( netdev );
369 netdev_put ( netdev );
370 err_alloc:
371 return rc;
372 }
373
374 /**
375 * Remove device
376 *
377 * @v func USB function
378 */
379 static void lan78xx_remove ( struct usb_function *func ) {
380 struct net_device *netdev = usb_func_get_drvdata ( func );
381
382 unregister_netdev ( netdev );
383 netdev_nullify ( netdev );
384 netdev_put ( netdev );
385 }
386
387 /** LAN78xx device IDs */
388 static struct usb_device_id lan78xx_ids[] = {
389 {
390 .name = "lan7800",
391 .vendor = 0x0424,
392 .product = 0x7800,
393 },
394 {
395 .name = "lan7850",
396 .vendor = 0x0424,
397 .product = 0x7850,
398 },
399 };
400
401 /** LAN78xx driver */
402 struct usb_driver lan78xx_driver __usb_driver = {
403 .ids = lan78xx_ids,
404 .id_count = ( sizeof ( lan78xx_ids ) / sizeof ( lan78xx_ids[0] ) ),
405 .class = USB_CLASS_ID ( 0xff, 0x00, 0xff ),
406 .score = USB_SCORE_NORMAL,
407 .probe = lan78xx_probe,
408 .remove = lan78xx_remove,
409 };