iPXE
rtl818x.c
Go to the documentation of this file.
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 iPXE, 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 <string.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <byteswap.h>
28 #include <ipxe/iobuf.h>
29 #include <ipxe/malloc.h>
30 #include <ipxe/pci.h>
31 #include <ipxe/net80211.h>
32 #include <ipxe/netdevice.h>
33 #include <ipxe/threewire.h>
34 
35 #include "rtl818x.h"
36 
37 /* rtl818x_rates[hw rate number] = rate in 100kbps units */
38 static const u16 rtl818x_rates[] = {
39  10, 20, 55, 110, /* 802.11b */
40  60, 90, 120, 180, 240, 360, 480, 540, /* 802.11g */
41  0, 0, 0, 0, /* index safely using a value masked with 0xF */
42 };
43 #define RTL818X_NR_B_RATES 4
44 #define RTL818X_NR_RATES 12
45 
46 /* used by RF drivers */
48 {
49  struct rtl818x_priv *priv = dev->priv;
50  int i = 10;
51  u32 buf;
52 
53  buf = (data << 8) | addr;
54 
55  rtl818x_iowrite32(priv, (u32 *)&priv->map->PHY[0], buf | 0x80);
56  while (i--) {
57  rtl818x_iowrite32(priv, (u32 *)&priv->map->PHY[0], buf);
58  if (rtl818x_ioread8(priv, &priv->map->PHY[2]) == (data & 0xFF))
59  return;
60  }
61 }
62 
63 static void rtl818x_handle_rx(struct net80211_device *dev)
64 {
65  struct rtl818x_priv *priv = dev->priv;
66  unsigned int count = RTL818X_RX_RING_SIZE;
67 
68  while (count--) {
69  struct rtl818x_rx_desc *entry = &priv->rx_ring[priv->rx_idx];
70  struct io_buffer *iob = priv->rx_buf[priv->rx_idx];
71  u32 flags = le32_to_cpu(entry->flags);
72 
74  return;
75 
79  /* This is crappy hardware. The Linux driver
80  doesn't even log these. */
81  goto done;
83  /* This is actually a corrupt packet. */
84  DBG2("rtl818x RX:%d CRC fail: flags %08x\n",
85  priv->rx_idx, flags);
86  net80211_rx_err(dev, NULL, EIO);
87  } else {
88  u32 flags2 = le32_to_cpu(entry->flags2);
89  struct io_buffer *new_iob = alloc_iob(MAX_RX_SIZE);
90  if (!new_iob) {
92  goto done;
93  }
94 
95  DBGP("rtl818x RX:%d success: flags %08x %08x\n",
96  priv->rx_idx, flags, flags2);
97 
98  iob_put(iob, flags & 0xFFF);
99 
100  net80211_rx(dev, iob, (flags2 >> 8) & 0x7f,
101  rtl818x_rates[(flags >> 20) & 0xf]);
102 
103  iob = new_iob;
104  priv->rx_buf[priv->rx_idx] = iob;
105  }
106 
107  done:
108  entry->rx_buf = cpu_to_le32(virt_to_bus(iob->data));
110 
111  if (priv->rx_idx == RTL818X_RX_RING_SIZE - 1)
113 
114  priv->rx_idx = (priv->rx_idx + 1) % RTL818X_RX_RING_SIZE;
115  }
116 }
117 
118 static void rtl818x_handle_tx(struct net80211_device *dev)
119 {
120  struct rtl818x_priv *priv = dev->priv;
121  unsigned int count = RTL818X_TX_RING_SIZE;
122 
123  while (count--) {
124  struct rtl818x_tx_desc *entry = &priv->tx_ring[priv->tx_cons];
125  struct io_buffer *iob = priv->tx_buf[priv->tx_cons];
126  u32 flags = le32_to_cpu(entry->flags);
127  int rc;
128 
129  if ((flags & RTL818X_TX_DESC_FLAG_OWN) || !iob)
130  return;
131 
132  rc = 0;
134  /* our packet was not ACKed properly */
135  rc = EIO;
136  }
137 
138  net80211_tx_complete(dev, iob, flags & 0xFF, rc);
139 
140  priv->tx_buf[priv->tx_cons] = NULL;
141  priv->tx_cons = (priv->tx_cons + 1) % RTL818X_TX_RING_SIZE;
142  }
143 }
144 
145 static void rtl818x_poll(struct net80211_device *dev)
146 {
147  struct rtl818x_priv *priv = dev->priv;
148  u16 reg = rtl818x_ioread16(priv, &priv->map->INT_STATUS);
149 
150  if (reg == 0xFFFF)
151  return;
152 
153  rtl818x_iowrite16(priv, &priv->map->INT_STATUS, reg);
154 
156  rtl818x_handle_tx(dev);
157 
159  rtl818x_handle_rx(dev);
160 }
161 
162 #define DIV_ROUND_UP(n,d) (((n)+(d)-1)/(d))
163 
164 static int rtl818x_tx(struct net80211_device *dev, struct io_buffer *iob)
165 {
166  struct rtl818x_priv *priv = dev->priv;
167  struct rtl818x_tx_desc *entry;
168  u32 tx_flags;
169  u16 plcp_len = 0;
170  int len = iob_len(iob);
171 
173  RTL818X_TX_DESC_FLAG_LS | (priv->hw_rate << 24) | len;
174 
175  if (priv->r8185) {
176  tx_flags |= RTL818X_TX_DESC_FLAG_DMA |
178  } else {
179  unsigned int remainder;
180 
181  plcp_len = DIV_ROUND_UP(16 * (len + 4),
182  (dev->rates[dev->rate] * 2) / 10);
183  remainder = (16 * (len + 4)) %
184  ((dev->rates[dev->rate] * 2) / 10);
185 
186  if (remainder > 0 && remainder <= 6)
187  plcp_len |= 1 << 15;
188  }
189 
190  entry = &priv->tx_ring[priv->tx_prod];
191 
193  tx_flags |= RTL818X_TX_DESC_FLAG_CTS;
194  tx_flags |= priv->hw_rtscts_rate << 19;
195  entry->rts_duration = net80211_cts_duration(dev, len);
196  } else {
197  entry->rts_duration = 0;
198  }
199 
200  if (entry->flags & RTL818X_TX_DESC_FLAG_OWN) {
201  /* card hasn't processed the old packet yet! */
202  return -EBUSY;
203  }
204 
205  priv->tx_buf[priv->tx_prod] = iob;
206  priv->tx_prod = (priv->tx_prod + 1) % RTL818X_TX_RING_SIZE;
207 
208  entry->plcp_len = cpu_to_le16(plcp_len);
209  entry->tx_buf = cpu_to_le32(virt_to_bus(iob->data));
210  entry->frame_len = cpu_to_le32(len);
211  entry->flags2 = /* alternate retry rate in 100kbps << 4 */ 0;
213  entry->flags = cpu_to_le32(tx_flags);
214 
215  rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING, (1 << 5));
216 
217  return 0;
218 }
219 
220 void rtl818x_set_anaparam(struct rtl818x_priv *priv, u32 anaparam)
221 {
222  u8 reg;
223 
225  reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
226  rtl818x_iowrite8(priv, &priv->map->CONFIG3,
228  rtl818x_iowrite32(priv, &priv->map->ANAPARAM, anaparam);
229  rtl818x_iowrite8(priv, &priv->map->CONFIG3,
232 }
233 
234 static int rtl818x_init_hw(struct net80211_device *dev)
235 {
236  struct rtl818x_priv *priv = dev->priv;
237  u16 reg;
238 
239  rtl818x_iowrite8(priv, &priv->map->CMD, 0);
240  rtl818x_ioread8(priv, &priv->map->CMD);
241  mdelay(10);
242 
243  /* reset */
244  rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
245  rtl818x_ioread8(priv, &priv->map->CMD);
246 
247  reg = rtl818x_ioread8(priv, &priv->map->CMD);
248  reg &= (1 << 1);
251  rtl818x_ioread8(priv, &priv->map->CMD);
252  mdelay(200);
253 
254  /* check success of reset */
255  if (rtl818x_ioread8(priv, &priv->map->CMD) & RTL818X_CMD_RESET) {
256  DBG("rtl818x %s: reset timeout!\n", dev->netdev->name);
257  return -ETIMEDOUT;
258  }
259 
260  rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
261  rtl818x_ioread8(priv, &priv->map->CMD);
262  mdelay(200);
263 
264  if (rtl818x_ioread8(priv, &priv->map->CONFIG3) & (1 << 3)) {
265  /* For cardbus */
266  reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
267  reg |= 1 << 1;
268  rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
269  reg = rtl818x_ioread16(priv, &priv->map->FEMR);
270  reg |= (1 << 15) | (1 << 14) | (1 << 4);
271  rtl818x_iowrite16(priv, &priv->map->FEMR, reg);
272  }
273 
274  rtl818x_iowrite8(priv, &priv->map->MSR, 0);
275 
276  if (!priv->r8185)
277  rtl818x_set_anaparam(priv, priv->anaparam);
278 
279  rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
280  rtl818x_iowrite32(priv, &priv->map->TNPDA, priv->tx_ring_dma);
281 
282  /* TODO: necessary? specs indicate not */
284  reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
285  rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
286  if (priv->r8185) {
287  reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
288  rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
289  }
291 
292  /* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */
293 
294  /* TODO: turn off hw wep on rtl8180 */
295 
296  rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
297 
298  if (priv->r8185) {
299  rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
300  rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
301  rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (8 << 4) | 0);
302 
303  rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
304 
305  /* TODO: set ClkRun enable? necessary? */
306  reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
307  rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, reg & ~(1 << 6));
309  reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
310  rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
312  } else {
313  rtl818x_iowrite16(priv, &priv->map->BRSR, 0x1);
314  rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);
315 
316  rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
317  rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, 0x4C);
318  }
319 
320  priv->rf->init(dev);
321  if (priv->r8185)
322  rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
323  return 0;
324 }
325 
326 static int rtl818x_init_rx_ring(struct net80211_device *dev)
327 {
328  struct rtl818x_priv *priv = dev->priv;
329  struct rtl818x_rx_desc *entry;
330  int i;
331 
332  priv->rx_ring = malloc_phys(sizeof(*priv->rx_ring) * RTL818X_RX_RING_SIZE,
334  priv->rx_ring_dma = virt_to_bus(priv->rx_ring);
335  if (!priv->rx_ring) {
336  DBG("rtl818x %s: cannot allocate RX ring\n", dev->netdev->name);
337  return -ENOMEM;
338  }
339 
340  memset(priv->rx_ring, 0, sizeof(*priv->rx_ring) * RTL818X_RX_RING_SIZE);
341  priv->rx_idx = 0;
342 
343  for (i = 0; i < RTL818X_RX_RING_SIZE; i++) {
344  struct io_buffer *iob = alloc_iob(MAX_RX_SIZE);
345  entry = &priv->rx_ring[i];
346  if (!iob)
347  return -ENOMEM;
348 
349  priv->rx_buf[i] = iob;
350  entry->rx_buf = cpu_to_le32(virt_to_bus(iob->data));
352  MAX_RX_SIZE);
353  }
355  return 0;
356 }
357 
358 static void rtl818x_free_rx_ring(struct net80211_device *dev)
359 {
360  struct rtl818x_priv *priv = dev->priv;
361  int i;
362 
363  for (i = 0; i < RTL818X_RX_RING_SIZE; i++) {
364  free_iob(priv->rx_buf[i]);
365  priv->rx_buf[i] = NULL;
366  }
367 
368  free_phys(priv->rx_ring, sizeof(*priv->rx_ring) * RTL818X_RX_RING_SIZE);
369  priv->rx_ring = NULL;
370 }
371 
372 static int rtl818x_init_tx_ring(struct net80211_device *dev)
373 {
374  struct rtl818x_priv *priv = dev->priv;
375  int i;
376 
377  priv->tx_ring = malloc_phys(sizeof(*priv->tx_ring) * RTL818X_TX_RING_SIZE,
379  priv->tx_ring_dma = virt_to_bus(priv->tx_ring);
380  if (!priv->tx_ring) {
381  DBG("rtl818x %s: cannot allocate TX ring\n", dev->netdev->name);
382  return -ENOMEM;
383  }
384 
385  memset(priv->tx_ring, 0, sizeof(*priv->tx_ring) * RTL818X_TX_RING_SIZE);
386  priv->tx_prod = priv->tx_cons = 0;
387 
388  for (i = 0; i < RTL818X_TX_RING_SIZE; i++)
389  priv->tx_ring[i].next_tx_desc = cpu_to_le32(priv->tx_ring_dma +
390  ((i + 1) % RTL818X_TX_RING_SIZE) * sizeof(*priv->tx_ring));
391 
392  return 0;
393 }
394 
395 static void rtl818x_free_tx_ring(struct net80211_device *dev)
396 {
397  struct rtl818x_priv *priv = dev->priv;
398  int i;
399 
400  for (i = 0; i < RTL818X_TX_RING_SIZE; i++) {
401  if (priv->tx_buf[i])
402  net80211_tx_complete(dev, priv->tx_buf[i], 0, ECANCELED);
403  priv->tx_buf[i] = NULL;
404  }
405 
406  free_phys(priv->tx_ring, sizeof(*priv->tx_ring) * RTL818X_TX_RING_SIZE);
407  priv->tx_ring = NULL;
408 }
409 
410 static void rtl818x_irq(struct net80211_device *dev, int enable)
411 {
412  struct rtl818x_priv *priv = dev->priv;
413  rtl818x_iowrite16(priv, &priv->map->INT_MASK, enable? 0xFFFF : 0);
414 }
415 
416 /* Sets the MAC address of the card. */
417 static void rtl818x_set_hwaddr(struct net80211_device *dev, u8 *hwaddr)
418 {
419  struct rtl818x_priv *priv = dev->priv;
421  rtl818x_iowrite32(priv, (u32 *)&priv->map->MAC[0],
422  le32_to_cpu(*(u32 *)hwaddr));
423  rtl818x_iowrite16(priv, (u16 *)&priv->map->MAC[4],
424  le16_to_cpu(*(u16 *)(hwaddr + 4)));
426 }
427 
428 static int rtl818x_start(struct net80211_device *dev)
429 {
430  struct rtl818x_priv *priv = dev->priv;
431  int ret;
432  u32 reg;
433 
434  ret = rtl818x_init_rx_ring(dev);
435  if (ret)
436  return ret;
437 
438  ret = rtl818x_init_tx_ring(dev);
439  if (ret)
440  goto err_free_rings;
441 
442  ret = rtl818x_init_hw(dev);
443  if (ret)
444  goto err_free_rings;
445 
446  rtl818x_set_hwaddr(dev, dev->netdev->ll_addr);
447 
448  rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
449  rtl818x_iowrite32(priv, &priv->map->TNPDA, priv->tx_ring_dma);
450 
451  rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
452 
453  rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
454  rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
455 
460  (7 << 8 /* MAX RX DMA */) |
463 
464  if (priv->r8185)
466  else {
467  reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
468  ? RTL818X_RX_CONF_CSDM1 : 0;
469  reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
470  ? RTL818X_RX_CONF_CSDM2 : 0;
471  }
472 
473  priv->rx_conf = reg;
474  rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
475 
476  if (priv->r8185) {
477  reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
480  rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
481 
482  reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
486  rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
487 
488  /* disable early TX */
489  rtl818x_iowrite8(priv, (u8 *)priv->map + 0xec, 0x3f);
490  }
491 
492  reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
493  reg |= (6 << 21 /* MAX TX DMA */) |
495 
496  if (priv->r8185)
498  else
500 
501  /* different meaning, same value on both rtl8185 and rtl8180 */
503 
504  rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
505 
506  reg = rtl818x_ioread8(priv, &priv->map->CMD);
509  rtl818x_iowrite8(priv, &priv->map->CMD, reg);
510 
511  DBG("%s rtl818x: started\n", dev->netdev->name);
512 
513  return 0;
514 
515  err_free_rings:
517  if (priv->tx_ring)
519 
520  DBG("%s rtl818x: failed to start\n", dev->netdev->name);
521 
522  return ret;
523 }
524 
525 static void rtl818x_stop(struct net80211_device *dev)
526 {
527  struct rtl818x_priv *priv = dev->priv;
528  u8 reg;
529 
530  rtl818x_irq(dev, 0);
531 
532  reg = rtl818x_ioread8(priv, &priv->map->CMD);
535  rtl818x_iowrite8(priv, &priv->map->CMD, reg);
536 
537  priv->rf->stop(dev);
538 
540  reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
543 
546 }
547 
548 static int rtl818x_config(struct net80211_device *dev, int changed)
549 {
550  struct rtl818x_priv *priv = dev->priv;
551  int i;
552 
553  if (changed & NET80211_CFG_CHANNEL)
554  priv->rf->set_chan(dev, &dev->channels[dev->channel]);
555 
556  if (changed & NET80211_CFG_ASSOC) {
557  for (i = 0; i < ETH_ALEN; i++)
558  rtl818x_iowrite8(priv, &priv->map->BSSID[i], dev->bssid[i]);
559  rtl818x_iowrite8(priv, &priv->map->MSR,
560  dev->state & NET80211_ASSOCIATED?
562  }
563 
564  if (changed & NET80211_CFG_PHY_PARAMS)
565  priv->rf->conf_erp(dev);
566 
567  if (changed & NET80211_CFG_RATE) {
568  /* figure out the hardware rate number for the new
569  logical rate */
570  int hw_rate;
571  for (hw_rate = 0; hw_rate < RTL818X_NR_RATES &&
572  rtl818x_rates[hw_rate] != dev->rates[dev->rate];
573  hw_rate++)
574  ;
575  if (hw_rate >= RTL818X_NR_RATES)
576  return -EINVAL;
577 
578  priv->hw_rate = hw_rate;
579 
580  /* and the RTS/CTS rate */
581  for (hw_rate = 0; hw_rate < RTL818X_NR_RATES &&
583  dev->rates[dev->rtscts_rate];
584  hw_rate++)
585  ;
586  if (hw_rate >= RTL818X_NR_RATES)
587  hw_rate = priv->hw_rate;
588 
589  priv->hw_rtscts_rate = hw_rate;
590  }
591 
592  return 0;
593 }
594 
595 static const u8 rtl818x_eeprom_bits[] = {
600 };
601 
602 static int rtl818x_spi_read_bit(struct bit_basher *basher, unsigned int bit_id)
603 {
604  struct rtl818x_priv *priv = container_of(basher, struct rtl818x_priv,
605  spibit.basher);
606 
607  u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
608  return reg & rtl818x_eeprom_bits[bit_id];
609 }
610 
611 static void rtl818x_spi_write_bit(struct bit_basher *basher,
612  unsigned int bit_id, unsigned long data)
613 {
614  struct rtl818x_priv *priv = container_of(basher, struct rtl818x_priv,
615  spibit.basher);
616 
617  u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
618  u8 mask = rtl818x_eeprom_bits[bit_id];
619  reg = (reg & ~mask) | (data & mask);
620 
621  rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
622 
623  rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
624  udelay(10);
625 }
626 
629  .write = rtl818x_spi_write_bit,
630 };
631 
632 #if DBGLVL_MAX
633 static const char *rtl818x_rf_names[] = {
634  NULL, /* no 0 */
635  "Intersil", "RFMD", /* unsupported 1-2 */
636  "SA2400", "max2820", "GRF5101", /* supported 3-5 */
637  NULL, NULL, NULL, /* no 6-8 */
638  "RTL8225", /* supported 9 */
639  "RTL8255", /* unsupported 10 */
640 };
641 #define RTL818X_NR_RF_NAMES 11
642 #endif
643 
645  .open = rtl818x_start,
646  .close = rtl818x_stop,
647  .transmit = rtl818x_tx,
648  .poll = rtl818x_poll,
649  .irq = rtl818x_irq,
650  .config = rtl818x_config,
651 };
652 
653 int rtl818x_probe(struct pci_device *pdev )
654 {
655  struct net80211_device *dev;
656  struct rtl818x_priv *priv;
657  struct rtl818x_rf_ops *rf;
658  int err, i;
659  const char *chip_name;
660  u32 reg;
661  u16 eeprom_val;
662  struct net80211_hw_info *hwinfo;
663 
664  hwinfo = zalloc(sizeof(*hwinfo));
665  if (!hwinfo) {
666  DBG("rtl818x: hwinfo alloc failed\n");
667  err = -ENOMEM;
668  goto err_alloc_hwinfo;
669  }
670 
671  adjust_pci_device(pdev);
672 
673  dev = net80211_alloc(sizeof(*priv));
674  if (!dev) {
675  DBG("rtl818x: net80211 alloc failed\n");
676  err = -ENOMEM;
677  goto err_alloc_dev;
678  }
679 
680  priv = dev->priv;
681  priv->pdev = pdev;
682  dev->netdev->dev = &pdev->dev;
683 
684  priv->map = (struct rtl818x_csr *)pdev->ioaddr;
685  if (!priv->map) {
686  DBG("rtl818x: cannot find device memory\n");
687  err = -ENXIO;
688  goto err_free_dev;
689  }
690 
691  reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
693  switch (reg) {
695  chip_name = "0";
696  break;
698  chip_name = "0vF";
699  break;
701  chip_name = "5";
702  break;
704  chip_name = "5vD";
705  break;
706  default:
707  DBG("rtl818x: Unknown chip! (0x%x)\n", reg >> 25);
708  err = -ENOSYS;
709  goto err_free_dev;
710  }
711 
713 
714  hwinfo->bands = NET80211_BAND_BIT_2GHZ;
715  hwinfo->flags = NET80211_HW_RX_HAS_FCS;
716  hwinfo->signal_type = NET80211_SIGNAL_ARBITRARY;
717  hwinfo->signal_max = 65;
718  hwinfo->channel_change_time = 1000;
719 
721  sizeof(*rtl818x_rates) * RTL818X_NR_RATES);
722 
723  if (priv->r8185) {
726  } else {
727  hwinfo->modes = NET80211_MODE_B;
729  }
730 
731  priv->spibit.basher.op = &rtl818x_basher_ops;
732  priv->spibit.bus.mode = SPI_MODE_THREEWIRE;
733  init_spi_bit_basher(&priv->spibit);
734 
735  DBG2("rtl818x RX_CONF: %08x\n", rtl818x_ioread32(priv, &priv->map->RX_CONF));
736 
737  if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
738  init_at93c66(&priv->eeprom, 16);
739  else
740  init_at93c46(&priv->eeprom, 16);
741  priv->eeprom.bus = &priv->spibit.bus;
742 
744  rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
745  udelay(10);
746 
747  nvs_read(&priv->eeprom.nvs, 0x06, &eeprom_val, 2);
748  DBG2("rtl818x eeprom val = %04x\n", eeprom_val);
749  eeprom_val &= 0xFF;
750 
751  priv->rf = NULL;
753  if (rf->id == eeprom_val) {
754  priv->rf = rf;
755  break;
756  }
757  }
758 
759  if (!priv->rf) {
760 #if DBGLVL_MAX
761  if (eeprom_val < RTL818X_NR_RF_NAMES &&
762  rtl818x_rf_names[eeprom_val] != NULL)
763  DBG("rtl818x: %s RF frontend not supported!\n",
764  rtl818x_rf_names[eeprom_val]);
765  else
766  DBG("rtl818x: RF frontend #%d not recognized!\n",
767  eeprom_val);
768 #endif
769 
770  err = -ENOSYS;
771  goto err_free_dev;
772  }
773 
774  nvs_read(&priv->eeprom.nvs, 0x17, &eeprom_val, 2);
775  priv->csthreshold = eeprom_val >> 8;
776  if (!priv->r8185) {
777  nvs_read(&priv->eeprom.nvs, 0xD, &priv->anaparam, 4);
778  nvs_read(&priv->eeprom.nvs, 0x19, &priv->rfparam, 2);
779  priv->anaparam = le32_to_cpu(priv->anaparam);
780  priv->rfparam = le16_to_cpu(priv->rfparam);
781  }
782 
783  /* read the MAC address */
784  nvs_read(&priv->eeprom.nvs, 0x7, hwinfo->hwaddr, 6);
785 
786  /* CCK TX power */
787  for (i = 0; i < 14; i += 2) {
788  u16 txpwr;
789  nvs_read(&priv->eeprom.nvs, 0x10 + (i >> 1), &txpwr, 2);
790  priv->txpower[i] = txpwr & 0xFF;
791  priv->txpower[i + 1] = txpwr >> 8;
792  }
793 
794  /* OFDM TX power */
795  if (priv->r8185) {
796  for (i = 0; i < 14; i += 2) {
797  u16 txpwr;
798  nvs_read(&priv->eeprom.nvs, 0x20 + (i >> 1), &txpwr, 2);
799  priv->txpower[i] |= (txpwr & 0xFF) << 8;
800  priv->txpower[i + 1] |= txpwr & 0xFF00;
801  }
802  }
803 
805 
806  err = net80211_register(dev, &rtl818x_operations, hwinfo);
807  if (err) {
808  DBG("rtl818x: cannot register device\n");
809  goto err_free_dev;
810  }
811 
812  free(hwinfo);
813 
814  DBG("rtl818x: Realtek RTL818%s (RF chip %s) with address %s\n",
815  chip_name, priv->rf->name, netdev_addr(dev->netdev));
816 
817  return 0;
818 
819  err_free_dev:
820  pci_set_drvdata(pdev, NULL);
821  net80211_free(dev);
822  err_alloc_dev:
823  free(hwinfo);
824  err_alloc_hwinfo:
825  return err;
826 }
827 
828 void rtl818x_remove(struct pci_device *pdev)
829 {
830  struct net80211_device *dev = pci_get_drvdata(pdev);
831 
832  if (!dev)
833  return;
834 
835  net80211_unregister(dev);
836  net80211_free(dev);
837 }
uint16_t u16
Definition: stdint.h:21
static int rtl818x_init_tx_ring(struct net80211_device *dev)
Definition: rtl818x.c:372
int hw_rate
Definition: rtl818x.h:284
#define RTL818X_RX_CONF_CSDM1
Definition: rtl818x.h:97
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct bit_basher basher
Bit-bashing interface.
Definition: spi_bit.h:20
#define RTL818X_CONFIG3_ANAPARAM_WRITE
Definition: rtl818x.h:123
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int modes
A bitwise OR of the 802.11x modes supported by this device.
Definition: net80211.h:450
void net80211_free(struct net80211_device *dev)
Free 802.11 device.
Definition: net80211.c:838
#define RTL818X_RX_CONF_RX_AUTORESETPHY
Definition: rtl818x.h:96
static int rtl818x_spi_read_bit(struct bit_basher *basher, unsigned int bit_id)
Definition: rtl818x.c:602
#define iob_put(iobuf, len)
Definition: iobuf.h:124
static const u8 rtl818x_eeprom_bits[]
Definition: rtl818x.c:595
#define RTL818X_INT_RX_OK
Definition: rtl818x.h:51
#define RTL818X_MSR_INFRA
Definition: rtl818x.h:119
#define EBUSY
Device or resource busy.
Definition: errno.h:338
static unsigned int unsigned int reg
Definition: myson.h:162
static void rtl818x_handle_tx(struct net80211_device *dev)
Definition: rtl818x.c:118
void rtl818x_write_phy(struct net80211_device *dev, u8 addr, u32 data)
Definition: rtl818x.c:47
static u8 rtl818x_ioread8(struct rtl818x_priv *priv __unused, u8 *addr)
Definition: rtl818x.h:315
u8 channel
The channel currently in use, as an index into the channels array.
Definition: net80211.h:812
#define le32_to_cpu(value)
Definition: byteswap.h:113
#define RTL818X_TX_CONF_R8185_D
Definition: rtl818x.h:76
#define RTL818X_TX_AGC_CTL_FEEDBACK_ANT
Definition: rtl818x.h:156
unsigned long ioaddr
I/O address.
Definition: pci.h:225
Error codes.
Bit-bashing operations.
Definition: bitbash.h:15
enum net80211_hw_info::@641 flags
A set of flags indicating peculiarities of this device.
#define RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT
Definition: rtl818x.h:155
static void rtl818x_iowrite8(struct rtl818x_priv *priv __unused, u8 *addr, u8 val)
Definition: rtl818x.h:330
#define RTL818X_EEPROM_CMD_CONFIG
Definition: rtl818x.h:110
void net80211_rx_err(struct net80211_device *dev, struct io_buffer *iob, int rc)
Indicate an error in receiving a packet.
Definition: net80211.c:2788
I/O buffers.
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:152
Master In Slave Out.
Definition: spi_bit.h:39
void net80211_rx(struct net80211_device *dev, struct io_buffer *iob, int signal, u16 rate)
Handle receipt of 802.11 frame.
Definition: net80211.c:2689
u8 rtscts_rate
The rate to use for RTS/CTS transmissions.
Definition: net80211.h:831
#define RTL818X_RX_CONF_CSDM2
Definition: rtl818x.h:98
static void rtl818x_handle_rx(struct net80211_device *dev)
Definition: rtl818x.c:63
void net80211_unregister(struct net80211_device *dev)
Unregister 802.11 device from network stack.
Definition: net80211.c:824
#define RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT
Definition: rtl818x.h:169
static const u16 rtl818x_rates[]
Definition: rtl818x.c:38
#define RTL818X_TX_CONF_R8185_ABC
Definition: rtl818x.h:75
static struct bit_basher_operations rtl818x_basher_ops
Definition: rtl818x.c:627
unsigned signal_max
Maximum signal in arbitrary cases.
Definition: net80211.h:495
#define RTL818X_TX_CONF_R8180_ABCD
Definition: rtl818x.h:73
#define RTL818X_RF_DRIVERS
Definition: rtl818x.h:348
#define RTL818X_CMD_RESET
Definition: rtl818x.h:47
void adjust_pci_device(struct pci_device *pci)
Enable PCI device.
Definition: pci.c:240
struct io_buffer * alloc_iob(size_t len)
Allocate I/O buffer.
Definition: iobuf.c:130
static const char * netdev_addr(struct net_device *netdev)
Get printable network device link-layer address.
Definition: netdevice.h:541
struct device dev
Generic device.
Definition: pci.h:212
#define RTL818X_CMD_TX_ENABLE
Definition: rtl818x.h:45
#define RTL818X_RX_CONF_BROADCAST
Definition: rtl818x.h:88
#define ECANCELED
Operation canceled.
Definition: errno.h:343
int(* open)(struct net80211_device *dev)
Open 802.11 device.
Definition: net80211.h:305
struct net80211_device_operations rtl818x_operations
Definition: rtl818x.c:644
Dynamic memory allocation.
static void rtl818x_iowrite32(struct rtl818x_priv *priv __unused, u32 *addr, u32 val)
Definition: rtl818x.h:342
#define RTL818X_EEPROM_CMD_WRITE
Definition: rtl818x.h:104
#define NET80211_BAND_2GHZ
The 2.4 GHz ISM band, unlicensed in most countries.
Definition: net80211.h:45
u16 rates[NET80211_NR_BANDS][NET80211_MAX_RATES]
List of transmission rates supported by the card, indexed by band.
Definition: net80211.h:508
static void rtl818x_poll(struct net80211_device *dev)
Definition: rtl818x.c:145
static void pci_set_drvdata(struct pci_device *pci, void *priv)
Set PCI driver-private data.
Definition: pci.h:365
Operations that must be implemented by an 802.11 driver.
Definition: net80211.h:293
#define ENOMEM
Not enough space.
Definition: errno.h:534
#define RTL818X_CW_CONF_PERPACKET_CW_SHIFT
Definition: rtl818x.h:168
#define RTL818X_NR_B_RATES
Definition: rtl818x.c:43
void * memcpy(void *dest, const void *src, size_t len) __nonnull
Three-wire serial interface.
enum net80211_hw_info::@642 signal_type
Signal strength information that can be provided by the device.
static __always_inline unsigned long virt_to_bus(volatile const void *addr)
Convert virtual address to a bus address.
Definition: io.h:183
static int rtl818x_tx(struct net80211_device *dev, struct io_buffer *iob)
Definition: rtl818x.c:164
#define DBGP(...)
Definition: compiler.h:532
Information on the capabilities of an 802.11 hardware device.
Definition: net80211.h:436
u8 hwaddr[ETH_ALEN]
Default hardware MAC address.
Definition: net80211.h:447
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
FILE_LICENCE(GPL2_ONLY)
#define RTL818X_TX_CONF_HW_SEQNUM
Definition: rtl818x.h:82
#define RTL818X_TX_CONF_SAT_HWPLCP
Definition: rtl818x.h:72
void init_spi_bit_basher(struct spi_bit_basher *spibit)
Initialise SPI bit-bashing interface.
Definition: spi_bit.c:235
int nr_rates[NET80211_NR_BANDS]
Number of supported rates, indexed by band.
Definition: net80211.h:511
ring len
Length.
Definition: dwmac.h:231
void udelay(unsigned long usecs)
Delay for a fixed number of microseconds.
Definition: timer.c:60
#define RTL818X_TX_CONF_R8180_F
Definition: rtl818x.h:74
static unsigned int count
Number of entries.
Definition: dwmac.h:225
static void rtl818x_spi_write_bit(struct bit_basher *basher, unsigned int bit_id, unsigned long data)
Definition: rtl818x.c:611
#define RTL818X_EEPROM_CMD_PROGRAM
Definition: rtl818x.h:109
static void rtl818x_free_rx_ring(struct net80211_device *dev)
Definition: rtl818x.c:358
#define RTL818X_RX_CONF_NICMAC
Definition: rtl818x.h:86
#define cpu_to_le32(value)
Definition: byteswap.h:107
void * priv
Driver private data.
Definition: net80211.h:798
static int rtl818x_init_rx_ring(struct net80211_device *dev)
Definition: rtl818x.c:326
#define RTL818X_INT_TXN_OK
Definition: rtl818x.h:57
int(* read)(struct bit_basher *basher, unsigned int bit_id)
Read input bit.
Definition: bitbash.h:51
#define DIV_ROUND_UP(n, d)
Definition: rtl818x.c:162
A bit-bashing interface.
Definition: bitbash.h:55
#define NET80211_PHY_USE_PROTECTION
Whether to use RTS/CTS or CTS-to-self protection for transmissions.
Definition: net80211.h:251
uint8_t flags
Flags.
Definition: ena.h:18
struct net80211_device * net80211_alloc(size_t priv_size)
Allocate 802.11 device.
Definition: net80211.c:754
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:661
The iPXE 802.11 MAC layer.
static void rtl818x_stop(struct net80211_device *dev)
Definition: rtl818x.c:525
#define RTL818X_TX_CONF_HWVER_MASK
Definition: rtl818x.h:79
PCI bus.
#define NET80211_CFG_PHY_PARAMS
Low-level link parameters (short preamble, protection, etc) have changed.
Definition: net80211.h:90
A PCI device.
Definition: pci.h:210
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:159
#define NET80211_ASSOCIATED
Whether we have successfully associated with the network.
Definition: net80211.h:201
Structure encapsulating the complete state of an 802.11 device.
Definition: net80211.h:786
uint32_t addr
Buffer address.
Definition: dwmac.h:20
#define RTL818X_EEPROM_CMD_CK
Definition: rtl818x.h:105
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:385
#define RTL818X_EEPROM_CMD_CS
Definition: rtl818x.h:106
#define SPI_BIT_SS(slave)
Determine bit index for a particular slave.
Definition: spi_bit.h:50
#define RTL818X_TX_CONF_PROBE_DTS
Definition: rtl818x.h:81
static int rtl818x_start(struct net80211_device *dev)
Definition: rtl818x.c:428
struct spi_bit_basher spibit
Definition: rtl818x.h:287
Master Out Slave In.
Definition: spi_bit.h:37
#define NET80211_MODE_B
802.11b: 1-11 Mbps operation using DSSS/CCK signaling on the 2.4GHz band
Definition: net80211.h:66
#define ETH_ALEN
Definition: if_ether.h:8
void net80211_tx_complete(struct net80211_device *dev, struct io_buffer *iob, int retries, int rc)
Indicate the completed transmission of a packet.
Definition: net80211.c:2808
#define le16_to_cpu(value)
Definition: byteswap.h:112
u16 rates[NET80211_MAX_RATES]
A list of all possible TX rates we might use.
Definition: net80211.h:818
Serial clock.
Definition: spi_bit.h:35
#define RTL818X_MAX_RETRIES
Definition: rtl818x.h:222
#define RTL818X_INT_RX_ERR
Definition: rtl818x.h:52
static int rtl818x_init_hw(struct net80211_device *dev)
Definition: rtl818x.c:234
int phy_flags
Physical layer options.
Definition: net80211.h:983
struct device * dev
Underlying hardware device.
Definition: netdevice.h:364
#define RTL818X_RX_CONF_DATA
Definition: rtl818x.h:90
Network device management.
#define RTL818X_RX_CONF_ONLYERLPKT
Definition: rtl818x.h:99
static void * pci_get_drvdata(struct pci_device *pci)
Get PCI driver-private data.
Definition: pci.h:375
struct net_device * netdev
The net_device that wraps us.
Definition: net80211.h:789
#define RTL818X_RX_RING_SIZE
Definition: rtl818x.h:218
static void rtl818x_irq(struct net80211_device *dev, int enable)
Definition: rtl818x.c:410
void mdelay(unsigned long msecs)
Delay for a fixed number of milliseconds.
Definition: timer.c:78
#define RF_PARAM_CARRIERSENSE1
Definition: rtl818x.h:201
#define RTL818X_INT_TXN_ERR
Definition: rtl818x.h:58
#define ENXIO
No such device or address.
Definition: errno.h:599
char name[NETDEV_NAME_LEN]
Name of this network device.
Definition: netdevice.h:362
static struct tlan_private * priv
Definition: tlan.c:225
#define MAX_RX_SIZE
Definition: rtl818x.h:197
#define RTL818X_EEPROM_CMD_NORMAL
Definition: rtl818x.h:107
#define NET80211_BAND_BIT_2GHZ
Bitmask for the 2GHz band.
Definition: net80211.h:52
#define RTL818X_TX_CONF_NO_ICV
Definition: rtl818x.h:70
static void rtl818x_free_tx_ring(struct net80211_device *dev)
Definition: rtl818x.c:395
void * data
Start of data.
Definition: iobuf.h:52
#define EIO
Input/output error.
Definition: errno.h:433
#define RTL818X_EEPROM_CMD_READ
Definition: rtl818x.h:103
#define RTL818X_CONFIG4_VCOOFF
Definition: rtl818x.h:127
struct net80211_channel channels[NET80211_MAX_CHANNELS]
A list of all possible channels we might use.
Definition: net80211.h:806
void free_phys(void *ptr, size_t size)
Free memory allocated with malloc_phys()
Definition: malloc.c:722
void rtl818x_remove(struct pci_device *pdev)
Definition: rtl818x.c:828
static void rtl818x_set_hwaddr(struct net80211_device *dev, u8 *hwaddr)
Definition: rtl818x.c:417
#define cpu_to_le16(value)
Definition: byteswap.h:106
#define RF_PARAM_CARRIERSENSE2
Definition: rtl818x.h:202
uint8_t data[48]
Additional event data.
Definition: ena.h:22
#define RTL818X_RX_CONF_MGMT
Definition: rtl818x.h:92
#define ENOSYS
Function not implemented.
Definition: errno.h:564
#define NET80211_CFG_RATE
Requested transmission rate (dev->rate) has changed.
Definition: net80211.h:84
static void rtl818x_iowrite16(struct rtl818x_priv *priv __unused, u16 *addr, u16 val)
Definition: rtl818x.h:336
int nvs_read(struct nvs_device *nvs, unsigned int address, void *data, size_t len)
Read from non-volatile storage device.
Definition: nvs.c:75
static u16 net80211_cts_duration(struct net80211_device *dev, int size)
Calculate duration field for a CTS control frame.
Definition: net80211.h:1179
#define RTL818X_TX_RING_SIZE
Definition: rtl818x.h:219
#define NET80211_CFG_CHANNEL
Channel choice (dev->channel) or regulatory parameters have changed.
Definition: net80211.h:81
int bands
A bitwise OR of the bands on which this device can communicate.
Definition: net80211.h:453
u8 bssid[ETH_ALEN]
MAC address of the access point most recently associated.
Definition: net80211.h:954
#define SPI_MODE_THREEWIRE
Threewire-compatible mode.
Definition: spi.h:199
#define RTL818X_CMD_RX_ENABLE
Definition: rtl818x.h:46
u16 state
State of our association to the network.
Definition: net80211.h:921
uint8_t ll_addr[MAX_LL_ADDR_LEN]
Link-layer address.
Definition: netdevice.h:387
static int rtl818x_config(struct net80211_device *dev, int changed)
Definition: rtl818x.c:548
#define RTL818X_EEPROM_CMD_LOAD
Definition: rtl818x.h:108
static u16 rtl818x_ioread16(struct rtl818x_priv *priv __unused, u16 *addr)
Definition: rtl818x.h:320
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
void rtl818x_set_anaparam(struct rtl818x_priv *priv, u32 anaparam)
Definition: rtl818x.c:220
#define RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT
Definition: rtl818x.h:154
#define RTL818X_MSR_NO_LINK
Definition: rtl818x.h:117
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
unsigned channel_change_time
Estimate of the time required to change channels, in microseconds.
Definition: net80211.h:518
#define ETIMEDOUT
Connection timed out.
Definition: errno.h:669
#define NET80211_CFG_ASSOC
Association has been established with a new BSS (dev->bssid)
Definition: net80211.h:87
String functions.
u8 rate
The rate currently in use, as an index into the rates array.
Definition: net80211.h:824
struct bofm_section_header done
Definition: bofm_test.c:46
#define NET80211_MODE_G
802.11g: 54 Mbps operation using ERP/OFDM signaling on the 2.4GHz band
Definition: net80211.h:69
uint8_t u8
Definition: stdint.h:19
#define RTL818X_NR_RATES
Definition: rtl818x.c:44
uint32_t u32
Definition: stdint.h:23
int rtl818x_probe(struct pci_device *pdev)
Definition: rtl818x.c:653
static u32 rtl818x_ioread32(struct rtl818x_priv *priv __unused, u32 *addr)
Definition: rtl818x.h:325
int net80211_register(struct net80211_device *dev, struct net80211_device_operations *ops, struct net80211_hw_info *hw)
Register 802.11 device with network stack.
Definition: net80211.c:791
void * malloc_phys(size_t size, size_t phys_align)
Allocate memory with specified physical alignment.
Definition: malloc.c:706
#define DBG2(...)
Definition: compiler.h:515
if(natsemi->flags &NATSEMI_64BIT) return 1
u16 rts_duration
Definition: rtl818x.h:240
#define RTL818X_RING_ALIGN
Definition: rtl818x.h:220
void * memset(void *dest, int character, size_t len) __nonnull
A persistent I/O buffer.
Definition: iobuf.h:37