summaryrefslogtreecommitdiffstats
path: root/src/drivers/net/cgem.c
blob: c935c8024cd1bb5961bbb91f713b5d846fb5e9f6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/*
 * Copyright (C) 2025 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/netdevice.h>
#include <ipxe/ethernet.h>
#include <ipxe/if_ether.h>
#include <ipxe/iobuf.h>
#include <ipxe/timer.h>
#include <ipxe/devtree.h>
#include <ipxe/fdt.h>
#include "cgem.h"

/** @file
 *
 * Cadence Gigabit Ethernet MAC (GEM) network driver
 *
 * Based primarily on the Zynq 7000 SoC Technical Reference Manual,
 * available at the time of writing from:
 *
 *     https://docs.amd.com/r/en-US/ug585-zynq-7000-SoC-TRM
 *
 */

/******************************************************************************
 *
 * Device reset
 *
 ******************************************************************************
 */

/**
 * Reset hardware
 *
 * @v cgem		Cadence GEM device
 * @ret rc		Return status code
 */
static int cgem_reset ( struct cgem_nic *cgem ) {

	/* There is no software-driven reset capability in the
	 * hardware.  Instead we have to write the expected reset
	 * values to the various registers.
	 */

	/* Disable all interrupts */
	writel ( CGEM_IDR_ALL, ( cgem->regs + CGEM_IDR ) );

	/* Clear network control register */
	writel ( 0, ( cgem->regs + CGEM_NWCTRL ) );

	/* Clear statistics registers now that TX and RX are stopped */
	writel ( CGEM_NWCTRL_STATCLR, ( cgem->regs + CGEM_NWCTRL ) );

	/* Clear TX queue base address */
	writel ( 0, ( cgem->regs + CGEM_TXQBASE ) );

	/* Clear RX queue base address */
	writel ( 0, ( cgem->regs + CGEM_RXQBASE ) );

	/* Configure DMA */
	writel ( ( CGEM_DMACR_RXBUF ( CGEM_RX_LEN ) | CGEM_DMACR_TXSIZE_MAX |
		   CGEM_DMACR_RXSIZE_MAX | CGEM_DMACR_BLENGTH_MAX ),
		 ( cgem->regs + CGEM_DMACR ) );

	/* Enable MII interface */
	writel ( CGEM_NWCTRL_MDEN, ( cgem->regs + CGEM_NWCTRL ) );

	return 0;
}

/******************************************************************************
 *
 * PHY access
 *
 ******************************************************************************
 */

/**
 * Wait for MII operation to complete
 *
 * @v cgem		Cadence GEM device
 * @ret rc		Return status code
 */
static int cgem_mii_wait ( struct cgem_nic *cgem ) {
	uint32_t nwsr;
	unsigned int i;

	/* Wait for MII interface to become idle */
	for ( i = 0 ; i < CGEM_MII_MAX_WAIT_US ; i++ ) {

		/* Check if MII interface is idle */
		nwsr = readl ( cgem->regs + CGEM_NWSR );
		if ( nwsr & CGEM_NWSR_MII_IDLE )
			return 0;

		/* Delay */
		udelay ( 1 );
	}

	DBGC ( cgem, "CGEM %s timed out waiting for MII\n", cgem->name );
	return -ETIMEDOUT;
}

/**
 * Read from MII register
 *
 * @v mdio		MII interface
 * @v phy		PHY address
 * @v reg		Register address
 * @ret data		Data read, or negative error
 */
static int cgem_mii_read ( struct mii_interface *mdio, unsigned int phy,
			   unsigned int reg ) {
	struct cgem_nic *cgem = container_of ( mdio, struct cgem_nic, mdio );
	unsigned int data;
	int rc;

	/* Initiate read */
	writel ( ( CGEM_PHYMNTNC_CLAUSE22 | CGEM_PHYMNTNC_OP_READ |
		   CGEM_PHYMNTNC_ADDR ( phy ) | CGEM_PHYMNTNC_REG ( reg ) |
		   CGEM_PHYMNTNC_FIXED ),
		 ( cgem->regs + CGEM_PHYMNTNC ) );

	/* Wait for read to complete */
	if ( ( rc = cgem_mii_wait ( cgem ) ) != 0 )
		return rc;

	/* Read data */
	data = ( readl ( cgem->regs + CGEM_PHYMNTNC ) &
		 CGEM_PHYMNTNC_DATA_MASK );

	return data;
}

/**
 * Write to MII register
 *
 * @v mdio		MII interface
 * @v phy		PHY address
 * @v reg		Register address
 * @v data		Data to write
 * @ret rc		Return status code
 */
