rt2800pci.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. /*
  2. Copyright (C) 2009 Ivo van Doorn <IvDoorn@gmail.com>
  3. Copyright (C) 2009 Alban Browaeys <prahal@yahoo.com>
  4. Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
  5. Copyright (C) 2009 Luis Correia <luis.f.correia@gmail.com>
  6. Copyright (C) 2009 Mattias Nissler <mattias.nissler@gmx.de>
  7. Copyright (C) 2009 Mark Asselstine <asselsm@gmail.com>
  8. Copyright (C) 2009 Xose Vazquez Perez <xose.vazquez@gmail.com>
  9. Copyright (C) 2009 Bart Zolnierkiewicz <bzolnier@gmail.com>
  10. <http://rt2x00.serialmonkey.com>
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the
  21. Free Software Foundation, Inc.,
  22. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. /*
  25. Module: rt2800pci
  26. Abstract: rt2800pci device specific routines.
  27. Supported chipsets: RT2800E & RT2800ED.
  28. */
  29. #include <linux/crc-ccitt.h>
  30. #include <linux/delay.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/init.h>
  33. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/pci.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/eeprom_93cx6.h>
  38. #include "rt2x00.h"
  39. #include "rt2x00pci.h"
  40. #include "rt2x00soc.h"
  41. #include "rt2800lib.h"
  42. #include "rt2800.h"
  43. #include "rt2800pci.h"
  44. /*
  45. * Allow hardware encryption to be disabled.
  46. */
  47. static int modparam_nohwcrypt = 1;
  48. module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
  49. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  50. static void rt2800pci_mcu_status(struct rt2x00_dev *rt2x00dev, const u8 token)
  51. {
  52. unsigned int i;
  53. u32 reg;
  54. for (i = 0; i < 200; i++) {
  55. rt2800_register_read(rt2x00dev, H2M_MAILBOX_CID, &reg);
  56. if ((rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD0) == token) ||
  57. (rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD1) == token) ||
  58. (rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD2) == token) ||
  59. (rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD3) == token))
  60. break;
  61. udelay(REGISTER_BUSY_DELAY);
  62. }
  63. if (i == 200)
  64. ERROR(rt2x00dev, "MCU request failed, no response from hardware\n");
  65. rt2800_register_write(rt2x00dev, H2M_MAILBOX_STATUS, ~0);
  66. rt2800_register_write(rt2x00dev, H2M_MAILBOX_CID, ~0);
  67. }
  68. #ifdef CONFIG_RT2800PCI_SOC
  69. static void rt2800pci_read_eeprom_soc(struct rt2x00_dev *rt2x00dev)
  70. {
  71. u32 *base_addr = (u32 *) KSEG1ADDR(0x1F040000); /* XXX for RT3052 */
  72. memcpy_fromio(rt2x00dev->eeprom, base_addr, EEPROM_SIZE);
  73. }
  74. #else
  75. static inline void rt2800pci_read_eeprom_soc(struct rt2x00_dev *rt2x00dev)
  76. {
  77. }
  78. #endif /* CONFIG_RT2800PCI_SOC */
  79. #ifdef CONFIG_RT2800PCI_PCI
  80. static void rt2800pci_eepromregister_read(struct eeprom_93cx6 *eeprom)
  81. {
  82. struct rt2x00_dev *rt2x00dev = eeprom->data;
  83. u32 reg;
  84. rt2800_register_read(rt2x00dev, E2PROM_CSR, &reg);
  85. eeprom->reg_data_in = !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_IN);
  86. eeprom->reg_data_out = !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_OUT);
  87. eeprom->reg_data_clock =
  88. !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_CLOCK);
  89. eeprom->reg_chip_select =
  90. !!rt2x00_get_field32(reg, E2PROM_CSR_CHIP_SELECT);
  91. }
  92. static void rt2800pci_eepromregister_write(struct eeprom_93cx6 *eeprom)
  93. {
  94. struct rt2x00_dev *rt2x00dev = eeprom->data;
  95. u32 reg = 0;
  96. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_IN, !!eeprom->reg_data_in);
  97. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_OUT, !!eeprom->reg_data_out);
  98. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_CLOCK,
  99. !!eeprom->reg_data_clock);
  100. rt2x00_set_field32(&reg, E2PROM_CSR_CHIP_SELECT,
  101. !!eeprom->reg_chip_select);
  102. rt2800_register_write(rt2x00dev, E2PROM_CSR, reg);
  103. }
  104. static void rt2800pci_read_eeprom_pci(struct rt2x00_dev *rt2x00dev)
  105. {
  106. struct eeprom_93cx6 eeprom;
  107. u32 reg;
  108. rt2800_register_read(rt2x00dev, E2PROM_CSR, &reg);
  109. eeprom.data = rt2x00dev;
  110. eeprom.register_read = rt2800pci_eepromregister_read;
  111. eeprom.register_write = rt2800pci_eepromregister_write;
  112. eeprom.width = !rt2x00_get_field32(reg, E2PROM_CSR_TYPE) ?
  113. PCI_EEPROM_WIDTH_93C46 : PCI_EEPROM_WIDTH_93C66;
  114. eeprom.reg_data_in = 0;
  115. eeprom.reg_data_out = 0;
  116. eeprom.reg_data_clock = 0;
  117. eeprom.reg_chip_select = 0;
  118. eeprom_93cx6_multiread(&eeprom, EEPROM_BASE, rt2x00dev->eeprom,
  119. EEPROM_SIZE / sizeof(u16));
  120. }
  121. static int rt2800pci_efuse_detect(struct rt2x00_dev *rt2x00dev)
  122. {
  123. return rt2800_efuse_detect(rt2x00dev);
  124. }
  125. static inline void rt2800pci_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
  126. {
  127. rt2800_read_eeprom_efuse(rt2x00dev);
  128. }
  129. #else
  130. static inline void rt2800pci_read_eeprom_pci(struct rt2x00_dev *rt2x00dev)
  131. {
  132. }
  133. static inline int rt2800pci_efuse_detect(struct rt2x00_dev *rt2x00dev)
  134. {
  135. return 0;
  136. }
  137. static inline void rt2800pci_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
  138. {
  139. }
  140. #endif /* CONFIG_RT2800PCI_PCI */
  141. /*
  142. * Firmware functions
  143. */
  144. static char *rt2800pci_get_firmware_name(struct rt2x00_dev *rt2x00dev)
  145. {
  146. return FIRMWARE_RT2860;
  147. }
  148. static int rt2800pci_check_firmware(struct rt2x00_dev *rt2x00dev,
  149. const u8 *data, const size_t len)
  150. {
  151. u16 fw_crc;
  152. u16 crc;
  153. /*
  154. * Only support 8kb firmware files.
  155. */
  156. if (len != 8192)
  157. return FW_BAD_LENGTH;
  158. /*
  159. * The last 2 bytes in the firmware array are the crc checksum itself,
  160. * this means that we should never pass those 2 bytes to the crc
  161. * algorithm.
  162. */
  163. fw_crc = (data[len - 2] << 8 | data[len - 1]);
  164. /*
  165. * Use the crc ccitt algorithm.
  166. * This will return the same value as the legacy driver which
  167. * used bit ordering reversion on the both the firmware bytes
  168. * before input input as well as on the final output.
  169. * Obviously using crc ccitt directly is much more efficient.
  170. */
  171. crc = crc_ccitt(~0, data, len - 2);
  172. /*
  173. * There is a small difference between the crc-itu-t + bitrev and
  174. * the crc-ccitt crc calculation. In the latter method the 2 bytes
  175. * will be swapped, use swab16 to convert the crc to the correct
  176. * value.
  177. */
  178. crc = swab16(crc);
  179. return (fw_crc == crc) ? FW_OK : FW_BAD_CRC;
  180. }
  181. static int rt2800pci_load_firmware(struct rt2x00_dev *rt2x00dev,
  182. const u8 *data, const size_t len)
  183. {
  184. unsigned int i;
  185. u32 reg;
  186. /*
  187. * Wait for stable hardware.
  188. */
  189. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  190. rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
  191. if (reg && reg != ~0)
  192. break;
  193. msleep(1);
  194. }
  195. if (i == REGISTER_BUSY_COUNT) {
  196. ERROR(rt2x00dev, "Unstable hardware.\n");
  197. return -EBUSY;
  198. }
  199. rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000002);
  200. rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0x00000000);
  201. /*
  202. * Disable DMA, will be reenabled later when enabling
  203. * the radio.
  204. */
  205. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  206. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
  207. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
  208. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
  209. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
  210. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  211. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  212. /*
  213. * enable Host program ram write selection
  214. */
  215. reg = 0;
  216. rt2x00_set_field32(&reg, PBF_SYS_CTRL_HOST_RAM_WRITE, 1);
  217. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, reg);
  218. /*
  219. * Write firmware to device.
  220. */
  221. rt2800_register_multiwrite(rt2x00dev, FIRMWARE_IMAGE_BASE,
  222. data, len);
  223. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000);
  224. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00001);
  225. /*
  226. * Wait for device to stabilize.
  227. */
  228. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  229. rt2800_register_read(rt2x00dev, PBF_SYS_CTRL, &reg);
  230. if (rt2x00_get_field32(reg, PBF_SYS_CTRL_READY))
  231. break;
  232. msleep(1);
  233. }
  234. if (i == REGISTER_BUSY_COUNT) {
  235. ERROR(rt2x00dev, "PBF system register not ready.\n");
  236. return -EBUSY;
  237. }
  238. /*
  239. * Disable interrupts
  240. */
  241. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_OFF);
  242. /*
  243. * Initialize BBP R/W access agent
  244. */
  245. rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
  246. rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
  247. return 0;
  248. }
  249. /*
  250. * Initialization functions.
  251. */
  252. static bool rt2800pci_get_entry_state(struct queue_entry *entry)
  253. {
  254. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  255. u32 word;
  256. if (entry->queue->qid == QID_RX) {
  257. rt2x00_desc_read(entry_priv->desc, 1, &word);
  258. return (!rt2x00_get_field32(word, RXD_W1_DMA_DONE));
  259. } else {
  260. rt2x00_desc_read(entry_priv->desc, 1, &word);
  261. return (!rt2x00_get_field32(word, TXD_W1_DMA_DONE));
  262. }
  263. }
  264. static void rt2800pci_clear_entry(struct queue_entry *entry)
  265. {
  266. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  267. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  268. u32 word;
  269. if (entry->queue->qid == QID_RX) {
  270. rt2x00_desc_read(entry_priv->desc, 0, &word);
  271. rt2x00_set_field32(&word, RXD_W0_SDP0, skbdesc->skb_dma);
  272. rt2x00_desc_write(entry_priv->desc, 0, word);
  273. rt2x00_desc_read(entry_priv->desc, 1, &word);
  274. rt2x00_set_field32(&word, RXD_W1_DMA_DONE, 0);
  275. rt2x00_desc_write(entry_priv->desc, 1, word);
  276. } else {
  277. rt2x00_desc_read(entry_priv->desc, 1, &word);
  278. rt2x00_set_field32(&word, TXD_W1_DMA_DONE, 1);
  279. rt2x00_desc_write(entry_priv->desc, 1, word);
  280. }
  281. }
  282. static int rt2800pci_init_queues(struct rt2x00_dev *rt2x00dev)
  283. {
  284. struct queue_entry_priv_pci *entry_priv;
  285. u32 reg;
  286. rt2800_register_read(rt2x00dev, WPDMA_RST_IDX, &reg);
  287. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX0, 1);
  288. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX1, 1);
  289. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX2, 1);
  290. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX3, 1);
  291. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX4, 1);
  292. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX5, 1);
  293. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DRX_IDX0, 1);
  294. rt2800_register_write(rt2x00dev, WPDMA_RST_IDX, reg);
  295. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000e1f);
  296. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000e00);
  297. /*
  298. * Initialize registers.
  299. */
  300. entry_priv = rt2x00dev->tx[0].entries[0].priv_data;
  301. rt2800_register_write(rt2x00dev, TX_BASE_PTR0, entry_priv->desc_dma);
  302. rt2800_register_write(rt2x00dev, TX_MAX_CNT0, rt2x00dev->tx[0].limit);
  303. rt2800_register_write(rt2x00dev, TX_CTX_IDX0, 0);
  304. rt2800_register_write(rt2x00dev, TX_DTX_IDX0, 0);
  305. entry_priv = rt2x00dev->tx[1].entries[0].priv_data;
  306. rt2800_register_write(rt2x00dev, TX_BASE_PTR1, entry_priv->desc_dma);
  307. rt2800_register_write(rt2x00dev, TX_MAX_CNT1, rt2x00dev->tx[1].limit);
  308. rt2800_register_write(rt2x00dev, TX_CTX_IDX1, 0);
  309. rt2800_register_write(rt2x00dev, TX_DTX_IDX1, 0);
  310. entry_priv = rt2x00dev->tx[2].entries[0].priv_data;
  311. rt2800_register_write(rt2x00dev, TX_BASE_PTR2, entry_priv->desc_dma);
  312. rt2800_register_write(rt2x00dev, TX_MAX_CNT2, rt2x00dev->tx[2].limit);
  313. rt2800_register_write(rt2x00dev, TX_CTX_IDX2, 0);
  314. rt2800_register_write(rt2x00dev, TX_DTX_IDX2, 0);
  315. entry_priv = rt2x00dev->tx[3].entries[0].priv_data;
  316. rt2800_register_write(rt2x00dev, TX_BASE_PTR3, entry_priv->desc_dma);
  317. rt2800_register_write(rt2x00dev, TX_MAX_CNT3, rt2x00dev->tx[3].limit);
  318. rt2800_register_write(rt2x00dev, TX_CTX_IDX3, 0);
  319. rt2800_register_write(rt2x00dev, TX_DTX_IDX3, 0);
  320. entry_priv = rt2x00dev->rx->entries[0].priv_data;
  321. rt2800_register_write(rt2x00dev, RX_BASE_PTR, entry_priv->desc_dma);
  322. rt2800_register_write(rt2x00dev, RX_MAX_CNT, rt2x00dev->rx[0].limit);
  323. rt2800_register_write(rt2x00dev, RX_CRX_IDX, rt2x00dev->rx[0].limit - 1);
  324. rt2800_register_write(rt2x00dev, RX_DRX_IDX, 0);
  325. /*
  326. * Enable global DMA configuration
  327. */
  328. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  329. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
  330. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
  331. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  332. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  333. rt2800_register_write(rt2x00dev, DELAY_INT_CFG, 0);
  334. return 0;
  335. }
  336. /*
  337. * Device state switch handlers.
  338. */
  339. static void rt2800pci_toggle_rx(struct rt2x00_dev *rt2x00dev,
  340. enum dev_state state)
  341. {
  342. u32 reg;
  343. rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  344. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX,
  345. (state == STATE_RADIO_RX_ON) ||
  346. (state == STATE_RADIO_RX_ON_LINK));
  347. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  348. }
  349. static void rt2800pci_toggle_irq(struct rt2x00_dev *rt2x00dev,
  350. enum dev_state state)
  351. {
  352. int mask = (state == STATE_RADIO_IRQ_ON);
  353. u32 reg;
  354. /*
  355. * When interrupts are being enabled, the interrupt registers
  356. * should clear the register to assure a clean state.
  357. */
  358. if (state == STATE_RADIO_IRQ_ON) {
  359. rt2800_register_read(rt2x00dev, INT_SOURCE_CSR, &reg);
  360. rt2800_register_write(rt2x00dev, INT_SOURCE_CSR, reg);
  361. }
  362. rt2800_register_read(rt2x00dev, INT_MASK_CSR, &reg);
  363. rt2x00_set_field32(&reg, INT_MASK_CSR_RXDELAYINT, mask);
  364. rt2x00_set_field32(&reg, INT_MASK_CSR_TXDELAYINT, mask);
  365. rt2x00_set_field32(&reg, INT_MASK_CSR_RX_DONE, mask);
  366. rt2x00_set_field32(&reg, INT_MASK_CSR_AC0_DMA_DONE, mask);
  367. rt2x00_set_field32(&reg, INT_MASK_CSR_AC1_DMA_DONE, mask);
  368. rt2x00_set_field32(&reg, INT_MASK_CSR_AC2_DMA_DONE, mask);
  369. rt2x00_set_field32(&reg, INT_MASK_CSR_AC3_DMA_DONE, mask);
  370. rt2x00_set_field32(&reg, INT_MASK_CSR_HCCA_DMA_DONE, mask);
  371. rt2x00_set_field32(&reg, INT_MASK_CSR_MGMT_DMA_DONE, mask);
  372. rt2x00_set_field32(&reg, INT_MASK_CSR_MCU_COMMAND, mask);
  373. rt2x00_set_field32(&reg, INT_MASK_CSR_RXTX_COHERENT, mask);
  374. rt2x00_set_field32(&reg, INT_MASK_CSR_TBTT, mask);
  375. rt2x00_set_field32(&reg, INT_MASK_CSR_PRE_TBTT, mask);
  376. rt2x00_set_field32(&reg, INT_MASK_CSR_TX_FIFO_STATUS, mask);
  377. rt2x00_set_field32(&reg, INT_MASK_CSR_AUTO_WAKEUP, mask);
  378. rt2x00_set_field32(&reg, INT_MASK_CSR_GPTIMER, mask);
  379. rt2x00_set_field32(&reg, INT_MASK_CSR_RX_COHERENT, mask);
  380. rt2x00_set_field32(&reg, INT_MASK_CSR_TX_COHERENT, mask);
  381. rt2800_register_write(rt2x00dev, INT_MASK_CSR, reg);
  382. }
  383. static int rt2800pci_wait_wpdma_ready(struct rt2x00_dev *rt2x00dev)
  384. {
  385. unsigned int i;
  386. u32 reg;
  387. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  388. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  389. if (!rt2x00_get_field32(reg, WPDMA_GLO_CFG_TX_DMA_BUSY) &&
  390. !rt2x00_get_field32(reg, WPDMA_GLO_CFG_RX_DMA_BUSY))
  391. return 0;
  392. msleep(1);
  393. }
  394. ERROR(rt2x00dev, "WPDMA TX/RX busy, aborting.\n");
  395. return -EACCES;
  396. }
  397. static int rt2800pci_enable_radio(struct rt2x00_dev *rt2x00dev)
  398. {
  399. u32 reg;
  400. u16 word;
  401. /*
  402. * Initialize all registers.
  403. */
  404. if (unlikely(rt2800pci_wait_wpdma_ready(rt2x00dev) ||
  405. rt2800pci_init_queues(rt2x00dev) ||
  406. rt2800_init_registers(rt2x00dev) ||
  407. rt2800pci_wait_wpdma_ready(rt2x00dev) ||
  408. rt2800_init_bbp(rt2x00dev) ||
  409. rt2800_init_rfcsr(rt2x00dev)))
  410. return -EIO;
  411. /*
  412. * Send signal to firmware during boot time.
  413. */
  414. rt2800_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0xff, 0, 0);
  415. /*
  416. * Enable RX.
  417. */
  418. rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  419. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
  420. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
  421. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  422. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  423. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 1);
  424. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 1);
  425. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 2);
  426. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  427. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  428. rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  429. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
  430. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 1);
  431. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  432. /*
  433. * Initialize LED control
  434. */
  435. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED1, &word);
  436. rt2800_mcu_request(rt2x00dev, MCU_LED_1, 0xff,
  437. word & 0xff, (word >> 8) & 0xff);
  438. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED2, &word);
  439. rt2800_mcu_request(rt2x00dev, MCU_LED_2, 0xff,
  440. word & 0xff, (word >> 8) & 0xff);
  441. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED3, &word);
  442. rt2800_mcu_request(rt2x00dev, MCU_LED_3, 0xff,
  443. word & 0xff, (word >> 8) & 0xff);
  444. return 0;
  445. }
  446. static void rt2800pci_disable_radio(struct rt2x00_dev *rt2x00dev)
  447. {
  448. u32 reg;
  449. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  450. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
  451. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
  452. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
  453. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
  454. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  455. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  456. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0);
  457. rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0);
  458. rt2800_register_write(rt2x00dev, TX_PIN_CFG, 0);
  459. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00001280);
  460. rt2800_register_read(rt2x00dev, WPDMA_RST_IDX, &reg);
  461. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX0, 1);
  462. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX1, 1);
  463. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX2, 1);
  464. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX3, 1);
  465. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX4, 1);
  466. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX5, 1);
  467. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DRX_IDX0, 1);
  468. rt2800_register_write(rt2x00dev, WPDMA_RST_IDX, reg);
  469. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000e1f);
  470. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000e00);
  471. /* Wait for DMA, ignore error */
  472. rt2800pci_wait_wpdma_ready(rt2x00dev);
  473. }
  474. static int rt2800pci_set_state(struct rt2x00_dev *rt2x00dev,
  475. enum dev_state state)
  476. {
  477. /*
  478. * Always put the device to sleep (even when we intend to wakeup!)
  479. * if the device is booting and wasn't asleep it will return
  480. * failure when attempting to wakeup.
  481. */
  482. rt2800_mcu_request(rt2x00dev, MCU_SLEEP, 0xff, 0, 2);
  483. if (state == STATE_AWAKE) {
  484. rt2800_mcu_request(rt2x00dev, MCU_WAKEUP, TOKEN_WAKUP, 0, 0);
  485. rt2800pci_mcu_status(rt2x00dev, TOKEN_WAKUP);
  486. }
  487. return 0;
  488. }
  489. static int rt2800pci_set_device_state(struct rt2x00_dev *rt2x00dev,
  490. enum dev_state state)
  491. {
  492. int retval = 0;
  493. switch (state) {
  494. case STATE_RADIO_ON:
  495. /*
  496. * Before the radio can be enabled, the device first has
  497. * to be woken up. After that it needs a bit of time
  498. * to be fully awake and then the radio can be enabled.
  499. */
  500. rt2800pci_set_state(rt2x00dev, STATE_AWAKE);
  501. msleep(1);
  502. retval = rt2800pci_enable_radio(rt2x00dev);
  503. break;
  504. case STATE_RADIO_OFF:
  505. /*
  506. * After the radio has been disabled, the device should
  507. * be put to sleep for powersaving.
  508. */
  509. rt2800pci_disable_radio(rt2x00dev);
  510. rt2800pci_set_state(rt2x00dev, STATE_SLEEP);
  511. break;
  512. case STATE_RADIO_RX_ON:
  513. case STATE_RADIO_RX_ON_LINK:
  514. case STATE_RADIO_RX_OFF:
  515. case STATE_RADIO_RX_OFF_LINK:
  516. rt2800pci_toggle_rx(rt2x00dev, state);
  517. break;
  518. case STATE_RADIO_IRQ_ON:
  519. case STATE_RADIO_IRQ_OFF:
  520. rt2800pci_toggle_irq(rt2x00dev, state);
  521. break;
  522. case STATE_DEEP_SLEEP:
  523. case STATE_SLEEP:
  524. case STATE_STANDBY:
  525. case STATE_AWAKE:
  526. retval = rt2800pci_set_state(rt2x00dev, state);
  527. break;
  528. default:
  529. retval = -ENOTSUPP;
  530. break;
  531. }
  532. if (unlikely(retval))
  533. ERROR(rt2x00dev, "Device failed to enter state %d (%d).\n",
  534. state, retval);
  535. return retval;
  536. }
  537. /*
  538. * TX descriptor initialization
  539. */
  540. static void rt2800pci_write_tx_desc(struct rt2x00_dev *rt2x00dev,
  541. struct sk_buff *skb,
  542. struct txentry_desc *txdesc)
  543. {
  544. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  545. __le32 *txd = skbdesc->desc;
  546. __le32 *txwi = (__le32 *)(skb->data - rt2x00dev->ops->extra_tx_headroom);
  547. u32 word;
  548. /*
  549. * Initialize TX Info descriptor
  550. */
  551. rt2x00_desc_read(txwi, 0, &word);
  552. rt2x00_set_field32(&word, TXWI_W0_FRAG,
  553. test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
  554. rt2x00_set_field32(&word, TXWI_W0_MIMO_PS, 0);
  555. rt2x00_set_field32(&word, TXWI_W0_CF_ACK, 0);
  556. rt2x00_set_field32(&word, TXWI_W0_TS,
  557. test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
  558. rt2x00_set_field32(&word, TXWI_W0_AMPDU,
  559. test_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags));
  560. rt2x00_set_field32(&word, TXWI_W0_MPDU_DENSITY, txdesc->mpdu_density);
  561. rt2x00_set_field32(&word, TXWI_W0_TX_OP, txdesc->ifs);
  562. rt2x00_set_field32(&word, TXWI_W0_MCS, txdesc->mcs);
  563. rt2x00_set_field32(&word, TXWI_W0_BW,
  564. test_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags));
  565. rt2x00_set_field32(&word, TXWI_W0_SHORT_GI,
  566. test_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags));
  567. rt2x00_set_field32(&word, TXWI_W0_STBC, txdesc->stbc);
  568. rt2x00_set_field32(&word, TXWI_W0_PHYMODE, txdesc->rate_mode);
  569. rt2x00_desc_write(txwi, 0, word);
  570. rt2x00_desc_read(txwi, 1, &word);
  571. rt2x00_set_field32(&word, TXWI_W1_ACK,
  572. test_bit(ENTRY_TXD_ACK, &txdesc->flags));
  573. rt2x00_set_field32(&word, TXWI_W1_NSEQ,
  574. test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
  575. rt2x00_set_field32(&word, TXWI_W1_BW_WIN_SIZE, txdesc->ba_size);
  576. rt2x00_set_field32(&word, TXWI_W1_WIRELESS_CLI_ID,
  577. test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags) ?
  578. txdesc->key_idx : 0xff);
  579. rt2x00_set_field32(&word, TXWI_W1_MPDU_TOTAL_BYTE_COUNT,
  580. skb->len - txdesc->l2pad);
  581. rt2x00_set_field32(&word, TXWI_W1_PACKETID,
  582. skbdesc->entry->queue->qid + 1);
  583. rt2x00_desc_write(txwi, 1, word);
  584. /*
  585. * Always write 0 to IV/EIV fields, hardware will insert the IV
  586. * from the IVEIV register when TXD_W3_WIV is set to 0.
  587. * When TXD_W3_WIV is set to 1 it will use the IV data
  588. * from the descriptor. The TXWI_W1_WIRELESS_CLI_ID indicates which
  589. * crypto entry in the registers should be used to encrypt the frame.
  590. */
  591. _rt2x00_desc_write(txwi, 2, 0 /* skbdesc->iv[0] */);
  592. _rt2x00_desc_write(txwi, 3, 0 /* skbdesc->iv[1] */);
  593. /*
  594. * The buffers pointed by SD_PTR0/SD_LEN0 and SD_PTR1/SD_LEN1
  595. * must contains a TXWI structure + 802.11 header + padding + 802.11
  596. * data. We choose to have SD_PTR0/SD_LEN0 only contains TXWI and
  597. * SD_PTR1/SD_LEN1 contains 802.11 header + padding + 802.11
  598. * data. It means that LAST_SEC0 is always 0.
  599. */
  600. /*
  601. * Initialize TX descriptor
  602. */
  603. rt2x00_desc_read(txd, 0, &word);
  604. rt2x00_set_field32(&word, TXD_W0_SD_PTR0, skbdesc->skb_dma);
  605. rt2x00_desc_write(txd, 0, word);
  606. rt2x00_desc_read(txd, 1, &word);
  607. rt2x00_set_field32(&word, TXD_W1_SD_LEN1, skb->len);
  608. rt2x00_set_field32(&word, TXD_W1_LAST_SEC1,
  609. !test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
  610. rt2x00_set_field32(&word, TXD_W1_BURST,
  611. test_bit(ENTRY_TXD_BURST, &txdesc->flags));
  612. rt2x00_set_field32(&word, TXD_W1_SD_LEN0,
  613. rt2x00dev->ops->extra_tx_headroom);
  614. rt2x00_set_field32(&word, TXD_W1_LAST_SEC0, 0);
  615. rt2x00_set_field32(&word, TXD_W1_DMA_DONE, 0);
  616. rt2x00_desc_write(txd, 1, word);
  617. rt2x00_desc_read(txd, 2, &word);
  618. rt2x00_set_field32(&word, TXD_W2_SD_PTR1,
  619. skbdesc->skb_dma + rt2x00dev->ops->extra_tx_headroom);
  620. rt2x00_desc_write(txd, 2, word);
  621. rt2x00_desc_read(txd, 3, &word);
  622. rt2x00_set_field32(&word, TXD_W3_WIV,
  623. !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags));
  624. rt2x00_set_field32(&word, TXD_W3_QSEL, 2);
  625. rt2x00_desc_write(txd, 3, word);
  626. }
  627. /*
  628. * TX data initialization
  629. */
  630. static void rt2800pci_write_beacon(struct queue_entry *entry)
  631. {
  632. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  633. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  634. unsigned int beacon_base;
  635. u32 reg;
  636. /*
  637. * Disable beaconing while we are reloading the beacon data,
  638. * otherwise we might be sending out invalid data.
  639. */
  640. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  641. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
  642. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  643. /*
  644. * Write entire beacon with descriptor to register.
  645. */
  646. beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
  647. rt2800_register_multiwrite(rt2x00dev,
  648. beacon_base,
  649. skbdesc->desc, skbdesc->desc_len);
  650. rt2800_register_multiwrite(rt2x00dev,
  651. beacon_base + skbdesc->desc_len,
  652. entry->skb->data, entry->skb->len);
  653. /*
  654. * Clean up beacon skb.
  655. */
  656. dev_kfree_skb_any(entry->skb);
  657. entry->skb = NULL;
  658. }
  659. static void rt2800pci_kick_tx_queue(struct rt2x00_dev *rt2x00dev,
  660. const enum data_queue_qid queue_idx)
  661. {
  662. struct data_queue *queue;
  663. unsigned int idx, qidx = 0;
  664. u32 reg;
  665. if (queue_idx == QID_BEACON) {
  666. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  667. if (!rt2x00_get_field32(reg, BCN_TIME_CFG_BEACON_GEN)) {
  668. rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
  669. rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 1);
  670. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
  671. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  672. }
  673. return;
  674. }
  675. if (queue_idx > QID_HCCA && queue_idx != QID_MGMT)
  676. return;
  677. queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
  678. idx = queue->index[Q_INDEX];
  679. if (queue_idx == QID_MGMT)
  680. qidx = 5;
  681. else
  682. qidx = queue_idx;
  683. rt2800_register_write(rt2x00dev, TX_CTX_IDX(qidx), idx);
  684. }
  685. static void rt2800pci_kill_tx_queue(struct rt2x00_dev *rt2x00dev,
  686. const enum data_queue_qid qid)
  687. {
  688. u32 reg;
  689. if (qid == QID_BEACON) {
  690. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, 0);
  691. return;
  692. }
  693. rt2800_register_read(rt2x00dev, WPDMA_RST_IDX, &reg);
  694. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX0, (qid == QID_AC_BE));
  695. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX1, (qid == QID_AC_BK));
  696. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX2, (qid == QID_AC_VI));
  697. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX3, (qid == QID_AC_VO));
  698. rt2800_register_write(rt2x00dev, WPDMA_RST_IDX, reg);
  699. }
  700. /*
  701. * RX control handlers
  702. */
  703. static void rt2800pci_fill_rxdone(struct queue_entry *entry,
  704. struct rxdone_entry_desc *rxdesc)
  705. {
  706. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  707. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  708. __le32 *rxd = entry_priv->desc;
  709. __le32 *rxwi = (__le32 *)entry->skb->data;
  710. u32 rxd3;
  711. u32 rxwi0;
  712. u32 rxwi1;
  713. u32 rxwi2;
  714. u32 rxwi3;
  715. rt2x00_desc_read(rxd, 3, &rxd3);
  716. rt2x00_desc_read(rxwi, 0, &rxwi0);
  717. rt2x00_desc_read(rxwi, 1, &rxwi1);
  718. rt2x00_desc_read(rxwi, 2, &rxwi2);
  719. rt2x00_desc_read(rxwi, 3, &rxwi3);
  720. if (rt2x00_get_field32(rxd3, RXD_W3_CRC_ERROR))
  721. rxdesc->flags |= RX_FLAG_FAILED_FCS_CRC;
  722. if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags)) {
  723. /*
  724. * Unfortunately we don't know the cipher type used during
  725. * decryption. This prevents us from correct providing
  726. * correct statistics through debugfs.
  727. */
  728. rxdesc->cipher = rt2x00_get_field32(rxwi0, RXWI_W0_UDF);
  729. rxdesc->cipher_status =
  730. rt2x00_get_field32(rxd3, RXD_W3_CIPHER_ERROR);
  731. }
  732. if (rt2x00_get_field32(rxd3, RXD_W3_DECRYPTED)) {
  733. /*
  734. * Hardware has stripped IV/EIV data from 802.11 frame during
  735. * decryption. Unfortunately the descriptor doesn't contain
  736. * any fields with the EIV/IV data either, so they can't
  737. * be restored by rt2x00lib.
  738. */
  739. rxdesc->flags |= RX_FLAG_IV_STRIPPED;
  740. if (rxdesc->cipher_status == RX_CRYPTO_SUCCESS)
  741. rxdesc->flags |= RX_FLAG_DECRYPTED;
  742. else if (rxdesc->cipher_status == RX_CRYPTO_FAIL_MIC)
  743. rxdesc->flags |= RX_FLAG_MMIC_ERROR;
  744. }
  745. if (rt2x00_get_field32(rxd3, RXD_W3_MY_BSS))
  746. rxdesc->dev_flags |= RXDONE_MY_BSS;
  747. if (rt2x00_get_field32(rxd3, RXD_W3_L2PAD))
  748. rxdesc->dev_flags |= RXDONE_L2PAD;
  749. if (rt2x00_get_field32(rxwi1, RXWI_W1_SHORT_GI))
  750. rxdesc->flags |= RX_FLAG_SHORT_GI;
  751. if (rt2x00_get_field32(rxwi1, RXWI_W1_BW))
  752. rxdesc->flags |= RX_FLAG_40MHZ;
  753. /*
  754. * Detect RX rate, always use MCS as signal type.
  755. */
  756. rxdesc->dev_flags |= RXDONE_SIGNAL_MCS;
  757. rxdesc->rate_mode = rt2x00_get_field32(rxwi1, RXWI_W1_PHYMODE);
  758. rxdesc->signal = rt2x00_get_field32(rxwi1, RXWI_W1_MCS);
  759. /*
  760. * Mask of 0x8 bit to remove the short preamble flag.
  761. */
  762. if (rxdesc->rate_mode == RATE_MODE_CCK)
  763. rxdesc->signal &= ~0x8;
  764. rxdesc->rssi =
  765. (rt2x00_get_field32(rxwi2, RXWI_W2_RSSI0) +
  766. rt2x00_get_field32(rxwi2, RXWI_W2_RSSI1)) / 2;
  767. rxdesc->noise =
  768. (rt2x00_get_field32(rxwi3, RXWI_W3_SNR0) +
  769. rt2x00_get_field32(rxwi3, RXWI_W3_SNR1)) / 2;
  770. rxdesc->size = rt2x00_get_field32(rxwi0, RXWI_W0_MPDU_TOTAL_BYTE_COUNT);
  771. /*
  772. * Set RX IDX in register to inform hardware that we have handled
  773. * this entry and it is available for reuse again.
  774. */
  775. rt2800_register_write(rt2x00dev, RX_CRX_IDX, entry->entry_idx);
  776. /*
  777. * Remove TXWI descriptor from start of buffer.
  778. */
  779. skb_pull(entry->skb, RXWI_DESC_SIZE);
  780. }
  781. /*
  782. * Interrupt functions.
  783. */
  784. static void rt2800pci_txdone(struct rt2x00_dev *rt2x00dev)
  785. {
  786. struct data_queue *queue;
  787. struct queue_entry *entry;
  788. struct queue_entry *entry_done;
  789. struct queue_entry_priv_pci *entry_priv;
  790. struct txdone_entry_desc txdesc;
  791. u32 word;
  792. u32 reg;
  793. u32 old_reg;
  794. unsigned int type;
  795. unsigned int index;
  796. u16 mcs, real_mcs;
  797. /*
  798. * During each loop we will compare the freshly read
  799. * TX_STA_FIFO register value with the value read from
  800. * the previous loop. If the 2 values are equal then
  801. * we should stop processing because the chance it
  802. * quite big that the device has been unplugged and
  803. * we risk going into an endless loop.
  804. */
  805. old_reg = 0;
  806. while (1) {
  807. rt2800_register_read(rt2x00dev, TX_STA_FIFO, &reg);
  808. if (!rt2x00_get_field32(reg, TX_STA_FIFO_VALID))
  809. break;
  810. if (old_reg == reg)
  811. break;
  812. old_reg = reg;
  813. /*
  814. * Skip this entry when it contains an invalid
  815. * queue identication number.
  816. */
  817. type = rt2x00_get_field32(reg, TX_STA_FIFO_PID_TYPE) - 1;
  818. if (type >= QID_RX)
  819. continue;
  820. queue = rt2x00queue_get_queue(rt2x00dev, type);
  821. if (unlikely(!queue))
  822. continue;
  823. /*
  824. * Skip this entry when it contains an invalid
  825. * index number.
  826. */
  827. index = rt2x00_get_field32(reg, TX_STA_FIFO_WCID) - 1;
  828. if (unlikely(index >= queue->limit))
  829. continue;
  830. entry = &queue->entries[index];
  831. entry_priv = entry->priv_data;
  832. rt2x00_desc_read((__le32 *)entry->skb->data, 0, &word);
  833. entry_done = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
  834. while (entry != entry_done) {
  835. /*
  836. * Catch up.
  837. * Just report any entries we missed as failed.
  838. */
  839. WARNING(rt2x00dev,
  840. "TX status report missed for entry %d\n",
  841. entry_done->entry_idx);
  842. txdesc.flags = 0;
  843. __set_bit(TXDONE_UNKNOWN, &txdesc.flags);
  844. txdesc.retry = 0;
  845. rt2x00lib_txdone(entry_done, &txdesc);
  846. entry_done = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
  847. }
  848. /*
  849. * Obtain the status about this packet.
  850. */
  851. txdesc.flags = 0;
  852. if (rt2x00_get_field32(reg, TX_STA_FIFO_TX_SUCCESS))
  853. __set_bit(TXDONE_SUCCESS, &txdesc.flags);
  854. else
  855. __set_bit(TXDONE_FAILURE, &txdesc.flags);
  856. /*
  857. * Ralink has a retry mechanism using a global fallback
  858. * table. We setup this fallback table to try immediate
  859. * lower rate for all rates. In the TX_STA_FIFO,
  860. * the MCS field contains the MCS used for the successfull
  861. * transmission. If the first transmission succeed,
  862. * we have mcs == tx_mcs. On the second transmission,
  863. * we have mcs = tx_mcs - 1. So the number of
  864. * retry is (tx_mcs - mcs).
  865. */
  866. mcs = rt2x00_get_field32(word, TXWI_W0_MCS);
  867. real_mcs = rt2x00_get_field32(reg, TX_STA_FIFO_MCS);
  868. __set_bit(TXDONE_FALLBACK, &txdesc.flags);
  869. txdesc.retry = mcs - min(mcs, real_mcs);
  870. rt2x00lib_txdone(entry, &txdesc);
  871. }
  872. }
  873. static irqreturn_t rt2800pci_interrupt(int irq, void *dev_instance)
  874. {
  875. struct rt2x00_dev *rt2x00dev = dev_instance;
  876. u32 reg;
  877. /* Read status and ACK all interrupts */
  878. rt2800_register_read(rt2x00dev, INT_SOURCE_CSR, &reg);
  879. rt2800_register_write(rt2x00dev, INT_SOURCE_CSR, reg);
  880. if (!reg)
  881. return IRQ_NONE;
  882. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  883. return IRQ_HANDLED;
  884. /*
  885. * 1 - Rx ring done interrupt.
  886. */
  887. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_RX_DONE))
  888. rt2x00pci_rxdone(rt2x00dev);
  889. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_TX_FIFO_STATUS))
  890. rt2800pci_txdone(rt2x00dev);
  891. return IRQ_HANDLED;
  892. }
  893. /*
  894. * Device probe functions.
  895. */
  896. static int rt2800pci_validate_eeprom(struct rt2x00_dev *rt2x00dev)
  897. {
  898. /*
  899. * Read EEPROM into buffer
  900. */
  901. switch (rt2x00dev->chip.rt) {
  902. case RT2880:
  903. case RT3052:
  904. rt2800pci_read_eeprom_soc(rt2x00dev);
  905. break;
  906. default:
  907. if (rt2800pci_efuse_detect(rt2x00dev))
  908. rt2800pci_read_eeprom_efuse(rt2x00dev);
  909. else
  910. rt2800pci_read_eeprom_pci(rt2x00dev);
  911. break;
  912. }
  913. return rt2800_validate_eeprom(rt2x00dev);
  914. }
  915. static const struct rt2800_ops rt2800pci_rt2800_ops = {
  916. .register_read = rt2x00pci_register_read,
  917. .register_read_lock = rt2x00pci_register_read, /* same for PCI */
  918. .register_write = rt2x00pci_register_write,
  919. .register_write_lock = rt2x00pci_register_write, /* same for PCI */
  920. .register_multiread = rt2x00pci_register_multiread,
  921. .register_multiwrite = rt2x00pci_register_multiwrite,
  922. .regbusy_read = rt2x00pci_regbusy_read,
  923. };
  924. static int rt2800pci_probe_hw(struct rt2x00_dev *rt2x00dev)
  925. {
  926. int retval;
  927. rt2x00dev->priv = (void *)&rt2800pci_rt2800_ops;
  928. /*
  929. * Allocate eeprom data.
  930. */
  931. retval = rt2800pci_validate_eeprom(rt2x00dev);
  932. if (retval)
  933. return retval;
  934. retval = rt2800_init_eeprom(rt2x00dev);
  935. if (retval)
  936. return retval;
  937. /*
  938. * Initialize hw specifications.
  939. */
  940. retval = rt2800_probe_hw_mode(rt2x00dev);
  941. if (retval)
  942. return retval;
  943. /*
  944. * This device has multiple filters for control frames
  945. * and has a separate filter for PS Poll frames.
  946. */
  947. __set_bit(DRIVER_SUPPORT_CONTROL_FILTERS, &rt2x00dev->flags);
  948. __set_bit(DRIVER_SUPPORT_CONTROL_FILTER_PSPOLL, &rt2x00dev->flags);
  949. /*
  950. * This device requires firmware.
  951. */
  952. if (!rt2x00_rt(rt2x00dev, RT2880) && !rt2x00_rt(rt2x00dev, RT3052))
  953. __set_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags);
  954. __set_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags);
  955. __set_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags);
  956. if (!modparam_nohwcrypt)
  957. __set_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags);
  958. /*
  959. * Set the rssi offset.
  960. */
  961. rt2x00dev->rssi_offset = DEFAULT_RSSI_OFFSET;
  962. return 0;
  963. }
  964. static const struct rt2x00lib_ops rt2800pci_rt2x00_ops = {
  965. .irq_handler = rt2800pci_interrupt,
  966. .probe_hw = rt2800pci_probe_hw,
  967. .get_firmware_name = rt2800pci_get_firmware_name,
  968. .check_firmware = rt2800pci_check_firmware,
  969. .load_firmware = rt2800pci_load_firmware,
  970. .initialize = rt2x00pci_initialize,
  971. .uninitialize = rt2x00pci_uninitialize,
  972. .get_entry_state = rt2800pci_get_entry_state,
  973. .clear_entry = rt2800pci_clear_entry,
  974. .set_device_state = rt2800pci_set_device_state,
  975. .rfkill_poll = rt2800_rfkill_poll,
  976. .link_stats = rt2800_link_stats,
  977. .reset_tuner = rt2800_reset_tuner,
  978. .link_tuner = rt2800_link_tuner,
  979. .write_tx_desc = rt2800pci_write_tx_desc,
  980. .write_tx_data = rt2x00pci_write_tx_data,
  981. .write_beacon = rt2800pci_write_beacon,
  982. .kick_tx_queue = rt2800pci_kick_tx_queue,
  983. .kill_tx_queue = rt2800pci_kill_tx_queue,
  984. .fill_rxdone = rt2800pci_fill_rxdone,
  985. .config_shared_key = rt2800_config_shared_key,
  986. .config_pairwise_key = rt2800_config_pairwise_key,
  987. .config_filter = rt2800_config_filter,
  988. .config_intf = rt2800_config_intf,
  989. .config_erp = rt2800_config_erp,
  990. .config_ant = rt2800_config_ant,
  991. .config = rt2800_config,
  992. };
  993. static const struct data_queue_desc rt2800pci_queue_rx = {
  994. .entry_num = RX_ENTRIES,
  995. .data_size = AGGREGATION_SIZE,
  996. .desc_size = RXD_DESC_SIZE,
  997. .priv_size = sizeof(struct queue_entry_priv_pci),
  998. };
  999. static const struct data_queue_desc rt2800pci_queue_tx = {
  1000. .entry_num = TX_ENTRIES,
  1001. .data_size = AGGREGATION_SIZE,
  1002. .desc_size = TXD_DESC_SIZE,
  1003. .priv_size = sizeof(struct queue_entry_priv_pci),
  1004. };
  1005. static const struct data_queue_desc rt2800pci_queue_bcn = {
  1006. .entry_num = 8 * BEACON_ENTRIES,
  1007. .data_size = 0, /* No DMA required for beacons */
  1008. .desc_size = TXWI_DESC_SIZE,
  1009. .priv_size = sizeof(struct queue_entry_priv_pci),
  1010. };
  1011. static const struct rt2x00_ops rt2800pci_ops = {
  1012. .name = KBUILD_MODNAME,
  1013. .max_sta_intf = 1,
  1014. .max_ap_intf = 8,
  1015. .eeprom_size = EEPROM_SIZE,
  1016. .rf_size = RF_SIZE,
  1017. .tx_queues = NUM_TX_QUEUES,
  1018. .extra_tx_headroom = TXWI_DESC_SIZE,
  1019. .rx = &rt2800pci_queue_rx,
  1020. .tx = &rt2800pci_queue_tx,
  1021. .bcn = &rt2800pci_queue_bcn,
  1022. .lib = &rt2800pci_rt2x00_ops,
  1023. .hw = &rt2800_mac80211_ops,
  1024. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  1025. .debugfs = &rt2800_rt2x00debug,
  1026. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  1027. };
  1028. /*
  1029. * RT2800pci module information.
  1030. */
  1031. static struct pci_device_id rt2800pci_device_table[] = {
  1032. { PCI_DEVICE(0x1462, 0x891a), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1033. { PCI_DEVICE(0x1432, 0x7708), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1034. { PCI_DEVICE(0x1432, 0x7727), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1035. { PCI_DEVICE(0x1432, 0x7728), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1036. { PCI_DEVICE(0x1432, 0x7738), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1037. { PCI_DEVICE(0x1432, 0x7748), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1038. { PCI_DEVICE(0x1432, 0x7758), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1039. { PCI_DEVICE(0x1432, 0x7768), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1040. { PCI_DEVICE(0x1814, 0x0601), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1041. { PCI_DEVICE(0x1814, 0x0681), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1042. { PCI_DEVICE(0x1814, 0x0701), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1043. { PCI_DEVICE(0x1814, 0x0781), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1044. { PCI_DEVICE(0x1814, 0x3060), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1045. { PCI_DEVICE(0x1814, 0x3062), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1046. { PCI_DEVICE(0x1814, 0x3090), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1047. { PCI_DEVICE(0x1814, 0x3091), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1048. { PCI_DEVICE(0x1814, 0x3092), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1049. { PCI_DEVICE(0x1814, 0x3562), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1050. { PCI_DEVICE(0x1814, 0x3592), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1051. { PCI_DEVICE(0x1a3b, 0x1059), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1052. { 0, }
  1053. };
  1054. MODULE_AUTHOR(DRV_PROJECT);
  1055. MODULE_VERSION(DRV_VERSION);
  1056. MODULE_DESCRIPTION("Ralink RT2800 PCI & PCMCIA Wireless LAN driver.");
  1057. MODULE_SUPPORTED_DEVICE("Ralink RT2860 PCI & PCMCIA chipset based cards");
  1058. #ifdef CONFIG_RT2800PCI_PCI
  1059. MODULE_FIRMWARE(FIRMWARE_RT2860);
  1060. MODULE_DEVICE_TABLE(pci, rt2800pci_device_table);
  1061. #endif /* CONFIG_RT2800PCI_PCI */
  1062. MODULE_LICENSE("GPL");
  1063. #ifdef CONFIG_RT2800PCI_SOC
  1064. #if defined(CONFIG_RALINK_RT288X)
  1065. __rt2x00soc_probe(RT2880, &rt2800pci_ops);
  1066. #elif defined(CONFIG_RALINK_RT305X)
  1067. __rt2x00soc_probe(RT3052, &rt2800pci_ops);
  1068. #endif
  1069. static struct platform_driver rt2800soc_driver = {
  1070. .driver = {
  1071. .name = "rt2800_wmac",
  1072. .owner = THIS_MODULE,
  1073. .mod_name = KBUILD_MODNAME,
  1074. },
  1075. .probe = __rt2x00soc_probe,
  1076. .remove = __devexit_p(rt2x00soc_remove),
  1077. .suspend = rt2x00soc_suspend,
  1078. .resume = rt2x00soc_resume,
  1079. };
  1080. #endif /* CONFIG_RT2800PCI_SOC */
  1081. #ifdef CONFIG_RT2800PCI_PCI
  1082. static struct pci_driver rt2800pci_driver = {
  1083. .name = KBUILD_MODNAME,
  1084. .id_table = rt2800pci_device_table,
  1085. .probe = rt2x00pci_probe,
  1086. .remove = __devexit_p(rt2x00pci_remove),
  1087. .suspend = rt2x00pci_suspend,
  1088. .resume = rt2x00pci_resume,
  1089. };
  1090. #endif /* CONFIG_RT2800PCI_PCI */
  1091. static int __init rt2800pci_init(void)
  1092. {
  1093. int ret = 0;
  1094. #ifdef CONFIG_RT2800PCI_SOC
  1095. ret = platform_driver_register(&rt2800soc_driver);
  1096. if (ret)
  1097. return ret;
  1098. #endif
  1099. #ifdef CONFIG_RT2800PCI_PCI
  1100. ret = pci_register_driver(&rt2800pci_driver);
  1101. if (ret) {
  1102. #ifdef CONFIG_RT2800PCI_SOC
  1103. platform_driver_unregister(&rt2800soc_driver);
  1104. #endif
  1105. return ret;
  1106. }
  1107. #endif
  1108. return ret;
  1109. }
  1110. static void __exit rt2800pci_exit(void)
  1111. {
  1112. #ifdef CONFIG_RT2800PCI_PCI
  1113. pci_unregister_driver(&rt2800pci_driver);
  1114. #endif
  1115. #ifdef CONFIG_RT2800PCI_SOC
  1116. platform_driver_unregister(&rt2800soc_driver);
  1117. #endif
  1118. }
  1119. module_init(rt2800pci_init);
  1120. module_exit(rt2800pci_exit);