summaryrefslogtreecommitdiff
path: root/sim/bfin/dv-bfin_emac.c
blob: 50f3d4f274d977b159e826428520c3734a4e262e (plain)
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
/* Blackfin Ethernet Media Access Controller (EMAC) model.

   Copyright (C) 2010-2013 Free Software Foundation, Inc.
   Contributed by Analog Devices, Inc.

   This file is part of simulators.

   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 3 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, see <http://www.gnu.org/licenses/>.  */

#include "config.h"

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_NET_IF_H
#include <net/if.h>
#endif
#ifdef HAVE_LINUX_IF_TUN_H
#include <linux/if_tun.h>
#endif

#ifdef HAVE_LINUX_IF_TUN_H
# define WITH_TUN 1
#else
# define WITH_TUN 0
#endif

#include "sim-main.h"
#include "sim-hw.h"
#include "devices.h"
#include "dv-bfin_emac.h"

/* XXX: This doesn't support partial DMA transfers.  */
/* XXX: The TUN pieces should be pushed to the PHY so that we work with
        multiple "networks" and the PHY takes care of it.  */

struct bfin_emac
{
  /* This top portion matches common dv_bfin struct.  */
  bu32 base;
  struct hw *dma_master;
  bool acked;

  int tap;
#if WITH_TUN
  struct ifreq ifr;
#endif
  bu32 rx_crc;

  /* Order after here is important -- matches hardware MMR layout.  */
  bu32 opmode, addrlo, addrhi, hashlo, hashhi, staadd, stadat, flc, vlan1, vlan2;
  bu32 _pad0;
  bu32 wkup_ctl, wkup_ffmsk0, wkup_ffmsk1, wkup_ffmsk2, wkup_ffmsk3;
  bu32 wkup_ffcmd, wkup_ffoff, wkup_ffcrc0, wkup_ffcrc1;
  bu32 _pad1[4];
  bu32 sysctl, systat, rx_stat, rx_stky, rx_irqe, tx_stat, tx_stky, tx_irqe;
  bu32 mmc_ctl, mmc_rirqs, mmc_rirqe, mmc_tirqs, mmc_tirqe;
  bu32 _pad2[3];
  bu16 BFIN_MMR_16(ptp_ctl);
  bu16 BFIN_MMR_16(ptp_ie);
  bu16 BFIN_MMR_16(ptp_istat);
  bu32 ptp_foff, ptp_fv1, ptp_fv2, ptp_fv3, ptp_addend, ptp_accr, ptp_offset;
  bu32 ptp_timelo, ptp_timehi, ptp_rxsnaplo, ptp_rxsnaphi, ptp_txsnaplo;
  bu32 ptp_txsnaphi, ptp_alarmlo, ptp_alarmhi, ptp_id_off, ptp_id_snap;
  bu32 ptp_pps_startlo, ptp_pps_starthi, ptp_pps_period;
  bu32 _pad3[1];
  bu32 rxc_ok, rxc_fcs, rxc_lign, rxc_octet, rxc_dmaovf, rxc_unicst, rxc_multi;
  bu32 rxc_broad, rxc_lnerri, rxc_lnerro, rxc_long, rxc_macctl, rxc_opcode;
  bu32 rxc_pause, rxc_allfrm, rxc_alloct, rxc_typed, rxc_short, rxc_eq64;
  bu32 rxc_lt128, rxc_lt256, rxc_lt512, rxc_lt1024, rxc_ge1024;
  bu32 _pad4[8];
  bu32 txc_ok, txc_1col, txc_gt1col, txc_octet, txc_defer, txc_latecl;
  bu32 txc_xs_col, txc_dmaund, txc_crserr, txc_unicst, txc_multi, txc_broad;
  bu32 txc_xs_dfr, txc_macctl, txc_allfrm, txc_alloct, txc_eq64, txc_lt128;
  bu32 txc_lt256, txc_lt512, txc_lt1024, txc_ge1024, txc_abort;
};
#define mmr_base()      offsetof(struct bfin_emac, opmode)
#define mmr_offset(mmr) (offsetof(struct bfin_emac, mmr) - mmr_base())
#define mmr_idx(mmr)    (mmr_offset (mmr) / 4)

