blob: 740a58308a90196d42ec08143ebd9d15ee05a534 [file] [log] [blame]
Michael Wuf6532112007-10-14 14:43:16 -04001
2/*
3 * Linux device driver for RTL8180 / RTL8185
4 *
5 * Copyright 2007 Michael Wu <flamingice@sourmilk.net>
Andrea Merello93ba2a82013-08-26 13:53:30 +02006 * Copyright 2007 Andrea Merello <andrea.merello@gmail.com>
Michael Wuf6532112007-10-14 14:43:16 -04007 *
8 * Based on the r8180 driver, which is:
Andrea Merello93ba2a82013-08-26 13:53:30 +02009 * Copyright 2004-2005 Andrea Merello <andrea.merello@gmail.com>, et al.
Michael Wuf6532112007-10-14 14:43:16 -040010 *
11 * Thanks to Realtek for their support!
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 */
17
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000018#include <linux/interrupt.h>
Michael Wuf6532112007-10-14 14:43:16 -040019#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Michael Wuf6532112007-10-14 14:43:16 -040021#include <linux/delay.h>
22#include <linux/etherdevice.h>
23#include <linux/eeprom_93cx6.h>
Paul Gortmaker9d9779e2011-07-03 15:21:01 -040024#include <linux/module.h>
Michael Wuf6532112007-10-14 14:43:16 -040025#include <net/mac80211.h>
26
27#include "rtl8180.h"
John W. Linville3cfeb0c2010-12-20 15:16:53 -050028#include "rtl8225.h"
29#include "sa2400.h"
30#include "max2820.h"
31#include "grf5101.h"
Michael Wuf6532112007-10-14 14:43:16 -040032
33MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
Andrea Merello93ba2a82013-08-26 13:53:30 +020034MODULE_AUTHOR("Andrea Merello <andrea.merello@gmail.com>");
Michael Wuf6532112007-10-14 14:43:16 -040035MODULE_DESCRIPTION("RTL8180 / RTL8185 PCI wireless driver");
36MODULE_LICENSE("GPL");
37
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000038static DEFINE_PCI_DEVICE_TABLE(rtl8180_table) = {
Michael Wuf6532112007-10-14 14:43:16 -040039 /* rtl8185 */
40 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8185) },
Adrian Bassett4fcc5472008-01-23 16:38:33 +000041 { PCI_DEVICE(PCI_VENDOR_ID_BELKIN, 0x700f) },
Michael Wuf6532112007-10-14 14:43:16 -040042 { PCI_DEVICE(PCI_VENDOR_ID_BELKIN, 0x701f) },
43
44 /* rtl8180 */
45 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8180) },
46 { PCI_DEVICE(0x1799, 0x6001) },
47 { PCI_DEVICE(0x1799, 0x6020) },
48 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x3300) },
Xose Vazquez Perez29a6b502012-06-15 17:27:05 +020049 { PCI_DEVICE(0x1186, 0x3301) },
50 { PCI_DEVICE(0x1432, 0x7106) },
Michael Wuf6532112007-10-14 14:43:16 -040051 { }
52};
53
54MODULE_DEVICE_TABLE(pci, rtl8180_table);
55
Johannes Berg8318d782008-01-24 19:38:38 +010056static const struct ieee80211_rate rtl818x_rates[] = {
57 { .bitrate = 10, .hw_value = 0, },
58 { .bitrate = 20, .hw_value = 1, },
59 { .bitrate = 55, .hw_value = 2, },
60 { .bitrate = 110, .hw_value = 3, },
61 { .bitrate = 60, .hw_value = 4, },
62 { .bitrate = 90, .hw_value = 5, },
63 { .bitrate = 120, .hw_value = 6, },
64 { .bitrate = 180, .hw_value = 7, },
65 { .bitrate = 240, .hw_value = 8, },
66 { .bitrate = 360, .hw_value = 9, },
67 { .bitrate = 480, .hw_value = 10, },
68 { .bitrate = 540, .hw_value = 11, },
69};
70
71static const struct ieee80211_channel rtl818x_channels[] = {
72 { .center_freq = 2412 },
73 { .center_freq = 2417 },
74 { .center_freq = 2422 },
75 { .center_freq = 2427 },
76 { .center_freq = 2432 },
77 { .center_freq = 2437 },
78 { .center_freq = 2442 },
79 { .center_freq = 2447 },
80 { .center_freq = 2452 },
81 { .center_freq = 2457 },
82 { .center_freq = 2462 },
83 { .center_freq = 2467 },
84 { .center_freq = 2472 },
85 { .center_freq = 2484 },
86};
87
Andrea Merello3ee44d62014-03-26 21:00:57 +010088/* Queues for rtl8187se card
89 *
90 * name | reg | queue
91 * BC | 7 | 6
92 * MG | 1 | 0
93 * HI | 6 | 1
94 * VO | 5 | 2
95 * VI | 4 | 3
96 * BE | 3 | 4
97 * BK | 2 | 5
98 *
99 * The complete map for DMA kick reg using use all queue is:
100 * static const int rtl8187se_queues_map[RTL8187SE_NR_TX_QUEUES] =
101 * {1, 6, 5, 4, 3, 2, 7};
102 *
103 * .. but.. Because for mac80211 4 queues are enough for QoS we use this
104 *
105 * name | reg | queue
106 * BC | 7 | 4 <- currently not used yet
107 * MG | 1 | x <- Not used
108 * HI | 6 | x <- Not used
109 * VO | 5 | 0 <- used
110 * VI | 4 | 1 <- used
111 * BE | 3 | 2 <- used
112 * BK | 2 | 3 <- used
113 *
114 * Beacon queue could be used, but this is not finished yet.
115 *
116 * I thougth about using the other two queues but I decided not to do this:
117 *
118 * - I'm unsure whether the mac80211 will ever try to use more than 4 queues
119 * by itself.
120 *
121 * - I could route MGMT frames (currently sent over VO queue) to the MGMT
122 * queue but since mac80211 will do not know about it, I will probably gain
123 * some HW priority whenever the VO queue is not empty, but this gain is
124 * limited by the fact that I had to stop the mac80211 queue whenever one of
125 * the VO or MGMT queues is full, stopping also submitting of MGMT frame
126 * to the driver.
127 *
128 * - I don't know how to set in the HW the contention window params for MGMT
129 * and HI-prio queues.
130 */
131
132static const int rtl8187se_queues_map[RTL8187SE_NR_TX_QUEUES] = {5, 4, 3, 2, 7};
133
Andrea Merellofd6564f2014-03-22 18:51:20 +0100134/* Queues for rtl8180/rtl8185 cards
135 *
136 * name | reg | prio
137 * BC | 7 | 3
138 * HI | 6 | 0
139 * NO | 5 | 1
140 * LO | 4 | 2
141 *
142 * The complete map for DMA kick reg using all queue is:
143 * static const int rtl8180_queues_map[RTL8180_NR_TX_QUEUES] = {6, 5, 4, 7};
144 *
145 * .. but .. Because the mac80211 needs at least 4 queues for QoS or
146 * otherwise QoS can't be done, we use just one.
147 * Beacon queue could be used, but this is not finished yet.
148 * Actual map is:
149 *
150 * name | reg | prio
151 * BC | 7 | 1 <- currently not used yet.
152 * HI | 6 | x <- not used
153 * NO | 5 | x <- not used
154 * LO | 4 | 0 <- used
155 */
156
157static const int rtl8180_queues_map[RTL8180_NR_TX_QUEUES] = {4, 7};
Johannes Berg8318d782008-01-24 19:38:38 +0100158
Michael Wuf6532112007-10-14 14:43:16 -0400159void rtl8180_write_phy(struct ieee80211_hw *dev, u8 addr, u32 data)
160{
161 struct rtl8180_priv *priv = dev->priv;
162 int i = 10;
163 u32 buf;
164
165 buf = (data << 8) | addr;
166
167 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf | 0x80);
168 while (i--) {
169 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf);
170 if (rtl818x_ioread8(priv, &priv->map->PHY[2]) == (data & 0xFF))
171 return;
172 }
173}
174
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400175static void rtl8180_handle_rx(struct ieee80211_hw *dev)
Michael Wuf6532112007-10-14 14:43:16 -0400176{
177 struct rtl8180_priv *priv = dev->priv;
Andrea Merello21025922014-03-26 20:59:52 +0100178 struct rtl818x_rx_cmd_desc *cmd_desc;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400179 unsigned int count = 32;
John W. Linville8b73fb82010-07-21 16:26:40 -0400180 u8 signal, agc, sq;
andrea.merello2b4db052014-02-05 22:38:05 +0100181 dma_addr_t mapping;
Michael Wuf6532112007-10-14 14:43:16 -0400182
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400183 while (count--) {
Andrea Merello21025922014-03-26 20:59:52 +0100184 void *entry = priv->rx_ring + priv->rx_idx * priv->rx_ring_sz;
Michael Wuf6532112007-10-14 14:43:16 -0400185 struct sk_buff *skb = priv->rx_buf[priv->rx_idx];
Andrea Merello21025922014-03-26 20:59:52 +0100186 u32 flags, flags2;
187 u64 tsft;
188
189 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
190 struct rtl8187se_rx_desc *desc = entry;
191
192 flags = le32_to_cpu(desc->flags);
193 flags2 = le32_to_cpu(desc->flags2);
194 tsft = le64_to_cpu(desc->tsft);
195 } else {
196 struct rtl8180_rx_desc *desc = entry;
197
198 flags = le32_to_cpu(desc->flags);
199 flags2 = le32_to_cpu(desc->flags2);
200 tsft = le64_to_cpu(desc->tsft);
201 }
Michael Wuf6532112007-10-14 14:43:16 -0400202
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300203 if (flags & RTL818X_RX_DESC_FLAG_OWN)
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400204 return;
Michael Wuf6532112007-10-14 14:43:16 -0400205
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300206 if (unlikely(flags & (RTL818X_RX_DESC_FLAG_DMA_FAIL |
207 RTL818X_RX_DESC_FLAG_FOF |
208 RTL818X_RX_DESC_FLAG_RX_ERR)))
Michael Wuf6532112007-10-14 14:43:16 -0400209 goto done;
210 else {
Michael Wuf6532112007-10-14 14:43:16 -0400211 struct ieee80211_rx_status rx_status = {0};
212 struct sk_buff *new_skb = dev_alloc_skb(MAX_RX_SIZE);
213
214 if (unlikely(!new_skb))
215 goto done;
216
andrea.merello2b4db052014-02-05 22:38:05 +0100217 mapping = pci_map_single(priv->pdev,
218 skb_tail_pointer(new_skb),
219 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
220
221 if (pci_dma_mapping_error(priv->pdev, mapping)) {
222 kfree_skb(new_skb);
223 dev_err(&priv->pdev->dev, "RX DMA map error\n");
224
225 goto done;
226 }
227
Michael Wuf6532112007-10-14 14:43:16 -0400228 pci_unmap_single(priv->pdev,
229 *((dma_addr_t *)skb->cb),
230 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
231 skb_put(skb, flags & 0xFFF);
232
233 rx_status.antenna = (flags2 >> 15) & 1;
Johannes Berg8318d782008-01-24 19:38:38 +0100234 rx_status.rate_idx = (flags >> 20) & 0xF;
John W. Linville8b73fb82010-07-21 16:26:40 -0400235 agc = (flags2 >> 17) & 0x7F;
Andrea Merello6caefd12014-03-08 18:36:37 +0100236
237 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
John W. Linville8b73fb82010-07-21 16:26:40 -0400238 if (rx_status.rate_idx > 3)
239 signal = 90 - clamp_t(u8, agc, 25, 90);
240 else
241 signal = 95 - clamp_t(u8, agc, 30, 95);
Andrea Merello21025922014-03-26 20:59:52 +0100242 } else if (priv->chip_family ==
243 RTL818X_CHIP_FAMILY_RTL8180) {
John W. Linville8b73fb82010-07-21 16:26:40 -0400244 sq = flags2 & 0xff;
245 signal = priv->rf->calc_rssi(agc, sq);
Andrea Merello21025922014-03-26 20:59:52 +0100246 } else {
247 /* TODO: rtl8187se rssi */
248 signal = 10;
John W. Linville8b73fb82010-07-21 16:26:40 -0400249 }
John W. Linville8b749642010-07-19 16:35:20 -0400250 rx_status.signal = signal;
Karl Beldan675a0b02013-03-25 16:26:57 +0100251 rx_status.freq = dev->conf.chandef.chan->center_freq;
252 rx_status.band = dev->conf.chandef.chan->band;
Andrea Merello21025922014-03-26 20:59:52 +0100253 rx_status.mactime = tsft;
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800254 rx_status.flag |= RX_FLAG_MACTIME_START;
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300255 if (flags & RTL818X_RX_DESC_FLAG_CRC32_ERR)
Michael Wuf6532112007-10-14 14:43:16 -0400256 rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
257
Johannes Bergf1d58c22009-06-17 13:13:00 +0200258 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400259 ieee80211_rx_irqsafe(dev, skb);
Michael Wuf6532112007-10-14 14:43:16 -0400260
261 skb = new_skb;
262 priv->rx_buf[priv->rx_idx] = skb;
andrea.merello2b4db052014-02-05 22:38:05 +0100263 *((dma_addr_t *) skb->cb) = mapping;
Michael Wuf6532112007-10-14 14:43:16 -0400264 }
265
266 done:
Andrea Merello21025922014-03-26 20:59:52 +0100267 cmd_desc = entry;
268 cmd_desc->rx_buf = cpu_to_le32(*((dma_addr_t *)skb->cb));
269 cmd_desc->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -0400270 MAX_RX_SIZE);
271 if (priv->rx_idx == 31)
Andrea Merello21025922014-03-26 20:59:52 +0100272 cmd_desc->flags |=
273 cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -0400274 priv->rx_idx = (priv->rx_idx + 1) % 32;
275 }
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400276}
Michael Wuf6532112007-10-14 14:43:16 -0400277
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400278static void rtl8180_handle_tx(struct ieee80211_hw *dev, unsigned int prio)
279{
280 struct rtl8180_priv *priv = dev->priv;
281 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
Michael Wuf6532112007-10-14 14:43:16 -0400282
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400283 while (skb_queue_len(&ring->queue)) {
284 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
285 struct sk_buff *skb;
286 struct ieee80211_tx_info *info;
287 u32 flags = le32_to_cpu(entry->flags);
288
289 if (flags & RTL818X_TX_DESC_FLAG_OWN)
290 return;
291
292 ring->idx = (ring->idx + 1) % ring->entries;
293 skb = __skb_dequeue(&ring->queue);
294 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
295 skb->len, PCI_DMA_TODEVICE);
296
297 info = IEEE80211_SKB_CB(skb);
298 ieee80211_tx_info_clear_status(info);
299
300 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
301 (flags & RTL818X_TX_DESC_FLAG_TX_OK))
302 info->flags |= IEEE80211_TX_STAT_ACK;
303
304 info->status.rates[0].count = (flags & 0xFF) + 1;
305 info->status.rates[1].idx = -1;
306
307 ieee80211_tx_status_irqsafe(dev, skb);
308 if (ring->entries - skb_queue_len(&ring->queue) == 2)
309 ieee80211_wake_queue(dev, prio);
Michael Wuf6532112007-10-14 14:43:16 -0400310 }
311}
312
Andrea Merelloa373ebc2014-03-26 21:00:06 +0100313static irqreturn_t rtl8187se_interrupt(int irq, void *dev_id)
314{
315 struct ieee80211_hw *dev = dev_id;
316 struct rtl8180_priv *priv = dev->priv;
317 u32 reg;
318 unsigned long flags;
319 static int desc_err;
320
321 spin_lock_irqsave(&priv->lock, flags);
322 /* Note: 32-bit interrupt status */
323 reg = rtl818x_ioread32(priv, &priv->map->INT_STATUS_SE);
324 if (unlikely(reg == 0xFFFFFFFF)) {
325 spin_unlock_irqrestore(&priv->lock, flags);
326 return IRQ_HANDLED;
327 }
328
329 rtl818x_iowrite32(priv, &priv->map->INT_STATUS_SE, reg);
330
331 if (reg & IMR_TIMEOUT1)
332 rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
333
334 if (reg & (IMR_TBDOK | IMR_TBDER))
335 rtl8180_handle_tx(dev, 4);
336
337 if (reg & (IMR_TVODOK | IMR_TVODER))
338 rtl8180_handle_tx(dev, 0);
339
340 if (reg & (IMR_TVIDOK | IMR_TVIDER))
341 rtl8180_handle_tx(dev, 1);
342
343 if (reg & (IMR_TBEDOK | IMR_TBEDER))
344 rtl8180_handle_tx(dev, 2);
345
346 if (reg & (IMR_TBKDOK | IMR_TBKDER))
347 rtl8180_handle_tx(dev, 3);
348
349 if (reg & (IMR_ROK | IMR_RER | RTL818X_INT_SE_RX_DU | IMR_RQOSOK))
350 rtl8180_handle_rx(dev);
351 /* The interface sometimes generates several RX DMA descriptor errors
352 * at startup. Do not report these.
353 */
354 if ((reg & RTL818X_INT_SE_RX_DU) && desc_err++ > 2)
355 if (net_ratelimit())
356 wiphy_err(dev->wiphy, "No RX DMA Descriptor avail\n");
357
358 spin_unlock_irqrestore(&priv->lock, flags);
359 return IRQ_HANDLED;
360}
361
Michael Wuf6532112007-10-14 14:43:16 -0400362static irqreturn_t rtl8180_interrupt(int irq, void *dev_id)
363{
364 struct ieee80211_hw *dev = dev_id;
365 struct rtl8180_priv *priv = dev->priv;
366 u16 reg;
367
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400368 spin_lock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400369 reg = rtl818x_ioread16(priv, &priv->map->INT_STATUS);
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400370 if (unlikely(reg == 0xFFFF)) {
371 spin_unlock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400372 return IRQ_HANDLED;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400373 }
Michael Wuf6532112007-10-14 14:43:16 -0400374
375 rtl818x_iowrite16(priv, &priv->map->INT_STATUS, reg);
376
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400377 if (reg & (RTL818X_INT_TXB_OK | RTL818X_INT_TXB_ERR))
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400378 rtl8180_handle_tx(dev, 1);
379
380 if (reg & (RTL818X_INT_TXL_OK | RTL818X_INT_TXL_ERR))
381 rtl8180_handle_tx(dev, 0);
382
383 if (reg & (RTL818X_INT_RX_OK | RTL818X_INT_RX_ERR))
384 rtl8180_handle_rx(dev);
385
386 spin_unlock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400387
388 return IRQ_HANDLED;
389}
390
Thomas Huehn36323f82012-07-23 21:33:42 +0200391static void rtl8180_tx(struct ieee80211_hw *dev,
392 struct ieee80211_tx_control *control,
393 struct sk_buff *skb)
Michael Wuf6532112007-10-14 14:43:16 -0400394{
Johannes Berge039fa42008-05-15 12:55:29 +0200395 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
John W. Linville51e080d2010-05-06 16:26:23 -0400396 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Michael Wuf6532112007-10-14 14:43:16 -0400397 struct rtl8180_priv *priv = dev->priv;
398 struct rtl8180_tx_ring *ring;
399 struct rtl8180_tx_desc *entry;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400400 unsigned long flags;
Andrea Merellofd6564f2014-03-22 18:51:20 +0100401 unsigned int idx, prio, hw_prio;
Michael Wuf6532112007-10-14 14:43:16 -0400402 dma_addr_t mapping;
403 u32 tx_flags;
Johannes Berge6a98542008-10-21 12:40:02 +0200404 u8 rc_flags;
Michael Wuf6532112007-10-14 14:43:16 -0400405 u16 plcp_len = 0;
406 __le16 rts_duration = 0;
Andrea Merello3ee44d62014-03-26 21:00:57 +0100407 /* do arithmetic and then convert to le16 */
408 u16 frame_duration = 0;
Michael Wuf6532112007-10-14 14:43:16 -0400409
Johannes Berge2530082008-05-17 00:57:14 +0200410 prio = skb_get_queue_mapping(skb);
Michael Wuf6532112007-10-14 14:43:16 -0400411 ring = &priv->tx_ring[prio];
412
413 mapping = pci_map_single(priv->pdev, skb->data,
414 skb->len, PCI_DMA_TODEVICE);
415
andrea.merello348f7d42014-02-05 22:38:06 +0100416 if (pci_dma_mapping_error(priv->pdev, mapping)) {
417 kfree_skb(skb);
418 dev_err(&priv->pdev->dev, "TX DMA mapping error\n");
419 return;
andrea.merello348f7d42014-02-05 22:38:06 +0100420 }
421
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300422 tx_flags = RTL818X_TX_DESC_FLAG_OWN | RTL818X_TX_DESC_FLAG_FS |
423 RTL818X_TX_DESC_FLAG_LS |
Johannes Berge039fa42008-05-15 12:55:29 +0200424 (ieee80211_get_tx_rate(dev, info)->hw_value << 24) |
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200425 skb->len;
Michael Wuf6532112007-10-14 14:43:16 -0400426
Andrea Merello6caefd12014-03-08 18:36:37 +0100427 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300428 tx_flags |= RTL818X_TX_DESC_FLAG_DMA |
429 RTL818X_TX_DESC_FLAG_NO_ENC;
Michael Wuf6532112007-10-14 14:43:16 -0400430
Johannes Berge6a98542008-10-21 12:40:02 +0200431 rc_flags = info->control.rates[0].flags;
432 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300433 tx_flags |= RTL818X_TX_DESC_FLAG_RTS;
Johannes Berge039fa42008-05-15 12:55:29 +0200434 tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
Johannes Berge6a98542008-10-21 12:40:02 +0200435 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300436 tx_flags |= RTL818X_TX_DESC_FLAG_CTS;
Johannes Berge039fa42008-05-15 12:55:29 +0200437 tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
Johannes Bergaa68cbf2008-02-18 14:20:30 +0100438 }
Michael Wuf6532112007-10-14 14:43:16 -0400439
Johannes Berge6a98542008-10-21 12:40:02 +0200440 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
Johannes Berg32bfd352007-12-19 01:31:26 +0100441 rts_duration = ieee80211_rts_duration(dev, priv->vif, skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +0200442 info);
Michael Wuf6532112007-10-14 14:43:16 -0400443
Andrea Merello6caefd12014-03-08 18:36:37 +0100444 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400445 unsigned int remainder;
446
447 plcp_len = DIV_ROUND_UP(16 * (skb->len + 4),
Johannes Berge039fa42008-05-15 12:55:29 +0200448 (ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
Michael Wuf6532112007-10-14 14:43:16 -0400449 remainder = (16 * (skb->len + 4)) %
Johannes Berge039fa42008-05-15 12:55:29 +0200450 ((ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
Roel Kluin35a0ace2009-06-22 17:42:21 +0200451 if (remainder <= 6)
Michael Wuf6532112007-10-14 14:43:16 -0400452 plcp_len |= 1 << 15;
453 }
454
Andrea Merello3ee44d62014-03-26 21:00:57 +0100455 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
456 __le16 duration;
457 /* SIFS time (required by HW) is already included by
458 * ieee80211_generic_frame_duration
459 */
460 duration = ieee80211_generic_frame_duration(dev, priv->vif,
461 IEEE80211_BAND_2GHZ, skb->len,
462 ieee80211_get_tx_rate(dev, info));
463
464 frame_duration = priv->ack_time + le16_to_cpu(duration);
465 }
466
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400467 spin_lock_irqsave(&priv->lock, flags);
John W. Linville51e080d2010-05-06 16:26:23 -0400468
469 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
470 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
471 priv->seqno += 0x10;
472 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
473 hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
474 }
475
Michael Wuf6532112007-10-14 14:43:16 -0400476 idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
477 entry = &ring->desc[idx];
478
Andrea Merello3ee44d62014-03-26 21:00:57 +0100479 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
480 entry->frame_duration = cpu_to_le16(frame_duration);
481 entry->frame_len_se = cpu_to_le16(skb->len);
482
483 /* tpc polarity */
484 entry->flags3 = cpu_to_le16(1<<4);
485 } else
486 entry->frame_len = cpu_to_le32(skb->len);
487
Michael Wuf6532112007-10-14 14:43:16 -0400488 entry->rts_duration = rts_duration;
489 entry->plcp_len = cpu_to_le16(plcp_len);
490 entry->tx_buf = cpu_to_le32(mapping);
Andrea Merello3ee44d62014-03-26 21:00:57 +0100491
Johannes Berge6a98542008-10-21 12:40:02 +0200492 entry->flags2 = info->control.rates[1].idx >= 0 ?
Felix Fietkau870abdf2008-10-05 18:04:24 +0200493 ieee80211_get_alt_retry_rate(dev, info, 0)->bitrate << 4 : 0;
Johannes Berge6a98542008-10-21 12:40:02 +0200494 entry->retry_limit = info->control.rates[0].count;
andrea merello4c552a52014-02-18 02:10:45 +0100495
496 /* We must be sure that tx_flags is written last because the HW
497 * looks at it to check if the rest of data is valid or not
498 */
499 wmb();
Michael Wuf6532112007-10-14 14:43:16 -0400500 entry->flags = cpu_to_le32(tx_flags);
andrea merelloc24782e2014-02-18 02:10:46 +0100501 /* We must be sure this has been written before followings HW
502 * register write, because this write will made the HW attempts
503 * to DMA the just-written data
504 */
505 wmb();
506
Michael Wuf6532112007-10-14 14:43:16 -0400507 __skb_queue_tail(&ring->queue, skb);
508 if (ring->entries - skb_queue_len(&ring->queue) < 2)
John W. Linvilled10e2e02010-04-27 16:57:38 -0400509 ieee80211_stop_queue(dev, prio);
John W. Linville51e080d2010-05-06 16:26:23 -0400510
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400511 spin_unlock_irqrestore(&priv->lock, flags);
Michael Wuf6532112007-10-14 14:43:16 -0400512
Andrea Merello3ee44d62014-03-26 21:00:57 +0100513 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
514 /* just poll: rings are stopped with TPPollStop reg */
515 hw_prio = rtl8187se_queues_map[prio];
516 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
517 (1 << hw_prio));
518 } else {
519 hw_prio = rtl8180_queues_map[prio];
520 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
Andrea Merellofd6564f2014-03-22 18:51:20 +0100521 (1 << hw_prio) | /* ring to poll */
522 (1<<1) | (1<<2));/* stopped rings */
Andrea Merello3ee44d62014-03-26 21:00:57 +0100523 }
Michael Wuf6532112007-10-14 14:43:16 -0400524}
525
526void rtl8180_set_anaparam(struct rtl8180_priv *priv, u32 anaparam)
527{
528 u8 reg;
529
530 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
531 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
532 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
533 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
534 rtl818x_iowrite32(priv, &priv->map->ANAPARAM, anaparam);
535 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
536 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
537 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
538}
539
Andrea Merello732c8932014-03-26 21:00:24 +0100540static void rtl8180_int_enable(struct ieee80211_hw *dev)
541{
542 struct rtl8180_priv *priv = dev->priv;
543
544 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
545 rtl818x_iowrite32(priv, &priv->map->IMR, IMR_TMGDOK |
546 IMR_TBDER | IMR_THPDER |
547 IMR_THPDER | IMR_THPDOK |
548 IMR_TVODER | IMR_TVODOK |
549 IMR_TVIDER | IMR_TVIDOK |
550 IMR_TBEDER | IMR_TBEDOK |
551 IMR_TBKDER | IMR_TBKDOK |
552 IMR_RDU | IMR_RER |
553 IMR_ROK | IMR_RQOSOK);
554 } else {
555 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
556 }
557}
558
559static void rtl8180_int_disable(struct ieee80211_hw *dev)
560{
561 struct rtl8180_priv *priv = dev->priv;
562
563 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
564 rtl818x_iowrite32(priv, &priv->map->IMR, 0);
565 } else {
566 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
567 }
568}
569
Andrea Merello516a0932014-03-15 18:29:36 +0100570static void rtl8180_conf_basic_rates(struct ieee80211_hw *dev,
571 u32 rates_mask)
572{
573 struct rtl8180_priv *priv = dev->priv;
574
575 u8 max, min;
576 u16 reg;
577
578 max = fls(rates_mask) - 1;
579 min = ffs(rates_mask) - 1;
580
581 switch (priv->chip_family) {
582
583 case RTL818X_CHIP_FAMILY_RTL8180:
584 /* in 8180 this is NOT a BITMAP */
585 reg = rtl818x_ioread16(priv, &priv->map->BRSR);
586 reg &= ~3;
587 reg |= max;
588 rtl818x_iowrite16(priv, &priv->map->BRSR, reg);
Andrea Merello516a0932014-03-15 18:29:36 +0100589 break;
590
591 case RTL818X_CHIP_FAMILY_RTL8185:
592 /* in 8185 this is a BITMAP */
593 rtl818x_iowrite16(priv, &priv->map->BRSR, rates_mask);
594 rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (max << 4) | min);
595 break;
Andrea Merellod209f3b2014-03-26 20:59:25 +0100596
597 case RTL818X_CHIP_FAMILY_RTL8187SE:
598 /* in 8187se this is a BITMAP */
599 rtl818x_iowrite16(priv, &priv->map->BRSR_8187SE, rates_mask);
600 break;
Andrea Merello516a0932014-03-15 18:29:36 +0100601 }
602}
603
Andrea Merellof1026df2014-03-26 21:01:19 +0100604static void rtl8180_config_cardbus(struct ieee80211_hw *dev)
605{
606 struct rtl8180_priv *priv = dev->priv;
607 u16 reg16;
608 u8 reg8;
609
610 reg8 = rtl818x_ioread8(priv, &priv->map->CONFIG3);
611 reg8 |= 1 << 1;
612 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg8);
613
614 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
615 rtl818x_iowrite16(priv, FEMR_SE, 0xffff);
616 } else {
617 reg16 = rtl818x_ioread16(priv, &priv->map->FEMR);
618 reg16 |= (1 << 15) | (1 << 14) | (1 << 4);
619 rtl818x_iowrite16(priv, &priv->map->FEMR, reg16);
620 }
621
622}
623
Michael Wuf6532112007-10-14 14:43:16 -0400624static int rtl8180_init_hw(struct ieee80211_hw *dev)
625{
626 struct rtl8180_priv *priv = dev->priv;
627 u16 reg;
628
629 rtl818x_iowrite8(priv, &priv->map->CMD, 0);
630 rtl818x_ioread8(priv, &priv->map->CMD);
631 msleep(10);
632
633 /* reset */
Andrea Merello732c8932014-03-26 21:00:24 +0100634 rtl8180_int_disable(dev);
Michael Wuf6532112007-10-14 14:43:16 -0400635 rtl818x_ioread8(priv, &priv->map->CMD);
636
637 reg = rtl818x_ioread8(priv, &priv->map->CMD);
638 reg &= (1 << 1);
639 reg |= RTL818X_CMD_RESET;
640 rtl818x_iowrite8(priv, &priv->map->CMD, RTL818X_CMD_RESET);
641 rtl818x_ioread8(priv, &priv->map->CMD);
642 msleep(200);
643
644 /* check success of reset */
645 if (rtl818x_ioread8(priv, &priv->map->CMD) & RTL818X_CMD_RESET) {
Joe Perchesc96c31e2010-07-26 14:39:58 -0700646 wiphy_err(dev->wiphy, "reset timeout!\n");
Michael Wuf6532112007-10-14 14:43:16 -0400647 return -ETIMEDOUT;
648 }
649
650 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
651 rtl818x_ioread8(priv, &priv->map->CMD);
652 msleep(200);
653
654 if (rtl818x_ioread8(priv, &priv->map->CONFIG3) & (1 << 3)) {
Andrea Merellof1026df2014-03-26 21:01:19 +0100655 rtl8180_config_cardbus(dev);
Michael Wuf6532112007-10-14 14:43:16 -0400656 }
657
658 rtl818x_iowrite8(priv, &priv->map->MSR, 0);
659
Andrea Merello6caefd12014-03-08 18:36:37 +0100660 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -0400661 rtl8180_set_anaparam(priv, priv->anaparam);
662
663 rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
Andrea Merellofd6564f2014-03-22 18:51:20 +0100664 rtl818x_iowrite32(priv, &priv->map->TBDA, priv->tx_ring[1].dma);
Michael Wuf6532112007-10-14 14:43:16 -0400665 rtl818x_iowrite32(priv, &priv->map->TLPDA, priv->tx_ring[0].dma);
666
667 /* TODO: necessary? specs indicate not */
668 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
669 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
670 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
Andrea Merello6caefd12014-03-08 18:36:37 +0100671 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
Michael Wuf6532112007-10-14 14:43:16 -0400672 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
673 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
674 }
675 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
676
677 /* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */
678
679 /* TODO: turn off hw wep on rtl8180 */
680
681 rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
682
Andrea Merello6caefd12014-03-08 18:36:37 +0100683 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400684 rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
685 rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
Michael Wuf6532112007-10-14 14:43:16 -0400686
687 /* TODO: set ClkRun enable? necessary? */
688 reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
689 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, reg & ~(1 << 6));
690 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
691 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
692 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
693 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
694 } else {
Michael Wuf6532112007-10-14 14:43:16 -0400695 rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);
696
697 rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
698 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, 0x4C);
699 }
700
701 priv->rf->init(dev);
Andrea Merello516a0932014-03-15 18:29:36 +0100702
703 /* default basic rates are 1,2 Mbps for rtl8180. 1,2,6,9,12,18,24 Mbps
704 * otherwise. bitmask 0x3 and 0x01f3 respectively.
705 * NOTE: currenty rtl8225 RF code changes basic rates, so we need to do
706 * this after rf init.
707 * TODO: try to find out whether RF code really needs to do this..
708 */
709 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
710 rtl8180_conf_basic_rates(dev, 0x3);
711 else
712 rtl8180_conf_basic_rates(dev, 0x1f3);
713
Michael Wuf6532112007-10-14 14:43:16 -0400714 return 0;
715}
716
717static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
718{
719 struct rtl8180_priv *priv = dev->priv;
Andrea Merello21025922014-03-26 20:59:52 +0100720 struct rtl818x_rx_cmd_desc *entry;
Michael Wuf6532112007-10-14 14:43:16 -0400721 int i;
722
Andrea Merello21025922014-03-26 20:59:52 +0100723 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
724 priv->rx_ring_sz = sizeof(struct rtl8187se_rx_desc);
725 else
726 priv->rx_ring_sz = sizeof(struct rtl8180_rx_desc);
727
Michael Wuf6532112007-10-14 14:43:16 -0400728 priv->rx_ring = pci_alloc_consistent(priv->pdev,
Andrea Merello21025922014-03-26 20:59:52 +0100729 priv->rx_ring_sz * 32,
Michael Wuf6532112007-10-14 14:43:16 -0400730 &priv->rx_ring_dma);
731
732 if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -0700733 wiphy_err(dev->wiphy, "Cannot allocate RX ring\n");
Michael Wuf6532112007-10-14 14:43:16 -0400734 return -ENOMEM;
735 }
736
Andrea Merello21025922014-03-26 20:59:52 +0100737 memset(priv->rx_ring, 0, priv->rx_ring_sz * 32);
Michael Wuf6532112007-10-14 14:43:16 -0400738 priv->rx_idx = 0;
739
740 for (i = 0; i < 32; i++) {
741 struct sk_buff *skb = dev_alloc_skb(MAX_RX_SIZE);
742 dma_addr_t *mapping;
Andrea Merello21025922014-03-26 20:59:52 +0100743 entry = priv->rx_ring + priv->rx_ring_sz*i;
andrea merello4da18bb2014-02-18 02:10:43 +0100744 if (!skb) {
745 wiphy_err(dev->wiphy, "Cannot allocate RX skb\n");
746 return -ENOMEM;
747 }
Michael Wuf6532112007-10-14 14:43:16 -0400748 priv->rx_buf[i] = skb;
749 mapping = (dma_addr_t *)skb->cb;
750 *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
751 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
andrea merelloec1da082014-02-22 17:57:23 +0100752
753 if (pci_dma_mapping_error(priv->pdev, *mapping)) {
754 kfree_skb(skb);
755 wiphy_err(dev->wiphy, "Cannot map DMA for RX skb\n");
756 return -ENOMEM;
757 }
758
Michael Wuf6532112007-10-14 14:43:16 -0400759 entry->rx_buf = cpu_to_le32(*mapping);
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300760 entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -0400761 MAX_RX_SIZE);
762 }
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300763 entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -0400764 return 0;
765}
766
767static void rtl8180_free_rx_ring(struct ieee80211_hw *dev)
768{
769 struct rtl8180_priv *priv = dev->priv;
770 int i;
771
772 for (i = 0; i < 32; i++) {
773 struct sk_buff *skb = priv->rx_buf[i];
774 if (!skb)
775 continue;
776
777 pci_unmap_single(priv->pdev,
778 *((dma_addr_t *)skb->cb),
779 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
780 kfree_skb(skb);
781 }
782
Andrea Merello21025922014-03-26 20:59:52 +0100783 pci_free_consistent(priv->pdev, priv->rx_ring_sz * 32,
Michael Wuf6532112007-10-14 14:43:16 -0400784 priv->rx_ring, priv->rx_ring_dma);
785 priv->rx_ring = NULL;
786}
787
788static int rtl8180_init_tx_ring(struct ieee80211_hw *dev,
789 unsigned int prio, unsigned int entries)
790{
791 struct rtl8180_priv *priv = dev->priv;
792 struct rtl8180_tx_desc *ring;
793 dma_addr_t dma;
794 int i;
795
796 ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
797 if (!ring || (unsigned long)ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -0700798 wiphy_err(dev->wiphy, "Cannot allocate TX ring (prio = %d)\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -0700799 prio);
Michael Wuf6532112007-10-14 14:43:16 -0400800 return -ENOMEM;
801 }
802
803 memset(ring, 0, sizeof(*ring)*entries);
804 priv->tx_ring[prio].desc = ring;
805 priv->tx_ring[prio].dma = dma;
806 priv->tx_ring[prio].idx = 0;
807 priv->tx_ring[prio].entries = entries;
808 skb_queue_head_init(&priv->tx_ring[prio].queue);
809
810 for (i = 0; i < entries; i++)
811 ring[i].next_tx_desc =
812 cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
813
814 return 0;
815}
816
817static void rtl8180_free_tx_ring(struct ieee80211_hw *dev, unsigned int prio)
818{
819 struct rtl8180_priv *priv = dev->priv;
820 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
821
822 while (skb_queue_len(&ring->queue)) {
823 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
824 struct sk_buff *skb = __skb_dequeue(&ring->queue);
825
826 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
827 skb->len, PCI_DMA_TODEVICE);
Michael Wuf6532112007-10-14 14:43:16 -0400828 kfree_skb(skb);
829 ring->idx = (ring->idx + 1) % ring->entries;
830 }
831
832 pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
833 ring->desc, ring->dma);
834 ring->desc = NULL;
835}
836
837static int rtl8180_start(struct ieee80211_hw *dev)
838{
839 struct rtl8180_priv *priv = dev->priv;
840 int ret, i;
841 u32 reg;
842
843 ret = rtl8180_init_rx_ring(dev);
844 if (ret)
845 return ret;
846
Andrea Merellofd6564f2014-03-22 18:51:20 +0100847 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -0400848 if ((ret = rtl8180_init_tx_ring(dev, i, 16)))
849 goto err_free_rings;
850
851 ret = rtl8180_init_hw(dev);
852 if (ret)
853 goto err_free_rings;
854
Andrea Merelloa373ebc2014-03-26 21:00:06 +0100855 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
856 ret = request_irq(priv->pdev->irq, rtl8187se_interrupt,
Michael Wuf6532112007-10-14 14:43:16 -0400857 IRQF_SHARED, KBUILD_MODNAME, dev);
Andrea Merelloa373ebc2014-03-26 21:00:06 +0100858 } else {
859 ret = request_irq(priv->pdev->irq, rtl8180_interrupt,
860 IRQF_SHARED, KBUILD_MODNAME, dev);
861 }
862
Michael Wuf6532112007-10-14 14:43:16 -0400863 if (ret) {
Joe Perches5db55842010-08-11 19:11:19 -0700864 wiphy_err(dev->wiphy, "failed to register IRQ handler\n");
Michael Wuf6532112007-10-14 14:43:16 -0400865 goto err_free_rings;
866 }
867
Andrea Merello732c8932014-03-26 21:00:24 +0100868 rtl8180_int_enable(dev);
Michael Wuf6532112007-10-14 14:43:16 -0400869
Andrea Merellof18f1122014-03-26 21:00:42 +0100870 /* in rtl8187se at MAR regs offset there is the management
871 * TX descriptor DMA addres..
872 */
873 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8187SE) {
874 rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
875 rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
876 }
Michael Wuf6532112007-10-14 14:43:16 -0400877
878 reg = RTL818X_RX_CONF_ONLYERLPKT |
879 RTL818X_RX_CONF_RX_AUTORESETPHY |
880 RTL818X_RX_CONF_MGMT |
881 RTL818X_RX_CONF_DATA |
882 (7 << 8 /* MAX RX DMA */) |
883 RTL818X_RX_CONF_BROADCAST |
884 RTL818X_RX_CONF_NICMAC;
885
Andrea Merello6caefd12014-03-08 18:36:37 +0100886 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185)
Michael Wuf6532112007-10-14 14:43:16 -0400887 reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
888 else {
889 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
890 ? RTL818X_RX_CONF_CSDM1 : 0;
891 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
892 ? RTL818X_RX_CONF_CSDM2 : 0;
893 }
894
895 priv->rx_conf = reg;
896 rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
897
Andrea Merello6caefd12014-03-08 18:36:37 +0100898 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400899 reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
andrea merello14c76152014-02-18 02:10:44 +0100900
901 /* CW is not on per-packet basis.
902 * in rtl8185 the CW_VALUE reg is used.
903 */
andrea merello6f7343d2014-01-21 20:16:43 +0100904 reg &= ~RTL818X_CW_CONF_PERPACKET_CW;
andrea merello14c76152014-02-18 02:10:44 +0100905 /* retry limit IS on per-packet basis.
906 * the short and long retry limit in TX_CONF
907 * reg are ignored
908 */
andrea merello6f7343d2014-01-21 20:16:43 +0100909 reg |= RTL818X_CW_CONF_PERPACKET_RETRY;
Michael Wuf6532112007-10-14 14:43:16 -0400910 rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
911
912 reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
andrea merello14c76152014-02-18 02:10:44 +0100913 /* TX antenna and TX gain are not on per-packet basis.
914 * TX Antenna is selected by ANTSEL reg (RX in BB regs).
915 * TX gain is selected with CCK_TX_AGC and OFDM_TX_AGC regs
916 */
andrea merello6f7343d2014-01-21 20:16:43 +0100917 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN;
918 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL;
Michael Wuf6532112007-10-14 14:43:16 -0400919 reg |= RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
920 rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
921
922 /* disable early TX */
923 rtl818x_iowrite8(priv, (u8 __iomem *)priv->map + 0xec, 0x3f);
924 }
925
926 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
927 reg |= (6 << 21 /* MAX TX DMA */) |
928 RTL818X_TX_CONF_NO_ICV;
929
Andrea Merello6caefd12014-03-08 18:36:37 +0100930
931
932 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -0400933 reg &= ~RTL818X_TX_CONF_PROBE_DTS;
934 else
935 reg &= ~RTL818X_TX_CONF_HW_SEQNUM;
936
andrea merelloe74075a2014-02-18 02:10:40 +0100937 reg &= ~RTL818X_TX_CONF_DISCW;
938
Michael Wuf6532112007-10-14 14:43:16 -0400939 /* different meaning, same value on both rtl8185 and rtl8180 */
940 reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;
941
942 rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
943
944 reg = rtl818x_ioread8(priv, &priv->map->CMD);
945 reg |= RTL818X_CMD_RX_ENABLE;
946 reg |= RTL818X_CMD_TX_ENABLE;
947 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
948
Michael Wuf6532112007-10-14 14:43:16 -0400949 return 0;
950
951 err_free_rings:
952 rtl8180_free_rx_ring(dev);
Andrea Merellofd6564f2014-03-22 18:51:20 +0100953 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -0400954 if (priv->tx_ring[i].desc)
955 rtl8180_free_tx_ring(dev, i);
956
957 return ret;
958}
959
960static void rtl8180_stop(struct ieee80211_hw *dev)
961{
962 struct rtl8180_priv *priv = dev->priv;
963 u8 reg;
964 int i;
965
Andrea Merello732c8932014-03-26 21:00:24 +0100966 rtl8180_int_disable(dev);
Michael Wuf6532112007-10-14 14:43:16 -0400967
968 reg = rtl818x_ioread8(priv, &priv->map->CMD);
969 reg &= ~RTL818X_CMD_TX_ENABLE;
970 reg &= ~RTL818X_CMD_RX_ENABLE;
971 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
972
973 priv->rf->stop(dev);
974
975 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
976 reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
977 rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
978 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
979
980 free_irq(priv->pdev->irq, dev);
981
982 rtl8180_free_rx_ring(dev);
Andrea Merellofd6564f2014-03-22 18:51:20 +0100983 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -0400984 rtl8180_free_tx_ring(dev, i);
985}
986
Eliad Peller37a41b42011-09-21 14:06:11 +0300987static u64 rtl8180_get_tsf(struct ieee80211_hw *dev,
988 struct ieee80211_vif *vif)
John W. Linvillec809e862010-05-06 16:49:40 -0400989{
990 struct rtl8180_priv *priv = dev->priv;
991
992 return rtl818x_ioread32(priv, &priv->map->TSFT[0]) |
993 (u64)(rtl818x_ioread32(priv, &priv->map->TSFT[1])) << 32;
994}
995
John W. Linvillea3275e22010-06-24 11:08:37 -0400996static void rtl8180_beacon_work(struct work_struct *work)
John W. Linvillec809e862010-05-06 16:49:40 -0400997{
998 struct rtl8180_vif *vif_priv =
999 container_of(work, struct rtl8180_vif, beacon_work.work);
1000 struct ieee80211_vif *vif =
1001 container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
1002 struct ieee80211_hw *dev = vif_priv->dev;
1003 struct ieee80211_mgmt *mgmt;
1004 struct sk_buff *skb;
John W. Linvillec809e862010-05-06 16:49:40 -04001005
1006 /* don't overflow the tx ring */
1007 if (ieee80211_queue_stopped(dev, 0))
1008 goto resched;
1009
1010 /* grab a fresh beacon */
1011 skb = ieee80211_beacon_get(dev, vif);
John W. Linville8f1d2d22010-08-05 13:46:27 -04001012 if (!skb)
1013 goto resched;
John W. Linvillec809e862010-05-06 16:49:40 -04001014
1015 /*
1016 * update beacon timestamp w/ TSF value
1017 * TODO: make hardware update beacon timestamp
1018 */
1019 mgmt = (struct ieee80211_mgmt *)skb->data;
Eliad Peller37a41b42011-09-21 14:06:11 +03001020 mgmt->u.beacon.timestamp = cpu_to_le64(rtl8180_get_tsf(dev, vif));
John W. Linvillec809e862010-05-06 16:49:40 -04001021
1022 /* TODO: use actual beacon queue */
1023 skb_set_queue_mapping(skb, 0);
1024
Thomas Huehn36323f82012-07-23 21:33:42 +02001025 rtl8180_tx(dev, NULL, skb);
John W. Linvillec809e862010-05-06 16:49:40 -04001026
1027resched:
1028 /*
1029 * schedule next beacon
1030 * TODO: use hardware support for beacon timing
1031 */
1032 schedule_delayed_work(&vif_priv->beacon_work,
1033 usecs_to_jiffies(1024 * vif->bss_conf.beacon_int));
1034}
1035
Michael Wuf6532112007-10-14 14:43:16 -04001036static int rtl8180_add_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001037 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -04001038{
1039 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -04001040 struct rtl8180_vif *vif_priv;
Michael Wuf6532112007-10-14 14:43:16 -04001041
John W. Linville643aab62009-12-22 18:13:04 -05001042 /*
1043 * We only support one active interface at a time.
1044 */
1045 if (priv->vif)
1046 return -EBUSY;
Michael Wuf6532112007-10-14 14:43:16 -04001047
Johannes Berg1ed32e42009-12-23 13:15:45 +01001048 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001049 case NL80211_IFTYPE_STATION:
John W. Linvillec809e862010-05-06 16:49:40 -04001050 case NL80211_IFTYPE_ADHOC:
Michael Wuf6532112007-10-14 14:43:16 -04001051 break;
1052 default:
1053 return -EOPNOTSUPP;
1054 }
1055
Johannes Berg1ed32e42009-12-23 13:15:45 +01001056 priv->vif = vif;
Johannes Berg32bfd352007-12-19 01:31:26 +01001057
John W. Linvillec809e862010-05-06 16:49:40 -04001058 /* Initialize driver private area */
1059 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
1060 vif_priv->dev = dev;
1061 INIT_DELAYED_WORK(&vif_priv->beacon_work, rtl8180_beacon_work);
1062 vif_priv->enable_beacon = false;
1063
Michael Wuf6532112007-10-14 14:43:16 -04001064 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
1065 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->MAC[0],
Johannes Berg1ed32e42009-12-23 13:15:45 +01001066 le32_to_cpu(*(__le32 *)vif->addr));
Michael Wuf6532112007-10-14 14:43:16 -04001067 rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->MAC[4],
Johannes Berg1ed32e42009-12-23 13:15:45 +01001068 le16_to_cpu(*(__le16 *)(vif->addr + 4)));
Michael Wuf6532112007-10-14 14:43:16 -04001069 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
1070
1071 return 0;
1072}
1073
1074static void rtl8180_remove_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001075 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -04001076{
1077 struct rtl8180_priv *priv = dev->priv;
Johannes Berg32bfd352007-12-19 01:31:26 +01001078 priv->vif = NULL;
Michael Wuf6532112007-10-14 14:43:16 -04001079}
1080
Johannes Berge8975582008-10-09 12:18:51 +02001081static int rtl8180_config(struct ieee80211_hw *dev, u32 changed)
Michael Wuf6532112007-10-14 14:43:16 -04001082{
1083 struct rtl8180_priv *priv = dev->priv;
Johannes Berge8975582008-10-09 12:18:51 +02001084 struct ieee80211_conf *conf = &dev->conf;
Michael Wuf6532112007-10-14 14:43:16 -04001085
1086 priv->rf->set_chan(dev, conf);
1087
1088 return 0;
1089}
1090
Andrea Merello9069af72014-03-15 18:29:37 +01001091static int rtl8180_conf_tx(struct ieee80211_hw *dev,
1092 struct ieee80211_vif *vif, u16 queue,
1093 const struct ieee80211_tx_queue_params *params)
1094{
1095 struct rtl8180_priv *priv = dev->priv;
1096 u8 cw_min, cw_max;
1097
1098 /* nothing to do ? */
1099 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
1100 return 0;
1101
1102 cw_min = fls(params->cw_min);
1103 cw_max = fls(params->cw_max);
1104
1105 rtl818x_iowrite8(priv, &priv->map->CW_VAL, (cw_max << 4) | cw_min);
1106
1107 return 0;
1108}
1109
1110static void rtl8180_conf_erp(struct ieee80211_hw *dev,
1111 struct ieee80211_bss_conf *info)
1112{
1113 struct rtl8180_priv *priv = dev->priv;
1114 u8 sifs, difs;
1115 int eifs;
1116 u8 hw_eifs;
1117
1118 /* TODO: should we do something ? */
1119 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
1120 return;
1121
1122 /* I _hope_ this means 10uS for the HW.
1123 * In reference code it is 0x22 for
1124 * both rtl8187L and rtl8187SE
1125 */
1126 sifs = 0x22;
1127
1128 if (info->use_short_slot)
1129 priv->slot_time = 9;
1130 else
1131 priv->slot_time = 20;
1132
1133 /* 10 is SIFS time in uS */
1134 difs = 10 + 2 * priv->slot_time;
1135 eifs = 10 + difs + priv->ack_time;
1136
1137 /* HW should use 4uS units for EIFS (I'm sure for rtl8185)*/
1138 hw_eifs = DIV_ROUND_UP(eifs, 4);
1139
1140
1141 rtl818x_iowrite8(priv, &priv->map->SLOT, priv->slot_time);
1142 rtl818x_iowrite8(priv, &priv->map->SIFS, sifs);
1143 rtl818x_iowrite8(priv, &priv->map->DIFS, difs);
1144
1145 /* from reference code. set ack timeout reg = eifs reg */
1146 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, hw_eifs);
1147
1148 /* rtl8187/rtl8185 HW bug. After EIFS is elapsed,
1149 * the HW still wait for DIFS.
1150 * HW uses 4uS units for EIFS.
1151 */
1152 hw_eifs = DIV_ROUND_UP(eifs - difs, 4);
1153
1154 rtl818x_iowrite8(priv, &priv->map->EIFS, hw_eifs);
1155}
1156
John W. Linvilleda81ded2008-11-12 14:37:11 -05001157static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
1158 struct ieee80211_vif *vif,
1159 struct ieee80211_bss_conf *info,
1160 u32 changed)
1161{
1162 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -04001163 struct rtl8180_vif *vif_priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001164 int i;
John W. Linville0f956e72010-07-29 21:50:29 -04001165 u8 reg;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001166
John W. Linvillec809e862010-05-06 16:49:40 -04001167 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
1168
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001169 if (changed & BSS_CHANGED_BSSID) {
1170 for (i = 0; i < ETH_ALEN; i++)
1171 rtl818x_iowrite8(priv, &priv->map->BSSID[i],
1172 info->bssid[i]);
1173
John W. Linville0f956e72010-07-29 21:50:29 -04001174 if (is_valid_ether_addr(info->bssid)) {
1175 if (vif->type == NL80211_IFTYPE_ADHOC)
1176 reg = RTL818X_MSR_ADHOC;
1177 else
1178 reg = RTL818X_MSR_INFRA;
1179 } else
1180 reg = RTL818X_MSR_NO_LINK;
1181 rtl818x_iowrite8(priv, &priv->map->MSR, reg);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001182 }
John W. Linvilleda81ded2008-11-12 14:37:11 -05001183
Andrea Merello516a0932014-03-15 18:29:36 +01001184 if (changed & BSS_CHANGED_BASIC_RATES)
1185 rtl8180_conf_basic_rates(dev, info->basic_rates);
1186
Andrea Merello9069af72014-03-15 18:29:37 +01001187 if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_ERP_PREAMBLE)) {
1188
1189 /* when preamble changes, acktime duration changes, and erp must
1190 * be recalculated. ACK time is calculated at lowest rate.
1191 * Since mac80211 include SIFS time we remove it (-10)
1192 */
1193 priv->ack_time =
1194 le16_to_cpu(ieee80211_generic_frame_duration(dev,
1195 priv->vif,
1196 IEEE80211_BAND_2GHZ, 10,
1197 &priv->rates[0])) - 10;
1198
1199 rtl8180_conf_erp(dev, info);
1200 }
John W. Linvillec809e862010-05-06 16:49:40 -04001201
1202 if (changed & BSS_CHANGED_BEACON_ENABLED)
1203 vif_priv->enable_beacon = info->enable_beacon;
1204
1205 if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON)) {
1206 cancel_delayed_work_sync(&vif_priv->beacon_work);
1207 if (vif_priv->enable_beacon)
1208 schedule_work(&vif_priv->beacon_work.work);
1209 }
John W. Linvilleda81ded2008-11-12 14:37:11 -05001210}
1211
Jiri Pirko22bedad32010-04-01 21:22:57 +00001212static u64 rtl8180_prepare_multicast(struct ieee80211_hw *dev,
1213 struct netdev_hw_addr_list *mc_list)
Johannes Berg3ac64be2009-08-17 16:16:53 +02001214{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001215 return netdev_hw_addr_list_count(mc_list);
Johannes Berg3ac64be2009-08-17 16:16:53 +02001216}
1217
Michael Wuf6532112007-10-14 14:43:16 -04001218static void rtl8180_configure_filter(struct ieee80211_hw *dev,
1219 unsigned int changed_flags,
1220 unsigned int *total_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001221 u64 multicast)
Michael Wuf6532112007-10-14 14:43:16 -04001222{
1223 struct rtl8180_priv *priv = dev->priv;
1224
1225 if (changed_flags & FIF_FCSFAIL)
1226 priv->rx_conf ^= RTL818X_RX_CONF_FCS;
1227 if (changed_flags & FIF_CONTROL)
1228 priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
1229 if (changed_flags & FIF_OTHER_BSS)
1230 priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
Johannes Berg3ac64be2009-08-17 16:16:53 +02001231 if (*total_flags & FIF_ALLMULTI || multicast > 0)
Michael Wuf6532112007-10-14 14:43:16 -04001232 priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
1233 else
1234 priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
1235
1236 *total_flags = 0;
1237
1238 if (priv->rx_conf & RTL818X_RX_CONF_FCS)
1239 *total_flags |= FIF_FCSFAIL;
1240 if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
1241 *total_flags |= FIF_CONTROL;
1242 if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
1243 *total_flags |= FIF_OTHER_BSS;
1244 if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
1245 *total_flags |= FIF_ALLMULTI;
1246
1247 rtl818x_iowrite32(priv, &priv->map->RX_CONF, priv->rx_conf);
1248}
1249
1250static const struct ieee80211_ops rtl8180_ops = {
1251 .tx = rtl8180_tx,
1252 .start = rtl8180_start,
1253 .stop = rtl8180_stop,
1254 .add_interface = rtl8180_add_interface,
1255 .remove_interface = rtl8180_remove_interface,
1256 .config = rtl8180_config,
John W. Linvilleda81ded2008-11-12 14:37:11 -05001257 .bss_info_changed = rtl8180_bss_info_changed,
Andrea Merello9069af72014-03-15 18:29:37 +01001258 .conf_tx = rtl8180_conf_tx,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001259 .prepare_multicast = rtl8180_prepare_multicast,
Michael Wuf6532112007-10-14 14:43:16 -04001260 .configure_filter = rtl8180_configure_filter,
John W. Linvilled2bb8e02010-01-26 16:22:20 -05001261 .get_tsf = rtl8180_get_tsf,
Michael Wuf6532112007-10-14 14:43:16 -04001262};
1263
1264static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
1265{
Andrea Merello7d4b8292014-03-15 18:29:38 +01001266 struct rtl8180_priv *priv = eeprom->data;
Michael Wuf6532112007-10-14 14:43:16 -04001267 u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1268
1269 eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
1270 eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
1271 eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
1272 eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
1273}
1274
1275static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
1276{
Andrea Merello7d4b8292014-03-15 18:29:38 +01001277 struct rtl8180_priv *priv = eeprom->data;
Michael Wuf6532112007-10-14 14:43:16 -04001278 u8 reg = 2 << 6;
1279
1280 if (eeprom->reg_data_in)
1281 reg |= RTL818X_EEPROM_CMD_WRITE;
1282 if (eeprom->reg_data_out)
1283 reg |= RTL818X_EEPROM_CMD_READ;
1284 if (eeprom->reg_data_clock)
1285 reg |= RTL818X_EEPROM_CMD_CK;
1286 if (eeprom->reg_chip_select)
1287 reg |= RTL818X_EEPROM_CMD_CS;
1288
1289 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
1290 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1291 udelay(10);
1292}
1293
Andrea Merello7d4b8292014-03-15 18:29:38 +01001294static void rtl8180_eeprom_read(struct rtl8180_priv *priv)
1295{
1296 struct eeprom_93cx6 eeprom;
1297 int eeprom_cck_table_adr;
1298 u16 eeprom_val;
1299 int i;
1300
1301 eeprom.data = priv;
1302 eeprom.register_read = rtl8180_eeprom_register_read;
1303 eeprom.register_write = rtl8180_eeprom_register_write;
1304 if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
1305 eeprom.width = PCI_EEPROM_WIDTH_93C66;
1306 else
1307 eeprom.width = PCI_EEPROM_WIDTH_93C46;
1308
1309 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
1310 RTL818X_EEPROM_CMD_PROGRAM);
1311 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1312 udelay(10);
1313
1314 eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
1315 eeprom_val &= 0xFF;
1316 priv->rf_type = eeprom_val;
1317
1318 eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
1319 priv->csthreshold = eeprom_val >> 8;
1320
1321 eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)priv->mac_addr, 3);
1322
Andrea Merellofc32ac92014-03-26 21:01:47 +01001323 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1324 eeprom_cck_table_adr = 0x30;
1325 else
1326 eeprom_cck_table_adr = 0x10;
Andrea Merello7d4b8292014-03-15 18:29:38 +01001327
1328 /* CCK TX power */
1329 for (i = 0; i < 14; i += 2) {
1330 u16 txpwr;
1331 eeprom_93cx6_read(&eeprom, eeprom_cck_table_adr + (i >> 1),
1332 &txpwr);
1333 priv->channels[i].hw_value = txpwr & 0xFF;
1334 priv->channels[i + 1].hw_value = txpwr >> 8;
1335 }
1336
1337 /* OFDM TX power */
1338 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
1339 for (i = 0; i < 14; i += 2) {
1340 u16 txpwr;
1341 eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
1342 priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
1343 priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
1344 }
1345 }
1346
1347 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
1348 __le32 anaparam;
1349 eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
1350 priv->anaparam = le32_to_cpu(anaparam);
1351 eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
1352 }
1353
Andrea Merellofc32ac92014-03-26 21:01:47 +01001354 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1355 eeprom_93cx6_read(&eeprom, 0x3F, &eeprom_val);
1356 priv->antenna_diversity_en = !!(eeprom_val & 0x100);
1357 priv->antenna_diversity_default = (eeprom_val & 0xC00) == 0x400;
1358
1359 eeprom_93cx6_read(&eeprom, 0x7C, &eeprom_val);
1360 priv->xtal_out = eeprom_val & 0xF;
1361 priv->xtal_in = (eeprom_val & 0xF0) >> 4;
1362 priv->xtal_cal = !!(eeprom_val & 0x1000);
1363 priv->thermal_meter_val = (eeprom_val & 0xF00) >> 8;
1364 priv->thermal_meter_en = !!(eeprom_val & 0x2000);
1365 }
1366
Andrea Merello7d4b8292014-03-15 18:29:38 +01001367 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
1368 RTL818X_EEPROM_CMD_NORMAL);
1369}
1370
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001371static int rtl8180_probe(struct pci_dev *pdev,
Michael Wuf6532112007-10-14 14:43:16 -04001372 const struct pci_device_id *id)
1373{
1374 struct ieee80211_hw *dev;
1375 struct rtl8180_priv *priv;
1376 unsigned long mem_addr, mem_len;
1377 unsigned int io_addr, io_len;
Andrea Merello7d4b8292014-03-15 18:29:38 +01001378 int err;
Michael Wuf6532112007-10-14 14:43:16 -04001379 const char *chip_name, *rf_name = NULL;
1380 u32 reg;
Michael Wuf6532112007-10-14 14:43:16 -04001381
1382 err = pci_enable_device(pdev);
1383 if (err) {
1384 printk(KERN_ERR "%s (rtl8180): Cannot enable new PCI device\n",
1385 pci_name(pdev));
1386 return err;
1387 }
1388
1389 err = pci_request_regions(pdev, KBUILD_MODNAME);
1390 if (err) {
1391 printk(KERN_ERR "%s (rtl8180): Cannot obtain PCI resources\n",
1392 pci_name(pdev));
1393 return err;
1394 }
1395
1396 io_addr = pci_resource_start(pdev, 0);
1397 io_len = pci_resource_len(pdev, 0);
1398 mem_addr = pci_resource_start(pdev, 1);
1399 mem_len = pci_resource_len(pdev, 1);
1400
1401 if (mem_len < sizeof(struct rtl818x_csr) ||
1402 io_len < sizeof(struct rtl818x_csr)) {
1403 printk(KERN_ERR "%s (rtl8180): Too short PCI resources\n",
1404 pci_name(pdev));
1405 err = -ENOMEM;
1406 goto err_free_reg;
1407 }
1408
John W. Linville9e385c52010-05-10 14:24:34 -04001409 if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) ||
1410 (err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))) {
Michael Wuf6532112007-10-14 14:43:16 -04001411 printk(KERN_ERR "%s (rtl8180): No suitable DMA available\n",
1412 pci_name(pdev));
1413 goto err_free_reg;
1414 }
1415
1416 pci_set_master(pdev);
1417
1418 dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8180_ops);
1419 if (!dev) {
1420 printk(KERN_ERR "%s (rtl8180): ieee80211 alloc failed\n",
1421 pci_name(pdev));
1422 err = -ENOMEM;
1423 goto err_free_reg;
1424 }
1425
1426 priv = dev->priv;
1427 priv->pdev = pdev;
1428
Johannes Berge6a98542008-10-21 12:40:02 +02001429 dev->max_rates = 2;
Michael Wuf6532112007-10-14 14:43:16 -04001430 SET_IEEE80211_DEV(dev, &pdev->dev);
1431 pci_set_drvdata(pdev, dev);
1432
1433 priv->map = pci_iomap(pdev, 1, mem_len);
1434 if (!priv->map)
1435 priv->map = pci_iomap(pdev, 0, io_len);
1436
1437 if (!priv->map) {
1438 printk(KERN_ERR "%s (rtl8180): Cannot map device memory\n",
1439 pci_name(pdev));
1440 goto err_free_dev;
1441 }
1442
Johannes Berg8318d782008-01-24 19:38:38 +01001443 BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
1444 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
1445
Michael Wuf6532112007-10-14 14:43:16 -04001446 memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
1447 memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
Johannes Berg8318d782008-01-24 19:38:38 +01001448
1449 priv->band.band = IEEE80211_BAND_2GHZ;
1450 priv->band.channels = priv->channels;
1451 priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
1452 priv->band.bitrates = priv->rates;
1453 priv->band.n_bitrates = 4;
1454 dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
1455
Michael Wuf6532112007-10-14 14:43:16 -04001456 dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
Bruno Randolf566bfe52008-05-08 19:15:40 +02001457 IEEE80211_HW_RX_INCLUDES_FCS |
1458 IEEE80211_HW_SIGNAL_UNSPEC;
John W. Linvillec809e862010-05-06 16:49:40 -04001459 dev->vif_data_size = sizeof(struct rtl8180_vif);
1460 dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1461 BIT(NL80211_IFTYPE_ADHOC);
Bruno Randolf566bfe52008-05-08 19:15:40 +02001462 dev->max_signal = 65;
Michael Wuf6532112007-10-14 14:43:16 -04001463
1464 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
1465 reg &= RTL818X_TX_CONF_HWVER_MASK;
1466 switch (reg) {
1467 case RTL818X_TX_CONF_R8180_ABCD:
1468 chip_name = "RTL8180";
Andrea Merello6caefd12014-03-08 18:36:37 +01001469 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001470 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001471
Michael Wuf6532112007-10-14 14:43:16 -04001472 case RTL818X_TX_CONF_R8180_F:
1473 chip_name = "RTL8180vF";
Andrea Merello6caefd12014-03-08 18:36:37 +01001474 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001475 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001476
Michael Wuf6532112007-10-14 14:43:16 -04001477 case RTL818X_TX_CONF_R8185_ABC:
1478 chip_name = "RTL8185";
Andrea Merello6caefd12014-03-08 18:36:37 +01001479 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001480 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001481
Michael Wuf6532112007-10-14 14:43:16 -04001482 case RTL818X_TX_CONF_R8185_D:
1483 chip_name = "RTL8185vD";
Andrea Merello6caefd12014-03-08 18:36:37 +01001484 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001485 break;
1486 default:
1487 printk(KERN_ERR "%s (rtl8180): Unknown chip! (0x%x)\n",
1488 pci_name(pdev), reg >> 25);
1489 goto err_iounmap;
1490 }
1491
Andrea Merellofd6564f2014-03-22 18:51:20 +01001492 /* we declare to MAC80211 all the queues except for beacon queue
1493 * that will be eventually handled by DRV.
1494 * TX rings are arranged in such a way that lower is the IDX,
1495 * higher is the priority, in order to achieve direct mapping
1496 * with mac80211, however the beacon queue is an exception and it
1497 * is mapped on the highst tx ring IDX.
1498 */
1499 dev->queues = RTL8180_NR_TX_QUEUES - 1;
1500
Andrea Merello6caefd12014-03-08 18:36:37 +01001501 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Johannes Berg8318d782008-01-24 19:38:38 +01001502 priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
Michael Wuf6532112007-10-14 14:43:16 -04001503 pci_try_set_mwi(pdev);
1504 }
1505
Andrea Merello7d4b8292014-03-15 18:29:38 +01001506 rtl8180_eeprom_read(priv);
Michael Wuf6532112007-10-14 14:43:16 -04001507
Andrea Merello7d4b8292014-03-15 18:29:38 +01001508 switch (priv->rf_type) {
Michael Wuf6532112007-10-14 14:43:16 -04001509 case 1: rf_name = "Intersil";
1510 break;
1511 case 2: rf_name = "RFMD";
1512 break;
1513 case 3: priv->rf = &sa2400_rf_ops;
1514 break;
1515 case 4: priv->rf = &max2820_rf_ops;
1516 break;
1517 case 5: priv->rf = &grf5101_rf_ops;
1518 break;
1519 case 9: priv->rf = rtl8180_detect_rf(dev);
1520 break;
1521 case 10:
1522 rf_name = "RTL8255";
1523 break;
1524 default:
1525 printk(KERN_ERR "%s (rtl8180): Unknown RF! (0x%x)\n",
Andrea Merello7d4b8292014-03-15 18:29:38 +01001526 pci_name(pdev), priv->rf_type);
Michael Wuf6532112007-10-14 14:43:16 -04001527 goto err_iounmap;
1528 }
1529
1530 if (!priv->rf) {
1531 printk(KERN_ERR "%s (rtl8180): %s RF frontend not supported!\n",
1532 pci_name(pdev), rf_name);
1533 goto err_iounmap;
1534 }
1535
Andrea Merello7d4b8292014-03-15 18:29:38 +01001536 if (!is_valid_ether_addr(priv->mac_addr)) {
Michael Wuf6532112007-10-14 14:43:16 -04001537 printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
1538 " randomly generated MAC addr\n", pci_name(pdev));
Andrea Merello7d4b8292014-03-15 18:29:38 +01001539 eth_random_addr(priv->mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001540 }
Andrea Merello7d4b8292014-03-15 18:29:38 +01001541 SET_IEEE80211_PERM_ADDR(dev, priv->mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001542
1543 spin_lock_init(&priv->lock);
1544
1545 err = ieee80211_register_hw(dev);
1546 if (err) {
1547 printk(KERN_ERR "%s (rtl8180): Cannot register device\n",
1548 pci_name(pdev));
1549 goto err_iounmap;
1550 }
1551
Joe Perchesc96c31e2010-07-26 14:39:58 -07001552 wiphy_info(dev->wiphy, "hwaddr %pm, %s + %s\n",
Andrea Merello7d4b8292014-03-15 18:29:38 +01001553 priv->mac_addr, chip_name, priv->rf->name);
Michael Wuf6532112007-10-14 14:43:16 -04001554
1555 return 0;
1556
1557 err_iounmap:
andrea merello0269da22014-02-18 02:10:41 +01001558 pci_iounmap(pdev, priv->map);
Michael Wuf6532112007-10-14 14:43:16 -04001559
1560 err_free_dev:
Michael Wuf6532112007-10-14 14:43:16 -04001561 ieee80211_free_hw(dev);
1562
1563 err_free_reg:
1564 pci_release_regions(pdev);
1565 pci_disable_device(pdev);
1566 return err;
1567}
1568
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001569static void rtl8180_remove(struct pci_dev *pdev)
Michael Wuf6532112007-10-14 14:43:16 -04001570{
1571 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
1572 struct rtl8180_priv *priv;
1573
1574 if (!dev)
1575 return;
1576
1577 ieee80211_unregister_hw(dev);
1578
1579 priv = dev->priv;
1580
1581 pci_iounmap(pdev, priv->map);
1582 pci_release_regions(pdev);
1583 pci_disable_device(pdev);
1584 ieee80211_free_hw(dev);
1585}
1586
1587#ifdef CONFIG_PM
1588static int rtl8180_suspend(struct pci_dev *pdev, pm_message_t state)
1589{
1590 pci_save_state(pdev);
1591 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1592 return 0;
1593}
1594
1595static int rtl8180_resume(struct pci_dev *pdev)
1596{
1597 pci_set_power_state(pdev, PCI_D0);
1598 pci_restore_state(pdev);
1599 return 0;
1600}
1601
1602#endif /* CONFIG_PM */
1603
1604static struct pci_driver rtl8180_driver = {
1605 .name = KBUILD_MODNAME,
1606 .id_table = rtl8180_table,
1607 .probe = rtl8180_probe,
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001608 .remove = rtl8180_remove,
Michael Wuf6532112007-10-14 14:43:16 -04001609#ifdef CONFIG_PM
1610 .suspend = rtl8180_suspend,
1611 .resume = rtl8180_resume,
1612#endif /* CONFIG_PM */
1613};
1614
Axel Lin5b0a3b72012-04-14 10:38:36 +08001615module_pci_driver(rtl8180_driver);