aboutsummaryrefslogtreecommitdiffstats
path: root/gpxe/src/drivers/net/rtl818x/rtl818x.c
blob: 7c7ca488aaf6b165503ad2e852af17e02b06066b (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
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
/*
 * Linux device driver for RTL8180 / RTL8185
 *
 * Copyright 2007 Michael Wu <flamingice@sourmilk.net>
 * Copyright 2007 Andrea Merello <andreamrl@tiscali.it>
 *
 * Modified for gPXE, June 2009, by Joshua Oreman <oremanj@rwcr.net>
 *
 * Based on the r8180 driver, which is:
 * Copyright 2004-2005 Andrea Merello <andreamrl@tiscali.it>, et al.
 *
 * Thanks to Realtek for their support!
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

FILE_LICENCE(GPL2_ONLY);

#include <stdint.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <byteswap.h>
#include <gpxe/iobuf.h>
#include <gpxe/malloc.h>
#include <gpxe/pci.h>
#include <gpxe/net80211.h>
#include <gpxe/netdevice.h>
#include <gpxe/threewire.h>

#include "rtl818x.h"

/* rtl818x_rates[hw rate number] = rate in 100kbps units */
static const u16 rtl818x_rates[] = {
	10, 20, 55, 110, /* 802.11b */
	60, 90, 120, 180, 240, 360, 480, 540, /* 802.11g */
	0, 0, 0, 0,		/* index safely using a value masked with 0xF */
};
#define RTL818X_NR_B_RATES  4
#define RTL818X_NR_RATES    12

/* used by RF drivers */
void rtl818x_write_phy(struct net80211_device *dev, u8 addr, u32 data)
{
	struct rtl818x_priv *priv = dev->priv;
	int i = 10;
	u32 buf;

	buf = (data << 8) | addr;

	rtl818x_iowrite32(priv, (u32 *)&priv->map->PHY[0], buf | 0x80);
	while (i--) {
		rtl818x_iowrite32(priv, (u32 *)&priv->map->PHY[0], buf);
		if (rtl818x_ioread8(priv, &priv->map->PHY[2]) == (data & 0xFF))
			return;
	}
}

static void rtl818x_handle_rx(struct net80211_device *dev)
{
	struct rtl818x_priv *priv = dev->priv;
	unsigned int count = RTL818X_RX_RING_SIZE;

	while (count--) {
		struct rtl818x_rx_desc *entry = &priv->rx_ring[priv->rx_idx];
		struct io_buffer *iob = priv->rx_buf[priv->rx_idx];
		u32 flags = le32_to_cpu(entry->flags);

		if (flags & RTL818X_RX_DESC_FLAG_OWN)
			return;

		if (flags & (RTL818X_RX_DESC_FLAG_DMA_FAIL |
			     RTL818X_RX_DESC_FLAG_FOF |
			     RTL818X_RX_DESC_FLAG_RX_ERR)) {
			/* This is crappy hardware. The Linux driver
			   doesn't even log these. */
			goto done;
		} else if (flags & RTL818X_RX_DESC_FLAG_CRC32_ERR) {
			/* This is actually a corrupt packet. */
			DBG2("rtl818x RX:%d CRC fail: flags %08x\n",
			     priv->rx_idx, flags);
			net80211_rx_err(dev, NULL, EIO);
		} else {
			u32 flags2 = le32_to_cpu(entry->flags2);
			struct io_buffer *new_iob = alloc_iob(MAX_RX_SIZE);
			if (!new_iob) {
				net80211_rx_err(dev, NULL, ENOMEM);
				goto done;
			}

			DBGP("rtl818x RX:%d success: flags %08x %08x\n",
			     priv->rx_idx, flags, flags2);

			iob_put(iob, flags & 0xFFF);

			net80211_rx(dev, iob, (flags2 >> 8) & 0x7f,
				    rtl818x_rates[(flags >> 20) & 0xf]);

			iob = new_iob;
			priv->rx_buf[priv->rx_idx] = iob;
		}

	done:
		entry->rx_buf = cpu_to_le32(virt_to_bus(iob->data));
		entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN | MAX_RX_SIZE);

		if (priv->rx_idx == RTL818X_RX_RING_SIZE - 1)
			entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);

		priv->rx_idx = (priv->rx_idx + 1) % RTL818X_RX_RING_SIZE;
	}
}

