dma.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /*
  2. * Copyright (c) 2004-2008 Reyk Floeter <reyk@openbsd.org>
  3. * Copyright (c) 2006-2008 Nick Kossifidis <mickflemm@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. *
  17. */
  18. /*************************************\
  19. * DMA and interrupt masking functions *
  20. \*************************************/
  21. /**
  22. * DOC: DMA and interrupt masking functions
  23. *
  24. * Here we setup descriptor pointers (rxdp/txdp) start/stop dma engine and
  25. * handle queue setup for 5210 chipset (rest are handled on qcu.c).
  26. * Also we setup interrupt mask register (IMR) and read the various interrupt
  27. * status registers (ISR).
  28. */
  29. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include "ath5k.h"
  31. #include "reg.h"
  32. #include "debug.h"
  33. /*********\
  34. * Receive *
  35. \*********/
  36. /**
  37. * ath5k_hw_start_rx_dma() - Start DMA receive
  38. * @ah: The &struct ath5k_hw
  39. */
  40. void
  41. ath5k_hw_start_rx_dma(struct ath5k_hw *ah)
  42. {
  43. ath5k_hw_reg_write(ah, AR5K_CR_RXE, AR5K_CR);
  44. ath5k_hw_reg_read(ah, AR5K_CR);
  45. }
  46. /**
  47. * ath5k_hw_stop_rx_dma() - Stop DMA receive
  48. * @ah: The &struct ath5k_hw
  49. */
  50. static int
  51. ath5k_hw_stop_rx_dma(struct ath5k_hw *ah)
  52. {
  53. unsigned int i;
  54. ath5k_hw_reg_write(ah, AR5K_CR_RXD, AR5K_CR);
  55. /*
  56. * It may take some time to disable the DMA receive unit
  57. */
  58. for (i = 1000; i > 0 &&
  59. (ath5k_hw_reg_read(ah, AR5K_CR) & AR5K_CR_RXE) != 0;
  60. i--)
  61. udelay(100);
  62. if (!i)
  63. ATH5K_DBG(ah, ATH5K_DEBUG_DMA,
  64. "failed to stop RX DMA !\n");
  65. return i ? 0 : -EBUSY;
  66. }
  67. /**
  68. * ath5k_hw_get_rxdp() - Get RX Descriptor's address
  69. * @ah: The &struct ath5k_hw
  70. */
  71. u32
  72. ath5k_hw_get_rxdp(struct ath5k_hw *ah)
  73. {
  74. return ath5k_hw_reg_read(ah, AR5K_RXDP);
  75. }
  76. /**
  77. * ath5k_hw_set_rxdp() - Set RX Descriptor's address
  78. * @ah: The &struct ath5k_hw
  79. * @phys_addr: RX descriptor address
  80. *
  81. * Returns -EIO if rx is active
  82. */
  83. int
  84. ath5k_hw_set_rxdp(struct ath5k_hw *ah, u32 phys_addr)
  85. {
  86. if (ath5k_hw_reg_read(ah, AR5K_CR) & AR5K_CR_RXE) {
  87. ATH5K_DBG(ah, ATH5K_DEBUG_DMA,
  88. "tried to set RXDP while rx was active !\n");
  89. return -EIO;
  90. }
  91. ath5k_hw_reg_write(ah, phys_addr, AR5K_RXDP);
  92. return 0;
  93. }
  94. /**********\
  95. * Transmit *
  96. \**********/
  97. /**
  98. * ath5k_hw_start_tx_dma() - Start DMA transmit for a specific queue
  99. * @ah: The &struct ath5k_hw
  100. * @queue: The hw queue number
  101. *
  102. * Start DMA transmit for a specific queue and since 5210 doesn't have
  103. * QCU/DCU, set up queue parameters for 5210 here based on queue type (one
  104. * queue for normal data and one queue for beacons). For queue setup
  105. * on newer chips check out qcu.c. Returns -EINVAL if queue number is out
  106. * of range or if queue is already disabled.
  107. *
  108. * NOTE: Must be called after setting up tx control descriptor for that
  109. * queue (see below).
  110. */
  111. int
  112. ath5k_hw_start_tx_dma(struct ath5k_hw *ah, unsigned int queue)
  113. {
  114. u32 tx_queue;
  115. AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
  116. /* Return if queue is declared inactive */
  117. if (ah->ah_txq[queue].tqi_type == AR5K_TX_QUEUE_INACTIVE)
  118. return -EINVAL;
  119. if (ah->ah_version == AR5K_AR5210) {
  120. tx_queue = ath5k_hw_reg_read(ah, AR5K_CR);
  121. /*
  122. * Set the queue by type on 5210
  123. */
  124. switch (ah->ah_txq[queue].tqi_type) {
  125. case AR5K_TX_QUEUE_DATA:
  126. tx_queue |= AR5K_CR_TXE0 & ~AR5K_CR_TXD0;
  127. break;
  128. case AR5K_TX_QUEUE_BEACON:
  129. tx_queue |= AR5K_CR_TXE1 & ~AR5K_CR_TXD1;
  130. ath5k_hw_reg_write(ah, AR5K_BCR_TQ1V | AR5K_BCR_BDMAE,
  131. AR5K_BSR);
  132. break;
  133. case AR5K_TX_QUEUE_CAB:
  134. tx_queue |= AR5K_CR_TXE1 & ~AR5K_CR_TXD1;
  135. ath5k_hw_reg_write(ah, AR5K_BCR_TQ1FV | AR5K_BCR_TQ1V |
  136. AR5K_BCR_BDMAE, AR5K_BSR);
  137. break;
  138. default:
  139. return -EINVAL;
  140. }
  141. /* Start queue */
  142. ath5k_hw_reg_write(ah, tx_queue, AR5K_CR);
  143. ath5k_hw_reg_read(ah, AR5K_CR);
  144. } else {
  145. /* Return if queue is disabled */
  146. if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXD, queue))
  147. return -EIO;
  148. /* Start queue */
  149. AR5K_REG_WRITE_Q(ah, AR5K_QCU_TXE, queue);
  150. }
  151. return 0;
  152. }
  153. /**
  154. * ath5k_hw_stop_tx_dma() - Stop DMA transmit on a specific queue
  155. * @ah: The &struct ath5k_hw
  156. * @queue: The hw queue number
  157. *
  158. * Stop DMA transmit on a specific hw queue and drain queue so we don't
  159. * have any pending frames. Returns -EBUSY if we still have pending frames,
  160. * -EINVAL if queue number is out of range or inactive.
  161. */
  162. static int
  163. ath5k_hw_stop_tx_dma(struct ath5k_hw *ah, unsigned int queue)
  164. {
  165. unsigned int i = 40;
  166. u32 tx_queue, pending;
  167. AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
  168. /* Return if queue is declared inactive */
  169. if (ah->ah_txq[queue].tqi_type == AR5K_TX_QUEUE_INACTIVE)
  170. return -EINVAL;
  171. if (ah->ah_version == AR5K_AR5210) {
  172. tx_queue = ath5k_hw_reg_read(ah, AR5K_CR);
  173. /*
  174. * Set by queue type
  175. */
  176. switch (ah->ah_txq[queue].tqi_type) {
  177. case AR5K_TX_QUEUE_DATA:
  178. tx_queue |= AR5K_CR_TXD0 & ~AR5K_CR_TXE0;
  179. break;
  180. case AR5K_TX_QUEUE_BEACON:
  181. case AR5K_TX_QUEUE_CAB:
  182. /* XXX Fix me... */
  183. tx_queue |= AR5K_CR_TXD1 & ~AR5K_CR_TXD1;
  184. ath5k_hw_reg_write(ah, 0, AR5K_BSR);
  185. break;
  186. default:
  187. return -EINVAL;
  188. }
  189. /* Stop queue */
  190. ath5k_hw_reg_write(ah, tx_queue, AR5K_CR);
  191. ath5k_hw_reg_read(ah, AR5K_CR);
  192. } else {
  193. /*
  194. * Enable DCU early termination to quickly
  195. * flush any pending frames from QCU
  196. */
  197. AR5K_REG_ENABLE_BITS(ah, AR5K_QUEUE_MISC(queue),
  198. AR5K_QCU_MISC_DCU_EARLY);
  199. /*
  200. * Schedule TX disable and wait until queue is empty
  201. */
  202. AR5K_REG_WRITE_Q(ah, AR5K_QCU_TXD, queue);
  203. /* Wait for queue to stop */
  204. for (i = 1000; i > 0 &&
  205. (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, queue) != 0);
  206. i--)
  207. udelay(100);
  208. if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, queue))
  209. ATH5K_DBG(ah, ATH5K_DEBUG_DMA,
  210. "queue %i didn't stop !\n", queue);
  211. /* Check for pending frames */
  212. i = 1000;
  213. do {
  214. pending = ath5k_hw_reg_read(ah,
  215. AR5K_QUEUE_STATUS(queue)) &
  216. AR5K_QCU_STS_FRMPENDCNT;
  217. udelay(100);
  218. } while (--i && pending);
  219. /* For 2413+ order PCU to drop packets using
  220. * QUIET mechanism */
  221. if (ah->ah_mac_version >= (AR5K_SREV_AR2414 >> 4) &&
  222. pending) {
  223. /* Set periodicity and duration */
  224. ath5k_hw_reg_write(ah,
  225. AR5K_REG_SM(100, AR5K_QUIET_CTL2_QT_PER)|
  226. AR5K_REG_SM(10, AR5K_QUIET_CTL2_QT_DUR),
  227. AR5K_QUIET_CTL2);
  228. /* Enable quiet period for current TSF */
  229. ath5k_hw_reg_write(ah,
  230. AR5K_QUIET_CTL1_QT_EN |
  231. AR5K_REG_SM(ath5k_hw_reg_read(ah,
  232. AR5K_TSF_L32_5211) >> 10,
  233. AR5K_QUIET_CTL1_NEXT_QT_TSF),
  234. AR5K_QUIET_CTL1);
  235. /* Force channel idle high */
  236. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5211,
  237. AR5K_DIAG_SW_CHANNEL_IDLE_HIGH);
  238. /* Wait a while and disable mechanism */
  239. udelay(400);
  240. AR5K_REG_DISABLE_BITS(ah, AR5K_QUIET_CTL1,
  241. AR5K_QUIET_CTL1_QT_EN);
  242. /* Re-check for pending frames */
  243. i = 100;
  244. do {
  245. pending = ath5k_hw_reg_read(ah,
  246. AR5K_QUEUE_STATUS(queue)) &
  247. AR5K_QCU_STS_FRMPENDCNT;
  248. udelay(100);
  249. } while (--i && pending);
  250. AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW_5211,
  251. AR5K_DIAG_SW_CHANNEL_IDLE_HIGH);
  252. if (pending)
  253. ATH5K_DBG(ah, ATH5K_DEBUG_DMA,
  254. "quiet mechanism didn't work q:%i !\n",
  255. queue);
  256. }
  257. /*
  258. * Disable DCU early termination
  259. */
  260. AR5K_REG_DISABLE_BITS(ah, AR5K_QUEUE_MISC(queue),
  261. AR5K_QCU_MISC_DCU_EARLY);
  262. /* Clear register */
  263. ath5k_hw_reg_write(ah, 0, AR5K_QCU_TXD);
  264. if (pending) {
  265. ATH5K_DBG(ah, ATH5K_DEBUG_DMA,
  266. "tx dma didn't stop (q:%i, frm:%i) !\n",
  267. queue, pending);
  268. return -EBUSY;
  269. }
  270. }
  271. /* TODO: Check for success on 5210 else return error */
  272. return 0;
  273. }
  274. /**
  275. * ath5k_hw_stop_beacon_queue() - Stop beacon queue
  276. * @ah: The &struct ath5k_hw
  277. * @queue: The queue number
  278. *
  279. * Returns -EIO if queue didn't stop
  280. */
  281. int
  282. ath5k_hw_stop_beacon_queue(struct ath5k_hw *ah, unsigned int queue)
  283. {
  284. int ret;
  285. ret = ath5k_hw_stop_tx_dma(ah, queue);
  286. if (ret) {
  287. ATH5K_DBG(ah, ATH5K_DEBUG_DMA,
  288. "beacon queue didn't stop !\n");
  289. return -EIO;
  290. }
  291. return 0;
  292. }
  293. /**
  294. * ath5k_hw_get_txdp() - Get TX Descriptor's address for a specific queue
  295. * @ah: The &struct ath5k_hw
  296. * @queue: The hw queue number
  297. *
  298. * Get TX descriptor's address for a specific queue. For 5210 we ignore
  299. * the queue number and use tx queue type since we only have 2 queues.
  300. * We use TXDP0 for normal data queue and TXDP1 for beacon queue.
  301. * For newer chips with QCU/DCU we just read the corresponding TXDP register.
  302. *
  303. * XXX: Is TXDP read and clear ?
  304. */
  305. u32
  306. ath5k_hw_get_txdp(struct ath5k_hw *ah, unsigned int queue)
  307. {
  308. u16 tx_reg;
  309. AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
  310. /*
  311. * Get the transmit queue descriptor pointer from the selected queue
  312. */
  313. /*5210 doesn't have QCU*/
  314. if (ah->ah_version == AR5K_AR5210) {
  315. switch (ah->ah_txq[queue].tqi_type) {
  316. case AR5K_TX_QUEUE_DATA:
  317. tx_reg = AR5K_NOQCU_TXDP0;
  318. break;
  319. case AR5K_TX_QUEUE_BEACON:
  320. case AR5K_TX_QUEUE_CAB:
  321. tx_reg = AR5K_NOQCU_TXDP1;
  322. break;
  323. default:
  324. return 0xffffffff;
  325. }
  326. } else {
  327. tx_reg = AR5K_QUEUE_TXDP(queue);
  328. }
  329. return ath5k_hw_reg_read(ah, tx_reg);
  330. }
  331. /**
  332. * ath5k_hw_set_txdp() - Set TX Descriptor's address for a specific queue
  333. * @ah: The &struct ath5k_hw
  334. * @queue: The hw queue number
  335. * @phys_addr: The physical address
  336. *
  337. * Set TX descriptor's address for a specific queue. For 5210 we ignore
  338. * the queue number and we use tx queue type since we only have 2 queues
  339. * so as above we use TXDP0 for normal data queue and TXDP1 for beacon queue.
  340. * For newer chips with QCU/DCU we just set the corresponding TXDP register.
  341. * Returns -EINVAL if queue type is invalid for 5210 and -EIO if queue is still
  342. * active.
  343. */
  344. int
  345. ath5k_hw_set_txdp(struct ath5k_hw *ah, unsigned int queue, u32 phys_addr)
  346. {
  347. u16 tx_reg;
  348. AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
  349. /*
  350. * Set the transmit queue descriptor pointer register by type
  351. * on 5210
  352. */
  353. if (ah->ah_version == AR5K_AR5210) {
  354. switch (ah->ah_txq[queue].tqi_type) {
  355. case AR5K_TX_QUEUE_DATA:
  356. tx_reg = AR5K_NOQCU_TXDP0;
  357. break;
  358. case AR5K_TX_QUEUE_BEACON:
  359. case AR5K_TX_QUEUE_CAB:
  360. tx_reg = AR5K_NOQCU_TXDP1;
  361. break;
  362. default:
  363. return -EINVAL;
  364. }
  365. } else {
  366. /*
  367. * Set the transmit queue descriptor pointer for
  368. * the selected queue on QCU for 5211+
  369. * (this won't work if the queue is still active)
  370. */
  371. if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, queue))
  372. return -EIO;
  373. tx_reg = AR5K_QUEUE_TXDP(queue);
  374. }
  375. /* Set descriptor pointer */
  376. ath5k_hw_reg_write(ah, phys_addr, tx_reg);
  377. return 0;
  378. }
  379. /**
  380. * ath5k_hw_update_tx_triglevel() - Update tx trigger level
  381. * @ah: The &struct ath5k_hw
  382. * @increase: Flag to force increase of trigger level
  383. *
  384. * This function increases/decreases the tx trigger level for the tx fifo
  385. * buffer (aka FIFO threshold) that is used to indicate when PCU flushes
  386. * the buffer and transmits its data. Lowering this results sending small
  387. * frames more quickly but can lead to tx underruns, raising it a lot can
  388. * result other problems. Right now we start with the lowest possible
  389. * (64Bytes) and if we get tx underrun we increase it using the increase
  390. * flag. Returns -EIO if we have reached maximum/minimum.
  391. *
  392. * XXX: Link this with tx DMA size ?
  393. * XXX2: Use it to save interrupts ?
  394. */
  395. int
  396. ath5k_hw_update_tx_triglevel(struct ath5k_hw *ah, bool increase)
  397. {
  398. u32 trigger_level, imr;
  399. int ret = -EIO;
  400. /*
  401. * Disable interrupts by setting the mask
  402. */
  403. imr = ath5k_hw_set_imr(ah, ah->ah_imr & ~AR5K_INT_GLOBAL);
  404. trigger_level = AR5K_REG_MS(ath5k_hw_reg_read(ah, AR5K_TXCFG),
  405. AR5K_TXCFG_TXFULL);
  406. if (!increase) {
  407. if (--trigger_level < AR5K_TUNE_MIN_TX_FIFO_THRES)
  408. goto done;
  409. } else
  410. trigger_level +=
  411. ((AR5K_TUNE_MAX_TX_FIFO_THRES - trigger_level) / 2);
  412. /*
  413. * Update trigger level on success
  414. */
  415. if (ah->ah_version == AR5K_AR5210)
  416. ath5k_hw_reg_write(ah, trigger_level, AR5K_TRIG_LVL);
  417. else
  418. AR5K_REG_WRITE_BITS(ah, AR5K_TXCFG,
  419. AR5K_TXCFG_TXFULL, trigger_level);
  420. ret = 0;
  421. done:
  422. /*
  423. * Restore interrupt mask
  424. */
  425. ath5k_hw_set_imr(ah, imr);
  426. return ret;
  427. }
  428. /*******************\
  429. * Interrupt masking *
  430. \*******************/
  431. /**
  432. * ath5k_hw_is_intr_pending() - Check if we have pending interrupts
  433. * @ah: The &struct ath5k_hw
  434. *
  435. * Check if we have pending interrupts to process. Returns 1 if we
  436. * have pending interrupts and 0 if we haven't.
  437. */
  438. bool
  439. ath5k_hw_is_intr_pending(struct ath5k_hw *ah)
  440. {
  441. return ath5k_hw_reg_read(ah, AR5K_INTPEND) == 1 ? 1 : 0;
  442. }
  443. /**
  444. * ath5k_hw_get_isr() - Get interrupt status
  445. * @ah: The @struct ath5k_hw
  446. * @interrupt_mask: Driver's interrupt mask used to filter out
  447. * interrupts in sw.
  448. *
  449. * This function is used inside our interrupt handler to determine the reason
  450. * for the interrupt by reading Primary Interrupt Status Register. Returns an
  451. * abstract interrupt status mask which is mostly ISR with some uncommon bits
  452. * being mapped on some standard non hw-specific positions
  453. * (check out &ath5k_int).
  454. *
  455. * NOTE: We do write-to-clear, so the active PISR/SISR bits at the time this
  456. * function gets called are cleared on return.
  457. */
  458. int
  459. ath5k_hw_get_isr(struct ath5k_hw *ah, enum ath5k_int *interrupt_mask)
  460. {
  461. u32 data = 0;
  462. /*
  463. * Read interrupt status from Primary Interrupt
  464. * Register.
  465. *
  466. * Note: PISR/SISR Not available on 5210
  467. */
  468. if (ah->ah_version == AR5K_AR5210) {
  469. u32 isr = 0;
  470. isr = ath5k_hw_reg_read(ah, AR5K_ISR);
  471. if (unlikely(isr == AR5K_INT_NOCARD)) {
  472. *interrupt_mask = isr;
  473. return -ENODEV;
  474. }
  475. /*
  476. * Filter out the non-common bits from the interrupt
  477. * status.
  478. */
  479. *interrupt_mask = (isr & AR5K_INT_COMMON) & ah->ah_imr;
  480. /* Hanlde INT_FATAL */
  481. if (unlikely(isr & (AR5K_ISR_SSERR | AR5K_ISR_MCABT
  482. | AR5K_ISR_DPERR)))
  483. *interrupt_mask |= AR5K_INT_FATAL;
  484. /*
  485. * XXX: BMISS interrupts may occur after association.
  486. * I found this on 5210 code but it needs testing. If this is
  487. * true we should disable them before assoc and re-enable them
  488. * after a successful assoc + some jiffies.
  489. interrupt_mask &= ~AR5K_INT_BMISS;
  490. */
  491. data = isr;
  492. } else {
  493. u32 pisr = 0;
  494. u32 pisr_clear = 0;
  495. u32 sisr0 = 0;
  496. u32 sisr1 = 0;
  497. u32 sisr2 = 0;
  498. u32 sisr3 = 0;
  499. u32 sisr4 = 0;
  500. /* Read PISR and SISRs... */
  501. pisr = ath5k_hw_reg_read(ah, AR5K_PISR);
  502. if (unlikely(pisr == AR5K_INT_NOCARD)) {
  503. *interrupt_mask = pisr;
  504. return -ENODEV;
  505. }
  506. sisr0 = ath5k_hw_reg_read(ah, AR5K_SISR0);
  507. sisr1 = ath5k_hw_reg_read(ah, AR5K_SISR1);
  508. sisr2 = ath5k_hw_reg_read(ah, AR5K_SISR2);
  509. sisr3 = ath5k_hw_reg_read(ah, AR5K_SISR3);
  510. sisr4 = ath5k_hw_reg_read(ah, AR5K_SISR4);
  511. /*
  512. * PISR holds the logical OR of interrupt bits
  513. * from SISR registers:
  514. *
  515. * TXOK and TXDESC -> Logical OR of TXOK and TXDESC
  516. * per-queue bits on SISR0
  517. *
  518. * TXERR and TXEOL -> Logical OR of TXERR and TXEOL
  519. * per-queue bits on SISR1
  520. *
  521. * TXURN -> Logical OR of TXURN per-queue bits on SISR2
  522. *
  523. * HIUERR -> Logical OR of MCABT, SSERR and DPER bits on SISR2
  524. *
  525. * BCNMISC -> Logical OR of TIM, CAB_END, DTIM_SYNC
  526. * BCN_TIMEOUT, CAB_TIMEOUT and DTIM
  527. * (and TSFOOR ?) bits on SISR2
  528. *
  529. * QCBRORN and QCBRURN -> Logical OR of QCBRORN and
  530. * QCBRURN per-queue bits on SISR3
  531. * QTRIG -> Logical OR of QTRIG per-queue bits on SISR4
  532. *
  533. * If we clean these bits on PISR we 'll also clear all
  534. * related bits from SISRs, e.g. if we write the TXOK bit on
  535. * PISR we 'll clean all TXOK bits from SISR0 so if a new TXOK
  536. * interrupt got fired for another queue while we were reading
  537. * the interrupt registers and we write back the TXOK bit on
  538. * PISR we 'll lose it. So make sure that we don't write back
  539. * on PISR any bits that come from SISRs. Clearing them from
  540. * SISRs will also clear PISR so no need to worry here.
  541. */
  542. /* XXX: There seems to be an issue on some cards
  543. * with tx interrupt flags not being updated
  544. * on PISR despite that all Tx interrupt bits
  545. * are cleared on SISRs. Since we handle all
  546. * Tx queues all together it shouldn't be an
  547. * issue if we clear Tx interrupt flags also
  548. * on PISR to avoid that.
  549. */
  550. pisr_clear = (pisr & ~AR5K_ISR_BITS_FROM_SISRS) |
  551. (pisr & AR5K_INT_TX_ALL);
  552. /*
  553. * Write to clear them...
  554. * Note: This means that each bit we write back
  555. * to the registers will get cleared, leaving the
  556. * rest unaffected. So this won't affect new interrupts
  557. * we didn't catch while reading/processing, we 'll get
  558. * them next time get_isr gets called.
  559. */
  560. ath5k_hw_reg_write(ah, sisr0, AR5K_SISR0);
  561. ath5k_hw_reg_write(ah, sisr1, AR5K_SISR1);
  562. ath5k_hw_reg_write(ah, sisr2, AR5K_SISR2);
  563. ath5k_hw_reg_write(ah, sisr3, AR5K_SISR3);
  564. ath5k_hw_reg_write(ah, sisr4, AR5K_SISR4);
  565. ath5k_hw_reg_write(ah, pisr_clear, AR5K_PISR);
  566. /* Flush previous write */
  567. ath5k_hw_reg_read(ah, AR5K_PISR);
  568. /*
  569. * Filter out the non-common bits from the interrupt
  570. * status.
  571. */
  572. *interrupt_mask = (pisr & AR5K_INT_COMMON) & ah->ah_imr;
  573. /* We treat TXOK,TXDESC, TXERR and TXEOL
  574. * the same way (schedule the tx tasklet)
  575. * so we track them all together per queue */
  576. if (pisr & AR5K_ISR_TXOK)
  577. ah->ah_txq_isr_txok_all |= AR5K_REG_MS(sisr0,
  578. AR5K_SISR0_QCU_TXOK);
  579. if (pisr & AR5K_ISR_TXDESC)
  580. ah->ah_txq_isr_txok_all |= AR5K_REG_MS(sisr0,
  581. AR5K_SISR0_QCU_TXDESC);
  582. if (pisr & AR5K_ISR_TXERR)
  583. ah->ah_txq_isr_txok_all |= AR5K_REG_MS(sisr1,
  584. AR5K_SISR1_QCU_TXERR);
  585. if (pisr & AR5K_ISR_TXEOL)
  586. ah->ah_txq_isr_txok_all |= AR5K_REG_MS(sisr1,
  587. AR5K_SISR1_QCU_TXEOL);
  588. /* Currently this is not much useful since we treat
  589. * all queues the same way if we get a TXURN (update
  590. * tx trigger level) but we might need it later on*/
  591. if (pisr & AR5K_ISR_TXURN)
  592. ah->ah_txq_isr_txurn |= AR5K_REG_MS(sisr2,
  593. AR5K_SISR2_QCU_TXURN);
  594. /* Misc Beacon related interrupts */
  595. /* For AR5211 */
  596. if (pisr & AR5K_ISR_TIM)
  597. *interrupt_mask |= AR5K_INT_TIM;
  598. /* For AR5212+ */
  599. if (pisr & AR5K_ISR_BCNMISC) {
  600. if (sisr2 & AR5K_SISR2_TIM)
  601. *interrupt_mask |= AR5K_INT_TIM;
  602. if (sisr2 & AR5K_SISR2_DTIM)
  603. *interrupt_mask |= AR5K_INT_DTIM;
  604. if (sisr2 & AR5K_SISR2_DTIM_SYNC)
  605. *interrupt_mask |= AR5K_INT_DTIM_SYNC;
  606. if (sisr2 & AR5K_SISR2_BCN_TIMEOUT)
  607. *interrupt_mask |= AR5K_INT_BCN_TIMEOUT;
  608. if (sisr2 & AR5K_SISR2_CAB_TIMEOUT)
  609. *interrupt_mask |= AR5K_INT_CAB_TIMEOUT;
  610. }
  611. /* Below interrupts are unlikely to happen */
  612. /* HIU = Host Interface Unit (PCI etc)
  613. * Can be one of MCABT, SSERR, DPERR from SISR2 */
  614. if (unlikely(pisr & (AR5K_ISR_HIUERR)))
  615. *interrupt_mask |= AR5K_INT_FATAL;
  616. /*Beacon Not Ready*/
  617. if (unlikely(pisr & (AR5K_ISR_BNR)))
  618. *interrupt_mask |= AR5K_INT_BNR;
  619. /* A queue got CBR overrun */
  620. if (unlikely(pisr & (AR5K_ISR_QCBRORN))) {
  621. *interrupt_mask |= AR5K_INT_QCBRORN;
  622. ah->ah_txq_isr_qcborn |= AR5K_REG_MS(sisr3,
  623. AR5K_SISR3_QCBRORN);
  624. }
  625. /* A queue got CBR underrun */
  626. if (unlikely(pisr & (AR5K_ISR_QCBRURN))) {
  627. *interrupt_mask |= AR5K_INT_QCBRURN;
  628. ah->ah_txq_isr_qcburn |= AR5K_REG_MS(sisr3,
  629. AR5K_SISR3_QCBRURN);
  630. }
  631. /* A queue got triggered */
  632. if (unlikely(pisr & (AR5K_ISR_QTRIG))) {
  633. *interrupt_mask |= AR5K_INT_QTRIG;
  634. ah->ah_txq_isr_qtrig |= AR5K_REG_MS(sisr4,
  635. AR5K_SISR4_QTRIG);
  636. }
  637. data = pisr;
  638. }
  639. /*
  640. * In case we didn't handle anything,
  641. * print the register value.
  642. */
  643. if (unlikely(*interrupt_mask == 0 && net_ratelimit()))
  644. ATH5K_PRINTF("ISR: 0x%08x IMR: 0x%08x\n", data, ah->ah_imr);
  645. return 0;
  646. }
  647. /**
  648. * ath5k_hw_set_imr() - Set interrupt mask
  649. * @ah: The &struct ath5k_hw
  650. * @new_mask: The new interrupt mask to be set
  651. *
  652. * Set the interrupt mask in hw to save interrupts. We do that by mapping
  653. * ath5k_int bits to hw-specific bits to remove abstraction and writing
  654. * Interrupt Mask Register.
  655. */
  656. enum ath5k_int
  657. ath5k_hw_set_imr(struct ath5k_hw *ah, enum ath5k_int new_mask)
  658. {
  659. enum ath5k_int old_mask, int_mask;
  660. old_mask = ah->ah_imr;
  661. /*
  662. * Disable card interrupts to prevent any race conditions
  663. * (they will be re-enabled afterwards if AR5K_INT GLOBAL
  664. * is set again on the new mask).
  665. */
  666. if (old_mask & AR5K_INT_GLOBAL) {
  667. ath5k_hw_reg_write(ah, AR5K_IER_DISABLE, AR5K_IER);
  668. ath5k_hw_reg_read(ah, AR5K_IER);
  669. }
  670. /*
  671. * Add additional, chipset-dependent interrupt mask flags
  672. * and write them to the IMR (interrupt mask register).
  673. */
  674. int_mask = new_mask & AR5K_INT_COMMON;
  675. if (ah->ah_version != AR5K_AR5210) {
  676. /* Preserve per queue TXURN interrupt mask */
  677. u32 simr2 = ath5k_hw_reg_read(ah, AR5K_SIMR2)
  678. & AR5K_SIMR2_QCU_TXURN;
  679. /* Fatal interrupt abstraction for 5211+ */
  680. if (new_mask & AR5K_INT_FATAL) {
  681. int_mask |= AR5K_IMR_HIUERR;
  682. simr2 |= (AR5K_SIMR2_MCABT | AR5K_SIMR2_SSERR
  683. | AR5K_SIMR2_DPERR);
  684. }
  685. /* Misc beacon related interrupts */
  686. if (new_mask & AR5K_INT_TIM)
  687. int_mask |= AR5K_IMR_TIM;
  688. if (new_mask & AR5K_INT_TIM)
  689. simr2 |= AR5K_SISR2_TIM;
  690. if (new_mask & AR5K_INT_DTIM)
  691. simr2 |= AR5K_SISR2_DTIM;
  692. if (new_mask & AR5K_INT_DTIM_SYNC)
  693. simr2 |= AR5K_SISR2_DTIM_SYNC;
  694. if (new_mask & AR5K_INT_BCN_TIMEOUT)
  695. simr2 |= AR5K_SISR2_BCN_TIMEOUT;
  696. if (new_mask & AR5K_INT_CAB_TIMEOUT)
  697. simr2 |= AR5K_SISR2_CAB_TIMEOUT;
  698. /*Beacon Not Ready*/
  699. if (new_mask & AR5K_INT_BNR)
  700. int_mask |= AR5K_INT_BNR;
  701. /* Note: Per queue interrupt masks
  702. * are set via ath5k_hw_reset_tx_queue() (qcu.c) */
  703. ath5k_hw_reg_write(ah, int_mask, AR5K_PIMR);
  704. ath5k_hw_reg_write(ah, simr2, AR5K_SIMR2);
  705. } else {
  706. /* Fatal interrupt abstraction for 5210 */
  707. if (new_mask & AR5K_INT_FATAL)
  708. int_mask |= (AR5K_IMR_SSERR | AR5K_IMR_MCABT
  709. | AR5K_IMR_HIUERR | AR5K_IMR_DPERR);
  710. /* Only common interrupts left for 5210 (no SIMRs) */
  711. ath5k_hw_reg_write(ah, int_mask, AR5K_IMR);
  712. }
  713. /* If RXNOFRM interrupt is masked disable it
  714. * by setting AR5K_RXNOFRM to zero */
  715. if (!(new_mask & AR5K_INT_RXNOFRM))
  716. ath5k_hw_reg_write(ah, 0, AR5K_RXNOFRM);
  717. /* Store new interrupt mask */
  718. ah->ah_imr = new_mask;
  719. /* ..re-enable interrupts if AR5K_INT_GLOBAL is set */
  720. if (new_mask & AR5K_INT_GLOBAL) {
  721. ath5k_hw_reg_write(ah, AR5K_IER_ENABLE, AR5K_IER);
  722. ath5k_hw_reg_read(ah, AR5K_IER);
  723. }
  724. return old_mask;
  725. }
  726. /********************\
  727. Init/Stop functions
  728. \********************/
  729. /**
  730. * ath5k_hw_dma_init() - Initialize DMA unit
  731. * @ah: The &struct ath5k_hw
  732. *
  733. * Set DMA size and pre-enable interrupts
  734. * (driver handles tx/rx buffer setup and
  735. * dma start/stop)
  736. *
  737. * XXX: Save/restore RXDP/TXDP registers ?
  738. */
  739. void
  740. ath5k_hw_dma_init(struct ath5k_hw *ah)
  741. {
  742. /*
  743. * Set Rx/Tx DMA Configuration
  744. *
  745. * Set standard DMA size (128). Note that
  746. * a DMA size of 512 causes rx overruns and tx errors
  747. * on pci-e cards (tested on 5424 but since rx overruns
  748. * also occur on 5416/5418 with madwifi we set 128
  749. * for all PCI-E cards to be safe).
  750. *
  751. * XXX: need to check 5210 for this
  752. * TODO: Check out tx trigger level, it's always 64 on dumps but I
  753. * guess we can tweak it and see how it goes ;-)
  754. */
  755. if (ah->ah_version != AR5K_AR5210) {
  756. AR5K_REG_WRITE_BITS(ah, AR5K_TXCFG,
  757. AR5K_TXCFG_SDMAMR, AR5K_DMASIZE_128B);
  758. AR5K_REG_WRITE_BITS(ah, AR5K_RXCFG,
  759. AR5K_RXCFG_SDMAMW, AR5K_DMASIZE_128B);
  760. }
  761. /* Pre-enable interrupts on 5211/5212*/
  762. if (ah->ah_version != AR5K_AR5210)
  763. ath5k_hw_set_imr(ah, ah->ah_imr);
  764. }
  765. /**
  766. * ath5k_hw_dma_stop() - stop DMA unit
  767. * @ah: The &struct ath5k_hw
  768. *
  769. * Stop tx/rx DMA and interrupts. Returns
  770. * -EBUSY if tx or rx dma failed to stop.
  771. *
  772. * XXX: Sometimes DMA unit hangs and we have
  773. * stuck frames on tx queues, only a reset
  774. * can fix that.
  775. */
  776. int
  777. ath5k_hw_dma_stop(struct ath5k_hw *ah)
  778. {
  779. int i, qmax, err;
  780. err = 0;
  781. /* Disable interrupts */
  782. ath5k_hw_set_imr(ah, 0);
  783. /* Stop rx dma */
  784. err = ath5k_hw_stop_rx_dma(ah);
  785. if (err)
  786. return err;
  787. /* Clear any pending interrupts
  788. * and disable tx dma */
  789. if (ah->ah_version != AR5K_AR5210) {
  790. ath5k_hw_reg_write(ah, 0xffffffff, AR5K_PISR);
  791. qmax = AR5K_NUM_TX_QUEUES;
  792. } else {
  793. /* PISR/SISR Not available on 5210 */
  794. ath5k_hw_reg_read(ah, AR5K_ISR);
  795. qmax = AR5K_NUM_TX_QUEUES_NOQCU;
  796. }
  797. for (i = 0; i < qmax; i++) {
  798. err = ath5k_hw_stop_tx_dma(ah, i);
  799. /* -EINVAL -> queue inactive */
  800. if (err && err != -EINVAL)
  801. return err;
  802. }
  803. return 0;
  804. }