blob: 4f4fcf8b2013050c6f562f72162bc34d9384072a [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.
Andrea Merellof82be7c2014-06-30 18:18:36 +020019 * (available in drivers/staging/rtl8187se directory of Linux 3.14)
Andrea Merello1eba6482014-03-26 21:04:23 +010020 * - other GPL, unpublished (until now), Linux driver code,
21 * Copyright Larry Finger <Larry.Finger@lwfinger.net>
22 *
23 * A huge thanks goes to Sara V. Nari who forgives me when I'm
24 * sitting in front of my laptop at evening, week-end, night...
25 *
26 * A special thanks goes to Antonio Cuni, who helped me with
27 * some python userspace stuff I used to debug RTL8187SE code, and who
28 * bought a laptop with an unsupported Wi-Fi card some years ago...
29 *
30 * Thanks to Larry Finger for writing some code for rtl8187se and for
31 * his suggestions.
32 *
33 * Thanks to Dan Carpenter for reviewing my initial patch and for his
34 * suggestions.
35 *
36 * Thanks to Bernhard Schiffner for his help in testing and for his
37 * suggestions.
38 *
39 ************************************************************************
40 *
Michael Wuf6532112007-10-14 14:43:16 -040041 * This program is free software; you can redistribute it and/or modify
42 * it under the terms of the GNU General Public License version 2 as
43 * published by the Free Software Foundation.
44 */
45
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000046#include <linux/interrupt.h>
Michael Wuf6532112007-10-14 14:43:16 -040047#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Michael Wuf6532112007-10-14 14:43:16 -040049#include <linux/delay.h>
50#include <linux/etherdevice.h>
51#include <linux/eeprom_93cx6.h>
Paul Gortmaker9d9779e2011-07-03 15:21:01 -040052#include <linux/module.h>
Michael Wuf6532112007-10-14 14:43:16 -040053#include <net/mac80211.h>
54
55#include "rtl8180.h"
John W. Linville3cfeb0c2010-12-20 15:16:53 -050056#include "rtl8225.h"
57#include "sa2400.h"
58#include "max2820.h"
59#include "grf5101.h"
Andrea Merello711d4ed32014-03-26 21:02:28 +010060#include "rtl8225se.h"
Michael Wuf6532112007-10-14 14:43:16 -040061
62MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
Andrea Merello93ba2a82013-08-26 13:53:30 +020063MODULE_AUTHOR("Andrea Merello <andrea.merello@gmail.com>");
Andrea Merello1eba6482014-03-26 21:04:23 +010064MODULE_DESCRIPTION("RTL8180 / RTL8185 / RTL8187SE PCI wireless driver");
Michael Wuf6532112007-10-14 14:43:16 -040065MODULE_LICENSE("GPL");
66
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000067static DEFINE_PCI_DEVICE_TABLE(rtl8180_table) = {
Andrea Merello1eba6482014-03-26 21:04:23 +010068
69 /* rtl8187se */
70 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8199) },
71
Michael Wuf6532112007-10-14 14:43:16 -040072 /* rtl8185 */
73 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8185) },
Adrian Bassett4fcc5472008-01-23 16:38:33 +000074 { PCI_DEVICE(PCI_VENDOR_ID_BELKIN, 0x700f) },
Michael Wuf6532112007-10-14 14:43:16 -040075 { PCI_DEVICE(PCI_VENDOR_ID_BELKIN, 0x701f) },
76
77 /* rtl8180 */
78 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8180) },
79 { PCI_DEVICE(0x1799, 0x6001) },
80 { PCI_DEVICE(0x1799, 0x6020) },
81 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x3300) },
Xose Vazquez Perez29a6b502012-06-15 17:27:05 +020082 { PCI_DEVICE(0x1186, 0x3301) },
83 { PCI_DEVICE(0x1432, 0x7106) },
Michael Wuf6532112007-10-14 14:43:16 -040084 { }
85};
86
87MODULE_DEVICE_TABLE(pci, rtl8180_table);
88
Johannes Berg8318d782008-01-24 19:38:38 +010089static const struct ieee80211_rate rtl818x_rates[] = {
90 { .bitrate = 10, .hw_value = 0, },
91 { .bitrate = 20, .hw_value = 1, },
92 { .bitrate = 55, .hw_value = 2, },
93 { .bitrate = 110, .hw_value = 3, },
94 { .bitrate = 60, .hw_value = 4, },
95 { .bitrate = 90, .hw_value = 5, },
96 { .bitrate = 120, .hw_value = 6, },
97 { .bitrate = 180, .hw_value = 7, },
98 { .bitrate = 240, .hw_value = 8, },
99 { .bitrate = 360, .hw_value = 9, },
100 { .bitrate = 480, .hw_value = 10, },
101 { .bitrate = 540, .hw_value = 11, },
102};
103
104static const struct ieee80211_channel rtl818x_channels[] = {
105 { .center_freq = 2412 },
106 { .center_freq = 2417 },
107 { .center_freq = 2422 },
108 { .center_freq = 2427 },
109 { .center_freq = 2432 },
110 { .center_freq = 2437 },
111 { .center_freq = 2442 },
112 { .center_freq = 2447 },
113 { .center_freq = 2452 },
114 { .center_freq = 2457 },
115 { .center_freq = 2462 },
116 { .center_freq = 2467 },
117 { .center_freq = 2472 },
118 { .center_freq = 2484 },
119};
120
Andrea Merello3ee44d62014-03-26 21:00:57 +0100121/* Queues for rtl8187se card
122 *
123 * name | reg | queue
124 * BC | 7 | 6
125 * MG | 1 | 0
126 * HI | 6 | 1
127 * VO | 5 | 2
128 * VI | 4 | 3
129 * BE | 3 | 4
130 * BK | 2 | 5
131 *
132 * The complete map for DMA kick reg using use all queue is:
133 * static const int rtl8187se_queues_map[RTL8187SE_NR_TX_QUEUES] =
134 * {1, 6, 5, 4, 3, 2, 7};
135 *
136 * .. but.. Because for mac80211 4 queues are enough for QoS we use this
137 *
138 * name | reg | queue
139 * BC | 7 | 4 <- currently not used yet
140 * MG | 1 | x <- Not used
141 * HI | 6 | x <- Not used
142 * VO | 5 | 0 <- used
143 * VI | 4 | 1 <- used
144 * BE | 3 | 2 <- used
145 * BK | 2 | 3 <- used
146 *
147 * Beacon queue could be used, but this is not finished yet.
148 *
149 * I thougth about using the other two queues but I decided not to do this:
150 *
151 * - I'm unsure whether the mac80211 will ever try to use more than 4 queues
152 * by itself.
153 *
154 * - I could route MGMT frames (currently sent over VO queue) to the MGMT
155 * queue but since mac80211 will do not know about it, I will probably gain
156 * some HW priority whenever the VO queue is not empty, but this gain is
157 * limited by the fact that I had to stop the mac80211 queue whenever one of
158 * the VO or MGMT queues is full, stopping also submitting of MGMT frame
159 * to the driver.
160 *
161 * - I don't know how to set in the HW the contention window params for MGMT
162 * and HI-prio queues.
163 */
164
165static const int rtl8187se_queues_map[RTL8187SE_NR_TX_QUEUES] = {5, 4, 3, 2, 7};
166
Andrea Merellofd6564f2014-03-22 18:51:20 +0100167/* Queues for rtl8180/rtl8185 cards
168 *
169 * name | reg | prio
170 * BC | 7 | 3
171 * HI | 6 | 0
172 * NO | 5 | 1
173 * LO | 4 | 2
174 *
175 * The complete map for DMA kick reg using all queue is:
176 * static const int rtl8180_queues_map[RTL8180_NR_TX_QUEUES] = {6, 5, 4, 7};
177 *
178 * .. but .. Because the mac80211 needs at least 4 queues for QoS or
179 * otherwise QoS can't be done, we use just one.
180 * Beacon queue could be used, but this is not finished yet.
181 * Actual map is:
182 *
183 * name | reg | prio
184 * BC | 7 | 1 <- currently not used yet.
185 * HI | 6 | x <- not used
186 * NO | 5 | x <- not used
187 * LO | 4 | 0 <- used
188 */
189
190static const int rtl8180_queues_map[RTL8180_NR_TX_QUEUES] = {4, 7};
Johannes Berg8318d782008-01-24 19:38:38 +0100191
Michael Wuf6532112007-10-14 14:43:16 -0400192void rtl8180_write_phy(struct ieee80211_hw *dev, u8 addr, u32 data)
193{
194 struct rtl8180_priv *priv = dev->priv;
195 int i = 10;
196 u32 buf;
197
198 buf = (data << 8) | addr;
199
200 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf | 0x80);
201 while (i--) {
202 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf);
203 if (rtl818x_ioread8(priv, &priv->map->PHY[2]) == (data & 0xFF))
204 return;
205 }
206}
207
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400208static void rtl8180_handle_rx(struct ieee80211_hw *dev)
Michael Wuf6532112007-10-14 14:43:16 -0400209{
210 struct rtl8180_priv *priv = dev->priv;
Andrea Merello21025922014-03-26 20:59:52 +0100211 struct rtl818x_rx_cmd_desc *cmd_desc;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400212 unsigned int count = 32;
Andrea Merelloafbedbf2014-05-31 18:29:46 +0200213 u8 agc, sq, signal = 1;
andrea.merello2b4db052014-02-05 22:38:05 +0100214 dma_addr_t mapping;
Michael Wuf6532112007-10-14 14:43:16 -0400215
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400216 while (count--) {
Andrea Merello21025922014-03-26 20:59:52 +0100217 void *entry = priv->rx_ring + priv->rx_idx * priv->rx_ring_sz;
Michael Wuf6532112007-10-14 14:43:16 -0400218 struct sk_buff *skb = priv->rx_buf[priv->rx_idx];
Andrea Merello21025922014-03-26 20:59:52 +0100219 u32 flags, flags2;
220 u64 tsft;
221
222 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
223 struct rtl8187se_rx_desc *desc = entry;
224
225 flags = le32_to_cpu(desc->flags);
Andrea Merello325ed9f2014-05-31 18:30:26 +0200226 /* if ownership flag is set, then we can trust the
227 * HW has written other fields. We must not trust
228 * other descriptor data read before we checked (read)
229 * the ownership flag
230 */
231 rmb();
Andrea Merello21025922014-03-26 20:59:52 +0100232 flags2 = le32_to_cpu(desc->flags2);
233 tsft = le64_to_cpu(desc->tsft);
234 } else {
235 struct rtl8180_rx_desc *desc = entry;
236
237 flags = le32_to_cpu(desc->flags);
Andrea Merello325ed9f2014-05-31 18:30:26 +0200238 /* same as above */
239 rmb();
Andrea Merello21025922014-03-26 20:59:52 +0100240 flags2 = le32_to_cpu(desc->flags2);
241 tsft = le64_to_cpu(desc->tsft);
242 }
Michael Wuf6532112007-10-14 14:43:16 -0400243
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300244 if (flags & RTL818X_RX_DESC_FLAG_OWN)
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400245 return;
Michael Wuf6532112007-10-14 14:43:16 -0400246
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300247 if (unlikely(flags & (RTL818X_RX_DESC_FLAG_DMA_FAIL |
248 RTL818X_RX_DESC_FLAG_FOF |
249 RTL818X_RX_DESC_FLAG_RX_ERR)))
Michael Wuf6532112007-10-14 14:43:16 -0400250 goto done;
251 else {
Michael Wuf6532112007-10-14 14:43:16 -0400252 struct ieee80211_rx_status rx_status = {0};
253 struct sk_buff *new_skb = dev_alloc_skb(MAX_RX_SIZE);
254
255 if (unlikely(!new_skb))
256 goto done;
257
andrea.merello2b4db052014-02-05 22:38:05 +0100258 mapping = pci_map_single(priv->pdev,
259 skb_tail_pointer(new_skb),
260 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
261
262 if (pci_dma_mapping_error(priv->pdev, mapping)) {
263 kfree_skb(new_skb);
264 dev_err(&priv->pdev->dev, "RX DMA map error\n");
265
266 goto done;
267 }
268
Michael Wuf6532112007-10-14 14:43:16 -0400269 pci_unmap_single(priv->pdev,
270 *((dma_addr_t *)skb->cb),
271 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
272 skb_put(skb, flags & 0xFFF);
273
274 rx_status.antenna = (flags2 >> 15) & 1;
Johannes Berg8318d782008-01-24 19:38:38 +0100275 rx_status.rate_idx = (flags >> 20) & 0xF;
John W. Linville8b73fb82010-07-21 16:26:40 -0400276 agc = (flags2 >> 17) & 0x7F;
Andrea Merello6caefd12014-03-08 18:36:37 +0100277
Andrea Merelloafbedbf2014-05-31 18:29:46 +0200278 switch (priv->chip_family) {
279 case RTL818X_CHIP_FAMILY_RTL8185:
John W. Linville8b73fb82010-07-21 16:26:40 -0400280 if (rx_status.rate_idx > 3)
Andrea Merello70493272014-05-31 18:30:13 +0200281 signal = -clamp_t(u8, agc, 25, 90) - 9;
John W. Linville8b73fb82010-07-21 16:26:40 -0400282 else
Andrea Merello70493272014-05-31 18:30:13 +0200283 signal = -clamp_t(u8, agc, 30, 95);
Andrea Merelloafbedbf2014-05-31 18:29:46 +0200284 break;
285 case RTL818X_CHIP_FAMILY_RTL8180:
John W. Linville8b73fb82010-07-21 16:26:40 -0400286 sq = flags2 & 0xff;
287 signal = priv->rf->calc_rssi(agc, sq);
Andrea Merelloafbedbf2014-05-31 18:29:46 +0200288 break;
289 case RTL818X_CHIP_FAMILY_RTL8187SE:
Andrea Merello21025922014-03-26 20:59:52 +0100290 /* TODO: rtl8187se rssi */
291 signal = 10;
Andrea Merelloafbedbf2014-05-31 18:29:46 +0200292 break;
John W. Linville8b73fb82010-07-21 16:26:40 -0400293 }
John W. Linville8b749642010-07-19 16:35:20 -0400294 rx_status.signal = signal;
Karl Beldan675a0b02013-03-25 16:26:57 +0100295 rx_status.freq = dev->conf.chandef.chan->center_freq;
296 rx_status.band = dev->conf.chandef.chan->band;
Andrea Merello21025922014-03-26 20:59:52 +0100297 rx_status.mactime = tsft;
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800298 rx_status.flag |= RX_FLAG_MACTIME_START;
Andrea Merelloe58342d2014-05-07 17:52:16 +0200299 if (flags & RTL818X_RX_DESC_FLAG_SPLCP)
300 rx_status.flag |= RX_FLAG_SHORTPRE;
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300301 if (flags & RTL818X_RX_DESC_FLAG_CRC32_ERR)
Michael Wuf6532112007-10-14 14:43:16 -0400302 rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
303
Johannes Bergf1d58c22009-06-17 13:13:00 +0200304 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400305 ieee80211_rx_irqsafe(dev, skb);
Michael Wuf6532112007-10-14 14:43:16 -0400306
307 skb = new_skb;
308 priv->rx_buf[priv->rx_idx] = skb;
andrea.merello2b4db052014-02-05 22:38:05 +0100309 *((dma_addr_t *) skb->cb) = mapping;
Michael Wuf6532112007-10-14 14:43:16 -0400310 }
311
312 done:
Andrea Merello21025922014-03-26 20:59:52 +0100313 cmd_desc = entry;
314 cmd_desc->rx_buf = cpu_to_le32(*((dma_addr_t *)skb->cb));
315 cmd_desc->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -0400316 MAX_RX_SIZE);
317 if (priv->rx_idx == 31)
Andrea Merello21025922014-03-26 20:59:52 +0100318 cmd_desc->flags |=
319 cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -0400320 priv->rx_idx = (priv->rx_idx + 1) % 32;
321 }
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400322}
Michael Wuf6532112007-10-14 14:43:16 -0400323
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400324static void rtl8180_handle_tx(struct ieee80211_hw *dev, unsigned int prio)
325{
326 struct rtl8180_priv *priv = dev->priv;
327 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
Michael Wuf6532112007-10-14 14:43:16 -0400328
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400329 while (skb_queue_len(&ring->queue)) {
330 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
331 struct sk_buff *skb;
332 struct ieee80211_tx_info *info;
333 u32 flags = le32_to_cpu(entry->flags);
334
335 if (flags & RTL818X_TX_DESC_FLAG_OWN)
336 return;
337
338 ring->idx = (ring->idx + 1) % ring->entries;
339 skb = __skb_dequeue(&ring->queue);
340 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
341 skb->len, PCI_DMA_TODEVICE);
342
343 info = IEEE80211_SKB_CB(skb);
344 ieee80211_tx_info_clear_status(info);
345
346 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
347 (flags & RTL818X_TX_DESC_FLAG_TX_OK))
348 info->flags |= IEEE80211_TX_STAT_ACK;
349
350 info->status.rates[0].count = (flags & 0xFF) + 1;
351 info->status.rates[1].idx = -1;
352
353 ieee80211_tx_status_irqsafe(dev, skb);
354 if (ring->entries - skb_queue_len(&ring->queue) == 2)
355 ieee80211_wake_queue(dev, prio);
Michael Wuf6532112007-10-14 14:43:16 -0400356 }
357}
358
Andrea Merelloa373ebc2014-03-26 21:00:06 +0100359static irqreturn_t rtl8187se_interrupt(int irq, void *dev_id)
360{
361 struct ieee80211_hw *dev = dev_id;
362 struct rtl8180_priv *priv = dev->priv;
363 u32 reg;
364 unsigned long flags;
365 static int desc_err;
366
367 spin_lock_irqsave(&priv->lock, flags);
368 /* Note: 32-bit interrupt status */
369 reg = rtl818x_ioread32(priv, &priv->map->INT_STATUS_SE);
370 if (unlikely(reg == 0xFFFFFFFF)) {
371 spin_unlock_irqrestore(&priv->lock, flags);
372 return IRQ_HANDLED;
373 }
374
375 rtl818x_iowrite32(priv, &priv->map->INT_STATUS_SE, reg);
376
377 if (reg & IMR_TIMEOUT1)
378 rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
379
380 if (reg & (IMR_TBDOK | IMR_TBDER))
381 rtl8180_handle_tx(dev, 4);
382
383 if (reg & (IMR_TVODOK | IMR_TVODER))
384 rtl8180_handle_tx(dev, 0);
385
386 if (reg & (IMR_TVIDOK | IMR_TVIDER))
387 rtl8180_handle_tx(dev, 1);
388
389 if (reg & (IMR_TBEDOK | IMR_TBEDER))
390 rtl8180_handle_tx(dev, 2);
391
392 if (reg & (IMR_TBKDOK | IMR_TBKDER))
393 rtl8180_handle_tx(dev, 3);
394
395 if (reg & (IMR_ROK | IMR_RER | RTL818X_INT_SE_RX_DU | IMR_RQOSOK))
396 rtl8180_handle_rx(dev);
397 /* The interface sometimes generates several RX DMA descriptor errors
398 * at startup. Do not report these.
399 */
400 if ((reg & RTL818X_INT_SE_RX_DU) && desc_err++ > 2)
401 if (net_ratelimit())
402 wiphy_err(dev->wiphy, "No RX DMA Descriptor avail\n");
403
404 spin_unlock_irqrestore(&priv->lock, flags);
405 return IRQ_HANDLED;
406}
407
Michael Wuf6532112007-10-14 14:43:16 -0400408static irqreturn_t rtl8180_interrupt(int irq, void *dev_id)
409{
410 struct ieee80211_hw *dev = dev_id;
411 struct rtl8180_priv *priv = dev->priv;
412 u16 reg;
413
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400414 spin_lock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400415 reg = rtl818x_ioread16(priv, &priv->map->INT_STATUS);
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400416 if (unlikely(reg == 0xFFFF)) {
417 spin_unlock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400418 return IRQ_HANDLED;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400419 }
Michael Wuf6532112007-10-14 14:43:16 -0400420
421 rtl818x_iowrite16(priv, &priv->map->INT_STATUS, reg);
422
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400423 if (reg & (RTL818X_INT_TXB_OK | RTL818X_INT_TXB_ERR))
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400424 rtl8180_handle_tx(dev, 1);
425
426 if (reg & (RTL818X_INT_TXL_OK | RTL818X_INT_TXL_ERR))
427 rtl8180_handle_tx(dev, 0);
428
429 if (reg & (RTL818X_INT_RX_OK | RTL818X_INT_RX_ERR))
430 rtl8180_handle_rx(dev);
431
432 spin_unlock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400433
434 return IRQ_HANDLED;
435}
436
Thomas Huehn36323f82012-07-23 21:33:42 +0200437static void rtl8180_tx(struct ieee80211_hw *dev,
438 struct ieee80211_tx_control *control,
439 struct sk_buff *skb)
Michael Wuf6532112007-10-14 14:43:16 -0400440{
Johannes Berge039fa42008-05-15 12:55:29 +0200441 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
John W. Linville51e080d2010-05-06 16:26:23 -0400442 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Michael Wuf6532112007-10-14 14:43:16 -0400443 struct rtl8180_priv *priv = dev->priv;
444 struct rtl8180_tx_ring *ring;
445 struct rtl8180_tx_desc *entry;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400446 unsigned long flags;
Andrea Merellofd6564f2014-03-22 18:51:20 +0100447 unsigned int idx, prio, hw_prio;
Michael Wuf6532112007-10-14 14:43:16 -0400448 dma_addr_t mapping;
449 u32 tx_flags;
Johannes Berge6a98542008-10-21 12:40:02 +0200450 u8 rc_flags;
Michael Wuf6532112007-10-14 14:43:16 -0400451 u16 plcp_len = 0;
452 __le16 rts_duration = 0;
Andrea Merello3ee44d62014-03-26 21:00:57 +0100453 /* do arithmetic and then convert to le16 */
454 u16 frame_duration = 0;
Michael Wuf6532112007-10-14 14:43:16 -0400455
Johannes Berge2530082008-05-17 00:57:14 +0200456 prio = skb_get_queue_mapping(skb);
Michael Wuf6532112007-10-14 14:43:16 -0400457 ring = &priv->tx_ring[prio];
458
459 mapping = pci_map_single(priv->pdev, skb->data,
460 skb->len, PCI_DMA_TODEVICE);
461
andrea.merello348f7d42014-02-05 22:38:06 +0100462 if (pci_dma_mapping_error(priv->pdev, mapping)) {
463 kfree_skb(skb);
464 dev_err(&priv->pdev->dev, "TX DMA mapping error\n");
465 return;
andrea.merello348f7d42014-02-05 22:38:06 +0100466 }
467
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300468 tx_flags = RTL818X_TX_DESC_FLAG_OWN | RTL818X_TX_DESC_FLAG_FS |
469 RTL818X_TX_DESC_FLAG_LS |
Johannes Berge039fa42008-05-15 12:55:29 +0200470 (ieee80211_get_tx_rate(dev, info)->hw_value << 24) |
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200471 skb->len;
Michael Wuf6532112007-10-14 14:43:16 -0400472
Andrea Merello6caefd12014-03-08 18:36:37 +0100473 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300474 tx_flags |= RTL818X_TX_DESC_FLAG_DMA |
475 RTL818X_TX_DESC_FLAG_NO_ENC;
Michael Wuf6532112007-10-14 14:43:16 -0400476
Johannes Berge6a98542008-10-21 12:40:02 +0200477 rc_flags = info->control.rates[0].flags;
Andrea Merello5d0d1e92014-05-07 17:53:17 +0200478
479 /* HW will perform RTS-CTS when only RTS flags is set.
480 * HW will perform CTS-to-self when both RTS and CTS flags are set.
481 * RTS rate and RTS duration will be used also for CTS-to-self.
482 */
Johannes Berge6a98542008-10-21 12:40:02 +0200483 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300484 tx_flags |= RTL818X_TX_DESC_FLAG_RTS;
Johannes Berge039fa42008-05-15 12:55:29 +0200485 tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
Andrea Merello5d0d1e92014-05-07 17:53:17 +0200486 rts_duration = ieee80211_rts_duration(dev, priv->vif,
487 skb->len, info);
Johannes Berge6a98542008-10-21 12:40:02 +0200488 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Andrea Merello5d0d1e92014-05-07 17:53:17 +0200489 tx_flags |= RTL818X_TX_DESC_FLAG_RTS | RTL818X_TX_DESC_FLAG_CTS;
Johannes Berge039fa42008-05-15 12:55:29 +0200490 tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
Andrea Merello5d0d1e92014-05-07 17:53:17 +0200491 rts_duration = ieee80211_ctstoself_duration(dev, priv->vif,
492 skb->len, info);
Johannes Bergaa68cbf2008-02-18 14:20:30 +0100493 }
Michael Wuf6532112007-10-14 14:43:16 -0400494
Andrea Merello6caefd12014-03-08 18:36:37 +0100495 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400496 unsigned int remainder;
497
498 plcp_len = DIV_ROUND_UP(16 * (skb->len + 4),
Johannes Berge039fa42008-05-15 12:55:29 +0200499 (ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
Michael Wuf6532112007-10-14 14:43:16 -0400500 remainder = (16 * (skb->len + 4)) %
Johannes Berge039fa42008-05-15 12:55:29 +0200501 ((ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
Roel Kluin35a0ace2009-06-22 17:42:21 +0200502 if (remainder <= 6)
Michael Wuf6532112007-10-14 14:43:16 -0400503 plcp_len |= 1 << 15;
504 }
505
Andrea Merello3ee44d62014-03-26 21:00:57 +0100506 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
507 __le16 duration;
508 /* SIFS time (required by HW) is already included by
509 * ieee80211_generic_frame_duration
510 */
511 duration = ieee80211_generic_frame_duration(dev, priv->vif,
512 IEEE80211_BAND_2GHZ, skb->len,
513 ieee80211_get_tx_rate(dev, info));
514
515 frame_duration = priv->ack_time + le16_to_cpu(duration);
516 }
517
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400518 spin_lock_irqsave(&priv->lock, flags);
John W. Linville51e080d2010-05-06 16:26:23 -0400519
520 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
521 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
522 priv->seqno += 0x10;
523 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
524 hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
525 }
526
Michael Wuf6532112007-10-14 14:43:16 -0400527 idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
528 entry = &ring->desc[idx];
529
Andrea Merello3ee44d62014-03-26 21:00:57 +0100530 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
531 entry->frame_duration = cpu_to_le16(frame_duration);
532 entry->frame_len_se = cpu_to_le16(skb->len);
533
534 /* tpc polarity */
535 entry->flags3 = cpu_to_le16(1<<4);
536 } else
537 entry->frame_len = cpu_to_le32(skb->len);
538
Michael Wuf6532112007-10-14 14:43:16 -0400539 entry->rts_duration = rts_duration;
540 entry->plcp_len = cpu_to_le16(plcp_len);
541 entry->tx_buf = cpu_to_le32(mapping);
Andrea Merello3ee44d62014-03-26 21:00:57 +0100542
Johannes Berge6a98542008-10-21 12:40:02 +0200543 entry->flags2 = info->control.rates[1].idx >= 0 ?
Felix Fietkau870abdf2008-10-05 18:04:24 +0200544 ieee80211_get_alt_retry_rate(dev, info, 0)->bitrate << 4 : 0;
Andrea Merello81129fc2014-06-30 18:18:55 +0200545 entry->retry_limit = info->control.rates[0].count - 1;
andrea merello4c552a52014-02-18 02:10:45 +0100546
547 /* We must be sure that tx_flags is written last because the HW
548 * looks at it to check if the rest of data is valid or not
549 */
550 wmb();
Michael Wuf6532112007-10-14 14:43:16 -0400551 entry->flags = cpu_to_le32(tx_flags);
andrea merelloc24782e2014-02-18 02:10:46 +0100552 /* We must be sure this has been written before followings HW
553 * register write, because this write will made the HW attempts
554 * to DMA the just-written data
555 */
556 wmb();
557
Michael Wuf6532112007-10-14 14:43:16 -0400558 __skb_queue_tail(&ring->queue, skb);
559 if (ring->entries - skb_queue_len(&ring->queue) < 2)
John W. Linvilled10e2e02010-04-27 16:57:38 -0400560 ieee80211_stop_queue(dev, prio);
John W. Linville51e080d2010-05-06 16:26:23 -0400561
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400562 spin_unlock_irqrestore(&priv->lock, flags);
Michael Wuf6532112007-10-14 14:43:16 -0400563
Andrea Merello3ee44d62014-03-26 21:00:57 +0100564 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
565 /* just poll: rings are stopped with TPPollStop reg */
566 hw_prio = rtl8187se_queues_map[prio];
567 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
568 (1 << hw_prio));
569 } else {
570 hw_prio = rtl8180_queues_map[prio];
571 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
Andrea Merellofd6564f2014-03-22 18:51:20 +0100572 (1 << hw_prio) | /* ring to poll */
573 (1<<1) | (1<<2));/* stopped rings */
Andrea Merello3ee44d62014-03-26 21:00:57 +0100574 }
Michael Wuf6532112007-10-14 14:43:16 -0400575}
576
Andrea Merelloff3cbc22014-03-26 21:02:05 +0100577static void rtl8180_set_anaparam3(struct rtl8180_priv *priv, u16 anaparam3)
578{
579 u8 reg;
580
581 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
582 RTL818X_EEPROM_CMD_CONFIG);
583
584 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
585 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
586 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
587
588 rtl818x_iowrite16(priv, &priv->map->ANAPARAM3, anaparam3);
589
590 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
591 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
592
593 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
594 RTL818X_EEPROM_CMD_NORMAL);
595}
596
597void rtl8180_set_anaparam2(struct rtl8180_priv *priv, u32 anaparam2)
598{
599 u8 reg;
600
601 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
602 RTL818X_EEPROM_CMD_CONFIG);
603
604 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
605 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
606 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
607
608 rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, anaparam2);
609
610 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
611 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
612
613 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
614 RTL818X_EEPROM_CMD_NORMAL);
615}
616
Michael Wuf6532112007-10-14 14:43:16 -0400617void rtl8180_set_anaparam(struct rtl8180_priv *priv, u32 anaparam)
618{
619 u8 reg;
620
621 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
622 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
623 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
624 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
625 rtl818x_iowrite32(priv, &priv->map->ANAPARAM, anaparam);
626 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
627 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
628 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
629}
630
Andrea Merello4a67aa52014-03-26 21:02:46 +0100631static void rtl8187se_mac_config(struct ieee80211_hw *dev)
632{
633 struct rtl8180_priv *priv = dev->priv;
634 u8 reg;
635
636 rtl818x_iowrite32(priv, REG_ADDR4(0x1F0), 0);
637 rtl818x_ioread32(priv, REG_ADDR4(0x1F0));
638 rtl818x_iowrite32(priv, REG_ADDR4(0x1F4), 0);
639 rtl818x_ioread32(priv, REG_ADDR4(0x1F4));
640 rtl818x_iowrite8(priv, REG_ADDR1(0x1F8), 0);
641 rtl818x_ioread8(priv, REG_ADDR1(0x1F8));
642 /* Enable DA10 TX power saving */
643 reg = rtl818x_ioread8(priv, &priv->map->PHY_PR);
644 rtl818x_iowrite8(priv, &priv->map->PHY_PR, reg | 0x04);
645 /* Power */
646 rtl818x_iowrite16(priv, PI_DATA_REG, 0x1000);
647 rtl818x_iowrite16(priv, SI_DATA_REG, 0x1000);
648 /* AFE - default to power ON */
649 rtl818x_iowrite16(priv, REG_ADDR2(0x370), 0x0560);
650 rtl818x_iowrite16(priv, REG_ADDR2(0x372), 0x0560);
651 rtl818x_iowrite16(priv, REG_ADDR2(0x374), 0x0DA4);
652 rtl818x_iowrite16(priv, REG_ADDR2(0x376), 0x0DA4);
653 rtl818x_iowrite16(priv, REG_ADDR2(0x378), 0x0560);
654 rtl818x_iowrite16(priv, REG_ADDR2(0x37A), 0x0560);
655 rtl818x_iowrite16(priv, REG_ADDR2(0x37C), 0x00EC);
656 rtl818x_iowrite16(priv, REG_ADDR2(0x37E), 0x00EC);
657 rtl818x_iowrite8(priv, REG_ADDR1(0x24E), 0x01);
658 /* unknown, needed for suspend to RAM resume */
659 rtl818x_iowrite8(priv, REG_ADDR1(0x0A), 0x72);
660}
661
662static void rtl8187se_set_antenna_config(struct ieee80211_hw *dev, u8 def_ant,
663 bool diversity)
664{
665 struct rtl8180_priv *priv = dev->priv;
666
667 rtl8225_write_phy_cck(dev, 0x0C, 0x09);
668 if (diversity) {
669 if (def_ant == 1) {
670 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x00);
671 rtl8225_write_phy_cck(dev, 0x11, 0xBB);
672 rtl8225_write_phy_cck(dev, 0x01, 0xC7);
673 rtl8225_write_phy_ofdm(dev, 0x0D, 0x54);
674 rtl8225_write_phy_ofdm(dev, 0x18, 0xB2);
675 } else { /* main antenna */
676 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x03);
677 rtl8225_write_phy_cck(dev, 0x11, 0x9B);
678 rtl8225_write_phy_cck(dev, 0x01, 0xC7);
679 rtl8225_write_phy_ofdm(dev, 0x0D, 0x5C);
680 rtl8225_write_phy_ofdm(dev, 0x18, 0xB2);
681 }
682 } else { /* disable antenna diversity */
683 if (def_ant == 1) {
684 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x00);
685 rtl8225_write_phy_cck(dev, 0x11, 0xBB);
686 rtl8225_write_phy_cck(dev, 0x01, 0x47);
687 rtl8225_write_phy_ofdm(dev, 0x0D, 0x54);
688 rtl8225_write_phy_ofdm(dev, 0x18, 0x32);
689 } else { /* main antenna */
690 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x03);
691 rtl8225_write_phy_cck(dev, 0x11, 0x9B);
692 rtl8225_write_phy_cck(dev, 0x01, 0x47);
693 rtl8225_write_phy_ofdm(dev, 0x0D, 0x5C);
694 rtl8225_write_phy_ofdm(dev, 0x18, 0x32);
695 }
696 }
697 /* priv->curr_ant = def_ant; */
698}
699
Andrea Merello732c8932014-03-26 21:00:24 +0100700static void rtl8180_int_enable(struct ieee80211_hw *dev)
701{
702 struct rtl8180_priv *priv = dev->priv;
703
704 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
Andrea Merello1c3fb9b2014-04-04 18:21:14 +0200705 rtl818x_iowrite32(priv, &priv->map->IMR,
706 IMR_TBDER | IMR_TBDOK |
Andrea Merello732c8932014-03-26 21:00:24 +0100707 IMR_TVODER | IMR_TVODOK |
708 IMR_TVIDER | IMR_TVIDOK |
709 IMR_TBEDER | IMR_TBEDOK |
710 IMR_TBKDER | IMR_TBKDOK |
711 IMR_RDU | IMR_RER |
712 IMR_ROK | IMR_RQOSOK);
713 } else {
714 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
715 }
716}
717
718static void rtl8180_int_disable(struct ieee80211_hw *dev)
719{
720 struct rtl8180_priv *priv = dev->priv;
721
722 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
723 rtl818x_iowrite32(priv, &priv->map->IMR, 0);
724 } else {
725 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
726 }
727}
728
Andrea Merello516a0932014-03-15 18:29:36 +0100729static void rtl8180_conf_basic_rates(struct ieee80211_hw *dev,
730 u32 rates_mask)
731{
732 struct rtl8180_priv *priv = dev->priv;
733
734 u8 max, min;
735 u16 reg;
736
737 max = fls(rates_mask) - 1;
738 min = ffs(rates_mask) - 1;
739
740 switch (priv->chip_family) {
741
742 case RTL818X_CHIP_FAMILY_RTL8180:
743 /* in 8180 this is NOT a BITMAP */
744 reg = rtl818x_ioread16(priv, &priv->map->BRSR);
745 reg &= ~3;
746 reg |= max;
747 rtl818x_iowrite16(priv, &priv->map->BRSR, reg);
Andrea Merello516a0932014-03-15 18:29:36 +0100748 break;
749
750 case RTL818X_CHIP_FAMILY_RTL8185:
751 /* in 8185 this is a BITMAP */
752 rtl818x_iowrite16(priv, &priv->map->BRSR, rates_mask);
753 rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (max << 4) | min);
754 break;
Andrea Merellod209f3b2014-03-26 20:59:25 +0100755
756 case RTL818X_CHIP_FAMILY_RTL8187SE:
757 /* in 8187se this is a BITMAP */
758 rtl818x_iowrite16(priv, &priv->map->BRSR_8187SE, rates_mask);
759 break;
Andrea Merello516a0932014-03-15 18:29:36 +0100760 }
761}
762
Andrea Merellof1026df2014-03-26 21:01:19 +0100763static void rtl8180_config_cardbus(struct ieee80211_hw *dev)
764{
765 struct rtl8180_priv *priv = dev->priv;
766 u16 reg16;
767 u8 reg8;
768
769 reg8 = rtl818x_ioread8(priv, &priv->map->CONFIG3);
770 reg8 |= 1 << 1;
771 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg8);
772
773 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
774 rtl818x_iowrite16(priv, FEMR_SE, 0xffff);
775 } else {
776 reg16 = rtl818x_ioread16(priv, &priv->map->FEMR);
777 reg16 |= (1 << 15) | (1 << 14) | (1 << 4);
778 rtl818x_iowrite16(priv, &priv->map->FEMR, reg16);
779 }
780
781}
782
Michael Wuf6532112007-10-14 14:43:16 -0400783static int rtl8180_init_hw(struct ieee80211_hw *dev)
784{
785 struct rtl8180_priv *priv = dev->priv;
786 u16 reg;
Andrea Merello4a67aa52014-03-26 21:02:46 +0100787 u32 reg32;
Michael Wuf6532112007-10-14 14:43:16 -0400788
789 rtl818x_iowrite8(priv, &priv->map->CMD, 0);
790 rtl818x_ioread8(priv, &priv->map->CMD);
791 msleep(10);
792
793 /* reset */
Andrea Merello732c8932014-03-26 21:00:24 +0100794 rtl8180_int_disable(dev);
Michael Wuf6532112007-10-14 14:43:16 -0400795 rtl818x_ioread8(priv, &priv->map->CMD);
796
797 reg = rtl818x_ioread8(priv, &priv->map->CMD);
798 reg &= (1 << 1);
799 reg |= RTL818X_CMD_RESET;
800 rtl818x_iowrite8(priv, &priv->map->CMD, RTL818X_CMD_RESET);
801 rtl818x_ioread8(priv, &priv->map->CMD);
802 msleep(200);
803
804 /* check success of reset */
805 if (rtl818x_ioread8(priv, &priv->map->CMD) & RTL818X_CMD_RESET) {
Joe Perchesc96c31e2010-07-26 14:39:58 -0700806 wiphy_err(dev->wiphy, "reset timeout!\n");
Michael Wuf6532112007-10-14 14:43:16 -0400807 return -ETIMEDOUT;
808 }
809
810 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
811 rtl818x_ioread8(priv, &priv->map->CMD);
812 msleep(200);
813
814 if (rtl818x_ioread8(priv, &priv->map->CONFIG3) & (1 << 3)) {
Andrea Merellof1026df2014-03-26 21:01:19 +0100815 rtl8180_config_cardbus(dev);
Michael Wuf6532112007-10-14 14:43:16 -0400816 }
817
Andrea Merello4a67aa52014-03-26 21:02:46 +0100818 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
819 rtl818x_iowrite8(priv, &priv->map->MSR, RTL818X_MSR_ENEDCA);
820 else
821 rtl818x_iowrite8(priv, &priv->map->MSR, 0);
Michael Wuf6532112007-10-14 14:43:16 -0400822
Andrea Merello6caefd12014-03-08 18:36:37 +0100823 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -0400824 rtl8180_set_anaparam(priv, priv->anaparam);
825
826 rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
Andrea Merello4a67aa52014-03-26 21:02:46 +0100827 /* mac80211 queue have higher prio for lower index. The last queue
828 * (that mac80211 is not aware of) is reserved for beacons (and have
829 * the highest priority on the NIC)
830 */
831 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8187SE) {
832 rtl818x_iowrite32(priv, &priv->map->TBDA,
833 priv->tx_ring[1].dma);
834 rtl818x_iowrite32(priv, &priv->map->TLPDA,
835 priv->tx_ring[0].dma);
836 } else {
837 rtl818x_iowrite32(priv, &priv->map->TBDA,
838 priv->tx_ring[4].dma);
839 rtl818x_iowrite32(priv, &priv->map->TVODA,
840 priv->tx_ring[0].dma);
841 rtl818x_iowrite32(priv, &priv->map->TVIDA,
842 priv->tx_ring[1].dma);
843 rtl818x_iowrite32(priv, &priv->map->TBEDA,
844 priv->tx_ring[2].dma);
845 rtl818x_iowrite32(priv, &priv->map->TBKDA,
846 priv->tx_ring[3].dma);
847 }
Michael Wuf6532112007-10-14 14:43:16 -0400848
849 /* TODO: necessary? specs indicate not */
850 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
851 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
852 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
Andrea Merello6caefd12014-03-08 18:36:37 +0100853 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
Michael Wuf6532112007-10-14 14:43:16 -0400854 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
855 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
856 }
857 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
858
859 /* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */
860
861 /* TODO: turn off hw wep on rtl8180 */
862
863 rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
864
Andrea Merello6caefd12014-03-08 18:36:37 +0100865 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400866 rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
867 rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
Andrea Merello4a67aa52014-03-26 21:02:46 +0100868 } else {
869 rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);
Michael Wuf6532112007-10-14 14:43:16 -0400870
Andrea Merello4a67aa52014-03-26 21:02:46 +0100871 rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
872 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, 0x4C);
873 }
874
875 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
Michael Wuf6532112007-10-14 14:43:16 -0400876 /* TODO: set ClkRun enable? necessary? */
877 reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
878 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, reg & ~(1 << 6));
879 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
880 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
881 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
882 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
Andrea Merello7df00722014-06-30 18:18:25 +0200883 /* fix eccessive IFS after CTS-to-self */
884 rtl818x_iowrite8(priv, REG_ADDR1(0x1ff), 0x35);
Andrea Merello4a67aa52014-03-26 21:02:46 +0100885 }
Michael Wuf6532112007-10-14 14:43:16 -0400886
Andrea Merello4a67aa52014-03-26 21:02:46 +0100887 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
888
889 /* the set auto rate fallback bitmask from 1M to 54 Mb/s */
890 rtl818x_iowrite16(priv, ARFR, 0xFFF);
891 rtl818x_ioread16(priv, ARFR);
892
893 /* stop unused queus (no dma alloc) */
894 rtl818x_iowrite8(priv, &priv->map->TPPOLL_STOP,
895 RTL818x_TPPOLL_STOP_MG | RTL818x_TPPOLL_STOP_HI);
896
897 rtl818x_iowrite8(priv, &priv->map->ACM_CONTROL, 0x00);
898 rtl818x_iowrite16(priv, &priv->map->TID_AC_MAP, 0xFA50);
899
900 rtl818x_iowrite16(priv, &priv->map->INT_MIG, 0);
901
902 /* some black magic here.. */
903 rtl8187se_mac_config(dev);
904
905 rtl818x_iowrite16(priv, RFSW_CTRL, 0x569A);
906 rtl818x_ioread16(priv, RFSW_CTRL);
907
908 rtl8180_set_anaparam(priv, RTL8225SE_ANAPARAM_ON);
909 rtl8180_set_anaparam2(priv, RTL8225SE_ANAPARAM2_ON);
910 rtl8180_set_anaparam3(priv, RTL8225SE_ANAPARAM3);
911
912
913 rtl818x_iowrite8(priv, &priv->map->CONFIG5,
914 rtl818x_ioread8(priv, &priv->map->CONFIG5) & 0x7F);
915
916 /*probably this switch led on */
917 rtl818x_iowrite8(priv, &priv->map->PGSELECT,
918 rtl818x_ioread8(priv, &priv->map->PGSELECT) | 0x08);
919
920 rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x0480);
921 rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1BFF);
922 rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x2488);
923
924 rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x4003);
925
926 /* the reference code mac hardcode table write
927 * this reg by doing byte-wide accesses.
928 * It does it just for lowest and highest byte..
929 */
930 reg32 = rtl818x_ioread32(priv, &priv->map->RF_PARA);
931 reg32 &= 0x00ffff00;
932 reg32 |= 0xb8000054;
933 rtl818x_iowrite32(priv, &priv->map->RF_PARA, reg32);
Andrea Merello294bc612014-04-04 18:25:51 +0200934 } else
935 /* stop unused queus (no dma alloc) */
936 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
937 (1<<1) | (1<<2));
Michael Wuf6532112007-10-14 14:43:16 -0400938
939 priv->rf->init(dev);
Andrea Merello516a0932014-03-15 18:29:36 +0100940
941 /* default basic rates are 1,2 Mbps for rtl8180. 1,2,6,9,12,18,24 Mbps
942 * otherwise. bitmask 0x3 and 0x01f3 respectively.
943 * NOTE: currenty rtl8225 RF code changes basic rates, so we need to do
944 * this after rf init.
945 * TODO: try to find out whether RF code really needs to do this..
946 */
947 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
948 rtl8180_conf_basic_rates(dev, 0x3);
949 else
950 rtl8180_conf_basic_rates(dev, 0x1f3);
951
Andrea Merello4a67aa52014-03-26 21:02:46 +0100952 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
953 rtl8187se_set_antenna_config(dev,
954 priv->antenna_diversity_default,
955 priv->antenna_diversity_en);
Michael Wuf6532112007-10-14 14:43:16 -0400956 return 0;
957}
958
959static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
960{
961 struct rtl8180_priv *priv = dev->priv;
Andrea Merello21025922014-03-26 20:59:52 +0100962 struct rtl818x_rx_cmd_desc *entry;
Michael Wuf6532112007-10-14 14:43:16 -0400963 int i;
964
Andrea Merello21025922014-03-26 20:59:52 +0100965 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
966 priv->rx_ring_sz = sizeof(struct rtl8187se_rx_desc);
967 else
968 priv->rx_ring_sz = sizeof(struct rtl8180_rx_desc);
969
Michael Wuf6532112007-10-14 14:43:16 -0400970 priv->rx_ring = pci_alloc_consistent(priv->pdev,
Andrea Merello21025922014-03-26 20:59:52 +0100971 priv->rx_ring_sz * 32,
Michael Wuf6532112007-10-14 14:43:16 -0400972 &priv->rx_ring_dma);
973
974 if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -0700975 wiphy_err(dev->wiphy, "Cannot allocate RX ring\n");
Michael Wuf6532112007-10-14 14:43:16 -0400976 return -ENOMEM;
977 }
978
Andrea Merello21025922014-03-26 20:59:52 +0100979 memset(priv->rx_ring, 0, priv->rx_ring_sz * 32);
Michael Wuf6532112007-10-14 14:43:16 -0400980 priv->rx_idx = 0;
981
982 for (i = 0; i < 32; i++) {
983 struct sk_buff *skb = dev_alloc_skb(MAX_RX_SIZE);
984 dma_addr_t *mapping;
Andrea Merello21025922014-03-26 20:59:52 +0100985 entry = priv->rx_ring + priv->rx_ring_sz*i;
andrea merello4da18bb2014-02-18 02:10:43 +0100986 if (!skb) {
987 wiphy_err(dev->wiphy, "Cannot allocate RX skb\n");
988 return -ENOMEM;
989 }
Michael Wuf6532112007-10-14 14:43:16 -0400990 priv->rx_buf[i] = skb;
991 mapping = (dma_addr_t *)skb->cb;
992 *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
993 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
andrea merelloec1da082014-02-22 17:57:23 +0100994
995 if (pci_dma_mapping_error(priv->pdev, *mapping)) {
996 kfree_skb(skb);
997 wiphy_err(dev->wiphy, "Cannot map DMA for RX skb\n");
998 return -ENOMEM;
999 }
1000
Michael Wuf6532112007-10-14 14:43:16 -04001001 entry->rx_buf = cpu_to_le32(*mapping);
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -03001002 entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -04001003 MAX_RX_SIZE);
1004 }
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -03001005 entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -04001006 return 0;
1007}
1008
1009static void rtl8180_free_rx_ring(struct ieee80211_hw *dev)
1010{
1011 struct rtl8180_priv *priv = dev->priv;
1012 int i;
1013
1014 for (i = 0; i < 32; i++) {
1015 struct sk_buff *skb = priv->rx_buf[i];
1016 if (!skb)
1017 continue;
1018
1019 pci_unmap_single(priv->pdev,
1020 *((dma_addr_t *)skb->cb),
1021 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
1022 kfree_skb(skb);
1023 }
1024
Andrea Merello21025922014-03-26 20:59:52 +01001025 pci_free_consistent(priv->pdev, priv->rx_ring_sz * 32,
Michael Wuf6532112007-10-14 14:43:16 -04001026 priv->rx_ring, priv->rx_ring_dma);
1027 priv->rx_ring = NULL;
1028}
1029
1030static int rtl8180_init_tx_ring(struct ieee80211_hw *dev,
1031 unsigned int prio, unsigned int entries)
1032{
1033 struct rtl8180_priv *priv = dev->priv;
1034 struct rtl8180_tx_desc *ring;
1035 dma_addr_t dma;
1036 int i;
1037
1038 ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
1039 if (!ring || (unsigned long)ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -07001040 wiphy_err(dev->wiphy, "Cannot allocate TX ring (prio = %d)\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001041 prio);
Michael Wuf6532112007-10-14 14:43:16 -04001042 return -ENOMEM;
1043 }
1044
1045 memset(ring, 0, sizeof(*ring)*entries);
1046 priv->tx_ring[prio].desc = ring;
1047 priv->tx_ring[prio].dma = dma;
1048 priv->tx_ring[prio].idx = 0;
1049 priv->tx_ring[prio].entries = entries;
1050 skb_queue_head_init(&priv->tx_ring[prio].queue);
1051
1052 for (i = 0; i < entries; i++)
1053 ring[i].next_tx_desc =
1054 cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
1055
1056 return 0;
1057}
1058
1059static void rtl8180_free_tx_ring(struct ieee80211_hw *dev, unsigned int prio)
1060{
1061 struct rtl8180_priv *priv = dev->priv;
1062 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
1063
1064 while (skb_queue_len(&ring->queue)) {
1065 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
1066 struct sk_buff *skb = __skb_dequeue(&ring->queue);
1067
1068 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
1069 skb->len, PCI_DMA_TODEVICE);
Michael Wuf6532112007-10-14 14:43:16 -04001070 kfree_skb(skb);
1071 ring->idx = (ring->idx + 1) % ring->entries;
1072 }
1073
1074 pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
1075 ring->desc, ring->dma);
1076 ring->desc = NULL;
1077}
1078
1079static int rtl8180_start(struct ieee80211_hw *dev)
1080{
1081 struct rtl8180_priv *priv = dev->priv;
1082 int ret, i;
1083 u32 reg;
1084
1085 ret = rtl8180_init_rx_ring(dev);
1086 if (ret)
1087 return ret;
1088
Andrea Merellofd6564f2014-03-22 18:51:20 +01001089 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -04001090 if ((ret = rtl8180_init_tx_ring(dev, i, 16)))
1091 goto err_free_rings;
1092
1093 ret = rtl8180_init_hw(dev);
1094 if (ret)
1095 goto err_free_rings;
1096
Andrea Merelloa373ebc2014-03-26 21:00:06 +01001097 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1098 ret = request_irq(priv->pdev->irq, rtl8187se_interrupt,
Michael Wuf6532112007-10-14 14:43:16 -04001099 IRQF_SHARED, KBUILD_MODNAME, dev);
Andrea Merelloa373ebc2014-03-26 21:00:06 +01001100 } else {
1101 ret = request_irq(priv->pdev->irq, rtl8180_interrupt,
1102 IRQF_SHARED, KBUILD_MODNAME, dev);
1103 }
1104
Michael Wuf6532112007-10-14 14:43:16 -04001105 if (ret) {
Joe Perches5db55842010-08-11 19:11:19 -07001106 wiphy_err(dev->wiphy, "failed to register IRQ handler\n");
Michael Wuf6532112007-10-14 14:43:16 -04001107 goto err_free_rings;
1108 }
1109
Andrea Merello732c8932014-03-26 21:00:24 +01001110 rtl8180_int_enable(dev);
Michael Wuf6532112007-10-14 14:43:16 -04001111
Andrea Merellof18f1122014-03-26 21:00:42 +01001112 /* in rtl8187se at MAR regs offset there is the management
1113 * TX descriptor DMA addres..
1114 */
1115 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8187SE) {
1116 rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
1117 rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
1118 }
Michael Wuf6532112007-10-14 14:43:16 -04001119
1120 reg = RTL818X_RX_CONF_ONLYERLPKT |
1121 RTL818X_RX_CONF_RX_AUTORESETPHY |
1122 RTL818X_RX_CONF_MGMT |
1123 RTL818X_RX_CONF_DATA |
1124 (7 << 8 /* MAX RX DMA */) |
1125 RTL818X_RX_CONF_BROADCAST |
1126 RTL818X_RX_CONF_NICMAC;
1127
Andrea Merello6caefd12014-03-08 18:36:37 +01001128 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185)
Michael Wuf6532112007-10-14 14:43:16 -04001129 reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
Andrea Merello4a67aa52014-03-26 21:02:46 +01001130 else if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -04001131 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
1132 ? RTL818X_RX_CONF_CSDM1 : 0;
1133 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
1134 ? RTL818X_RX_CONF_CSDM2 : 0;
Andrea Merello4a67aa52014-03-26 21:02:46 +01001135 } else {
1136 reg &= ~(RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2);
Michael Wuf6532112007-10-14 14:43:16 -04001137 }
1138
1139 priv->rx_conf = reg;
1140 rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
1141
Andrea Merello6caefd12014-03-08 18:36:37 +01001142 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -04001143 reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
andrea merello14c76152014-02-18 02:10:44 +01001144
1145 /* CW is not on per-packet basis.
1146 * in rtl8185 the CW_VALUE reg is used.
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001147 * in rtl8187se the AC param regs are used.
andrea merello14c76152014-02-18 02:10:44 +01001148 */
andrea merello6f7343d2014-01-21 20:16:43 +01001149 reg &= ~RTL818X_CW_CONF_PERPACKET_CW;
andrea merello14c76152014-02-18 02:10:44 +01001150 /* retry limit IS on per-packet basis.
1151 * the short and long retry limit in TX_CONF
1152 * reg are ignored
1153 */
andrea merello6f7343d2014-01-21 20:16:43 +01001154 reg |= RTL818X_CW_CONF_PERPACKET_RETRY;
Michael Wuf6532112007-10-14 14:43:16 -04001155 rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
1156
1157 reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
andrea merello14c76152014-02-18 02:10:44 +01001158 /* TX antenna and TX gain are not on per-packet basis.
1159 * TX Antenna is selected by ANTSEL reg (RX in BB regs).
1160 * TX gain is selected with CCK_TX_AGC and OFDM_TX_AGC regs
1161 */
andrea merello6f7343d2014-01-21 20:16:43 +01001162 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN;
1163 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL;
Michael Wuf6532112007-10-14 14:43:16 -04001164 reg |= RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
1165 rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
1166
1167 /* disable early TX */
1168 rtl818x_iowrite8(priv, (u8 __iomem *)priv->map + 0xec, 0x3f);
1169 }
1170
1171 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
1172 reg |= (6 << 21 /* MAX TX DMA */) |
1173 RTL818X_TX_CONF_NO_ICV;
1174
Andrea Merello4a67aa52014-03-26 21:02:46 +01001175 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1176 reg |= 1<<30; /* "duration procedure mode" */
Andrea Merello6caefd12014-03-08 18:36:37 +01001177
1178 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -04001179 reg &= ~RTL818X_TX_CONF_PROBE_DTS;
1180 else
1181 reg &= ~RTL818X_TX_CONF_HW_SEQNUM;
1182
andrea merelloe74075a2014-02-18 02:10:40 +01001183 reg &= ~RTL818X_TX_CONF_DISCW;
1184
Michael Wuf6532112007-10-14 14:43:16 -04001185 /* different meaning, same value on both rtl8185 and rtl8180 */
1186 reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;
1187
1188 rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
1189
1190 reg = rtl818x_ioread8(priv, &priv->map->CMD);
1191 reg |= RTL818X_CMD_RX_ENABLE;
1192 reg |= RTL818X_CMD_TX_ENABLE;
1193 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
1194
Michael Wuf6532112007-10-14 14:43:16 -04001195 return 0;
1196
1197 err_free_rings:
1198 rtl8180_free_rx_ring(dev);
Andrea Merellofd6564f2014-03-22 18:51:20 +01001199 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -04001200 if (priv->tx_ring[i].desc)
1201 rtl8180_free_tx_ring(dev, i);
1202
1203 return ret;
1204}
1205
1206static void rtl8180_stop(struct ieee80211_hw *dev)
1207{
1208 struct rtl8180_priv *priv = dev->priv;
1209 u8 reg;
1210 int i;
1211
Andrea Merello732c8932014-03-26 21:00:24 +01001212 rtl8180_int_disable(dev);
Michael Wuf6532112007-10-14 14:43:16 -04001213
1214 reg = rtl818x_ioread8(priv, &priv->map->CMD);
1215 reg &= ~RTL818X_CMD_TX_ENABLE;
1216 reg &= ~RTL818X_CMD_RX_ENABLE;
1217 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
1218
1219 priv->rf->stop(dev);
1220
1221 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
1222 reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
1223 rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
1224 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
1225
1226 free_irq(priv->pdev->irq, dev);
1227
1228 rtl8180_free_rx_ring(dev);
Andrea Merellofd6564f2014-03-22 18:51:20 +01001229 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -04001230 rtl8180_free_tx_ring(dev, i);
1231}
1232
Eliad Peller37a41b42011-09-21 14:06:11 +03001233static u64 rtl8180_get_tsf(struct ieee80211_hw *dev,
1234 struct ieee80211_vif *vif)
John W. Linvillec809e862010-05-06 16:49:40 -04001235{
1236 struct rtl8180_priv *priv = dev->priv;
1237
1238 return rtl818x_ioread32(priv, &priv->map->TSFT[0]) |
1239 (u64)(rtl818x_ioread32(priv, &priv->map->TSFT[1])) << 32;
1240}
1241
John W. Linvillea3275e22010-06-24 11:08:37 -04001242static void rtl8180_beacon_work(struct work_struct *work)
John W. Linvillec809e862010-05-06 16:49:40 -04001243{
1244 struct rtl8180_vif *vif_priv =
1245 container_of(work, struct rtl8180_vif, beacon_work.work);
1246 struct ieee80211_vif *vif =
1247 container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
1248 struct ieee80211_hw *dev = vif_priv->dev;
1249 struct ieee80211_mgmt *mgmt;
1250 struct sk_buff *skb;
John W. Linvillec809e862010-05-06 16:49:40 -04001251
1252 /* don't overflow the tx ring */
1253 if (ieee80211_queue_stopped(dev, 0))
1254 goto resched;
1255
1256 /* grab a fresh beacon */
1257 skb = ieee80211_beacon_get(dev, vif);
John W. Linville8f1d2d22010-08-05 13:46:27 -04001258 if (!skb)
1259 goto resched;
John W. Linvillec809e862010-05-06 16:49:40 -04001260
1261 /*
1262 * update beacon timestamp w/ TSF value
1263 * TODO: make hardware update beacon timestamp
1264 */
1265 mgmt = (struct ieee80211_mgmt *)skb->data;
Eliad Peller37a41b42011-09-21 14:06:11 +03001266 mgmt->u.beacon.timestamp = cpu_to_le64(rtl8180_get_tsf(dev, vif));
John W. Linvillec809e862010-05-06 16:49:40 -04001267
1268 /* TODO: use actual beacon queue */
1269 skb_set_queue_mapping(skb, 0);
1270
Thomas Huehn36323f82012-07-23 21:33:42 +02001271 rtl8180_tx(dev, NULL, skb);
John W. Linvillec809e862010-05-06 16:49:40 -04001272
1273resched:
1274 /*
1275 * schedule next beacon
1276 * TODO: use hardware support for beacon timing
1277 */
1278 schedule_delayed_work(&vif_priv->beacon_work,
1279 usecs_to_jiffies(1024 * vif->bss_conf.beacon_int));
1280}
1281
Michael Wuf6532112007-10-14 14:43:16 -04001282static int rtl8180_add_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001283 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -04001284{
1285 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -04001286 struct rtl8180_vif *vif_priv;
Michael Wuf6532112007-10-14 14:43:16 -04001287
John W. Linville643aab62009-12-22 18:13:04 -05001288 /*
1289 * We only support one active interface at a time.
1290 */
1291 if (priv->vif)
1292 return -EBUSY;
Michael Wuf6532112007-10-14 14:43:16 -04001293
Johannes Berg1ed32e42009-12-23 13:15:45 +01001294 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001295 case NL80211_IFTYPE_STATION:
John W. Linvillec809e862010-05-06 16:49:40 -04001296 case NL80211_IFTYPE_ADHOC:
Michael Wuf6532112007-10-14 14:43:16 -04001297 break;
1298 default:
1299 return -EOPNOTSUPP;
1300 }
1301
Johannes Berg1ed32e42009-12-23 13:15:45 +01001302 priv->vif = vif;
Johannes Berg32bfd352007-12-19 01:31:26 +01001303
John W. Linvillec809e862010-05-06 16:49:40 -04001304 /* Initialize driver private area */
1305 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
1306 vif_priv->dev = dev;
1307 INIT_DELAYED_WORK(&vif_priv->beacon_work, rtl8180_beacon_work);
1308 vif_priv->enable_beacon = false;
1309
Michael Wuf6532112007-10-14 14:43:16 -04001310 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
1311 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->MAC[0],
Johannes Berg1ed32e42009-12-23 13:15:45 +01001312 le32_to_cpu(*(__le32 *)vif->addr));
Michael Wuf6532112007-10-14 14:43:16 -04001313 rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->MAC[4],
Johannes Berg1ed32e42009-12-23 13:15:45 +01001314 le16_to_cpu(*(__le16 *)(vif->addr + 4)));
Michael Wuf6532112007-10-14 14:43:16 -04001315 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
1316
1317 return 0;
1318}
1319
1320static void rtl8180_remove_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001321 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -04001322{
1323 struct rtl8180_priv *priv = dev->priv;
Johannes Berg32bfd352007-12-19 01:31:26 +01001324 priv->vif = NULL;
Michael Wuf6532112007-10-14 14:43:16 -04001325}
1326
Johannes Berge8975582008-10-09 12:18:51 +02001327static int rtl8180_config(struct ieee80211_hw *dev, u32 changed)
Michael Wuf6532112007-10-14 14:43:16 -04001328{
1329 struct rtl8180_priv *priv = dev->priv;
Johannes Berge8975582008-10-09 12:18:51 +02001330 struct ieee80211_conf *conf = &dev->conf;
Michael Wuf6532112007-10-14 14:43:16 -04001331
1332 priv->rf->set_chan(dev, conf);
1333
1334 return 0;
1335}
1336
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001337static void rtl8187se_conf_ac_parm(struct ieee80211_hw *dev, u8 queue)
1338{
1339 const struct ieee80211_tx_queue_params *params;
1340 struct rtl8180_priv *priv = dev->priv;
1341
1342 /* hw value */
1343 u32 ac_param;
1344
1345 u8 aifs;
1346 u8 txop;
1347 u8 cw_min, cw_max;
1348
1349 params = &priv->queue_param[queue];
1350
1351 cw_min = fls(params->cw_min);
1352 cw_max = fls(params->cw_max);
1353
1354 aifs = 10 + params->aifs * priv->slot_time;
1355
1356 /* TODO: check if txop HW is in us (mult by 32) */
1357 txop = params->txop;
1358
1359 ac_param = txop << AC_PARAM_TXOP_LIMIT_SHIFT |
1360 cw_max << AC_PARAM_ECW_MAX_SHIFT |
1361 cw_min << AC_PARAM_ECW_MIN_SHIFT |
1362 aifs << AC_PARAM_AIFS_SHIFT;
1363
1364 switch (queue) {
1365 case IEEE80211_AC_BK:
1366 rtl818x_iowrite32(priv, &priv->map->AC_BK_PARAM, ac_param);
1367 break;
1368 case IEEE80211_AC_BE:
1369 rtl818x_iowrite32(priv, &priv->map->AC_BE_PARAM, ac_param);
1370 break;
1371 case IEEE80211_AC_VI:
1372 rtl818x_iowrite32(priv, &priv->map->AC_VI_PARAM, ac_param);
1373 break;
1374 case IEEE80211_AC_VO:
1375 rtl818x_iowrite32(priv, &priv->map->AC_VO_PARAM, ac_param);
1376 break;
1377 }
1378}
1379
Andrea Merello9069af72014-03-15 18:29:37 +01001380static int rtl8180_conf_tx(struct ieee80211_hw *dev,
1381 struct ieee80211_vif *vif, u16 queue,
1382 const struct ieee80211_tx_queue_params *params)
1383{
1384 struct rtl8180_priv *priv = dev->priv;
1385 u8 cw_min, cw_max;
1386
1387 /* nothing to do ? */
1388 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
1389 return 0;
1390
1391 cw_min = fls(params->cw_min);
1392 cw_max = fls(params->cw_max);
1393
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001394 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1395 priv->queue_param[queue] = *params;
1396 rtl8187se_conf_ac_parm(dev, queue);
1397 } else
1398 rtl818x_iowrite8(priv, &priv->map->CW_VAL,
1399 (cw_max << 4) | cw_min);
Andrea Merello9069af72014-03-15 18:29:37 +01001400 return 0;
1401}
1402
1403static void rtl8180_conf_erp(struct ieee80211_hw *dev,
1404 struct ieee80211_bss_conf *info)
1405{
1406 struct rtl8180_priv *priv = dev->priv;
1407 u8 sifs, difs;
1408 int eifs;
1409 u8 hw_eifs;
1410
1411 /* TODO: should we do something ? */
1412 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
1413 return;
1414
1415 /* I _hope_ this means 10uS for the HW.
1416 * In reference code it is 0x22 for
1417 * both rtl8187L and rtl8187SE
1418 */
1419 sifs = 0x22;
1420
1421 if (info->use_short_slot)
1422 priv->slot_time = 9;
1423 else
1424 priv->slot_time = 20;
1425
1426 /* 10 is SIFS time in uS */
1427 difs = 10 + 2 * priv->slot_time;
1428 eifs = 10 + difs + priv->ack_time;
1429
1430 /* HW should use 4uS units for EIFS (I'm sure for rtl8185)*/
1431 hw_eifs = DIV_ROUND_UP(eifs, 4);
1432
1433
1434 rtl818x_iowrite8(priv, &priv->map->SLOT, priv->slot_time);
1435 rtl818x_iowrite8(priv, &priv->map->SIFS, sifs);
1436 rtl818x_iowrite8(priv, &priv->map->DIFS, difs);
1437
1438 /* from reference code. set ack timeout reg = eifs reg */
1439 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, hw_eifs);
1440
Andrea Merello355668d2014-03-26 21:03:03 +01001441 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1442 rtl818x_iowrite8(priv, &priv->map->EIFS_8187SE, hw_eifs);
1443 else if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
1444 /* rtl8187/rtl8185 HW bug. After EIFS is elapsed,
1445 * the HW still wait for DIFS.
1446 * HW uses 4uS units for EIFS.
1447 */
1448 hw_eifs = DIV_ROUND_UP(eifs - difs, 4);
Andrea Merello9069af72014-03-15 18:29:37 +01001449
Andrea Merello355668d2014-03-26 21:03:03 +01001450 rtl818x_iowrite8(priv, &priv->map->EIFS, hw_eifs);
1451 }
Andrea Merello9069af72014-03-15 18:29:37 +01001452}
1453
John W. Linvilleda81ded2008-11-12 14:37:11 -05001454static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
1455 struct ieee80211_vif *vif,
1456 struct ieee80211_bss_conf *info,
1457 u32 changed)
1458{
1459 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -04001460 struct rtl8180_vif *vif_priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001461 int i;
John W. Linville0f956e72010-07-29 21:50:29 -04001462 u8 reg;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001463
John W. Linvillec809e862010-05-06 16:49:40 -04001464 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
1465
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001466 if (changed & BSS_CHANGED_BSSID) {
Andrea Merello1f622d72014-06-30 18:17:48 +02001467 rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->BSSID[0],
1468 le16_to_cpu(*(__le16 *)info->bssid));
1469 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->BSSID[2],
1470 le32_to_cpu(*(__le32 *)(info->bssid + 2)));
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001471
John W. Linville0f956e72010-07-29 21:50:29 -04001472 if (is_valid_ether_addr(info->bssid)) {
1473 if (vif->type == NL80211_IFTYPE_ADHOC)
1474 reg = RTL818X_MSR_ADHOC;
1475 else
1476 reg = RTL818X_MSR_INFRA;
1477 } else
1478 reg = RTL818X_MSR_NO_LINK;
Andrea Merello833d15a2014-03-26 21:03:17 +01001479
1480 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1481 reg |= RTL818X_MSR_ENEDCA;
1482
John W. Linville0f956e72010-07-29 21:50:29 -04001483 rtl818x_iowrite8(priv, &priv->map->MSR, reg);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001484 }
John W. Linvilleda81ded2008-11-12 14:37:11 -05001485
Andrea Merello516a0932014-03-15 18:29:36 +01001486 if (changed & BSS_CHANGED_BASIC_RATES)
1487 rtl8180_conf_basic_rates(dev, info->basic_rates);
1488
Andrea Merello9069af72014-03-15 18:29:37 +01001489 if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_ERP_PREAMBLE)) {
1490
1491 /* when preamble changes, acktime duration changes, and erp must
1492 * be recalculated. ACK time is calculated at lowest rate.
1493 * Since mac80211 include SIFS time we remove it (-10)
1494 */
1495 priv->ack_time =
1496 le16_to_cpu(ieee80211_generic_frame_duration(dev,
1497 priv->vif,
1498 IEEE80211_BAND_2GHZ, 10,
1499 &priv->rates[0])) - 10;
1500
1501 rtl8180_conf_erp(dev, info);
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001502
1503 /* mac80211 supplies aifs_n to driver and calls
1504 * conf_tx callback whether aifs_n changes, NOT
1505 * when aifs changes.
1506 * Aifs should be recalculated if slot changes.
1507 */
1508 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1509 for (i = 0; i < 4; i++)
1510 rtl8187se_conf_ac_parm(dev, i);
1511 }
Andrea Merello9069af72014-03-15 18:29:37 +01001512 }
John W. Linvillec809e862010-05-06 16:49:40 -04001513
1514 if (changed & BSS_CHANGED_BEACON_ENABLED)
1515 vif_priv->enable_beacon = info->enable_beacon;
1516
1517 if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON)) {
1518 cancel_delayed_work_sync(&vif_priv->beacon_work);
1519 if (vif_priv->enable_beacon)
1520 schedule_work(&vif_priv->beacon_work.work);
1521 }
John W. Linvilleda81ded2008-11-12 14:37:11 -05001522}
1523
Jiri Pirko22bedad32010-04-01 21:22:57 +00001524static u64 rtl8180_prepare_multicast(struct ieee80211_hw *dev,
1525 struct netdev_hw_addr_list *mc_list)
Johannes Berg3ac64be2009-08-17 16:16:53 +02001526{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001527 return netdev_hw_addr_list_count(mc_list);
Johannes Berg3ac64be2009-08-17 16:16:53 +02001528}
1529
Michael Wuf6532112007-10-14 14:43:16 -04001530static void rtl8180_configure_filter(struct ieee80211_hw *dev,
1531 unsigned int changed_flags,
1532 unsigned int *total_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001533 u64 multicast)
Michael Wuf6532112007-10-14 14:43:16 -04001534{
1535 struct rtl8180_priv *priv = dev->priv;
1536
1537 if (changed_flags & FIF_FCSFAIL)
1538 priv->rx_conf ^= RTL818X_RX_CONF_FCS;
1539 if (changed_flags & FIF_CONTROL)
1540 priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
1541 if (changed_flags & FIF_OTHER_BSS)
1542 priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
Johannes Berg3ac64be2009-08-17 16:16:53 +02001543 if (*total_flags & FIF_ALLMULTI || multicast > 0)
Michael Wuf6532112007-10-14 14:43:16 -04001544 priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
1545 else
1546 priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
1547
1548 *total_flags = 0;
1549
1550 if (priv->rx_conf & RTL818X_RX_CONF_FCS)
1551 *total_flags |= FIF_FCSFAIL;
1552 if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
1553 *total_flags |= FIF_CONTROL;
1554 if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
1555 *total_flags |= FIF_OTHER_BSS;
1556 if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
1557 *total_flags |= FIF_ALLMULTI;
1558
1559 rtl818x_iowrite32(priv, &priv->map->RX_CONF, priv->rx_conf);
1560}
1561
1562static const struct ieee80211_ops rtl8180_ops = {
1563 .tx = rtl8180_tx,
1564 .start = rtl8180_start,
1565 .stop = rtl8180_stop,
1566 .add_interface = rtl8180_add_interface,
1567 .remove_interface = rtl8180_remove_interface,
1568 .config = rtl8180_config,
John W. Linvilleda81ded2008-11-12 14:37:11 -05001569 .bss_info_changed = rtl8180_bss_info_changed,
Andrea Merello9069af72014-03-15 18:29:37 +01001570 .conf_tx = rtl8180_conf_tx,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001571 .prepare_multicast = rtl8180_prepare_multicast,
Michael Wuf6532112007-10-14 14:43:16 -04001572 .configure_filter = rtl8180_configure_filter,
John W. Linvilled2bb8e02010-01-26 16:22:20 -05001573 .get_tsf = rtl8180_get_tsf,
Michael Wuf6532112007-10-14 14:43:16 -04001574};
1575
1576static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
1577{
Andrea Merello7d4b8292014-03-15 18:29:38 +01001578 struct rtl8180_priv *priv = eeprom->data;
Michael Wuf6532112007-10-14 14:43:16 -04001579 u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1580
1581 eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
1582 eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
1583 eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
1584 eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
1585}
1586
1587static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
1588{
Andrea Merello7d4b8292014-03-15 18:29:38 +01001589 struct rtl8180_priv *priv = eeprom->data;
Michael Wuf6532112007-10-14 14:43:16 -04001590 u8 reg = 2 << 6;
1591
1592 if (eeprom->reg_data_in)
1593 reg |= RTL818X_EEPROM_CMD_WRITE;
1594 if (eeprom->reg_data_out)
1595 reg |= RTL818X_EEPROM_CMD_READ;
1596 if (eeprom->reg_data_clock)
1597 reg |= RTL818X_EEPROM_CMD_CK;
1598 if (eeprom->reg_chip_select)
1599 reg |= RTL818X_EEPROM_CMD_CS;
1600
1601 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
1602 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1603 udelay(10);
1604}
1605
Andrea Merello7d4b8292014-03-15 18:29:38 +01001606static void rtl8180_eeprom_read(struct rtl8180_priv *priv)
1607{
1608 struct eeprom_93cx6 eeprom;
1609 int eeprom_cck_table_adr;
1610 u16 eeprom_val;
1611 int i;
1612
1613 eeprom.data = priv;
1614 eeprom.register_read = rtl8180_eeprom_register_read;
1615 eeprom.register_write = rtl8180_eeprom_register_write;
1616 if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
1617 eeprom.width = PCI_EEPROM_WIDTH_93C66;
1618 else
1619 eeprom.width = PCI_EEPROM_WIDTH_93C46;
1620
1621 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
1622 RTL818X_EEPROM_CMD_PROGRAM);
1623 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1624 udelay(10);
1625
1626 eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
1627 eeprom_val &= 0xFF;
1628 priv->rf_type = eeprom_val;
1629
1630 eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
1631 priv->csthreshold = eeprom_val >> 8;
1632
1633 eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)priv->mac_addr, 3);
1634
Andrea Merellofc32ac92014-03-26 21:01:47 +01001635 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1636 eeprom_cck_table_adr = 0x30;
1637 else
1638 eeprom_cck_table_adr = 0x10;
Andrea Merello7d4b8292014-03-15 18:29:38 +01001639
1640 /* CCK TX power */
1641 for (i = 0; i < 14; i += 2) {
1642 u16 txpwr;
1643 eeprom_93cx6_read(&eeprom, eeprom_cck_table_adr + (i >> 1),
1644 &txpwr);
1645 priv->channels[i].hw_value = txpwr & 0xFF;
1646 priv->channels[i + 1].hw_value = txpwr >> 8;
1647 }
1648
1649 /* OFDM TX power */
1650 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
1651 for (i = 0; i < 14; i += 2) {
1652 u16 txpwr;
1653 eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
1654 priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
1655 priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
1656 }
1657 }
1658
1659 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
1660 __le32 anaparam;
1661 eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
1662 priv->anaparam = le32_to_cpu(anaparam);
1663 eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
1664 }
1665
Andrea Merellofc32ac92014-03-26 21:01:47 +01001666 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1667 eeprom_93cx6_read(&eeprom, 0x3F, &eeprom_val);
1668 priv->antenna_diversity_en = !!(eeprom_val & 0x100);
1669 priv->antenna_diversity_default = (eeprom_val & 0xC00) == 0x400;
1670
1671 eeprom_93cx6_read(&eeprom, 0x7C, &eeprom_val);
1672 priv->xtal_out = eeprom_val & 0xF;
1673 priv->xtal_in = (eeprom_val & 0xF0) >> 4;
1674 priv->xtal_cal = !!(eeprom_val & 0x1000);
1675 priv->thermal_meter_val = (eeprom_val & 0xF00) >> 8;
1676 priv->thermal_meter_en = !!(eeprom_val & 0x2000);
1677 }
1678
Andrea Merello7d4b8292014-03-15 18:29:38 +01001679 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
1680 RTL818X_EEPROM_CMD_NORMAL);
1681}
1682
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001683static int rtl8180_probe(struct pci_dev *pdev,
Michael Wuf6532112007-10-14 14:43:16 -04001684 const struct pci_device_id *id)
1685{
1686 struct ieee80211_hw *dev;
1687 struct rtl8180_priv *priv;
1688 unsigned long mem_addr, mem_len;
1689 unsigned int io_addr, io_len;
Andrea Merello7d4b8292014-03-15 18:29:38 +01001690 int err;
Michael Wuf6532112007-10-14 14:43:16 -04001691 const char *chip_name, *rf_name = NULL;
1692 u32 reg;
Michael Wuf6532112007-10-14 14:43:16 -04001693
1694 err = pci_enable_device(pdev);
1695 if (err) {
1696 printk(KERN_ERR "%s (rtl8180): Cannot enable new PCI device\n",
1697 pci_name(pdev));
1698 return err;
1699 }
1700
1701 err = pci_request_regions(pdev, KBUILD_MODNAME);
1702 if (err) {
1703 printk(KERN_ERR "%s (rtl8180): Cannot obtain PCI resources\n",
1704 pci_name(pdev));
1705 return err;
1706 }
1707
1708 io_addr = pci_resource_start(pdev, 0);
1709 io_len = pci_resource_len(pdev, 0);
1710 mem_addr = pci_resource_start(pdev, 1);
1711 mem_len = pci_resource_len(pdev, 1);
1712
1713 if (mem_len < sizeof(struct rtl818x_csr) ||
1714 io_len < sizeof(struct rtl818x_csr)) {
1715 printk(KERN_ERR "%s (rtl8180): Too short PCI resources\n",
1716 pci_name(pdev));
1717 err = -ENOMEM;
1718 goto err_free_reg;
1719 }
1720
John W. Linville9e385c52010-05-10 14:24:34 -04001721 if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) ||
1722 (err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))) {
Michael Wuf6532112007-10-14 14:43:16 -04001723 printk(KERN_ERR "%s (rtl8180): No suitable DMA available\n",
1724 pci_name(pdev));
1725 goto err_free_reg;
1726 }
1727
1728 pci_set_master(pdev);
1729
1730 dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8180_ops);
1731 if (!dev) {
1732 printk(KERN_ERR "%s (rtl8180): ieee80211 alloc failed\n",
1733 pci_name(pdev));
1734 err = -ENOMEM;
1735 goto err_free_reg;
1736 }
1737
1738 priv = dev->priv;
1739 priv->pdev = pdev;
1740
Johannes Berge6a98542008-10-21 12:40:02 +02001741 dev->max_rates = 2;
Michael Wuf6532112007-10-14 14:43:16 -04001742 SET_IEEE80211_DEV(dev, &pdev->dev);
1743 pci_set_drvdata(pdev, dev);
1744
1745 priv->map = pci_iomap(pdev, 1, mem_len);
1746 if (!priv->map)
1747 priv->map = pci_iomap(pdev, 0, io_len);
1748
1749 if (!priv->map) {
1750 printk(KERN_ERR "%s (rtl8180): Cannot map device memory\n",
1751 pci_name(pdev));
1752 goto err_free_dev;
1753 }
1754
Johannes Berg8318d782008-01-24 19:38:38 +01001755 BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
1756 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
1757
Michael Wuf6532112007-10-14 14:43:16 -04001758 memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
1759 memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
Johannes Berg8318d782008-01-24 19:38:38 +01001760
1761 priv->band.band = IEEE80211_BAND_2GHZ;
1762 priv->band.channels = priv->channels;
1763 priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
1764 priv->band.bitrates = priv->rates;
1765 priv->band.n_bitrates = 4;
1766 dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
1767
Michael Wuf6532112007-10-14 14:43:16 -04001768 dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
Andrea Merello70493272014-05-31 18:30:13 +02001769 IEEE80211_HW_RX_INCLUDES_FCS;
John W. Linvillec809e862010-05-06 16:49:40 -04001770 dev->vif_data_size = sizeof(struct rtl8180_vif);
1771 dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1772 BIT(NL80211_IFTYPE_ADHOC);
Bruno Randolf566bfe52008-05-08 19:15:40 +02001773 dev->max_signal = 65;
Michael Wuf6532112007-10-14 14:43:16 -04001774
1775 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
1776 reg &= RTL818X_TX_CONF_HWVER_MASK;
1777 switch (reg) {
1778 case RTL818X_TX_CONF_R8180_ABCD:
1779 chip_name = "RTL8180";
Andrea Merello6caefd12014-03-08 18:36:37 +01001780 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001781 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001782
Michael Wuf6532112007-10-14 14:43:16 -04001783 case RTL818X_TX_CONF_R8180_F:
1784 chip_name = "RTL8180vF";
Andrea Merello6caefd12014-03-08 18:36:37 +01001785 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001786 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001787
Michael Wuf6532112007-10-14 14:43:16 -04001788 case RTL818X_TX_CONF_R8185_ABC:
1789 chip_name = "RTL8185";
Andrea Merello6caefd12014-03-08 18:36:37 +01001790 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001791 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001792
Michael Wuf6532112007-10-14 14:43:16 -04001793 case RTL818X_TX_CONF_R8185_D:
1794 chip_name = "RTL8185vD";
Andrea Merello6caefd12014-03-08 18:36:37 +01001795 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001796 break;
Andrea Merello24b5fbf2014-03-26 21:04:05 +01001797
1798 case RTL818X_TX_CONF_RTL8187SE:
1799 chip_name = "RTL8187SE";
1800 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8187SE;
1801 break;
1802
Michael Wuf6532112007-10-14 14:43:16 -04001803 default:
1804 printk(KERN_ERR "%s (rtl8180): Unknown chip! (0x%x)\n",
1805 pci_name(pdev), reg >> 25);
1806 goto err_iounmap;
1807 }
1808
Andrea Merellofd6564f2014-03-22 18:51:20 +01001809 /* we declare to MAC80211 all the queues except for beacon queue
1810 * that will be eventually handled by DRV.
1811 * TX rings are arranged in such a way that lower is the IDX,
1812 * higher is the priority, in order to achieve direct mapping
1813 * with mac80211, however the beacon queue is an exception and it
1814 * is mapped on the highst tx ring IDX.
1815 */
Andrea Merello24b5fbf2014-03-26 21:04:05 +01001816 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1817 dev->queues = RTL8187SE_NR_TX_QUEUES - 1;
1818 else
1819 dev->queues = RTL8180_NR_TX_QUEUES - 1;
Andrea Merellofd6564f2014-03-22 18:51:20 +01001820
Andrea Merello6caefd12014-03-08 18:36:37 +01001821 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Johannes Berg8318d782008-01-24 19:38:38 +01001822 priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
Michael Wuf6532112007-10-14 14:43:16 -04001823 pci_try_set_mwi(pdev);
1824 }
1825
Andrea Merello70493272014-05-31 18:30:13 +02001826 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185)
1827 dev->flags |= IEEE80211_HW_SIGNAL_DBM;
1828 else
1829 dev->flags |= IEEE80211_HW_SIGNAL_UNSPEC;
1830
Andrea Merello7d4b8292014-03-15 18:29:38 +01001831 rtl8180_eeprom_read(priv);
Michael Wuf6532112007-10-14 14:43:16 -04001832
Andrea Merello7d4b8292014-03-15 18:29:38 +01001833 switch (priv->rf_type) {
Michael Wuf6532112007-10-14 14:43:16 -04001834 case 1: rf_name = "Intersil";
1835 break;
1836 case 2: rf_name = "RFMD";
1837 break;
1838 case 3: priv->rf = &sa2400_rf_ops;
1839 break;
1840 case 4: priv->rf = &max2820_rf_ops;
1841 break;
1842 case 5: priv->rf = &grf5101_rf_ops;
1843 break;
Andrea Merello24b5fbf2014-03-26 21:04:05 +01001844 case 9:
1845 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1846 priv->rf = rtl8187se_detect_rf(dev);
1847 else
1848 priv->rf = rtl8180_detect_rf(dev);
Michael Wuf6532112007-10-14 14:43:16 -04001849 break;
1850 case 10:
1851 rf_name = "RTL8255";
1852 break;
1853 default:
1854 printk(KERN_ERR "%s (rtl8180): Unknown RF! (0x%x)\n",
Andrea Merello7d4b8292014-03-15 18:29:38 +01001855 pci_name(pdev), priv->rf_type);
Michael Wuf6532112007-10-14 14:43:16 -04001856 goto err_iounmap;
1857 }
1858
1859 if (!priv->rf) {
1860 printk(KERN_ERR "%s (rtl8180): %s RF frontend not supported!\n",
1861 pci_name(pdev), rf_name);
1862 goto err_iounmap;
1863 }
1864
Andrea Merello7d4b8292014-03-15 18:29:38 +01001865 if (!is_valid_ether_addr(priv->mac_addr)) {
Michael Wuf6532112007-10-14 14:43:16 -04001866 printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
1867 " randomly generated MAC addr\n", pci_name(pdev));
Andrea Merello7d4b8292014-03-15 18:29:38 +01001868 eth_random_addr(priv->mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001869 }
Andrea Merello7d4b8292014-03-15 18:29:38 +01001870 SET_IEEE80211_PERM_ADDR(dev, priv->mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001871
1872 spin_lock_init(&priv->lock);
1873
1874 err = ieee80211_register_hw(dev);
1875 if (err) {
1876 printk(KERN_ERR "%s (rtl8180): Cannot register device\n",
1877 pci_name(pdev));
1878 goto err_iounmap;
1879 }
1880
Joe Perchesc96c31e2010-07-26 14:39:58 -07001881 wiphy_info(dev->wiphy, "hwaddr %pm, %s + %s\n",
Andrea Merello7d4b8292014-03-15 18:29:38 +01001882 priv->mac_addr, chip_name, priv->rf->name);
Michael Wuf6532112007-10-14 14:43:16 -04001883
1884 return 0;
1885
1886 err_iounmap:
andrea merello0269da22014-02-18 02:10:41 +01001887 pci_iounmap(pdev, priv->map);
Michael Wuf6532112007-10-14 14:43:16 -04001888
1889 err_free_dev:
Michael Wuf6532112007-10-14 14:43:16 -04001890 ieee80211_free_hw(dev);
1891
1892 err_free_reg:
1893 pci_release_regions(pdev);
1894 pci_disable_device(pdev);
1895 return err;
1896}
1897
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001898static void rtl8180_remove(struct pci_dev *pdev)
Michael Wuf6532112007-10-14 14:43:16 -04001899{
1900 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
1901 struct rtl8180_priv *priv;
1902
1903 if (!dev)
1904 return;
1905
1906 ieee80211_unregister_hw(dev);
1907
1908 priv = dev->priv;
1909
1910 pci_iounmap(pdev, priv->map);
1911 pci_release_regions(pdev);
1912 pci_disable_device(pdev);
1913 ieee80211_free_hw(dev);
1914}
1915
1916#ifdef CONFIG_PM
1917static int rtl8180_suspend(struct pci_dev *pdev, pm_message_t state)
1918{
1919 pci_save_state(pdev);
1920 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1921 return 0;
1922}
1923
1924static int rtl8180_resume(struct pci_dev *pdev)
1925{
1926 pci_set_power_state(pdev, PCI_D0);
1927 pci_restore_state(pdev);
1928 return 0;
1929}
1930
1931#endif /* CONFIG_PM */
1932
1933static struct pci_driver rtl8180_driver = {
1934 .name = KBUILD_MODNAME,
1935 .id_table = rtl8180_table,
1936 .probe = rtl8180_probe,
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001937 .remove = rtl8180_remove,
Michael Wuf6532112007-10-14 14:43:16 -04001938#ifdef CONFIG_PM
1939 .suspend = rtl8180_suspend,
1940 .resume = rtl8180_resume,
1941#endif /* CONFIG_PM */
1942};
1943
Axel Lin5b0a3b72012-04-14 10:38:36 +08001944module_pci_driver(rtl8180_driver);