blob: 2046fe2f1065d67acdf1c4ea8a91310cb399409a [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"
Andrea Merello711d4ed32014-03-26 21:02:28 +010032#include "rtl8225se.h"
Michael Wuf6532112007-10-14 14:43:16 -040033
34MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
Andrea Merello93ba2a82013-08-26 13:53:30 +020035MODULE_AUTHOR("Andrea Merello <andrea.merello@gmail.com>");
Michael Wuf6532112007-10-14 14:43:16 -040036MODULE_DESCRIPTION("RTL8180 / RTL8185 PCI wireless driver");
37MODULE_LICENSE("GPL");
38
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000039static DEFINE_PCI_DEVICE_TABLE(rtl8180_table) = {
Michael Wuf6532112007-10-14 14:43:16 -040040 /* rtl8185 */
41 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8185) },
Adrian Bassett4fcc5472008-01-23 16:38:33 +000042 { PCI_DEVICE(PCI_VENDOR_ID_BELKIN, 0x700f) },
Michael Wuf6532112007-10-14 14:43:16 -040043 { PCI_DEVICE(PCI_VENDOR_ID_BELKIN, 0x701f) },
44
45 /* rtl8180 */
46 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8180) },
47 { PCI_DEVICE(0x1799, 0x6001) },
48 { PCI_DEVICE(0x1799, 0x6020) },
49 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x3300) },
Xose Vazquez Perez29a6b502012-06-15 17:27:05 +020050 { PCI_DEVICE(0x1186, 0x3301) },
51 { PCI_DEVICE(0x1432, 0x7106) },
Michael Wuf6532112007-10-14 14:43:16 -040052 { }
53};
54
55MODULE_DEVICE_TABLE(pci, rtl8180_table);
56
Johannes Berg8318d782008-01-24 19:38:38 +010057static const struct ieee80211_rate rtl818x_rates[] = {
58 { .bitrate = 10, .hw_value = 0, },
59 { .bitrate = 20, .hw_value = 1, },
60 { .bitrate = 55, .hw_value = 2, },
61 { .bitrate = 110, .hw_value = 3, },
62 { .bitrate = 60, .hw_value = 4, },
63 { .bitrate = 90, .hw_value = 5, },
64 { .bitrate = 120, .hw_value = 6, },
65 { .bitrate = 180, .hw_value = 7, },
66 { .bitrate = 240, .hw_value = 8, },
67 { .bitrate = 360, .hw_value = 9, },
68 { .bitrate = 480, .hw_value = 10, },
69 { .bitrate = 540, .hw_value = 11, },
70};
71
72static const struct ieee80211_channel rtl818x_channels[] = {
73 { .center_freq = 2412 },
74 { .center_freq = 2417 },
75 { .center_freq = 2422 },
76 { .center_freq = 2427 },
77 { .center_freq = 2432 },
78 { .center_freq = 2437 },
79 { .center_freq = 2442 },
80 { .center_freq = 2447 },
81 { .center_freq = 2452 },
82 { .center_freq = 2457 },
83 { .center_freq = 2462 },
84 { .center_freq = 2467 },
85 { .center_freq = 2472 },
86 { .center_freq = 2484 },
87};
88
Andrea Merello3ee44d62014-03-26 21:00:57 +010089/* Queues for rtl8187se card
90 *
91 * name | reg | queue
92 * BC | 7 | 6
93 * MG | 1 | 0
94 * HI | 6 | 1
95 * VO | 5 | 2
96 * VI | 4 | 3
97 * BE | 3 | 4
98 * BK | 2 | 5
99 *
100 * The complete map for DMA kick reg using use all queue is:
101 * static const int rtl8187se_queues_map[RTL8187SE_NR_TX_QUEUES] =
102 * {1, 6, 5, 4, 3, 2, 7};
103 *
104 * .. but.. Because for mac80211 4 queues are enough for QoS we use this
105 *
106 * name | reg | queue
107 * BC | 7 | 4 <- currently not used yet
108 * MG | 1 | x <- Not used
109 * HI | 6 | x <- Not used
110 * VO | 5 | 0 <- used
111 * VI | 4 | 1 <- used
112 * BE | 3 | 2 <- used
113 * BK | 2 | 3 <- used
114 *
115 * Beacon queue could be used, but this is not finished yet.
116 *
117 * I thougth about using the other two queues but I decided not to do this:
118 *
119 * - I'm unsure whether the mac80211 will ever try to use more than 4 queues
120 * by itself.
121 *
122 * - I could route MGMT frames (currently sent over VO queue) to the MGMT
123 * queue but since mac80211 will do not know about it, I will probably gain
124 * some HW priority whenever the VO queue is not empty, but this gain is
125 * limited by the fact that I had to stop the mac80211 queue whenever one of
126 * the VO or MGMT queues is full, stopping also submitting of MGMT frame
127 * to the driver.
128 *
129 * - I don't know how to set in the HW the contention window params for MGMT
130 * and HI-prio queues.
131 */
132
133static const int rtl8187se_queues_map[RTL8187SE_NR_TX_QUEUES] = {5, 4, 3, 2, 7};
134
Andrea Merellofd6564f2014-03-22 18:51:20 +0100135/* Queues for rtl8180/rtl8185 cards
136 *
137 * name | reg | prio
138 * BC | 7 | 3
139 * HI | 6 | 0
140 * NO | 5 | 1
141 * LO | 4 | 2
142 *
143 * The complete map for DMA kick reg using all queue is:
144 * static const int rtl8180_queues_map[RTL8180_NR_TX_QUEUES] = {6, 5, 4, 7};
145 *
146 * .. but .. Because the mac80211 needs at least 4 queues for QoS or
147 * otherwise QoS can't be done, we use just one.
148 * Beacon queue could be used, but this is not finished yet.
149 * Actual map is:
150 *
151 * name | reg | prio
152 * BC | 7 | 1 <- currently not used yet.
153 * HI | 6 | x <- not used
154 * NO | 5 | x <- not used
155 * LO | 4 | 0 <- used
156 */
157
158static const int rtl8180_queues_map[RTL8180_NR_TX_QUEUES] = {4, 7};
Johannes Berg8318d782008-01-24 19:38:38 +0100159
Michael Wuf6532112007-10-14 14:43:16 -0400160void rtl8180_write_phy(struct ieee80211_hw *dev, u8 addr, u32 data)
161{
162 struct rtl8180_priv *priv = dev->priv;
163 int i = 10;
164 u32 buf;
165
166 buf = (data << 8) | addr;
167
168 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf | 0x80);
169 while (i--) {
170 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf);
171 if (rtl818x_ioread8(priv, &priv->map->PHY[2]) == (data & 0xFF))
172 return;
173 }
174}
175
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400176static void rtl8180_handle_rx(struct ieee80211_hw *dev)
Michael Wuf6532112007-10-14 14:43:16 -0400177{
178 struct rtl8180_priv *priv = dev->priv;
Andrea Merello21025922014-03-26 20:59:52 +0100179 struct rtl818x_rx_cmd_desc *cmd_desc;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400180 unsigned int count = 32;
John W. Linville8b73fb82010-07-21 16:26:40 -0400181 u8 signal, agc, sq;
andrea.merello2b4db052014-02-05 22:38:05 +0100182 dma_addr_t mapping;
Michael Wuf6532112007-10-14 14:43:16 -0400183
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400184 while (count--) {
Andrea Merello21025922014-03-26 20:59:52 +0100185 void *entry = priv->rx_ring + priv->rx_idx * priv->rx_ring_sz;
Michael Wuf6532112007-10-14 14:43:16 -0400186 struct sk_buff *skb = priv->rx_buf[priv->rx_idx];
Andrea Merello21025922014-03-26 20:59:52 +0100187 u32 flags, flags2;
188 u64 tsft;
189
190 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
191 struct rtl8187se_rx_desc *desc = entry;
192
193 flags = le32_to_cpu(desc->flags);
194 flags2 = le32_to_cpu(desc->flags2);
195 tsft = le64_to_cpu(desc->tsft);
196 } else {
197 struct rtl8180_rx_desc *desc = entry;
198
199 flags = le32_to_cpu(desc->flags);
200 flags2 = le32_to_cpu(desc->flags2);
201 tsft = le64_to_cpu(desc->tsft);
202 }
Michael Wuf6532112007-10-14 14:43:16 -0400203
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300204 if (flags & RTL818X_RX_DESC_FLAG_OWN)
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400205 return;
Michael Wuf6532112007-10-14 14:43:16 -0400206
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300207 if (unlikely(flags & (RTL818X_RX_DESC_FLAG_DMA_FAIL |
208 RTL818X_RX_DESC_FLAG_FOF |
209 RTL818X_RX_DESC_FLAG_RX_ERR)))
Michael Wuf6532112007-10-14 14:43:16 -0400210 goto done;
211 else {
Michael Wuf6532112007-10-14 14:43:16 -0400212 struct ieee80211_rx_status rx_status = {0};
213 struct sk_buff *new_skb = dev_alloc_skb(MAX_RX_SIZE);
214
215 if (unlikely(!new_skb))
216 goto done;
217
andrea.merello2b4db052014-02-05 22:38:05 +0100218 mapping = pci_map_single(priv->pdev,
219 skb_tail_pointer(new_skb),
220 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
221
222 if (pci_dma_mapping_error(priv->pdev, mapping)) {
223 kfree_skb(new_skb);
224 dev_err(&priv->pdev->dev, "RX DMA map error\n");
225
226 goto done;
227 }
228
Michael Wuf6532112007-10-14 14:43:16 -0400229 pci_unmap_single(priv->pdev,
230 *((dma_addr_t *)skb->cb),
231 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
232 skb_put(skb, flags & 0xFFF);
233
234 rx_status.antenna = (flags2 >> 15) & 1;
Johannes Berg8318d782008-01-24 19:38:38 +0100235 rx_status.rate_idx = (flags >> 20) & 0xF;
John W. Linville8b73fb82010-07-21 16:26:40 -0400236 agc = (flags2 >> 17) & 0x7F;
Andrea Merello6caefd12014-03-08 18:36:37 +0100237
238 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
John W. Linville8b73fb82010-07-21 16:26:40 -0400239 if (rx_status.rate_idx > 3)
240 signal = 90 - clamp_t(u8, agc, 25, 90);
241 else
242 signal = 95 - clamp_t(u8, agc, 30, 95);
Andrea Merello21025922014-03-26 20:59:52 +0100243 } else if (priv->chip_family ==
244 RTL818X_CHIP_FAMILY_RTL8180) {
John W. Linville8b73fb82010-07-21 16:26:40 -0400245 sq = flags2 & 0xff;
246 signal = priv->rf->calc_rssi(agc, sq);
Andrea Merello21025922014-03-26 20:59:52 +0100247 } else {
248 /* TODO: rtl8187se rssi */
249 signal = 10;
John W. Linville8b73fb82010-07-21 16:26:40 -0400250 }
John W. Linville8b749642010-07-19 16:35:20 -0400251 rx_status.signal = signal;
Karl Beldan675a0b02013-03-25 16:26:57 +0100252 rx_status.freq = dev->conf.chandef.chan->center_freq;
253 rx_status.band = dev->conf.chandef.chan->band;
Andrea Merello21025922014-03-26 20:59:52 +0100254 rx_status.mactime = tsft;
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800255 rx_status.flag |= RX_FLAG_MACTIME_START;
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300256 if (flags & RTL818X_RX_DESC_FLAG_CRC32_ERR)
Michael Wuf6532112007-10-14 14:43:16 -0400257 rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
258
Johannes Bergf1d58c22009-06-17 13:13:00 +0200259 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400260 ieee80211_rx_irqsafe(dev, skb);
Michael Wuf6532112007-10-14 14:43:16 -0400261
262 skb = new_skb;
263 priv->rx_buf[priv->rx_idx] = skb;
andrea.merello2b4db052014-02-05 22:38:05 +0100264 *((dma_addr_t *) skb->cb) = mapping;
Michael Wuf6532112007-10-14 14:43:16 -0400265 }
266
267 done:
Andrea Merello21025922014-03-26 20:59:52 +0100268 cmd_desc = entry;
269 cmd_desc->rx_buf = cpu_to_le32(*((dma_addr_t *)skb->cb));
270 cmd_desc->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -0400271 MAX_RX_SIZE);
272 if (priv->rx_idx == 31)
Andrea Merello21025922014-03-26 20:59:52 +0100273 cmd_desc->flags |=
274 cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -0400275 priv->rx_idx = (priv->rx_idx + 1) % 32;
276 }
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400277}
Michael Wuf6532112007-10-14 14:43:16 -0400278
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400279static void rtl8180_handle_tx(struct ieee80211_hw *dev, unsigned int prio)
280{
281 struct rtl8180_priv *priv = dev->priv;
282 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
Michael Wuf6532112007-10-14 14:43:16 -0400283
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400284 while (skb_queue_len(&ring->queue)) {
285 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
286 struct sk_buff *skb;
287 struct ieee80211_tx_info *info;
288 u32 flags = le32_to_cpu(entry->flags);
289
290 if (flags & RTL818X_TX_DESC_FLAG_OWN)
291 return;
292
293 ring->idx = (ring->idx + 1) % ring->entries;
294 skb = __skb_dequeue(&ring->queue);
295 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
296 skb->len, PCI_DMA_TODEVICE);
297
298 info = IEEE80211_SKB_CB(skb);
299 ieee80211_tx_info_clear_status(info);
300
301 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
302 (flags & RTL818X_TX_DESC_FLAG_TX_OK))
303 info->flags |= IEEE80211_TX_STAT_ACK;
304
305 info->status.rates[0].count = (flags & 0xFF) + 1;
306 info->status.rates[1].idx = -1;
307
308 ieee80211_tx_status_irqsafe(dev, skb);
309 if (ring->entries - skb_queue_len(&ring->queue) == 2)
310 ieee80211_wake_queue(dev, prio);
Michael Wuf6532112007-10-14 14:43:16 -0400311 }
312}
313
Andrea Merelloa373ebc2014-03-26 21:00:06 +0100314static irqreturn_t rtl8187se_interrupt(int irq, void *dev_id)
315{
316 struct ieee80211_hw *dev = dev_id;
317 struct rtl8180_priv *priv = dev->priv;
318 u32 reg;
319 unsigned long flags;
320 static int desc_err;
321
322 spin_lock_irqsave(&priv->lock, flags);
323 /* Note: 32-bit interrupt status */
324 reg = rtl818x_ioread32(priv, &priv->map->INT_STATUS_SE);
325 if (unlikely(reg == 0xFFFFFFFF)) {
326 spin_unlock_irqrestore(&priv->lock, flags);
327 return IRQ_HANDLED;
328 }
329
330 rtl818x_iowrite32(priv, &priv->map->INT_STATUS_SE, reg);
331
332 if (reg & IMR_TIMEOUT1)
333 rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
334
335 if (reg & (IMR_TBDOK | IMR_TBDER))
336 rtl8180_handle_tx(dev, 4);
337
338 if (reg & (IMR_TVODOK | IMR_TVODER))
339 rtl8180_handle_tx(dev, 0);
340
341 if (reg & (IMR_TVIDOK | IMR_TVIDER))
342 rtl8180_handle_tx(dev, 1);
343
344 if (reg & (IMR_TBEDOK | IMR_TBEDER))
345 rtl8180_handle_tx(dev, 2);
346
347 if (reg & (IMR_TBKDOK | IMR_TBKDER))
348 rtl8180_handle_tx(dev, 3);
349
350 if (reg & (IMR_ROK | IMR_RER | RTL818X_INT_SE_RX_DU | IMR_RQOSOK))
351 rtl8180_handle_rx(dev);
352 /* The interface sometimes generates several RX DMA descriptor errors
353 * at startup. Do not report these.
354 */
355 if ((reg & RTL818X_INT_SE_RX_DU) && desc_err++ > 2)
356 if (net_ratelimit())
357 wiphy_err(dev->wiphy, "No RX DMA Descriptor avail\n");
358
359 spin_unlock_irqrestore(&priv->lock, flags);
360 return IRQ_HANDLED;
361}
362
Michael Wuf6532112007-10-14 14:43:16 -0400363static irqreturn_t rtl8180_interrupt(int irq, void *dev_id)
364{
365 struct ieee80211_hw *dev = dev_id;
366 struct rtl8180_priv *priv = dev->priv;
367 u16 reg;
368
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400369 spin_lock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400370 reg = rtl818x_ioread16(priv, &priv->map->INT_STATUS);
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400371 if (unlikely(reg == 0xFFFF)) {
372 spin_unlock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400373 return IRQ_HANDLED;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400374 }
Michael Wuf6532112007-10-14 14:43:16 -0400375
376 rtl818x_iowrite16(priv, &priv->map->INT_STATUS, reg);
377
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400378 if (reg & (RTL818X_INT_TXB_OK | RTL818X_INT_TXB_ERR))
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400379 rtl8180_handle_tx(dev, 1);
380
381 if (reg & (RTL818X_INT_TXL_OK | RTL818X_INT_TXL_ERR))
382 rtl8180_handle_tx(dev, 0);
383
384 if (reg & (RTL818X_INT_RX_OK | RTL818X_INT_RX_ERR))
385 rtl8180_handle_rx(dev);
386
387 spin_unlock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400388
389 return IRQ_HANDLED;
390}
391
Thomas Huehn36323f82012-07-23 21:33:42 +0200392static void rtl8180_tx(struct ieee80211_hw *dev,
393 struct ieee80211_tx_control *control,
394 struct sk_buff *skb)
Michael Wuf6532112007-10-14 14:43:16 -0400395{
Johannes Berge039fa42008-05-15 12:55:29 +0200396 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
John W. Linville51e080d2010-05-06 16:26:23 -0400397 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Michael Wuf6532112007-10-14 14:43:16 -0400398 struct rtl8180_priv *priv = dev->priv;
399 struct rtl8180_tx_ring *ring;
400 struct rtl8180_tx_desc *entry;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400401 unsigned long flags;
Andrea Merellofd6564f2014-03-22 18:51:20 +0100402 unsigned int idx, prio, hw_prio;
Michael Wuf6532112007-10-14 14:43:16 -0400403 dma_addr_t mapping;
404 u32 tx_flags;
Johannes Berge6a98542008-10-21 12:40:02 +0200405 u8 rc_flags;
Michael Wuf6532112007-10-14 14:43:16 -0400406 u16 plcp_len = 0;
407 __le16 rts_duration = 0;
Andrea Merello3ee44d62014-03-26 21:00:57 +0100408 /* do arithmetic and then convert to le16 */
409 u16 frame_duration = 0;
Michael Wuf6532112007-10-14 14:43:16 -0400410
Johannes Berge2530082008-05-17 00:57:14 +0200411 prio = skb_get_queue_mapping(skb);
Michael Wuf6532112007-10-14 14:43:16 -0400412 ring = &priv->tx_ring[prio];
413
414 mapping = pci_map_single(priv->pdev, skb->data,
415 skb->len, PCI_DMA_TODEVICE);
416
andrea.merello348f7d42014-02-05 22:38:06 +0100417 if (pci_dma_mapping_error(priv->pdev, mapping)) {
418 kfree_skb(skb);
419 dev_err(&priv->pdev->dev, "TX DMA mapping error\n");
420 return;
andrea.merello348f7d42014-02-05 22:38:06 +0100421 }
422
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300423 tx_flags = RTL818X_TX_DESC_FLAG_OWN | RTL818X_TX_DESC_FLAG_FS |
424 RTL818X_TX_DESC_FLAG_LS |
Johannes Berge039fa42008-05-15 12:55:29 +0200425 (ieee80211_get_tx_rate(dev, info)->hw_value << 24) |
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200426 skb->len;
Michael Wuf6532112007-10-14 14:43:16 -0400427
Andrea Merello6caefd12014-03-08 18:36:37 +0100428 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300429 tx_flags |= RTL818X_TX_DESC_FLAG_DMA |
430 RTL818X_TX_DESC_FLAG_NO_ENC;
Michael Wuf6532112007-10-14 14:43:16 -0400431
Johannes Berge6a98542008-10-21 12:40:02 +0200432 rc_flags = info->control.rates[0].flags;
433 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300434 tx_flags |= RTL818X_TX_DESC_FLAG_RTS;
Johannes Berge039fa42008-05-15 12:55:29 +0200435 tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
Johannes Berge6a98542008-10-21 12:40:02 +0200436 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300437 tx_flags |= RTL818X_TX_DESC_FLAG_CTS;
Johannes Berge039fa42008-05-15 12:55:29 +0200438 tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
Johannes Bergaa68cbf2008-02-18 14:20:30 +0100439 }
Michael Wuf6532112007-10-14 14:43:16 -0400440
Johannes Berge6a98542008-10-21 12:40:02 +0200441 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
Johannes Berg32bfd352007-12-19 01:31:26 +0100442 rts_duration = ieee80211_rts_duration(dev, priv->vif, skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +0200443 info);
Michael Wuf6532112007-10-14 14:43:16 -0400444
Andrea Merello6caefd12014-03-08 18:36:37 +0100445 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400446 unsigned int remainder;
447
448 plcp_len = DIV_ROUND_UP(16 * (skb->len + 4),
Johannes Berge039fa42008-05-15 12:55:29 +0200449 (ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
Michael Wuf6532112007-10-14 14:43:16 -0400450 remainder = (16 * (skb->len + 4)) %
Johannes Berge039fa42008-05-15 12:55:29 +0200451 ((ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
Roel Kluin35a0ace2009-06-22 17:42:21 +0200452 if (remainder <= 6)
Michael Wuf6532112007-10-14 14:43:16 -0400453 plcp_len |= 1 << 15;
454 }
455
Andrea Merello3ee44d62014-03-26 21:00:57 +0100456 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
457 __le16 duration;
458 /* SIFS time (required by HW) is already included by
459 * ieee80211_generic_frame_duration
460 */
461 duration = ieee80211_generic_frame_duration(dev, priv->vif,
462 IEEE80211_BAND_2GHZ, skb->len,
463 ieee80211_get_tx_rate(dev, info));
464
465 frame_duration = priv->ack_time + le16_to_cpu(duration);
466 }
467
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400468 spin_lock_irqsave(&priv->lock, flags);
John W. Linville51e080d2010-05-06 16:26:23 -0400469
470 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
471 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
472 priv->seqno += 0x10;
473 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
474 hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
475 }
476
Michael Wuf6532112007-10-14 14:43:16 -0400477 idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
478 entry = &ring->desc[idx];
479
Andrea Merello3ee44d62014-03-26 21:00:57 +0100480 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
481 entry->frame_duration = cpu_to_le16(frame_duration);
482 entry->frame_len_se = cpu_to_le16(skb->len);
483
484 /* tpc polarity */
485 entry->flags3 = cpu_to_le16(1<<4);
486 } else
487 entry->frame_len = cpu_to_le32(skb->len);
488
Michael Wuf6532112007-10-14 14:43:16 -0400489 entry->rts_duration = rts_duration;
490 entry->plcp_len = cpu_to_le16(plcp_len);
491 entry->tx_buf = cpu_to_le32(mapping);
Andrea Merello3ee44d62014-03-26 21:00:57 +0100492
Johannes Berge6a98542008-10-21 12:40:02 +0200493 entry->flags2 = info->control.rates[1].idx >= 0 ?
Felix Fietkau870abdf2008-10-05 18:04:24 +0200494 ieee80211_get_alt_retry_rate(dev, info, 0)->bitrate << 4 : 0;
Johannes Berge6a98542008-10-21 12:40:02 +0200495 entry->retry_limit = info->control.rates[0].count;
andrea merello4c552a52014-02-18 02:10:45 +0100496
497 /* We must be sure that tx_flags is written last because the HW
498 * looks at it to check if the rest of data is valid or not
499 */
500 wmb();
Michael Wuf6532112007-10-14 14:43:16 -0400501 entry->flags = cpu_to_le32(tx_flags);
andrea merelloc24782e2014-02-18 02:10:46 +0100502 /* We must be sure this has been written before followings HW
503 * register write, because this write will made the HW attempts
504 * to DMA the just-written data
505 */
506 wmb();
507
Michael Wuf6532112007-10-14 14:43:16 -0400508 __skb_queue_tail(&ring->queue, skb);
509 if (ring->entries - skb_queue_len(&ring->queue) < 2)
John W. Linvilled10e2e02010-04-27 16:57:38 -0400510 ieee80211_stop_queue(dev, prio);
John W. Linville51e080d2010-05-06 16:26:23 -0400511
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400512 spin_unlock_irqrestore(&priv->lock, flags);
Michael Wuf6532112007-10-14 14:43:16 -0400513
Andrea Merello3ee44d62014-03-26 21:00:57 +0100514 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
515 /* just poll: rings are stopped with TPPollStop reg */
516 hw_prio = rtl8187se_queues_map[prio];
517 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
518 (1 << hw_prio));
519 } else {
520 hw_prio = rtl8180_queues_map[prio];
521 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
Andrea Merellofd6564f2014-03-22 18:51:20 +0100522 (1 << hw_prio) | /* ring to poll */
523 (1<<1) | (1<<2));/* stopped rings */
Andrea Merello3ee44d62014-03-26 21:00:57 +0100524 }
Michael Wuf6532112007-10-14 14:43:16 -0400525}
526
Andrea Merelloff3cbc22014-03-26 21:02:05 +0100527static void rtl8180_set_anaparam3(struct rtl8180_priv *priv, u16 anaparam3)
528{
529 u8 reg;
530
531 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
532 RTL818X_EEPROM_CMD_CONFIG);
533
534 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
535 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
536 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
537
538 rtl818x_iowrite16(priv, &priv->map->ANAPARAM3, anaparam3);
539
540 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
541 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
542
543 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
544 RTL818X_EEPROM_CMD_NORMAL);
545}
546
547void rtl8180_set_anaparam2(struct rtl8180_priv *priv, u32 anaparam2)
548{
549 u8 reg;
550
551 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
552 RTL818X_EEPROM_CMD_CONFIG);
553
554 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
555 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
556 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
557
558 rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, anaparam2);
559
560 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
561 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
562
563 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
564 RTL818X_EEPROM_CMD_NORMAL);
565}
566
Michael Wuf6532112007-10-14 14:43:16 -0400567void rtl8180_set_anaparam(struct rtl8180_priv *priv, u32 anaparam)
568{
569 u8 reg;
570
571 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
572 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
573 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
574 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
575 rtl818x_iowrite32(priv, &priv->map->ANAPARAM, anaparam);
576 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
577 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
578 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
579}
580
Andrea Merello4a67aa52014-03-26 21:02:46 +0100581static void rtl8187se_mac_config(struct ieee80211_hw *dev)
582{
583 struct rtl8180_priv *priv = dev->priv;
584 u8 reg;
585
586 rtl818x_iowrite32(priv, REG_ADDR4(0x1F0), 0);
587 rtl818x_ioread32(priv, REG_ADDR4(0x1F0));
588 rtl818x_iowrite32(priv, REG_ADDR4(0x1F4), 0);
589 rtl818x_ioread32(priv, REG_ADDR4(0x1F4));
590 rtl818x_iowrite8(priv, REG_ADDR1(0x1F8), 0);
591 rtl818x_ioread8(priv, REG_ADDR1(0x1F8));
592 /* Enable DA10 TX power saving */
593 reg = rtl818x_ioread8(priv, &priv->map->PHY_PR);
594 rtl818x_iowrite8(priv, &priv->map->PHY_PR, reg | 0x04);
595 /* Power */
596 rtl818x_iowrite16(priv, PI_DATA_REG, 0x1000);
597 rtl818x_iowrite16(priv, SI_DATA_REG, 0x1000);
598 /* AFE - default to power ON */
599 rtl818x_iowrite16(priv, REG_ADDR2(0x370), 0x0560);
600 rtl818x_iowrite16(priv, REG_ADDR2(0x372), 0x0560);
601 rtl818x_iowrite16(priv, REG_ADDR2(0x374), 0x0DA4);
602 rtl818x_iowrite16(priv, REG_ADDR2(0x376), 0x0DA4);
603 rtl818x_iowrite16(priv, REG_ADDR2(0x378), 0x0560);
604 rtl818x_iowrite16(priv, REG_ADDR2(0x37A), 0x0560);
605 rtl818x_iowrite16(priv, REG_ADDR2(0x37C), 0x00EC);
606 rtl818x_iowrite16(priv, REG_ADDR2(0x37E), 0x00EC);
607 rtl818x_iowrite8(priv, REG_ADDR1(0x24E), 0x01);
608 /* unknown, needed for suspend to RAM resume */
609 rtl818x_iowrite8(priv, REG_ADDR1(0x0A), 0x72);
610}
611
612static void rtl8187se_set_antenna_config(struct ieee80211_hw *dev, u8 def_ant,
613 bool diversity)
614{
615 struct rtl8180_priv *priv = dev->priv;
616
617 rtl8225_write_phy_cck(dev, 0x0C, 0x09);
618 if (diversity) {
619 if (def_ant == 1) {
620 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x00);
621 rtl8225_write_phy_cck(dev, 0x11, 0xBB);
622 rtl8225_write_phy_cck(dev, 0x01, 0xC7);
623 rtl8225_write_phy_ofdm(dev, 0x0D, 0x54);
624 rtl8225_write_phy_ofdm(dev, 0x18, 0xB2);
625 } else { /* main antenna */
626 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x03);
627 rtl8225_write_phy_cck(dev, 0x11, 0x9B);
628 rtl8225_write_phy_cck(dev, 0x01, 0xC7);
629 rtl8225_write_phy_ofdm(dev, 0x0D, 0x5C);
630 rtl8225_write_phy_ofdm(dev, 0x18, 0xB2);
631 }
632 } else { /* disable antenna diversity */
633 if (def_ant == 1) {
634 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x00);
635 rtl8225_write_phy_cck(dev, 0x11, 0xBB);
636 rtl8225_write_phy_cck(dev, 0x01, 0x47);
637 rtl8225_write_phy_ofdm(dev, 0x0D, 0x54);
638 rtl8225_write_phy_ofdm(dev, 0x18, 0x32);
639 } else { /* main antenna */
640 rtl818x_iowrite8(priv, &priv->map->TX_ANTENNA, 0x03);
641 rtl8225_write_phy_cck(dev, 0x11, 0x9B);
642 rtl8225_write_phy_cck(dev, 0x01, 0x47);
643 rtl8225_write_phy_ofdm(dev, 0x0D, 0x5C);
644 rtl8225_write_phy_ofdm(dev, 0x18, 0x32);
645 }
646 }
647 /* priv->curr_ant = def_ant; */
648}
649
Andrea Merello732c8932014-03-26 21:00:24 +0100650static void rtl8180_int_enable(struct ieee80211_hw *dev)
651{
652 struct rtl8180_priv *priv = dev->priv;
653
654 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
655 rtl818x_iowrite32(priv, &priv->map->IMR, IMR_TMGDOK |
656 IMR_TBDER | IMR_THPDER |
657 IMR_THPDER | IMR_THPDOK |
658 IMR_TVODER | IMR_TVODOK |
659 IMR_TVIDER | IMR_TVIDOK |
660 IMR_TBEDER | IMR_TBEDOK |
661 IMR_TBKDER | IMR_TBKDOK |
662 IMR_RDU | IMR_RER |
663 IMR_ROK | IMR_RQOSOK);
664 } else {
665 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
666 }
667}
668
669static void rtl8180_int_disable(struct ieee80211_hw *dev)
670{
671 struct rtl8180_priv *priv = dev->priv;
672
673 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
674 rtl818x_iowrite32(priv, &priv->map->IMR, 0);
675 } else {
676 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
677 }
678}
679
Andrea Merello516a0932014-03-15 18:29:36 +0100680static void rtl8180_conf_basic_rates(struct ieee80211_hw *dev,
681 u32 rates_mask)
682{
683 struct rtl8180_priv *priv = dev->priv;
684
685 u8 max, min;
686 u16 reg;
687
688 max = fls(rates_mask) - 1;
689 min = ffs(rates_mask) - 1;
690
691 switch (priv->chip_family) {
692
693 case RTL818X_CHIP_FAMILY_RTL8180:
694 /* in 8180 this is NOT a BITMAP */
695 reg = rtl818x_ioread16(priv, &priv->map->BRSR);
696 reg &= ~3;
697 reg |= max;
698 rtl818x_iowrite16(priv, &priv->map->BRSR, reg);
Andrea Merello516a0932014-03-15 18:29:36 +0100699 break;
700
701 case RTL818X_CHIP_FAMILY_RTL8185:
702 /* in 8185 this is a BITMAP */
703 rtl818x_iowrite16(priv, &priv->map->BRSR, rates_mask);
704 rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (max << 4) | min);
705 break;
Andrea Merellod209f3b2014-03-26 20:59:25 +0100706
707 case RTL818X_CHIP_FAMILY_RTL8187SE:
708 /* in 8187se this is a BITMAP */
709 rtl818x_iowrite16(priv, &priv->map->BRSR_8187SE, rates_mask);
710 break;
Andrea Merello516a0932014-03-15 18:29:36 +0100711 }
712}
713
Andrea Merellof1026df2014-03-26 21:01:19 +0100714static void rtl8180_config_cardbus(struct ieee80211_hw *dev)
715{
716 struct rtl8180_priv *priv = dev->priv;
717 u16 reg16;
718 u8 reg8;
719
720 reg8 = rtl818x_ioread8(priv, &priv->map->CONFIG3);
721 reg8 |= 1 << 1;
722 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg8);
723
724 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
725 rtl818x_iowrite16(priv, FEMR_SE, 0xffff);
726 } else {
727 reg16 = rtl818x_ioread16(priv, &priv->map->FEMR);
728 reg16 |= (1 << 15) | (1 << 14) | (1 << 4);
729 rtl818x_iowrite16(priv, &priv->map->FEMR, reg16);
730 }
731
732}
733
Michael Wuf6532112007-10-14 14:43:16 -0400734static int rtl8180_init_hw(struct ieee80211_hw *dev)
735{
736 struct rtl8180_priv *priv = dev->priv;
737 u16 reg;
Andrea Merello4a67aa52014-03-26 21:02:46 +0100738 u32 reg32;
Michael Wuf6532112007-10-14 14:43:16 -0400739
740 rtl818x_iowrite8(priv, &priv->map->CMD, 0);
741 rtl818x_ioread8(priv, &priv->map->CMD);
742 msleep(10);
743
744 /* reset */
Andrea Merello732c8932014-03-26 21:00:24 +0100745 rtl8180_int_disable(dev);
Michael Wuf6532112007-10-14 14:43:16 -0400746 rtl818x_ioread8(priv, &priv->map->CMD);
747
748 reg = rtl818x_ioread8(priv, &priv->map->CMD);
749 reg &= (1 << 1);
750 reg |= RTL818X_CMD_RESET;
751 rtl818x_iowrite8(priv, &priv->map->CMD, RTL818X_CMD_RESET);
752 rtl818x_ioread8(priv, &priv->map->CMD);
753 msleep(200);
754
755 /* check success of reset */
756 if (rtl818x_ioread8(priv, &priv->map->CMD) & RTL818X_CMD_RESET) {
Joe Perchesc96c31e2010-07-26 14:39:58 -0700757 wiphy_err(dev->wiphy, "reset timeout!\n");
Michael Wuf6532112007-10-14 14:43:16 -0400758 return -ETIMEDOUT;
759 }
760
761 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
762 rtl818x_ioread8(priv, &priv->map->CMD);
763 msleep(200);
764
765 if (rtl818x_ioread8(priv, &priv->map->CONFIG3) & (1 << 3)) {
Andrea Merellof1026df2014-03-26 21:01:19 +0100766 rtl8180_config_cardbus(dev);
Michael Wuf6532112007-10-14 14:43:16 -0400767 }
768
Andrea Merello4a67aa52014-03-26 21:02:46 +0100769 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
770 rtl818x_iowrite8(priv, &priv->map->MSR, RTL818X_MSR_ENEDCA);
771 else
772 rtl818x_iowrite8(priv, &priv->map->MSR, 0);
Michael Wuf6532112007-10-14 14:43:16 -0400773
Andrea Merello6caefd12014-03-08 18:36:37 +0100774 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -0400775 rtl8180_set_anaparam(priv, priv->anaparam);
776
777 rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
Andrea Merello4a67aa52014-03-26 21:02:46 +0100778 /* mac80211 queue have higher prio for lower index. The last queue
779 * (that mac80211 is not aware of) is reserved for beacons (and have
780 * the highest priority on the NIC)
781 */
782 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8187SE) {
783 rtl818x_iowrite32(priv, &priv->map->TBDA,
784 priv->tx_ring[1].dma);
785 rtl818x_iowrite32(priv, &priv->map->TLPDA,
786 priv->tx_ring[0].dma);
787 } else {
788 rtl818x_iowrite32(priv, &priv->map->TBDA,
789 priv->tx_ring[4].dma);
790 rtl818x_iowrite32(priv, &priv->map->TVODA,
791 priv->tx_ring[0].dma);
792 rtl818x_iowrite32(priv, &priv->map->TVIDA,
793 priv->tx_ring[1].dma);
794 rtl818x_iowrite32(priv, &priv->map->TBEDA,
795 priv->tx_ring[2].dma);
796 rtl818x_iowrite32(priv, &priv->map->TBKDA,
797 priv->tx_ring[3].dma);
798 }
Michael Wuf6532112007-10-14 14:43:16 -0400799
800 /* TODO: necessary? specs indicate not */
801 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
802 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
803 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
Andrea Merello6caefd12014-03-08 18:36:37 +0100804 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
Michael Wuf6532112007-10-14 14:43:16 -0400805 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
806 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
807 }
808 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
809
810 /* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */
811
812 /* TODO: turn off hw wep on rtl8180 */
813
814 rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
815
Andrea Merello6caefd12014-03-08 18:36:37 +0100816 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400817 rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
818 rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
Andrea Merello4a67aa52014-03-26 21:02:46 +0100819 } else {
820 rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);
Michael Wuf6532112007-10-14 14:43:16 -0400821
Andrea Merello4a67aa52014-03-26 21:02:46 +0100822 rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
823 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, 0x4C);
824 }
825
826 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
Michael Wuf6532112007-10-14 14:43:16 -0400827 /* TODO: set ClkRun enable? necessary? */
828 reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
829 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, reg & ~(1 << 6));
830 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
831 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
832 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
833 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
Andrea Merello4a67aa52014-03-26 21:02:46 +0100834 }
Michael Wuf6532112007-10-14 14:43:16 -0400835
Andrea Merello4a67aa52014-03-26 21:02:46 +0100836 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
837
838 /* the set auto rate fallback bitmask from 1M to 54 Mb/s */
839 rtl818x_iowrite16(priv, ARFR, 0xFFF);
840 rtl818x_ioread16(priv, ARFR);
841
842 /* stop unused queus (no dma alloc) */
843 rtl818x_iowrite8(priv, &priv->map->TPPOLL_STOP,
844 RTL818x_TPPOLL_STOP_MG | RTL818x_TPPOLL_STOP_HI);
845
846 rtl818x_iowrite8(priv, &priv->map->ACM_CONTROL, 0x00);
847 rtl818x_iowrite16(priv, &priv->map->TID_AC_MAP, 0xFA50);
848
849 rtl818x_iowrite16(priv, &priv->map->INT_MIG, 0);
850
851 /* some black magic here.. */
852 rtl8187se_mac_config(dev);
853
854 rtl818x_iowrite16(priv, RFSW_CTRL, 0x569A);
855 rtl818x_ioread16(priv, RFSW_CTRL);
856
857 rtl8180_set_anaparam(priv, RTL8225SE_ANAPARAM_ON);
858 rtl8180_set_anaparam2(priv, RTL8225SE_ANAPARAM2_ON);
859 rtl8180_set_anaparam3(priv, RTL8225SE_ANAPARAM3);
860
861
862 rtl818x_iowrite8(priv, &priv->map->CONFIG5,
863 rtl818x_ioread8(priv, &priv->map->CONFIG5) & 0x7F);
864
865 /*probably this switch led on */
866 rtl818x_iowrite8(priv, &priv->map->PGSELECT,
867 rtl818x_ioread8(priv, &priv->map->PGSELECT) | 0x08);
868
869 rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x0480);
870 rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1BFF);
871 rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x2488);
872
873 rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x4003);
874
875 /* the reference code mac hardcode table write
876 * this reg by doing byte-wide accesses.
877 * It does it just for lowest and highest byte..
878 */
879 reg32 = rtl818x_ioread32(priv, &priv->map->RF_PARA);
880 reg32 &= 0x00ffff00;
881 reg32 |= 0xb8000054;
882 rtl818x_iowrite32(priv, &priv->map->RF_PARA, reg32);
Michael Wuf6532112007-10-14 14:43:16 -0400883 }
884
885 priv->rf->init(dev);
Andrea Merello516a0932014-03-15 18:29:36 +0100886
887 /* default basic rates are 1,2 Mbps for rtl8180. 1,2,6,9,12,18,24 Mbps
888 * otherwise. bitmask 0x3 and 0x01f3 respectively.
889 * NOTE: currenty rtl8225 RF code changes basic rates, so we need to do
890 * this after rf init.
891 * TODO: try to find out whether RF code really needs to do this..
892 */
893 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
894 rtl8180_conf_basic_rates(dev, 0x3);
895 else
896 rtl8180_conf_basic_rates(dev, 0x1f3);
897
Andrea Merello4a67aa52014-03-26 21:02:46 +0100898 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
899 rtl8187se_set_antenna_config(dev,
900 priv->antenna_diversity_default,
901 priv->antenna_diversity_en);
Michael Wuf6532112007-10-14 14:43:16 -0400902 return 0;
903}
904
905static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
906{
907 struct rtl8180_priv *priv = dev->priv;
Andrea Merello21025922014-03-26 20:59:52 +0100908 struct rtl818x_rx_cmd_desc *entry;
Michael Wuf6532112007-10-14 14:43:16 -0400909 int i;
910
Andrea Merello21025922014-03-26 20:59:52 +0100911 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
912 priv->rx_ring_sz = sizeof(struct rtl8187se_rx_desc);
913 else
914 priv->rx_ring_sz = sizeof(struct rtl8180_rx_desc);
915
Michael Wuf6532112007-10-14 14:43:16 -0400916 priv->rx_ring = pci_alloc_consistent(priv->pdev,
Andrea Merello21025922014-03-26 20:59:52 +0100917 priv->rx_ring_sz * 32,
Michael Wuf6532112007-10-14 14:43:16 -0400918 &priv->rx_ring_dma);
919
920 if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -0700921 wiphy_err(dev->wiphy, "Cannot allocate RX ring\n");
Michael Wuf6532112007-10-14 14:43:16 -0400922 return -ENOMEM;
923 }
924
Andrea Merello21025922014-03-26 20:59:52 +0100925 memset(priv->rx_ring, 0, priv->rx_ring_sz * 32);
Michael Wuf6532112007-10-14 14:43:16 -0400926 priv->rx_idx = 0;
927
928 for (i = 0; i < 32; i++) {
929 struct sk_buff *skb = dev_alloc_skb(MAX_RX_SIZE);
930 dma_addr_t *mapping;
Andrea Merello21025922014-03-26 20:59:52 +0100931 entry = priv->rx_ring + priv->rx_ring_sz*i;
andrea merello4da18bb2014-02-18 02:10:43 +0100932 if (!skb) {
933 wiphy_err(dev->wiphy, "Cannot allocate RX skb\n");
934 return -ENOMEM;
935 }
Michael Wuf6532112007-10-14 14:43:16 -0400936 priv->rx_buf[i] = skb;
937 mapping = (dma_addr_t *)skb->cb;
938 *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
939 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
andrea merelloec1da082014-02-22 17:57:23 +0100940
941 if (pci_dma_mapping_error(priv->pdev, *mapping)) {
942 kfree_skb(skb);
943 wiphy_err(dev->wiphy, "Cannot map DMA for RX skb\n");
944 return -ENOMEM;
945 }
946
Michael Wuf6532112007-10-14 14:43:16 -0400947 entry->rx_buf = cpu_to_le32(*mapping);
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300948 entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -0400949 MAX_RX_SIZE);
950 }
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300951 entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -0400952 return 0;
953}
954
955static void rtl8180_free_rx_ring(struct ieee80211_hw *dev)
956{
957 struct rtl8180_priv *priv = dev->priv;
958 int i;
959
960 for (i = 0; i < 32; i++) {
961 struct sk_buff *skb = priv->rx_buf[i];
962 if (!skb)
963 continue;
964
965 pci_unmap_single(priv->pdev,
966 *((dma_addr_t *)skb->cb),
967 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
968 kfree_skb(skb);
969 }
970
Andrea Merello21025922014-03-26 20:59:52 +0100971 pci_free_consistent(priv->pdev, priv->rx_ring_sz * 32,
Michael Wuf6532112007-10-14 14:43:16 -0400972 priv->rx_ring, priv->rx_ring_dma);
973 priv->rx_ring = NULL;
974}
975
976static int rtl8180_init_tx_ring(struct ieee80211_hw *dev,
977 unsigned int prio, unsigned int entries)
978{
979 struct rtl8180_priv *priv = dev->priv;
980 struct rtl8180_tx_desc *ring;
981 dma_addr_t dma;
982 int i;
983
984 ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
985 if (!ring || (unsigned long)ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -0700986 wiphy_err(dev->wiphy, "Cannot allocate TX ring (prio = %d)\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -0700987 prio);
Michael Wuf6532112007-10-14 14:43:16 -0400988 return -ENOMEM;
989 }
990
991 memset(ring, 0, sizeof(*ring)*entries);
992 priv->tx_ring[prio].desc = ring;
993 priv->tx_ring[prio].dma = dma;
994 priv->tx_ring[prio].idx = 0;
995 priv->tx_ring[prio].entries = entries;
996 skb_queue_head_init(&priv->tx_ring[prio].queue);
997
998 for (i = 0; i < entries; i++)
999 ring[i].next_tx_desc =
1000 cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
1001
1002 return 0;
1003}
1004
1005static void rtl8180_free_tx_ring(struct ieee80211_hw *dev, unsigned int prio)
1006{
1007 struct rtl8180_priv *priv = dev->priv;
1008 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
1009
1010 while (skb_queue_len(&ring->queue)) {
1011 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
1012 struct sk_buff *skb = __skb_dequeue(&ring->queue);
1013
1014 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
1015 skb->len, PCI_DMA_TODEVICE);
Michael Wuf6532112007-10-14 14:43:16 -04001016 kfree_skb(skb);
1017 ring->idx = (ring->idx + 1) % ring->entries;
1018 }
1019
1020 pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
1021 ring->desc, ring->dma);
1022 ring->desc = NULL;
1023}
1024
1025static int rtl8180_start(struct ieee80211_hw *dev)
1026{
1027 struct rtl8180_priv *priv = dev->priv;
1028 int ret, i;
1029 u32 reg;
1030
1031 ret = rtl8180_init_rx_ring(dev);
1032 if (ret)
1033 return ret;
1034
Andrea Merellofd6564f2014-03-22 18:51:20 +01001035 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -04001036 if ((ret = rtl8180_init_tx_ring(dev, i, 16)))
1037 goto err_free_rings;
1038
1039 ret = rtl8180_init_hw(dev);
1040 if (ret)
1041 goto err_free_rings;
1042
Andrea Merelloa373ebc2014-03-26 21:00:06 +01001043 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1044 ret = request_irq(priv->pdev->irq, rtl8187se_interrupt,
Michael Wuf6532112007-10-14 14:43:16 -04001045 IRQF_SHARED, KBUILD_MODNAME, dev);
Andrea Merelloa373ebc2014-03-26 21:00:06 +01001046 } else {
1047 ret = request_irq(priv->pdev->irq, rtl8180_interrupt,
1048 IRQF_SHARED, KBUILD_MODNAME, dev);
1049 }
1050
Michael Wuf6532112007-10-14 14:43:16 -04001051 if (ret) {
Joe Perches5db55842010-08-11 19:11:19 -07001052 wiphy_err(dev->wiphy, "failed to register IRQ handler\n");
Michael Wuf6532112007-10-14 14:43:16 -04001053 goto err_free_rings;
1054 }
1055
Andrea Merello732c8932014-03-26 21:00:24 +01001056 rtl8180_int_enable(dev);
Michael Wuf6532112007-10-14 14:43:16 -04001057
Andrea Merellof18f1122014-03-26 21:00:42 +01001058 /* in rtl8187se at MAR regs offset there is the management
1059 * TX descriptor DMA addres..
1060 */
1061 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8187SE) {
1062 rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
1063 rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
1064 }
Michael Wuf6532112007-10-14 14:43:16 -04001065
1066 reg = RTL818X_RX_CONF_ONLYERLPKT |
1067 RTL818X_RX_CONF_RX_AUTORESETPHY |
1068 RTL818X_RX_CONF_MGMT |
1069 RTL818X_RX_CONF_DATA |
1070 (7 << 8 /* MAX RX DMA */) |
1071 RTL818X_RX_CONF_BROADCAST |
1072 RTL818X_RX_CONF_NICMAC;
1073
Andrea Merello6caefd12014-03-08 18:36:37 +01001074 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185)
Michael Wuf6532112007-10-14 14:43:16 -04001075 reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
Andrea Merello4a67aa52014-03-26 21:02:46 +01001076 else if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -04001077 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
1078 ? RTL818X_RX_CONF_CSDM1 : 0;
1079 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
1080 ? RTL818X_RX_CONF_CSDM2 : 0;
Andrea Merello4a67aa52014-03-26 21:02:46 +01001081 } else {
1082 reg &= ~(RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2);
Michael Wuf6532112007-10-14 14:43:16 -04001083 }
1084
1085 priv->rx_conf = reg;
1086 rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
1087
Andrea Merello6caefd12014-03-08 18:36:37 +01001088 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -04001089 reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
andrea merello14c76152014-02-18 02:10:44 +01001090
1091 /* CW is not on per-packet basis.
1092 * in rtl8185 the CW_VALUE reg is used.
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001093 * in rtl8187se the AC param regs are used.
andrea merello14c76152014-02-18 02:10:44 +01001094 */
andrea merello6f7343d2014-01-21 20:16:43 +01001095 reg &= ~RTL818X_CW_CONF_PERPACKET_CW;
andrea merello14c76152014-02-18 02:10:44 +01001096 /* retry limit IS on per-packet basis.
1097 * the short and long retry limit in TX_CONF
1098 * reg are ignored
1099 */
andrea merello6f7343d2014-01-21 20:16:43 +01001100 reg |= RTL818X_CW_CONF_PERPACKET_RETRY;
Michael Wuf6532112007-10-14 14:43:16 -04001101 rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
1102
1103 reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
andrea merello14c76152014-02-18 02:10:44 +01001104 /* TX antenna and TX gain are not on per-packet basis.
1105 * TX Antenna is selected by ANTSEL reg (RX in BB regs).
1106 * TX gain is selected with CCK_TX_AGC and OFDM_TX_AGC regs
1107 */
andrea merello6f7343d2014-01-21 20:16:43 +01001108 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN;
1109 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL;
Michael Wuf6532112007-10-14 14:43:16 -04001110 reg |= RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
1111 rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
1112
1113 /* disable early TX */
1114 rtl818x_iowrite8(priv, (u8 __iomem *)priv->map + 0xec, 0x3f);
1115 }
1116
1117 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
1118 reg |= (6 << 21 /* MAX TX DMA */) |
1119 RTL818X_TX_CONF_NO_ICV;
1120
Andrea Merello4a67aa52014-03-26 21:02:46 +01001121 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1122 reg |= 1<<30; /* "duration procedure mode" */
Andrea Merello6caefd12014-03-08 18:36:37 +01001123
1124 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -04001125 reg &= ~RTL818X_TX_CONF_PROBE_DTS;
1126 else
1127 reg &= ~RTL818X_TX_CONF_HW_SEQNUM;
1128
andrea merelloe74075a2014-02-18 02:10:40 +01001129 reg &= ~RTL818X_TX_CONF_DISCW;
1130
Michael Wuf6532112007-10-14 14:43:16 -04001131 /* different meaning, same value on both rtl8185 and rtl8180 */
1132 reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;
1133
1134 rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
1135
1136 reg = rtl818x_ioread8(priv, &priv->map->CMD);
1137 reg |= RTL818X_CMD_RX_ENABLE;
1138 reg |= RTL818X_CMD_TX_ENABLE;
1139 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
1140
Michael Wuf6532112007-10-14 14:43:16 -04001141 return 0;
1142
1143 err_free_rings:
1144 rtl8180_free_rx_ring(dev);
Andrea Merellofd6564f2014-03-22 18:51:20 +01001145 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -04001146 if (priv->tx_ring[i].desc)
1147 rtl8180_free_tx_ring(dev, i);
1148
1149 return ret;
1150}
1151
1152static void rtl8180_stop(struct ieee80211_hw *dev)
1153{
1154 struct rtl8180_priv *priv = dev->priv;
1155 u8 reg;
1156 int i;
1157
Andrea Merello732c8932014-03-26 21:00:24 +01001158 rtl8180_int_disable(dev);
Michael Wuf6532112007-10-14 14:43:16 -04001159
1160 reg = rtl818x_ioread8(priv, &priv->map->CMD);
1161 reg &= ~RTL818X_CMD_TX_ENABLE;
1162 reg &= ~RTL818X_CMD_RX_ENABLE;
1163 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
1164
1165 priv->rf->stop(dev);
1166
1167 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
1168 reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
1169 rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
1170 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
1171
1172 free_irq(priv->pdev->irq, dev);
1173
1174 rtl8180_free_rx_ring(dev);
Andrea Merellofd6564f2014-03-22 18:51:20 +01001175 for (i = 0; i < (dev->queues + 1); i++)
Michael Wuf6532112007-10-14 14:43:16 -04001176 rtl8180_free_tx_ring(dev, i);
1177}
1178
Eliad Peller37a41b42011-09-21 14:06:11 +03001179static u64 rtl8180_get_tsf(struct ieee80211_hw *dev,
1180 struct ieee80211_vif *vif)
John W. Linvillec809e862010-05-06 16:49:40 -04001181{
1182 struct rtl8180_priv *priv = dev->priv;
1183
1184 return rtl818x_ioread32(priv, &priv->map->TSFT[0]) |
1185 (u64)(rtl818x_ioread32(priv, &priv->map->TSFT[1])) << 32;
1186}
1187
John W. Linvillea3275e22010-06-24 11:08:37 -04001188static void rtl8180_beacon_work(struct work_struct *work)
John W. Linvillec809e862010-05-06 16:49:40 -04001189{
1190 struct rtl8180_vif *vif_priv =
1191 container_of(work, struct rtl8180_vif, beacon_work.work);
1192 struct ieee80211_vif *vif =
1193 container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
1194 struct ieee80211_hw *dev = vif_priv->dev;
1195 struct ieee80211_mgmt *mgmt;
1196 struct sk_buff *skb;
John W. Linvillec809e862010-05-06 16:49:40 -04001197
1198 /* don't overflow the tx ring */
1199 if (ieee80211_queue_stopped(dev, 0))
1200 goto resched;
1201
1202 /* grab a fresh beacon */
1203 skb = ieee80211_beacon_get(dev, vif);
John W. Linville8f1d2d22010-08-05 13:46:27 -04001204 if (!skb)
1205 goto resched;
John W. Linvillec809e862010-05-06 16:49:40 -04001206
1207 /*
1208 * update beacon timestamp w/ TSF value
1209 * TODO: make hardware update beacon timestamp
1210 */
1211 mgmt = (struct ieee80211_mgmt *)skb->data;
Eliad Peller37a41b42011-09-21 14:06:11 +03001212 mgmt->u.beacon.timestamp = cpu_to_le64(rtl8180_get_tsf(dev, vif));
John W. Linvillec809e862010-05-06 16:49:40 -04001213
1214 /* TODO: use actual beacon queue */
1215 skb_set_queue_mapping(skb, 0);
1216
Thomas Huehn36323f82012-07-23 21:33:42 +02001217 rtl8180_tx(dev, NULL, skb);
John W. Linvillec809e862010-05-06 16:49:40 -04001218
1219resched:
1220 /*
1221 * schedule next beacon
1222 * TODO: use hardware support for beacon timing
1223 */
1224 schedule_delayed_work(&vif_priv->beacon_work,
1225 usecs_to_jiffies(1024 * vif->bss_conf.beacon_int));
1226}
1227
Michael Wuf6532112007-10-14 14:43:16 -04001228static int rtl8180_add_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001229 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -04001230{
1231 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -04001232 struct rtl8180_vif *vif_priv;
Michael Wuf6532112007-10-14 14:43:16 -04001233
John W. Linville643aab62009-12-22 18:13:04 -05001234 /*
1235 * We only support one active interface at a time.
1236 */
1237 if (priv->vif)
1238 return -EBUSY;
Michael Wuf6532112007-10-14 14:43:16 -04001239
Johannes Berg1ed32e42009-12-23 13:15:45 +01001240 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001241 case NL80211_IFTYPE_STATION:
John W. Linvillec809e862010-05-06 16:49:40 -04001242 case NL80211_IFTYPE_ADHOC:
Michael Wuf6532112007-10-14 14:43:16 -04001243 break;
1244 default:
1245 return -EOPNOTSUPP;
1246 }
1247
Johannes Berg1ed32e42009-12-23 13:15:45 +01001248 priv->vif = vif;
Johannes Berg32bfd352007-12-19 01:31:26 +01001249
John W. Linvillec809e862010-05-06 16:49:40 -04001250 /* Initialize driver private area */
1251 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
1252 vif_priv->dev = dev;
1253 INIT_DELAYED_WORK(&vif_priv->beacon_work, rtl8180_beacon_work);
1254 vif_priv->enable_beacon = false;
1255
Michael Wuf6532112007-10-14 14:43:16 -04001256 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
1257 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->MAC[0],
Johannes Berg1ed32e42009-12-23 13:15:45 +01001258 le32_to_cpu(*(__le32 *)vif->addr));
Michael Wuf6532112007-10-14 14:43:16 -04001259 rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->MAC[4],
Johannes Berg1ed32e42009-12-23 13:15:45 +01001260 le16_to_cpu(*(__le16 *)(vif->addr + 4)));
Michael Wuf6532112007-10-14 14:43:16 -04001261 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
1262
1263 return 0;
1264}
1265
1266static void rtl8180_remove_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001267 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -04001268{
1269 struct rtl8180_priv *priv = dev->priv;
Johannes Berg32bfd352007-12-19 01:31:26 +01001270 priv->vif = NULL;
Michael Wuf6532112007-10-14 14:43:16 -04001271}
1272
Johannes Berge8975582008-10-09 12:18:51 +02001273static int rtl8180_config(struct ieee80211_hw *dev, u32 changed)
Michael Wuf6532112007-10-14 14:43:16 -04001274{
1275 struct rtl8180_priv *priv = dev->priv;
Johannes Berge8975582008-10-09 12:18:51 +02001276 struct ieee80211_conf *conf = &dev->conf;
Michael Wuf6532112007-10-14 14:43:16 -04001277
1278 priv->rf->set_chan(dev, conf);
1279
1280 return 0;
1281}
1282
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001283static void rtl8187se_conf_ac_parm(struct ieee80211_hw *dev, u8 queue)
1284{
1285 const struct ieee80211_tx_queue_params *params;
1286 struct rtl8180_priv *priv = dev->priv;
1287
1288 /* hw value */
1289 u32 ac_param;
1290
1291 u8 aifs;
1292 u8 txop;
1293 u8 cw_min, cw_max;
1294
1295 params = &priv->queue_param[queue];
1296
1297 cw_min = fls(params->cw_min);
1298 cw_max = fls(params->cw_max);
1299
1300 aifs = 10 + params->aifs * priv->slot_time;
1301
1302 /* TODO: check if txop HW is in us (mult by 32) */
1303 txop = params->txop;
1304
1305 ac_param = txop << AC_PARAM_TXOP_LIMIT_SHIFT |
1306 cw_max << AC_PARAM_ECW_MAX_SHIFT |
1307 cw_min << AC_PARAM_ECW_MIN_SHIFT |
1308 aifs << AC_PARAM_AIFS_SHIFT;
1309
1310 switch (queue) {
1311 case IEEE80211_AC_BK:
1312 rtl818x_iowrite32(priv, &priv->map->AC_BK_PARAM, ac_param);
1313 break;
1314 case IEEE80211_AC_BE:
1315 rtl818x_iowrite32(priv, &priv->map->AC_BE_PARAM, ac_param);
1316 break;
1317 case IEEE80211_AC_VI:
1318 rtl818x_iowrite32(priv, &priv->map->AC_VI_PARAM, ac_param);
1319 break;
1320 case IEEE80211_AC_VO:
1321 rtl818x_iowrite32(priv, &priv->map->AC_VO_PARAM, ac_param);
1322 break;
1323 }
1324}
1325
Andrea Merello9069af72014-03-15 18:29:37 +01001326static int rtl8180_conf_tx(struct ieee80211_hw *dev,
1327 struct ieee80211_vif *vif, u16 queue,
1328 const struct ieee80211_tx_queue_params *params)
1329{
1330 struct rtl8180_priv *priv = dev->priv;
1331 u8 cw_min, cw_max;
1332
1333 /* nothing to do ? */
1334 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
1335 return 0;
1336
1337 cw_min = fls(params->cw_min);
1338 cw_max = fls(params->cw_max);
1339
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001340 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1341 priv->queue_param[queue] = *params;
1342 rtl8187se_conf_ac_parm(dev, queue);
1343 } else
1344 rtl818x_iowrite8(priv, &priv->map->CW_VAL,
1345 (cw_max << 4) | cw_min);
Andrea Merello9069af72014-03-15 18:29:37 +01001346 return 0;
1347}
1348
1349static void rtl8180_conf_erp(struct ieee80211_hw *dev,
1350 struct ieee80211_bss_conf *info)
1351{
1352 struct rtl8180_priv *priv = dev->priv;
1353 u8 sifs, difs;
1354 int eifs;
1355 u8 hw_eifs;
1356
1357 /* TODO: should we do something ? */
1358 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
1359 return;
1360
1361 /* I _hope_ this means 10uS for the HW.
1362 * In reference code it is 0x22 for
1363 * both rtl8187L and rtl8187SE
1364 */
1365 sifs = 0x22;
1366
1367 if (info->use_short_slot)
1368 priv->slot_time = 9;
1369 else
1370 priv->slot_time = 20;
1371
1372 /* 10 is SIFS time in uS */
1373 difs = 10 + 2 * priv->slot_time;
1374 eifs = 10 + difs + priv->ack_time;
1375
1376 /* HW should use 4uS units for EIFS (I'm sure for rtl8185)*/
1377 hw_eifs = DIV_ROUND_UP(eifs, 4);
1378
1379
1380 rtl818x_iowrite8(priv, &priv->map->SLOT, priv->slot_time);
1381 rtl818x_iowrite8(priv, &priv->map->SIFS, sifs);
1382 rtl818x_iowrite8(priv, &priv->map->DIFS, difs);
1383
1384 /* from reference code. set ack timeout reg = eifs reg */
1385 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, hw_eifs);
1386
Andrea Merello355668d2014-03-26 21:03:03 +01001387 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1388 rtl818x_iowrite8(priv, &priv->map->EIFS_8187SE, hw_eifs);
1389 else if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
1390 /* rtl8187/rtl8185 HW bug. After EIFS is elapsed,
1391 * the HW still wait for DIFS.
1392 * HW uses 4uS units for EIFS.
1393 */
1394 hw_eifs = DIV_ROUND_UP(eifs - difs, 4);
Andrea Merello9069af72014-03-15 18:29:37 +01001395
Andrea Merello355668d2014-03-26 21:03:03 +01001396 rtl818x_iowrite8(priv, &priv->map->EIFS, hw_eifs);
1397 }
Andrea Merello9069af72014-03-15 18:29:37 +01001398}
1399
John W. Linvilleda81ded2008-11-12 14:37:11 -05001400static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
1401 struct ieee80211_vif *vif,
1402 struct ieee80211_bss_conf *info,
1403 u32 changed)
1404{
1405 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -04001406 struct rtl8180_vif *vif_priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001407 int i;
John W. Linville0f956e72010-07-29 21:50:29 -04001408 u8 reg;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001409
John W. Linvillec809e862010-05-06 16:49:40 -04001410 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
1411
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001412 if (changed & BSS_CHANGED_BSSID) {
1413 for (i = 0; i < ETH_ALEN; i++)
1414 rtl818x_iowrite8(priv, &priv->map->BSSID[i],
1415 info->bssid[i]);
1416
John W. Linville0f956e72010-07-29 21:50:29 -04001417 if (is_valid_ether_addr(info->bssid)) {
1418 if (vif->type == NL80211_IFTYPE_ADHOC)
1419 reg = RTL818X_MSR_ADHOC;
1420 else
1421 reg = RTL818X_MSR_INFRA;
1422 } else
1423 reg = RTL818X_MSR_NO_LINK;
Andrea Merello833d15a2014-03-26 21:03:17 +01001424
1425 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1426 reg |= RTL818X_MSR_ENEDCA;
1427
John W. Linville0f956e72010-07-29 21:50:29 -04001428 rtl818x_iowrite8(priv, &priv->map->MSR, reg);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001429 }
John W. Linvilleda81ded2008-11-12 14:37:11 -05001430
Andrea Merello516a0932014-03-15 18:29:36 +01001431 if (changed & BSS_CHANGED_BASIC_RATES)
1432 rtl8180_conf_basic_rates(dev, info->basic_rates);
1433
Andrea Merello9069af72014-03-15 18:29:37 +01001434 if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_ERP_PREAMBLE)) {
1435
1436 /* when preamble changes, acktime duration changes, and erp must
1437 * be recalculated. ACK time is calculated at lowest rate.
1438 * Since mac80211 include SIFS time we remove it (-10)
1439 */
1440 priv->ack_time =
1441 le16_to_cpu(ieee80211_generic_frame_duration(dev,
1442 priv->vif,
1443 IEEE80211_BAND_2GHZ, 10,
1444 &priv->rates[0])) - 10;
1445
1446 rtl8180_conf_erp(dev, info);
Andrea Merelloe944b0a2014-03-26 21:03:40 +01001447
1448 /* mac80211 supplies aifs_n to driver and calls
1449 * conf_tx callback whether aifs_n changes, NOT
1450 * when aifs changes.
1451 * Aifs should be recalculated if slot changes.
1452 */
1453 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1454 for (i = 0; i < 4; i++)
1455 rtl8187se_conf_ac_parm(dev, i);
1456 }
Andrea Merello9069af72014-03-15 18:29:37 +01001457 }
John W. Linvillec809e862010-05-06 16:49:40 -04001458
1459 if (changed & BSS_CHANGED_BEACON_ENABLED)
1460 vif_priv->enable_beacon = info->enable_beacon;
1461
1462 if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON)) {
1463 cancel_delayed_work_sync(&vif_priv->beacon_work);
1464 if (vif_priv->enable_beacon)
1465 schedule_work(&vif_priv->beacon_work.work);
1466 }
John W. Linvilleda81ded2008-11-12 14:37:11 -05001467}
1468
Jiri Pirko22bedad32010-04-01 21:22:57 +00001469static u64 rtl8180_prepare_multicast(struct ieee80211_hw *dev,
1470 struct netdev_hw_addr_list *mc_list)
Johannes Berg3ac64be2009-08-17 16:16:53 +02001471{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001472 return netdev_hw_addr_list_count(mc_list);
Johannes Berg3ac64be2009-08-17 16:16:53 +02001473}
1474
Michael Wuf6532112007-10-14 14:43:16 -04001475static void rtl8180_configure_filter(struct ieee80211_hw *dev,
1476 unsigned int changed_flags,
1477 unsigned int *total_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001478 u64 multicast)
Michael Wuf6532112007-10-14 14:43:16 -04001479{
1480 struct rtl8180_priv *priv = dev->priv;
1481
1482 if (changed_flags & FIF_FCSFAIL)
1483 priv->rx_conf ^= RTL818X_RX_CONF_FCS;
1484 if (changed_flags & FIF_CONTROL)
1485 priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
1486 if (changed_flags & FIF_OTHER_BSS)
1487 priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
Johannes Berg3ac64be2009-08-17 16:16:53 +02001488 if (*total_flags & FIF_ALLMULTI || multicast > 0)
Michael Wuf6532112007-10-14 14:43:16 -04001489 priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
1490 else
1491 priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
1492
1493 *total_flags = 0;
1494
1495 if (priv->rx_conf & RTL818X_RX_CONF_FCS)
1496 *total_flags |= FIF_FCSFAIL;
1497 if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
1498 *total_flags |= FIF_CONTROL;
1499 if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
1500 *total_flags |= FIF_OTHER_BSS;
1501 if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
1502 *total_flags |= FIF_ALLMULTI;
1503
1504 rtl818x_iowrite32(priv, &priv->map->RX_CONF, priv->rx_conf);
1505}
1506
1507static const struct ieee80211_ops rtl8180_ops = {
1508 .tx = rtl8180_tx,
1509 .start = rtl8180_start,
1510 .stop = rtl8180_stop,
1511 .add_interface = rtl8180_add_interface,
1512 .remove_interface = rtl8180_remove_interface,
1513 .config = rtl8180_config,
John W. Linvilleda81ded2008-11-12 14:37:11 -05001514 .bss_info_changed = rtl8180_bss_info_changed,
Andrea Merello9069af72014-03-15 18:29:37 +01001515 .conf_tx = rtl8180_conf_tx,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001516 .prepare_multicast = rtl8180_prepare_multicast,
Michael Wuf6532112007-10-14 14:43:16 -04001517 .configure_filter = rtl8180_configure_filter,
John W. Linvilled2bb8e02010-01-26 16:22:20 -05001518 .get_tsf = rtl8180_get_tsf,
Michael Wuf6532112007-10-14 14:43:16 -04001519};
1520
1521static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
1522{
Andrea Merello7d4b8292014-03-15 18:29:38 +01001523 struct rtl8180_priv *priv = eeprom->data;
Michael Wuf6532112007-10-14 14:43:16 -04001524 u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1525
1526 eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
1527 eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
1528 eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
1529 eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
1530}
1531
1532static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
1533{
Andrea Merello7d4b8292014-03-15 18:29:38 +01001534 struct rtl8180_priv *priv = eeprom->data;
Michael Wuf6532112007-10-14 14:43:16 -04001535 u8 reg = 2 << 6;
1536
1537 if (eeprom->reg_data_in)
1538 reg |= RTL818X_EEPROM_CMD_WRITE;
1539 if (eeprom->reg_data_out)
1540 reg |= RTL818X_EEPROM_CMD_READ;
1541 if (eeprom->reg_data_clock)
1542 reg |= RTL818X_EEPROM_CMD_CK;
1543 if (eeprom->reg_chip_select)
1544 reg |= RTL818X_EEPROM_CMD_CS;
1545
1546 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
1547 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1548 udelay(10);
1549}
1550
Andrea Merello7d4b8292014-03-15 18:29:38 +01001551static void rtl8180_eeprom_read(struct rtl8180_priv *priv)
1552{
1553 struct eeprom_93cx6 eeprom;
1554 int eeprom_cck_table_adr;
1555 u16 eeprom_val;
1556 int i;
1557
1558 eeprom.data = priv;
1559 eeprom.register_read = rtl8180_eeprom_register_read;
1560 eeprom.register_write = rtl8180_eeprom_register_write;
1561 if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
1562 eeprom.width = PCI_EEPROM_WIDTH_93C66;
1563 else
1564 eeprom.width = PCI_EEPROM_WIDTH_93C46;
1565
1566 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
1567 RTL818X_EEPROM_CMD_PROGRAM);
1568 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1569 udelay(10);
1570
1571 eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
1572 eeprom_val &= 0xFF;
1573 priv->rf_type = eeprom_val;
1574
1575 eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
1576 priv->csthreshold = eeprom_val >> 8;
1577
1578 eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)priv->mac_addr, 3);
1579
Andrea Merellofc32ac92014-03-26 21:01:47 +01001580 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1581 eeprom_cck_table_adr = 0x30;
1582 else
1583 eeprom_cck_table_adr = 0x10;
Andrea Merello7d4b8292014-03-15 18:29:38 +01001584
1585 /* CCK TX power */
1586 for (i = 0; i < 14; i += 2) {
1587 u16 txpwr;
1588 eeprom_93cx6_read(&eeprom, eeprom_cck_table_adr + (i >> 1),
1589 &txpwr);
1590 priv->channels[i].hw_value = txpwr & 0xFF;
1591 priv->channels[i + 1].hw_value = txpwr >> 8;
1592 }
1593
1594 /* OFDM TX power */
1595 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
1596 for (i = 0; i < 14; i += 2) {
1597 u16 txpwr;
1598 eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
1599 priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
1600 priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
1601 }
1602 }
1603
1604 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
1605 __le32 anaparam;
1606 eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
1607 priv->anaparam = le32_to_cpu(anaparam);
1608 eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
1609 }
1610
Andrea Merellofc32ac92014-03-26 21:01:47 +01001611 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
1612 eeprom_93cx6_read(&eeprom, 0x3F, &eeprom_val);
1613 priv->antenna_diversity_en = !!(eeprom_val & 0x100);
1614 priv->antenna_diversity_default = (eeprom_val & 0xC00) == 0x400;
1615
1616 eeprom_93cx6_read(&eeprom, 0x7C, &eeprom_val);
1617 priv->xtal_out = eeprom_val & 0xF;
1618 priv->xtal_in = (eeprom_val & 0xF0) >> 4;
1619 priv->xtal_cal = !!(eeprom_val & 0x1000);
1620 priv->thermal_meter_val = (eeprom_val & 0xF00) >> 8;
1621 priv->thermal_meter_en = !!(eeprom_val & 0x2000);
1622 }
1623
Andrea Merello7d4b8292014-03-15 18:29:38 +01001624 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
1625 RTL818X_EEPROM_CMD_NORMAL);
1626}
1627
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001628static int rtl8180_probe(struct pci_dev *pdev,
Michael Wuf6532112007-10-14 14:43:16 -04001629 const struct pci_device_id *id)
1630{
1631 struct ieee80211_hw *dev;
1632 struct rtl8180_priv *priv;
1633 unsigned long mem_addr, mem_len;
1634 unsigned int io_addr, io_len;
Andrea Merello7d4b8292014-03-15 18:29:38 +01001635 int err;
Michael Wuf6532112007-10-14 14:43:16 -04001636 const char *chip_name, *rf_name = NULL;
1637 u32 reg;
Michael Wuf6532112007-10-14 14:43:16 -04001638
1639 err = pci_enable_device(pdev);
1640 if (err) {
1641 printk(KERN_ERR "%s (rtl8180): Cannot enable new PCI device\n",
1642 pci_name(pdev));
1643 return err;
1644 }
1645
1646 err = pci_request_regions(pdev, KBUILD_MODNAME);
1647 if (err) {
1648 printk(KERN_ERR "%s (rtl8180): Cannot obtain PCI resources\n",
1649 pci_name(pdev));
1650 return err;
1651 }
1652
1653 io_addr = pci_resource_start(pdev, 0);
1654 io_len = pci_resource_len(pdev, 0);
1655 mem_addr = pci_resource_start(pdev, 1);
1656 mem_len = pci_resource_len(pdev, 1);
1657
1658 if (mem_len < sizeof(struct rtl818x_csr) ||
1659 io_len < sizeof(struct rtl818x_csr)) {
1660 printk(KERN_ERR "%s (rtl8180): Too short PCI resources\n",
1661 pci_name(pdev));
1662 err = -ENOMEM;
1663 goto err_free_reg;
1664 }
1665
John W. Linville9e385c52010-05-10 14:24:34 -04001666 if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) ||
1667 (err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))) {
Michael Wuf6532112007-10-14 14:43:16 -04001668 printk(KERN_ERR "%s (rtl8180): No suitable DMA available\n",
1669 pci_name(pdev));
1670 goto err_free_reg;
1671 }
1672
1673 pci_set_master(pdev);
1674
1675 dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8180_ops);
1676 if (!dev) {
1677 printk(KERN_ERR "%s (rtl8180): ieee80211 alloc failed\n",
1678 pci_name(pdev));
1679 err = -ENOMEM;
1680 goto err_free_reg;
1681 }
1682
1683 priv = dev->priv;
1684 priv->pdev = pdev;
1685
Johannes Berge6a98542008-10-21 12:40:02 +02001686 dev->max_rates = 2;
Michael Wuf6532112007-10-14 14:43:16 -04001687 SET_IEEE80211_DEV(dev, &pdev->dev);
1688 pci_set_drvdata(pdev, dev);
1689
1690 priv->map = pci_iomap(pdev, 1, mem_len);
1691 if (!priv->map)
1692 priv->map = pci_iomap(pdev, 0, io_len);
1693
1694 if (!priv->map) {
1695 printk(KERN_ERR "%s (rtl8180): Cannot map device memory\n",
1696 pci_name(pdev));
1697 goto err_free_dev;
1698 }
1699
Johannes Berg8318d782008-01-24 19:38:38 +01001700 BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
1701 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
1702
Michael Wuf6532112007-10-14 14:43:16 -04001703 memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
1704 memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
Johannes Berg8318d782008-01-24 19:38:38 +01001705
1706 priv->band.band = IEEE80211_BAND_2GHZ;
1707 priv->band.channels = priv->channels;
1708 priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
1709 priv->band.bitrates = priv->rates;
1710 priv->band.n_bitrates = 4;
1711 dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
1712
Michael Wuf6532112007-10-14 14:43:16 -04001713 dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
Bruno Randolf566bfe52008-05-08 19:15:40 +02001714 IEEE80211_HW_RX_INCLUDES_FCS |
1715 IEEE80211_HW_SIGNAL_UNSPEC;
John W. Linvillec809e862010-05-06 16:49:40 -04001716 dev->vif_data_size = sizeof(struct rtl8180_vif);
1717 dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1718 BIT(NL80211_IFTYPE_ADHOC);
Bruno Randolf566bfe52008-05-08 19:15:40 +02001719 dev->max_signal = 65;
Michael Wuf6532112007-10-14 14:43:16 -04001720
1721 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
1722 reg &= RTL818X_TX_CONF_HWVER_MASK;
1723 switch (reg) {
1724 case RTL818X_TX_CONF_R8180_ABCD:
1725 chip_name = "RTL8180";
Andrea Merello6caefd12014-03-08 18:36:37 +01001726 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001727 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001728
Michael Wuf6532112007-10-14 14:43:16 -04001729 case RTL818X_TX_CONF_R8180_F:
1730 chip_name = "RTL8180vF";
Andrea Merello6caefd12014-03-08 18:36:37 +01001731 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001732 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001733
Michael Wuf6532112007-10-14 14:43:16 -04001734 case RTL818X_TX_CONF_R8185_ABC:
1735 chip_name = "RTL8185";
Andrea Merello6caefd12014-03-08 18:36:37 +01001736 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001737 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001738
Michael Wuf6532112007-10-14 14:43:16 -04001739 case RTL818X_TX_CONF_R8185_D:
1740 chip_name = "RTL8185vD";
Andrea Merello6caefd12014-03-08 18:36:37 +01001741 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001742 break;
Andrea Merello24b5fbf2014-03-26 21:04:05 +01001743
1744 case RTL818X_TX_CONF_RTL8187SE:
1745 chip_name = "RTL8187SE";
1746 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8187SE;
1747 break;
1748
Michael Wuf6532112007-10-14 14:43:16 -04001749 default:
1750 printk(KERN_ERR "%s (rtl8180): Unknown chip! (0x%x)\n",
1751 pci_name(pdev), reg >> 25);
1752 goto err_iounmap;
1753 }
1754
Andrea Merellofd6564f2014-03-22 18:51:20 +01001755 /* we declare to MAC80211 all the queues except for beacon queue
1756 * that will be eventually handled by DRV.
1757 * TX rings are arranged in such a way that lower is the IDX,
1758 * higher is the priority, in order to achieve direct mapping
1759 * with mac80211, however the beacon queue is an exception and it
1760 * is mapped on the highst tx ring IDX.
1761 */
Andrea Merello24b5fbf2014-03-26 21:04:05 +01001762 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1763 dev->queues = RTL8187SE_NR_TX_QUEUES - 1;
1764 else
1765 dev->queues = RTL8180_NR_TX_QUEUES - 1;
Andrea Merellofd6564f2014-03-22 18:51:20 +01001766
Andrea Merello6caefd12014-03-08 18:36:37 +01001767 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Johannes Berg8318d782008-01-24 19:38:38 +01001768 priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
Michael Wuf6532112007-10-14 14:43:16 -04001769 pci_try_set_mwi(pdev);
1770 }
1771
Andrea Merello7d4b8292014-03-15 18:29:38 +01001772 rtl8180_eeprom_read(priv);
Michael Wuf6532112007-10-14 14:43:16 -04001773
Andrea Merello7d4b8292014-03-15 18:29:38 +01001774 switch (priv->rf_type) {
Michael Wuf6532112007-10-14 14:43:16 -04001775 case 1: rf_name = "Intersil";
1776 break;
1777 case 2: rf_name = "RFMD";
1778 break;
1779 case 3: priv->rf = &sa2400_rf_ops;
1780 break;
1781 case 4: priv->rf = &max2820_rf_ops;
1782 break;
1783 case 5: priv->rf = &grf5101_rf_ops;
1784 break;
Andrea Merello24b5fbf2014-03-26 21:04:05 +01001785 case 9:
1786 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
1787 priv->rf = rtl8187se_detect_rf(dev);
1788 else
1789 priv->rf = rtl8180_detect_rf(dev);
Michael Wuf6532112007-10-14 14:43:16 -04001790 break;
1791 case 10:
1792 rf_name = "RTL8255";
1793 break;
1794 default:
1795 printk(KERN_ERR "%s (rtl8180): Unknown RF! (0x%x)\n",
Andrea Merello7d4b8292014-03-15 18:29:38 +01001796 pci_name(pdev), priv->rf_type);
Michael Wuf6532112007-10-14 14:43:16 -04001797 goto err_iounmap;
1798 }
1799
1800 if (!priv->rf) {
1801 printk(KERN_ERR "%s (rtl8180): %s RF frontend not supported!\n",
1802 pci_name(pdev), rf_name);
1803 goto err_iounmap;
1804 }
1805
Andrea Merello7d4b8292014-03-15 18:29:38 +01001806 if (!is_valid_ether_addr(priv->mac_addr)) {
Michael Wuf6532112007-10-14 14:43:16 -04001807 printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
1808 " randomly generated MAC addr\n", pci_name(pdev));
Andrea Merello7d4b8292014-03-15 18:29:38 +01001809 eth_random_addr(priv->mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001810 }
Andrea Merello7d4b8292014-03-15 18:29:38 +01001811 SET_IEEE80211_PERM_ADDR(dev, priv->mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001812
1813 spin_lock_init(&priv->lock);
1814
1815 err = ieee80211_register_hw(dev);
1816 if (err) {
1817 printk(KERN_ERR "%s (rtl8180): Cannot register device\n",
1818 pci_name(pdev));
1819 goto err_iounmap;
1820 }
1821
Joe Perchesc96c31e2010-07-26 14:39:58 -07001822 wiphy_info(dev->wiphy, "hwaddr %pm, %s + %s\n",
Andrea Merello7d4b8292014-03-15 18:29:38 +01001823 priv->mac_addr, chip_name, priv->rf->name);
Michael Wuf6532112007-10-14 14:43:16 -04001824
1825 return 0;
1826
1827 err_iounmap:
andrea merello0269da22014-02-18 02:10:41 +01001828 pci_iounmap(pdev, priv->map);
Michael Wuf6532112007-10-14 14:43:16 -04001829
1830 err_free_dev:
Michael Wuf6532112007-10-14 14:43:16 -04001831 ieee80211_free_hw(dev);
1832
1833 err_free_reg:
1834 pci_release_regions(pdev);
1835 pci_disable_device(pdev);
1836 return err;
1837}
1838
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001839static void rtl8180_remove(struct pci_dev *pdev)
Michael Wuf6532112007-10-14 14:43:16 -04001840{
1841 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
1842 struct rtl8180_priv *priv;
1843
1844 if (!dev)
1845 return;
1846
1847 ieee80211_unregister_hw(dev);
1848
1849 priv = dev->priv;
1850
1851 pci_iounmap(pdev, priv->map);
1852 pci_release_regions(pdev);
1853 pci_disable_device(pdev);
1854 ieee80211_free_hw(dev);
1855}
1856
1857#ifdef CONFIG_PM
1858static int rtl8180_suspend(struct pci_dev *pdev, pm_message_t state)
1859{
1860 pci_save_state(pdev);
1861 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1862 return 0;
1863}
1864
1865static int rtl8180_resume(struct pci_dev *pdev)
1866{
1867 pci_set_power_state(pdev, PCI_D0);
1868 pci_restore_state(pdev);
1869 return 0;
1870}
1871
1872#endif /* CONFIG_PM */
1873
1874static struct pci_driver rtl8180_driver = {
1875 .name = KBUILD_MODNAME,
1876 .id_table = rtl8180_table,
1877 .probe = rtl8180_probe,
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001878 .remove = rtl8180_remove,
Michael Wuf6532112007-10-14 14:43:16 -04001879#ifdef CONFIG_PM
1880 .suspend = rtl8180_suspend,
1881 .resume = rtl8180_resume,
1882#endif /* CONFIG_PM */
1883};
1884
Axel Lin5b0a3b72012-04-14 10:38:36 +08001885module_pci_driver(rtl8180_driver);