static void rtl818x_handle_tx(struct net80211_device *dev)
{
	struct rtl818x_priv *priv = dev->priv;
	unsigned int count = RTL818X_TX_RING_SIZE;

	while (count--) {
		struct rtl818x_tx_desc *entry = &priv->tx_ring[priv->tx_cons];
		struct io_buffer *iob = priv->tx_buf[priv->tx_cons];
		u32 flags = le32_to_cpu(entry->flags);
		int rc;

		if ((flags & RTL818X_TX_DESC_FLAG_OWN) || !iob)
			return;

		rc = 0;
		if (!(flags & RTL818X_TX_DESC_FLAG_TX_OK)) {
			/* our packet was not ACKed properly */
			rc = EIO;
		}

		net80211_tx_complete(dev, iob, flags & 0xFF, rc);

		priv->tx_buf[priv->tx_cons] = NULL;
		priv->tx_cons = (priv->tx_cons + 1) % RTL818X_TX_RING_SIZE;
	}
}

static void rtl818x_poll(struct net80211_device *dev)
{
	struct rtl818x_priv *priv = dev->priv;
	u16 reg = rtl818x_ioread16(priv, &priv->map->INT_STATUS);

	if (reg == 0xFFFF)
		return;

	rtl818x_iowrite16(priv, &priv->map->INT_STATUS, reg);

	if (reg & (RTL818X_INT_TXN_OK | RTL818X_INT_TXN_ERR))
		rtl818x_handle_tx(dev);

	if (reg & (RTL818X_INT_RX_OK | RTL818X_INT_RX_ERR))
		rtl818x_handle_rx(dev);
}

#define DIV_ROUND_UP(n,d) (((n)+(d)-1)/(d))

static int rtl818x_tx(struct net80211_device *dev, struct io_buffer *iob)
{
	struct rtl818x_priv *priv = dev->priv;
	struct rtl818x_tx_desc *entry;
	u32 tx_flags;
	u16 plcp_len = 0;
	int len = iob_len(iob);

	tx_flags = RTL818X_TX_DESC_FLAG_OWN | RTL818X_TX_DESC_FLAG_FS |
		RTL818X_TX_DESC_FLAG_LS | (priv->hw_rate << 24) | len;

	if (priv->r8185) {
		tx_flags |= RTL818X_TX_DESC_FLAG_DMA |
			    RTL818X_TX_DESC_FLAG_NO_ENC;
	} else {
		unsigned int remainder;

		plcp_len = DIV_ROUND_UP(16 * (len + 4),
					(dev->rates[dev->rate] * 2) / 10);
		remainder = (16 * (len + 4)) %
			    ((dev->rates[dev->rate] * 2) / 10);

		if (remainder > 0 && remainder <= 6)
			plcp_len |= 1 << 15;
	}

	entry = &priv->tx_ring[priv->tx_prod];

	if (dev->phy_flags & NET80211_PHY_USE_PROTECTION) {
		tx_flags |= RTL818X_TX_DESC_FLAG_CTS;
		tx_flags |= priv->hw_rtscts_rate << 19;
		entry->rts_duration = net80211_cts_duration(dev, len);
	} else {
		entry->rts_duration = 0;
	}

	if (entry->flags & RTL818X_TX_DESC_FLAG_OWN) {
		/* card hasn't processed the old packet yet! */
		return -EBUSY;
	}

	priv->tx_buf[priv->tx_prod] = iob;
	priv->tx_prod = (priv->tx_prod + 1) % RTL818X_TX_RING_SIZE;

	entry->plcp_len = cpu_to_le16(plcp_len);
	entry->tx_buf = cpu_to_le32(virt_to_bus(iob->data));
	entry->frame_len = cpu_to_le32(len);
	entry->flags2 = /* alternate retry rate in 100kbps << 4 */ 0;
	entry->retry_limit = RTL818X_MAX_RETRIES;
	entry->flags = cpu_to_le32(tx_flags);

	rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING, (1 << 5));

	return 0;
}

