blob: 73baf268fe67c4630d0c012512ca6183bc7d7a66 [file] [log] [blame]
Michael Wuf6532112007-10-14 14:43:16 -04001
Andrea Merello1eba6482014-03-26 21:04:23 +01002/* Linux device driver for RTL8180 / RTL8185 / RTL8187SE
Michael Wuf6532112007-10-14 14:43:16 -04003 *
4 * Copyright 2007 Michael Wu <flamingice@sourmilk.net>
Andrea Merello1eba6482014-03-26 21:04:23 +01005 * Copyright 2007,2014 Andrea Merello <andrea.merello@gmail.com>
Michael Wuf6532112007-10-14 14:43:16 -04006 *
7 * Based on the r8180 driver, which is:
Andrea Merello93ba2a82013-08-26 13:53:30 +02008 * Copyright 2004-2005 Andrea Merello <andrea.merello@gmail.com>, et al.
Michael Wuf6532112007-10-14 14:43:16 -04009 *
10 * Thanks to Realtek for their support!
11 *
Andrea Merello1eba6482014-03-26 21:04:23 +010012 ************************************************************************
13 *
14 * The driver was extended to the RTL8187SE in 2014 by
15 * Andrea Merello <andrea.merello@gmail.com>
16 *
17 * based also on:
18 * - portions of rtl8187se Linux staging driver, Copyright Realtek corp.
19 * - other GPL, unpublished (until now), Linux driver code,
20 * Copyright Larry Finger <Larry.Finger@lwfinger.net>
21 *
22 * A huge thanks goes to Sara V. Nari who forgives me when I'm
23 * sitting in front of my laptop at evening, week-end, night...
24 *
25 * A special thanks goes to Antonio Cuni, who helped me with
26 * some python userspace stuff I used to debug RTL8187SE code, and who
27 * bought a laptop with an unsupported Wi-Fi card some years ago...
28 *
29 * Thanks to Larry Finger for writing some code for rtl8187se and for
30 * his suggestions.
31 *
32 * Thanks to Dan Carpenter for reviewing my initial patch and for his
33 * suggestions.
34 *
35 * Thanks to Bernhard Schiffner for his help in testing and for his
36 * suggestions.
37 *
38 ************************************************************************
39 *
Michael Wuf6532112007-10-14 14:43:16 -040040 * This program is free software; you can redistribute it and/or modify
41 * it under the terms of the GNU General Public License version 2 as
42 * published by the Free Software Foundation.
43 */
44
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000045#include <linux/interrupt.h>
Michael Wuf6532112007-10-14 14:43:16 -040046#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Michael Wuf6532112007-10-14 14:43:16 -040048#include <linux/delay.h>
49#include <linux/etherdevice.h>
50#include <linux/eeprom_93cx6.h>
Paul Gortmaker9d9779e2011-07-03 15:21:01 -040051#include <linux/module.h>
Michael Wuf6532112007-10-14 14:43:16 -040052#include <net/mac80211.h>
53
54#include "rtl8180.h"
John W. Linville3cfeb0c2010-12-20 15:16:53 -050055#include "rtl8225.h"
56#include "sa2400.h"
57#include "max2820.h"
58#include "grf5101.h"
Andrea Merello711d4ed32014-03-26 21:02:28 +010059#include "rtl8225se.h"
Michael Wuf6532112007-10-14 14:43:16 -040060
61MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
Andrea Merello93ba2a82013-08-26 13:53:30 +020062MODULE_AUTHOR("Andrea Merello <andrea.merello@gmail.com>");
Andrea Merello1eba6482014-03-26 21:04:23 +010063MODULE_DESCRIPTION("RTL8180 / RTL8185 / RTL8187SE PCI wireless driver");
Michael Wuf6532112007-10-14 14:43:16 -040064MODULE_LICENSE("GPL");
65
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000066static DEFINE_PCI_DEVICE_TABLE(rtl8180_table) = {
Andrea Merello1eba6482014-03-26 21:04:23 +010067
68 /* rtl8187se */
69 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8199) },
70
Michael Wuf6532112007-10-14 14:43:16 -040071 /* rtl8185 */
72 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8185) },
Adrian Bassett4fcc5472008-01-23 16:38:33 +000073 { PCI_DEVICE(PCI_VENDOR_ID_BELKIN, 0x700f) },
Michael Wuf6532112007-10-14 14:43:16 -040074 { PCI_DEVICE(PCI_VENDOR_ID_BELKIN, 0x701f) },
75
76 /* rtl8180 */
77 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8180) },
78 { PCI_DEVICE(0x1799, 0x6001) },
79 { PCI_DEVICE(0x1799, 0x6020) },
80 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x3300) },
Xose Vazquez Perez29a6b502012-06-15 17:27:05 +020081 { PCI_DEVICE(0x1186, 0x3301) },
82 { PCI_DEVICE(0x1432, 0x7106) },
Michael Wuf6532112007-10-14 14:43:16 -040083 { }
84};
85
86MODULE_DEVICE_TABLE(pci, rtl8180_table);
87
Johannes Berg8318d782008-01-24 19:38:38 +010088static const struct ieee80211_rate rtl818x_rates[] = {
89 { .bitrate = 10, .hw_value = 0, },
90 { .bitrate = 20, .hw_value = 1, },
91 { .bitrate = 55, .hw_value = 2, },
92 { .bitrate = 110, .hw_value = 3, },
93 { .bitrate = 60, .hw_value = 4, },
94 { .bitrate = 90, .hw_value = 5, },
95 { .bitrate = 120, .hw_value = 6, },
96 { .bitrate = 180, .hw_value = 7, },
97 { .bitrate = 240, .hw_value = 8, },
98 { .bitrate = 360, .hw_value = 9, },
99 { .bitrate = 480, .hw_value = 10, },
100 { .bitrate = 540, .hw_value = 11, },
101};
102
103static const struct ieee80211_channel rtl818x_channels[] = {
104 { .center_freq = 2412 },
105 { .center_freq = 2417 },
106 { .center_freq = 2422 },
107 { .center_freq = 2427 },
108 { .center_freq = 2432 },
109 { .center_freq = 2437 },
110 { .center_freq = 2442 },
111 { .center_freq = 2447 },
112 { .center_freq = 2452 },
113 { .center_freq = 2457 },
114 { .center_freq = 2462 },
115 { .center_freq = 2467 },
116 { .center_freq = 2472 },
117 { .center_freq = 2484 },
118};
119
Andrea Merello3ee44d62014-03-26 21:00:57 +0100120/* Queues for rtl8187se card
121 *
122 * name | reg | queue
123 * BC | 7 | 6
124 * MG | 1 | 0
125 * HI | 6 | 1
126 * VO | 5 | 2
127 * VI | 4 | 3
128 * BE | 3 | 4
129 * BK | 2 | 5
130 *
131 * The complete map for DMA kick reg using use all queue is:
132 * static const int rtl8187se_queues_map[RTL8187SE_NR_TX_QUEUES] =
133 * {1, 6, 5, 4, 3, 2, 7};
134 *
135 * .. but.. Because for mac80211 4 queues are enough for QoS we use this
136 *
137 * name | reg | queue
138 * BC | 7 | 4 <- currently not used yet
139 * MG | 1 | x <- Not used
140 * HI | 6 | x <- Not used
141 * VO | 5 | 0 <- used
142 * VI | 4 | 1 <- used
143 * BE | 3 | 2 <- used
144 * BK | 2 | 3 <- used
145 *
146 * Beacon queue could be used, but this is not finished yet.
147 *
148 * I thougth about using the other two queues but I decided not to do this:
149 *
150 * - I'm unsure whether the mac80211 will ever try to use more than 4 queues
151 * by itself.
152 *
153 * - I could route MGMT frames (currently sent over VO queue) to the MGMT
154 * queue but since mac80211 will do not know about it, I will probably gain
155 * some HW priority whenever the VO queue is not empty, but this gain is
156 * limited by the fact that I had to stop the mac80211 queue whenever one of
157 * the VO or MGMT queues is full, stopping also submitting of MGMT frame
158 * to the driver.
159 *
160 * - I don't know how to set in the HW the contention window params for MGMT
161 * and HI-prio queues.
162 */
163
164static const int rtl8187se_queues_map[RTL8187SE_NR_TX_QUEUES] = {5, 4, 3, 2, 7};
165
Andrea Merellofd6564f2014-03-22 18:51:20 +0100166/* Queues for rtl8180/rtl8185 cards
167 *
168 * name | reg | prio
169 * BC | 7 | 3
170 * HI | 6 | 0
171 * NO | 5 | 1
172 * LO | 4 | 2
173 *
174 * The complete map for DMA kick reg using all queue is:
175 * static const int rtl8180_queues_map[RTL8180_NR_TX_QUEUES] = {6, 5, 4, 7};
176 *
177 * .. but .. Because the mac80211 needs at least 4 queues for QoS or
178 * otherwise QoS can't be done, we use just one.
179 * Beacon queue could be used, but this is not finished yet.
180 * Actual map is:
181 *
182 * name | reg | prio
183 * BC | 7 | 1 <- currently not used yet.
184 * HI | 6 | x <- not used
185 * NO | 5 | x <- not used
186 * LO | 4 | 0 <- used
187 */
188
189static const int rtl8180_queues_map[RTL8180_NR_TX_QUEUES] = {4, 7};
Johannes Berg8318d782008-01-24 19:38:38 +0100190
Michael Wuf6532112007-10-14 14:43:16 -0400191void rtl8180_write_phy(struct ieee80211_hw *dev, u8 addr, u32 data)
192{
193 struct rtl8180_priv *priv = dev->priv;
194 int i = 10;
195 u32 buf;
196
197 buf = (data << 8) | addr;
198
199 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf | 0x80);
200 while (i--) {
201 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf);
202 if (rtl818x_ioread8(priv, &priv->map->PHY[2]) == (data & 0xFF))
203 return;
204 }
205}
206
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400207static void rtl8180_handle_rx(struct ieee80211_hw *dev)
Michael Wuf6532112007-10-14 14:43:16 -0400208{
209 struct rtl8180_priv *priv = dev->priv;
Andrea Merello21025922014-03-26 20:59:52 +0100210 struct rtl818x_rx_cmd_desc *cmd_desc;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400211 unsigned int count = 32;
Andrea Merelloafbedbf2014-05-31 18:29:46 +0200212 u8 agc, sq, signal = 1;
andrea.merello2b4db052014-02-05 22:38:05 +0100213 dma_addr_t mapping;
Michael Wuf6532112007-10-14 14:43:16 -0400214
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400215 while (count--) {
Andrea Merello21025922014-03-26 20:59:52 +0100216 void *entry = priv->rx_ring + priv->rx_idx * priv->rx_ring_sz;
Michael Wuf6532112007-10-14 14:43:16 -0400217 struct sk_buff *skb = priv->rx_buf[priv->rx_idx];
Andrea Merello21025922014-03-26 20:59:52 +0100218 u32 flags, flags2;
219 u64 tsft;
220
221 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
222 struct rtl8187se_rx_desc *desc = entry;
223
224 flags = le32_to_cpu(desc->flags);
Andrea Merello325ed9f2014-05-31 18:30:26 +0200225 /* if ownership flag is set, then we can trust the
226 * HW has written other fields. We must not trust
227 * other descriptor data read before we checked (read)
228 * the ownership flag
229 */
230 rmb();
Andrea Merello21025922014-03-26 20:59:52 +0100231 flags2 = le32_to_cpu(desc->flags2);
232 tsft = le64_to_cpu(desc->tsft);
233 } else {
234 struct rtl8180_rx_desc *desc = entry;
235
236 flags = le32_to_cpu(desc->flags);
Andrea Merello325ed9f2014-05-31 18:30:26 +0200237 /* same as above */
238 rmb();
Andrea Merello21025922014-03-26 20:59:52 +0100239 flags2 = le32_to_cpu(desc->flags2);
240 tsft = le64_to_cpu(desc->tsft);
241 }
Michael Wuf6532112007-10-14 14:43:16 -0400242
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300243 if (flags & RTL818X_RX_DESC_FLAG_OWN)
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400244 return;
Michael Wuf6532112007-10-14 14:43:16 -0400245
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300246 if (unlikely(flags & (RTL818X_RX_DESC_FLAG_DMA_FAIL |
247 RTL818X_RX_DESC_FLAG_FOF |
248 RTL818X_RX_DESC_FLAG_RX_ERR)))
Michael Wuf6532112007-10-14 14:43:16 -0400249 goto done;
250 else {
Michael Wuf6532112007-10-14 14:43:16 -0400251 struct ieee80211_rx_status rx_status = {0};
252 struct sk_buff *new_skb = dev_alloc_skb(MAX_RX_SIZE);
253
254 if (unlikely(!new_skb))
255 goto done;
256
andrea.merello2b4db052014-02-05 22:38:05 +0100257 mapping = pci_map_single(priv->pdev,
258 skb_tail_pointer(new_skb),
259 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
260
261 if (pci_dma_mapping_error(priv->pdev, mapping)) {
262 kfree_skb(new_skb);
263 dev_err(&priv->pdev->dev, "RX DMA map error\n");
264
265 goto done;
266 }
267
Michael Wuf6532112007-10-14 14:43:16 -0400268 pci_unmap_single(priv->pdev,
269 *((dma_addr_t *)skb->cb),
270 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
271 skb_put(skb, flags & 0xFFF);
272
273 rx_status.antenna = (flags2 >> 15) & 1;
Johannes Berg8318d782008-01-24 19:38:38 +0100274 rx_status.rate_idx = (flags >> 20) & 0xF;
John W. Linville8b73fb82010-07-21 16:26:40 -0400275 agc = (flags2 >> 17) & 0x7F;
Andrea Merello6caefd12014-03-08 18:36:37 +0100276
Andrea Merelloafbedbf2014-05-31 18:29:46 +0200277 switch (priv->chip_family) {
278 case RTL818X_CHIP_FAMILY_RTL8185:
John W. Linville8b73fb82010-07-21 16:26:40 -0400279 if (rx_status.rate_idx > 3)
Andrea Merello70493272014-05-31 18:30:13 +0200280 signal = -clamp_t(u8, agc, 25, 90) - 9;
John W. Linville8b73fb82010-07-21 16:26:40 -0400281 else
Andrea Merello70493272014-05-31 18:30:13 +0200282 signal = -clamp_t(u8, agc, 30, 95);
Andrea Merelloafbedbf2014-05-31 18:29:46 +0200283 break;
284 case RTL818X_CHIP_FAMILY_RTL8180:
John W. Linville8b73fb82010-07-21 16:26:40 -0400285 sq = flags2 & 0xff;
286 signal = priv->rf->calc_rssi(agc, sq);
Andrea Merelloafbedbf2014-05-31 18:29:46 +0200287 break;
288 case RTL818X_CHIP_FAMILY_RTL8187SE:
Andrea Merello21025922014-03-26 20:59:52 +0100289 /* TODO: rtl8187se rssi */
290 signal = 10;
Andrea Merelloafbedbf2014-05-31 18:29:46 +0200291 break;
John W. Linville8b73fb82010-07-21 16:26:40 -0400292 }
John W. Linville8b749642010-07-19 16:35:20 -0400293 rx_status.signal = signal;
Karl Beldan675a0b02013-03-25 16:26:57 +0100294 rx_status.freq = dev->conf.chandef.chan->center_freq;
295 rx_status.band = dev->conf.chandef.chan->band;
Andrea Merello21025922014-03-26 20:59:52 +0100296 rx_status.mactime = tsft;
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800297 rx_status.flag |= RX_FLAG_MACTIME_START;
Andrea Merelloe58342d2014-05-07 17:52:16 +0200298 if (flags & RTL818X_RX_DESC_FLAG_SPLCP)
299 rx_status.flag |= RX_FLAG_SHORTPRE;
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300300 if (flags & RTL818X_RX_DESC_FLAG_CRC32_ERR)
Michael Wuf6532112007-10-14 14:43:16 -0400301 rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
302
Johannes Bergf1d58c22009-06-17 13:13:00 +0200303 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400304 ieee80211_rx_irqsafe(dev, skb);
Michael Wuf6532112007-10-14 14:43:16 -0400305
306 skb = new_skb;
307 priv->rx_buf[priv->rx_idx] = skb;
andrea.merello2b4db052014-02-05 22:38:05 +0100308 *((dma_addr_t *) skb->cb) = mapping;
Michael Wuf6532112007-10-14 14:43:16 -0400309 }
310
311 done:
Andrea Merello21025922014-03-26 20:59:52 +0100312 cmd_desc = entry;
313 cmd_desc->rx_buf = cpu_to_le32(*((dma_addr_t *)skb->cb));
314 cmd_desc->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -0400315 MAX_RX_SIZE);
316 if (priv->rx_idx == 31)
Andrea Merello21025922014-03-26 20:59:52 +0100317 cmd_desc->flags |=
318 cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -0400319 priv->rx_idx = (priv->rx_idx + 1) % 32;
320 }
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400321}
Michael Wuf6532112007-10-14 14:43:16 -0400322
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400323static void rtl8180_handle_tx(struct ieee80211_hw *dev, unsigned int prio)
324{
325 struct rtl8180_priv *priv = dev->priv;
326 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
Michael Wuf6532112007-10-14 14:43:16 -0400327
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400328 while (skb_queue_len(&ring->queue)) {
329 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
330 struct sk_buff *skb;
331 struct ieee80211_tx_info *info;
332 u32 flags = le32_to_cpu(entry->flags);
333
334 if (flags & RTL818X_TX_DESC_FLAG_OWN)
335 return;
336
337 ring->idx = (ring->idx + 1) % ring->entries;
338 skb = __skb_dequeue(&ring->queue);
339 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
340 skb->len, PCI_DMA_TODEVICE);
341
342 info = IEEE80211_SKB_CB(skb);
343 ieee80211_tx_info_clear_status(info);
344
345 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
346 (flags & RTL818X_TX_DESC_FLAG_TX_OK))
347 info->flags |= IEEE80211_TX_STAT_ACK;
348
349 info->status.rates[0].count = (flags & 0xFF) + 1;
350 info->status.rates[1].idx = -1;
351
352 ieee80211_tx_status_irqsafe(dev, skb);
353 if (ring->entries - skb_queue_len(&ring->queue) == 2)
354 ieee80211_wake_queue(dev, prio);
Michael Wuf6532112007-10-14 14:43:16 -0400355 }
356}
357
Andrea Merelloa373ebc2014-03-26 21:00:06 +0100358static irqreturn_t rtl8187se_interrupt(int irq, void *dev_id)
359{
360 struct ieee80211_hw *dev = dev_id;
361 struct rtl8180_priv *priv = dev->priv;
362 u32 reg;
363 unsigned long flags;
364 static int desc_err;
365
366 spin_lock_irqsave(&priv->lock, flags);
367 /* Note: 32-bit interrupt status */
368 reg = rtl818x_ioread32(priv, &priv->map->INT_STATUS_SE);
369 if (unlikely(reg == 0xFFFFFFFF)) {
370 spin_unlock_irqrestore(&priv->lock, flags);
371 return IRQ_HANDLED;
372 }
373
374 rtl818x_iowrite32(priv, &priv->map->INT_STATUS_SE, reg);
375
376 if (reg & IMR_TIMEOUT1)
377 rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
378
379 if (reg & (IMR_TBDOK | IMR_TBDER))
380 rtl8180_handle_tx(dev, 4);
381
382 if (reg & (IMR_TVODOK | IMR_TVODER))
383 rtl8180_handle_tx(dev, 0);
384
385 if (reg & (IMR_TVIDOK | IMR_TVIDER))
386 rtl8180_handle_tx(dev, 1);
387
388 if (reg & (IMR_TBEDOK | IMR_TBEDER))
389 rtl8180_handle_tx(dev, 2);
390
391 if (reg & (IMR_TBKDOK | IMR_TBKDER))
392 rtl8180_handle_tx(dev, 3);
393
394 if (reg & (IMR_ROK | IMR_RER | RTL818X_INT_SE_RX_DU | IMR_RQOSOK))
395 rtl8180_handle_rx(dev);
396 /* The interface sometimes generates several RX DMA descriptor errors
397 * at startup. Do not report these.
398 */
399 if ((reg & RTL818X_INT_SE_RX_DU) && desc_err++ > 2)
400 if (net_ratelimit())
401 wiphy_err(dev->wiphy, "No RX DMA Descriptor avail\n");
402
403 spin_unlock_irqrestore(&priv->lock, flags);
404 return IRQ_HANDLED;
405}
406
Michael Wuf6532112007-10-14 14:43:16 -0400407static irqreturn_t rtl8180_interrupt(int irq, void *dev_id)
408{
409 struct ieee80211_hw *dev = dev_id;
410 struct rtl8180_priv *priv = dev->priv;
411 u16 reg;
412
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400413 spin_lock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400414 reg = rtl818x_ioread16(priv, &priv->map->INT_STATUS);
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400415 if (unlikely(reg == 0xFFFF)) {
416 spin_unlock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400417 return IRQ_HANDLED;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400418 }
Michael Wuf6532112007-10-14 14:43:16 -0400419
420 rtl818x_iowrite16(priv, &priv->map->INT_STATUS, reg);
421
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400422 if (reg & (RTL818X_INT_TXB_OK | RTL818X_INT_TXB_ERR))
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400423 rtl8180_handle_tx(dev, 1);
424
425 if (reg & (RTL818X_INT_TXL_OK | RTL818X_INT_TXL_ERR))
426 rtl8180_handle_tx(dev, 0);
427
428 if (reg & (RTL818X_INT_RX_OK | RTL818X_INT_RX_ERR))
429 rtl8180_handle_rx(dev);
430
431 spin_unlock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400432
433 return IRQ_HANDLED;
434}
435
Thomas Huehn36323f82012-07-23 21:33:42 +0200436static void rtl8180_tx(struct ieee80211_hw *dev,
437 struct ieee80211_tx_control *control,
438 struct sk_buff *skb)
Michael Wuf6532112007-10-14 14:43:16 -0400439{
Johannes Berge039fa42008-05-15 12:55:29 +0200440 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
John W. Linville51e080d2010-05-06 16:26:23 -0400441 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Michael Wuf6532112007-10-14 14:43:16 -0400442 struct rtl8180_priv *priv = dev->priv;
443 struct rtl8180_tx_ring *ring;
444 struct rtl8180_tx_desc *entry;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400445 unsigned long flags;
Andrea Merellofd6564f2014-03-22 18:51:20 +0100446 unsigned int idx, prio, hw_prio;
Michael Wuf6532112007-10-14 14:43:16 -0400447 dma_addr_t mapping;
448 u32 tx_flags;
Johannes Berge6a98542008-10-21 12:40:02 +0200449 u8 rc_flags;
Michael Wuf6532112007-10-14 14:43:16 -0400450 u16 plcp_len = 0;
451 __le16 rts_duration = 0;
Andrea Merello3ee44d62014-03-26 21:00:57 +0100452 /* do arithmetic and then convert to le16 */
453 u16 frame_duration = 0;
Michael Wuf6532112007-10-14 14:43:16 -0400454
Johannes Berge2530082008-05-17 00:57:14 +0200455 prio = skb_get_queue_mapping(skb);
Michael Wuf6532112007-10-14 14:43:16 -0400456 ring = &priv->tx_ring[prio];
457
458 mapping = pci_map_single(priv->pdev, skb->data,
459 skb->len, PCI_DMA_TODEVICE);
460
andrea.merello348f7d42014-02-05 22:38:06 +0100461 if (pci_dma_mapping_error(priv->pdev, mapping)) {
462 kfree_skb(skb);
463 dev_err(&priv->pdev->dev, "TX DMA mapping error\n");
464 return;
andrea.merello348f7d42014-02-05 22:38:06 +0100465 }
466
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300467 tx_flags = RTL818X_TX_DESC_FLAG_OWN | RTL818X_TX_DESC_FLAG_FS |
468 RTL818X_TX_DESC_FLAG_LS |
Johannes Berge039fa42008-05-15 12:55:29 +0200469 (ieee80211_get_tx_rate(dev, info)->hw_value << 24) |
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200470 skb->len;
Michael Wuf6532112007-10-14 14:43:16 -0400471
Andrea Merello6caefd12014-03-08 18:36:37 +0100472 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300473 tx_flags |= RTL818X_TX_DESC_FLAG_DMA |
474 RTL818X_TX_DESC_FLAG_NO_ENC;
Michael Wuf6532112007-10-14 14:43:16 -0400475
Johannes Berge6a98542008-10-21 12:40:02 +0200476 rc_flags = info->control.rates[0].flags;
Andrea Merello5d0d1e92014-05-07 17:53:17 +0200477
478 /* HW will perform RTS-CTS when only RTS flags is set.
479 * HW will perform CTS-to-self when both RTS and CTS flags are set.
480 * RTS rate and RTS duration will be used also for CTS-to-self.
481 */
Johannes Berge6a98542008-10-21 12:40:02 +0200482 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300483 tx_flags |= RTL818X_TX_DESC_FLAG_RTS;
Johannes Berge039fa42008-05-15 12:55:29 +0200484 tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
Andrea Merello5d0d1e92014-05-07 17:53:17 +0200485 rts_duration = ieee80211_rts_duration(dev, priv->vif,
486 skb->len, info);
Johannes Berge6a98542008-10-21 12:40:02 +0200487 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Andrea Merello5d0d1e92014-05-07 17:53:17 +0200488 tx_flags |= RTL818X_TX_DESC_FLAG_RTS | RTL818X_TX_DESC_FLAG_CTS;
Johannes Berge039fa42008-05-15 12:55:29 +0200489 tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
Andrea Merello5d0d1e92014-05-07 17:53:17 +0200490 rts_duration = ieee80211_ctstoself_duration(dev, priv->vif,
491 skb->len, info);
Johannes Bergaa68cbf2008-02-18 14:20:30 +0100492 }
Michael Wuf6532112007-10-14 14:43:16 -0400493
Andrea Merello6caefd12014-03-08 18:36:37 +0100494 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400495 unsigned int remainder;
496
497 plcp_len = DIV_ROUND_UP(16 * (skb->len + 4),
Johannes Berge039fa42008-05-15 12:55:29 +0200498 (ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
Michael Wuf6532112007-10-14 14:43:16 -0400499 remainder = (16 * (skb->len + 4)) %
Johannes Berge039fa42008-05-15 12:55:29 +0200500 ((ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
Roel Kluin35a0ace2009-06-22 17:42:21 +0200501 if (remainder <= 6)
Michael Wuf6532112007-10-14 14:43:16 -0400502 plcp_len |= 1 << 15;
503 }
504
Andrea Merello3ee44d62014-03-26 21:00:57 +0100505 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
506 __le16 duration;
507 /* SIFS time (required by HW) is already included by
508 * ieee80211_generic_frame_duration
509 */
510 duration = ieee80211_generic_frame_duration(dev, priv->vif,
511 IEEE80211_BAND_2GHZ, skb->len,
512 ieee80211_get_tx_rate(dev, info));
513
514 frame_duration = priv->ack_time + le16_to_cpu(duration);
515 }
516
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400517 spin_lock_irqsave(&priv->lock, flags);
John W. Linville51e080d2010-05-06 16:26:23 -0400518
519 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
520 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
521 priv->seqno += 0x10;
522 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
523 hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
524 }
525
Michael Wuf6532112007-10-14 14:43:16 -0400526 idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
527 entry = &ring->desc[idx];
528
Andrea Merello3ee44d62014-03-26 21:00:57 +0100529 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
530 entry->frame_duration = cpu_to_le16(frame_duration);
531 entry->frame_len_se = cpu_to_le16(skb->len);
532
533 /* tpc polarity */
534 entry->flags3 = cpu_to_le16(1<<4);
535 } else
536 entry->frame_len = cpu_to_le32(skb->len);
537
Michael Wuf6532112007-10-14 14:43:16 -0400538 entry->rts_duration = rts_duration;
539 entry->plcp_len = cpu_to_le16(plcp_len);
540 entry->tx_buf = cpu_to_le32(mapping);
Andrea Merello3ee44d62014-03-26 21:00:57 +0100541
Johannes Berge6a98542008-10-21 12:40:02 +0200542 entry->flags2 = info->control.rates[1].idx >= 0 ?
Felix Fietkau870abdf2008-10-05 18:04:24 +0200543 ieee80211_get_alt_retry_rate(dev, info, 0)->bitrate << 4 : 0;
Johannes Berge6a98542008-10-21 12:40:02 +0200544 entry->retry_limit = info->control.rates[0].count;
andrea merello4c552a52014-02-18 02:10:45 +0100545
546 /* We must be sure that tx_flags is written last because the HW
547 * looks at it to check if the rest of data is valid or not
548 */
549 wmb();
Michael Wuf6532112007-10-14 14:43:16 -0400550 entry->flags = cpu_to_le32(tx_flags);
andrea merelloc24782e2014-02-18 02:10:46 +0100551 /* We must be sure this has been written before followings HW
552 * register write, because this write will made the HW attempts
553 * to DMA the just-written data
554 */
555 wmb();
556
Michael Wuf6532112007-10-14 14:43:16 -0400557 __skb_queue_tail(&ring->queue, skb);
558 if (ring->entries - skb_queue_len(&ring->queue) < 2)
John W. Linvilled10e2e02010-04-27 16:57:38 -0400559 ieee80211_stop_queue(dev, prio);
John W. Linville51e080d2010-05-06 16:26:23 -0400560
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400561 spin_unlock_irqrestore(&priv->lock, flags);
Michael Wuf6532112007-10-14 14:43:16 -0400562
Andrea Merello3ee44d62014-03-26 21:00:57 +0100563 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
564 /* just poll: rings are stopped with TPPollStop reg */
565 hw_prio = rtl8187se_queues_map[prio];
566 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
567 (1 << hw_prio));
568 } else {
569 hw_prio = rtl8180_queues_map[prio];
570 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
Andrea Merellofd6564f2014-03-22 18:51:20 +0100571 (1 << hw_prio) | /* ring to poll */
572 (1<<1) | (1<<2));/* stopped rings */
Andrea Merello3ee44d62014-03-26 21:00:57 +0100573 }
Michael Wuf6532112007-10-14 14:43:16 -0400574}
575
Andrea Merelloff3cbc22014-03-26 21:02:05 +0100576static void rtl8180_set_anaparam3(struct rtl8180_priv *priv, u16 anaparam3)
577{
578 u8 reg;
579
580 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
581 RTL818X_EEPROM_CMD_CONFIG);
582
583 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
584 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
585 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
586
587 rtl818x_iowrite16(priv, &priv->map->ANAPARAM3, anaparam3);
588
589 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
590 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
591
592 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
593 RTL818X_EEPROM_CMD_NORMAL);
594}
595
596void rtl8180_set_anaparam2(struct rtl8180_priv *priv, u32 anaparam2)
597{
598 u8 reg;
599
600 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
601 RTL818X_EEPROM_CMD_CONFIG);
602
603 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
604 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
605 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
606
607 rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, anaparam2);
608
609 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
610 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
611
612 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
613 RTL818X_EEPROM_CMD_NORMAL);
614}
615
Michael Wuf6532112007-10-14 14:43:16 -0400616void rtl8180_set_anaparam(struct rtl8180_priv *priv, u32 anaparam)
617{
618 u8 reg;
619
620 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
621 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
622 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
623 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
624 rtl818x_iowrite32(priv, &priv->map->ANAPARAM, anaparam);
625 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
626 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
627 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
628}
629
Andrea Merello4a67aa52014-03-26 21:02:46 +0100630static void rtl8187se_mac_config(struct ieee80211_hw *dev)
631{
632 struct rtl8180_priv *priv = dev->priv;
633 u8 reg;
634
635 rtl818x_iowrite32(priv, REG_ADDR4(0x1F0), 0);
636 rtl818x_ioread32(priv, REG_ADDR4(0x1F0));
637 rtl818x_iowrite32(priv, REG_ADDR4(0x1F4), 0);
638 rtl818x_ioread32(priv, REG_ADDR4(0x1F4));
639 rtl818x_iowrite8(priv, REG_ADDR1(0x1F8), 0);
640 rtl818x_ioread8(priv, REG_ADDR1(0x1F8));
641 /* Enable DA10 TX power saving */
642 reg = rtl818x_ioread8(priv, &priv->map->PHY_PR);
643 rtl818x_iowrite8(priv, &priv->map->PHY_PR, reg | 0x04);
644 /* Power */
645 rtl818x_iowrite16(priv, PI_DATA_REG, 0x1000);
646 rtl818x_iowrite16(priv, SI_DATA_REG, 0x1000);
647 /* AFE - default to power ON */
648 rtl818x_iowrite16(priv, REG_ADDR2(0x370), 0x0560);
649 rtl818x_iowrite16(priv, REG_ADDR2(0x372), 0x0560);
650 rtl818x_iowrite16(priv, REG_ADDR2(0x374), 0x0DA4);
651 rtl818x_iowrite16(priv, REG_ADDR2(0x376), 0x0DA4);
652 rtl818x_iowrite16(priv, REG_ADDR2(0x378), 0x0560);
653 rtl818x_iowrite16(priv, REG_ADDR2(0x37A), 0x0560);
654 rtl818x_iowrite16(priv, REG_ADDR2(0x37C), 0x00EC);
655 rtl818x_iowrite16(priv, REG_ADDR2(0x37E), 0x00EC);
656 rtl818x_iowrite8(priv, REG_ADDR1(0x24E), 0x01);
657 /* unknown, needed for suspend to RAM resume */
658 rtl818x_iowrite8(priv, REG_ADDR1(0x0A), 0x72);
659}
660
661static void rtl8187se_set_antenna_config(struct ieee80211_hw *dev, u8 def_ant,
662 bool diversity)
663{
664 struct rtl8180_priv *priv = dev->priv;
665
666 rtl8225_write_phy_cck(dev, 0x0C, 0x09);
667 if (diversity) {
668 if (def_ant == 1) {
669 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x00);
670 rtl8225_write_phy_cck(dev, 0x11, 0xBB);
671 rtl8225_write_phy_cck(dev, 0x01, 0xC7);
672 rtl8225_write_phy_ofdm(dev, 0x0D, 0x54);
673 rtl8225_write_phy_ofdm(dev, 0x18, 0xB2);
674 } else { /* main antenna */
675 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x03);
676 rtl8225_write_phy_cck(dev, 0x11, 0x9B);
677 rtl8225_write_phy_cck(dev, 0x01, 0xC7);
678 rtl8225_write_phy_ofdm(dev, 0x0D, 0x5C);
679 rtl8225_write_phy_ofdm(dev, 0x18, 0xB2);
680 }
681 } else { /* disable antenna diversity */
682 if (def_ant == 1) {
683 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x00);
684 rtl8225_write_phy_cck(dev, 0x11, 0xBB);
685 rtl8225_write_phy_cck(dev, 0x01, 0x47);
686 rtl8225_write_phy_ofdm(dev, 0x0D, 0x54);
687 rtl8225_write_phy_ofdm(dev, 0x18, 0x32);
688 } else { /* main antenna */
689 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x03);
690 rtl8225_write_phy_cck(dev, 0x11, 0x9B);
691 rtl8225_write_phy_cck(dev, 0x01, 0x47);
692 rtl8225_write_phy_ofdm(dev, 0x0D, 0x5C);
693 rtl8225_write_phy_ofdm(dev, 0x18, 0x32);
694 }
695 }
696 /* priv->curr_ant = def_ant; */
697}
698
Andrea Merello732c8932014-03-26 21:00:24 +0100699static void rtl8180_int_enable(struct ieee80211_hw *dev)
700{
701 struct rtl8180_priv *priv = dev->priv;
702
703 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
Andrea Merello1c3fb9b2014-04-04 18:21:14 +0200704 rtl818x_iowrite32(priv, &priv->map->IMR,
705 IMR_TBDER | IMR_TBDOK |
Andrea Merello732c8932014-03-26 21:00:24 +0100706 IMR_TVODER | IMR_TVODOK |
707 IMR_TVIDER | IMR_TVIDOK |
708 IMR_TBEDER | IMR_TBEDOK |
709 IMR_TBKDER | IMR_TBKDOK |
710 IMR_RDU | IMR_RER |
711 IMR_ROK | IMR_RQOSOK);
712 } else {
713 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
714 }
715}
716
717static void rtl8180_int_disable(struct ieee80211_hw *dev)
718{
719 struct rtl8180_priv *priv = dev->priv;
720
721 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
722 rtl818x_iowrite32(priv, &priv->map->IMR, 0);
723 } else {
724 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
725 }
726}
727
Andrea Merello516a0932014-03-15 18:29:36 +0100728static void rtl8180_conf_basic_rates(struct ieee80211_hw *dev,
729 u32 rates_mask)
730{
731 struct rtl8180_priv *priv = dev->priv;
732
733 u8 max, min;
734 u16 reg;
735
736 max = fls(rates_mask) - 1;
737 min = ffs(rates_mask) - 1;
738
739 switch (priv->chip_family) {
740
741 case RTL818X_CHIP_FAMILY_RTL8180:
742 /* in 8180 this is NOT a BITMAP */
743 reg = rtl818x_ioread16(priv, &priv->map->BRSR);
744 reg &= ~3;
745 reg |= max;
746 rtl818x_iowrite16(priv, &priv->map->BRSR, reg);
Andrea Merello516a0932014-03-15 18:29:36 +0100747 break;
748
749 case RTL818X_CHIP_FAMILY_RTL8185:
750 /* in 8185 this is a BITMAP */
751 rtl818x_iowrite16(priv, &priv->map->BRSR, rates_mask);
752 rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (max << 4) | min);
753 break;
Andrea Merellod209f3b2014-03-26 20:59:25 +0100754
755 case RTL818X_CHIP_FAMILY_RTL8187SE:
756 /* in 8187se this is a BITMAP */
757 rtl818x_iowrite16(priv, &priv->map->BRSR_8187SE, rates_mask);
758 break;
Andrea Merello516a0932014-03-15 18:29:36 +0100759 }
760}
761
Andrea Merellof1026df2014-03-26 21:01:19 +0100762static void rtl8180_config_cardbus(struct ieee80211_hw *dev)
763{
764 struct rtl8180_priv *priv = dev->priv;
765 u16 reg16;
766 u8 reg8;
767
768 reg8 = rtl818x_ioread8(priv, &priv->map->CONFIG3);
769 reg8 |= 1 << 1;
770 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg8);
771
772 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
773 rtl818x_iowrite16(priv, FEMR_SE, 0xffff);
774 } else {
775 reg16 = rtl818x_ioread16(priv, &priv->map->FEMR);
776 reg16 |= (1 << 15) | (1 << 14) | (1 << 4);
777 rtl818x_iowrite16(priv, &priv->map->FEMR, reg16);
778 }
779
780}
781
Michael Wuf6532112007-10-14 14:43:16 -0400782static int rtl8180_init_hw(struct ieee80211_hw *dev)
783{
784 struct rtl8180_priv *priv = dev->priv;
785 u16 reg;
Andrea Merello4a67aa52014-03-26 21:02:46 +0100786 u32 reg32;
Michael Wuf6532112007-10-14 14:43:16 -0400787
788 rtl818x_iowrite8(priv, &priv->map->CMD, 0);
789 rtl818x_ioread8(priv, &priv->map->CMD);
790 msleep(10);
791
792 /* reset */
Andrea Merello732c8932014-03-26 21:00:24 +0100793 rtl8180_int_disable(dev);
Michael Wuf6532112007-10-14 14:43:16 -0400794 rtl818x_ioread8(priv, &priv->map->CMD);
795
796 reg = rtl818x_ioread8(priv, &priv->map->CMD);
797 reg &= (1 << 1);
798 reg |= RTL818X_CMD_RESET;
799 rtl818x_iowrite8(priv, &priv->map->CMD, RTL818X_CMD_RESET);
800 rtl818x_ioread8(priv, &priv->map->CMD);
801 msleep(200);
802
803 /* check success of reset */
804 if (rtl818x_ioread8(priv, &priv->map->CMD) & RTL818X_CMD_RESET) {
Joe Perchesc96c31e2010-07-26 14:39:58 -0700805 wiphy_err(dev->wiphy, "reset timeout!\n");
Michael Wuf6532112007-10-14 14:43:16 -0400806 return -ETIMEDOUT;
807 }
808
809 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
810 rtl818x_ioread8(priv, &priv->map->CMD);
811 msleep(200);
812
813 if (rtl818x_ioread8(priv, &priv->map->CONFIG3) & (1 << 3)) {
Andrea Merellof1026df2014-03-26 21:01:19 +0100814 rtl8180_config_cardbus(dev);
Michael Wuf6532112007-10-14 14:43:16 -0400815 }
816
Andrea Merello4a67aa52014-03-26 21:02:46 +0100817 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
818 rtl818x_iowrite8(priv, &priv->map->MSR, RTL818X_MSR_ENEDCA);
819 else
820 rtl818x_iowrite8(priv, &priv->map->MSR, 0);
Michael Wuf6532112007-10-14 14:43:16 -0400821
Andrea Merello6caefd12014-03-08 18:36:37 +0100822 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -0400823 rtl8180_set_anaparam(priv, priv->anaparam);
824
825 rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
Andrea Merello4a67aa52014-03-26 21:02:46 +0100826 /* mac80211 queue have higher prio for lower index. The last queue
827 * (that mac80211 is not aware of) is reserved for beacons (and have
828 * the highest priority on the NIC)
829 */
830 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8187SE) {
831 rtl818x_iowrite32(priv, &priv->map->TBDA,
832 priv->tx_ring[1].dma);
833 rtl818x_iowrite32(priv, &priv->map->TLPDA,
834 priv->tx_ring[0].dma);
835 } else {
836 rtl818x_iowrite32(priv, &priv->map->TBDA,
837 priv->tx_ring[4].dma);
838 rtl818x_iowrite32(priv, &priv->map->TVODA,
839 priv->tx_ring[0].dma);
840 rtl818x_iowrite32(priv, &priv->map->TVIDA,
841 priv->tx_ring[1].dma);
842 rtl818x_iowrite32(priv, &priv->map->TBEDA,
843 priv->tx_ring[2].dma);
844 rtl818x_iowrite32(priv, &priv->map->TBKDA,
845 priv->tx_ring[3].dma);
846 }
Michael Wuf6532112007-10-14 14:43:16 -0400847
848 /* TODO: necessary? specs indicate not */
849 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
850 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
851 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
Andrea Merello6caefd12014-03-08 18:36:37 +0100852 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
Michael Wuf6532112007-10-14 14:43:16 -0400853 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
854 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
855 }
856 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
857
858 /* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */
859
860 /* TODO: turn off hw wep on rtl8180 */
861
862 rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
863
Andrea Merello6caefd12014-03-08 18:36:37 +0100864 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400865 rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
866 rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
Andrea Merello4a67aa52014-03-26 21:02:46 +0100867 } else {
868 rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);
Michael Wuf6532112007-10-14 14:43:16 -0400869
Andrea Merello4a67aa52014-03-26 21:02:46 +0100870 rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
871 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, 0x4C);
872 }
873
874 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
Michael Wuf6532112007-10-14 14:43:16 -0400875 /* TODO: set ClkRun enable? necessary? */
876 reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
877 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, reg & ~(1 << 6));
878 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
879 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
880 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
881 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
Andrea Merello7df00722014-06-30 18:18:25 +0200882 /* fix eccessive IFS after CTS-to-self */
883 rtl818x_iowrite8(priv, REG_ADDR1(0x1ff), 0x35);
Andrea Merello4a67aa52014-03-26 21:02:46 +0100884 }
Michael Wuf6532112007-10-14 14:43:16 -0400885
Andrea Merello4a67aa52014-03-26 21:02:46 +0100886 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
887
888 /* the set auto rate fallback bitmask from 1M to 54 Mb/s */
889 rtl818x_iowrite16(priv, ARFR, 0xFFF);
890 rtl818x_ioread16(priv, ARFR);
891
892 /* stop unused queus (no dma alloc) */
893 rtl818x_iowrite8(priv, &priv->map->TPPOLL_STOP,
894 RTL818x_TPPOLL_STOP_MG | RTL818x_TPPOLL_STOP_HI);
895
896 rtl818x_iowrite8(priv, &priv->map->ACM_CONTROL, 0x00);
897 rtl818x_iowrite16(priv, &priv->map->TID_AC_MAP, 0xFA50);
898
899 rtl818x_iowrite16(priv, &priv->map->INT_MIG, 0);
900
901 /* some black magic here.. */
902 rtl8187se_mac_config(dev);
903
904 rtl818x_iowrite16(priv, RFSW_CTRL, 0x569A);
905 rtl818x_ioread16(priv, RFSW_CTRL);
906
907 rtl8180_set_anaparam(priv, RTL8225SE_ANAPARAM_ON);
908 rtl8180_set_anaparam2(priv, RTL8225SE_ANAPARAM2_ON);
909 rtl8180_set_anaparam3(priv, RTL8225SE_ANAPARAM3);
910
911
912 rtl818x_iowrite8(priv, &priv->map->CONFIG5,
913 rtl818x_ioread8(priv, &priv->map->CONFIG5) & 0x7F);
914
915 /*probably this switch led on */
916 rtl818x_iowrite8(priv, &priv->map->PGSELECT,
917 rtl818x_ioread8(priv, &priv->map->PGSELECT) | 0x08);
918
919 rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x0480);
920 rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1BFF);
921 rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x2488);
922
923 rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x4003);
924
925 /* the reference code mac hardcode table write
926 * this reg by doing byte-wide accesses.
927 * It does it just for lowest and highest byte..
928 */
929 reg32 = rtl818x_ioread32(priv, &priv->map->RF_PARA);
930 reg32 &= 0x00ffff00;
931 reg32 |= 0xb8000054;
932 rtl818x_iowrite32(priv, &priv->map->RF_PARA, reg32);
Andrea Merello294bc612014-04-04 18:25:51 +0200933 } else
934 /* stop unused queus (no dma alloc) */
935 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
936 (1<<1) | (1<<2));
Michael Wuf6532112007-10-14 14:43:16 -0400937
938 priv->rf->init(dev);
Andrea Merello516a0932014-03-15 18:29:36 +0100939
940 /* default basic rates are 1,2 Mbps for rtl8180. 1,2,6,9,12,18,24 Mbps
941 * otherwise. bitmask 0x3 and 0x01f3 respectively.
942 * NOTE: currenty rtl8225 RF code changes basic rates, so we need to do
943 * this after rf init.
944 * TODO: try to find out whether RF code really needs to do this..
945 */
946 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
947 rtl8180_conf_basic_rates(dev, 0x3);
948 else
949 rtl8180_conf_basic_rates(dev, 0x1f3);
950
Andrea Merello4a67aa52014-03-26 21:02:46 +0100951 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
952 rtl8187se_set_antenna_config(dev,
953 priv->antenna_diversity_default,
954 priv->antenna_diversity_en);
Michael Wuf6532112007-10-14 14:43:16 -0400955 return 0;
956}
957
958static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
959{
960 struct rtl8180_priv *priv = dev->priv;
Andrea Merello21025922014-03-26 20:59:52 +0100961 struct rtl818x_rx_cmd_desc *entry;
Michael Wuf6532112007-10-14 14:43:16 -0400962 int i;
963
Andrea Merello21025922014-03-26 20:59:52 +0100964 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
965 priv->rx_ring_sz = sizeof(struct rtl8187se_rx_desc);
966 else
967 priv->rx_ring_sz = sizeof(struct rtl8180_rx_desc);
968
Michael Wuf6532112007-10-14 14:43:16 -0400969 priv->rx_ring = pci_alloc_consistent(priv->pdev,
Andrea Merello21025922014-03-26 20:59:52 +0100970 priv->rx_ring_sz * 32,
Michael Wuf6532112007-10-14 14:43:16 -0400971 &priv->rx_ring_dma);
972
973 if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -0700974 wiphy_err(dev->wiphy, "Cannot allocate RX ring\n");
Michael Wuf6532112007-10-14 14:43:16 -0400975 return -ENOMEM;
976 }
977
Andrea Merello21025922014-03-26 20:59:52 +0100978 memset(priv->rx_ring, 0, priv->rx_ring_sz * 32);
Michael Wuf6532112007-10-14 14:43:16 -0400979 priv->rx_idx = 0;
980
981 for (i = 0; i < 32; i++) {
982 struct sk_buff *skb = dev_alloc_skb(MAX_RX_SIZE);
983 dma_addr_t *mapping;
Andrea Merello21025922014-03-26 20:59:52 +0100984 entry = priv->rx_ring + priv->rx_ring_sz*i;
andrea merello4da18bb2014-02-18 02:10:43 +0100985 if (!skb) {
986 wiphy_err(dev->wiphy, "Cannot allocate RX skb\n");
987 return -ENOMEM;
988 }
Michael Wuf6532112007-10-14 14:43:16 -0400989 priv->rx_buf[i] = skb;
990 mapping = (dma_addr_t *)skb->cb;
991 *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
992 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
andrea merelloec1da082014-02-22 17:57:23 +0100993
994 if (pci_dma_mapping_error(priv->pdev, *mapping)) {
995 kfree_skb(skb);
996 wiphy_err(dev->wiphy, "Cannot map DMA for RX skb\n");
997 return -ENOMEM;
998 }
999
Michael Wuf6532112007-10-14 14:43:16 -04001000 entry->rx_buf = cpu_to_le32(*mapping);
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -03001001 entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -04001002 MAX_RX_SIZE);
1003 }
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -03001004 entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -04001005 return 0;
1006}
1007
1008static void rtl8180_free_rx_ring(struct ieee80211_hw *dev)
1009{
1010 struct rtl8180_priv *priv = dev->priv;
1011 int i;
1012
1013 for (i = 0; i < 32; i++) {
1014 struct sk_buff *skb = priv->rx_buf[i];
1015 if (!skb)
1016 continue;
1017
1018 pci_unmap_single(priv->pdev,
1019 *((dma_addr_t *)skb->cb),
1020 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
1021 kfree_skb(skb);
1022 }
1023
Andrea Merello21025922014-03-26 20:59:52 +01001024 pci_free_consistent(priv->pdev, priv->rx_ring_sz * 32,
Michael Wuf6532112007-10-14 14:43:16 -04001025 priv->rx_ring, priv->rx_ring_dma);
1026 priv->rx_ring = NULL;
1027}
1028
1029static int rtl8180_init_tx_ring(struct ieee80211_hw *dev,
1030 unsigned int prio, unsigned int entries)
1031{
1032 struct rtl8180_priv *priv = dev->priv;
1033 struct rtl8180_tx_desc *ring;
1034 dma_addr_t dma;
1035 int i;
1036
1037 ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
1038 if (!ring || (unsigned long)ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -07001039 wiphy_err(dev->wiphy, "Cannot allocate TX ring (prio = %d)\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001040 prio);
Michael Wuf6532112007-10-14 14:43:16 -04001041 return -ENOMEM;
1042 }
1043
1044 memset(ring, 0, sizeof(*ring)*entries);
1045 priv->tx_ring[prio].desc = ring;
1046 priv->tx_ring[prio].dma = dma;
1047 priv->tx_ring[prio].idx = 0;
1048 priv->tx_ring[prio].entries = entries;
1049 skb_queue_head_init(&priv->tx_ring[prio].queue);
1050
1051 for (i = 0; i < entries; i++)
1052 ring[i].next_tx_desc =
1053 cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
1054
1055 return 0;
1056}
1057
1058static void rtl8180_free_tx_ring(struct ieee80211_hw *dev, unsigned int prio)
1059{
1060 struct rtl8180_priv *priv = dev->priv;
1061 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
1062
1063 while (skb_queue_len(&ring->queue)) {
1064 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
1065 struct sk_buff *skb = __skb_dequeue(&ring->queue);
1066
1067 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
1068 skb->len, PCI_DMA_TODEVICE);
Michael Wuf6532112007-10-14 14:43:16 -04001069 kfree_skb(skb);
1070 ring->idx = (ring->idx + 1) % ring->entries;
1071 }
1072
1073 pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
1074 ring->desc, ring->dma);
1075 ring->desc = NULL;
1076}
1077
1078static int rtl8180_start(struct ieee80211_hw *dev)
1079{
1080 struct rtl8180_priv *priv = dev->priv;
1081 int ret, i;
1082 u32 reg;
1083
1084 ret = rtl8180_init_rx_ring(dev);
1085 if (ret)
1086 return ret;
1087
Andrea Merellofd6564f2014-03-22 18:51:20 +01001088 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -04001089 if ((ret = rtl8180_init_tx_ring(dev, i, 16)))
1090 goto err_free_rings;
1091
1092 ret = rtl8180_init_hw(dev);
1093 if (ret)
1094 goto err_free_rings;
1095
Andrea Merelloa373ebc2014-03-26 21:00:06 +01001096 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1097 ret = request_irq(priv->pdev->irq, rtl8187se_interrupt,
Michael Wuf6532112007-10-14 14:43:16 -04001098 IRQF_SHARED, KBUILD_MODNAME, dev);
Andrea Merelloa373ebc2014-03-26 21:00:06 +01001099 } else {
1100 ret = request_irq(priv->pdev->irq, rtl8180_interrupt,
1101 IRQF_SHARED, KBUILD_MODNAME, dev);
1102 }
1103
Michael Wuf6532112007-10-14 14:43:16 -04001104 if (ret) {
Joe Perches5db55842010-08-11 19:11:19 -07001105 wiphy_err(dev->wiphy, "failed to register IRQ handler\n");
Michael Wuf6532112007-10-14 14:43:16 -04001106 goto err_free_rings;
1107 }
1108
Andrea Merello732c8932014-03-26 21:00:24 +01001109 rtl8180_int_enable(dev);
Michael Wuf6532112007-10-14 14:43:16 -04001110
Andrea Merellof18f1122014-03-26 21:00:42 +01001111 /* in rtl8187se at MAR regs offset there is the management
1112 * TX descriptor DMA addres..
1113 */
1114 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8187SE) {
1115 rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
1116 rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
1117 }
Michael Wuf6532112007-10-14 14:43:16 -04001118
1119 reg = RTL818X_RX_CONF_ONLYERLPKT |
1120 RTL818X_RX_CONF_RX_AUTORESETPHY |
1121 RTL818X_RX_CONF_MGMT |
1122 RTL818X_RX_CONF_DATA |
1123 (7 << 8 /* MAX RX DMA */) |
1124 RTL818X_RX_CONF_BROADCAST |
1125 RTL818X_RX_CONF_NICMAC;
1126
Andrea Merello6caefd12014-03-08 18:36:37 +01001127 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185)
Michael Wuf6532112007-10-14 14:43:16 -04001128 reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
Andrea Merello4a67aa52014-03-26 21:02:46 +01001129 else if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -04001130 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
1131 ? RTL818X_RX_CONF_CSDM1 : 0;
1132 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
1133 ? RTL818X_RX_CONF_CSDM2 : 0;
Andrea Merello4a67aa52014-03-26 21:02:46 +01001134 } else {
1135 reg &= ~(RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2);
Michael Wuf6532112007-10-14 14:43:16 -04001136 }
1137
1138 priv->rx_conf = reg;
1139 rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
1140
Andrea Merello6caefd12014-03-08 18:36:37 +01001141 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -04001142 reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
andrea merello14c76152014-02-18 02:10:44 +01001143
1144 /* CW is not on per-packet basis.
1145 * in rtl8185 the CW_VALUE reg is used.
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001146 * in rtl8187se the AC param regs are used.
andrea merello14c76152014-02-18 02:10:44 +01001147 */
andrea merello6f7343d2014-01-21 20:16:43 +01001148 reg &= ~RTL818X_CW_CONF_PERPACKET_CW;
andrea merello14c76152014-02-18 02:10:44 +01001149 /* retry limit IS on per-packet basis.
1150 * the short and long retry limit in TX_CONF
1151 * reg are ignored
1152 */
andrea merello6f7343d2014-01-21 20:16:43 +01001153 reg |= RTL818X_CW_CONF_PERPACKET_RETRY;
Michael Wuf6532112007-10-14 14:43:16 -04001154 rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
1155
1156 reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
andrea merello14c76152014-02-18 02:10:44 +01001157 /* TX antenna and TX gain are not on per-packet basis.
1158 * TX Antenna is selected by ANTSEL reg (RX in BB regs).
1159 * TX gain is selected with CCK_TX_AGC and OFDM_TX_AGC regs
1160 */
andrea merello6f7343d2014-01-21 20:16:43 +01001161 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN;
1162 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL;
Michael Wuf6532112007-10-14 14:43:16 -04001163 reg |= RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
1164 rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
1165
1166 /* disable early TX */
1167 rtl818x_iowrite8(priv, (u8 __iomem *)priv->map + 0xec, 0x3f);
1168 }
1169
1170 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
1171 reg |= (6 << 21 /* MAX TX DMA */) |
1172 RTL818X_TX_CONF_NO_ICV;
1173
Andrea Merello4a67aa52014-03-26 21:02:46 +01001174 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1175 reg |= 1<<30; /* "duration procedure mode" */
Andrea Merello6caefd12014-03-08 18:36:37 +01001176
1177 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -04001178 reg &= ~RTL818X_TX_CONF_PROBE_DTS;
1179 else
1180 reg &= ~RTL818X_TX_CONF_HW_SEQNUM;
1181
andrea merelloe74075a2014-02-18 02:10:40 +01001182 reg &= ~RTL818X_TX_CONF_DISCW;
1183
Michael Wuf6532112007-10-14 14:43:16 -04001184 /* different meaning, same value on both rtl8185 and rtl8180 */
1185 reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;
1186
1187 rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
1188
1189 reg = rtl818x_ioread8(priv, &priv->map->CMD);
1190 reg |= RTL818X_CMD_RX_ENABLE;
1191 reg |= RTL818X_CMD_TX_ENABLE;
1192 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
1193
Michael Wuf6532112007-10-14 14:43:16 -04001194 return 0;
1195
1196 err_free_rings:
1197 rtl8180_free_rx_ring(dev);
Andrea Merellofd6564f2014-03-22 18:51:20 +01001198 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -04001199 if (priv->tx_ring[i].desc)
1200 rtl8180_free_tx_ring(dev, i);
1201
1202 return ret;
1203}
1204
1205static void rtl8180_stop(struct ieee80211_hw *dev)
1206{
1207 struct rtl8180_priv *priv = dev->priv;
1208 u8 reg;
1209 int i;
1210
Andrea Merello732c8932014-03-26 21:00:24 +01001211 rtl8180_int_disable(dev);
Michael Wuf6532112007-10-14 14:43:16 -04001212
1213 reg = rtl818x_ioread8(priv, &priv->map->CMD);
1214 reg &= ~RTL818X_CMD_TX_ENABLE;
1215 reg &= ~RTL818X_CMD_RX_ENABLE;
1216 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
1217
1218 priv->rf->stop(dev);
1219
1220 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
1221 reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
1222 rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
1223 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
1224
1225 free_irq(priv->pdev->irq, dev);
1226
1227 rtl8180_free_rx_ring(dev);
Andrea Merellofd6564f2014-03-22 18:51:20 +01001228 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -04001229 rtl8180_free_tx_ring(dev, i);
1230}
1231
Eliad Peller37a41b42011-09-21 14:06:11 +03001232static u64 rtl8180_get_tsf(struct ieee80211_hw *dev,
1233 struct ieee80211_vif *vif)
John W. Linvillec809e862010-05-06 16:49:40 -04001234{
1235 struct rtl8180_priv *priv = dev->priv;
1236
1237 return rtl818x_ioread32(priv, &priv->map->TSFT[0]) |
1238 (u64)(rtl818x_ioread32(priv, &priv->map->TSFT[1])) << 32;
1239}
1240
John W. Linvillea3275e22010-06-24 11:08:37 -04001241static void rtl8180_beacon_work(struct work_struct *work)
John W. Linvillec809e862010-05-06 16:49:40 -04001242{
1243 struct rtl8180_vif *vif_priv =
1244 container_of(work, struct rtl8180_vif, beacon_work.work);
1245 struct ieee80211_vif *vif =
1246 container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
1247 struct ieee80211_hw *dev = vif_priv->dev;
1248 struct ieee80211_mgmt *mgmt;
1249 struct sk_buff *skb;
John W. Linvillec809e862010-05-06 16:49:40 -04001250
1251 /* don't overflow the tx ring */
1252 if (ieee80211_queue_stopped(dev, 0))
1253 goto resched;
1254
1255 /* grab a fresh beacon */
1256 skb = ieee80211_beacon_get(dev, vif);
John W. Linville8f1d2d22010-08-05 13:46:27 -04001257 if (!skb)
1258 goto resched;
John W. Linvillec809e862010-05-06 16:49:40 -04001259
1260 /*
1261 * update beacon timestamp w/ TSF value
1262 * TODO: make hardware update beacon timestamp
1263 */
1264 mgmt = (struct ieee80211_mgmt *)skb->data;
Eliad Peller37a41b42011-09-21 14:06:11 +03001265 mgmt->u.beacon.timestamp = cpu_to_le64(rtl8180_get_tsf(dev, vif));
John W. Linvillec809e862010-05-06 16:49:40 -04001266
1267 /* TODO: use actual beacon queue */
1268 skb_set_queue_mapping(skb, 0);
1269
Thomas Huehn36323f82012-07-23 21:33:42 +02001270 rtl8180_tx(dev, NULL, skb);
John W. Linvillec809e862010-05-06 16:49:40 -04001271
1272resched:
1273 /*
1274 * schedule next beacon
1275 * TODO: use hardware support for beacon timing
1276 */
1277 schedule_delayed_work(&vif_priv->beacon_work,
1278 usecs_to_jiffies(1024 * vif->bss_conf.beacon_int));
1279}
1280
Michael Wuf6532112007-10-14 14:43:16 -04001281static int rtl8180_add_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001282 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -04001283{
1284 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -04001285 struct rtl8180_vif *vif_priv;
Michael Wuf6532112007-10-14 14:43:16 -04001286
John W. Linville643aab62009-12-22 18:13:04 -05001287 /*
1288 * We only support one active interface at a time.
1289 */
1290 if (priv->vif)
1291 return -EBUSY;
Michael Wuf6532112007-10-14 14:43:16 -04001292
Johannes Berg1ed32e42009-12-23 13:15:45 +01001293 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001294 case NL80211_IFTYPE_STATION:
John W. Linvillec809e862010-05-06 16:49:40 -04001295 case NL80211_IFTYPE_ADHOC:
Michael Wuf6532112007-10-14 14:43:16 -04001296 break;
1297 default:
1298 return -EOPNOTSUPP;
1299 }
1300
Johannes Berg1ed32e42009-12-23 13:15:45 +01001301 priv->vif = vif;
Johannes Berg32bfd352007-12-19 01:31:26 +01001302
John W. Linvillec809e862010-05-06 16:49:40 -04001303 /* Initialize driver private area */
1304 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
1305 vif_priv->dev = dev;
1306 INIT_DELAYED_WORK(&vif_priv->beacon_work, rtl8180_beacon_work);
1307 vif_priv->enable_beacon = false;
1308
Michael Wuf6532112007-10-14 14:43:16 -04001309 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
1310 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->MAC[0],
Johannes Berg1ed32e42009-12-23 13:15:45 +01001311 le32_to_cpu(*(__le32 *)vif->addr));
Michael Wuf6532112007-10-14 14:43:16 -04001312 rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->MAC[4],
Johannes Berg1ed32e42009-12-23 13:15:45 +01001313 le16_to_cpu(*(__le16 *)(vif->addr + 4)));
Michael Wuf6532112007-10-14 14:43:16 -04001314 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
1315
1316 return 0;
1317}
1318
1319static void rtl8180_remove_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001320 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -04001321{
1322 struct rtl8180_priv *priv = dev->priv;
Johannes Berg32bfd352007-12-19 01:31:26 +01001323 priv->vif = NULL;
Michael Wuf6532112007-10-14 14:43:16 -04001324}
1325
Johannes Berge8975582008-10-09 12:18:51 +02001326static int rtl8180_config(struct ieee80211_hw *dev, u32 changed)
Michael Wuf6532112007-10-14 14:43:16 -04001327{
1328 struct rtl8180_priv *priv = dev->priv;
Johannes Berge8975582008-10-09 12:18:51 +02001329 struct ieee80211_conf *conf = &dev->conf;
Michael Wuf6532112007-10-14 14:43:16 -04001330
1331 priv->rf->set_chan(dev, conf);
1332
1333 return 0;
1334}
1335
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001336static void rtl8187se_conf_ac_parm(struct ieee80211_hw *dev, u8 queue)
1337{
1338 const struct ieee80211_tx_queue_params *params;
1339 struct rtl8180_priv *priv = dev->priv;
1340
1341 /* hw value */
1342 u32 ac_param;
1343
1344 u8 aifs;
1345 u8 txop;
1346 u8 cw_min, cw_max;
1347
1348 params = &priv->queue_param[queue];
1349
1350 cw_min = fls(params->cw_min);
1351 cw_max = fls(params->cw_max);
1352
1353 aifs = 10 + params->aifs * priv->slot_time;
1354
1355 /* TODO: check if txop HW is in us (mult by 32) */
1356 txop = params->txop;
1357
1358 ac_param = txop << AC_PARAM_TXOP_LIMIT_SHIFT |
1359 cw_max << AC_PARAM_ECW_MAX_SHIFT |
1360 cw_min << AC_PARAM_ECW_MIN_SHIFT |
1361 aifs << AC_PARAM_AIFS_SHIFT;
1362
1363 switch (queue) {
1364 case IEEE80211_AC_BK:
1365 rtl818x_iowrite32(priv, &priv->map->AC_BK_PARAM, ac_param);
1366 break;
1367 case IEEE80211_AC_BE:
1368 rtl818x_iowrite32(priv, &priv->map->AC_BE_PARAM, ac_param);
1369 break;
1370 case IEEE80211_AC_VI:
1371 rtl818x_iowrite32(priv, &priv->map->AC_VI_PARAM, ac_param);
1372 break;
1373 case IEEE80211_AC_VO:
1374 rtl818x_iowrite32(priv, &priv->map->AC_VO_PARAM, ac_param);
1375 break;
1376 }
1377}
1378
Andrea Merello9069af72014-03-15 18:29:37 +01001379static int rtl8180_conf_tx(struct ieee80211_hw *dev,
1380 struct ieee80211_vif *vif, u16 queue,
1381 const struct ieee80211_tx_queue_params *params)
1382{
1383 struct rtl8180_priv *priv = dev->priv;
1384 u8 cw_min, cw_max;
1385
1386 /* nothing to do ? */
1387 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
1388 return 0;
1389
1390 cw_min = fls(params->cw_min);
1391 cw_max = fls(params->cw_max);
1392
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001393 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1394 priv->queue_param[queue] = *params;
1395 rtl8187se_conf_ac_parm(dev, queue);
1396 } else
1397 rtl818x_iowrite8(priv, &priv->map->CW_VAL,
1398 (cw_max << 4) | cw_min);
Andrea Merello9069af72014-03-15 18:29:37 +01001399 return 0;
1400}
1401
1402static void rtl8180_conf_erp(struct ieee80211_hw *dev,
1403 struct ieee80211_bss_conf *info)
1404{
1405 struct rtl8180_priv *priv = dev->priv;
1406 u8 sifs, difs;
1407 int eifs;
1408 u8 hw_eifs;
1409
1410 /* TODO: should we do something ? */
1411 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
1412 return;
1413
1414 /* I _hope_ this means 10uS for the HW.
1415 * In reference code it is 0x22 for
1416 * both rtl8187L and rtl8187SE
1417 */
1418 sifs = 0x22;
1419
1420 if (info->use_short_slot)
1421 priv->slot_time = 9;
1422 else
1423 priv->slot_time = 20;
1424
1425 /* 10 is SIFS time in uS */
1426 difs = 10 + 2 * priv->slot_time;
1427 eifs = 10 + difs + priv->ack_time;
1428
1429 /* HW should use 4uS units for EIFS (I'm sure for rtl8185)*/
1430 hw_eifs = DIV_ROUND_UP(eifs, 4);
1431
1432
1433 rtl818x_iowrite8(priv, &priv->map->SLOT, priv->slot_time);
1434 rtl818x_iowrite8(priv, &priv->map->SIFS, sifs);
1435 rtl818x_iowrite8(priv, &priv->map->DIFS, difs);
1436
1437 /* from reference code. set ack timeout reg = eifs reg */
1438 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, hw_eifs);
1439
Andrea Merello355668d2014-03-26 21:03:03 +01001440 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1441 rtl818x_iowrite8(priv, &priv->map->EIFS_8187SE, hw_eifs);
1442 else if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
1443 /* rtl8187/rtl8185 HW bug. After EIFS is elapsed,
1444 * the HW still wait for DIFS.
1445 * HW uses 4uS units for EIFS.
1446 */
1447 hw_eifs = DIV_ROUND_UP(eifs - difs, 4);
Andrea Merello9069af72014-03-15 18:29:37 +01001448
Andrea Merello355668d2014-03-26 21:03:03 +01001449 rtl818x_iowrite8(priv, &priv->map->EIFS, hw_eifs);
1450 }
Andrea Merello9069af72014-03-15 18:29:37 +01001451}
1452
John W. Linvilleda81ded2008-11-12 14:37:11 -05001453static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
1454 struct ieee80211_vif *vif,
1455 struct ieee80211_bss_conf *info,
1456 u32 changed)
1457{
1458 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -04001459 struct rtl8180_vif *vif_priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001460 int i;
John W. Linville0f956e72010-07-29 21:50:29 -04001461 u8 reg;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001462
John W. Linvillec809e862010-05-06 16:49:40 -04001463 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
1464
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001465 if (changed & BSS_CHANGED_BSSID) {
Andrea Merello1f622d72014-06-30 18:17:48 +02001466 rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->BSSID[0],
1467 le16_to_cpu(*(__le16 *)info->bssid));
1468 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->BSSID[2],
1469 le32_to_cpu(*(__le32 *)(info->bssid + 2)));
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001470
John W. Linville0f956e72010-07-29 21:50:29 -04001471 if (is_valid_ether_addr(info->bssid)) {
1472 if (vif->type == NL80211_IFTYPE_ADHOC)
1473 reg = RTL818X_MSR_ADHOC;
1474 else
1475 reg = RTL818X_MSR_INFRA;
1476 } else
1477 reg = RTL818X_MSR_NO_LINK;
Andrea Merello833d15a2014-03-26 21:03:17 +01001478
1479 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1480 reg |= RTL818X_MSR_ENEDCA;
1481
John W. Linville0f956e72010-07-29 21:50:29 -04001482 rtl818x_iowrite8(priv, &priv->map->MSR, reg);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001483 }
John W. Linvilleda81ded2008-11-12 14:37:11 -05001484
Andrea Merello516a0932014-03-15 18:29:36 +01001485 if (changed & BSS_CHANGED_BASIC_RATES)
1486 rtl8180_conf_basic_rates(dev, info->basic_rates);
1487
Andrea Merello9069af72014-03-15 18:29:37 +01001488 if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_ERP_PREAMBLE)) {
1489
1490 /* when preamble changes, acktime duration changes, and erp must
1491 * be recalculated. ACK time is calculated at lowest rate.
1492 * Since mac80211 include SIFS time we remove it (-10)
1493 */
1494 priv->ack_time =
1495 le16_to_cpu(ieee80211_generic_frame_duration(dev,
1496 priv->vif,
1497 IEEE80211_BAND_2GHZ, 10,
1498 &priv->rates[0])) - 10;
1499
1500 rtl8180_conf_erp(dev, info);
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001501
1502 /* mac80211 supplies aifs_n to driver and calls
1503 * conf_tx callback whether aifs_n changes, NOT
1504 * when aifs changes.
1505 * Aifs should be recalculated if slot changes.
1506 */
1507 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1508 for (i = 0; i < 4; i++)
1509 rtl8187se_conf_ac_parm(dev, i);
1510 }
Andrea Merello9069af72014-03-15 18:29:37 +01001511 }
John W. Linvillec809e862010-05-06 16:49:40 -04001512
1513 if (changed & BSS_CHANGED_BEACON_ENABLED)
1514 vif_priv->enable_beacon = info->enable_beacon;
1515
1516 if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON)) {
1517 cancel_delayed_work_sync(&vif_priv->beacon_work);
1518 if (vif_priv->enable_beacon)
1519 schedule_work(&vif_priv->beacon_work.work);
1520 }
John W. Linvilleda81ded2008-11-12 14:37:11 -05001521}
1522
Jiri Pirko22bedad32010-04-01 21:22:57 +00001523static u64 rtl8180_prepare_multicast(struct ieee80211_hw *dev,
1524 struct netdev_hw_addr_list *mc_list)
Johannes Berg3ac64be2009-08-17 16:16:53 +02001525{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001526 return netdev_hw_addr_list_count(mc_list);
Johannes Berg3ac64be2009-08-17 16:16:53 +02001527}
1528
Michael Wuf6532112007-10-14 14:43:16 -04001529static void rtl8180_configure_filter(struct ieee80211_hw *dev,
1530 unsigned int changed_flags,
1531 unsigned int *total_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001532 u64 multicast)
Michael Wuf6532112007-10-14 14:43:16 -04001533{
1534 struct rtl8180_priv *priv = dev->priv;
1535
1536 if (changed_flags & FIF_FCSFAIL)
1537 priv->rx_conf ^= RTL818X_RX_CONF_FCS;
1538 if (changed_flags & FIF_CONTROL)
1539 priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
1540 if (changed_flags & FIF_OTHER_BSS)
1541 priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
Johannes Berg3ac64be2009-08-17 16:16:53 +02001542 if (*total_flags & FIF_ALLMULTI || multicast > 0)
Michael Wuf6532112007-10-14 14:43:16 -04001543 priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
1544 else
1545 priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
1546
1547 *total_flags = 0;
1548
1549 if (priv->rx_conf & RTL818X_RX_CONF_FCS)
1550 *total_flags |= FIF_FCSFAIL;
1551 if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
1552 *total_flags |= FIF_CONTROL;
1553 if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
1554 *total_flags |= FIF_OTHER_BSS;
1555 if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
1556 *total_flags |= FIF_ALLMULTI;
1557
1558 rtl818x_iowrite32(priv, &priv->map->RX_CONF, priv->rx_conf);
1559}
1560
1561static const struct ieee80211_ops rtl8180_ops = {
1562 .tx = rtl8180_tx,
1563 .start = rtl8180_start,
1564 .stop = rtl8180_stop,
1565 .add_interface = rtl8180_add_interface,
1566 .remove_interface = rtl8180_remove_interface,
1567 .config = rtl8180_config,
John W. Linvilleda81ded2008-11-12 14:37:11 -05001568 .bss_info_changed = rtl8180_bss_info_changed,
Andrea Merello9069af72014-03-15 18:29:37 +01001569 .conf_tx = rtl8180_conf_tx,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001570 .prepare_multicast = rtl8180_prepare_multicast,
Michael Wuf6532112007-10-14 14:43:16 -04001571 .configure_filter = rtl8180_configure_filter,
John W. Linvilled2bb8e02010-01-26 16:22:20 -05001572 .get_tsf = rtl8180_get_tsf,
Michael Wuf6532112007-10-14 14:43:16 -04001573};
1574
1575static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
1576{
Andrea Merello7d4b8292014-03-15 18:29:38 +01001577 struct rtl8180_priv *priv = eeprom->data;
Michael Wuf6532112007-10-14 14:43:16 -04001578 u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1579
1580 eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
1581 eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
1582 eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
1583 eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
1584}
1585
1586static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
1587{
Andrea Merello7d4b8292014-03-15 18:29:38 +01001588 struct rtl8180_priv *priv = eeprom->data;
Michael Wuf6532112007-10-14 14:43:16 -04001589 u8 reg = 2 << 6;
1590
1591 if (eeprom->reg_data_in)
1592 reg |= RTL818X_EEPROM_CMD_WRITE;
1593 if (eeprom->reg_data_out)
1594 reg |= RTL818X_EEPROM_CMD_READ;
1595 if (eeprom->reg_data_clock)
1596 reg |= RTL818X_EEPROM_CMD_CK;
1597 if (eeprom->reg_chip_select)
1598 reg |= RTL818X_EEPROM_CMD_CS;
1599
1600 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
1601 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1602 udelay(10);
1603}
1604
Andrea Merello7d4b8292014-03-15 18:29:38 +01001605static void rtl8180_eeprom_read(struct rtl8180_priv *priv)
1606{
1607 struct eeprom_93cx6 eeprom;
1608 int eeprom_cck_table_adr;
1609 u16 eeprom_val;
1610 int i;
1611
1612 eeprom.data = priv;
1613 eeprom.register_read = rtl8180_eeprom_register_read;
1614 eeprom.register_write = rtl8180_eeprom_register_write;
1615 if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
1616 eeprom.width = PCI_EEPROM_WIDTH_93C66;
1617 else
1618 eeprom.width = PCI_EEPROM_WIDTH_93C46;
1619
1620 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
1621 RTL818X_EEPROM_CMD_PROGRAM);
1622 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1623 udelay(10);
1624
1625 eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
1626 eeprom_val &= 0xFF;
1627 priv->rf_type = eeprom_val;
1628
1629 eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
1630 priv->csthreshold = eeprom_val >> 8;
1631
1632 eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)priv->mac_addr, 3);
1633
Andrea Merellofc32ac92014-03-26 21:01:47 +01001634 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1635 eeprom_cck_table_adr = 0x30;
1636 else
1637 eeprom_cck_table_adr = 0x10;
Andrea Merello7d4b8292014-03-15 18:29:38 +01001638
1639 /* CCK TX power */
1640 for (i = 0; i < 14; i += 2) {
1641 u16 txpwr;
1642 eeprom_93cx6_read(&eeprom, eeprom_cck_table_adr + (i >> 1),
1643 &txpwr);
1644 priv->channels[i].hw_value = txpwr & 0xFF;
1645 priv->channels[i + 1].hw_value = txpwr >> 8;
1646 }
1647
1648 /* OFDM TX power */
1649 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
1650 for (i = 0; i < 14; i += 2) {
1651 u16 txpwr;
1652 eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
1653 priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
1654 priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
1655 }
1656 }
1657
1658 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
1659 __le32 anaparam;
1660 eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
1661 priv->anaparam = le32_to_cpu(anaparam);
1662 eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
1663 }
1664
Andrea Merellofc32ac92014-03-26 21:01:47 +01001665 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1666 eeprom_93cx6_read(&eeprom, 0x3F, &eeprom_val);
1667 priv->antenna_diversity_en = !!(eeprom_val & 0x100);
1668 priv->antenna_diversity_default = (eeprom_val & 0xC00) == 0x400;
1669
1670 eeprom_93cx6_read(&eeprom, 0x7C, &eeprom_val);
1671 priv->xtal_out = eeprom_val & 0xF;
1672 priv->xtal_in = (eeprom_val & 0xF0) >> 4;
1673 priv->xtal_cal = !!(eeprom_val & 0x1000);
1674 priv->thermal_meter_val = (eeprom_val & 0xF00) >> 8;
1675 priv->thermal_meter_en = !!(eeprom_val & 0x2000);
1676 }
1677
Andrea Merello7d4b8292014-03-15 18:29:38 +01001678 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
1679 RTL818X_EEPROM_CMD_NORMAL);
1680}
1681
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001682static int rtl8180_probe(struct pci_dev *pdev,
Michael Wuf6532112007-10-14 14:43:16 -04001683 const struct pci_device_id *id)
1684{
1685 struct ieee80211_hw *dev;
1686 struct rtl8180_priv *priv;
1687 unsigned long mem_addr, mem_len;
1688 unsigned int io_addr, io_len;
Andrea Merello7d4b8292014-03-15 18:29:38 +01001689 int err;
Michael Wuf6532112007-10-14 14:43:16 -04001690 const char *chip_name, *rf_name = NULL;
1691 u32 reg;
Michael Wuf6532112007-10-14 14:43:16 -04001692
1693 err = pci_enable_device(pdev);
1694 if (err) {
1695 printk(KERN_ERR "%s (rtl8180): Cannot enable new PCI device\n",
1696 pci_name(pdev));
1697 return err;
1698 }
1699
1700 err = pci_request_regions(pdev, KBUILD_MODNAME);
1701 if (err) {
1702 printk(KERN_ERR "%s (rtl8180): Cannot obtain PCI resources\n",
1703 pci_name(pdev));
1704 return err;
1705 }
1706
1707 io_addr = pci_resource_start(pdev, 0);
1708 io_len = pci_resource_len(pdev, 0);
1709 mem_addr = pci_resource_start(pdev, 1);
1710 mem_len = pci_resource_len(pdev, 1);
1711
1712 if (mem_len < sizeof(struct rtl818x_csr) ||
1713 io_len < sizeof(struct rtl818x_csr)) {
1714 printk(KERN_ERR "%s (rtl8180): Too short PCI resources\n",
1715 pci_name(pdev));
1716 err = -ENOMEM;
1717 goto err_free_reg;
1718 }
1719
John W. Linville9e385c52010-05-10 14:24:34 -04001720 if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) ||
1721 (err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))) {
Michael Wuf6532112007-10-14 14:43:16 -04001722 printk(KERN_ERR "%s (rtl8180): No suitable DMA available\n",
1723 pci_name(pdev));
1724 goto err_free_reg;
1725 }
1726
1727 pci_set_master(pdev);
1728
1729 dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8180_ops);
1730 if (!dev) {
1731 printk(KERN_ERR "%s (rtl8180): ieee80211 alloc failed\n",
1732 pci_name(pdev));
1733 err = -ENOMEM;
1734 goto err_free_reg;
1735 }
1736
1737 priv = dev->priv;
1738 priv->pdev = pdev;
1739
Johannes Berge6a98542008-10-21 12:40:02 +02001740 dev->max_rates = 2;
Michael Wuf6532112007-10-14 14:43:16 -04001741 SET_IEEE80211_DEV(dev, &pdev->dev);
1742 pci_set_drvdata(pdev, dev);
1743
1744 priv->map = pci_iomap(pdev, 1, mem_len);
1745 if (!priv->map)
1746 priv->map = pci_iomap(pdev, 0, io_len);
1747
1748 if (!priv->map) {
1749 printk(KERN_ERR "%s (rtl8180): Cannot map device memory\n",
1750 pci_name(pdev));
1751 goto err_free_dev;
1752 }
1753
Johannes Berg8318d782008-01-24 19:38:38 +01001754 BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
1755 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
1756
Michael Wuf6532112007-10-14 14:43:16 -04001757 memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
1758 memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
Johannes Berg8318d782008-01-24 19:38:38 +01001759
1760 priv->band.band = IEEE80211_BAND_2GHZ;
1761 priv->band.channels = priv->channels;
1762 priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
1763 priv->band.bitrates = priv->rates;
1764 priv->band.n_bitrates = 4;
1765 dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
1766
Michael Wuf6532112007-10-14 14:43:16 -04001767 dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
Andrea Merello70493272014-05-31 18:30:13 +02001768 IEEE80211_HW_RX_INCLUDES_FCS;
John W. Linvillec809e862010-05-06 16:49:40 -04001769 dev->vif_data_size = sizeof(struct rtl8180_vif);
1770 dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1771 BIT(NL80211_IFTYPE_ADHOC);
Bruno Randolf566bfe52008-05-08 19:15:40 +02001772 dev->max_signal = 65;
Michael Wuf6532112007-10-14 14:43:16 -04001773
1774 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
1775 reg &= RTL818X_TX_CONF_HWVER_MASK;
1776 switch (reg) {
1777 case RTL818X_TX_CONF_R8180_ABCD:
1778 chip_name = "RTL8180";
Andrea Merello6caefd12014-03-08 18:36:37 +01001779 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001780 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001781
Michael Wuf6532112007-10-14 14:43:16 -04001782 case RTL818X_TX_CONF_R8180_F:
1783 chip_name = "RTL8180vF";
Andrea Merello6caefd12014-03-08 18:36:37 +01001784 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001785 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001786
Michael Wuf6532112007-10-14 14:43:16 -04001787 case RTL818X_TX_CONF_R8185_ABC:
1788 chip_name = "RTL8185";
Andrea Merello6caefd12014-03-08 18:36:37 +01001789 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001790 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001791
Michael Wuf6532112007-10-14 14:43:16 -04001792 case RTL818X_TX_CONF_R8185_D:
1793 chip_name = "RTL8185vD";
Andrea Merello6caefd12014-03-08 18:36:37 +01001794 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001795 break;
Andrea Merello24b5fbf2014-03-26 21:04:05 +01001796
1797 case RTL818X_TX_CONF_RTL8187SE:
1798 chip_name = "RTL8187SE";
1799 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8187SE;
1800 break;
1801
Michael Wuf6532112007-10-14 14:43:16 -04001802 default:
1803 printk(KERN_ERR "%s (rtl8180): Unknown chip! (0x%x)\n",
1804 pci_name(pdev), reg >> 25);
1805 goto err_iounmap;
1806 }
1807
Andrea Merellofd6564f2014-03-22 18:51:20 +01001808 /* we declare to MAC80211 all the queues except for beacon queue
1809 * that will be eventually handled by DRV.
1810 * TX rings are arranged in such a way that lower is the IDX,
1811 * higher is the priority, in order to achieve direct mapping
1812 * with mac80211, however the beacon queue is an exception and it
1813 * is mapped on the highst tx ring IDX.
1814 */
Andrea Merello24b5fbf2014-03-26 21:04:05 +01001815 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1816 dev->queues = RTL8187SE_NR_TX_QUEUES - 1;
1817 else
1818 dev->queues = RTL8180_NR_TX_QUEUES - 1;
Andrea Merellofd6564f2014-03-22 18:51:20 +01001819
Andrea Merello6caefd12014-03-08 18:36:37 +01001820 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Johannes Berg8318d782008-01-24 19:38:38 +01001821 priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
Michael Wuf6532112007-10-14 14:43:16 -04001822 pci_try_set_mwi(pdev);
1823 }
1824
Andrea Merello70493272014-05-31 18:30:13 +02001825 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185)
1826 dev->flags |= IEEE80211_HW_SIGNAL_DBM;
1827 else
1828 dev->flags |= IEEE80211_HW_SIGNAL_UNSPEC;
1829
Andrea Merello7d4b8292014-03-15 18:29:38 +01001830 rtl8180_eeprom_read(priv);
Michael Wuf6532112007-10-14 14:43:16 -04001831
Andrea Merello7d4b8292014-03-15 18:29:38 +01001832 switch (priv->rf_type) {
Michael Wuf6532112007-10-14 14:43:16 -04001833 case 1: rf_name = "Intersil";
1834 break;
1835 case 2: rf_name = "RFMD";
1836 break;
1837 case 3: priv->rf = &sa2400_rf_ops;
1838 break;
1839 case 4: priv->rf = &max2820_rf_ops;
1840 break;
1841 case 5: priv->rf = &grf5101_rf_ops;
1842 break;
Andrea Merello24b5fbf2014-03-26 21:04:05 +01001843 case 9:
1844 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1845 priv->rf = rtl8187se_detect_rf(dev);
1846 else
1847 priv->rf = rtl8180_detect_rf(dev);
Michael Wuf6532112007-10-14 14:43:16 -04001848 break;
1849 case 10:
1850 rf_name = "RTL8255";
1851 break;
1852 default:
1853 printk(KERN_ERR "%s (rtl8180): Unknown RF! (0x%x)\n",
Andrea Merello7d4b8292014-03-15 18:29:38 +01001854 pci_name(pdev), priv->rf_type);
Michael Wuf6532112007-10-14 14:43:16 -04001855 goto err_iounmap;
1856 }
1857
1858 if (!priv->rf) {
1859 printk(KERN_ERR "%s (rtl8180): %s RF frontend not supported!\n",
1860 pci_name(pdev), rf_name);
1861 goto err_iounmap;
1862 }
1863
Andrea Merello7d4b8292014-03-15 18:29:38 +01001864 if (!is_valid_ether_addr(priv->mac_addr)) {
Michael Wuf6532112007-10-14 14:43:16 -04001865 printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
1866 " randomly generated MAC addr\n", pci_name(pdev));
Andrea Merello7d4b8292014-03-15 18:29:38 +01001867 eth_random_addr(priv->mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001868 }
Andrea Merello7d4b8292014-03-15 18:29:38 +01001869 SET_IEEE80211_PERM_ADDR(dev, priv->mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001870
1871 spin_lock_init(&priv->lock);
1872
1873 err = ieee80211_register_hw(dev);
1874 if (err) {
1875 printk(KERN_ERR "%s (rtl8180): Cannot register device\n",
1876 pci_name(pdev));
1877 goto err_iounmap;
1878 }
1879
Joe Perchesc96c31e2010-07-26 14:39:58 -07001880 wiphy_info(dev->wiphy, "hwaddr %pm, %s + %s\n",
Andrea Merello7d4b8292014-03-15 18:29:38 +01001881 priv->mac_addr, chip_name, priv->rf->name);
Michael Wuf6532112007-10-14 14:43:16 -04001882
1883 return 0;
1884
1885 err_iounmap:
andrea merello0269da22014-02-18 02:10:41 +01001886 pci_iounmap(pdev, priv->map);
Michael Wuf6532112007-10-14 14:43:16 -04001887
1888 err_free_dev:
Michael Wuf6532112007-10-14 14:43:16 -04001889 ieee80211_free_hw(dev);
1890
1891 err_free_reg:
1892 pci_release_regions(pdev);
1893 pci_disable_device(pdev);
1894 return err;
1895}
1896
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001897static void rtl8180_remove(struct pci_dev *pdev)
Michael Wuf6532112007-10-14 14:43:16 -04001898{
1899 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
1900 struct rtl8180_priv *priv;
1901
1902 if (!dev)
1903 return;
1904
1905 ieee80211_unregister_hw(dev);
1906
1907 priv = dev->priv;
1908
1909 pci_iounmap(pdev, priv->map);
1910 pci_release_regions(pdev);
1911 pci_disable_device(pdev);
1912 ieee80211_free_hw(dev);
1913}
1914
1915#ifdef CONFIG_PM
1916static int rtl8180_suspend(struct pci_dev *pdev, pm_message_t state)
1917{
1918 pci_save_state(pdev);
1919 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1920 return 0;
1921}
1922
1923static int rtl8180_resume(struct pci_dev *pdev)
1924{
1925 pci_set_power_state(pdev, PCI_D0);
1926 pci_restore_state(pdev);
1927 return 0;
1928}
1929
1930#endif /* CONFIG_PM */
1931
1932static struct pci_driver rtl8180_driver = {
1933 .name = KBUILD_MODNAME,
1934 .id_table = rtl8180_table,
1935 .probe = rtl8180_probe,
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001936 .remove = rtl8180_remove,
Michael Wuf6532112007-10-14 14:43:16 -04001937#ifdef CONFIG_PM
1938 .suspend = rtl8180_suspend,
1939 .resume = rtl8180_resume,
1940#endif /* CONFIG_PM */
1941};
1942
Axel Lin5b0a3b72012-04-14 10:38:36 +08001943module_pci_driver(rtl8180_driver);