blob: 68304a9297fd3495b4f07031aae2072cf822fbfa [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;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400351
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
Andrea Merello81129fc2014-06-30 18:18:55 +0200542 entry->retry_limit = info->control.rates[0].count - 1;
andrea merello4c552a52014-02-18 02:10:45 +0100543
544 /* We must be sure that tx_flags is written last because the HW
545 * looks at it to check if the rest of data is valid or not
546 */
547 wmb();
Michael Wuf6532112007-10-14 14:43:16 -0400548 entry->flags = cpu_to_le32(tx_flags);
andrea merelloc24782e2014-02-18 02:10:46 +0100549 /* We must be sure this has been written before followings HW
550 * register write, because this write will made the HW attempts
551 * to DMA the just-written data
552 */
553 wmb();
554
Michael Wuf6532112007-10-14 14:43:16 -0400555 __skb_queue_tail(&ring->queue, skb);
556 if (ring->entries - skb_queue_len(&ring->queue) < 2)
John W. Linvilled10e2e02010-04-27 16:57:38 -0400557 ieee80211_stop_queue(dev, prio);
John W. Linville51e080d2010-05-06 16:26:23 -0400558
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400559 spin_unlock_irqrestore(&priv->lock, flags);
Michael Wuf6532112007-10-14 14:43:16 -0400560
Andrea Merello3ee44d62014-03-26 21:00:57 +0100561 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
562 /* just poll: rings are stopped with TPPollStop reg */
563 hw_prio = rtl8187se_queues_map[prio];
564 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
565 (1 << hw_prio));
566 } else {
567 hw_prio = rtl8180_queues_map[prio];
568 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
Andrea Merellofd6564f2014-03-22 18:51:20 +0100569 (1 << hw_prio) | /* ring to poll */
570 (1<<1) | (1<<2));/* stopped rings */
Andrea Merello3ee44d62014-03-26 21:00:57 +0100571 }
Michael Wuf6532112007-10-14 14:43:16 -0400572}
573
Andrea Merelloff3cbc22014-03-26 21:02:05 +0100574static void rtl8180_set_anaparam3(struct rtl8180_priv *priv, u16 anaparam3)
575{
576 u8 reg;
577
578 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
579 RTL818X_EEPROM_CMD_CONFIG);
580
581 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
582 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
583 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
584
585 rtl818x_iowrite16(priv, &priv->map->ANAPARAM3, anaparam3);
586
587 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
588 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
589
590 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
591 RTL818X_EEPROM_CMD_NORMAL);
592}
593
594void rtl8180_set_anaparam2(struct rtl8180_priv *priv, u32 anaparam2)
595{
596 u8 reg;
597
598 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
599 RTL818X_EEPROM_CMD_CONFIG);
600
601 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
602 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
603 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
604
605 rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, anaparam2);
606
607 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
608 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
609
610 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
611 RTL818X_EEPROM_CMD_NORMAL);
612}
613
Michael Wuf6532112007-10-14 14:43:16 -0400614void rtl8180_set_anaparam(struct rtl8180_priv *priv, u32 anaparam)
615{
616 u8 reg;
617
618 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
619 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
620 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
621 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
622 rtl818x_iowrite32(priv, &priv->map->ANAPARAM, anaparam);
623 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
624 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
625 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
626}
627
Andrea Merello4a67aa52014-03-26 21:02:46 +0100628static void rtl8187se_mac_config(struct ieee80211_hw *dev)
629{
630 struct rtl8180_priv *priv = dev->priv;
631 u8 reg;
632
633 rtl818x_iowrite32(priv, REG_ADDR4(0x1F0), 0);
634 rtl818x_ioread32(priv, REG_ADDR4(0x1F0));
635 rtl818x_iowrite32(priv, REG_ADDR4(0x1F4), 0);
636 rtl818x_ioread32(priv, REG_ADDR4(0x1F4));
637 rtl818x_iowrite8(priv, REG_ADDR1(0x1F8), 0);
638 rtl818x_ioread8(priv, REG_ADDR1(0x1F8));
639 /* Enable DA10 TX power saving */
640 reg = rtl818x_ioread8(priv, &priv->map->PHY_PR);
641 rtl818x_iowrite8(priv, &priv->map->PHY_PR, reg | 0x04);
642 /* Power */
643 rtl818x_iowrite16(priv, PI_DATA_REG, 0x1000);
644 rtl818x_iowrite16(priv, SI_DATA_REG, 0x1000);
645 /* AFE - default to power ON */
646 rtl818x_iowrite16(priv, REG_ADDR2(0x370), 0x0560);
647 rtl818x_iowrite16(priv, REG_ADDR2(0x372), 0x0560);
648 rtl818x_iowrite16(priv, REG_ADDR2(0x374), 0x0DA4);
649 rtl818x_iowrite16(priv, REG_ADDR2(0x376), 0x0DA4);
650 rtl818x_iowrite16(priv, REG_ADDR2(0x378), 0x0560);
651 rtl818x_iowrite16(priv, REG_ADDR2(0x37A), 0x0560);
652 rtl818x_iowrite16(priv, REG_ADDR2(0x37C), 0x00EC);
653 rtl818x_iowrite16(priv, REG_ADDR2(0x37E), 0x00EC);
654 rtl818x_iowrite8(priv, REG_ADDR1(0x24E), 0x01);
655 /* unknown, needed for suspend to RAM resume */
656 rtl818x_iowrite8(priv, REG_ADDR1(0x0A), 0x72);
657}
658
659static void rtl8187se_set_antenna_config(struct ieee80211_hw *dev, u8 def_ant,
660 bool diversity)
661{
662 struct rtl8180_priv *priv = dev->priv;
663
664 rtl8225_write_phy_cck(dev, 0x0C, 0x09);
665 if (diversity) {
666 if (def_ant == 1) {
667 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x00);
668 rtl8225_write_phy_cck(dev, 0x11, 0xBB);
669 rtl8225_write_phy_cck(dev, 0x01, 0xC7);
670 rtl8225_write_phy_ofdm(dev, 0x0D, 0x54);
671 rtl8225_write_phy_ofdm(dev, 0x18, 0xB2);
672 } else { /* main antenna */
673 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x03);
674 rtl8225_write_phy_cck(dev, 0x11, 0x9B);
675 rtl8225_write_phy_cck(dev, 0x01, 0xC7);
676 rtl8225_write_phy_ofdm(dev, 0x0D, 0x5C);
677 rtl8225_write_phy_ofdm(dev, 0x18, 0xB2);
678 }
679 } else { /* disable antenna diversity */
680 if (def_ant == 1) {
681 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x00);
682 rtl8225_write_phy_cck(dev, 0x11, 0xBB);
683 rtl8225_write_phy_cck(dev, 0x01, 0x47);
684 rtl8225_write_phy_ofdm(dev, 0x0D, 0x54);
685 rtl8225_write_phy_ofdm(dev, 0x18, 0x32);
686 } else { /* main antenna */
687 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x03);
688 rtl8225_write_phy_cck(dev, 0x11, 0x9B);
689 rtl8225_write_phy_cck(dev, 0x01, 0x47);
690 rtl8225_write_phy_ofdm(dev, 0x0D, 0x5C);
691 rtl8225_write_phy_ofdm(dev, 0x18, 0x32);
692 }
693 }
694 /* priv->curr_ant = def_ant; */
695}
696
Andrea Merello732c8932014-03-26 21:00:24 +0100697static void rtl8180_int_enable(struct ieee80211_hw *dev)
698{
699 struct rtl8180_priv *priv = dev->priv;
700
701 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
Andrea Merello1c3fb9b2014-04-04 18:21:14 +0200702 rtl818x_iowrite32(priv, &priv->map->IMR,
703 IMR_TBDER | IMR_TBDOK |
Andrea Merello732c8932014-03-26 21:00:24 +0100704 IMR_TVODER | IMR_TVODOK |
705 IMR_TVIDER | IMR_TVIDOK |
706 IMR_TBEDER | IMR_TBEDOK |
707 IMR_TBKDER | IMR_TBKDOK |
708 IMR_RDU | IMR_RER |
709 IMR_ROK | IMR_RQOSOK);
710 } else {
711 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
712 }
713}
714
715static void rtl8180_int_disable(struct ieee80211_hw *dev)
716{
717 struct rtl8180_priv *priv = dev->priv;
718
719 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
720 rtl818x_iowrite32(priv, &priv->map->IMR, 0);
721 } else {
722 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
723 }
724}
725
Andrea Merello516a0932014-03-15 18:29:36 +0100726static void rtl8180_conf_basic_rates(struct ieee80211_hw *dev,
727 u32 rates_mask)
728{
729 struct rtl8180_priv *priv = dev->priv;
730
731 u8 max, min;
732 u16 reg;
733
734 max = fls(rates_mask) - 1;
735 min = ffs(rates_mask) - 1;
736
737 switch (priv->chip_family) {
738
739 case RTL818X_CHIP_FAMILY_RTL8180:
740 /* in 8180 this is NOT a BITMAP */
741 reg = rtl818x_ioread16(priv, &priv->map->BRSR);
742 reg &= ~3;
743 reg |= max;
744 rtl818x_iowrite16(priv, &priv->map->BRSR, reg);
Andrea Merello516a0932014-03-15 18:29:36 +0100745 break;
746
747 case RTL818X_CHIP_FAMILY_RTL8185:
748 /* in 8185 this is a BITMAP */
749 rtl818x_iowrite16(priv, &priv->map->BRSR, rates_mask);
750 rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (max << 4) | min);
751 break;
Andrea Merellod209f3b2014-03-26 20:59:25 +0100752
753 case RTL818X_CHIP_FAMILY_RTL8187SE:
754 /* in 8187se this is a BITMAP */
755 rtl818x_iowrite16(priv, &priv->map->BRSR_8187SE, rates_mask);
756 break;
Andrea Merello516a0932014-03-15 18:29:36 +0100757 }
758}
759
Andrea Merellof1026df2014-03-26 21:01:19 +0100760static void rtl8180_config_cardbus(struct ieee80211_hw *dev)
761{
762 struct rtl8180_priv *priv = dev->priv;
763 u16 reg16;
764 u8 reg8;
765
766 reg8 = rtl818x_ioread8(priv, &priv->map->CONFIG3);
767 reg8 |= 1 << 1;
768 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg8);
769
770 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
771 rtl818x_iowrite16(priv, FEMR_SE, 0xffff);
772 } else {
773 reg16 = rtl818x_ioread16(priv, &priv->map->FEMR);
774 reg16 |= (1 << 15) | (1 << 14) | (1 << 4);
775 rtl818x_iowrite16(priv, &priv->map->FEMR, reg16);
776 }
777
778}
779
Michael Wuf6532112007-10-14 14:43:16 -0400780static int rtl8180_init_hw(struct ieee80211_hw *dev)
781{
782 struct rtl8180_priv *priv = dev->priv;
783 u16 reg;
Andrea Merello4a67aa52014-03-26 21:02:46 +0100784 u32 reg32;
Michael Wuf6532112007-10-14 14:43:16 -0400785
786 rtl818x_iowrite8(priv, &priv->map->CMD, 0);
787 rtl818x_ioread8(priv, &priv->map->CMD);
788 msleep(10);
789
790 /* reset */
Andrea Merello732c8932014-03-26 21:00:24 +0100791 rtl8180_int_disable(dev);
Michael Wuf6532112007-10-14 14:43:16 -0400792 rtl818x_ioread8(priv, &priv->map->CMD);
793
794 reg = rtl818x_ioread8(priv, &priv->map->CMD);
795 reg &= (1 << 1);
796 reg |= RTL818X_CMD_RESET;
797 rtl818x_iowrite8(priv, &priv->map->CMD, RTL818X_CMD_RESET);
798 rtl818x_ioread8(priv, &priv->map->CMD);
799 msleep(200);
800
801 /* check success of reset */
802 if (rtl818x_ioread8(priv, &priv->map->CMD) & RTL818X_CMD_RESET) {
Joe Perchesc96c31e2010-07-26 14:39:58 -0700803 wiphy_err(dev->wiphy, "reset timeout!\n");
Michael Wuf6532112007-10-14 14:43:16 -0400804 return -ETIMEDOUT;
805 }
806
807 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
808 rtl818x_ioread8(priv, &priv->map->CMD);
809 msleep(200);
810
811 if (rtl818x_ioread8(priv, &priv->map->CONFIG3) & (1 << 3)) {
Andrea Merellof1026df2014-03-26 21:01:19 +0100812 rtl8180_config_cardbus(dev);
Michael Wuf6532112007-10-14 14:43:16 -0400813 }
814
Andrea Merello4a67aa52014-03-26 21:02:46 +0100815 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
816 rtl818x_iowrite8(priv, &priv->map->MSR, RTL818X_MSR_ENEDCA);
817 else
818 rtl818x_iowrite8(priv, &priv->map->MSR, 0);
Michael Wuf6532112007-10-14 14:43:16 -0400819
Andrea Merello6caefd12014-03-08 18:36:37 +0100820 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -0400821 rtl8180_set_anaparam(priv, priv->anaparam);
822
823 rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
Andrea Merello4a67aa52014-03-26 21:02:46 +0100824 /* mac80211 queue have higher prio for lower index. The last queue
825 * (that mac80211 is not aware of) is reserved for beacons (and have
826 * the highest priority on the NIC)
827 */
828 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8187SE) {
829 rtl818x_iowrite32(priv, &priv->map->TBDA,
830 priv->tx_ring[1].dma);
831 rtl818x_iowrite32(priv, &priv->map->TLPDA,
832 priv->tx_ring[0].dma);
833 } else {
834 rtl818x_iowrite32(priv, &priv->map->TBDA,
835 priv->tx_ring[4].dma);
836 rtl818x_iowrite32(priv, &priv->map->TVODA,
837 priv->tx_ring[0].dma);
838 rtl818x_iowrite32(priv, &priv->map->TVIDA,
839 priv->tx_ring[1].dma);
840 rtl818x_iowrite32(priv, &priv->map->TBEDA,
841 priv->tx_ring[2].dma);
842 rtl818x_iowrite32(priv, &priv->map->TBKDA,
843 priv->tx_ring[3].dma);
844 }
Michael Wuf6532112007-10-14 14:43:16 -0400845
846 /* TODO: necessary? specs indicate not */
847 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
848 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
849 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
Andrea Merello6caefd12014-03-08 18:36:37 +0100850 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
Michael Wuf6532112007-10-14 14:43:16 -0400851 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
852 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
853 }
854 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
855
856 /* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */
857
858 /* TODO: turn off hw wep on rtl8180 */
859
860 rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
861
Andrea Merello6caefd12014-03-08 18:36:37 +0100862 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400863 rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
Andrea Merellofe67bcd2014-06-30 18:19:10 +0200864 rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0);
Andrea Merello4a67aa52014-03-26 21:02:46 +0100865 } else {
866 rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);
Michael Wuf6532112007-10-14 14:43:16 -0400867
Andrea Merello4a67aa52014-03-26 21:02:46 +0100868 rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
869 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, 0x4C);
870 }
871
872 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
Michael Wuf6532112007-10-14 14:43:16 -0400873 /* TODO: set ClkRun enable? necessary? */
874 reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
875 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, reg & ~(1 << 6));
876 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
877 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
878 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
879 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
Andrea Merello7df00722014-06-30 18:18:25 +0200880 /* fix eccessive IFS after CTS-to-self */
Andrea Merellof4cf6282014-06-30 18:19:27 +0200881 if (priv->map_pio) {
882 u8 reg;
883
884 reg = rtl818x_ioread8(priv, &priv->map->PGSELECT);
885 rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg | 1);
886 rtl818x_iowrite8(priv, REG_ADDR1(0xff), 0x35);
887 rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
888 } else
889 rtl818x_iowrite8(priv, REG_ADDR1(0x1ff), 0x35);
Andrea Merello4a67aa52014-03-26 21:02:46 +0100890 }
Michael Wuf6532112007-10-14 14:43:16 -0400891
Andrea Merello4a67aa52014-03-26 21:02:46 +0100892 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
893
894 /* the set auto rate fallback bitmask from 1M to 54 Mb/s */
895 rtl818x_iowrite16(priv, ARFR, 0xFFF);
896 rtl818x_ioread16(priv, ARFR);
897
898 /* stop unused queus (no dma alloc) */
899 rtl818x_iowrite8(priv, &priv->map->TPPOLL_STOP,
900 RTL818x_TPPOLL_STOP_MG | RTL818x_TPPOLL_STOP_HI);
901
902 rtl818x_iowrite8(priv, &priv->map->ACM_CONTROL, 0x00);
903 rtl818x_iowrite16(priv, &priv->map->TID_AC_MAP, 0xFA50);
904
905 rtl818x_iowrite16(priv, &priv->map->INT_MIG, 0);
906
907 /* some black magic here.. */
908 rtl8187se_mac_config(dev);
909
910 rtl818x_iowrite16(priv, RFSW_CTRL, 0x569A);
911 rtl818x_ioread16(priv, RFSW_CTRL);
912
913 rtl8180_set_anaparam(priv, RTL8225SE_ANAPARAM_ON);
914 rtl8180_set_anaparam2(priv, RTL8225SE_ANAPARAM2_ON);
915 rtl8180_set_anaparam3(priv, RTL8225SE_ANAPARAM3);
916
917
918 rtl818x_iowrite8(priv, &priv->map->CONFIG5,
919 rtl818x_ioread8(priv, &priv->map->CONFIG5) & 0x7F);
920
921 /*probably this switch led on */
922 rtl818x_iowrite8(priv, &priv->map->PGSELECT,
923 rtl818x_ioread8(priv, &priv->map->PGSELECT) | 0x08);
924
925 rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x0480);
926 rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1BFF);
927 rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x2488);
928
929 rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x4003);
930
931 /* the reference code mac hardcode table write
932 * this reg by doing byte-wide accesses.
933 * It does it just for lowest and highest byte..
934 */
935 reg32 = rtl818x_ioread32(priv, &priv->map->RF_PARA);
936 reg32 &= 0x00ffff00;
937 reg32 |= 0xb8000054;
938 rtl818x_iowrite32(priv, &priv->map->RF_PARA, reg32);
Andrea Merello294bc612014-04-04 18:25:51 +0200939 } else
940 /* stop unused queus (no dma alloc) */
941 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
942 (1<<1) | (1<<2));
Michael Wuf6532112007-10-14 14:43:16 -0400943
944 priv->rf->init(dev);
Andrea Merello516a0932014-03-15 18:29:36 +0100945
946 /* default basic rates are 1,2 Mbps for rtl8180. 1,2,6,9,12,18,24 Mbps
947 * otherwise. bitmask 0x3 and 0x01f3 respectively.
948 * NOTE: currenty rtl8225 RF code changes basic rates, so we need to do
949 * this after rf init.
950 * TODO: try to find out whether RF code really needs to do this..
951 */
952 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
953 rtl8180_conf_basic_rates(dev, 0x3);
954 else
955 rtl8180_conf_basic_rates(dev, 0x1f3);
956
Andrea Merello4a67aa52014-03-26 21:02:46 +0100957 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
958 rtl8187se_set_antenna_config(dev,
959 priv->antenna_diversity_default,
960 priv->antenna_diversity_en);
Michael Wuf6532112007-10-14 14:43:16 -0400961 return 0;
962}
963
964static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
965{
966 struct rtl8180_priv *priv = dev->priv;
Andrea Merello21025922014-03-26 20:59:52 +0100967 struct rtl818x_rx_cmd_desc *entry;
Michael Wuf6532112007-10-14 14:43:16 -0400968 int i;
969
Andrea Merello21025922014-03-26 20:59:52 +0100970 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
971 priv->rx_ring_sz = sizeof(struct rtl8187se_rx_desc);
972 else
973 priv->rx_ring_sz = sizeof(struct rtl8180_rx_desc);
974
Michael Wuf6532112007-10-14 14:43:16 -0400975 priv->rx_ring = pci_alloc_consistent(priv->pdev,
Andrea Merello21025922014-03-26 20:59:52 +0100976 priv->rx_ring_sz * 32,
Michael Wuf6532112007-10-14 14:43:16 -0400977 &priv->rx_ring_dma);
978
979 if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -0700980 wiphy_err(dev->wiphy, "Cannot allocate RX ring\n");
Michael Wuf6532112007-10-14 14:43:16 -0400981 return -ENOMEM;
982 }
983
Andrea Merello21025922014-03-26 20:59:52 +0100984 memset(priv->rx_ring, 0, priv->rx_ring_sz * 32);
Michael Wuf6532112007-10-14 14:43:16 -0400985 priv->rx_idx = 0;
986
987 for (i = 0; i < 32; i++) {
988 struct sk_buff *skb = dev_alloc_skb(MAX_RX_SIZE);
989 dma_addr_t *mapping;
Andrea Merello21025922014-03-26 20:59:52 +0100990 entry = priv->rx_ring + priv->rx_ring_sz*i;
andrea merello4da18bb2014-02-18 02:10:43 +0100991 if (!skb) {
992 wiphy_err(dev->wiphy, "Cannot allocate RX skb\n");
993 return -ENOMEM;
994 }
Michael Wuf6532112007-10-14 14:43:16 -0400995 priv->rx_buf[i] = skb;
996 mapping = (dma_addr_t *)skb->cb;
997 *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
998 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
andrea merelloec1da082014-02-22 17:57:23 +0100999
1000 if (pci_dma_mapping_error(priv->pdev, *mapping)) {
1001 kfree_skb(skb);
1002 wiphy_err(dev->wiphy, "Cannot map DMA for RX skb\n");
1003 return -ENOMEM;
1004 }
1005
Michael Wuf6532112007-10-14 14:43:16 -04001006 entry->rx_buf = cpu_to_le32(*mapping);
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -03001007 entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -04001008 MAX_RX_SIZE);
1009 }
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -03001010 entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -04001011 return 0;
1012}
1013
1014static void rtl8180_free_rx_ring(struct ieee80211_hw *dev)
1015{
1016 struct rtl8180_priv *priv = dev->priv;
1017 int i;
1018
1019 for (i = 0; i < 32; i++) {
1020 struct sk_buff *skb = priv->rx_buf[i];
1021 if (!skb)
1022 continue;
1023
1024 pci_unmap_single(priv->pdev,
1025 *((dma_addr_t *)skb->cb),
1026 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
1027 kfree_skb(skb);
1028 }
1029
Andrea Merello21025922014-03-26 20:59:52 +01001030 pci_free_consistent(priv->pdev, priv->rx_ring_sz * 32,
Michael Wuf6532112007-10-14 14:43:16 -04001031 priv->rx_ring, priv->rx_ring_dma);
1032 priv->rx_ring = NULL;
1033}
1034
1035static int rtl8180_init_tx_ring(struct ieee80211_hw *dev,
1036 unsigned int prio, unsigned int entries)
1037{
1038 struct rtl8180_priv *priv = dev->priv;
1039 struct rtl8180_tx_desc *ring;
1040 dma_addr_t dma;
1041 int i;
1042
1043 ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
1044 if (!ring || (unsigned long)ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -07001045 wiphy_err(dev->wiphy, "Cannot allocate TX ring (prio = %d)\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001046 prio);
Michael Wuf6532112007-10-14 14:43:16 -04001047 return -ENOMEM;
1048 }
1049
1050 memset(ring, 0, sizeof(*ring)*entries);
1051 priv->tx_ring[prio].desc = ring;
1052 priv->tx_ring[prio].dma = dma;
1053 priv->tx_ring[prio].idx = 0;
1054 priv->tx_ring[prio].entries = entries;
1055 skb_queue_head_init(&priv->tx_ring[prio].queue);
1056
1057 for (i = 0; i < entries; i++)
1058 ring[i].next_tx_desc =
1059 cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
1060
1061 return 0;
1062}
1063
1064static void rtl8180_free_tx_ring(struct ieee80211_hw *dev, unsigned int prio)
1065{
1066 struct rtl8180_priv *priv = dev->priv;
1067 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
1068
1069 while (skb_queue_len(&ring->queue)) {
1070 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
1071 struct sk_buff *skb = __skb_dequeue(&ring->queue);
1072
1073 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
1074 skb->len, PCI_DMA_TODEVICE);
Michael Wuf6532112007-10-14 14:43:16 -04001075 kfree_skb(skb);
1076 ring->idx = (ring->idx + 1) % ring->entries;
1077 }
1078
1079 pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
1080 ring->desc, ring->dma);
1081 ring->desc = NULL;
1082}
1083
1084static int rtl8180_start(struct ieee80211_hw *dev)
1085{
1086 struct rtl8180_priv *priv = dev->priv;
1087 int ret, i;
1088 u32 reg;
1089
1090 ret = rtl8180_init_rx_ring(dev);
1091 if (ret)
1092 return ret;
1093
Andrea Merellofd6564f2014-03-22 18:51:20 +01001094 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -04001095 if ((ret = rtl8180_init_tx_ring(dev, i, 16)))
1096 goto err_free_rings;
1097
1098 ret = rtl8180_init_hw(dev);
1099 if (ret)
1100 goto err_free_rings;
1101
Andrea Merelloa373ebc2014-03-26 21:00:06 +01001102 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1103 ret = request_irq(priv->pdev->irq, rtl8187se_interrupt,
Michael Wuf6532112007-10-14 14:43:16 -04001104 IRQF_SHARED, KBUILD_MODNAME, dev);
Andrea Merelloa373ebc2014-03-26 21:00:06 +01001105 } else {
1106 ret = request_irq(priv->pdev->irq, rtl8180_interrupt,
1107 IRQF_SHARED, KBUILD_MODNAME, dev);
1108 }
1109
Michael Wuf6532112007-10-14 14:43:16 -04001110 if (ret) {
Joe Perches5db55842010-08-11 19:11:19 -07001111 wiphy_err(dev->wiphy, "failed to register IRQ handler\n");
Michael Wuf6532112007-10-14 14:43:16 -04001112 goto err_free_rings;
1113 }
1114
Andrea Merello732c8932014-03-26 21:00:24 +01001115 rtl8180_int_enable(dev);
Michael Wuf6532112007-10-14 14:43:16 -04001116
Andrea Merellof18f1122014-03-26 21:00:42 +01001117 /* in rtl8187se at MAR regs offset there is the management
1118 * TX descriptor DMA addres..
1119 */
1120 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8187SE) {
1121 rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
1122 rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
1123 }
Michael Wuf6532112007-10-14 14:43:16 -04001124
1125 reg = RTL818X_RX_CONF_ONLYERLPKT |
1126 RTL818X_RX_CONF_RX_AUTORESETPHY |
1127 RTL818X_RX_CONF_MGMT |
1128 RTL818X_RX_CONF_DATA |
1129 (7 << 8 /* MAX RX DMA */) |
1130 RTL818X_RX_CONF_BROADCAST |
1131 RTL818X_RX_CONF_NICMAC;
1132
Andrea Merello6caefd12014-03-08 18:36:37 +01001133 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185)
Michael Wuf6532112007-10-14 14:43:16 -04001134 reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
Andrea Merello4a67aa52014-03-26 21:02:46 +01001135 else if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -04001136 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
1137 ? RTL818X_RX_CONF_CSDM1 : 0;
1138 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
1139 ? RTL818X_RX_CONF_CSDM2 : 0;
Andrea Merello4a67aa52014-03-26 21:02:46 +01001140 } else {
1141 reg &= ~(RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2);
Michael Wuf6532112007-10-14 14:43:16 -04001142 }
1143
1144 priv->rx_conf = reg;
1145 rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
1146
Andrea Merello6caefd12014-03-08 18:36:37 +01001147 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -04001148 reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
andrea merello14c76152014-02-18 02:10:44 +01001149
1150 /* CW is not on per-packet basis.
1151 * in rtl8185 the CW_VALUE reg is used.
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001152 * in rtl8187se the AC param regs are used.
andrea merello14c76152014-02-18 02:10:44 +01001153 */
andrea merello6f7343d2014-01-21 20:16:43 +01001154 reg &= ~RTL818X_CW_CONF_PERPACKET_CW;
andrea merello14c76152014-02-18 02:10:44 +01001155 /* retry limit IS on per-packet basis.
1156 * the short and long retry limit in TX_CONF
1157 * reg are ignored
1158 */
andrea merello6f7343d2014-01-21 20:16:43 +01001159 reg |= RTL818X_CW_CONF_PERPACKET_RETRY;
Michael Wuf6532112007-10-14 14:43:16 -04001160 rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
1161
1162 reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
andrea merello14c76152014-02-18 02:10:44 +01001163 /* TX antenna and TX gain are not on per-packet basis.
1164 * TX Antenna is selected by ANTSEL reg (RX in BB regs).
1165 * TX gain is selected with CCK_TX_AGC and OFDM_TX_AGC regs
1166 */
andrea merello6f7343d2014-01-21 20:16:43 +01001167 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN;
1168 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL;
Michael Wuf6532112007-10-14 14:43:16 -04001169 reg |= RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
1170 rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
1171
1172 /* disable early TX */
1173 rtl818x_iowrite8(priv, (u8 __iomem *)priv->map + 0xec, 0x3f);
1174 }
1175
1176 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
1177 reg |= (6 << 21 /* MAX TX DMA */) |
1178 RTL818X_TX_CONF_NO_ICV;
1179
Andrea Merello4a67aa52014-03-26 21:02:46 +01001180 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1181 reg |= 1<<30; /* "duration procedure mode" */
Andrea Merello6caefd12014-03-08 18:36:37 +01001182
1183 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -04001184 reg &= ~RTL818X_TX_CONF_PROBE_DTS;
1185 else
1186 reg &= ~RTL818X_TX_CONF_HW_SEQNUM;
1187
andrea merelloe74075a2014-02-18 02:10:40 +01001188 reg &= ~RTL818X_TX_CONF_DISCW;
1189
Michael Wuf6532112007-10-14 14:43:16 -04001190 /* different meaning, same value on both rtl8185 and rtl8180 */
1191 reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;
1192
1193 rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
1194
1195 reg = rtl818x_ioread8(priv, &priv->map->CMD);
1196 reg |= RTL818X_CMD_RX_ENABLE;
1197 reg |= RTL818X_CMD_TX_ENABLE;
1198 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
1199
Michael Wuf6532112007-10-14 14:43:16 -04001200 return 0;
1201
1202 err_free_rings:
1203 rtl8180_free_rx_ring(dev);
Andrea Merellofd6564f2014-03-22 18:51:20 +01001204 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -04001205 if (priv->tx_ring[i].desc)
1206 rtl8180_free_tx_ring(dev, i);
1207
1208 return ret;
1209}
1210
1211static void rtl8180_stop(struct ieee80211_hw *dev)
1212{
1213 struct rtl8180_priv *priv = dev->priv;
1214 u8 reg;
1215 int i;
1216
Andrea Merello732c8932014-03-26 21:00:24 +01001217 rtl8180_int_disable(dev);
Michael Wuf6532112007-10-14 14:43:16 -04001218
1219 reg = rtl818x_ioread8(priv, &priv->map->CMD);
1220 reg &= ~RTL818X_CMD_TX_ENABLE;
1221 reg &= ~RTL818X_CMD_RX_ENABLE;
1222 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
1223
1224 priv->rf->stop(dev);
1225
1226 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
1227 reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
1228 rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
1229 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
1230
1231 free_irq(priv->pdev->irq, dev);
1232
1233 rtl8180_free_rx_ring(dev);
Andrea Merellofd6564f2014-03-22 18:51:20 +01001234 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -04001235 rtl8180_free_tx_ring(dev, i);
1236}
1237
Eliad Peller37a41b42011-09-21 14:06:11 +03001238static u64 rtl8180_get_tsf(struct ieee80211_hw *dev,
1239 struct ieee80211_vif *vif)
John W. Linvillec809e862010-05-06 16:49:40 -04001240{
1241 struct rtl8180_priv *priv = dev->priv;
1242
1243 return rtl818x_ioread32(priv, &priv->map->TSFT[0]) |
1244 (u64)(rtl818x_ioread32(priv, &priv->map->TSFT[1])) << 32;
1245}
1246
John W. Linvillea3275e22010-06-24 11:08:37 -04001247static void rtl8180_beacon_work(struct work_struct *work)
John W. Linvillec809e862010-05-06 16:49:40 -04001248{
1249 struct rtl8180_vif *vif_priv =
1250 container_of(work, struct rtl8180_vif, beacon_work.work);
1251 struct ieee80211_vif *vif =
1252 container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
1253 struct ieee80211_hw *dev = vif_priv->dev;
1254 struct ieee80211_mgmt *mgmt;
1255 struct sk_buff *skb;
John W. Linvillec809e862010-05-06 16:49:40 -04001256
1257 /* don't overflow the tx ring */
1258 if (ieee80211_queue_stopped(dev, 0))
1259 goto resched;
1260
1261 /* grab a fresh beacon */
1262 skb = ieee80211_beacon_get(dev, vif);
John W. Linville8f1d2d22010-08-05 13:46:27 -04001263 if (!skb)
1264 goto resched;
John W. Linvillec809e862010-05-06 16:49:40 -04001265
1266 /*
1267 * update beacon timestamp w/ TSF value
1268 * TODO: make hardware update beacon timestamp
1269 */
1270 mgmt = (struct ieee80211_mgmt *)skb->data;
Eliad Peller37a41b42011-09-21 14:06:11 +03001271 mgmt->u.beacon.timestamp = cpu_to_le64(rtl8180_get_tsf(dev, vif));
John W. Linvillec809e862010-05-06 16:49:40 -04001272
1273 /* TODO: use actual beacon queue */
1274 skb_set_queue_mapping(skb, 0);
1275
Thomas Huehn36323f82012-07-23 21:33:42 +02001276 rtl8180_tx(dev, NULL, skb);
John W. Linvillec809e862010-05-06 16:49:40 -04001277
1278resched:
1279 /*
1280 * schedule next beacon
1281 * TODO: use hardware support for beacon timing
1282 */
1283 schedule_delayed_work(&vif_priv->beacon_work,
1284 usecs_to_jiffies(1024 * vif->bss_conf.beacon_int));
1285}
1286
Michael Wuf6532112007-10-14 14:43:16 -04001287static int rtl8180_add_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001288 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -04001289{
1290 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -04001291 struct rtl8180_vif *vif_priv;
Michael Wuf6532112007-10-14 14:43:16 -04001292
John W. Linville643aab62009-12-22 18:13:04 -05001293 /*
1294 * We only support one active interface at a time.
1295 */
1296 if (priv->vif)
1297 return -EBUSY;
Michael Wuf6532112007-10-14 14:43:16 -04001298
Johannes Berg1ed32e42009-12-23 13:15:45 +01001299 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001300 case NL80211_IFTYPE_STATION:
John W. Linvillec809e862010-05-06 16:49:40 -04001301 case NL80211_IFTYPE_ADHOC:
Michael Wuf6532112007-10-14 14:43:16 -04001302 break;
1303 default:
1304 return -EOPNOTSUPP;
1305 }
1306
Johannes Berg1ed32e42009-12-23 13:15:45 +01001307 priv->vif = vif;
Johannes Berg32bfd352007-12-19 01:31:26 +01001308
John W. Linvillec809e862010-05-06 16:49:40 -04001309 /* Initialize driver private area */
1310 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
1311 vif_priv->dev = dev;
1312 INIT_DELAYED_WORK(&vif_priv->beacon_work, rtl8180_beacon_work);
1313 vif_priv->enable_beacon = false;
1314
Michael Wuf6532112007-10-14 14:43:16 -04001315 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
1316 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->MAC[0],
Johannes Berg1ed32e42009-12-23 13:15:45 +01001317 le32_to_cpu(*(__le32 *)vif->addr));
Michael Wuf6532112007-10-14 14:43:16 -04001318 rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->MAC[4],
Johannes Berg1ed32e42009-12-23 13:15:45 +01001319 le16_to_cpu(*(__le16 *)(vif->addr + 4)));
Michael Wuf6532112007-10-14 14:43:16 -04001320 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
1321
1322 return 0;
1323}
1324
1325static void rtl8180_remove_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001326 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -04001327{
1328 struct rtl8180_priv *priv = dev->priv;
Johannes Berg32bfd352007-12-19 01:31:26 +01001329 priv->vif = NULL;
Michael Wuf6532112007-10-14 14:43:16 -04001330}
1331
Johannes Berge8975582008-10-09 12:18:51 +02001332static int rtl8180_config(struct ieee80211_hw *dev, u32 changed)
Michael Wuf6532112007-10-14 14:43:16 -04001333{
1334 struct rtl8180_priv *priv = dev->priv;
Johannes Berge8975582008-10-09 12:18:51 +02001335 struct ieee80211_conf *conf = &dev->conf;
Michael Wuf6532112007-10-14 14:43:16 -04001336
1337 priv->rf->set_chan(dev, conf);
1338
1339 return 0;
1340}
1341
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001342static void rtl8187se_conf_ac_parm(struct ieee80211_hw *dev, u8 queue)
1343{
1344 const struct ieee80211_tx_queue_params *params;
1345 struct rtl8180_priv *priv = dev->priv;
1346
1347 /* hw value */
1348 u32 ac_param;
1349
1350 u8 aifs;
1351 u8 txop;
1352 u8 cw_min, cw_max;
1353
1354 params = &priv->queue_param[queue];
1355
1356 cw_min = fls(params->cw_min);
1357 cw_max = fls(params->cw_max);
1358
1359 aifs = 10 + params->aifs * priv->slot_time;
1360
1361 /* TODO: check if txop HW is in us (mult by 32) */
1362 txop = params->txop;
1363
1364 ac_param = txop << AC_PARAM_TXOP_LIMIT_SHIFT |
1365 cw_max << AC_PARAM_ECW_MAX_SHIFT |
1366 cw_min << AC_PARAM_ECW_MIN_SHIFT |
1367 aifs << AC_PARAM_AIFS_SHIFT;
1368
1369 switch (queue) {
1370 case IEEE80211_AC_BK:
1371 rtl818x_iowrite32(priv, &priv->map->AC_BK_PARAM, ac_param);
1372 break;
1373 case IEEE80211_AC_BE:
1374 rtl818x_iowrite32(priv, &priv->map->AC_BE_PARAM, ac_param);
1375 break;
1376 case IEEE80211_AC_VI:
1377 rtl818x_iowrite32(priv, &priv->map->AC_VI_PARAM, ac_param);
1378 break;
1379 case IEEE80211_AC_VO:
1380 rtl818x_iowrite32(priv, &priv->map->AC_VO_PARAM, ac_param);
1381 break;
1382 }
1383}
1384
Andrea Merello9069af72014-03-15 18:29:37 +01001385static int rtl8180_conf_tx(struct ieee80211_hw *dev,
1386 struct ieee80211_vif *vif, u16 queue,
1387 const struct ieee80211_tx_queue_params *params)
1388{
1389 struct rtl8180_priv *priv = dev->priv;
1390 u8 cw_min, cw_max;
1391
1392 /* nothing to do ? */
1393 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
1394 return 0;
1395
1396 cw_min = fls(params->cw_min);
1397 cw_max = fls(params->cw_max);
1398
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001399 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1400 priv->queue_param[queue] = *params;
1401 rtl8187se_conf_ac_parm(dev, queue);
1402 } else
1403 rtl818x_iowrite8(priv, &priv->map->CW_VAL,
1404 (cw_max << 4) | cw_min);
Andrea Merello9069af72014-03-15 18:29:37 +01001405 return 0;
1406}
1407
1408static void rtl8180_conf_erp(struct ieee80211_hw *dev,
1409 struct ieee80211_bss_conf *info)
1410{
1411 struct rtl8180_priv *priv = dev->priv;
1412 u8 sifs, difs;
1413 int eifs;
1414 u8 hw_eifs;
1415
1416 /* TODO: should we do something ? */
1417 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
1418 return;
1419
1420 /* I _hope_ this means 10uS for the HW.
1421 * In reference code it is 0x22 for
1422 * both rtl8187L and rtl8187SE
1423 */
1424 sifs = 0x22;
1425
1426 if (info->use_short_slot)
1427 priv->slot_time = 9;
1428 else
1429 priv->slot_time = 20;
1430
1431 /* 10 is SIFS time in uS */
1432 difs = 10 + 2 * priv->slot_time;
1433 eifs = 10 + difs + priv->ack_time;
1434
1435 /* HW should use 4uS units for EIFS (I'm sure for rtl8185)*/
1436 hw_eifs = DIV_ROUND_UP(eifs, 4);
1437
1438
1439 rtl818x_iowrite8(priv, &priv->map->SLOT, priv->slot_time);
1440 rtl818x_iowrite8(priv, &priv->map->SIFS, sifs);
1441 rtl818x_iowrite8(priv, &priv->map->DIFS, difs);
1442
1443 /* from reference code. set ack timeout reg = eifs reg */
1444 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, hw_eifs);
1445
Andrea Merello355668d2014-03-26 21:03:03 +01001446 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1447 rtl818x_iowrite8(priv, &priv->map->EIFS_8187SE, hw_eifs);
1448 else if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
1449 /* rtl8187/rtl8185 HW bug. After EIFS is elapsed,
1450 * the HW still wait for DIFS.
1451 * HW uses 4uS units for EIFS.
1452 */
1453 hw_eifs = DIV_ROUND_UP(eifs - difs, 4);
Andrea Merello9069af72014-03-15 18:29:37 +01001454
Andrea Merello355668d2014-03-26 21:03:03 +01001455 rtl818x_iowrite8(priv, &priv->map->EIFS, hw_eifs);
1456 }
Andrea Merello9069af72014-03-15 18:29:37 +01001457}
1458
John W. Linvilleda81ded2008-11-12 14:37:11 -05001459static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
1460 struct ieee80211_vif *vif,
1461 struct ieee80211_bss_conf *info,
1462 u32 changed)
1463{
1464 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -04001465 struct rtl8180_vif *vif_priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001466 int i;
John W. Linville0f956e72010-07-29 21:50:29 -04001467 u8 reg;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001468
John W. Linvillec809e862010-05-06 16:49:40 -04001469 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
1470
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001471 if (changed & BSS_CHANGED_BSSID) {
Andrea Merello1f622d72014-06-30 18:17:48 +02001472 rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->BSSID[0],
1473 le16_to_cpu(*(__le16 *)info->bssid));
1474 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->BSSID[2],
1475 le32_to_cpu(*(__le32 *)(info->bssid + 2)));
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001476
John W. Linville0f956e72010-07-29 21:50:29 -04001477 if (is_valid_ether_addr(info->bssid)) {
1478 if (vif->type == NL80211_IFTYPE_ADHOC)
1479 reg = RTL818X_MSR_ADHOC;
1480 else
1481 reg = RTL818X_MSR_INFRA;
1482 } else
1483 reg = RTL818X_MSR_NO_LINK;
Andrea Merello833d15a2014-03-26 21:03:17 +01001484
1485 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1486 reg |= RTL818X_MSR_ENEDCA;
1487
John W. Linville0f956e72010-07-29 21:50:29 -04001488 rtl818x_iowrite8(priv, &priv->map->MSR, reg);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001489 }
John W. Linvilleda81ded2008-11-12 14:37:11 -05001490
Andrea Merello516a0932014-03-15 18:29:36 +01001491 if (changed & BSS_CHANGED_BASIC_RATES)
1492 rtl8180_conf_basic_rates(dev, info->basic_rates);
1493
Andrea Merello9069af72014-03-15 18:29:37 +01001494 if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_ERP_PREAMBLE)) {
1495
1496 /* when preamble changes, acktime duration changes, and erp must
1497 * be recalculated. ACK time is calculated at lowest rate.
1498 * Since mac80211 include SIFS time we remove it (-10)
1499 */
1500 priv->ack_time =
1501 le16_to_cpu(ieee80211_generic_frame_duration(dev,
1502 priv->vif,
1503 IEEE80211_BAND_2GHZ, 10,
1504 &priv->rates[0])) - 10;
1505
1506 rtl8180_conf_erp(dev, info);
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001507
1508 /* mac80211 supplies aifs_n to driver and calls
1509 * conf_tx callback whether aifs_n changes, NOT
1510 * when aifs changes.
1511 * Aifs should be recalculated if slot changes.
1512 */
1513 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1514 for (i = 0; i < 4; i++)
1515 rtl8187se_conf_ac_parm(dev, i);
1516 }
Andrea Merello9069af72014-03-15 18:29:37 +01001517 }
John W. Linvillec809e862010-05-06 16:49:40 -04001518
1519 if (changed & BSS_CHANGED_BEACON_ENABLED)
1520 vif_priv->enable_beacon = info->enable_beacon;
1521
1522 if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON)) {
1523 cancel_delayed_work_sync(&vif_priv->beacon_work);
1524 if (vif_priv->enable_beacon)
1525 schedule_work(&vif_priv->beacon_work.work);
1526 }
John W. Linvilleda81ded2008-11-12 14:37:11 -05001527}
1528
Jiri Pirko22bedad32010-04-01 21:22:57 +00001529static u64 rtl8180_prepare_multicast(struct ieee80211_hw *dev,
1530 struct netdev_hw_addr_list *mc_list)
Johannes Berg3ac64be2009-08-17 16:16:53 +02001531{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001532 return netdev_hw_addr_list_count(mc_list);
Johannes Berg3ac64be2009-08-17 16:16:53 +02001533}
1534
Michael Wuf6532112007-10-14 14:43:16 -04001535static void rtl8180_configure_filter(struct ieee80211_hw *dev,
1536 unsigned int changed_flags,
1537 unsigned int *total_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001538 u64 multicast)
Michael Wuf6532112007-10-14 14:43:16 -04001539{
1540 struct rtl8180_priv *priv = dev->priv;
1541
1542 if (changed_flags & FIF_FCSFAIL)
1543 priv->rx_conf ^= RTL818X_RX_CONF_FCS;
1544 if (changed_flags & FIF_CONTROL)
1545 priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
1546 if (changed_flags & FIF_OTHER_BSS)
1547 priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
Johannes Berg3ac64be2009-08-17 16:16:53 +02001548 if (*total_flags & FIF_ALLMULTI || multicast > 0)
Michael Wuf6532112007-10-14 14:43:16 -04001549 priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
1550 else
1551 priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
1552
1553 *total_flags = 0;
1554
1555 if (priv->rx_conf & RTL818X_RX_CONF_FCS)
1556 *total_flags |= FIF_FCSFAIL;
1557 if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
1558 *total_flags |= FIF_CONTROL;
1559 if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
1560 *total_flags |= FIF_OTHER_BSS;
1561 if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
1562 *total_flags |= FIF_ALLMULTI;
1563
1564 rtl818x_iowrite32(priv, &priv->map->RX_CONF, priv->rx_conf);
1565}
1566
1567static const struct ieee80211_ops rtl8180_ops = {
1568 .tx = rtl8180_tx,
1569 .start = rtl8180_start,
1570 .stop = rtl8180_stop,
1571 .add_interface = rtl8180_add_interface,
1572 .remove_interface = rtl8180_remove_interface,
1573 .config = rtl8180_config,
John W. Linvilleda81ded2008-11-12 14:37:11 -05001574 .bss_info_changed = rtl8180_bss_info_changed,
Andrea Merello9069af72014-03-15 18:29:37 +01001575 .conf_tx = rtl8180_conf_tx,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001576 .prepare_multicast = rtl8180_prepare_multicast,
Michael Wuf6532112007-10-14 14:43:16 -04001577 .configure_filter = rtl8180_configure_filter,
John W. Linvilled2bb8e02010-01-26 16:22:20 -05001578 .get_tsf = rtl8180_get_tsf,
Michael Wuf6532112007-10-14 14:43:16 -04001579};
1580
1581static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
1582{
Andrea Merello7d4b8292014-03-15 18:29:38 +01001583 struct rtl8180_priv *priv = eeprom->data;
Michael Wuf6532112007-10-14 14:43:16 -04001584 u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1585
1586 eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
1587 eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
1588 eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
1589 eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
1590}
1591
1592static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
1593{
Andrea Merello7d4b8292014-03-15 18:29:38 +01001594 struct rtl8180_priv *priv = eeprom->data;
Michael Wuf6532112007-10-14 14:43:16 -04001595 u8 reg = 2 << 6;
1596
1597 if (eeprom->reg_data_in)
1598 reg |= RTL818X_EEPROM_CMD_WRITE;
1599 if (eeprom->reg_data_out)
1600 reg |= RTL818X_EEPROM_CMD_READ;
1601 if (eeprom->reg_data_clock)
1602 reg |= RTL818X_EEPROM_CMD_CK;
1603 if (eeprom->reg_chip_select)
1604 reg |= RTL818X_EEPROM_CMD_CS;
1605
1606 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
1607 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1608 udelay(10);
1609}
1610
Andrea Merello7d4b8292014-03-15 18:29:38 +01001611static void rtl8180_eeprom_read(struct rtl8180_priv *priv)
1612{
1613 struct eeprom_93cx6 eeprom;
1614 int eeprom_cck_table_adr;
1615 u16 eeprom_val;
1616 int i;
1617
1618 eeprom.data = priv;
1619 eeprom.register_read = rtl8180_eeprom_register_read;
1620 eeprom.register_write = rtl8180_eeprom_register_write;
1621 if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
1622 eeprom.width = PCI_EEPROM_WIDTH_93C66;
1623 else
1624 eeprom.width = PCI_EEPROM_WIDTH_93C46;
1625
1626 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
1627 RTL818X_EEPROM_CMD_PROGRAM);
1628 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1629 udelay(10);
1630
1631 eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
1632 eeprom_val &= 0xFF;
1633 priv->rf_type = eeprom_val;
1634
1635 eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
1636 priv->csthreshold = eeprom_val >> 8;
1637
1638 eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)priv->mac_addr, 3);
1639
Andrea Merellofc32ac92014-03-26 21:01:47 +01001640 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1641 eeprom_cck_table_adr = 0x30;
1642 else
1643 eeprom_cck_table_adr = 0x10;
Andrea Merello7d4b8292014-03-15 18:29:38 +01001644
1645 /* CCK TX power */
1646 for (i = 0; i < 14; i += 2) {
1647 u16 txpwr;
1648 eeprom_93cx6_read(&eeprom, eeprom_cck_table_adr + (i >> 1),
1649 &txpwr);
1650 priv->channels[i].hw_value = txpwr & 0xFF;
1651 priv->channels[i + 1].hw_value = txpwr >> 8;
1652 }
1653
1654 /* OFDM TX power */
1655 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
1656 for (i = 0; i < 14; i += 2) {
1657 u16 txpwr;
1658 eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
1659 priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
1660 priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
1661 }
1662 }
1663
1664 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
1665 __le32 anaparam;
1666 eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
1667 priv->anaparam = le32_to_cpu(anaparam);
1668 eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
1669 }
1670
Andrea Merellofc32ac92014-03-26 21:01:47 +01001671 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1672 eeprom_93cx6_read(&eeprom, 0x3F, &eeprom_val);
1673 priv->antenna_diversity_en = !!(eeprom_val & 0x100);
1674 priv->antenna_diversity_default = (eeprom_val & 0xC00) == 0x400;
1675
1676 eeprom_93cx6_read(&eeprom, 0x7C, &eeprom_val);
1677 priv->xtal_out = eeprom_val & 0xF;
1678 priv->xtal_in = (eeprom_val & 0xF0) >> 4;
1679 priv->xtal_cal = !!(eeprom_val & 0x1000);
1680 priv->thermal_meter_val = (eeprom_val & 0xF00) >> 8;
1681 priv->thermal_meter_en = !!(eeprom_val & 0x2000);
1682 }
1683
Andrea Merello7d4b8292014-03-15 18:29:38 +01001684 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
1685 RTL818X_EEPROM_CMD_NORMAL);
1686}
1687
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001688static int rtl8180_probe(struct pci_dev *pdev,
Michael Wuf6532112007-10-14 14:43:16 -04001689 const struct pci_device_id *id)
1690{
1691 struct ieee80211_hw *dev;
1692 struct rtl8180_priv *priv;
1693 unsigned long mem_addr, mem_len;
1694 unsigned int io_addr, io_len;
Andrea Merello7d4b8292014-03-15 18:29:38 +01001695 int err;
Michael Wuf6532112007-10-14 14:43:16 -04001696 const char *chip_name, *rf_name = NULL;
1697 u32 reg;
Michael Wuf6532112007-10-14 14:43:16 -04001698
1699 err = pci_enable_device(pdev);
1700 if (err) {
1701 printk(KERN_ERR "%s (rtl8180): Cannot enable new PCI device\n",
1702 pci_name(pdev));
1703 return err;
1704 }
1705
1706 err = pci_request_regions(pdev, KBUILD_MODNAME);
1707 if (err) {
1708 printk(KERN_ERR "%s (rtl8180): Cannot obtain PCI resources\n",
1709 pci_name(pdev));
1710 return err;
1711 }
1712
1713 io_addr = pci_resource_start(pdev, 0);
1714 io_len = pci_resource_len(pdev, 0);
1715 mem_addr = pci_resource_start(pdev, 1);
1716 mem_len = pci_resource_len(pdev, 1);
1717
1718 if (mem_len < sizeof(struct rtl818x_csr) ||
1719 io_len < sizeof(struct rtl818x_csr)) {
1720 printk(KERN_ERR "%s (rtl8180): Too short PCI resources\n",
1721 pci_name(pdev));
1722 err = -ENOMEM;
1723 goto err_free_reg;
1724 }
1725
John W. Linville9e385c52010-05-10 14:24:34 -04001726 if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) ||
1727 (err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))) {
Michael Wuf6532112007-10-14 14:43:16 -04001728 printk(KERN_ERR "%s (rtl8180): No suitable DMA available\n",
1729 pci_name(pdev));
1730 goto err_free_reg;
1731 }
1732
1733 pci_set_master(pdev);
1734
1735 dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8180_ops);
1736 if (!dev) {
1737 printk(KERN_ERR "%s (rtl8180): ieee80211 alloc failed\n",
1738 pci_name(pdev));
1739 err = -ENOMEM;
1740 goto err_free_reg;
1741 }
1742
1743 priv = dev->priv;
1744 priv->pdev = pdev;
1745
Andrea Merellofe67bcd2014-06-30 18:19:10 +02001746 dev->max_rates = 1;
Michael Wuf6532112007-10-14 14:43:16 -04001747 SET_IEEE80211_DEV(dev, &pdev->dev);
1748 pci_set_drvdata(pdev, dev);
1749
Andrea Merellof4cf6282014-06-30 18:19:27 +02001750 priv->map_pio = false;
Michael Wuf6532112007-10-14 14:43:16 -04001751 priv->map = pci_iomap(pdev, 1, mem_len);
Andrea Merellof4cf6282014-06-30 18:19:27 +02001752 if (!priv->map) {
Michael Wuf6532112007-10-14 14:43:16 -04001753 priv->map = pci_iomap(pdev, 0, io_len);
Andrea Merellof4cf6282014-06-30 18:19:27 +02001754 priv->map_pio = true;
1755 }
Michael Wuf6532112007-10-14 14:43:16 -04001756
1757 if (!priv->map) {
Andrea Merellof4cf6282014-06-30 18:19:27 +02001758 dev_err(&pdev->dev, "Cannot map device memory/PIO\n");
Michael Wuf6532112007-10-14 14:43:16 -04001759 goto err_free_dev;
1760 }
1761
Johannes Berg8318d782008-01-24 19:38:38 +01001762 BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
1763 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
1764
Michael Wuf6532112007-10-14 14:43:16 -04001765 memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
1766 memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
Johannes Berg8318d782008-01-24 19:38:38 +01001767
1768 priv->band.band = IEEE80211_BAND_2GHZ;
1769 priv->band.channels = priv->channels;
1770 priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
1771 priv->band.bitrates = priv->rates;
1772 priv->band.n_bitrates = 4;
1773 dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
1774
Michael Wuf6532112007-10-14 14:43:16 -04001775 dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
Andrea Merello70493272014-05-31 18:30:13 +02001776 IEEE80211_HW_RX_INCLUDES_FCS;
John W. Linvillec809e862010-05-06 16:49:40 -04001777 dev->vif_data_size = sizeof(struct rtl8180_vif);
1778 dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1779 BIT(NL80211_IFTYPE_ADHOC);
Bruno Randolf566bfe52008-05-08 19:15:40 +02001780 dev->max_signal = 65;
Michael Wuf6532112007-10-14 14:43:16 -04001781
1782 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
1783 reg &= RTL818X_TX_CONF_HWVER_MASK;
1784 switch (reg) {
1785 case RTL818X_TX_CONF_R8180_ABCD:
1786 chip_name = "RTL8180";
Andrea Merello6caefd12014-03-08 18:36:37 +01001787 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001788 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001789
Michael Wuf6532112007-10-14 14:43:16 -04001790 case RTL818X_TX_CONF_R8180_F:
1791 chip_name = "RTL8180vF";
Andrea Merello6caefd12014-03-08 18:36:37 +01001792 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001793 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001794
Michael Wuf6532112007-10-14 14:43:16 -04001795 case RTL818X_TX_CONF_R8185_ABC:
1796 chip_name = "RTL8185";
Andrea Merello6caefd12014-03-08 18:36:37 +01001797 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001798 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001799
Michael Wuf6532112007-10-14 14:43:16 -04001800 case RTL818X_TX_CONF_R8185_D:
1801 chip_name = "RTL8185vD";
Andrea Merello6caefd12014-03-08 18:36:37 +01001802 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001803 break;
Andrea Merello24b5fbf2014-03-26 21:04:05 +01001804
1805 case RTL818X_TX_CONF_RTL8187SE:
1806 chip_name = "RTL8187SE";
Andrea Merellof4cf6282014-06-30 18:19:27 +02001807 if (priv->map_pio) {
1808 dev_err(&pdev->dev,
1809 "MMIO failed. PIO not supported on RTL8187SE\n");
1810 err = -ENOMEM;
1811 goto err_iounmap;
1812 }
Andrea Merello24b5fbf2014-03-26 21:04:05 +01001813 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8187SE;
1814 break;
1815
Michael Wuf6532112007-10-14 14:43:16 -04001816 default:
1817 printk(KERN_ERR "%s (rtl8180): Unknown chip! (0x%x)\n",
1818 pci_name(pdev), reg >> 25);
1819 goto err_iounmap;
1820 }
1821
Andrea Merellofd6564f2014-03-22 18:51:20 +01001822 /* we declare to MAC80211 all the queues except for beacon queue
1823 * that will be eventually handled by DRV.
1824 * TX rings are arranged in such a way that lower is the IDX,
1825 * higher is the priority, in order to achieve direct mapping
1826 * with mac80211, however the beacon queue is an exception and it
1827 * is mapped on the highst tx ring IDX.
1828 */
Andrea Merello24b5fbf2014-03-26 21:04:05 +01001829 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1830 dev->queues = RTL8187SE_NR_TX_QUEUES - 1;
1831 else
1832 dev->queues = RTL8180_NR_TX_QUEUES - 1;
Andrea Merellofd6564f2014-03-22 18:51:20 +01001833
Andrea Merello6caefd12014-03-08 18:36:37 +01001834 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Johannes Berg8318d782008-01-24 19:38:38 +01001835 priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
Michael Wuf6532112007-10-14 14:43:16 -04001836 pci_try_set_mwi(pdev);
1837 }
1838
Andrea Merello70493272014-05-31 18:30:13 +02001839 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185)
1840 dev->flags |= IEEE80211_HW_SIGNAL_DBM;
1841 else
1842 dev->flags |= IEEE80211_HW_SIGNAL_UNSPEC;
1843
Andrea Merello7d4b8292014-03-15 18:29:38 +01001844 rtl8180_eeprom_read(priv);
Michael Wuf6532112007-10-14 14:43:16 -04001845
Andrea Merello7d4b8292014-03-15 18:29:38 +01001846 switch (priv->rf_type) {
Michael Wuf6532112007-10-14 14:43:16 -04001847 case 1: rf_name = "Intersil";
1848 break;
1849 case 2: rf_name = "RFMD";
1850 break;
1851 case 3: priv->rf = &sa2400_rf_ops;
1852 break;
1853 case 4: priv->rf = &max2820_rf_ops;
1854 break;
1855 case 5: priv->rf = &grf5101_rf_ops;
1856 break;
Andrea Merello24b5fbf2014-03-26 21:04:05 +01001857 case 9:
1858 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1859 priv->rf = rtl8187se_detect_rf(dev);
1860 else
1861 priv->rf = rtl8180_detect_rf(dev);
Michael Wuf6532112007-10-14 14:43:16 -04001862 break;
1863 case 10:
1864 rf_name = "RTL8255";
1865 break;
1866 default:
1867 printk(KERN_ERR "%s (rtl8180): Unknown RF! (0x%x)\n",
Andrea Merello7d4b8292014-03-15 18:29:38 +01001868 pci_name(pdev), priv->rf_type);
Michael Wuf6532112007-10-14 14:43:16 -04001869 goto err_iounmap;
1870 }
1871
1872 if (!priv->rf) {
1873 printk(KERN_ERR "%s (rtl8180): %s RF frontend not supported!\n",
1874 pci_name(pdev), rf_name);
1875 goto err_iounmap;
1876 }
1877
Andrea Merello7d4b8292014-03-15 18:29:38 +01001878 if (!is_valid_ether_addr(priv->mac_addr)) {
Michael Wuf6532112007-10-14 14:43:16 -04001879 printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
1880 " randomly generated MAC addr\n", pci_name(pdev));
Andrea Merello7d4b8292014-03-15 18:29:38 +01001881 eth_random_addr(priv->mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001882 }
Andrea Merello7d4b8292014-03-15 18:29:38 +01001883 SET_IEEE80211_PERM_ADDR(dev, priv->mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001884
1885 spin_lock_init(&priv->lock);
1886
1887 err = ieee80211_register_hw(dev);
1888 if (err) {
1889 printk(KERN_ERR "%s (rtl8180): Cannot register device\n",
1890 pci_name(pdev));
1891 goto err_iounmap;
1892 }
1893
Joe Perchesc96c31e2010-07-26 14:39:58 -07001894 wiphy_info(dev->wiphy, "hwaddr %pm, %s + %s\n",
Andrea Merello7d4b8292014-03-15 18:29:38 +01001895 priv->mac_addr, chip_name, priv->rf->name);
Michael Wuf6532112007-10-14 14:43:16 -04001896
1897 return 0;
1898
1899 err_iounmap:
andrea merello0269da22014-02-18 02:10:41 +01001900 pci_iounmap(pdev, priv->map);
Michael Wuf6532112007-10-14 14:43:16 -04001901
1902 err_free_dev:
Michael Wuf6532112007-10-14 14:43:16 -04001903 ieee80211_free_hw(dev);
1904
1905 err_free_reg:
1906 pci_release_regions(pdev);
1907 pci_disable_device(pdev);
1908 return err;
1909}
1910
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001911static void rtl8180_remove(struct pci_dev *pdev)
Michael Wuf6532112007-10-14 14:43:16 -04001912{
1913 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
1914 struct rtl8180_priv *priv;
1915
1916 if (!dev)
1917 return;
1918
1919 ieee80211_unregister_hw(dev);
1920
1921 priv = dev->priv;
1922
1923 pci_iounmap(pdev, priv->map);
1924 pci_release_regions(pdev);
1925 pci_disable_device(pdev);
1926 ieee80211_free_hw(dev);
1927}
1928
1929#ifdef CONFIG_PM
1930static int rtl8180_suspend(struct pci_dev *pdev, pm_message_t state)
1931{
1932 pci_save_state(pdev);
1933 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1934 return 0;
1935}
1936
1937static int rtl8180_resume(struct pci_dev *pdev)
1938{
1939 pci_set_power_state(pdev, PCI_D0);
1940 pci_restore_state(pdev);
1941 return 0;
1942}
1943
1944#endif /* CONFIG_PM */
1945
1946static struct pci_driver rtl8180_driver = {
1947 .name = KBUILD_MODNAME,
1948 .id_table = rtl8180_table,
1949 .probe = rtl8180_probe,
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001950 .remove = rtl8180_remove,
Michael Wuf6532112007-10-14 14:43:16 -04001951#ifdef CONFIG_PM
1952 .suspend = rtl8180_suspend,
1953 .resume = rtl8180_resume,
1954#endif /* CONFIG_PM */
1955};
1956
Axel Lin5b0a3b72012-04-14 10:38:36 +08001957module_pci_driver(rtl8180_driver);