void rtl818x_set_anaparam(struct rtl818x_priv *priv, u32 anaparam)
{
	u8 reg;

	rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
	reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
	rtl818x_iowrite8(priv, &priv->map->CONFIG3,
		 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
	rtl818x_iowrite32(priv, &priv->map->ANAPARAM, anaparam);
	rtl818x_iowrite8(priv, &priv->map->CONFIG3,
		 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
	rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
}

static int rtl818x_init_hw(struct net80211_device *dev)
{
	struct rtl818x_priv *priv = dev->priv;
	u16 reg;

	rtl818x_iowrite8(priv, &priv->map->CMD, 0);
	rtl818x_ioread8(priv, &priv->map->CMD);
	mdelay(10);

	/* reset */
	rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
	rtl818x_ioread8(priv, &priv->map->CMD);

	reg = rtl818x_ioread8(priv, &priv->map->CMD);
	reg &= (1 << 1);
	reg |= RTL818X_CMD_RESET;
	rtl818x_iowrite8(priv, &priv->map->CMD, RTL818X_CMD_RESET);
	rtl818x_ioread8(priv, &priv->map->CMD);
	mdelay(200);

	/* check success of reset */
	if (rtl818x_ioread8(priv, &priv->map->CMD) & RTL818X_CMD_RESET) {
		DBG("rtl818x %s: reset timeout!\n", dev->netdev->name);
		return -ETIMEDOUT;
	}

	rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
	rtl818x_ioread8(priv, &priv->map->CMD);
	mdelay(200);

	if (rtl818x_ioread8(priv, &priv->map->CONFIG3) & (1 << 3)) {
		/* For cardbus */
		reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
		reg |= 1 << 1;
		rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
		reg = rtl818x_ioread16(priv, &priv->map->FEMR);
		reg |= (1 << 15) | (1 << 14) | (1 << 4);
		rtl818x_iowrite16(priv, &priv->map->FEMR, reg);
	}

	rtl818x_iowrite8(priv, &priv->map->MSR, 0);

	if (!priv->r8185)
		rtl818x_set_anaparam(priv, priv->anaparam);

	rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
	rtl818x_iowrite32(priv, &priv->map->TNPDA, priv->tx_ring_dma);

	/* TODO: necessary? specs indicate not */
	rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
	reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
	rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
	if (priv->r8185) {
		reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
		rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
	}
	rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);

	/* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */

	/* TODO: turn off hw wep on rtl8180 */

	rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);

	if (priv->r8185) {
		rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
		rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
		rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (8 << 4) | 0);

		rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);

		/* TODO: set ClkRun enable? necessary? */
		reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
		rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, reg & ~(1 << 6));
		rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
		reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
		rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
		rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
	} else {
		rtl818x_iowrite16(priv, &priv->map->BRSR, 0x1);
		rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);

		rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
		rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, 0x4C);
	}

	priv->rf->init(dev);
	if (priv->r8185)
		rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
	return 0;
}

static int rtl818x_init_rx_ring(struct net80211_device *dev)
{
	struct rtl818x_priv *priv = dev->priv;
	struct rtl818x_rx_desc *entry;
	int i;

	priv->rx_ring = malloc_dma(sizeof(*priv->rx_ring) * RTL818X_RX_RING_SIZE,
				   RTL818X_RING_ALIGN);
	priv->rx_ring_dma = virt_to_bus(priv->rx_ring);
	if (!priv->rx_ring) {
		DBG("rtl818x %s: cannot allocate RX ring\n", dev->netdev->name);
		return -ENOMEM;
	}

	memset(priv->rx_ring, 0, sizeof(*priv->rx_ring) * RTL818X_RX_RING_SIZE);
	priv->rx_idx = 0;

	for (i = 0; i < RTL818X_RX_RING_SIZE; i++) {
		struct io_buffer *iob = alloc_iob(MAX_RX_SIZE);
		entry = &priv->rx_ring[i];
		if (!iob)
			return -ENOMEM;

		priv->rx_buf[i] = iob;
		entry->rx_buf = cpu_to_le32(virt_to_bus(iob->data));
		entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
					   MAX_RX_SIZE);
	}
	entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
	return 0;
}

static void rtl818x_free_rx_ring(struct net80211_device *dev)
{
	struct rtl818x_priv *priv = dev->priv;
	int i;

	for (i = 0; i < RTL818X_RX_RING_SIZE; i++) {
		free_iob(priv->rx_buf[i]);
		priv->rx_buf[i] = NULL;
	}

	free_dma(priv->rx_ring, sizeof(*priv->rx_ring) * RTL818X_RX_RING_SIZE);
	priv->rx_ring = NULL;
}