static const char * const mmr_names[BFIN_MMR_EMAC_SIZE / 4] =
{
  "EMAC_OPMODE", "EMAC_ADDRLO", "EMAC_ADDRHI", "EMAC_HASHLO", "EMAC_HASHHI",
  "EMAC_STAADD", "EMAC_STADAT", "EMAC_FLC", "EMAC_VLAN1", "EMAC_VLAN2", NULL,
  "EMAC_WKUP_CTL", "EMAC_WKUP_FFMSK0", "EMAC_WKUP_FFMSK1", "EMAC_WKUP_FFMSK2",
  "EMAC_WKUP_FFMSK3", "EMAC_WKUP_FFCMD", "EMAC_WKUP_FFOFF", "EMAC_WKUP_FFCRC0",
  "EMAC_WKUP_FFCRC1", [mmr_idx (sysctl)] = "EMAC_SYSCTL", "EMAC_SYSTAT",
  "EMAC_RX_STAT", "EMAC_RX_STKY", "EMAC_RX_IRQE", "EMAC_TX_STAT",
  "EMAC_TX_STKY", "EMAC_TX_IRQE", "EMAC_MMC_CTL", "EMAC_MMC_RIRQS",
  "EMAC_MMC_RIRQE", "EMAC_MMC_TIRQS", "EMAC_MMC_TIRQE",
  [mmr_idx (ptp_ctl)] = "EMAC_PTP_CTL", "EMAC_PTP_IE", "EMAC_PTP_ISTAT",
  "EMAC_PTP_FOFF", "EMAC_PTP_FV1", "EMAC_PTP_FV2", "EMAC_PTP_FV3",
  "EMAC_PTP_ADDEND", "EMAC_PTP_ACCR", "EMAC_PTP_OFFSET", "EMAC_PTP_TIMELO",
  "EMAC_PTP_TIMEHI", "EMAC_PTP_RXSNAPLO", "EMAC_PTP_RXSNAPHI",
  "EMAC_PTP_TXSNAPLO", "EMAC_PTP_TXSNAPHI", "EMAC_PTP_ALARMLO",
  "EMAC_PTP_ALARMHI", "EMAC_PTP_ID_OFF", "EMAC_PTP_ID_SNAP",
  "EMAC_PTP_PPS_STARTLO", "EMAC_PTP_PPS_STARTHI", "EMAC_PTP_PPS_PERIOD",
  [mmr_idx (rxc_ok)] = "EMAC_RXC_OK", "EMAC_RXC_FCS", "EMAC_RXC_LIGN",
  "EMAC_RXC_OCTET", "EMAC_RXC_DMAOVF", "EMAC_RXC_UNICST", "EMAC_RXC_MULTI",
  "EMAC_RXC_BROAD", "EMAC_RXC_LNERRI", "EMAC_RXC_LNERRO", "EMAC_RXC_LONG",
  "EMAC_RXC_MACCTL", "EMAC_RXC_OPCODE", "EMAC_RXC_PAUSE", "EMAC_RXC_ALLFRM",
  "EMAC_RXC_ALLOCT", "EMAC_RXC_TYPED", "EMAC_RXC_SHORT", "EMAC_RXC_EQ64",
  "EMAC_RXC_LT128", "EMAC_RXC_LT256", "EMAC_RXC_LT512", "EMAC_RXC_LT1024",
  "EMAC_RXC_GE1024",
  [mmr_idx (txc_ok)] = "EMAC_TXC_OK", "EMAC_TXC_1COL", "EMAC_TXC_GT1COL",
  "EMAC_TXC_OCTET", "EMAC_TXC_DEFER", "EMAC_TXC_LATECL", "EMAC_TXC_XS_COL",
  "EMAC_TXC_DMAUND", "EMAC_TXC_CRSERR", "EMAC_TXC_UNICST", "EMAC_TXC_MULTI",
  "EMAC_TXC_BROAD", "EMAC_TXC_XS_DFR", "EMAC_TXC_MACCTL", "EMAC_TXC_ALLFRM",
  "EMAC_TXC_ALLOCT", "EMAC_TXC_EQ64", "EMAC_TXC_LT128", "EMAC_TXC_LT256",
  "EMAC_TXC_LT512", "EMAC_TXC_LT1024", "EMAC_TXC_GE1024", "EMAC_TXC_ABORT",
};
#define mmr_name(off) (mmr_names[(off) / 4] ? : "<INV>")

