blob: 3c2b784fd783a1ce76ec3998e63cdc4019631648 [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
88
Michael Wuf6532112007-10-14 14:43:16 -040089void rtl8180_write_phy(struct ieee80211_hw *dev, u8 addr, u32 data)
90{
91 struct rtl8180_priv *priv = dev->priv;
92 int i = 10;
93 u32 buf;
94
95 buf = (data << 8) | addr;
96
97 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf | 0x80);
98 while (i--) {
99 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf);
100 if (rtl818x_ioread8(priv, &priv->map->PHY[2]) == (data & 0xFF))
101 return;
102 }
103}
104
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400105static void rtl8180_handle_rx(struct ieee80211_hw *dev)
Michael Wuf6532112007-10-14 14:43:16 -0400106{
107 struct rtl8180_priv *priv = dev->priv;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400108 unsigned int count = 32;
John W. Linville8b73fb82010-07-21 16:26:40 -0400109 u8 signal, agc, sq;
andrea.merello2b4db052014-02-05 22:38:05 +0100110 dma_addr_t mapping;
Michael Wuf6532112007-10-14 14:43:16 -0400111
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400112 while (count--) {
Michael Wuf6532112007-10-14 14:43:16 -0400113 struct rtl8180_rx_desc *entry = &priv->rx_ring[priv->rx_idx];
114 struct sk_buff *skb = priv->rx_buf[priv->rx_idx];
115 u32 flags = le32_to_cpu(entry->flags);
116
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300117 if (flags & RTL818X_RX_DESC_FLAG_OWN)
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400118 return;
Michael Wuf6532112007-10-14 14:43:16 -0400119
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300120 if (unlikely(flags & (RTL818X_RX_DESC_FLAG_DMA_FAIL |
121 RTL818X_RX_DESC_FLAG_FOF |
122 RTL818X_RX_DESC_FLAG_RX_ERR)))
Michael Wuf6532112007-10-14 14:43:16 -0400123 goto done;
124 else {
125 u32 flags2 = le32_to_cpu(entry->flags2);
126 struct ieee80211_rx_status rx_status = {0};
127 struct sk_buff *new_skb = dev_alloc_skb(MAX_RX_SIZE);
128
129 if (unlikely(!new_skb))
130 goto done;
131
andrea.merello2b4db052014-02-05 22:38:05 +0100132 mapping = pci_map_single(priv->pdev,
133 skb_tail_pointer(new_skb),
134 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
135
136 if (pci_dma_mapping_error(priv->pdev, mapping)) {
137 kfree_skb(new_skb);
138 dev_err(&priv->pdev->dev, "RX DMA map error\n");
139
140 goto done;
141 }
142
Michael Wuf6532112007-10-14 14:43:16 -0400143 pci_unmap_single(priv->pdev,
144 *((dma_addr_t *)skb->cb),
145 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
146 skb_put(skb, flags & 0xFFF);
147
148 rx_status.antenna = (flags2 >> 15) & 1;
Johannes Berg8318d782008-01-24 19:38:38 +0100149 rx_status.rate_idx = (flags >> 20) & 0xF;
John W. Linville8b73fb82010-07-21 16:26:40 -0400150 agc = (flags2 >> 17) & 0x7F;
Andrea Merello6caefd12014-03-08 18:36:37 +0100151
152 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
John W. Linville8b73fb82010-07-21 16:26:40 -0400153 if (rx_status.rate_idx > 3)
154 signal = 90 - clamp_t(u8, agc, 25, 90);
155 else
156 signal = 95 - clamp_t(u8, agc, 30, 95);
157 } else {
158 sq = flags2 & 0xff;
159 signal = priv->rf->calc_rssi(agc, sq);
160 }
John W. Linville8b749642010-07-19 16:35:20 -0400161 rx_status.signal = signal;
Karl Beldan675a0b02013-03-25 16:26:57 +0100162 rx_status.freq = dev->conf.chandef.chan->center_freq;
163 rx_status.band = dev->conf.chandef.chan->band;
Michael Wuf6532112007-10-14 14:43:16 -0400164 rx_status.mactime = le64_to_cpu(entry->tsft);
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800165 rx_status.flag |= RX_FLAG_MACTIME_START;
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300166 if (flags & RTL818X_RX_DESC_FLAG_CRC32_ERR)
Michael Wuf6532112007-10-14 14:43:16 -0400167 rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
168
Johannes Bergf1d58c22009-06-17 13:13:00 +0200169 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400170 ieee80211_rx_irqsafe(dev, skb);
Michael Wuf6532112007-10-14 14:43:16 -0400171
172 skb = new_skb;
173 priv->rx_buf[priv->rx_idx] = skb;
andrea.merello2b4db052014-02-05 22:38:05 +0100174 *((dma_addr_t *) skb->cb) = mapping;
Michael Wuf6532112007-10-14 14:43:16 -0400175 }
176
177 done:
178 entry->rx_buf = cpu_to_le32(*((dma_addr_t *)skb->cb));
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300179 entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -0400180 MAX_RX_SIZE);
181 if (priv->rx_idx == 31)
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300182 entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -0400183 priv->rx_idx = (priv->rx_idx + 1) % 32;
184 }
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400185}
Michael Wuf6532112007-10-14 14:43:16 -0400186
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400187static void rtl8180_handle_tx(struct ieee80211_hw *dev, unsigned int prio)
188{
189 struct rtl8180_priv *priv = dev->priv;
190 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
Michael Wuf6532112007-10-14 14:43:16 -0400191
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400192 while (skb_queue_len(&ring->queue)) {
193 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
194 struct sk_buff *skb;
195 struct ieee80211_tx_info *info;
196 u32 flags = le32_to_cpu(entry->flags);
197
198 if (flags & RTL818X_TX_DESC_FLAG_OWN)
199 return;
200
201 ring->idx = (ring->idx + 1) % ring->entries;
202 skb = __skb_dequeue(&ring->queue);
203 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
204 skb->len, PCI_DMA_TODEVICE);
205
206 info = IEEE80211_SKB_CB(skb);
207 ieee80211_tx_info_clear_status(info);
208
209 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
210 (flags & RTL818X_TX_DESC_FLAG_TX_OK))
211 info->flags |= IEEE80211_TX_STAT_ACK;
212
213 info->status.rates[0].count = (flags & 0xFF) + 1;
214 info->status.rates[1].idx = -1;
215
216 ieee80211_tx_status_irqsafe(dev, skb);
217 if (ring->entries - skb_queue_len(&ring->queue) == 2)
218 ieee80211_wake_queue(dev, prio);
Michael Wuf6532112007-10-14 14:43:16 -0400219 }
220}
221
222static irqreturn_t rtl8180_interrupt(int irq, void *dev_id)
223{
224 struct ieee80211_hw *dev = dev_id;
225 struct rtl8180_priv *priv = dev->priv;
226 u16 reg;
227
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400228 spin_lock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400229 reg = rtl818x_ioread16(priv, &priv->map->INT_STATUS);
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400230 if (unlikely(reg == 0xFFFF)) {
231 spin_unlock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400232 return IRQ_HANDLED;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400233 }
Michael Wuf6532112007-10-14 14:43:16 -0400234
235 rtl818x_iowrite16(priv, &priv->map->INT_STATUS, reg);
236
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400237 if (reg & (RTL818X_INT_TXB_OK | RTL818X_INT_TXB_ERR))
238 rtl8180_handle_tx(dev, 3);
Michael Wuf6532112007-10-14 14:43:16 -0400239
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400240 if (reg & (RTL818X_INT_TXH_OK | RTL818X_INT_TXH_ERR))
241 rtl8180_handle_tx(dev, 2);
242
243 if (reg & (RTL818X_INT_TXN_OK | RTL818X_INT_TXN_ERR))
244 rtl8180_handle_tx(dev, 1);
245
246 if (reg & (RTL818X_INT_TXL_OK | RTL818X_INT_TXL_ERR))
247 rtl8180_handle_tx(dev, 0);
248
249 if (reg & (RTL818X_INT_RX_OK | RTL818X_INT_RX_ERR))
250 rtl8180_handle_rx(dev);
251
252 spin_unlock(&priv->lock);
Michael Wuf6532112007-10-14 14:43:16 -0400253
254 return IRQ_HANDLED;
255}
256
Thomas Huehn36323f82012-07-23 21:33:42 +0200257static void rtl8180_tx(struct ieee80211_hw *dev,
258 struct ieee80211_tx_control *control,
259 struct sk_buff *skb)
Michael Wuf6532112007-10-14 14:43:16 -0400260{
Johannes Berge039fa42008-05-15 12:55:29 +0200261 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
John W. Linville51e080d2010-05-06 16:26:23 -0400262 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Michael Wuf6532112007-10-14 14:43:16 -0400263 struct rtl8180_priv *priv = dev->priv;
264 struct rtl8180_tx_ring *ring;
265 struct rtl8180_tx_desc *entry;
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400266 unsigned long flags;
Michael Wuf6532112007-10-14 14:43:16 -0400267 unsigned int idx, prio;
268 dma_addr_t mapping;
269 u32 tx_flags;
Johannes Berge6a98542008-10-21 12:40:02 +0200270 u8 rc_flags;
Michael Wuf6532112007-10-14 14:43:16 -0400271 u16 plcp_len = 0;
272 __le16 rts_duration = 0;
273
Johannes Berge2530082008-05-17 00:57:14 +0200274 prio = skb_get_queue_mapping(skb);
Michael Wuf6532112007-10-14 14:43:16 -0400275 ring = &priv->tx_ring[prio];
276
277 mapping = pci_map_single(priv->pdev, skb->data,
278 skb->len, PCI_DMA_TODEVICE);
279
andrea.merello348f7d42014-02-05 22:38:06 +0100280 if (pci_dma_mapping_error(priv->pdev, mapping)) {
281 kfree_skb(skb);
282 dev_err(&priv->pdev->dev, "TX DMA mapping error\n");
283 return;
284
285 }
286
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300287 tx_flags = RTL818X_TX_DESC_FLAG_OWN | RTL818X_TX_DESC_FLAG_FS |
288 RTL818X_TX_DESC_FLAG_LS |
Johannes Berge039fa42008-05-15 12:55:29 +0200289 (ieee80211_get_tx_rate(dev, info)->hw_value << 24) |
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200290 skb->len;
Michael Wuf6532112007-10-14 14:43:16 -0400291
Andrea Merello6caefd12014-03-08 18:36:37 +0100292 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300293 tx_flags |= RTL818X_TX_DESC_FLAG_DMA |
294 RTL818X_TX_DESC_FLAG_NO_ENC;
Michael Wuf6532112007-10-14 14:43:16 -0400295
Johannes Berge6a98542008-10-21 12:40:02 +0200296 rc_flags = info->control.rates[0].flags;
297 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300298 tx_flags |= RTL818X_TX_DESC_FLAG_RTS;
Johannes Berge039fa42008-05-15 12:55:29 +0200299 tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
Johannes Berge6a98542008-10-21 12:40:02 +0200300 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300301 tx_flags |= RTL818X_TX_DESC_FLAG_CTS;
Johannes Berge039fa42008-05-15 12:55:29 +0200302 tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
Johannes Bergaa68cbf2008-02-18 14:20:30 +0100303 }
Michael Wuf6532112007-10-14 14:43:16 -0400304
Johannes Berge6a98542008-10-21 12:40:02 +0200305 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
Johannes Berg32bfd352007-12-19 01:31:26 +0100306 rts_duration = ieee80211_rts_duration(dev, priv->vif, skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +0200307 info);
Michael Wuf6532112007-10-14 14:43:16 -0400308
Andrea Merello6caefd12014-03-08 18:36:37 +0100309 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400310 unsigned int remainder;
311
312 plcp_len = DIV_ROUND_UP(16 * (skb->len + 4),
Johannes Berge039fa42008-05-15 12:55:29 +0200313 (ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
Michael Wuf6532112007-10-14 14:43:16 -0400314 remainder = (16 * (skb->len + 4)) %
Johannes Berge039fa42008-05-15 12:55:29 +0200315 ((ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
Roel Kluin35a0ace2009-06-22 17:42:21 +0200316 if (remainder <= 6)
Michael Wuf6532112007-10-14 14:43:16 -0400317 plcp_len |= 1 << 15;
318 }
319
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400320 spin_lock_irqsave(&priv->lock, flags);
John W. Linville51e080d2010-05-06 16:26:23 -0400321
322 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
323 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
324 priv->seqno += 0x10;
325 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
326 hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
327 }
328
Michael Wuf6532112007-10-14 14:43:16 -0400329 idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
330 entry = &ring->desc[idx];
331
332 entry->rts_duration = rts_duration;
333 entry->plcp_len = cpu_to_le16(plcp_len);
334 entry->tx_buf = cpu_to_le32(mapping);
335 entry->frame_len = cpu_to_le32(skb->len);
Johannes Berge6a98542008-10-21 12:40:02 +0200336 entry->flags2 = info->control.rates[1].idx >= 0 ?
Felix Fietkau870abdf2008-10-05 18:04:24 +0200337 ieee80211_get_alt_retry_rate(dev, info, 0)->bitrate << 4 : 0;
Johannes Berge6a98542008-10-21 12:40:02 +0200338 entry->retry_limit = info->control.rates[0].count;
andrea merello4c552a52014-02-18 02:10:45 +0100339
340 /* We must be sure that tx_flags is written last because the HW
341 * looks at it to check if the rest of data is valid or not
342 */
343 wmb();
Michael Wuf6532112007-10-14 14:43:16 -0400344 entry->flags = cpu_to_le32(tx_flags);
andrea merelloc24782e2014-02-18 02:10:46 +0100345 /* We must be sure this has been written before followings HW
346 * register write, because this write will made the HW attempts
347 * to DMA the just-written data
348 */
349 wmb();
350
Michael Wuf6532112007-10-14 14:43:16 -0400351 __skb_queue_tail(&ring->queue, skb);
352 if (ring->entries - skb_queue_len(&ring->queue) < 2)
John W. Linvilled10e2e02010-04-27 16:57:38 -0400353 ieee80211_stop_queue(dev, prio);
John W. Linville51e080d2010-05-06 16:26:23 -0400354
John W. Linvillea6d27d2a2010-10-07 11:31:56 -0400355 spin_unlock_irqrestore(&priv->lock, flags);
Michael Wuf6532112007-10-14 14:43:16 -0400356
357 rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING, (1 << (prio + 4)));
Michael Wuf6532112007-10-14 14:43:16 -0400358}
359
360void rtl8180_set_anaparam(struct rtl8180_priv *priv, u32 anaparam)
361{
362 u8 reg;
363
364 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
365 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
366 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
367 reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
368 rtl818x_iowrite32(priv, &priv->map->ANAPARAM, anaparam);
369 rtl818x_iowrite8(priv, &priv->map->CONFIG3,
370 reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
371 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
372}
373
374static int rtl8180_init_hw(struct ieee80211_hw *dev)
375{
376 struct rtl8180_priv *priv = dev->priv;
377 u16 reg;
378
379 rtl818x_iowrite8(priv, &priv->map->CMD, 0);
380 rtl818x_ioread8(priv, &priv->map->CMD);
381 msleep(10);
382
383 /* reset */
384 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
385 rtl818x_ioread8(priv, &priv->map->CMD);
386
387 reg = rtl818x_ioread8(priv, &priv->map->CMD);
388 reg &= (1 << 1);
389 reg |= RTL818X_CMD_RESET;
390 rtl818x_iowrite8(priv, &priv->map->CMD, RTL818X_CMD_RESET);
391 rtl818x_ioread8(priv, &priv->map->CMD);
392 msleep(200);
393
394 /* check success of reset */
395 if (rtl818x_ioread8(priv, &priv->map->CMD) & RTL818X_CMD_RESET) {
Joe Perchesc96c31e2010-07-26 14:39:58 -0700396 wiphy_err(dev->wiphy, "reset timeout!\n");
Michael Wuf6532112007-10-14 14:43:16 -0400397 return -ETIMEDOUT;
398 }
399
400 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
401 rtl818x_ioread8(priv, &priv->map->CMD);
402 msleep(200);
403
404 if (rtl818x_ioread8(priv, &priv->map->CONFIG3) & (1 << 3)) {
405 /* For cardbus */
406 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
407 reg |= 1 << 1;
408 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
409 reg = rtl818x_ioread16(priv, &priv->map->FEMR);
410 reg |= (1 << 15) | (1 << 14) | (1 << 4);
411 rtl818x_iowrite16(priv, &priv->map->FEMR, reg);
412 }
413
414 rtl818x_iowrite8(priv, &priv->map->MSR, 0);
415
Andrea Merello6caefd12014-03-08 18:36:37 +0100416 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -0400417 rtl8180_set_anaparam(priv, priv->anaparam);
418
419 rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
420 rtl818x_iowrite32(priv, &priv->map->TBDA, priv->tx_ring[3].dma);
421 rtl818x_iowrite32(priv, &priv->map->THPDA, priv->tx_ring[2].dma);
422 rtl818x_iowrite32(priv, &priv->map->TNPDA, priv->tx_ring[1].dma);
423 rtl818x_iowrite32(priv, &priv->map->TLPDA, priv->tx_ring[0].dma);
424
425 /* TODO: necessary? specs indicate not */
426 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
427 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
428 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
Andrea Merello6caefd12014-03-08 18:36:37 +0100429 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
Michael Wuf6532112007-10-14 14:43:16 -0400430 reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
431 rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
432 }
433 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
434
435 /* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */
436
437 /* TODO: turn off hw wep on rtl8180 */
438
439 rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
440
Andrea Merello6caefd12014-03-08 18:36:37 +0100441 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400442 rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
443 rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
444 rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (8 << 4) | 0);
445
446 rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
447
448 /* TODO: set ClkRun enable? necessary? */
449 reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
450 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, reg & ~(1 << 6));
451 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
452 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
453 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
454 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
455 } else {
456 rtl818x_iowrite16(priv, &priv->map->BRSR, 0x1);
457 rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);
458
459 rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
460 rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, 0x4C);
461 }
462
463 priv->rf->init(dev);
Andrea Merello6caefd12014-03-08 18:36:37 +0100464 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185)
Michael Wuf6532112007-10-14 14:43:16 -0400465 rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
466 return 0;
467}
468
469static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
470{
471 struct rtl8180_priv *priv = dev->priv;
472 struct rtl8180_rx_desc *entry;
473 int i;
474
475 priv->rx_ring = pci_alloc_consistent(priv->pdev,
476 sizeof(*priv->rx_ring) * 32,
477 &priv->rx_ring_dma);
478
479 if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -0700480 wiphy_err(dev->wiphy, "Cannot allocate RX ring\n");
Michael Wuf6532112007-10-14 14:43:16 -0400481 return -ENOMEM;
482 }
483
484 memset(priv->rx_ring, 0, sizeof(*priv->rx_ring) * 32);
485 priv->rx_idx = 0;
486
487 for (i = 0; i < 32; i++) {
488 struct sk_buff *skb = dev_alloc_skb(MAX_RX_SIZE);
489 dma_addr_t *mapping;
490 entry = &priv->rx_ring[i];
andrea merello4da18bb2014-02-18 02:10:43 +0100491 if (!skb) {
492 wiphy_err(dev->wiphy, "Cannot allocate RX skb\n");
493 return -ENOMEM;
494 }
Michael Wuf6532112007-10-14 14:43:16 -0400495 priv->rx_buf[i] = skb;
496 mapping = (dma_addr_t *)skb->cb;
497 *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
498 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
andrea merelloec1da082014-02-22 17:57:23 +0100499
500 if (pci_dma_mapping_error(priv->pdev, *mapping)) {
501 kfree_skb(skb);
502 wiphy_err(dev->wiphy, "Cannot map DMA for RX skb\n");
503 return -ENOMEM;
504 }
505
Michael Wuf6532112007-10-14 14:43:16 -0400506 entry->rx_buf = cpu_to_le32(*mapping);
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300507 entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
Michael Wuf6532112007-10-14 14:43:16 -0400508 MAX_RX_SIZE);
509 }
Herton Ronaldo Krzesinski38e3b0d2008-07-16 11:44:18 -0300510 entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
Michael Wuf6532112007-10-14 14:43:16 -0400511 return 0;
512}
513
514static void rtl8180_free_rx_ring(struct ieee80211_hw *dev)
515{
516 struct rtl8180_priv *priv = dev->priv;
517 int i;
518
519 for (i = 0; i < 32; i++) {
520 struct sk_buff *skb = priv->rx_buf[i];
521 if (!skb)
522 continue;
523
524 pci_unmap_single(priv->pdev,
525 *((dma_addr_t *)skb->cb),
526 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
527 kfree_skb(skb);
528 }
529
530 pci_free_consistent(priv->pdev, sizeof(*priv->rx_ring) * 32,
531 priv->rx_ring, priv->rx_ring_dma);
532 priv->rx_ring = NULL;
533}
534
535static int rtl8180_init_tx_ring(struct ieee80211_hw *dev,
536 unsigned int prio, unsigned int entries)
537{
538 struct rtl8180_priv *priv = dev->priv;
539 struct rtl8180_tx_desc *ring;
540 dma_addr_t dma;
541 int i;
542
543 ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
544 if (!ring || (unsigned long)ring & 0xFF) {
Joe Perches5db55842010-08-11 19:11:19 -0700545 wiphy_err(dev->wiphy, "Cannot allocate TX ring (prio = %d)\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -0700546 prio);
Michael Wuf6532112007-10-14 14:43:16 -0400547 return -ENOMEM;
548 }
549
550 memset(ring, 0, sizeof(*ring)*entries);
551 priv->tx_ring[prio].desc = ring;
552 priv->tx_ring[prio].dma = dma;
553 priv->tx_ring[prio].idx = 0;
554 priv->tx_ring[prio].entries = entries;
555 skb_queue_head_init(&priv->tx_ring[prio].queue);
556
557 for (i = 0; i < entries; i++)
558 ring[i].next_tx_desc =
559 cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
560
561 return 0;
562}
563
564static void rtl8180_free_tx_ring(struct ieee80211_hw *dev, unsigned int prio)
565{
566 struct rtl8180_priv *priv = dev->priv;
567 struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
568
569 while (skb_queue_len(&ring->queue)) {
570 struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
571 struct sk_buff *skb = __skb_dequeue(&ring->queue);
572
573 pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
574 skb->len, PCI_DMA_TODEVICE);
Michael Wuf6532112007-10-14 14:43:16 -0400575 kfree_skb(skb);
576 ring->idx = (ring->idx + 1) % ring->entries;
577 }
578
579 pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
580 ring->desc, ring->dma);
581 ring->desc = NULL;
582}
583
584static int rtl8180_start(struct ieee80211_hw *dev)
585{
586 struct rtl8180_priv *priv = dev->priv;
587 int ret, i;
588 u32 reg;
589
590 ret = rtl8180_init_rx_ring(dev);
591 if (ret)
592 return ret;
593
594 for (i = 0; i < 4; i++)
595 if ((ret = rtl8180_init_tx_ring(dev, i, 16)))
596 goto err_free_rings;
597
598 ret = rtl8180_init_hw(dev);
599 if (ret)
600 goto err_free_rings;
601
602 rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
603 rtl818x_iowrite32(priv, &priv->map->TBDA, priv->tx_ring[3].dma);
604 rtl818x_iowrite32(priv, &priv->map->THPDA, priv->tx_ring[2].dma);
605 rtl818x_iowrite32(priv, &priv->map->TNPDA, priv->tx_ring[1].dma);
606 rtl818x_iowrite32(priv, &priv->map->TLPDA, priv->tx_ring[0].dma);
607
Julia Lawallea31ba32009-11-18 08:26:02 +0000608 ret = request_irq(priv->pdev->irq, rtl8180_interrupt,
Michael Wuf6532112007-10-14 14:43:16 -0400609 IRQF_SHARED, KBUILD_MODNAME, dev);
610 if (ret) {
Joe Perches5db55842010-08-11 19:11:19 -0700611 wiphy_err(dev->wiphy, "failed to register IRQ handler\n");
Michael Wuf6532112007-10-14 14:43:16 -0400612 goto err_free_rings;
613 }
614
615 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
616
617 rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
618 rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
619
620 reg = RTL818X_RX_CONF_ONLYERLPKT |
621 RTL818X_RX_CONF_RX_AUTORESETPHY |
622 RTL818X_RX_CONF_MGMT |
623 RTL818X_RX_CONF_DATA |
624 (7 << 8 /* MAX RX DMA */) |
625 RTL818X_RX_CONF_BROADCAST |
626 RTL818X_RX_CONF_NICMAC;
627
Andrea Merello6caefd12014-03-08 18:36:37 +0100628 if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185)
Michael Wuf6532112007-10-14 14:43:16 -0400629 reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
630 else {
631 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
632 ? RTL818X_RX_CONF_CSDM1 : 0;
633 reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
634 ? RTL818X_RX_CONF_CSDM2 : 0;
635 }
636
637 priv->rx_conf = reg;
638 rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
639
Andrea Merello6caefd12014-03-08 18:36:37 +0100640 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -0400641 reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
andrea merello14c76152014-02-18 02:10:44 +0100642
643 /* CW is not on per-packet basis.
644 * in rtl8185 the CW_VALUE reg is used.
645 */
andrea merello6f7343d2014-01-21 20:16:43 +0100646 reg &= ~RTL818X_CW_CONF_PERPACKET_CW;
andrea merello14c76152014-02-18 02:10:44 +0100647 /* retry limit IS on per-packet basis.
648 * the short and long retry limit in TX_CONF
649 * reg are ignored
650 */
andrea merello6f7343d2014-01-21 20:16:43 +0100651 reg |= RTL818X_CW_CONF_PERPACKET_RETRY;
Michael Wuf6532112007-10-14 14:43:16 -0400652 rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
653
654 reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
andrea merello14c76152014-02-18 02:10:44 +0100655 /* TX antenna and TX gain are not on per-packet basis.
656 * TX Antenna is selected by ANTSEL reg (RX in BB regs).
657 * TX gain is selected with CCK_TX_AGC and OFDM_TX_AGC regs
658 */
andrea merello6f7343d2014-01-21 20:16:43 +0100659 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN;
660 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL;
Michael Wuf6532112007-10-14 14:43:16 -0400661 reg |= RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
662 rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
663
664 /* disable early TX */
665 rtl818x_iowrite8(priv, (u8 __iomem *)priv->map + 0xec, 0x3f);
666 }
667
668 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
669 reg |= (6 << 21 /* MAX TX DMA */) |
670 RTL818X_TX_CONF_NO_ICV;
671
Andrea Merello6caefd12014-03-08 18:36:37 +0100672
673
674 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
Michael Wuf6532112007-10-14 14:43:16 -0400675 reg &= ~RTL818X_TX_CONF_PROBE_DTS;
676 else
677 reg &= ~RTL818X_TX_CONF_HW_SEQNUM;
678
andrea merelloe74075a2014-02-18 02:10:40 +0100679 reg &= ~RTL818X_TX_CONF_DISCW;
680
Michael Wuf6532112007-10-14 14:43:16 -0400681 /* different meaning, same value on both rtl8185 and rtl8180 */
682 reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;
683
684 rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
685
686 reg = rtl818x_ioread8(priv, &priv->map->CMD);
687 reg |= RTL818X_CMD_RX_ENABLE;
688 reg |= RTL818X_CMD_TX_ENABLE;
689 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
690
Michael Wuf6532112007-10-14 14:43:16 -0400691 return 0;
692
693 err_free_rings:
694 rtl8180_free_rx_ring(dev);
695 for (i = 0; i < 4; i++)
696 if (priv->tx_ring[i].desc)
697 rtl8180_free_tx_ring(dev, i);
698
699 return ret;
700}
701
702static void rtl8180_stop(struct ieee80211_hw *dev)
703{
704 struct rtl8180_priv *priv = dev->priv;
705 u8 reg;
706 int i;
707
Michael Wuf6532112007-10-14 14:43:16 -0400708 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
709
710 reg = rtl818x_ioread8(priv, &priv->map->CMD);
711 reg &= ~RTL818X_CMD_TX_ENABLE;
712 reg &= ~RTL818X_CMD_RX_ENABLE;
713 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
714
715 priv->rf->stop(dev);
716
717 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
718 reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
719 rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
720 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
721
722 free_irq(priv->pdev->irq, dev);
723
724 rtl8180_free_rx_ring(dev);
725 for (i = 0; i < 4; i++)
726 rtl8180_free_tx_ring(dev, i);
727}
728
Eliad Peller37a41b42011-09-21 14:06:11 +0300729static u64 rtl8180_get_tsf(struct ieee80211_hw *dev,
730 struct ieee80211_vif *vif)
John W. Linvillec809e862010-05-06 16:49:40 -0400731{
732 struct rtl8180_priv *priv = dev->priv;
733
734 return rtl818x_ioread32(priv, &priv->map->TSFT[0]) |
735 (u64)(rtl818x_ioread32(priv, &priv->map->TSFT[1])) << 32;
736}
737
John W. Linvillea3275e22010-06-24 11:08:37 -0400738static void rtl8180_beacon_work(struct work_struct *work)
John W. Linvillec809e862010-05-06 16:49:40 -0400739{
740 struct rtl8180_vif *vif_priv =
741 container_of(work, struct rtl8180_vif, beacon_work.work);
742 struct ieee80211_vif *vif =
743 container_of((void *)vif_priv, struct ieee80211_vif, drv_priv);
744 struct ieee80211_hw *dev = vif_priv->dev;
745 struct ieee80211_mgmt *mgmt;
746 struct sk_buff *skb;
John W. Linvillec809e862010-05-06 16:49:40 -0400747
748 /* don't overflow the tx ring */
749 if (ieee80211_queue_stopped(dev, 0))
750 goto resched;
751
752 /* grab a fresh beacon */
753 skb = ieee80211_beacon_get(dev, vif);
John W. Linville8f1d2d22010-08-05 13:46:27 -0400754 if (!skb)
755 goto resched;
John W. Linvillec809e862010-05-06 16:49:40 -0400756
757 /*
758 * update beacon timestamp w/ TSF value
759 * TODO: make hardware update beacon timestamp
760 */
761 mgmt = (struct ieee80211_mgmt *)skb->data;
Eliad Peller37a41b42011-09-21 14:06:11 +0300762 mgmt->u.beacon.timestamp = cpu_to_le64(rtl8180_get_tsf(dev, vif));
John W. Linvillec809e862010-05-06 16:49:40 -0400763
764 /* TODO: use actual beacon queue */
765 skb_set_queue_mapping(skb, 0);
766
Thomas Huehn36323f82012-07-23 21:33:42 +0200767 rtl8180_tx(dev, NULL, skb);
John W. Linvillec809e862010-05-06 16:49:40 -0400768
769resched:
770 /*
771 * schedule next beacon
772 * TODO: use hardware support for beacon timing
773 */
774 schedule_delayed_work(&vif_priv->beacon_work,
775 usecs_to_jiffies(1024 * vif->bss_conf.beacon_int));
776}
777
Michael Wuf6532112007-10-14 14:43:16 -0400778static int rtl8180_add_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100779 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -0400780{
781 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -0400782 struct rtl8180_vif *vif_priv;
Michael Wuf6532112007-10-14 14:43:16 -0400783
John W. Linville643aab62009-12-22 18:13:04 -0500784 /*
785 * We only support one active interface at a time.
786 */
787 if (priv->vif)
788 return -EBUSY;
Michael Wuf6532112007-10-14 14:43:16 -0400789
Johannes Berg1ed32e42009-12-23 13:15:45 +0100790 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200791 case NL80211_IFTYPE_STATION:
John W. Linvillec809e862010-05-06 16:49:40 -0400792 case NL80211_IFTYPE_ADHOC:
Michael Wuf6532112007-10-14 14:43:16 -0400793 break;
794 default:
795 return -EOPNOTSUPP;
796 }
797
Johannes Berg1ed32e42009-12-23 13:15:45 +0100798 priv->vif = vif;
Johannes Berg32bfd352007-12-19 01:31:26 +0100799
John W. Linvillec809e862010-05-06 16:49:40 -0400800 /* Initialize driver private area */
801 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
802 vif_priv->dev = dev;
803 INIT_DELAYED_WORK(&vif_priv->beacon_work, rtl8180_beacon_work);
804 vif_priv->enable_beacon = false;
805
Michael Wuf6532112007-10-14 14:43:16 -0400806 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
807 rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->MAC[0],
Johannes Berg1ed32e42009-12-23 13:15:45 +0100808 le32_to_cpu(*(__le32 *)vif->addr));
Michael Wuf6532112007-10-14 14:43:16 -0400809 rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->MAC[4],
Johannes Berg1ed32e42009-12-23 13:15:45 +0100810 le16_to_cpu(*(__le16 *)(vif->addr + 4)));
Michael Wuf6532112007-10-14 14:43:16 -0400811 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
812
813 return 0;
814}
815
816static void rtl8180_remove_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100817 struct ieee80211_vif *vif)
Michael Wuf6532112007-10-14 14:43:16 -0400818{
819 struct rtl8180_priv *priv = dev->priv;
Johannes Berg32bfd352007-12-19 01:31:26 +0100820 priv->vif = NULL;
Michael Wuf6532112007-10-14 14:43:16 -0400821}
822
Johannes Berge8975582008-10-09 12:18:51 +0200823static int rtl8180_config(struct ieee80211_hw *dev, u32 changed)
Michael Wuf6532112007-10-14 14:43:16 -0400824{
825 struct rtl8180_priv *priv = dev->priv;
Johannes Berge8975582008-10-09 12:18:51 +0200826 struct ieee80211_conf *conf = &dev->conf;
Michael Wuf6532112007-10-14 14:43:16 -0400827
828 priv->rf->set_chan(dev, conf);
829
830 return 0;
831}
832
John W. Linvilleda81ded2008-11-12 14:37:11 -0500833static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
834 struct ieee80211_vif *vif,
835 struct ieee80211_bss_conf *info,
836 u32 changed)
837{
838 struct rtl8180_priv *priv = dev->priv;
John W. Linvillec809e862010-05-06 16:49:40 -0400839 struct rtl8180_vif *vif_priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200840 int i;
John W. Linville0f956e72010-07-29 21:50:29 -0400841 u8 reg;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200842
John W. Linvillec809e862010-05-06 16:49:40 -0400843 vif_priv = (struct rtl8180_vif *)&vif->drv_priv;
844
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200845 if (changed & BSS_CHANGED_BSSID) {
846 for (i = 0; i < ETH_ALEN; i++)
847 rtl818x_iowrite8(priv, &priv->map->BSSID[i],
848 info->bssid[i]);
849
John W. Linville0f956e72010-07-29 21:50:29 -0400850 if (is_valid_ether_addr(info->bssid)) {
851 if (vif->type == NL80211_IFTYPE_ADHOC)
852 reg = RTL818X_MSR_ADHOC;
853 else
854 reg = RTL818X_MSR_INFRA;
855 } else
856 reg = RTL818X_MSR_NO_LINK;
857 rtl818x_iowrite8(priv, &priv->map->MSR, reg);
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200858 }
John W. Linvilleda81ded2008-11-12 14:37:11 -0500859
860 if (changed & BSS_CHANGED_ERP_SLOT && priv->rf->conf_erp)
John W. Linvillec809e862010-05-06 16:49:40 -0400861 priv->rf->conf_erp(dev, info);
862
863 if (changed & BSS_CHANGED_BEACON_ENABLED)
864 vif_priv->enable_beacon = info->enable_beacon;
865
866 if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON)) {
867 cancel_delayed_work_sync(&vif_priv->beacon_work);
868 if (vif_priv->enable_beacon)
869 schedule_work(&vif_priv->beacon_work.work);
870 }
John W. Linvilleda81ded2008-11-12 14:37:11 -0500871}
872
Jiri Pirko22bedad32010-04-01 21:22:57 +0000873static u64 rtl8180_prepare_multicast(struct ieee80211_hw *dev,
874 struct netdev_hw_addr_list *mc_list)
Johannes Berg3ac64be2009-08-17 16:16:53 +0200875{
Jiri Pirko22bedad32010-04-01 21:22:57 +0000876 return netdev_hw_addr_list_count(mc_list);
Johannes Berg3ac64be2009-08-17 16:16:53 +0200877}
878
Michael Wuf6532112007-10-14 14:43:16 -0400879static void rtl8180_configure_filter(struct ieee80211_hw *dev,
880 unsigned int changed_flags,
881 unsigned int *total_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +0200882 u64 multicast)
Michael Wuf6532112007-10-14 14:43:16 -0400883{
884 struct rtl8180_priv *priv = dev->priv;
885
886 if (changed_flags & FIF_FCSFAIL)
887 priv->rx_conf ^= RTL818X_RX_CONF_FCS;
888 if (changed_flags & FIF_CONTROL)
889 priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
890 if (changed_flags & FIF_OTHER_BSS)
891 priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
Johannes Berg3ac64be2009-08-17 16:16:53 +0200892 if (*total_flags & FIF_ALLMULTI || multicast > 0)
Michael Wuf6532112007-10-14 14:43:16 -0400893 priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
894 else
895 priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
896
897 *total_flags = 0;
898
899 if (priv->rx_conf & RTL818X_RX_CONF_FCS)
900 *total_flags |= FIF_FCSFAIL;
901 if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
902 *total_flags |= FIF_CONTROL;
903 if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
904 *total_flags |= FIF_OTHER_BSS;
905 if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
906 *total_flags |= FIF_ALLMULTI;
907
908 rtl818x_iowrite32(priv, &priv->map->RX_CONF, priv->rx_conf);
909}
910
911static const struct ieee80211_ops rtl8180_ops = {
912 .tx = rtl8180_tx,
913 .start = rtl8180_start,
914 .stop = rtl8180_stop,
915 .add_interface = rtl8180_add_interface,
916 .remove_interface = rtl8180_remove_interface,
917 .config = rtl8180_config,
John W. Linvilleda81ded2008-11-12 14:37:11 -0500918 .bss_info_changed = rtl8180_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +0200919 .prepare_multicast = rtl8180_prepare_multicast,
Michael Wuf6532112007-10-14 14:43:16 -0400920 .configure_filter = rtl8180_configure_filter,
John W. Linvilled2bb8e02010-01-26 16:22:20 -0500921 .get_tsf = rtl8180_get_tsf,
Michael Wuf6532112007-10-14 14:43:16 -0400922};
923
924static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
925{
926 struct ieee80211_hw *dev = eeprom->data;
927 struct rtl8180_priv *priv = dev->priv;
928 u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
929
930 eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
931 eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
932 eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
933 eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
934}
935
936static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
937{
938 struct ieee80211_hw *dev = eeprom->data;
939 struct rtl8180_priv *priv = dev->priv;
940 u8 reg = 2 << 6;
941
942 if (eeprom->reg_data_in)
943 reg |= RTL818X_EEPROM_CMD_WRITE;
944 if (eeprom->reg_data_out)
945 reg |= RTL818X_EEPROM_CMD_READ;
946 if (eeprom->reg_data_clock)
947 reg |= RTL818X_EEPROM_CMD_CK;
948 if (eeprom->reg_chip_select)
949 reg |= RTL818X_EEPROM_CMD_CS;
950
951 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
952 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
953 udelay(10);
954}
955
Bill Pembertonfb4e8992012-12-03 09:56:40 -0500956static int rtl8180_probe(struct pci_dev *pdev,
Michael Wuf6532112007-10-14 14:43:16 -0400957 const struct pci_device_id *id)
958{
959 struct ieee80211_hw *dev;
960 struct rtl8180_priv *priv;
961 unsigned long mem_addr, mem_len;
962 unsigned int io_addr, io_len;
963 int err, i;
964 struct eeprom_93cx6 eeprom;
965 const char *chip_name, *rf_name = NULL;
966 u32 reg;
967 u16 eeprom_val;
John W. Linvillec693bf92010-05-04 15:46:15 -0400968 u8 mac_addr[ETH_ALEN];
Michael Wuf6532112007-10-14 14:43:16 -0400969
970 err = pci_enable_device(pdev);
971 if (err) {
972 printk(KERN_ERR "%s (rtl8180): Cannot enable new PCI device\n",
973 pci_name(pdev));
974 return err;
975 }
976
977 err = pci_request_regions(pdev, KBUILD_MODNAME);
978 if (err) {
979 printk(KERN_ERR "%s (rtl8180): Cannot obtain PCI resources\n",
980 pci_name(pdev));
981 return err;
982 }
983
984 io_addr = pci_resource_start(pdev, 0);
985 io_len = pci_resource_len(pdev, 0);
986 mem_addr = pci_resource_start(pdev, 1);
987 mem_len = pci_resource_len(pdev, 1);
988
989 if (mem_len < sizeof(struct rtl818x_csr) ||
990 io_len < sizeof(struct rtl818x_csr)) {
991 printk(KERN_ERR "%s (rtl8180): Too short PCI resources\n",
992 pci_name(pdev));
993 err = -ENOMEM;
994 goto err_free_reg;
995 }
996
John W. Linville9e385c52010-05-10 14:24:34 -0400997 if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) ||
998 (err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))) {
Michael Wuf6532112007-10-14 14:43:16 -0400999 printk(KERN_ERR "%s (rtl8180): No suitable DMA available\n",
1000 pci_name(pdev));
1001 goto err_free_reg;
1002 }
1003
1004 pci_set_master(pdev);
1005
1006 dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8180_ops);
1007 if (!dev) {
1008 printk(KERN_ERR "%s (rtl8180): ieee80211 alloc failed\n",
1009 pci_name(pdev));
1010 err = -ENOMEM;
1011 goto err_free_reg;
1012 }
1013
1014 priv = dev->priv;
1015 priv->pdev = pdev;
1016
Johannes Berge6a98542008-10-21 12:40:02 +02001017 dev->max_rates = 2;
Michael Wuf6532112007-10-14 14:43:16 -04001018 SET_IEEE80211_DEV(dev, &pdev->dev);
1019 pci_set_drvdata(pdev, dev);
1020
1021 priv->map = pci_iomap(pdev, 1, mem_len);
1022 if (!priv->map)
1023 priv->map = pci_iomap(pdev, 0, io_len);
1024
1025 if (!priv->map) {
1026 printk(KERN_ERR "%s (rtl8180): Cannot map device memory\n",
1027 pci_name(pdev));
1028 goto err_free_dev;
1029 }
1030
Johannes Berg8318d782008-01-24 19:38:38 +01001031 BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
1032 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
1033
Michael Wuf6532112007-10-14 14:43:16 -04001034 memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
1035 memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
Johannes Berg8318d782008-01-24 19:38:38 +01001036
1037 priv->band.band = IEEE80211_BAND_2GHZ;
1038 priv->band.channels = priv->channels;
1039 priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
1040 priv->band.bitrates = priv->rates;
1041 priv->band.n_bitrates = 4;
1042 dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
1043
Michael Wuf6532112007-10-14 14:43:16 -04001044 dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
Bruno Randolf566bfe52008-05-08 19:15:40 +02001045 IEEE80211_HW_RX_INCLUDES_FCS |
1046 IEEE80211_HW_SIGNAL_UNSPEC;
John W. Linvillec809e862010-05-06 16:49:40 -04001047 dev->vif_data_size = sizeof(struct rtl8180_vif);
1048 dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1049 BIT(NL80211_IFTYPE_ADHOC);
Michael Wuf6532112007-10-14 14:43:16 -04001050 dev->queues = 1;
Bruno Randolf566bfe52008-05-08 19:15:40 +02001051 dev->max_signal = 65;
Michael Wuf6532112007-10-14 14:43:16 -04001052
1053 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
1054 reg &= RTL818X_TX_CONF_HWVER_MASK;
1055 switch (reg) {
1056 case RTL818X_TX_CONF_R8180_ABCD:
1057 chip_name = "RTL8180";
Andrea Merello6caefd12014-03-08 18:36:37 +01001058 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001059 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001060
Michael Wuf6532112007-10-14 14:43:16 -04001061 case RTL818X_TX_CONF_R8180_F:
1062 chip_name = "RTL8180vF";
Andrea Merello6caefd12014-03-08 18:36:37 +01001063 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
Michael Wuf6532112007-10-14 14:43:16 -04001064 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001065
Michael Wuf6532112007-10-14 14:43:16 -04001066 case RTL818X_TX_CONF_R8185_ABC:
1067 chip_name = "RTL8185";
Andrea Merello6caefd12014-03-08 18:36:37 +01001068 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001069 break;
Andrea Merello6caefd12014-03-08 18:36:37 +01001070
Michael Wuf6532112007-10-14 14:43:16 -04001071 case RTL818X_TX_CONF_R8185_D:
1072 chip_name = "RTL8185vD";
Andrea Merello6caefd12014-03-08 18:36:37 +01001073 priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
Michael Wuf6532112007-10-14 14:43:16 -04001074 break;
1075 default:
1076 printk(KERN_ERR "%s (rtl8180): Unknown chip! (0x%x)\n",
1077 pci_name(pdev), reg >> 25);
1078 goto err_iounmap;
1079 }
1080
Andrea Merello6caefd12014-03-08 18:36:37 +01001081 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Johannes Berg8318d782008-01-24 19:38:38 +01001082 priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
Michael Wuf6532112007-10-14 14:43:16 -04001083 pci_try_set_mwi(pdev);
1084 }
1085
Michael Wuf6532112007-10-14 14:43:16 -04001086 eeprom.data = dev;
1087 eeprom.register_read = rtl8180_eeprom_register_read;
1088 eeprom.register_write = rtl8180_eeprom_register_write;
1089 if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
1090 eeprom.width = PCI_EEPROM_WIDTH_93C66;
1091 else
1092 eeprom.width = PCI_EEPROM_WIDTH_93C46;
1093
1094 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_PROGRAM);
1095 rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
1096 udelay(10);
1097
1098 eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
1099 eeprom_val &= 0xFF;
1100 switch (eeprom_val) {
1101 case 1: rf_name = "Intersil";
1102 break;
1103 case 2: rf_name = "RFMD";
1104 break;
1105 case 3: priv->rf = &sa2400_rf_ops;
1106 break;
1107 case 4: priv->rf = &max2820_rf_ops;
1108 break;
1109 case 5: priv->rf = &grf5101_rf_ops;
1110 break;
1111 case 9: priv->rf = rtl8180_detect_rf(dev);
1112 break;
1113 case 10:
1114 rf_name = "RTL8255";
1115 break;
1116 default:
1117 printk(KERN_ERR "%s (rtl8180): Unknown RF! (0x%x)\n",
1118 pci_name(pdev), eeprom_val);
1119 goto err_iounmap;
1120 }
1121
1122 if (!priv->rf) {
1123 printk(KERN_ERR "%s (rtl8180): %s RF frontend not supported!\n",
1124 pci_name(pdev), rf_name);
1125 goto err_iounmap;
1126 }
1127
1128 eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
1129 priv->csthreshold = eeprom_val >> 8;
Andrea Merello6caefd12014-03-08 18:36:37 +01001130 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8185) {
Michael Wuf6532112007-10-14 14:43:16 -04001131 __le32 anaparam;
1132 eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
1133 priv->anaparam = le32_to_cpu(anaparam);
1134 eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
1135 }
1136
John W. Linvillec693bf92010-05-04 15:46:15 -04001137 eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)mac_addr, 3);
1138 if (!is_valid_ether_addr(mac_addr)) {
Michael Wuf6532112007-10-14 14:43:16 -04001139 printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
1140 " randomly generated MAC addr\n", pci_name(pdev));
Joe Perchesf4f7f4142012-07-12 19:33:08 +00001141 eth_random_addr(mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001142 }
John W. Linvillec693bf92010-05-04 15:46:15 -04001143 SET_IEEE80211_PERM_ADDR(dev, mac_addr);
Michael Wuf6532112007-10-14 14:43:16 -04001144
1145 /* CCK TX power */
1146 for (i = 0; i < 14; i += 2) {
1147 u16 txpwr;
1148 eeprom_93cx6_read(&eeprom, 0x10 + (i >> 1), &txpwr);
Johannes Berg8318d782008-01-24 19:38:38 +01001149 priv->channels[i].hw_value = txpwr & 0xFF;
1150 priv->channels[i + 1].hw_value = txpwr >> 8;
Michael Wuf6532112007-10-14 14:43:16 -04001151 }
1152
1153 /* OFDM TX power */
Andrea Merello6caefd12014-03-08 18:36:37 +01001154 if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
Michael Wuf6532112007-10-14 14:43:16 -04001155 for (i = 0; i < 14; i += 2) {
1156 u16 txpwr;
1157 eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
Johannes Berg8318d782008-01-24 19:38:38 +01001158 priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
1159 priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
Michael Wuf6532112007-10-14 14:43:16 -04001160 }
1161 }
1162
1163 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
1164
1165 spin_lock_init(&priv->lock);
1166
1167 err = ieee80211_register_hw(dev);
1168 if (err) {
1169 printk(KERN_ERR "%s (rtl8180): Cannot register device\n",
1170 pci_name(pdev));
1171 goto err_iounmap;
1172 }
1173
Joe Perchesc96c31e2010-07-26 14:39:58 -07001174 wiphy_info(dev->wiphy, "hwaddr %pm, %s + %s\n",
1175 mac_addr, chip_name, priv->rf->name);
Michael Wuf6532112007-10-14 14:43:16 -04001176
1177 return 0;
1178
1179 err_iounmap:
andrea merello0269da22014-02-18 02:10:41 +01001180 pci_iounmap(pdev, priv->map);
Michael Wuf6532112007-10-14 14:43:16 -04001181
1182 err_free_dev:
Michael Wuf6532112007-10-14 14:43:16 -04001183 ieee80211_free_hw(dev);
1184
1185 err_free_reg:
1186 pci_release_regions(pdev);
1187 pci_disable_device(pdev);
1188 return err;
1189}
1190
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001191static void rtl8180_remove(struct pci_dev *pdev)
Michael Wuf6532112007-10-14 14:43:16 -04001192{
1193 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
1194 struct rtl8180_priv *priv;
1195
1196 if (!dev)
1197 return;
1198
1199 ieee80211_unregister_hw(dev);
1200
1201 priv = dev->priv;
1202
1203 pci_iounmap(pdev, priv->map);
1204 pci_release_regions(pdev);
1205 pci_disable_device(pdev);
1206 ieee80211_free_hw(dev);
1207}
1208
1209#ifdef CONFIG_PM
1210static int rtl8180_suspend(struct pci_dev *pdev, pm_message_t state)
1211{
1212 pci_save_state(pdev);
1213 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1214 return 0;
1215}
1216
1217static int rtl8180_resume(struct pci_dev *pdev)
1218{
1219 pci_set_power_state(pdev, PCI_D0);
1220 pci_restore_state(pdev);
1221 return 0;
1222}
1223
1224#endif /* CONFIG_PM */
1225
1226static struct pci_driver rtl8180_driver = {
1227 .name = KBUILD_MODNAME,
1228 .id_table = rtl8180_table,
1229 .probe = rtl8180_probe,
Bill Pembertonfb4e8992012-12-03 09:56:40 -05001230 .remove = rtl8180_remove,
Michael Wuf6532112007-10-14 14:43:16 -04001231#ifdef CONFIG_PM
1232 .suspend = rtl8180_suspend,
1233 .resume = rtl8180_resume,
1234#endif /* CONFIG_PM */
1235};
1236
Axel Lin5b0a3b72012-04-14 10:38:36 +08001237module_pci_driver(rtl8180_driver);