static int rtl818x_init_tx_ring(struct net80211_device *dev)
{
	struct rtl818x_priv *priv = dev->priv;
	int i;

	priv->tx_ring = malloc_dma(sizeof(*priv->tx_ring) * RTL818X_TX_RING_SIZE,
				   RTL818X_RING_ALIGN);
	priv->tx_ring_dma = virt_to_bus(priv->tx_ring);
	if (!priv->tx_ring) {
		DBG("rtl818x %s: cannot allocate TX ring\n", dev->netdev->name);
		return -ENOMEM;
	}

	memset(priv->tx_ring, 0, sizeof(*priv->tx_ring) * RTL818X_TX_RING_SIZE);
	priv->tx_prod = priv->tx_cons = 0;

	for (i = 0; i < RTL818X_TX_RING_SIZE; i++)
		priv->tx_ring[i].next_tx_desc = cpu_to_le32(priv->tx_ring_dma +
				((i + 1) % RTL818X_TX_RING_SIZE) * sizeof(*priv->tx_ring));

	return 0;
}

static void rtl818x_free_tx_ring(struct net80211_device *dev)
{
	struct rtl818x_priv *priv = dev->priv;
	int i;

	for (i = 0; i < RTL818X_TX_RING_SIZE; i++) {
		if (priv->tx_buf[i])
			net80211_tx_complete(dev, priv->tx_buf[i], 0, ECANCELED);
		priv->tx_buf[i] = NULL;
	}

	free_dma(priv->tx_ring, sizeof(*priv->tx_ring) * RTL818X_TX_RING_SIZE);
	priv->tx_ring = NULL;
}

static void rtl818x_irq(struct net80211_device *dev, int enable)
{
	struct rtl818x_priv *priv = dev->priv;
	rtl818x_iowrite16(priv, &priv->map->INT_MASK, enable? 0xFFFF : 0);
}

/* Sets the MAC address of the card. */
static void rtl818x_set_hwaddr(struct net80211_device *dev, u8 *hwaddr)
{
	struct rtl818x_priv *priv = dev->priv;
	rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
	rtl818x_iowrite32(priv, (u32 *)&priv->map->MAC[0],
			  le32_to_cpu(*(u32 *)hwaddr));
	rtl818x_iowrite16(priv, (u16 *)&priv->map->MAC[4],
			  le16_to_cpu(*(u16 *)(hwaddr + 4)));
	rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
}

static int rtl818x_start(struct net80211_device *dev)
{
	struct rtl818x_priv *priv = dev->priv;
	int ret;
	u32 reg;

	ret = rtl818x_init_rx_ring(dev);
	if (ret)
		return ret;

	ret = rtl818x_init_tx_ring(dev);
	if (ret)
		goto err_free_rings;

	ret = rtl818x_init_hw(dev);
	if (ret)
		goto err_free_rings;

	rtl818x_set_hwaddr(dev, dev->netdev->ll_addr);

	rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
	rtl818x_iowrite32(priv, &priv->map->TNPDA, priv->tx_ring_dma);

	rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);

	rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
	rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);

	reg = RTL818X_RX_CONF_ONLYERLPKT |
	      RTL818X_RX_CONF_RX_AUTORESETPHY |
	      RTL818X_RX_CONF_MGMT |
	      RTL818X_RX_CONF_DATA |
	      (7 << 8 /* MAX RX DMA */) |
	      RTL818X_RX_CONF_BROADCAST |
	      RTL818X_RX_CONF_NICMAC;

	if (priv->r8185)
		reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
	else {
		reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
			? RTL818X_RX_CONF_CSDM1 : 0;
		reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
			? RTL818X_RX_CONF_CSDM2 : 0;
	}

	priv->rx_conf = reg;
	rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);

	if (priv->r8185) {
		reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
		reg &= ~RTL818X_CW_CONF_PERPACKET_CW_SHIFT;
		reg |= RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT;
		rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);

		reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
		reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT;
		reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT;
		reg |=  RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
		rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);

		/* disable early TX */
		rtl818x_iowrite8(priv, (u8 *)priv->map + 0xec, 0x3f);
	}

	reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
	reg |= (6 << 21 /* MAX TX DMA */) |
	       RTL818X_TX_CONF_NO_ICV;

	if (priv->r8185)
		reg &= ~RTL818X_TX_CONF_PROBE_DTS;
	else
		reg &= ~RTL818X_TX_CONF_HW_SEQNUM;

	/* different meaning, same value on both rtl8185 and rtl8180 */
	reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;

	rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);

	reg = rtl818x_ioread8(priv, &priv->map->CMD);
	reg |= RTL818X_CMD_RX_ENABLE;
	reg |= RTL818X_CMD_TX_ENABLE;
	rtl818x_iowrite8(priv, &priv->map->CMD, reg);

	DBG("%s rtl818x: started\n", dev->netdev->name);

	return 0;

 err_free_rings:
	rtl818x_free_rx_ring(dev);
	if (priv->tx_ring)
		rtl818x_free_tx_ring(dev);

	DBG("%s rtl818x: failed to start\n", dev->netdev->name);

	return ret;
}