static int cgem_mii_write ( struct mii_interface *mdio, unsigned int phy,
			    unsigned int reg, unsigned int data ) {
	struct cgem_nic *cgem = container_of ( mdio, struct cgem_nic, mdio );
	int rc;

	/* Initiate write */
	writel ( ( CGEM_PHYMNTNC_CLAUSE22 | CGEM_PHYMNTNC_OP_READ |
		   CGEM_PHYMNTNC_ADDR ( phy ) | CGEM_PHYMNTNC_REG ( reg ) |
		   CGEM_PHYMNTNC_FIXED | data ),
		 ( cgem->regs + CGEM_PHYMNTNC ) );

	/* Wait for write to complete */
	if ( ( rc = cgem_mii_wait ( cgem ) ) != 0 )
		return rc;

	return 0;
}

/** MII operations */
static struct mii_operations cgem_mii_operations = {
	.read = cgem_mii_read,
	.write = cgem_mii_write,
};

/******************************************************************************
 *
 * Link state
 *
 ******************************************************************************
 */

/**
 * Initialise PHY
 *
 * @v cgem		Cadence GEM device
 * @ret rc		Return status code
 */
static int cgem_init_phy ( struct cgem_nic *cgem ) {
	int rc;

	/* Find PHY address */
	if ( ( rc = mii_find ( &cgem->mii ) ) != 0 ) {
		DBGC ( cgem, "CGEM %s could not find PHY address: %s\n",
		       cgem->name, strerror ( rc ) );
		return rc;
	}

	/* Reset PHY */
	if ( ( rc = mii_reset ( &cgem->mii ) ) != 0 ) {
		DBGC ( cgem, "CGEM %s could not reset PHY: %s\n",
		       cgem->name, strerror ( rc ) );
		return rc;
	}

	return 0;
}

/**
 * Check link state
 *
 * @v netdev		Network device
 */
static int cgem_check_link ( struct net_device *netdev ) {
	struct cgem_nic *cgem = netdev->priv;
	int rc;

	/* Check link state */
	if ( ( rc = mii_check_link ( &cgem->mii, netdev ) ) != 0 ) {
		DBGC ( cgem, "CGEM %s could not check link: %s\n",
		       cgem->name, strerror ( rc ) );
		return rc;
	}

	return 0;
}

/**
 * Check link state periodically
 *
 * @v retry		Link state check timer
 * @v over		Failure indicator
 */
static void cgem_expired ( struct retry_timer *timer, int over __unused ) {
	struct cgem_nic *cgem = container_of ( timer, struct cgem_nic, timer );
	struct net_device *netdev = cgem->netdev;

	/* Restart timer */
	start_timer_fixed ( timer, CGEM_LINK_INTERVAL );

	/* Check link state */
	cgem_check_link ( netdev );
}

/******************************************************************************
 *
 * Network device interface
 *
 ******************************************************************************
 */

/**
 * Create descriptor ring
 *
 * @v cgem		Cadence GEM device
 * @v ring		Descriptor ring
 * @ret rc		Return status code
 */
static int cgem_create_ring ( struct cgem_nic *cgem, struct cgem_ring *ring ) {
	struct cgem_descriptor *desc;
	unsigned int i;

	/* Allocate descriptor ring (on its own size) */
	ring->desc = dma_alloc ( cgem->dma, &ring->map, ring->len, ring->len );
	if ( ! ring->desc )
		return -ENOMEM;

	/* Initialise descriptor ring */
	for ( i = 0 ; i < ring->count ; i++ ) {
		desc = &ring->desc[i];
		desc->addr = cpu_to_le32 ( CGEM_RX_ADDR_OWNED );
		desc->flags = cpu_to_le32 ( CGEM_TX_FL_OWNED );
	}
	desc = &ring->desc[ ring->count - 1 ];
	desc->addr |= cpu_to_le32 ( CGEM_RX_ADDR_WRAP );
	desc->flags |= cpu_to_le32 ( CGEM_TX_FL_WRAP );

	/* Program ring address */
	writel ( dma ( &ring->map, ring->desc ),
		 ( cgem->regs + ring->qbase ) );

	DBGC ( cgem, "CGEM %s ring %02x is at [%08lx,%08lx)\n",
	       cgem->name, ring->qbase, virt_to_phys ( ring->desc ),
	       ( virt_to_phys ( ring->desc ) + ring->len ) );
	return 0;
}

/**
 * Destroy descriptor ring
 *
 * @v cgem		Cadence GEM device
 * @v ring		Descriptor ring
 */