static struct hw *
mii_find_phy (struct hw *me, bu8 addr)
{
  struct hw *phy = hw_child (me);
  while (phy && --addr)
    phy = hw_sibling (phy);
  return phy;
}

static void
mii_write (struct hw *me)
{
  SIM_DESC sd = hw_system (me);
  struct bfin_emac *emac = hw_data (me);
  struct hw *phy;
  bu8 addr = PHYAD (emac->staadd);
  bu8 reg = REGAD (emac->staadd);
  bu16 data = emac->stadat;

  phy = mii_find_phy (me, addr);
  if (!phy)
    return;
  sim_hw_io_write_buffer (sd, phy, &data, 1, reg, 2);
}

static void
mii_read (struct hw *me)
{
  SIM_DESC sd = hw_system (me);
  struct bfin_emac *emac = hw_data (me);
  struct hw *phy;
  bu8 addr = PHYAD (emac->staadd);
  bu8 reg = REGAD (emac->staadd);
  bu16 data;

  phy = mii_find_phy (me, addr);
  if (!phy || sim_hw_io_read_buffer (sd, phy, &data, 1, reg, 2) != 2)
    data = 0xffff;

  emac->stadat = data;
}

static unsigned
bfin_emac_io_write_buffer (struct hw *me, const void *source,
			   int space, address_word addr, unsigned nr_bytes)
{
  struct bfin_emac *emac = hw_data (me);
  bu32 mmr_off;
  bu32 value;
  bu32 *valuep;

  /* XXX: 16bit accesses are allowed ...  */
  dv_bfin_mmr_require_32 (me, addr, nr_bytes, true);
  value = dv_load_4 (source);

  mmr_off = addr - emac->base;
  valuep = (void *)((unsigned long)emac + mmr_base() + mmr_off);

  HW_TRACE_WRITE ();

  switch (mmr_off)
    {
    case mmr_offset(hashlo):
    case mmr_offset(hashhi):
    case mmr_offset(stadat):
    case mmr_offset(flc):
    case mmr_offset(vlan1):
    case mmr_offset(vlan2):
    case mmr_offset(wkup_ffmsk0):
    case mmr_offset(wkup_ffmsk1):
    case mmr_offset(wkup_ffmsk2):
    case mmr_offset(wkup_ffmsk3):
    case mmr_offset(wkup_ffcmd):
    case mmr_offset(wkup_ffoff):
    case mmr_offset(wkup_ffcrc0):
    case mmr_offset(wkup_ffcrc1):
    case mmr_offset(sysctl):
    case mmr_offset(rx_irqe):
    case mmr_offset(tx_irqe):
    case mmr_offset(mmc_rirqe):
    case mmr_offset(mmc_tirqe):
      *valuep = value;
      break;
    case mmr_offset(opmode):
      if (!(*valuep & RE) && (value & RE))
	emac->rx_stat &= ~RX_COMP;
      if (!(*valuep & TE) && (value & TE))
	emac->tx_stat &= ~TX_COMP;
      *valuep = value;
      break;
    case mmr_offset(addrlo):
    case mmr_offset(addrhi):
      *valuep = value;
      break;
    case mmr_offset(wkup_ctl):
      dv_w1c_4_partial (valuep, value, 0xf20);
      break;
    case mmr_offset(systat):
      dv_w1c_4 (valuep, value, 0xe1);
      break;
    case mmr_offset(staadd):
      *valuep = value | STABUSY;
      if (value & STAOP)
	mii_write (me);
      else
	mii_read (me);
      *valuep &= ~STABUSY;
      break;
    case mmr_offset(rx_stat):
    case mmr_offset(tx_stat):
      /* Discard writes to these.  */
      break;
    case mmr_offset(rx_stky):
    case mmr_offset(tx_stky):
    case mmr_offset(mmc_rirqs):
    case mmr_offset(mmc_tirqs):
      dv_w1c_4 (valuep, value, -1);
      break;
    case mmr_offset(mmc_ctl):
      /* Writing to bit 0 clears all counters.  */
      *valuep = value & ~1;
      if (value & 1)
	{
	  memset (&emac->rxc_ok, 0, mmr_offset (rxc_ge1024) - mmr_offset (rxc_ok) + 4);
	  memset (&emac->txc_ok, 0, mmr_offset (txc_abort) - mmr_offset (txc_ok) + 4);
	}
      break;
    case mmr_offset(rxc_ok) ... mmr_offset(rxc_ge1024):
    case mmr_offset(txc_ok) ... mmr_offset(txc_abort):
      /* XXX: Are these supposed to be read-only ?  */
      *valuep = value;
      break;
    case mmr_offset(ptp_ctl) ... mmr_offset(ptp_pps_period):
      /* XXX: Only on some models; ignore for now.  */
      break;
    default:
      dv_bfin_mmr_invalid (me, addr, nr_bytes, true);
      break;
    }

  return nr_bytes;
}