static void rtl818x_stop(struct net80211_device *dev)
{
	struct rtl818x_priv *priv = dev->priv;
	u8 reg;

	rtl818x_irq(dev, 0);

	reg = rtl818x_ioread8(priv, &priv->map->CMD);
	reg &= ~RTL818X_CMD_TX_ENABLE;
	reg &= ~RTL818X_CMD_RX_ENABLE;
	rtl818x_iowrite8(priv, &priv->map->CMD, reg);

	priv->rf->stop(dev);

	rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
	reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
	rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
	rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);

	rtl818x_free_rx_ring(dev);
	rtl818x_free_tx_ring(dev);
}

static int rtl818x_config(struct net80211_device *dev, int changed)
{
	struct rtl818x_priv *priv = dev->priv;
	int i;

	if (changed & NET80211_CFG_CHANNEL)
		priv->rf->set_chan(dev, &dev->channels[dev->channel]);

	if (changed & NET80211_CFG_ASSOC) {
		for (i = 0; i < ETH_ALEN; i++)
			rtl818x_iowrite8(priv, &priv->map->BSSID[i], dev->bssid[i]);
		rtl818x_iowrite8(priv, &priv->map->MSR,
				 dev->state & NET80211_ASSOCIATED?
					RTL818X_MSR_INFRA : RTL818X_MSR_NO_LINK);
	}

	if (changed & NET80211_CFG_PHY_PARAMS)
		priv->rf->conf_erp(dev);

	if (changed & NET80211_CFG_RATE) {
		/* figure out the hardware rate number for the new
		   logical rate */
		int hw_rate;
		for (hw_rate = 0; hw_rate < RTL818X_NR_RATES &&
			     rtl818x_rates[hw_rate] != dev->rates[dev->rate];
		     hw_rate++)
			;
		if (hw_rate >= RTL818X_NR_RATES)
			return -EINVAL;

		priv->hw_rate = hw_rate;

		/* and the RTS/CTS rate */
		for (hw_rate = 0; hw_rate < RTL818X_NR_RATES &&
			     rtl818x_rates[hw_rate] !=
				dev->rates[dev->rtscts_rate];
		     hw_rate++)
			;
		if (hw_rate >= RTL818X_NR_RATES)
			hw_rate = priv->hw_rate;

		priv->hw_rtscts_rate = hw_rate;
	}

	return 0;
}

static const u8 rtl818x_eeprom_bits[] = {
	[SPI_BIT_SCLK] = RTL818X_EEPROM_CMD_CK,
	[SPI_BIT_MISO] = RTL818X_EEPROM_CMD_READ,
	[SPI_BIT_MOSI] = RTL818X_EEPROM_CMD_WRITE,
	[SPI_BIT_SS(0)] = RTL818X_EEPROM_CMD_CS,
};

static int rtl818x_spi_read_bit(struct bit_basher *basher, unsigned int bit_id)
{
	struct rtl818x_priv *priv = container_of(basher, struct rtl818x_priv,
						 spibit.basher);

	u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
	return reg & rtl818x_eeprom_bits[bit_id];
}

static void rtl818x_spi_write_bit(struct bit_basher *basher,
				  unsigned int bit_id, unsigned long data)
{
	struct rtl818x_priv *priv = container_of(basher, struct rtl818x_priv,
						 spibit.basher);

	u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
	u8 mask = rtl818x_eeprom_bits[bit_id];
	reg = (reg & ~mask) | (data & mask);

	rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);

	rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
	udelay(10);
}