static void cgem_destroy_ring ( struct cgem_nic *cgem,
				struct cgem_ring *ring ) {

	/* Clear ring address */
	writel ( 0, ( cgem->regs + ring->qbase ) );

	/* Free descriptor ring */
	dma_free ( &ring->map, ring->desc, ring->len );
	ring->desc = NULL;
	ring->prod = 0;
	ring->cons = 0;
}

/**
 * Refill receive descriptor ring
 *
 * @v cgem		Cadence GEM device
 */
static void cgem_refill_rx ( struct cgem_nic *cgem ) {
	struct cgem_descriptor *rx;
	struct io_buffer *iobuf;
	unsigned int rx_idx;
	uint32_t addr;

	/* Refill ring */
	while ( ( cgem->rx.prod - cgem->rx.cons ) != CGEM_NUM_RX_DESC ) {

		/* Allocate I/O buffer */
		iobuf = alloc_rx_iob ( CGEM_RX_LEN, cgem->dma );
		if ( ! iobuf ) {
			/* Wait for next refill */
			break;
		}

		/* Get next receive descriptor */
		rx_idx = ( cgem->rx.prod++ % CGEM_NUM_RX_DESC );
		rx = &cgem->rx.desc[rx_idx];

		/* Populate receive descriptor */
		rx->flags = 0;
		addr = 0;
		if ( ( cgem->rx.prod % CGEM_NUM_RX_DESC ) == 0 )
			addr |= CGEM_RX_ADDR_WRAP;
		rx->addr = cpu_to_le32 ( addr | iob_dma ( iobuf ) );

		/* Record I/O buffer */
		assert ( cgem->rx_iobuf[rx_idx] == NULL );
		cgem->rx_iobuf[rx_idx] = iobuf;

		DBGC2 ( cgem, "CGEM %s RX %d is [%08lx,%08lx)\n",
			cgem->name, rx_idx, virt_to_phys ( iobuf->data ),
			( virt_to_phys ( iobuf->data ) + CGEM_RX_LEN ) );
	}
}

/**
 * Open network device
 *
 * @v netdev		Network device
 * @ret rc		Return status code
 */
static int cgem_open ( struct net_device *netdev ) {
	struct cgem_nic *cgem = netdev->priv;
	union cgem_mac mac;
	int rc;

	/* Create transmit descriptor ring */
	if ( ( rc = cgem_create_ring ( cgem, &cgem->tx ) ) != 0 )
		goto err_create_tx;

	/* Create receive descriptor ring */
	if ( ( rc = cgem_create_ring ( cgem, &cgem->rx ) ) != 0 )
		goto err_create_rx;

	/* Set MAC address */
	memcpy ( mac.raw, netdev->ll_addr, ETH_ALEN );
	writel ( mac.reg.low, ( cgem->regs + CGEM_LADDRL ) );
	writel ( mac.reg.high, ( cgem->regs + CGEM_LADDRH ) );

	/* Enable transmit and receive */
	writel ( CGEM_NWCTRL_NORMAL, ( cgem->regs + CGEM_NWCTRL ) );

	/* Refill receive descriptor ring */
	cgem_refill_rx ( cgem );

	/* Update link state */
	cgem_check_link ( netdev );

	/* Start link state timer */
	start_timer_fixed ( &cgem->timer, CGEM_LINK_INTERVAL );

	return 0;

	cgem_destroy_ring ( cgem, &cgem->rx );
 err_create_rx:
	cgem_destroy_ring ( cgem, &cgem->tx );
 err_create_tx:
	return rc;
}

/**
 * Close network device
 *
 * @v netdev		Network device
 */
static void cgem_close ( struct net_device *netdev ) {
	struct cgem_nic *cgem = netdev->priv;
	unsigned int i;

	/* Stop link state timer */
	stop_timer ( &cgem->timer );

	/* Reset NIC */
	cgem_reset ( cgem );

	/* Discard unused receive buffers */
	for ( i = 0 ; i < CGEM_NUM_RX_DESC ; i++ ) {
		if ( cgem->rx_iobuf[i] )
			free_rx_iob ( cgem->rx_iobuf[i] );
		cgem->rx_iobuf[i] = NULL;
	}

	/* Destroy receive descriptor ring */
	cgem_destroy_ring ( cgem, &cgem->rx );

	/* Destroy transmit descriptor ring */
	cgem_destroy_ring ( cgem, &cgem->tx );
}

/**
 * Transmit packet
 *
 * @v netdev		Network device
 * @v iobuf		I/O buffer
 * @ret rc		Return status code
 */