static unsigned
bfin_emac_io_read_buffer (struct hw *me, void *dest,
			  int space, address_word addr, unsigned nr_bytes)
{
  struct bfin_emac *emac = hw_data (me);
  bu32 mmr_off;
  bu32 *valuep;

  /* XXX: 16bit accesses are allowed ...  */
  dv_bfin_mmr_require_32 (me, addr, nr_bytes, false);

  mmr_off = addr - emac->base;
  valuep = (void *)((unsigned long)emac + mmr_base() + mmr_off);

  HW_TRACE_READ ();

  switch (mmr_off)
    {
    case mmr_offset(opmode):
    case mmr_offset(addrlo):
    case mmr_offset(addrhi):
    case mmr_offset(hashlo):
    case mmr_offset(hashhi):
    case mmr_offset(staadd):
    case mmr_offset(stadat):
    case mmr_offset(flc):
    case mmr_offset(vlan1):
    case mmr_offset(vlan2):
    case mmr_offset(wkup_ctl):
    case mmr_offset(wkup_ffmsk0):
    case mmr_offset(wkup_ffmsk1):
    case mmr_offset(wkup_ffmsk2):
    case mmr_offset(wkup_ffmsk3):
    case mmr_offset(wkup_ffcmd):
    case mmr_offset(wkup_ffoff):
    case mmr_offset(wkup_ffcrc0):
    case mmr_offset(wkup_ffcrc1):
    case mmr_offset(sysctl):
    case mmr_offset(systat):
    case mmr_offset(rx_stat):
    case mmr_offset(rx_stky):
    case mmr_offset(rx_irqe):
    case mmr_offset(tx_stat):
    case mmr_offset(tx_stky):
    case mmr_offset(tx_irqe):
    case mmr_offset(mmc_rirqs):
    case mmr_offset(mmc_rirqe):
    case mmr_offset(mmc_tirqs):
    case mmr_offset(mmc_tirqe):
    case mmr_offset(mmc_ctl):
    case mmr_offset(rxc_ok) ... mmr_offset(rxc_ge1024):
    case mmr_offset(txc_ok) ... mmr_offset(txc_abort):
      dv_store_4 (dest, *valuep);
      break;
    case mmr_offset(ptp_ctl) ... mmr_offset(ptp_pps_period):
      /* XXX: Only on some models; ignore for now.  */
      break;
    default:
      dv_bfin_mmr_invalid (me, addr, nr_bytes, false);
      break;
    }

  return nr_bytes;
}