static struct bit_basher_operations rtl818x_basher_ops = {
	.read = rtl818x_spi_read_bit,
	.write = rtl818x_spi_write_bit,
};

#if DBGLVL_MAX
static const char *rtl818x_rf_names[] = {
	NULL,			/* no 0 */
	"Intersil", "RFMD",	/* unsupported 1-2 */
	"SA2400", "max2820", "GRF5101",	/* supported 3-5 */
	NULL, NULL, NULL,	/* no 6-8 */
	"RTL8225",		/* supported 9 */
	"RTL8255",		/* unsupported 10 */
};
#define RTL818X_NR_RF_NAMES 11
#endif

struct net80211_device_operations rtl818x_operations = {
	.open = rtl818x_start,
	.close = rtl818x_stop,
	.transmit = rtl818x_tx,
	.poll = rtl818x_poll,
	.irq = rtl818x_irq,
	.config = rtl818x_config,
};

static int rtl818x_probe(struct pci_device *pdev,
			 const struct pci_device_id *id __unused)
{
	struct net80211_device *dev;
	struct rtl818x_priv *priv;
	struct rtl818x_rf_ops *rf;
	int err, i;
	const char *chip_name;
	u32 reg;
	u16 eeprom_val;
	struct net80211_hw_info *hwinfo;

	hwinfo = zalloc(sizeof(*hwinfo));
	if (!hwinfo) {
		DBG("rtl818x: hwinfo alloc failed\n");
		return -ENOMEM;
	}

	adjust_pci_device(pdev);

	dev = net80211_alloc(sizeof(*priv));
	if (!dev) {
		DBG("rtl818x: net80211 alloc failed\n");
		return -ENOMEM;
	}

	priv = dev->priv;
	priv->pdev = pdev;
	dev->netdev->dev = &pdev->dev;

	priv->map = (struct rtl818x_csr *)pdev->ioaddr;
	if (!priv->map) {
		DBG("rtl818x: cannot find device memory\n");
		err = -ENXIO;
		goto err_free_dev;
	}

	reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
	reg &= RTL818X_TX_CONF_HWVER_MASK;
	switch (reg) {
	case RTL818X_TX_CONF_R8180_ABCD:
		chip_name = "0";
		break;
	case RTL818X_TX_CONF_R8180_F:
		chip_name = "0vF";
		break;
	case RTL818X_TX_CONF_R8185_ABC:
		chip_name = "5";
		break;
	case RTL818X_TX_CONF_R8185_D:
		chip_name = "5vD";
		break;
	default:
		DBG("rtl818x: Unknown chip! (0x%x)\n", reg >> 25);
		err = -ENOSYS;
		goto err_free_dev;
	}

	priv->r8185 = reg & RTL818X_TX_CONF_R8185_ABC;

	hwinfo->bands = NET80211_BAND_BIT_2GHZ;
	hwinfo->flags = NET80211_HW_RX_HAS_FCS;
	hwinfo->signal_type = NET80211_SIGNAL_ARBITRARY;
	hwinfo->signal_max = 65;
	hwinfo->channel_change_time = 1000;

	memcpy(hwinfo->rates[NET80211_BAND_2GHZ], rtl818x_rates,
	       sizeof(*rtl818x_rates) * RTL818X_NR_RATES);

	if (priv->r8185) {
		hwinfo->modes = NET80211_MODE_B | NET80211_MODE_G;
		hwinfo->nr_rates[NET80211_BAND_2GHZ] = RTL818X_NR_RATES;
	} else {
		hwinfo->modes = NET80211_MODE_B;
		hwinfo->nr_rates[NET80211_BAND_2GHZ] = RTL818X_NR_B_RATES;
	}

	priv->spibit.basher.op = &rtl818x_basher_ops;
	priv->spibit.bus.mode = SPI_MODE_THREEWIRE;
	init_spi_bit_basher(&priv->spibit);

	DBG2("rtl818x RX_CONF: %08x\n", rtl818x_ioread32(priv, &priv->map->RX_CONF));

	if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
		init_at93c66(&priv->eeprom, 16);
	else
		init_at93c46(&priv->eeprom, 16);
	priv->eeprom.bus = &priv->spibit.bus;

	rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_PROGRAM);
	rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
	udelay(10);

	nvs_read(&priv->eeprom.nvs, 0x06, &eeprom_val, 2);
	DBG2("rtl818x eeprom val = %04x\n", eeprom_val);
	eeprom_val &= 0xFF;

	priv->rf = NULL;
	for_each_table_entry(rf, RTL818X_RF_DRIVERS) {
		if (rf->id == eeprom_val) {
			priv->rf = rf;
			break;
		}
	}

	if (!priv->rf) {
#if DBGLVL_MAX
		if (eeprom_val < RTL818X_NR_RF_NAMES &&
		    rtl818x_rf_names[eeprom_val] != NULL)
			DBG("rtl818x: %s RF frontend not supported!\n",
			    rtl818x_rf_names[eeprom_val]);
		else
			DBG("rtl818x: RF frontend #%d not recognized!\n",
			    eeprom_val);
#endif

		err = -ENOSYS;
		goto err_free_dev;
	}

	nvs_read(&priv->eeprom.nvs, 0x17, &eeprom_val, 2);
	priv->csthreshold = eeprom_val >> 8;
	if (!priv->r8185) {
		nvs_read(&priv->eeprom.nvs, 0xD, &priv->anaparam, 4);
		nvs_read(&priv->eeprom.nvs, 0x19, &priv->rfparam, 2);
		priv->anaparam = le32_to_cpu(priv->anaparam);
		priv->rfparam = le16_to_cpu(priv->rfparam);
	}

	/* read the MAC address */
	nvs_read(&priv->eeprom.nvs, 0x7, hwinfo->hwaddr, 6);

	/* CCK TX power */
	for (i = 0; i < 14; i += 2) {
		u16 txpwr;
		nvs_read(&priv->eeprom.nvs, 0x10 + (i >> 1), &txpwr, 2);
		priv->txpower[i] = txpwr & 0xFF;
		priv->txpower[i + 1] = txpwr >> 8;
	}

	/* OFDM TX power */
	if (priv->r8185) {
		for (i = 0; i < 14; i += 2) {
			u16 txpwr;
			nvs_read(&priv->eeprom.nvs, 0x20 + (i >> 1), &txpwr, 2);
			priv->txpower[i] |= (txpwr & 0xFF) << 8;
			priv->txpower[i + 1] |= txpwr & 0xFF00;
		}
	}

	rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);

	err = net80211_register(dev, &rtl818x_operations, hwinfo);
	if (err) {
		DBG("rtl818x: cannot register device\n");
		goto err_free_dev;
	}

	free(hwinfo);

	DBG("rtl818x: Realtek RTL818%s (RF chip %s) with address %s\n",
	    chip_name, priv->rf->name, netdev_addr(dev->netdev));

	return 0;

 err_free_dev:
	pci_set_drvdata(pdev, NULL);
	net80211_free(dev);
	free(hwinfo);
	return err;
}