static int cgem_transmit ( struct net_device *netdev,
			   struct io_buffer *iobuf ) {
	struct cgem_nic *cgem = netdev->priv;
	struct cgem_descriptor *tx;
	unsigned int tx_idx;
	uint32_t flags;
	int rc;

	/* Get next transmit descriptor */
	if ( ( cgem->tx.prod - cgem->tx.cons ) >= CGEM_NUM_TX_DESC ) {
		DBGC ( cgem, "CGEM %s out of transmit descriptors\n",
		       cgem->name );
		return -ENOBUFS;
	}
	tx_idx = ( cgem->tx.prod % CGEM_NUM_TX_DESC );
	tx = &cgem->tx.desc[tx_idx];

	/* Pad to minimum length */
	iob_pad ( iobuf, ETH_ZLEN );

	/* Map I/O buffer */
	if ( ( rc = iob_map_tx ( iobuf, cgem->dma ) ) != 0 )
		return rc;

	/* Update producer index */
	cgem->tx.prod++;

	/* Populate transmit descriptor */
	flags = CGEM_TX_FL_LAST;
	if ( ( cgem->tx.prod % CGEM_NUM_TX_DESC ) == 0 )
		flags |= CGEM_TX_FL_WRAP;
	tx->addr = cpu_to_le32 ( iob_dma ( iobuf ) );
	wmb();
	tx->flags = cpu_to_le32 ( flags | iob_len ( iobuf ) );
	wmb();

	/* Initiate transmission */
	writel ( ( CGEM_NWCTRL_NORMAL | CGEM_NWCTRL_STARTTX ),
		 ( cgem->regs + CGEM_NWCTRL ) );

	DBGC2 ( cgem, "CGEM %s TX %d is [%08lx,%08lx)\n",
		cgem->name, tx_idx, virt_to_phys ( iobuf->data ),
		( virt_to_phys ( iobuf->data ) + iob_len ( iobuf ) ) );
	return 0;
}

/**
 * Poll for completed packets
 *
 * @V netdev		Network device
 */
static void cgem_poll_tx ( struct net_device *netdev ) {
	struct cgem_nic *cgem = netdev->priv;
	struct cgem_descriptor *tx;
	unsigned int tx_idx;

	/* Check for completed packets */
	while ( cgem->tx.cons != cgem->tx.prod ) {

		/* Get next transmit descriptor */
		tx_idx = ( cgem->tx.cons % CGEM_NUM_TX_DESC );
		tx = &cgem->tx.desc[tx_idx];

		/* Stop if descriptor is still owned by hardware */
		if ( ! ( tx->flags & cpu_to_le32 ( CGEM_TX_FL_OWNED ) ) )
			return;
		DBGC2 ( cgem, "CGEM %s TX %d complete\n",
			cgem->name, tx_idx );

		/* Complete transmit descriptor */
		netdev_tx_complete_next ( netdev );
		cgem->tx.cons++;
	}
}

/**
 * Poll for received packets
 *
 * @v netdev		Network device
 */
static void cgem_poll_rx ( struct net_device *netdev ) {
	struct cgem_nic *cgem = netdev->priv;
	struct cgem_descriptor *rx;
	struct io_buffer *iobuf;
	unsigned int rx_idx;
	uint32_t flags;
	size_t len;

	/* Check for received packets */
	while ( cgem->rx.cons != cgem->rx.prod ) {

		/* Get next receive descriptor */
		rx_idx = ( cgem->rx.cons % CGEM_NUM_RX_DESC );
		rx = &cgem->rx.desc[rx_idx];

		/* Stop if descriptor is still in use */
		if ( ! ( rx->addr & cpu_to_le32 ( CGEM_RX_ADDR_OWNED ) ) )
			return;

		/* Populate I/O buffer */
		iobuf = cgem->rx_iobuf[rx_idx];
		cgem->rx_iobuf[rx_idx] = NULL;
		flags = le32_to_cpu ( rx->flags );
		len = CGEM_RX_FL_LEN ( flags );
		iob_put ( iobuf, len );
		DBGC2 ( cgem, "CGEM %s RX %d complete (length %zd)\n",
			cgem->name, rx_idx, len );

		/* Hand off to network stack */
		netdev_rx ( netdev, iobuf );
		cgem->rx.cons++;
	}
}

/**
 * Poll for completed and received packets
 *
 * @v netdev		Network device
 */
