[smscusb] Fetch MAC from device tree for Raspberry Pi Model B+
[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 /* Configure receive filters */
202 if ( ( rc = smscusb_writel ( smscusb, LAN78XX_RFE_CTL,
203 ( LAN78XX_RFE_CTL_AB |
204 LAN78XX_RFE_CTL_AM |
205 LAN78XX_RFE_CTL_AU ) ) ) != 0 )
206 goto err_rfe_ctl;
207
208 /* Configure receive FIFO */
209 if ( ( rc = smscusb_writel ( smscusb, LAN78XX_FCT_RX_CTL,
210 ( LAN78XX_FCT_RX_CTL_EN |
211 LAN78XX_FCT_RX_CTL_BAD ) ) ) != 0 )
212 goto err_fct_rx_ctl;
213
214 /* Configure transmit FIFO */
215 if ( ( rc = smscusb_writel ( smscusb, LAN78XX_FCT_TX_CTL,
216 LAN78XX_FCT_TX_CTL_EN ) ) != 0 )
217 goto err_fct_tx_ctl;
218
219 /* Configure receive datapath */
220 if ( ( rc = smscusb_writel ( smscusb, LAN78XX_MAC_RX,
221 ( LAN78XX_MAC_RX_MAX_SIZE_DEFAULT |
222 LAN78XX_MAC_RX_FCS |
223 LAN78XX_MAC_RX_EN ) ) ) != 0 )
224 goto err_mac_rx;
225
226 /* Configure transmit datapath */
227 if ( ( rc = smscusb_writel ( smscusb, LAN78XX_MAC_TX,
228 LAN78XX_MAC_TX_EN ) ) != 0 )
229 goto err_mac_tx;
230
231 /* Set MAC address */
232 if ( ( rc = smscusb_set_address ( smscusb,
233 LAN78XX_RX_ADDR_BASE ) ) != 0 )
234 goto err_set_address;
235
236 /* Set MAC address perfect filter */
237 if ( ( rc = smscusb_set_filter ( smscusb,
238 LAN78XX_ADDR_FILT_BASE ) ) != 0 )
239 goto err_set_filter;
240
241 /* Enable PHY interrupts and update link status */
242 if ( ( rc = smscusb_mii_open ( smscusb, LAN78XX_MII_PHY_INTR_MASK,
243 ( LAN78XX_PHY_INTR_ENABLE |
244 LAN78XX_PHY_INTR_LINK |
245 LAN78XX_PHY_INTR_ANEG_ERR |
246 LAN78XX_PHY_INTR_ANEG_DONE ) ) ) != 0 )
247 goto err_mii_open;
248
249 return 0;
250
251 err_mii_open:
252 err_set_filter:
253 err_set_address:
254 err_mac_tx:
255 err_mac_rx:
256 err_fct_tx_ctl:
257 err_fct_rx_ctl:
258 err_rfe_ctl:
259 err_bulk_in_dly:
260 err_int_ep_ctl:
261 usbnet_close ( &smscusb->usbnet );
262 err_open:
263 err_usb_cfg0_write:
264 err_usb_cfg0_read:
265 lan78xx_reset ( smscusb );
266 return rc;
267 }
268
269 /**
270 * Close network device
271 *
272 * @v netdev Network device
273 */
274 static void lan78xx_close ( struct net_device *netdev ) {
275 struct smscusb_device *smscusb = netdev->priv;
276
277 /* Close USB network device */
278 usbnet_close ( &smscusb->usbnet );
279
280 /* Dump statistics (for debugging) */
281 if ( DBG_LOG )
282 smsc75xx_dump_statistics ( smscusb );
283
284 /* Reset device */
285 lan78xx_reset ( smscusb );
286 }
287
288 /** LAN78xx network device operations */
289 static struct net_device_operations lan78xx_operations = {
290 .open = lan78xx_open,
291 .close = lan78xx_close,
292 .transmit = smsc75xx_transmit,
293 .poll = smsc75xx_poll,
294 };
295
296 /******************************************************************************
297 *
298 * USB interface
299 *
300 ******************************************************************************
301 */
302
303 /**
304 * Probe device
305 *
306 * @v func USB function
307 * @v config Configuration descriptor
308 * @ret rc Return status code
309 */
310 static int lan78xx_probe ( struct usb_function *func,
311 struct usb_configuration_descriptor *config ) {
312 struct net_device *netdev;
313 struct smscusb_device *smscusb;
314 int rc;
315
316 /* Allocate and initialise structure */
317 netdev = alloc_etherdev ( sizeof ( *smscusb ) );
318 if ( ! netdev ) {
319 rc = -ENOMEM;
320 goto err_alloc;
321 }
322 netdev_init ( netdev, &lan78xx_operations );
323 netdev->dev = &func->dev;
324 smscusb = netdev->priv;
325 memset ( smscusb, 0, sizeof ( *smscusb ) );
326 smscusb_init ( smscusb, netdev, func, &smsc75xx_in_operations );
327 smscusb_mii_init ( smscusb, LAN78XX_MII_BASE,
328 LAN78XX_MII_PHY_INTR_SOURCE );
329 usb_refill_init ( &smscusb->usbnet.in, 0, SMSC75XX_IN_MTU,
330 SMSC75XX_IN_MAX_FILL );
331 DBGC ( smscusb, "LAN78XX %p on %s\n", smscusb, func->name );
332
333 /* Describe USB network device */
334 if ( ( rc = usbnet_describe ( &smscusb->usbnet, config ) ) != 0 ) {
335 DBGC ( smscusb, "LAN78XX %p could not describe: %s\n",
336 smscusb, strerror ( rc ) );
337 goto err_describe;
338 }
339
340 /* Reset device */
341 if ( ( rc = lan78xx_reset ( smscusb ) ) != 0 )
342 goto err_reset;
343
344 /* Read MAC address */
345 if ( ( rc = lan78xx_fetch_mac ( smscusb ) ) != 0 )
346 goto err_fetch_mac;
347
348 /* Register network device */
349 if ( ( rc = register_netdev ( netdev ) ) != 0 )
350 goto err_register;
351
352 usb_func_set_drvdata ( func, netdev );
353 return 0;
354
355 unregister_netdev ( netdev );
356 err_register:
357 err_fetch_mac:
358 err_reset:
359 err_describe:
360 netdev_nullify ( netdev );
361 netdev_put ( netdev );
362 err_alloc:
363 return rc;
364 }
365
366 /**
367 * Remove device
368 *
369 * @v func USB function
370 */
371 static void lan78xx_remove ( struct usb_function *func ) {
372 struct net_device *netdev = usb_func_get_drvdata ( func );
373
374 unregister_netdev ( netdev );
375 netdev_nullify ( netdev );
376 netdev_put ( netdev );
377 }
378
379 /** LAN78xx device IDs */
380 static struct usb_device_id lan78xx_ids[] = {
381 {
382 .name = "lan7800",
383 .vendor = 0x0424,
384 .product = 0x7800,
385 },
386 {
387 .name = "lan7850",
388 .vendor = 0x0424,
389 .product = 0x7850,
390 },
391 };
392
393 /** LAN78xx driver */
394 struct usb_driver lan78xx_driver __usb_driver = {
395 .ids = lan78xx_ids,
396 .id_count = ( sizeof ( lan78xx_ids ) / sizeof ( lan78xx_ids[0] ) ),
397 .class = USB_CLASS_ID ( 0xff, 0x00, 0xff ),
398 .score = USB_SCORE_NORMAL,
399 .probe = lan78xx_probe,
400 .remove = lan78xx_remove,
401 };