static void rtl818x_remove(struct pci_device *pdev)
{
	struct net80211_device *dev = pci_get_drvdata(pdev);

	if (!dev)
		return;

	net80211_unregister(dev);
	net80211_free(dev);
}

/* Hide PCI_ROM definitions in here from parserom.pl; the definitions
   that should be used are in rtl8180.c and rtl8185.c. */
#define RTL_ROM PCI_ROM

static struct pci_device_id rtl818x_nics[] = {
	RTL_ROM(0x10ec, 0x8185, "rtl8185", "Realtek 8185", 0),
	RTL_ROM(0x1799, 0x700f, "f5d7000", "Belkin F5D7000", 0),
	RTL_ROM(0x1799, 0x701f, "f5d7010", "Belkin F5D7010", 0),

	RTL_ROM(0x10ec, 0x8180, "rtl8180", "Realtek 8180", 0),
	RTL_ROM(0x1799, 0x6001, "f5d6001", "Belkin F5D6001", 0),
	RTL_ROM(0x1799, 0x6020, "f5d6020", "Belkin F5D6020", 0),
	RTL_ROM(0x1186, 0x3300, "dwl510",  "D-Link DWL-510", 0),
};

struct pci_driver rtl818x_driver __pci_driver = {
	.ids            = rtl818x_nics,
	.id_count       = sizeof(rtl818x_nics) / sizeof(rtl818x_nics[0]),
	.probe		= rtl818x_probe,
	.remove		= rtl818x_remove,
};