static void cgem_poll ( struct net_device *netdev ) {
	struct cgem_nic *cgem = netdev->priv;

	/* Poll for TX competions */
	cgem_poll_tx ( netdev );

	/* Poll for RX completions */
	cgem_poll_rx ( netdev );

	/* Refill RX ring */
	cgem_refill_rx ( cgem );
}

/** Cadence GEM network device operations */
static struct net_device_operations cgem_operations = {
	.open		= cgem_open,
	.close		= cgem_close,
	.transmit	= cgem_transmit,
	.poll		= cgem_poll,
};

/******************************************************************************
 *
 * Devicetree interface
 *
 ******************************************************************************
 */

/**
 * Probe devicetree device
 *
 * @v dt		Devicetree device
 * @v offset		Starting node offset
 * @ret rc		Return status code
 */
static int cgem_probe ( struct dt_device *dt, unsigned int offset ) {
	struct net_device *netdev;
	struct cgem_nic *cgem;
	union cgem_mac mac;
	int rc;

	/* Allocate and initialise net device */
	netdev = alloc_etherdev ( sizeof ( *cgem ) );
	if ( ! netdev ) {
		rc = -ENOMEM;
		goto err_alloc;
	}
	netdev_init ( netdev, &cgem_operations );
	cgem = netdev->priv;
	dt_set_drvdata ( dt, netdev );
	netdev->dev = &dt->dev;
	memset ( cgem, 0, sizeof ( *cgem ) );
	cgem->dma = &dt->dma;
	cgem->netdev = netdev;
	cgem->name = netdev->dev->name;
	mdio_init ( &cgem->mdio, &cgem_mii_operations );
	mii_init ( &cgem->mii, &cgem->mdio, 0 );
	timer_init ( &cgem->timer, cgem_expired, &netdev->refcnt );
	cgem_init_ring ( &cgem->tx, CGEM_NUM_TX_DESC, CGEM_TXQBASE );
	cgem_init_ring ( &cgem->rx, CGEM_NUM_RX_DESC, CGEM_RXQBASE );

	/* Map registers */
	cgem->regs = dt_ioremap ( dt, offset, CGEM_REG_IDX, CGEM_REG_LEN );
	if ( ! cgem->regs ) {
		rc = -ENODEV;
		goto err_ioremap;
	}

	/* Reset the NIC */
	if ( ( rc = cgem_reset ( cgem ) ) != 0 )
		goto err_reset;

	/* Initialise the PHY */
	if ( ( rc = cgem_init_phy ( cgem ) ) != 0 )
		goto err_init_phy;

	/* Fetch devicetree MAC address */
	if ( ( rc = fdt_mac ( &sysfdt, offset, netdev ) ) != 0 ) {
		DBGC ( cgem, "CGEM %s could not fetch MAC: %s\n",
		       cgem->name, strerror ( rc ) );
		goto err_mac;
	}

	/* Fetch current MAC address, if set */
	mac.reg.low = readl ( cgem->regs + CGEM_LADDRL );
	mac.reg.high = readl ( cgem->regs + CGEM_LADDRH );
	memcpy ( netdev->ll_addr, mac.raw, ETH_ALEN );

	/* Register network device */
	if ( ( rc = register_netdev ( netdev ) ) != 0 )
		goto err_register_netdev;

	/* Set initial link state */
	cgem_check_link ( netdev );

	return 0;

	unregister_netdev ( netdev );
 err_register_netdev:
 err_mac:
 err_init_phy:
	cgem_reset ( cgem );
 err_reset:
	iounmap ( cgem->regs );
 err_ioremap:
	netdev_nullify ( netdev );
	netdev_put ( netdev );
 err_alloc:
	return rc;
}

/**
 * Remove devicetree device
 *
 * @v dt		Devicetree device
 */
static void cgem_remove ( struct dt_device *dt ) {
	struct net_device *netdev = dt_get_drvdata ( dt );
	struct cgem_nic *cgem = netdev->priv;

	/* Unregister network device */
	unregister_netdev ( netdev );

	/* Reset card */
	cgem_reset ( cgem );

	/* Free network device */
	iounmap ( cgem->regs );
	netdev_nullify ( netdev );
	netdev_put ( netdev );
}

/** Cadence GEM compatible model identifiers */
static const char * cgem_ids[] = {
	"sifive,fu540-c000-gem",
};

/** Cadence GEM devicetree driver */
struct dt_driver cgem_driver __dt_driver = {
	.name = "cgem",
	.ids = cgem_ids,
	.id_count = ( sizeof ( cgem_ids ) / sizeof ( cgem_ids[0] ) ),
	.probe = cgem_probe,
	.remove = cgem_remove,
};