static void
attach_bfin_emac_regs (struct hw *me, struct bfin_emac *emac)
{
  address_word attach_address;
  int attach_space;
  unsigned attach_size;
  reg_property_spec reg;

  if (hw_find_property (me, "reg") == NULL)
    hw_abort (me, "Missing \"reg\" property");

  if (!hw_find_reg_array_property (me, "reg", 0, &reg))
    hw_abort (me, "\"reg\" property must contain three addr/size entries");

  hw_unit_address_to_attach_address (hw_parent (me),
				     &reg.address,
				     &attach_space, &attach_address, me);
  hw_unit_size_to_attach_size (hw_parent (me), &reg.size, &attach_size, me);

  if (attach_size != BFIN_MMR_EMAC_SIZE)
    hw_abort (me, "\"reg\" size must be %#x", BFIN_MMR_EMAC_SIZE);

  hw_attach_address (hw_parent (me),
		     0, attach_space, attach_address, attach_size, me);

  emac->base = attach_address;
}

static struct dv_bfin *dma_tx;

static unsigned
bfin_emac_dma_read_buffer (struct hw *me, void *dest, int space,
			   unsigned_word addr, unsigned nr_bytes)
{
  struct bfin_emac *emac = hw_data (me);
  struct dv_bfin *dma = hw_data (emac->dma_master);
  unsigned char *data = dest;
  static bool flop; /* XXX: This sucks.  */
  bu16 len;
  ssize_t ret;

  HW_TRACE_DMA_READ ();

  if (dma_tx == dma)
    {
      /* Handle the TX turn around and write the status.  */
      emac->tx_stat |= TX_OK;
      emac->tx_stky |= TX_OK;

      memcpy (data, &emac->tx_stat, 4);

      dma->acked = true;
      return 4;
    }

  if (!(emac->opmode & RE))
    return 0;

  if (!flop)
    {
      ssize_t pad_ret;
      /* Outgoing DMA buffer has 16bit len prepended to it.  */
      data += 2;

      /* This doesn't seem to work.
      if (emac->sysctl & RXDWA)
	{
	  memset (data, 0, 2);
	  data += 2;
	} */

      ret = read (emac->tap, data, nr_bytes);
      if (ret < 0)
	return 0;
      ret += 4; /* include crc */
      pad_ret = MAX (ret + 4, 64);
      len = pad_ret;
      memcpy (dest, &len, 2);

      pad_ret = (pad_ret + 3) & ~3;
      if (ret < pad_ret)
	memset (data + ret, 0, pad_ret - ret);
      pad_ret += 4;

      /* XXX: Need to check -- u-boot doesn't look at this.  */
      if (emac->sysctl & RXCKS)
	{
	  pad_ret += 4;
	  emac->rx_crc = 0;
	}
      ret = pad_ret;

      /* XXX: Don't support promiscuous yet.  */
      emac->rx_stat |= RX_ACCEPT;
      emac->rx_stat = (emac->rx_stat & ~RX_FRLEN) | len;

      emac->rx_stat |= RX_COMP;
      emac->rx_stky |= RX_COMP;
    }
  else
    {
      /* Write the RX status and crc info.  */
      emac->rx_stat |= RX_OK;
      emac->rx_stky |= RX_OK;

      ret = 4;
      if (emac->sysctl & RXCKS)
	{
	  memcpy (data, &emac->rx_crc, 4);
	  data += 4;
	  ret += 4;
	}
      memcpy (data, &emac->rx_stat, 4);
    }

  flop = !flop;
  dma->acked = true;
  return ret;
}

static unsigned
bfin_emac_dma_write_buffer (struct hw *me, const void *source,
			    int space, unsigned_word addr,
			    unsigned nr_bytes,
			    int violate_read_only_section)
{
  struct bfin_emac *emac = hw_data (me);
  struct dv_bfin *dma = hw_data (emac->dma_master);
  const unsigned char *data = source;
  bu16 len;
  ssize_t ret;

  HW_TRACE_DMA_WRITE ();

  if (!(emac->opmode & TE))
    return 0;

  /* Incoming DMA buffer has 16bit len prepended to it.  */
  memcpy (&len, data, 2);
  if (!len)
    return 0;

  ret = write (emac->tap, data + 2, len);
  if (ret < 0)
    return 0;
  ret += 2;

  emac->tx_stat |= TX_COMP;
  emac->tx_stky |= TX_COMP;

  dma_tx = dma;
  dma->acked = true;
  return ret;
}

