[rtl818x] Add driver for Realtek 8180/8185 wireless cards
[people/oremanj/gpxe.git] / src / drivers / net / rtl818x / rtl818x.c
1
2 /*
3  * Linux device driver for RTL8180 / RTL8185
4  *
5  * Copyright 2007 Michael Wu <flamingice@sourmilk.net>
6  * Copyright 2007 Andrea Merello <andreamrl@tiscali.it>
7  *
8  * Modified for gPXE, June 2009, by Joshua Oreman <oremanj@rwcr.net>
9  *
10  * Based on the r8180 driver, which is:
11  * Copyright 2004-2005 Andrea Merello <andreamrl@tiscali.it>, et al.
12  *
13  * Thanks to Realtek for their support!
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License version 2 as
17  * published by the Free Software Foundation.
18  */
19
20 FILE_LICENCE(GPL2_ONLY);
21
22 #include <stdint.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <byteswap.h>
27 #include <gpxe/iobuf.h>
28 #include <gpxe/malloc.h>
29 #include <gpxe/pci.h>
30 #include <gpxe/net80211.h>
31 #include <gpxe/netdevice.h>
32 #include <gpxe/threewire.h>
33
34 #include "rtl818x.h"
35
36 /* rtl818x_rates[hw rate number] = rate in 100kbps units */
37 static const u16 rtl818x_rates[] = {
38         10, 20, 55, 110, /* 802.11b */
39         60, 90, 120, 180, 240, 360, 480, 540, /* 802.11g */
40         0, 0, 0, 0,             /* index safely using a value masked with 0xF */
41 };
42 #define RTL818X_NR_B_RATES  4
43 #define RTL818X_NR_RATES    12
44
45 /* used by RF drivers */
46 void rtl818x_write_phy(struct net80211_device *dev, u8 addr, u32 data)
47 {
48         struct rtl818x_priv *priv = dev->priv;
49         int i = 10;
50         u32 buf;
51
52         buf = (data << 8) | addr;
53
54         rtl818x_iowrite32(priv, (u32 *)&priv->map->PHY[0], buf | 0x80);
55         while (i--) {
56                 rtl818x_iowrite32(priv, (u32 *)&priv->map->PHY[0], buf);
57                 if (rtl818x_ioread8(priv, &priv->map->PHY[2]) == (data & 0xFF))
58                         return;
59         }
60 }
61
62 static void rtl818x_handle_rx(struct net80211_device *dev)
63 {
64         struct rtl818x_priv *priv = dev->priv;
65         unsigned int count = RTL818X_RX_RING_SIZE;
66
67         while (count--) {
68                 struct rtl818x_rx_desc *entry = &priv->rx_ring[priv->rx_idx];
69                 struct io_buffer *iob = priv->rx_buf[priv->rx_idx];
70                 u32 flags = le32_to_cpu(entry->flags);
71
72                 if (flags & RTL818X_RX_DESC_FLAG_OWN)
73                         return;
74
75                 if (flags & (RTL818X_RX_DESC_FLAG_DMA_FAIL |
76                              RTL818X_RX_DESC_FLAG_FOF |
77                              RTL818X_RX_DESC_FLAG_RX_ERR)) {
78                         /* This is crappy hardware. The Linux driver
79                            doesn't even log these. */
80                         goto done;
81                 } else if (flags & RTL818X_RX_DESC_FLAG_CRC32_ERR) {
82                         /* This is actually a corrupt packet. */
83                         DBG2("rtl818x RX:%d CRC fail: flags %08x\n",
84                              priv->rx_idx, flags);
85                         net80211_rx_err(dev, NULL, EIO);
86                 } else {
87                         u32 flags2 = le32_to_cpu(entry->flags2);
88                         struct io_buffer *new_iob = alloc_iob(MAX_RX_SIZE);
89                         if (!new_iob) {
90                                 net80211_rx_err(dev, NULL, ENOMEM);
91                                 goto done;
92                         }
93
94                         DBGP("rtl818x RX:%d success: flags %08x %08x\n",
95                              priv->rx_idx, flags, flags2);
96
97                         iob_put(iob, flags & 0xFFF);
98
99                         net80211_rx(dev, iob, (flags2 >> 8) & 0x7f,
100                                     rtl818x_rates[(flags >> 20) & 0xf]);
101
102                         iob = new_iob;
103                         priv->rx_buf[priv->rx_idx] = iob;
104                 }
105
106         done:
107                 entry->rx_buf = cpu_to_le32(virt_to_bus(iob->data));
108                 entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN | MAX_RX_SIZE);
109
110                 if (priv->rx_idx == RTL818X_RX_RING_SIZE - 1)
111                         entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
112
113                 priv->rx_idx = (priv->rx_idx + 1) % RTL818X_RX_RING_SIZE;
114         }
115 }
116
117 static void rtl818x_handle_tx(struct net80211_device *dev)
118 {
119         struct rtl818x_priv *priv = dev->priv;
120         unsigned int count = RTL818X_TX_RING_SIZE;
121
122         while (count--) {
123                 struct rtl818x_tx_desc *entry = &priv->tx_ring[priv->tx_cons];
124                 struct io_buffer *iob = priv->tx_buf[priv->tx_cons];
125                 u32 flags = le32_to_cpu(entry->flags);
126                 int rc;
127
128                 if ((flags & RTL818X_TX_DESC_FLAG_OWN) || !iob)
129                         return;
130
131                 rc = 0;
132                 if (!(flags & RTL818X_TX_DESC_FLAG_TX_OK)) {
133                         /* our packet was not ACKed properly */
134                         rc = EIO;
135                 }
136
137                 net80211_tx_complete(dev, iob, flags & 0xFF, rc);
138
139                 priv->tx_buf[priv->tx_cons] = NULL;
140                 priv->tx_cons = (priv->tx_cons + 1) % RTL818X_TX_RING_SIZE;
141         }
142 }
143
144 static void rtl818x_poll(struct net80211_device *dev)
145 {
146         struct rtl818x_priv *priv = dev->priv;
147         u16 reg = rtl818x_ioread16(priv, &priv->map->INT_STATUS);
148
149         if (reg == 0xFFFF)
150                 return;
151
152         rtl818x_iowrite16(priv, &priv->map->INT_STATUS, reg);
153
154         if (reg & (RTL818X_INT_TXN_OK | RTL818X_INT_TXN_ERR))
155                 rtl818x_handle_tx(dev);
156
157         if (reg & (RTL818X_INT_RX_OK | RTL818X_INT_RX_ERR))
158                 rtl818x_handle_rx(dev);
159 }
160
161 #define DIV_ROUND_UP(n,d) (((n)+(d)-1)/(d))
162
163 static int rtl818x_tx(struct net80211_device *dev, struct io_buffer *iob)
164 {
165         struct rtl818x_priv *priv = dev->priv;
166         struct rtl818x_tx_desc *entry;
167         u32 tx_flags;
168         u16 plcp_len = 0;
169         int len = iob_len(iob);
170
171         tx_flags = RTL818X_TX_DESC_FLAG_OWN | RTL818X_TX_DESC_FLAG_FS |
172                 RTL818X_TX_DESC_FLAG_LS | (priv->hw_rate << 24) | len;
173
174         if (priv->r8185) {
175                 tx_flags |= RTL818X_TX_DESC_FLAG_DMA |
176                             RTL818X_TX_DESC_FLAG_NO_ENC;
177         } else {
178                 unsigned int remainder;
179
180                 plcp_len = DIV_ROUND_UP(16 * (len + 4),
181                                         (dev->rates[dev->rate] * 2) / 10);
182                 remainder = (16 * (len + 4)) %
183                             ((dev->rates[dev->rate] * 2) / 10);
184
185                 if (remainder > 0 && remainder <= 6)
186                         plcp_len |= 1 << 15;
187         }
188
189         if (dev->phy_flags & NET80211_PHY_USE_PROTECTION) {
190                 tx_flags |= RTL818X_TX_DESC_FLAG_CTS;
191                 tx_flags |= priv->hw_rtscts_rate << 19;
192         }
193
194         entry = &priv->tx_ring[priv->tx_prod];
195
196         if (entry->flags & RTL818X_TX_DESC_FLAG_OWN) {
197                 /* card hasn't processed the old packet yet! */
198                 return -EBUSY;
199         }
200
201         priv->tx_buf[priv->tx_prod] = iob;
202         priv->tx_prod = (priv->tx_prod + 1) % RTL818X_TX_RING_SIZE;
203
204         entry->rts_duration = 0;
205         entry->plcp_len = cpu_to_le16(plcp_len);
206         entry->tx_buf = cpu_to_le32(virt_to_bus(iob->data));
207         entry->frame_len = cpu_to_le32(len);
208         entry->flags2 = /* alternate retry rate in 100kbps << 4 */ 0;
209         entry->retry_limit = RTL818X_MAX_RETRIES;
210         entry->flags = cpu_to_le32(tx_flags);
211
212         rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING, (1 << 5));
213
214         return 0;
215 }
216
217 void rtl818x_set_anaparam(struct rtl818x_priv *priv, u32 anaparam)
218 {
219         u8 reg;
220
221         rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
222         reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
223         rtl818x_iowrite8(priv, &priv->map->CONFIG3,
224                  reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
225         rtl818x_iowrite32(priv, &priv->map->ANAPARAM, anaparam);
226         rtl818x_iowrite8(priv, &priv->map->CONFIG3,
227                  reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
228         rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
229 }
230
231 static int rtl818x_init_hw(struct net80211_device *dev)
232 {
233         struct rtl818x_priv *priv = dev->priv;
234         u16 reg;
235
236         rtl818x_iowrite8(priv, &priv->map->CMD, 0);
237         rtl818x_ioread8(priv, &priv->map->CMD);
238         mdelay(10);
239
240         /* reset */
241         rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
242         rtl818x_ioread8(priv, &priv->map->CMD);
243
244         reg = rtl818x_ioread8(priv, &priv->map->CMD);
245         reg &= (1 << 1);
246         reg |= RTL818X_CMD_RESET;
247         rtl818x_iowrite8(priv, &priv->map->CMD, RTL818X_CMD_RESET);
248         rtl818x_ioread8(priv, &priv->map->CMD);
249         mdelay(200);
250
251         /* check success of reset */
252         if (rtl818x_ioread8(priv, &priv->map->CMD) & RTL818X_CMD_RESET) {
253                 DBG("rtl818x %s: reset timeout!\n", dev->netdev->name);
254                 return -ETIMEDOUT;
255         }
256
257         rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
258         rtl818x_ioread8(priv, &priv->map->CMD);
259         mdelay(200);
260
261         if (rtl818x_ioread8(priv, &priv->map->CONFIG3) & (1 << 3)) {
262                 /* For cardbus */
263                 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
264                 reg |= 1 << 1;
265                 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
266                 reg = rtl818x_ioread16(priv, &priv->map->FEMR);
267                 reg |= (1 << 15) | (1 << 14) | (1 << 4);
268                 rtl818x_iowrite16(priv, &priv->map->FEMR, reg);
269         }
270
271         rtl818x_iowrite8(priv, &priv->map->MSR, 0);
272
273         if (!priv->r8185)
274                 rtl818x_set_anaparam(priv, priv->anaparam);
275
276         rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
277         rtl818x_iowrite32(priv, &priv->map->TNPDA, priv->tx_ring_dma);
278
279         /* TODO: necessary? specs indicate not */
280         rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
281         reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
282         rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
283         if (priv->r8185) {
284                 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
285                 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
286         }
287         rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
288
289         /* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */
290
291         /* TODO: turn off hw wep on rtl8180 */
292
293         rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
294
295         if (priv->r8185) {
296                 rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
297                 rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
298                 rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (8 << 4) | 0);
299
300                 rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
301
302                 /* TODO: set ClkRun enable? necessary? */
303                 reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
304                 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, reg & ~(1 << 6));
305                 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
306                 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
307                 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
308                 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
309         } else {
310                 rtl818x_iowrite16(priv, &priv->map->BRSR, 0x1);
311                 rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);
312
313                 rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
314                 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, 0x4C);
315         }
316
317         priv->rf->init(dev);
318         if (priv->r8185)
319                 rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
320         return 0;
321 }
322
323 static int rtl818x_init_rx_ring(struct net80211_device *dev)
324 {
325         struct rtl818x_priv *priv = dev->priv;
326         struct rtl818x_rx_desc *entry;
327         int i;
328
329         priv->rx_ring = malloc_dma(sizeof(*priv->rx_ring) * RTL818X_RX_RING_SIZE,
330                                    RTL818X_RING_ALIGN);
331         priv->rx_ring_dma = virt_to_bus(priv->rx_ring);
332         if (!priv->rx_ring) {
333                 DBG("rtl818x %s: cannot allocate RX ring\n", dev->netdev->name);
334                 return -ENOMEM;
335         }
336
337         memset(priv->rx_ring, 0, sizeof(*priv->rx_ring) * RTL818X_RX_RING_SIZE);
338         priv->rx_idx = 0;
339
340         for (i = 0; i < RTL818X_RX_RING_SIZE; i++) {
341                 struct io_buffer *iob = alloc_iob(MAX_RX_SIZE);
342                 entry = &priv->rx_ring[i];
343                 if (!iob)
344                         return -ENOMEM;
345
346                 priv->rx_buf[i] = iob;
347                 entry->rx_buf = cpu_to_le32(virt_to_bus(iob->data));
348                 entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
349                                            MAX_RX_SIZE);
350         }
351         entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
352         return 0;
353 }
354
355 static void rtl818x_free_rx_ring(struct net80211_device *dev)
356 {
357         struct rtl818x_priv *priv = dev->priv;
358         int i;
359
360         for (i = 0; i < RTL818X_RX_RING_SIZE; i++) {
361                 free_iob(priv->rx_buf[i]);
362                 priv->rx_buf[i] = NULL;
363         }
364
365         free_dma(priv->rx_ring, sizeof(*priv->rx_ring) * RTL818X_RX_RING_SIZE);
366         priv->rx_ring = NULL;
367 }
368
369 static int rtl818x_init_tx_ring(struct net80211_device *dev)
370 {
371         struct rtl818x_priv *priv = dev->priv;
372         int i;
373
374         priv->tx_ring = malloc_dma(sizeof(*priv->tx_ring) * RTL818X_TX_RING_SIZE,
375                                    RTL818X_RING_ALIGN);
376         priv->tx_ring_dma = virt_to_bus(priv->tx_ring);
377         if (!priv->tx_ring) {
378                 DBG("rtl818x %s: cannot allocate TX ring\n", dev->netdev->name);
379                 return -ENOMEM;
380         }
381
382         memset(priv->tx_ring, 0, sizeof(*priv->tx_ring) * RTL818X_TX_RING_SIZE);
383         priv->tx_prod = priv->tx_cons = 0;
384
385         for (i = 0; i < RTL818X_TX_RING_SIZE; i++)
386                 priv->tx_ring[i].next_tx_desc = cpu_to_le32(priv->tx_ring_dma +
387                                 ((i + 1) % RTL818X_TX_RING_SIZE) * sizeof(*priv->tx_ring));
388
389         return 0;
390 }
391
392 static void rtl818x_free_tx_ring(struct net80211_device *dev)
393 {
394         struct rtl818x_priv *priv = dev->priv;
395         int i;
396
397         for (i = 0; i < RTL818X_TX_RING_SIZE; i++) {
398                 if (priv->tx_buf[i])
399                         net80211_tx_complete(dev, priv->tx_buf[i], 0, ECANCELED);
400                 priv->tx_buf[i] = NULL;
401         }
402
403         free_dma(priv->tx_ring, sizeof(*priv->tx_ring) * RTL818X_TX_RING_SIZE);
404         priv->tx_ring = NULL;
405 }
406
407 static void rtl818x_irq(struct net80211_device *dev, int enable)
408 {
409         struct rtl818x_priv *priv = dev->priv;
410         rtl818x_iowrite16(priv, &priv->map->INT_MASK, enable? 0xFFFF : 0);
411 }
412
413 /* Sets the MAC address of the card. */
414 static void rtl818x_set_hwaddr(struct net80211_device *dev, u8 *hwaddr)
415 {
416         struct rtl818x_priv *priv = dev->priv;
417         rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
418         rtl818x_iowrite32(priv, (u32 *)&priv->map->MAC[0],
419                           le32_to_cpu(*(u32 *)hwaddr));
420         rtl818x_iowrite16(priv, (u16 *)&priv->map->MAC[4],
421                           le16_to_cpu(*(u16 *)(hwaddr + 4)));
422         rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
423 }
424
425 static int rtl818x_start(struct net80211_device *dev)
426 {
427         struct rtl818x_priv *priv = dev->priv;
428         int ret;
429         u32 reg;
430
431         ret = rtl818x_init_rx_ring(dev);
432         if (ret)
433                 return ret;
434
435         ret = rtl818x_init_tx_ring(dev);
436         if (ret)
437                 goto err_free_rings;
438
439         ret = rtl818x_init_hw(dev);
440         if (ret)
441                 goto err_free_rings;
442
443         rtl818x_set_hwaddr(dev, dev->netdev->ll_addr);
444
445         rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
446         rtl818x_iowrite32(priv, &priv->map->TNPDA, priv->tx_ring_dma);
447
448         rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
449
450         rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
451         rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
452
453         reg = RTL818X_RX_CONF_ONLYERLPKT |
454               RTL818X_RX_CONF_RX_AUTORESETPHY |
455               RTL818X_RX_CONF_MGMT |
456               RTL818X_RX_CONF_DATA |
457               (7 << 8 /* MAX RX DMA */) |
458               RTL818X_RX_CONF_BROADCAST |
459               RTL818X_RX_CONF_NICMAC;
460
461         if (priv->r8185)
462                 reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
463         else {
464                 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
465                         ? RTL818X_RX_CONF_CSDM1 : 0;
466                 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
467                         ? RTL818X_RX_CONF_CSDM2 : 0;
468         }
469
470         priv->rx_conf = reg;
471         rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
472
473         if (priv->r8185) {
474                 reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
475                 reg &= ~RTL818X_CW_CONF_PERPACKET_CW_SHIFT;
476                 reg |= RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT;
477                 rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
478
479                 reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
480                 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT;
481                 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT;
482                 reg |=  RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
483                 rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
484
485                 /* disable early TX */
486                 rtl818x_iowrite8(priv, (u8 *)priv->map + 0xec, 0x3f);
487         }
488
489         reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
490         reg |= (6 << 21 /* MAX TX DMA */) |
491                RTL818X_TX_CONF_NO_ICV;
492
493         if (priv->r8185)
494                 reg &= ~RTL818X_TX_CONF_PROBE_DTS;
495         else
496                 reg &= ~RTL818X_TX_CONF_HW_SEQNUM;
497
498         /* different meaning, same value on both rtl8185 and rtl8180 */
499         reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;
500
501         rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
502
503         reg = rtl818x_ioread8(priv, &priv->map->CMD);
504         reg |= RTL818X_CMD_RX_ENABLE;
505         reg |= RTL818X_CMD_TX_ENABLE;
506         rtl818x_iowrite8(priv, &priv->map->CMD, reg);
507
508         DBG("%s rtl818x: started\n", dev->netdev->name);
509
510         return 0;
511
512  err_free_rings:
513         rtl818x_free_rx_ring(dev);
514         if (priv->tx_ring)
515                 rtl818x_free_tx_ring(dev);
516
517         DBG("%s rtl818x: failed to start\n", dev->netdev->name);
518
519         return ret;
520 }
521
522 static void rtl818x_stop(struct net80211_device *dev)
523 {
524         struct rtl818x_priv *priv = dev->priv;
525         u8 reg;
526
527         rtl818x_irq(dev, 0);
528
529         reg = rtl818x_ioread8(priv, &priv->map->CMD);
530         reg &= ~RTL818X_CMD_TX_ENABLE;
531         reg &= ~RTL818X_CMD_RX_ENABLE;
532         rtl818x_iowrite8(priv, &priv->map->CMD, reg);
533
534         priv->rf->stop(dev);
535
536         rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
537         reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
538         rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
539         rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
540
541         rtl818x_free_rx_ring(dev);
542         rtl818x_free_tx_ring(dev);
543 }
544
545 static int rtl818x_config(struct net80211_device *dev, int changed)
546 {
547         struct rtl818x_priv *priv = dev->priv;
548         int i;
549
550         if (changed & NET80211_CFG_CHANNEL)
551                 priv->rf->set_chan(dev, &dev->channels[dev->channel]);
552
553         if (changed & NET80211_CFG_ASSOC) {
554                 for (i = 0; i < ETH_ALEN; i++)
555                         rtl818x_iowrite8(priv, &priv->map->BSSID[i], dev->bssid[i]);
556                 rtl818x_iowrite8(priv, &priv->map->MSR,
557                                  dev->state & NET80211_ASSOCIATED?
558                                         RTL818X_MSR_INFRA : RTL818X_MSR_NO_LINK);
559         }
560
561         if (changed & NET80211_CFG_PHY_PARAMS)
562                 priv->rf->conf_erp(dev);
563
564         if (changed & NET80211_CFG_RATE) {
565                 /* figure out the hardware rate number for the new
566                    logical rate */
567                 int hw_rate;
568                 for (hw_rate = 0; hw_rate < RTL818X_NR_RATES &&
569                              rtl818x_rates[hw_rate] != dev->rates[dev->rate];
570                      hw_rate++)
571                         ;
572                 if (hw_rate >= RTL818X_NR_RATES)
573                         return -EINVAL;
574
575                 priv->hw_rate = hw_rate;
576
577                 /* and the RTS/CTS rate */
578                 for (hw_rate = 0; hw_rate < RTL818X_NR_RATES &&
579                              rtl818x_rates[hw_rate] !=
580                                 dev->rates[dev->rtscts_rate];
581                      hw_rate++)
582                         ;
583                 if (hw_rate >= RTL818X_NR_RATES)
584                         hw_rate = priv->hw_rate;
585
586                 priv->hw_rtscts_rate = hw_rate;
587         }
588
589         return 0;
590 }
591
592 static const u8 rtl818x_eeprom_bits[] = {
593         [SPI_BIT_SCLK] = RTL818X_EEPROM_CMD_CK,
594         [SPI_BIT_MISO] = RTL818X_EEPROM_CMD_READ,
595         [SPI_BIT_MOSI] = RTL818X_EEPROM_CMD_WRITE,
596         [SPI_BIT_SS(0)] = RTL818X_EEPROM_CMD_CS,
597 };
598
599 static int rtl818x_spi_read_bit(struct bit_basher *basher, unsigned int bit_id)
600 {
601         struct rtl818x_priv *priv = container_of(basher, struct rtl818x_priv,
602                                                  spibit.basher);
603
604         u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
605         return reg & rtl818x_eeprom_bits[bit_id];
606 }
607
608 static void rtl818x_spi_write_bit(struct bit_basher *basher,
609                                   unsigned int bit_id, unsigned long data)
610 {
611         struct rtl818x_priv *priv = container_of(basher, struct rtl818x_priv,
612                                                  spibit.basher);
613
614         u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
615         u8 mask = rtl818x_eeprom_bits[bit_id];
616         reg = (reg & ~mask) | (data & mask);
617
618         rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
619
620         rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
621         udelay(10);
622 }
623
624 static struct bit_basher_operations rtl818x_basher_ops = {
625         .read = rtl818x_spi_read_bit,
626         .write = rtl818x_spi_write_bit,
627 };
628
629 /* The net80211 code makes a copy of this, so we're OK modifying the
630    static version as we initialize the card, as long as we don't
631    depend on possibly-modified values in case there are multiple cards. */
632 static struct net80211_hw_info rtl818x_hwinfo = {
633         /* MAC address filled in at runtime */
634         /* modes filled in at runtime */
635         .bands = NET80211_BAND_2GHZ,
636         .flags = NET80211_HW_RX_HAS_FCS,
637         .signal_type = NET80211_SIGNAL_ARBITRARY,
638         /* supported rates filled in at runtime */
639         .signal_max = 65,
640         .channel_change_time = 1000, /* no idea what the actual value is */
641 };
642
643 static const char *rtl818x_rf_names[] = {
644         NULL,                   /* no 0 */
645         "Intersil", "RFMD",     /* unsupported 1-2 */
646         "SA2400", "max2820", "GRF5101", /* supported 3-5 */
647         NULL, NULL, NULL,       /* no 6-8 */
648         "RTL8225",              /* supported 9 */
649         "RTL8255",              /* unsupported 10 */
650 };
651 #define RTL818X_NR_RF_NAMES 11
652
653 struct net80211_device_operations rtl818x_operations = {
654         .open = rtl818x_start,
655         .close = rtl818x_stop,
656         .transmit = rtl818x_tx,
657         .poll = rtl818x_poll,
658         .irq = rtl818x_irq,
659         .config = rtl818x_config,
660 };
661
662 static int rtl818x_probe(struct pci_device *pdev,
663                          const struct pci_device_id *id __unused)
664 {
665         struct net80211_device *dev;
666         struct rtl818x_priv *priv;
667         struct rtl818x_rf_ops *rf;
668         int err, i;
669         const char *chip_name;
670         u32 reg;
671         u16 eeprom_val;
672
673         adjust_pci_device(pdev);
674
675         dev = net80211_alloc(sizeof(*priv));
676         if (!dev) {
677                 DBG("rtl818x: net80211 alloc failed\n");
678                 return -ENOMEM;
679         }
680
681         priv = dev->priv;
682         priv->pdev = pdev;
683         dev->netdev->dev = &pdev->dev;
684
685         priv->map = (struct rtl818x_csr *)pdev->ioaddr;
686         if (!priv->map)
687                 priv->map = (struct rtl818x_csr *)pdev->membase;
688
689         if (!priv->map) {
690                 DBG("rtl818x: cannot find device memory\n");
691                 err = -ENXIO;
692                 goto err_free_dev;
693         }
694
695         reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
696         reg &= RTL818X_TX_CONF_HWVER_MASK;
697         switch (reg) {
698         case RTL818X_TX_CONF_R8180_ABCD:
699                 chip_name = "0";
700                 break;
701         case RTL818X_TX_CONF_R8180_F:
702                 chip_name = "0vF";
703                 break;
704         case RTL818X_TX_CONF_R8185_ABC:
705                 chip_name = "5";
706                 break;
707         case RTL818X_TX_CONF_R8185_D:
708                 chip_name = "5vD";
709                 break;
710         default:
711                 DBG("rtl818x: Unknown chip! (0x%x)\n", reg >> 25);
712                 err = -ENOSYS;
713                 goto err_free_dev;
714         }
715
716         priv->r8185 = reg & RTL818X_TX_CONF_R8185_ABC;
717
718         memcpy(rtl818x_hwinfo.supported_rates, rtl818x_rates,
719                sizeof(*rtl818x_rates) * RTL818X_NR_RATES);
720
721         if (priv->r8185) {
722                 rtl818x_hwinfo.modes = NET80211_MODE_B | NET80211_MODE_G;
723                 rtl818x_hwinfo.nr_supported_rates = RTL818X_NR_RATES;
724         } else {
725                 rtl818x_hwinfo.modes = NET80211_MODE_B;
726                 rtl818x_hwinfo.nr_supported_rates = RTL818X_NR_B_RATES;
727         }
728
729         priv->spibit.basher.op = &rtl818x_basher_ops;
730         priv->spibit.bus.mode = SPI_MODE_THREEWIRE;
731         init_spi_bit_basher(&priv->spibit);
732
733         DBG2("rtl818x RX_CONF: %08x\n", rtl818x_ioread32(priv, &priv->map->RX_CONF));
734
735         if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
736                 init_at93c66(&priv->eeprom, 16);
737         else
738                 init_at93c46(&priv->eeprom, 16);
739         priv->eeprom.bus = &priv->spibit.bus;
740
741         rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_PROGRAM);
742         rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
743         udelay(10);
744
745         nvs_read(&priv->eeprom.nvs, 0x06, &eeprom_val, 2);
746         DBG2("rtl818x eeprom val = %04x\n", eeprom_val);
747         eeprom_val &= 0xFF;
748
749         priv->rf = NULL;
750         for_each_table_entry(rf, RTL818X_RF_DRIVERS) {
751                 if (rf->id == eeprom_val) {
752                         priv->rf = rf;
753                         break;
754                 }
755         }
756
757         if (!priv->rf) {
758                 if (eeprom_val < RTL818X_NR_RF_NAMES &&
759                     rtl818x_rf_names[eeprom_val] != NULL)
760                         DBG("rtl818x: %s RF frontend not supported!\n",
761                             rtl818x_rf_names[eeprom_val]);
762                 else
763                         DBG("rtl818x: RF frontend #%d not recognized!\n",
764                             eeprom_val);
765
766                 err = -ENOSYS;
767                 goto err_free_dev;
768         }
769
770         nvs_read(&priv->eeprom.nvs, 0x17, &eeprom_val, 2);
771         priv->csthreshold = eeprom_val >> 8;
772         if (!priv->r8185) {
773                 nvs_read(&priv->eeprom.nvs, 0xD, &priv->anaparam, 4);
774                 nvs_read(&priv->eeprom.nvs, 0x19, &priv->rfparam, 2);
775                 priv->anaparam = le32_to_cpu(priv->anaparam);
776                 priv->rfparam = le16_to_cpu(priv->rfparam);
777         }
778
779         /* read the MAC address */
780         nvs_read(&priv->eeprom.nvs, 0x7, rtl818x_hwinfo.hwaddr, 6);
781
782         /* CCK TX power */
783         for (i = 0; i < 14; i += 2) {
784                 u16 txpwr;
785                 nvs_read(&priv->eeprom.nvs, 0x10 + (i >> 1), &txpwr, 2);
786                 priv->txpower[i] = txpwr & 0xFF;
787                 priv->txpower[i + 1] = txpwr >> 8;
788         }
789
790         /* OFDM TX power */
791         if (priv->r8185) {
792                 for (i = 0; i < 14; i += 2) {
793                         u16 txpwr;
794                         nvs_read(&priv->eeprom.nvs, 0x20 + (i >> 1), &txpwr, 2);
795                         priv->txpower[i] |= (txpwr & 0xFF) << 8;
796                         priv->txpower[i + 1] |= txpwr & 0xFF00;
797                 }
798         }
799
800         rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
801
802         err = net80211_register(dev, &rtl818x_operations, &rtl818x_hwinfo);
803         if (err) {
804                 DBG("rtl818x: cannot register device\n");
805                 goto err_free_dev;
806         }
807
808         DBG("rtl818x: Realtek RTL818%s (RF chip %s) with address %s\n",
809             chip_name, priv->rf->name, netdev_hwaddr(dev->netdev));
810
811         return 0;
812
813  err_free_dev:
814         pci_set_drvdata(pdev, NULL);
815         net80211_free(dev);
816         return err;
817 }
818
819 static void rtl818x_remove(struct pci_device *pdev)
820 {
821         struct net80211_device *dev = pci_get_drvdata(pdev);
822
823         if (!dev)
824                 return;
825
826         net80211_unregister(dev);
827         net80211_free(dev);
828 }
829
830 /* Hide PCI_ROM definitions in here from parserom.pl; the definitions
831    that should be used are in rtl8180.c and rtl8185.c. */
832 #define RTL_ROM PCI_ROM
833
834 static struct pci_device_id rtl818x_nics[] = {
835         RTL_ROM(0x10ec, 0x8185, "rtl8185", "Realtek 8185", 0),
836         RTL_ROM(0x1799, 0x700f, "f5d7000", "Belkin F5D7000", 0),
837         RTL_ROM(0x1799, 0x701f, "f5d7010", "Belkin F5D7010", 0),
838
839         RTL_ROM(0x10ec, 0x8180, "rtl8180", "Realtek 8180", 0),
840         RTL_ROM(0x1799, 0x6001, "f5d6001", "Belkin F5D6001", 0),
841         RTL_ROM(0x1799, 0x6020, "f5d6020", "Belkin F5D6020", 0),
842         RTL_ROM(0x1186, 0x3300, "dwl510",  "D-Link DWL-510", 0),
843 };
844
845 struct pci_driver rtl818x_driver __pci_driver = {
846         .ids            = rtl818x_nics,
847         .id_count       = sizeof(rtl818x_nics) / sizeof(rtl818x_nics[0]),
848         .probe          = rtl818x_probe,
849         .remove         = rtl818x_remove,
850 };