rt2800pci.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  1. /*
  2. Copyright (C) 2009 - 2010 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/delay.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/init.h>
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/pci.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/eeprom_93cx6.h>
  37. #include "rt2x00.h"
  38. #include "rt2x00mmio.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 bool modparam_nohwcrypt = false;
  48. module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
  49. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  50. static bool rt2800pci_hwcrypt_disabled(struct rt2x00_dev *rt2x00dev)
  51. {
  52. return modparam_nohwcrypt;
  53. }
  54. static void rt2800pci_mcu_status(struct rt2x00_dev *rt2x00dev, const u8 token)
  55. {
  56. unsigned int i;
  57. u32 reg;
  58. /*
  59. * SOC devices don't support MCU requests.
  60. */
  61. if (rt2x00_is_soc(rt2x00dev))
  62. return;
  63. for (i = 0; i < 200; i++) {
  64. rt2x00mmio_register_read(rt2x00dev, H2M_MAILBOX_CID, &reg);
  65. if ((rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD0) == token) ||
  66. (rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD1) == token) ||
  67. (rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD2) == token) ||
  68. (rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD3) == token))
  69. break;
  70. udelay(REGISTER_BUSY_DELAY);
  71. }
  72. if (i == 200)
  73. rt2x00_err(rt2x00dev, "MCU request failed, no response from hardware\n");
  74. rt2x00mmio_register_write(rt2x00dev, H2M_MAILBOX_STATUS, ~0);
  75. rt2x00mmio_register_write(rt2x00dev, H2M_MAILBOX_CID, ~0);
  76. }
  77. #if defined(CONFIG_SOC_RT288X) || defined(CONFIG_SOC_RT305X)
  78. static int rt2800pci_read_eeprom_soc(struct rt2x00_dev *rt2x00dev)
  79. {
  80. void __iomem *base_addr = ioremap(0x1F040000, EEPROM_SIZE);
  81. if (!base_addr)
  82. return -ENOMEM;
  83. memcpy_fromio(rt2x00dev->eeprom, base_addr, EEPROM_SIZE);
  84. iounmap(base_addr);
  85. return 0;
  86. }
  87. #else
  88. static inline int rt2800pci_read_eeprom_soc(struct rt2x00_dev *rt2x00dev)
  89. {
  90. return -ENOMEM;
  91. }
  92. #endif /* CONFIG_SOC_RT288X || CONFIG_SOC_RT305X */
  93. #ifdef CONFIG_PCI
  94. static void rt2800pci_eepromregister_read(struct eeprom_93cx6 *eeprom)
  95. {
  96. struct rt2x00_dev *rt2x00dev = eeprom->data;
  97. u32 reg;
  98. rt2x00mmio_register_read(rt2x00dev, E2PROM_CSR, &reg);
  99. eeprom->reg_data_in = !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_IN);
  100. eeprom->reg_data_out = !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_OUT);
  101. eeprom->reg_data_clock =
  102. !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_CLOCK);
  103. eeprom->reg_chip_select =
  104. !!rt2x00_get_field32(reg, E2PROM_CSR_CHIP_SELECT);
  105. }
  106. static void rt2800pci_eepromregister_write(struct eeprom_93cx6 *eeprom)
  107. {
  108. struct rt2x00_dev *rt2x00dev = eeprom->data;
  109. u32 reg = 0;
  110. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_IN, !!eeprom->reg_data_in);
  111. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_OUT, !!eeprom->reg_data_out);
  112. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_CLOCK,
  113. !!eeprom->reg_data_clock);
  114. rt2x00_set_field32(&reg, E2PROM_CSR_CHIP_SELECT,
  115. !!eeprom->reg_chip_select);
  116. rt2x00mmio_register_write(rt2x00dev, E2PROM_CSR, reg);
  117. }
  118. static int rt2800pci_read_eeprom_pci(struct rt2x00_dev *rt2x00dev)
  119. {
  120. struct eeprom_93cx6 eeprom;
  121. u32 reg;
  122. rt2x00mmio_register_read(rt2x00dev, E2PROM_CSR, &reg);
  123. eeprom.data = rt2x00dev;
  124. eeprom.register_read = rt2800pci_eepromregister_read;
  125. eeprom.register_write = rt2800pci_eepromregister_write;
  126. switch (rt2x00_get_field32(reg, E2PROM_CSR_TYPE))
  127. {
  128. case 0:
  129. eeprom.width = PCI_EEPROM_WIDTH_93C46;
  130. break;
  131. case 1:
  132. eeprom.width = PCI_EEPROM_WIDTH_93C66;
  133. break;
  134. default:
  135. eeprom.width = PCI_EEPROM_WIDTH_93C86;
  136. break;
  137. }
  138. eeprom.reg_data_in = 0;
  139. eeprom.reg_data_out = 0;
  140. eeprom.reg_data_clock = 0;
  141. eeprom.reg_chip_select = 0;
  142. eeprom_93cx6_multiread(&eeprom, EEPROM_BASE, rt2x00dev->eeprom,
  143. EEPROM_SIZE / sizeof(u16));
  144. return 0;
  145. }
  146. static int rt2800pci_efuse_detect(struct rt2x00_dev *rt2x00dev)
  147. {
  148. return rt2800_efuse_detect(rt2x00dev);
  149. }
  150. static inline int rt2800pci_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
  151. {
  152. return rt2800_read_eeprom_efuse(rt2x00dev);
  153. }
  154. #else
  155. static inline int rt2800pci_read_eeprom_pci(struct rt2x00_dev *rt2x00dev)
  156. {
  157. return -EOPNOTSUPP;
  158. }
  159. static inline int rt2800pci_efuse_detect(struct rt2x00_dev *rt2x00dev)
  160. {
  161. return 0;
  162. }
  163. static inline int rt2800pci_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
  164. {
  165. return -EOPNOTSUPP;
  166. }
  167. #endif /* CONFIG_PCI */
  168. /*
  169. * Queue handlers.
  170. */
  171. static void rt2800pci_start_queue(struct data_queue *queue)
  172. {
  173. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  174. u32 reg;
  175. switch (queue->qid) {
  176. case QID_RX:
  177. rt2x00mmio_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  178. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 1);
  179. rt2x00mmio_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  180. break;
  181. case QID_BEACON:
  182. rt2x00mmio_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  183. rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
  184. rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 1);
  185. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
  186. rt2x00mmio_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  187. rt2x00mmio_register_read(rt2x00dev, INT_TIMER_EN, &reg);
  188. rt2x00_set_field32(&reg, INT_TIMER_EN_PRE_TBTT_TIMER, 1);
  189. rt2x00mmio_register_write(rt2x00dev, INT_TIMER_EN, reg);
  190. break;
  191. default:
  192. break;
  193. }
  194. }
  195. static void rt2800pci_kick_queue(struct data_queue *queue)
  196. {
  197. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  198. struct queue_entry *entry;
  199. switch (queue->qid) {
  200. case QID_AC_VO:
  201. case QID_AC_VI:
  202. case QID_AC_BE:
  203. case QID_AC_BK:
  204. entry = rt2x00queue_get_entry(queue, Q_INDEX);
  205. rt2x00mmio_register_write(rt2x00dev, TX_CTX_IDX(queue->qid),
  206. entry->entry_idx);
  207. break;
  208. case QID_MGMT:
  209. entry = rt2x00queue_get_entry(queue, Q_INDEX);
  210. rt2x00mmio_register_write(rt2x00dev, TX_CTX_IDX(5),
  211. entry->entry_idx);
  212. break;
  213. default:
  214. break;
  215. }
  216. }
  217. static void rt2800pci_stop_queue(struct data_queue *queue)
  218. {
  219. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  220. u32 reg;
  221. switch (queue->qid) {
  222. case QID_RX:
  223. rt2x00mmio_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  224. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
  225. rt2x00mmio_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  226. break;
  227. case QID_BEACON:
  228. rt2x00mmio_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  229. rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
  230. rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
  231. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
  232. rt2x00mmio_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  233. rt2x00mmio_register_read(rt2x00dev, INT_TIMER_EN, &reg);
  234. rt2x00_set_field32(&reg, INT_TIMER_EN_PRE_TBTT_TIMER, 0);
  235. rt2x00mmio_register_write(rt2x00dev, INT_TIMER_EN, reg);
  236. /*
  237. * Wait for current invocation to finish. The tasklet
  238. * won't be scheduled anymore afterwards since we disabled
  239. * the TBTT and PRE TBTT timer.
  240. */
  241. tasklet_kill(&rt2x00dev->tbtt_tasklet);
  242. tasklet_kill(&rt2x00dev->pretbtt_tasklet);
  243. break;
  244. default:
  245. break;
  246. }
  247. }
  248. /*
  249. * Firmware functions
  250. */
  251. static char *rt2800pci_get_firmware_name(struct rt2x00_dev *rt2x00dev)
  252. {
  253. /*
  254. * Chip rt3290 use specific 4KB firmware named rt3290.bin.
  255. */
  256. if (rt2x00_rt(rt2x00dev, RT3290))
  257. return FIRMWARE_RT3290;
  258. else
  259. return FIRMWARE_RT2860;
  260. }
  261. static int rt2800pci_write_firmware(struct rt2x00_dev *rt2x00dev,
  262. const u8 *data, const size_t len)
  263. {
  264. u32 reg;
  265. /*
  266. * enable Host program ram write selection
  267. */
  268. reg = 0;
  269. rt2x00_set_field32(&reg, PBF_SYS_CTRL_HOST_RAM_WRITE, 1);
  270. rt2x00mmio_register_write(rt2x00dev, PBF_SYS_CTRL, reg);
  271. /*
  272. * Write firmware to device.
  273. */
  274. rt2x00mmio_register_multiwrite(rt2x00dev, FIRMWARE_IMAGE_BASE,
  275. data, len);
  276. rt2x00mmio_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000);
  277. rt2x00mmio_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00001);
  278. rt2x00mmio_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
  279. rt2x00mmio_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
  280. return 0;
  281. }
  282. /*
  283. * Initialization functions.
  284. */
  285. static bool rt2800pci_get_entry_state(struct queue_entry *entry)
  286. {
  287. struct queue_entry_priv_mmio *entry_priv = entry->priv_data;
  288. u32 word;
  289. if (entry->queue->qid == QID_RX) {
  290. rt2x00_desc_read(entry_priv->desc, 1, &word);
  291. return (!rt2x00_get_field32(word, RXD_W1_DMA_DONE));
  292. } else {
  293. rt2x00_desc_read(entry_priv->desc, 1, &word);
  294. return (!rt2x00_get_field32(word, TXD_W1_DMA_DONE));
  295. }
  296. }
  297. static void rt2800pci_clear_entry(struct queue_entry *entry)
  298. {
  299. struct queue_entry_priv_mmio *entry_priv = entry->priv_data;
  300. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  301. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  302. u32 word;
  303. if (entry->queue->qid == QID_RX) {
  304. rt2x00_desc_read(entry_priv->desc, 0, &word);
  305. rt2x00_set_field32(&word, RXD_W0_SDP0, skbdesc->skb_dma);
  306. rt2x00_desc_write(entry_priv->desc, 0, word);
  307. rt2x00_desc_read(entry_priv->desc, 1, &word);
  308. rt2x00_set_field32(&word, RXD_W1_DMA_DONE, 0);
  309. rt2x00_desc_write(entry_priv->desc, 1, word);
  310. /*
  311. * Set RX IDX in register to inform hardware that we have
  312. * handled this entry and it is available for reuse again.
  313. */
  314. rt2x00mmio_register_write(rt2x00dev, RX_CRX_IDX,
  315. entry->entry_idx);
  316. } else {
  317. rt2x00_desc_read(entry_priv->desc, 1, &word);
  318. rt2x00_set_field32(&word, TXD_W1_DMA_DONE, 1);
  319. rt2x00_desc_write(entry_priv->desc, 1, word);
  320. }
  321. }
  322. static int rt2800pci_init_queues(struct rt2x00_dev *rt2x00dev)
  323. {
  324. struct queue_entry_priv_mmio *entry_priv;
  325. /*
  326. * Initialize registers.
  327. */
  328. entry_priv = rt2x00dev->tx[0].entries[0].priv_data;
  329. rt2x00mmio_register_write(rt2x00dev, TX_BASE_PTR0,
  330. entry_priv->desc_dma);
  331. rt2x00mmio_register_write(rt2x00dev, TX_MAX_CNT0,
  332. rt2x00dev->tx[0].limit);
  333. rt2x00mmio_register_write(rt2x00dev, TX_CTX_IDX0, 0);
  334. rt2x00mmio_register_write(rt2x00dev, TX_DTX_IDX0, 0);
  335. entry_priv = rt2x00dev->tx[1].entries[0].priv_data;
  336. rt2x00mmio_register_write(rt2x00dev, TX_BASE_PTR1,
  337. entry_priv->desc_dma);
  338. rt2x00mmio_register_write(rt2x00dev, TX_MAX_CNT1,
  339. rt2x00dev->tx[1].limit);
  340. rt2x00mmio_register_write(rt2x00dev, TX_CTX_IDX1, 0);
  341. rt2x00mmio_register_write(rt2x00dev, TX_DTX_IDX1, 0);
  342. entry_priv = rt2x00dev->tx[2].entries[0].priv_data;
  343. rt2x00mmio_register_write(rt2x00dev, TX_BASE_PTR2,
  344. entry_priv->desc_dma);
  345. rt2x00mmio_register_write(rt2x00dev, TX_MAX_CNT2,
  346. rt2x00dev->tx[2].limit);
  347. rt2x00mmio_register_write(rt2x00dev, TX_CTX_IDX2, 0);
  348. rt2x00mmio_register_write(rt2x00dev, TX_DTX_IDX2, 0);
  349. entry_priv = rt2x00dev->tx[3].entries[0].priv_data;
  350. rt2x00mmio_register_write(rt2x00dev, TX_BASE_PTR3,
  351. entry_priv->desc_dma);
  352. rt2x00mmio_register_write(rt2x00dev, TX_MAX_CNT3,
  353. rt2x00dev->tx[3].limit);
  354. rt2x00mmio_register_write(rt2x00dev, TX_CTX_IDX3, 0);
  355. rt2x00mmio_register_write(rt2x00dev, TX_DTX_IDX3, 0);
  356. rt2x00mmio_register_write(rt2x00dev, TX_BASE_PTR4, 0);
  357. rt2x00mmio_register_write(rt2x00dev, TX_MAX_CNT4, 0);
  358. rt2x00mmio_register_write(rt2x00dev, TX_CTX_IDX4, 0);
  359. rt2x00mmio_register_write(rt2x00dev, TX_DTX_IDX4, 0);
  360. rt2x00mmio_register_write(rt2x00dev, TX_BASE_PTR5, 0);
  361. rt2x00mmio_register_write(rt2x00dev, TX_MAX_CNT5, 0);
  362. rt2x00mmio_register_write(rt2x00dev, TX_CTX_IDX5, 0);
  363. rt2x00mmio_register_write(rt2x00dev, TX_DTX_IDX5, 0);
  364. entry_priv = rt2x00dev->rx->entries[0].priv_data;
  365. rt2x00mmio_register_write(rt2x00dev, RX_BASE_PTR,
  366. entry_priv->desc_dma);
  367. rt2x00mmio_register_write(rt2x00dev, RX_MAX_CNT,
  368. rt2x00dev->rx[0].limit);
  369. rt2x00mmio_register_write(rt2x00dev, RX_CRX_IDX,
  370. rt2x00dev->rx[0].limit - 1);
  371. rt2x00mmio_register_write(rt2x00dev, RX_DRX_IDX, 0);
  372. rt2800_disable_wpdma(rt2x00dev);
  373. rt2x00mmio_register_write(rt2x00dev, DELAY_INT_CFG, 0);
  374. return 0;
  375. }
  376. /*
  377. * Device state switch handlers.
  378. */
  379. static void rt2800pci_toggle_irq(struct rt2x00_dev *rt2x00dev,
  380. enum dev_state state)
  381. {
  382. u32 reg;
  383. unsigned long flags;
  384. /*
  385. * When interrupts are being enabled, the interrupt registers
  386. * should clear the register to assure a clean state.
  387. */
  388. if (state == STATE_RADIO_IRQ_ON) {
  389. rt2x00mmio_register_read(rt2x00dev, INT_SOURCE_CSR, &reg);
  390. rt2x00mmio_register_write(rt2x00dev, INT_SOURCE_CSR, reg);
  391. }
  392. spin_lock_irqsave(&rt2x00dev->irqmask_lock, flags);
  393. reg = 0;
  394. if (state == STATE_RADIO_IRQ_ON) {
  395. rt2x00_set_field32(&reg, INT_MASK_CSR_RX_DONE, 1);
  396. rt2x00_set_field32(&reg, INT_MASK_CSR_TBTT, 1);
  397. rt2x00_set_field32(&reg, INT_MASK_CSR_PRE_TBTT, 1);
  398. rt2x00_set_field32(&reg, INT_MASK_CSR_TX_FIFO_STATUS, 1);
  399. rt2x00_set_field32(&reg, INT_MASK_CSR_AUTO_WAKEUP, 1);
  400. }
  401. rt2x00mmio_register_write(rt2x00dev, INT_MASK_CSR, reg);
  402. spin_unlock_irqrestore(&rt2x00dev->irqmask_lock, flags);
  403. if (state == STATE_RADIO_IRQ_OFF) {
  404. /*
  405. * Wait for possibly running tasklets to finish.
  406. */
  407. tasklet_kill(&rt2x00dev->txstatus_tasklet);
  408. tasklet_kill(&rt2x00dev->rxdone_tasklet);
  409. tasklet_kill(&rt2x00dev->autowake_tasklet);
  410. tasklet_kill(&rt2x00dev->tbtt_tasklet);
  411. tasklet_kill(&rt2x00dev->pretbtt_tasklet);
  412. }
  413. }
  414. static int rt2800pci_init_registers(struct rt2x00_dev *rt2x00dev)
  415. {
  416. u32 reg;
  417. /*
  418. * Reset DMA indexes
  419. */
  420. rt2x00mmio_register_read(rt2x00dev, WPDMA_RST_IDX, &reg);
  421. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX0, 1);
  422. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX1, 1);
  423. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX2, 1);
  424. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX3, 1);
  425. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX4, 1);
  426. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX5, 1);
  427. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DRX_IDX0, 1);
  428. rt2x00mmio_register_write(rt2x00dev, WPDMA_RST_IDX, reg);
  429. rt2x00mmio_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000e1f);
  430. rt2x00mmio_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000e00);
  431. if (rt2x00_is_pcie(rt2x00dev) &&
  432. (rt2x00_rt(rt2x00dev, RT3090) ||
  433. rt2x00_rt(rt2x00dev, RT3390) ||
  434. rt2x00_rt(rt2x00dev, RT3572) ||
  435. rt2x00_rt(rt2x00dev, RT3593) ||
  436. rt2x00_rt(rt2x00dev, RT5390) ||
  437. rt2x00_rt(rt2x00dev, RT5392) ||
  438. rt2x00_rt(rt2x00dev, RT5592))) {
  439. rt2x00mmio_register_read(rt2x00dev, AUX_CTRL, &reg);
  440. rt2x00_set_field32(&reg, AUX_CTRL_FORCE_PCIE_CLK, 1);
  441. rt2x00_set_field32(&reg, AUX_CTRL_WAKE_PCIE_EN, 1);
  442. rt2x00mmio_register_write(rt2x00dev, AUX_CTRL, reg);
  443. }
  444. rt2x00mmio_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
  445. reg = 0;
  446. rt2x00_set_field32(&reg, MAC_SYS_CTRL_RESET_CSR, 1);
  447. rt2x00_set_field32(&reg, MAC_SYS_CTRL_RESET_BBP, 1);
  448. rt2x00mmio_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  449. rt2x00mmio_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
  450. return 0;
  451. }
  452. static int rt2800pci_enable_radio(struct rt2x00_dev *rt2x00dev)
  453. {
  454. int retval;
  455. /* Wait for DMA, ignore error until we initialize queues. */
  456. rt2800_wait_wpdma_ready(rt2x00dev);
  457. if (unlikely(rt2800pci_init_queues(rt2x00dev)))
  458. return -EIO;
  459. retval = rt2800_enable_radio(rt2x00dev);
  460. if (retval)
  461. return retval;
  462. /* After resume MCU_BOOT_SIGNAL will trash these. */
  463. rt2x00mmio_register_write(rt2x00dev, H2M_MAILBOX_STATUS, ~0);
  464. rt2x00mmio_register_write(rt2x00dev, H2M_MAILBOX_CID, ~0);
  465. rt2800_mcu_request(rt2x00dev, MCU_SLEEP, TOKEN_RADIO_OFF, 0xff, 0x02);
  466. rt2800pci_mcu_status(rt2x00dev, TOKEN_RADIO_OFF);
  467. rt2800_mcu_request(rt2x00dev, MCU_WAKEUP, TOKEN_WAKEUP, 0, 0);
  468. rt2800pci_mcu_status(rt2x00dev, TOKEN_WAKEUP);
  469. return retval;
  470. }
  471. static void rt2800pci_disable_radio(struct rt2x00_dev *rt2x00dev)
  472. {
  473. if (rt2x00_is_soc(rt2x00dev)) {
  474. rt2800_disable_radio(rt2x00dev);
  475. rt2x00mmio_register_write(rt2x00dev, PWR_PIN_CFG, 0);
  476. rt2x00mmio_register_write(rt2x00dev, TX_PIN_CFG, 0);
  477. }
  478. }
  479. static int rt2800pci_set_state(struct rt2x00_dev *rt2x00dev,
  480. enum dev_state state)
  481. {
  482. if (state == STATE_AWAKE) {
  483. rt2800_mcu_request(rt2x00dev, MCU_WAKEUP, TOKEN_WAKEUP,
  484. 0, 0x02);
  485. rt2800pci_mcu_status(rt2x00dev, TOKEN_WAKEUP);
  486. } else if (state == STATE_SLEEP) {
  487. rt2x00mmio_register_write(rt2x00dev, H2M_MAILBOX_STATUS,
  488. 0xffffffff);
  489. rt2x00mmio_register_write(rt2x00dev, H2M_MAILBOX_CID,
  490. 0xffffffff);
  491. rt2800_mcu_request(rt2x00dev, MCU_SLEEP, TOKEN_SLEEP,
  492. 0xff, 0x01);
  493. }
  494. return 0;
  495. }
  496. static int rt2800pci_set_device_state(struct rt2x00_dev *rt2x00dev,
  497. enum dev_state state)
  498. {
  499. int retval = 0;
  500. switch (state) {
  501. case STATE_RADIO_ON:
  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_IRQ_ON:
  513. case STATE_RADIO_IRQ_OFF:
  514. rt2800pci_toggle_irq(rt2x00dev, state);
  515. break;
  516. case STATE_DEEP_SLEEP:
  517. case STATE_SLEEP:
  518. case STATE_STANDBY:
  519. case STATE_AWAKE:
  520. retval = rt2800pci_set_state(rt2x00dev, state);
  521. break;
  522. default:
  523. retval = -ENOTSUPP;
  524. break;
  525. }
  526. if (unlikely(retval))
  527. rt2x00_err(rt2x00dev, "Device failed to enter state %d (%d)\n",
  528. state, retval);
  529. return retval;
  530. }
  531. /*
  532. * TX descriptor initialization
  533. */
  534. static __le32 *rt2800pci_get_txwi(struct queue_entry *entry)
  535. {
  536. return (__le32 *) entry->skb->data;
  537. }
  538. static void rt2800pci_write_tx_desc(struct queue_entry *entry,
  539. struct txentry_desc *txdesc)
  540. {
  541. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  542. struct queue_entry_priv_mmio *entry_priv = entry->priv_data;
  543. __le32 *txd = entry_priv->desc;
  544. u32 word;
  545. const unsigned int txwi_size = entry->queue->winfo_size;
  546. /*
  547. * The buffers pointed by SD_PTR0/SD_LEN0 and SD_PTR1/SD_LEN1
  548. * must contains a TXWI structure + 802.11 header + padding + 802.11
  549. * data. We choose to have SD_PTR0/SD_LEN0 only contains TXWI and
  550. * SD_PTR1/SD_LEN1 contains 802.11 header + padding + 802.11
  551. * data. It means that LAST_SEC0 is always 0.
  552. */
  553. /*
  554. * Initialize TX descriptor
  555. */
  556. word = 0;
  557. rt2x00_set_field32(&word, TXD_W0_SD_PTR0, skbdesc->skb_dma);
  558. rt2x00_desc_write(txd, 0, word);
  559. word = 0;
  560. rt2x00_set_field32(&word, TXD_W1_SD_LEN1, entry->skb->len);
  561. rt2x00_set_field32(&word, TXD_W1_LAST_SEC1,
  562. !test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
  563. rt2x00_set_field32(&word, TXD_W1_BURST,
  564. test_bit(ENTRY_TXD_BURST, &txdesc->flags));
  565. rt2x00_set_field32(&word, TXD_W1_SD_LEN0, txwi_size);
  566. rt2x00_set_field32(&word, TXD_W1_LAST_SEC0, 0);
  567. rt2x00_set_field32(&word, TXD_W1_DMA_DONE, 0);
  568. rt2x00_desc_write(txd, 1, word);
  569. word = 0;
  570. rt2x00_set_field32(&word, TXD_W2_SD_PTR1,
  571. skbdesc->skb_dma + txwi_size);
  572. rt2x00_desc_write(txd, 2, word);
  573. word = 0;
  574. rt2x00_set_field32(&word, TXD_W3_WIV,
  575. !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags));
  576. rt2x00_set_field32(&word, TXD_W3_QSEL, 2);
  577. rt2x00_desc_write(txd, 3, word);
  578. /*
  579. * Register descriptor details in skb frame descriptor.
  580. */
  581. skbdesc->desc = txd;
  582. skbdesc->desc_len = TXD_DESC_SIZE;
  583. }
  584. /*
  585. * RX control handlers
  586. */
  587. static void rt2800pci_fill_rxdone(struct queue_entry *entry,
  588. struct rxdone_entry_desc *rxdesc)
  589. {
  590. struct queue_entry_priv_mmio *entry_priv = entry->priv_data;
  591. __le32 *rxd = entry_priv->desc;
  592. u32 word;
  593. rt2x00_desc_read(rxd, 3, &word);
  594. if (rt2x00_get_field32(word, RXD_W3_CRC_ERROR))
  595. rxdesc->flags |= RX_FLAG_FAILED_FCS_CRC;
  596. /*
  597. * Unfortunately we don't know the cipher type used during
  598. * decryption. This prevents us from correct providing
  599. * correct statistics through debugfs.
  600. */
  601. rxdesc->cipher_status = rt2x00_get_field32(word, RXD_W3_CIPHER_ERROR);
  602. if (rt2x00_get_field32(word, RXD_W3_DECRYPTED)) {
  603. /*
  604. * Hardware has stripped IV/EIV data from 802.11 frame during
  605. * decryption. Unfortunately the descriptor doesn't contain
  606. * any fields with the EIV/IV data either, so they can't
  607. * be restored by rt2x00lib.
  608. */
  609. rxdesc->flags |= RX_FLAG_IV_STRIPPED;
  610. /*
  611. * The hardware has already checked the Michael Mic and has
  612. * stripped it from the frame. Signal this to mac80211.
  613. */
  614. rxdesc->flags |= RX_FLAG_MMIC_STRIPPED;
  615. if (rxdesc->cipher_status == RX_CRYPTO_SUCCESS)
  616. rxdesc->flags |= RX_FLAG_DECRYPTED;
  617. else if (rxdesc->cipher_status == RX_CRYPTO_FAIL_MIC)
  618. rxdesc->flags |= RX_FLAG_MMIC_ERROR;
  619. }
  620. if (rt2x00_get_field32(word, RXD_W3_MY_BSS))
  621. rxdesc->dev_flags |= RXDONE_MY_BSS;
  622. if (rt2x00_get_field32(word, RXD_W3_L2PAD))
  623. rxdesc->dev_flags |= RXDONE_L2PAD;
  624. /*
  625. * Process the RXWI structure that is at the start of the buffer.
  626. */
  627. rt2800_process_rxwi(entry, rxdesc);
  628. }
  629. /*
  630. * Interrupt functions.
  631. */
  632. static void rt2800pci_wakeup(struct rt2x00_dev *rt2x00dev)
  633. {
  634. struct ieee80211_conf conf = { .flags = 0 };
  635. struct rt2x00lib_conf libconf = { .conf = &conf };
  636. rt2800_config(rt2x00dev, &libconf, IEEE80211_CONF_CHANGE_PS);
  637. }
  638. static bool rt2800pci_txdone_entry_check(struct queue_entry *entry, u32 status)
  639. {
  640. __le32 *txwi;
  641. u32 word;
  642. int wcid, tx_wcid;
  643. wcid = rt2x00_get_field32(status, TX_STA_FIFO_WCID);
  644. txwi = rt2800_drv_get_txwi(entry);
  645. rt2x00_desc_read(txwi, 1, &word);
  646. tx_wcid = rt2x00_get_field32(word, TXWI_W1_WIRELESS_CLI_ID);
  647. return (tx_wcid == wcid);
  648. }
  649. static bool rt2800pci_txdone_find_entry(struct queue_entry *entry, void *data)
  650. {
  651. u32 status = *(u32 *)data;
  652. /*
  653. * rt2800pci hardware might reorder frames when exchanging traffic
  654. * with multiple BA enabled STAs.
  655. *
  656. * For example, a tx queue
  657. * [ STA1 | STA2 | STA1 | STA2 ]
  658. * can result in tx status reports
  659. * [ STA1 | STA1 | STA2 | STA2 ]
  660. * when the hw decides to aggregate the frames for STA1 into one AMPDU.
  661. *
  662. * To mitigate this effect, associate the tx status to the first frame
  663. * in the tx queue with a matching wcid.
  664. */
  665. if (rt2800pci_txdone_entry_check(entry, status) &&
  666. !test_bit(ENTRY_DATA_STATUS_SET, &entry->flags)) {
  667. /*
  668. * Got a matching frame, associate the tx status with
  669. * the frame
  670. */
  671. entry->status = status;
  672. set_bit(ENTRY_DATA_STATUS_SET, &entry->flags);
  673. return true;
  674. }
  675. /* Check the next frame */
  676. return false;
  677. }
  678. static bool rt2800pci_txdone_match_first(struct queue_entry *entry, void *data)
  679. {
  680. u32 status = *(u32 *)data;
  681. /*
  682. * Find the first frame without tx status and assign this status to it
  683. * regardless if it matches or not.
  684. */
  685. if (!test_bit(ENTRY_DATA_STATUS_SET, &entry->flags)) {
  686. /*
  687. * Got a matching frame, associate the tx status with
  688. * the frame
  689. */
  690. entry->status = status;
  691. set_bit(ENTRY_DATA_STATUS_SET, &entry->flags);
  692. return true;
  693. }
  694. /* Check the next frame */
  695. return false;
  696. }
  697. static bool rt2800pci_txdone_release_entries(struct queue_entry *entry,
  698. void *data)
  699. {
  700. if (test_bit(ENTRY_DATA_STATUS_SET, &entry->flags)) {
  701. rt2800_txdone_entry(entry, entry->status,
  702. rt2800pci_get_txwi(entry));
  703. return false;
  704. }
  705. /* No more frames to release */
  706. return true;
  707. }
  708. static bool rt2800pci_txdone(struct rt2x00_dev *rt2x00dev)
  709. {
  710. struct data_queue *queue;
  711. u32 status;
  712. u8 qid;
  713. int max_tx_done = 16;
  714. while (kfifo_get(&rt2x00dev->txstatus_fifo, &status)) {
  715. qid = rt2x00_get_field32(status, TX_STA_FIFO_PID_QUEUE);
  716. if (unlikely(qid >= QID_RX)) {
  717. /*
  718. * Unknown queue, this shouldn't happen. Just drop
  719. * this tx status.
  720. */
  721. rt2x00_warn(rt2x00dev, "Got TX status report with unexpected pid %u, dropping\n",
  722. qid);
  723. break;
  724. }
  725. queue = rt2x00queue_get_tx_queue(rt2x00dev, qid);
  726. if (unlikely(queue == NULL)) {
  727. /*
  728. * The queue is NULL, this shouldn't happen. Stop
  729. * processing here and drop the tx status
  730. */
  731. rt2x00_warn(rt2x00dev, "Got TX status for an unavailable queue %u, dropping\n",
  732. qid);
  733. break;
  734. }
  735. if (unlikely(rt2x00queue_empty(queue))) {
  736. /*
  737. * The queue is empty. Stop processing here
  738. * and drop the tx status.
  739. */
  740. rt2x00_warn(rt2x00dev, "Got TX status for an empty queue %u, dropping\n",
  741. qid);
  742. break;
  743. }
  744. /*
  745. * Let's associate this tx status with the first
  746. * matching frame.
  747. */
  748. if (!rt2x00queue_for_each_entry(queue, Q_INDEX_DONE,
  749. Q_INDEX, &status,
  750. rt2800pci_txdone_find_entry)) {
  751. /*
  752. * We cannot match the tx status to any frame, so just
  753. * use the first one.
  754. */
  755. if (!rt2x00queue_for_each_entry(queue, Q_INDEX_DONE,
  756. Q_INDEX, &status,
  757. rt2800pci_txdone_match_first)) {
  758. rt2x00_warn(rt2x00dev, "No frame found for TX status on queue %u, dropping\n",
  759. qid);
  760. break;
  761. }
  762. }
  763. /*
  764. * Release all frames with a valid tx status.
  765. */
  766. rt2x00queue_for_each_entry(queue, Q_INDEX_DONE,
  767. Q_INDEX, NULL,
  768. rt2800pci_txdone_release_entries);
  769. if (--max_tx_done == 0)
  770. break;
  771. }
  772. return !max_tx_done;
  773. }
  774. static inline void rt2800pci_enable_interrupt(struct rt2x00_dev *rt2x00dev,
  775. struct rt2x00_field32 irq_field)
  776. {
  777. u32 reg;
  778. /*
  779. * Enable a single interrupt. The interrupt mask register
  780. * access needs locking.
  781. */
  782. spin_lock_irq(&rt2x00dev->irqmask_lock);
  783. rt2x00mmio_register_read(rt2x00dev, INT_MASK_CSR, &reg);
  784. rt2x00_set_field32(&reg, irq_field, 1);
  785. rt2x00mmio_register_write(rt2x00dev, INT_MASK_CSR, reg);
  786. spin_unlock_irq(&rt2x00dev->irqmask_lock);
  787. }
  788. static void rt2800pci_txstatus_tasklet(unsigned long data)
  789. {
  790. struct rt2x00_dev *rt2x00dev = (struct rt2x00_dev *)data;
  791. if (rt2800pci_txdone(rt2x00dev))
  792. tasklet_schedule(&rt2x00dev->txstatus_tasklet);
  793. /*
  794. * No need to enable the tx status interrupt here as we always
  795. * leave it enabled to minimize the possibility of a tx status
  796. * register overflow. See comment in interrupt handler.
  797. */
  798. }
  799. static void rt2800pci_pretbtt_tasklet(unsigned long data)
  800. {
  801. struct rt2x00_dev *rt2x00dev = (struct rt2x00_dev *)data;
  802. rt2x00lib_pretbtt(rt2x00dev);
  803. if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  804. rt2800pci_enable_interrupt(rt2x00dev, INT_MASK_CSR_PRE_TBTT);
  805. }
  806. static void rt2800pci_tbtt_tasklet(unsigned long data)
  807. {
  808. struct rt2x00_dev *rt2x00dev = (struct rt2x00_dev *)data;
  809. struct rt2800_drv_data *drv_data = rt2x00dev->drv_data;
  810. u32 reg;
  811. rt2x00lib_beacondone(rt2x00dev);
  812. if (rt2x00dev->intf_ap_count) {
  813. /*
  814. * The rt2800pci hardware tbtt timer is off by 1us per tbtt
  815. * causing beacon skew and as a result causing problems with
  816. * some powersaving clients over time. Shorten the beacon
  817. * interval every 64 beacons by 64us to mitigate this effect.
  818. */
  819. if (drv_data->tbtt_tick == (BCN_TBTT_OFFSET - 2)) {
  820. rt2x00mmio_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  821. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
  822. (rt2x00dev->beacon_int * 16) - 1);
  823. rt2x00mmio_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  824. } else if (drv_data->tbtt_tick == (BCN_TBTT_OFFSET - 1)) {
  825. rt2x00mmio_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  826. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
  827. (rt2x00dev->beacon_int * 16));
  828. rt2x00mmio_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  829. }
  830. drv_data->tbtt_tick++;
  831. drv_data->tbtt_tick %= BCN_TBTT_OFFSET;
  832. }
  833. if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  834. rt2800pci_enable_interrupt(rt2x00dev, INT_MASK_CSR_TBTT);
  835. }
  836. static void rt2800pci_rxdone_tasklet(unsigned long data)
  837. {
  838. struct rt2x00_dev *rt2x00dev = (struct rt2x00_dev *)data;
  839. if (rt2x00mmio_rxdone(rt2x00dev))
  840. tasklet_schedule(&rt2x00dev->rxdone_tasklet);
  841. else if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  842. rt2800pci_enable_interrupt(rt2x00dev, INT_MASK_CSR_RX_DONE);
  843. }
  844. static void rt2800pci_autowake_tasklet(unsigned long data)
  845. {
  846. struct rt2x00_dev *rt2x00dev = (struct rt2x00_dev *)data;
  847. rt2800pci_wakeup(rt2x00dev);
  848. if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  849. rt2800pci_enable_interrupt(rt2x00dev, INT_MASK_CSR_AUTO_WAKEUP);
  850. }
  851. static void rt2800pci_txstatus_interrupt(struct rt2x00_dev *rt2x00dev)
  852. {
  853. u32 status;
  854. int i;
  855. /*
  856. * The TX_FIFO_STATUS interrupt needs special care. We should
  857. * read TX_STA_FIFO but we should do it immediately as otherwise
  858. * the register can overflow and we would lose status reports.
  859. *
  860. * Hence, read the TX_STA_FIFO register and copy all tx status
  861. * reports into a kernel FIFO which is handled in the txstatus
  862. * tasklet. We use a tasklet to process the tx status reports
  863. * because we can schedule the tasklet multiple times (when the
  864. * interrupt fires again during tx status processing).
  865. *
  866. * Furthermore we don't disable the TX_FIFO_STATUS
  867. * interrupt here but leave it enabled so that the TX_STA_FIFO
  868. * can also be read while the tx status tasklet gets executed.
  869. *
  870. * Since we have only one producer and one consumer we don't
  871. * need to lock the kfifo.
  872. */
  873. for (i = 0; i < rt2x00dev->tx->limit; i++) {
  874. rt2x00mmio_register_read(rt2x00dev, TX_STA_FIFO, &status);
  875. if (!rt2x00_get_field32(status, TX_STA_FIFO_VALID))
  876. break;
  877. if (!kfifo_put(&rt2x00dev->txstatus_fifo, &status)) {
  878. rt2x00_warn(rt2x00dev, "TX status FIFO overrun, drop tx status report\n");
  879. break;
  880. }
  881. }
  882. /* Schedule the tasklet for processing the tx status. */
  883. tasklet_schedule(&rt2x00dev->txstatus_tasklet);
  884. }
  885. static irqreturn_t rt2800pci_interrupt(int irq, void *dev_instance)
  886. {
  887. struct rt2x00_dev *rt2x00dev = dev_instance;
  888. u32 reg, mask;
  889. /* Read status and ACK all interrupts */
  890. rt2x00mmio_register_read(rt2x00dev, INT_SOURCE_CSR, &reg);
  891. rt2x00mmio_register_write(rt2x00dev, INT_SOURCE_CSR, reg);
  892. if (!reg)
  893. return IRQ_NONE;
  894. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  895. return IRQ_HANDLED;
  896. /*
  897. * Since INT_MASK_CSR and INT_SOURCE_CSR use the same bits
  898. * for interrupts and interrupt masks we can just use the value of
  899. * INT_SOURCE_CSR to create the interrupt mask.
  900. */
  901. mask = ~reg;
  902. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_TX_FIFO_STATUS)) {
  903. rt2800pci_txstatus_interrupt(rt2x00dev);
  904. /*
  905. * Never disable the TX_FIFO_STATUS interrupt.
  906. */
  907. rt2x00_set_field32(&mask, INT_MASK_CSR_TX_FIFO_STATUS, 1);
  908. }
  909. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_PRE_TBTT))
  910. tasklet_hi_schedule(&rt2x00dev->pretbtt_tasklet);
  911. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_TBTT))
  912. tasklet_hi_schedule(&rt2x00dev->tbtt_tasklet);
  913. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_RX_DONE))
  914. tasklet_schedule(&rt2x00dev->rxdone_tasklet);
  915. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_AUTO_WAKEUP))
  916. tasklet_schedule(&rt2x00dev->autowake_tasklet);
  917. /*
  918. * Disable all interrupts for which a tasklet was scheduled right now,
  919. * the tasklet will reenable the appropriate interrupts.
  920. */
  921. spin_lock(&rt2x00dev->irqmask_lock);
  922. rt2x00mmio_register_read(rt2x00dev, INT_MASK_CSR, &reg);
  923. reg &= mask;
  924. rt2x00mmio_register_write(rt2x00dev, INT_MASK_CSR, reg);
  925. spin_unlock(&rt2x00dev->irqmask_lock);
  926. return IRQ_HANDLED;
  927. }
  928. /*
  929. * Device probe functions.
  930. */
  931. static int rt2800pci_read_eeprom(struct rt2x00_dev *rt2x00dev)
  932. {
  933. int retval;
  934. if (rt2x00_is_soc(rt2x00dev))
  935. retval = rt2800pci_read_eeprom_soc(rt2x00dev);
  936. else if (rt2800pci_efuse_detect(rt2x00dev))
  937. retval = rt2800pci_read_eeprom_efuse(rt2x00dev);
  938. else
  939. retval = rt2800pci_read_eeprom_pci(rt2x00dev);
  940. return retval;
  941. }
  942. static const struct ieee80211_ops rt2800pci_mac80211_ops = {
  943. .tx = rt2x00mac_tx,
  944. .start = rt2x00mac_start,
  945. .stop = rt2x00mac_stop,
  946. .add_interface = rt2x00mac_add_interface,
  947. .remove_interface = rt2x00mac_remove_interface,
  948. .config = rt2x00mac_config,
  949. .configure_filter = rt2x00mac_configure_filter,
  950. .set_key = rt2x00mac_set_key,
  951. .sw_scan_start = rt2x00mac_sw_scan_start,
  952. .sw_scan_complete = rt2x00mac_sw_scan_complete,
  953. .get_stats = rt2x00mac_get_stats,
  954. .get_tkip_seq = rt2800_get_tkip_seq,
  955. .set_rts_threshold = rt2800_set_rts_threshold,
  956. .sta_add = rt2x00mac_sta_add,
  957. .sta_remove = rt2x00mac_sta_remove,
  958. .bss_info_changed = rt2x00mac_bss_info_changed,
  959. .conf_tx = rt2800_conf_tx,
  960. .get_tsf = rt2800_get_tsf,
  961. .rfkill_poll = rt2x00mac_rfkill_poll,
  962. .ampdu_action = rt2800_ampdu_action,
  963. .flush = rt2x00mac_flush,
  964. .get_survey = rt2800_get_survey,
  965. .get_ringparam = rt2x00mac_get_ringparam,
  966. .tx_frames_pending = rt2x00mac_tx_frames_pending,
  967. };
  968. static const struct rt2800_ops rt2800pci_rt2800_ops = {
  969. .register_read = rt2x00mmio_register_read,
  970. .register_read_lock = rt2x00mmio_register_read, /* same for PCI */
  971. .register_write = rt2x00mmio_register_write,
  972. .register_write_lock = rt2x00mmio_register_write, /* same for PCI */
  973. .register_multiread = rt2x00mmio_register_multiread,
  974. .register_multiwrite = rt2x00mmio_register_multiwrite,
  975. .regbusy_read = rt2x00mmio_regbusy_read,
  976. .read_eeprom = rt2800pci_read_eeprom,
  977. .hwcrypt_disabled = rt2800pci_hwcrypt_disabled,
  978. .drv_write_firmware = rt2800pci_write_firmware,
  979. .drv_init_registers = rt2800pci_init_registers,
  980. .drv_get_txwi = rt2800pci_get_txwi,
  981. };
  982. static const struct rt2x00lib_ops rt2800pci_rt2x00_ops = {
  983. .irq_handler = rt2800pci_interrupt,
  984. .txstatus_tasklet = rt2800pci_txstatus_tasklet,
  985. .pretbtt_tasklet = rt2800pci_pretbtt_tasklet,
  986. .tbtt_tasklet = rt2800pci_tbtt_tasklet,
  987. .rxdone_tasklet = rt2800pci_rxdone_tasklet,
  988. .autowake_tasklet = rt2800pci_autowake_tasklet,
  989. .probe_hw = rt2800_probe_hw,
  990. .get_firmware_name = rt2800pci_get_firmware_name,
  991. .check_firmware = rt2800_check_firmware,
  992. .load_firmware = rt2800_load_firmware,
  993. .initialize = rt2x00mmio_initialize,
  994. .uninitialize = rt2x00mmio_uninitialize,
  995. .get_entry_state = rt2800pci_get_entry_state,
  996. .clear_entry = rt2800pci_clear_entry,
  997. .set_device_state = rt2800pci_set_device_state,
  998. .rfkill_poll = rt2800_rfkill_poll,
  999. .link_stats = rt2800_link_stats,
  1000. .reset_tuner = rt2800_reset_tuner,
  1001. .link_tuner = rt2800_link_tuner,
  1002. .gain_calibration = rt2800_gain_calibration,
  1003. .vco_calibration = rt2800_vco_calibration,
  1004. .start_queue = rt2800pci_start_queue,
  1005. .kick_queue = rt2800pci_kick_queue,
  1006. .stop_queue = rt2800pci_stop_queue,
  1007. .flush_queue = rt2x00mmio_flush_queue,
  1008. .write_tx_desc = rt2800pci_write_tx_desc,
  1009. .write_tx_data = rt2800_write_tx_data,
  1010. .write_beacon = rt2800_write_beacon,
  1011. .clear_beacon = rt2800_clear_beacon,
  1012. .fill_rxdone = rt2800pci_fill_rxdone,
  1013. .config_shared_key = rt2800_config_shared_key,
  1014. .config_pairwise_key = rt2800_config_pairwise_key,
  1015. .config_filter = rt2800_config_filter,
  1016. .config_intf = rt2800_config_intf,
  1017. .config_erp = rt2800_config_erp,
  1018. .config_ant = rt2800_config_ant,
  1019. .config = rt2800_config,
  1020. .sta_add = rt2800_sta_add,
  1021. .sta_remove = rt2800_sta_remove,
  1022. };
  1023. static void rt2800pci_queue_init(struct data_queue *queue)
  1024. {
  1025. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  1026. unsigned short txwi_size, rxwi_size;
  1027. rt2800_get_txwi_rxwi_size(rt2x00dev, &txwi_size, &rxwi_size);
  1028. switch (queue->qid) {
  1029. case QID_RX:
  1030. queue->limit = 128;
  1031. queue->data_size = AGGREGATION_SIZE;
  1032. queue->desc_size = RXD_DESC_SIZE;
  1033. queue->winfo_size = rxwi_size;
  1034. queue->priv_size = sizeof(struct queue_entry_priv_mmio);
  1035. break;
  1036. case QID_AC_VO:
  1037. case QID_AC_VI:
  1038. case QID_AC_BE:
  1039. case QID_AC_BK:
  1040. queue->limit = 64;
  1041. queue->data_size = AGGREGATION_SIZE;
  1042. queue->desc_size = TXD_DESC_SIZE;
  1043. queue->winfo_size = txwi_size;
  1044. queue->priv_size = sizeof(struct queue_entry_priv_mmio);
  1045. break;
  1046. case QID_BEACON:
  1047. queue->limit = 8;
  1048. queue->data_size = 0; /* No DMA required for beacons */
  1049. queue->desc_size = TXD_DESC_SIZE;
  1050. queue->winfo_size = txwi_size;
  1051. queue->priv_size = sizeof(struct queue_entry_priv_mmio);
  1052. break;
  1053. case QID_ATIM:
  1054. /* fallthrough */
  1055. default:
  1056. BUG();
  1057. break;
  1058. }
  1059. }
  1060. static const struct rt2x00_ops rt2800pci_ops = {
  1061. .name = KBUILD_MODNAME,
  1062. .drv_data_size = sizeof(struct rt2800_drv_data),
  1063. .max_ap_intf = 8,
  1064. .eeprom_size = EEPROM_SIZE,
  1065. .rf_size = RF_SIZE,
  1066. .tx_queues = NUM_TX_QUEUES,
  1067. .queue_init = rt2800pci_queue_init,
  1068. .lib = &rt2800pci_rt2x00_ops,
  1069. .drv = &rt2800pci_rt2800_ops,
  1070. .hw = &rt2800pci_mac80211_ops,
  1071. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  1072. .debugfs = &rt2800_rt2x00debug,
  1073. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  1074. };
  1075. /*
  1076. * RT2800pci module information.
  1077. */
  1078. #ifdef CONFIG_PCI
  1079. static DEFINE_PCI_DEVICE_TABLE(rt2800pci_device_table) = {
  1080. { PCI_DEVICE(0x1814, 0x0601) },
  1081. { PCI_DEVICE(0x1814, 0x0681) },
  1082. { PCI_DEVICE(0x1814, 0x0701) },
  1083. { PCI_DEVICE(0x1814, 0x0781) },
  1084. { PCI_DEVICE(0x1814, 0x3090) },
  1085. { PCI_DEVICE(0x1814, 0x3091) },
  1086. { PCI_DEVICE(0x1814, 0x3092) },
  1087. { PCI_DEVICE(0x1432, 0x7708) },
  1088. { PCI_DEVICE(0x1432, 0x7727) },
  1089. { PCI_DEVICE(0x1432, 0x7728) },
  1090. { PCI_DEVICE(0x1432, 0x7738) },
  1091. { PCI_DEVICE(0x1432, 0x7748) },
  1092. { PCI_DEVICE(0x1432, 0x7758) },
  1093. { PCI_DEVICE(0x1432, 0x7768) },
  1094. { PCI_DEVICE(0x1462, 0x891a) },
  1095. { PCI_DEVICE(0x1a3b, 0x1059) },
  1096. #ifdef CONFIG_RT2800PCI_RT3290
  1097. { PCI_DEVICE(0x1814, 0x3290) },
  1098. #endif
  1099. #ifdef CONFIG_RT2800PCI_RT33XX
  1100. { PCI_DEVICE(0x1814, 0x3390) },
  1101. #endif
  1102. #ifdef CONFIG_RT2800PCI_RT35XX
  1103. { PCI_DEVICE(0x1432, 0x7711) },
  1104. { PCI_DEVICE(0x1432, 0x7722) },
  1105. { PCI_DEVICE(0x1814, 0x3060) },
  1106. { PCI_DEVICE(0x1814, 0x3062) },
  1107. { PCI_DEVICE(0x1814, 0x3562) },
  1108. { PCI_DEVICE(0x1814, 0x3592) },
  1109. { PCI_DEVICE(0x1814, 0x3593) },
  1110. { PCI_DEVICE(0x1814, 0x359f) },
  1111. #endif
  1112. #ifdef CONFIG_RT2800PCI_RT53XX
  1113. { PCI_DEVICE(0x1814, 0x5360) },
  1114. { PCI_DEVICE(0x1814, 0x5362) },
  1115. { PCI_DEVICE(0x1814, 0x5390) },
  1116. { PCI_DEVICE(0x1814, 0x5392) },
  1117. { PCI_DEVICE(0x1814, 0x539a) },
  1118. { PCI_DEVICE(0x1814, 0x539b) },
  1119. { PCI_DEVICE(0x1814, 0x539f) },
  1120. #endif
  1121. { 0, }
  1122. };
  1123. #endif /* CONFIG_PCI */
  1124. MODULE_AUTHOR(DRV_PROJECT);
  1125. MODULE_VERSION(DRV_VERSION);
  1126. MODULE_DESCRIPTION("Ralink RT2800 PCI & PCMCIA Wireless LAN driver.");
  1127. MODULE_SUPPORTED_DEVICE("Ralink RT2860 PCI & PCMCIA chipset based cards");
  1128. #ifdef CONFIG_PCI
  1129. MODULE_FIRMWARE(FIRMWARE_RT2860);
  1130. MODULE_DEVICE_TABLE(pci, rt2800pci_device_table);
  1131. #endif /* CONFIG_PCI */
  1132. MODULE_LICENSE("GPL");
  1133. #if defined(CONFIG_SOC_RT288X) || defined(CONFIG_SOC_RT305X)
  1134. static int rt2800soc_probe(struct platform_device *pdev)
  1135. {
  1136. return rt2x00soc_probe(pdev, &rt2800pci_ops);
  1137. }
  1138. static struct platform_driver rt2800soc_driver = {
  1139. .driver = {
  1140. .name = "rt2800_wmac",
  1141. .owner = THIS_MODULE,
  1142. .mod_name = KBUILD_MODNAME,
  1143. },
  1144. .probe = rt2800soc_probe,
  1145. .remove = rt2x00soc_remove,
  1146. .suspend = rt2x00soc_suspend,
  1147. .resume = rt2x00soc_resume,
  1148. };
  1149. #endif /* CONFIG_SOC_RT288X || CONFIG_SOC_RT305X */
  1150. #ifdef CONFIG_PCI
  1151. static int rt2800pci_probe(struct pci_dev *pci_dev,
  1152. const struct pci_device_id *id)
  1153. {
  1154. return rt2x00pci_probe(pci_dev, &rt2800pci_ops);
  1155. }
  1156. static struct pci_driver rt2800pci_driver = {
  1157. .name = KBUILD_MODNAME,
  1158. .id_table = rt2800pci_device_table,
  1159. .probe = rt2800pci_probe,
  1160. .remove = rt2x00pci_remove,
  1161. .suspend = rt2x00pci_suspend,
  1162. .resume = rt2x00pci_resume,
  1163. };
  1164. #endif /* CONFIG_PCI */
  1165. static int __init rt2800pci_init(void)
  1166. {
  1167. int ret = 0;
  1168. #if defined(CONFIG_SOC_RT288X) || defined(CONFIG_SOC_RT305X)
  1169. ret = platform_driver_register(&rt2800soc_driver);
  1170. if (ret)
  1171. return ret;
  1172. #endif
  1173. #ifdef CONFIG_PCI
  1174. ret = pci_register_driver(&rt2800pci_driver);
  1175. if (ret) {
  1176. #if defined(CONFIG_SOC_RT288X) || defined(CONFIG_SOC_RT305X)
  1177. platform_driver_unregister(&rt2800soc_driver);
  1178. #endif
  1179. return ret;
  1180. }
  1181. #endif
  1182. return ret;
  1183. }
  1184. static void __exit rt2800pci_exit(void)
  1185. {
  1186. #ifdef CONFIG_PCI
  1187. pci_unregister_driver(&rt2800pci_driver);
  1188. #endif
  1189. #if defined(CONFIG_SOC_RT288X) || defined(CONFIG_SOC_RT305X)
  1190. platform_driver_unregister(&rt2800soc_driver);
  1191. #endif
  1192. }
  1193. module_init(rt2800pci_init);
  1194. module_exit(rt2800pci_exit);