static const struct hw_port_descriptor bfin_emac_ports[] =
{
  { "tx",   DV_PORT_TX,   0, output_port, },
  { "rx",   DV_PORT_RX,   0, output_port, },
  { "stat", DV_PORT_STAT, 0, output_port, },
  { NULL, 0, 0, 0, },
};

static void
bfin_emac_attach_address_callback (struct hw *me,
				   int level,
				   int space,
				   address_word addr,
				   address_word nr_bytes,
				   struct hw *client)
{
  const hw_unit *unit = hw_unit_address (client);
  HW_TRACE ((me, "attach - level=%d, space=%d, addr=0x%lx, nr_bytes=%lu, client=%s",
	     level, space, (unsigned long) addr, (unsigned long) nr_bytes, hw_path (client)));
  /* NOTE: At preset the space is assumed to be zero.  Perhaphs the
     space should be mapped onto something for instance: space0 -
     unified memory; space1 - IO memory; ... */
  sim_core_attach (hw_system (me),
		   NULL, /*cpu*/
		   level + 10 + unit->cells[unit->nr_cells - 1],
		   access_read_write_exec,
		   space, addr,
		   nr_bytes,
		   0, /* modulo */
		   client,
		   NULL);
}

static void
bfin_emac_delete (struct hw *me)
{
  struct bfin_emac *emac = hw_data (me);
  close (emac->tap);
}

static void
bfin_emac_tap_init (struct hw *me)
{
#if WITH_TUN
  struct bfin_emac *emac = hw_data (me);
  const hw_unit *unit;
  int flags;

  unit = hw_unit_address (me);

  emac->tap = open ("/dev/net/tun", O_RDWR);
  if (emac->tap == -1)
    {
      HW_TRACE ((me, "unable to open /dev/net/tun: %s", strerror (errno)));
      return;
    }

  memset (&emac->ifr, 0, sizeof (emac->ifr));
  emac->ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  strcpy (emac->ifr.ifr_name, "tap-gdb");

  flags = 1 * 1024 * 1024;
  if (ioctl (emac->tap, TUNSETIFF, &emac->ifr) < 0
#ifdef TUNSETNOCSUM
      || ioctl (emac->tap, TUNSETNOCSUM) < 0
#endif
#ifdef TUNSETSNDBUF
      || ioctl (emac->tap, TUNSETSNDBUF, &flags) < 0
#endif
     )
    {
      HW_TRACE ((me, "tap ioctl setup failed: %s", strerror (errno)));
      close (emac->tap);
      return;
    }

  flags = fcntl (emac->tap, F_GETFL);
  fcntl (emac->tap, F_SETFL, flags | O_NONBLOCK);
#endif
}

static void
bfin_emac_finish (struct hw *me)
{
  struct bfin_emac *emac;

  emac = HW_ZALLOC (me, struct bfin_emac);

  set_hw_data (me, emac);
  set_hw_io_read_buffer (me, bfin_emac_io_read_buffer);
  set_hw_io_write_buffer (me, bfin_emac_io_write_buffer);
  set_hw_dma_read_buffer (me, bfin_emac_dma_read_buffer);
  set_hw_dma_write_buffer (me, bfin_emac_dma_write_buffer);
  set_hw_ports (me, bfin_emac_ports);
  set_hw_attach_address (me, bfin_emac_attach_address_callback);
  set_hw_delete (me, bfin_emac_delete);

  attach_bfin_emac_regs (me, emac);

  /* Initialize the EMAC.  */
  emac->addrlo = 0xffffffff;
  emac->addrhi = 0x0000ffff;
  emac->vlan1 = 0x0000ffff;
  emac->vlan2 = 0x0000ffff;
  emac->sysctl = 0x00003f00;
  emac->mmc_ctl = 0x0000000a;

  bfin_emac_tap_init (me);
}

const struct hw_descriptor dv_bfin_emac_descriptor[] =
{
  {"bfin_emac", bfin_emac_finish,},
  {NULL, NULL},
};