2 * Copyright (c) 2004-2008 Reyk Floeter <reyk@openbsd.org>
3 * Copyright (c) 2006-2008 Nick Kossifidis <mickflemm@gmail.com>
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.
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.
19 /*************************************\
20 * DMA and interrupt masking functions *
21 \*************************************/
24 * dma.c - DMA and interrupt masking functions
26 * Here we setup descriptor pointers (rxdp/txdp) start/stop dma engine and
27 * handle queue setup for 5210 chipset (rest are handled on qcu.c).
28 * Also we setup interrupt mask register (IMR) and read the various iterrupt
29 * status registers (ISR).
31 * TODO: Handle SISR on 5211+ and introduce a function to return the queue
32 * number that resulted the interrupt.
45 * ath5k_hw_start_rx_dma - Start DMA receive
47 * @ah: The &struct ath5k_hw
49 void ath5k_hw_start_rx_dma(struct ath5k_hw *ah)
51 ATH5K_TRACE(ah->ah_sc);
52 ath5k_hw_reg_write(ah, AR5K_CR_RXE, AR5K_CR);
53 ath5k_hw_reg_read(ah, AR5K_CR);
57 * ath5k_hw_stop_rx_dma - Stop DMA receive
59 * @ah: The &struct ath5k_hw
61 int ath5k_hw_stop_rx_dma(struct ath5k_hw *ah)
65 ATH5K_TRACE(ah->ah_sc);
66 ath5k_hw_reg_write(ah, AR5K_CR_RXD, AR5K_CR);
69 * It may take some time to disable the DMA receive unit
71 for (i = 1000; i > 0 &&
72 (ath5k_hw_reg_read(ah, AR5K_CR) & AR5K_CR_RXE) != 0;
76 return i ? 0 : -EBUSY;
80 * ath5k_hw_get_rxdp - Get RX Descriptor's address
82 * @ah: The &struct ath5k_hw
84 u32 ath5k_hw_get_rxdp(struct ath5k_hw *ah)
86 return ath5k_hw_reg_read(ah, AR5K_RXDP);
90 * ath5k_hw_set_rxdp - Set RX Descriptor's address
92 * @ah: The &struct ath5k_hw
93 * @phys_addr: RX descriptor address
95 * XXX: Should we check if rx is enabled before setting rxdp ?
97 void ath5k_hw_set_rxdp(struct ath5k_hw *ah, u32 phys_addr)
99 ATH5K_TRACE(ah->ah_sc);
101 ath5k_hw_reg_write(ah, phys_addr, AR5K_RXDP);
110 * ath5k_hw_start_tx_dma - Start DMA transmit for a specific queue
112 * @ah: The &struct ath5k_hw
113 * @queue: The hw queue number
115 * Start DMA transmit for a specific queue and since 5210 doesn't have
116 * QCU/DCU, set up queue parameters for 5210 here based on queue type (one
117 * queue for normal data and one queue for beacons). For queue setup
118 * on newer chips check out qcu.c. Returns -EINVAL if queue number is out
119 * of range or if queue is already disabled.
121 * NOTE: Must be called after setting up tx control descriptor for that
124 int ath5k_hw_start_tx_dma(struct ath5k_hw *ah, unsigned int queue)
128 ATH5K_TRACE(ah->ah_sc);
129 AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
131 /* Return if queue is declared inactive */
132 if (ah->ah_txq[queue].tqi_type == AR5K_TX_QUEUE_INACTIVE)
135 if (ah->ah_version == AR5K_AR5210) {
136 tx_queue = ath5k_hw_reg_read(ah, AR5K_CR);
139 * Set the queue by type on 5210
141 switch (ah->ah_txq[queue].tqi_type) {
142 case AR5K_TX_QUEUE_DATA:
143 tx_queue |= AR5K_CR_TXE0 & ~AR5K_CR_TXD0;
145 case AR5K_TX_QUEUE_BEACON:
146 tx_queue |= AR5K_CR_TXE1 & ~AR5K_CR_TXD1;
147 ath5k_hw_reg_write(ah, AR5K_BCR_TQ1V | AR5K_BCR_BDMAE,
150 case AR5K_TX_QUEUE_CAB:
151 tx_queue |= AR5K_CR_TXE1 & ~AR5K_CR_TXD1;
152 ath5k_hw_reg_write(ah, AR5K_BCR_TQ1FV | AR5K_BCR_TQ1V |
153 AR5K_BCR_BDMAE, AR5K_BSR);
159 ath5k_hw_reg_write(ah, tx_queue, AR5K_CR);
160 ath5k_hw_reg_read(ah, AR5K_CR);
162 /* Return if queue is disabled */
163 if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXD, queue))
167 AR5K_REG_WRITE_Q(ah, AR5K_QCU_TXE, queue);
174 * ath5k_hw_stop_tx_dma - Stop DMA transmit on a specific queue
176 * @ah: The &struct ath5k_hw
177 * @queue: The hw queue number
179 * Stop DMA transmit on a specific hw queue and drain queue so we don't
180 * have any pending frames. Returns -EBUSY if we still have pending frames,
181 * -EINVAL if queue number is out of range.
184 int ath5k_hw_stop_tx_dma(struct ath5k_hw *ah, unsigned int queue)
187 u32 tx_queue, pending;
189 ATH5K_TRACE(ah->ah_sc);
190 AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
192 /* Return if queue is declared inactive */
193 if (ah->ah_txq[queue].tqi_type == AR5K_TX_QUEUE_INACTIVE)
196 if (ah->ah_version == AR5K_AR5210) {
197 tx_queue = ath5k_hw_reg_read(ah, AR5K_CR);
202 switch (ah->ah_txq[queue].tqi_type) {
203 case AR5K_TX_QUEUE_DATA:
204 tx_queue |= AR5K_CR_TXD0 & ~AR5K_CR_TXE0;
206 case AR5K_TX_QUEUE_BEACON:
207 case AR5K_TX_QUEUE_CAB:
209 tx_queue |= AR5K_CR_TXD1 & ~AR5K_CR_TXD1;
210 ath5k_hw_reg_write(ah, 0, AR5K_BSR);
217 ath5k_hw_reg_write(ah, tx_queue, AR5K_CR);
218 ath5k_hw_reg_read(ah, AR5K_CR);
221 * Schedule TX disable and wait until queue is empty
223 AR5K_REG_WRITE_Q(ah, AR5K_QCU_TXD, queue);
225 /*Check for pending frames*/
227 pending = ath5k_hw_reg_read(ah,
228 AR5K_QUEUE_STATUS(queue)) &
229 AR5K_QCU_STS_FRMPENDCNT;
231 } while (--i && pending);
233 /* For 2413+ order PCU to drop packets using
235 if (ah->ah_mac_version >= (AR5K_SREV_AR2414 >> 4) &&
237 /* Set periodicity and duration */
238 ath5k_hw_reg_write(ah,
239 AR5K_REG_SM(100, AR5K_QUIET_CTL2_QT_PER)|
240 AR5K_REG_SM(10, AR5K_QUIET_CTL2_QT_DUR),
243 /* Enable quiet period for current TSF */
244 ath5k_hw_reg_write(ah,
245 AR5K_QUIET_CTL1_QT_EN |
246 AR5K_REG_SM(ath5k_hw_reg_read(ah,
247 AR5K_TSF_L32_5211) >> 10,
248 AR5K_QUIET_CTL1_NEXT_QT_TSF),
251 /* Force channel idle high */
252 AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5211,
253 AR5K_DIAG_SW_CHANEL_IDLE_HIGH);
255 /* Wait a while and disable mechanism */
257 AR5K_REG_DISABLE_BITS(ah, AR5K_QUIET_CTL1,
258 AR5K_QUIET_CTL1_QT_EN);
260 /* Re-check for pending frames */
263 pending = ath5k_hw_reg_read(ah,
264 AR5K_QUEUE_STATUS(queue)) &
265 AR5K_QCU_STS_FRMPENDCNT;
267 } while (--i && pending);
269 AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW_5211,
270 AR5K_DIAG_SW_CHANEL_IDLE_HIGH);
274 ath5k_hw_reg_write(ah, 0, AR5K_QCU_TXD);
279 /* TODO: Check for success on 5210 else return error */
284 * ath5k_hw_get_txdp - Get TX Descriptor's address for a specific queue
286 * @ah: The &struct ath5k_hw
287 * @queue: The hw queue number
289 * Get TX descriptor's address for a specific queue. For 5210 we ignore
290 * the queue number and use tx queue type since we only have 2 queues.
291 * We use TXDP0 for normal data queue and TXDP1 for beacon queue.
292 * For newer chips with QCU/DCU we just read the corresponding TXDP register.
294 * XXX: Is TXDP read and clear ?
296 u32 ath5k_hw_get_txdp(struct ath5k_hw *ah, unsigned int queue)
300 ATH5K_TRACE(ah->ah_sc);
301 AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
304 * Get the transmit queue descriptor pointer from the selected queue
306 /*5210 doesn't have QCU*/
307 if (ah->ah_version == AR5K_AR5210) {
308 switch (ah->ah_txq[queue].tqi_type) {
309 case AR5K_TX_QUEUE_DATA:
310 tx_reg = AR5K_NOQCU_TXDP0;
312 case AR5K_TX_QUEUE_BEACON:
313 case AR5K_TX_QUEUE_CAB:
314 tx_reg = AR5K_NOQCU_TXDP1;
320 tx_reg = AR5K_QUEUE_TXDP(queue);
323 return ath5k_hw_reg_read(ah, tx_reg);
327 * ath5k_hw_set_txdp - Set TX Descriptor's address for a specific queue
329 * @ah: The &struct ath5k_hw
330 * @queue: The hw queue number
332 * Set TX descriptor's address for a specific queue. For 5210 we ignore
333 * the queue number and we use tx queue type since we only have 2 queues
334 * so as above we use TXDP0 for normal data queue and TXDP1 for beacon queue.
335 * For newer chips with QCU/DCU we just set the corresponding TXDP register.
336 * Returns -EINVAL if queue type is invalid for 5210 and -EIO if queue is still
339 int ath5k_hw_set_txdp(struct ath5k_hw *ah, unsigned int queue, u32 phys_addr)
343 ATH5K_TRACE(ah->ah_sc);
344 AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
347 * Set the transmit queue descriptor pointer register by type
350 if (ah->ah_version == AR5K_AR5210) {
351 switch (ah->ah_txq[queue].tqi_type) {
352 case AR5K_TX_QUEUE_DATA:
353 tx_reg = AR5K_NOQCU_TXDP0;
355 case AR5K_TX_QUEUE_BEACON:
356 case AR5K_TX_QUEUE_CAB:
357 tx_reg = AR5K_NOQCU_TXDP1;
364 * Set the transmit queue descriptor pointer for
365 * the selected queue on QCU for 5211+
366 * (this won't work if the queue is still active)
368 if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, queue))
371 tx_reg = AR5K_QUEUE_TXDP(queue);
374 /* Set descriptor pointer */
375 ath5k_hw_reg_write(ah, phys_addr, tx_reg);
381 * ath5k_hw_update_tx_triglevel - Update tx trigger level
383 * @ah: The &struct ath5k_hw
384 * @increase: Flag to force increase of trigger level
386 * This function increases/decreases the tx trigger level for the tx fifo
387 * buffer (aka FIFO threshold) that is used to indicate when PCU flushes
388 * the buffer and transmits it's data. Lowering this results sending small
389 * frames more quickly but can lead to tx underruns, raising it a lot can
390 * result other problems (i think bmiss is related). Right now we start with
391 * the lowest possible (64Bytes) and if we get tx underrun we increase it using
392 * the increase flag. Returns -EIO if we have have reached maximum/minimum.
394 * XXX: Link this with tx DMA size ?
395 * XXX: Use it to save interrupts ?
396 * TODO: Needs testing, i think it's related to bmiss...
398 int ath5k_hw_update_tx_triglevel(struct ath5k_hw *ah, bool increase)
400 u32 trigger_level, imr;
403 ATH5K_TRACE(ah->ah_sc);
406 * Disable interrupts by setting the mask
408 imr = ath5k_hw_set_imr(ah, ah->ah_imr & ~AR5K_INT_GLOBAL);
410 trigger_level = AR5K_REG_MS(ath5k_hw_reg_read(ah, AR5K_TXCFG),
414 if (--trigger_level < AR5K_TUNE_MIN_TX_FIFO_THRES)
418 ((AR5K_TUNE_MAX_TX_FIFO_THRES - trigger_level) / 2);
421 * Update trigger level on success
423 if (ah->ah_version == AR5K_AR5210)
424 ath5k_hw_reg_write(ah, trigger_level, AR5K_TRIG_LVL);
426 AR5K_REG_WRITE_BITS(ah, AR5K_TXCFG,
427 AR5K_TXCFG_TXFULL, trigger_level);
433 * Restore interrupt mask
435 ath5k_hw_set_imr(ah, imr);
440 /*******************\
441 * Interrupt masking *
442 \*******************/
445 * ath5k_hw_is_intr_pending - Check if we have pending interrupts
447 * @ah: The &struct ath5k_hw
449 * Check if we have pending interrupts to process. Returns 1 if we
450 * have pending interrupts and 0 if we haven't.
452 bool ath5k_hw_is_intr_pending(struct ath5k_hw *ah)
454 ATH5K_TRACE(ah->ah_sc);
455 return ath5k_hw_reg_read(ah, AR5K_INTPEND) == 1 ? 1 : 0;
459 * ath5k_hw_get_isr - Get interrupt status
461 * @ah: The @struct ath5k_hw
462 * @interrupt_mask: Driver's interrupt mask used to filter out
465 * This function is used inside our interrupt handler to determine the reason
466 * for the interrupt by reading Primary Interrupt Status Register. Returns an
467 * abstract interrupt status mask which is mostly ISR with some uncommon bits
468 * being mapped on some standard non hw-specific positions
469 * (check out &ath5k_int).
471 * NOTE: We use read-and-clear register, so after this function is called ISR
474 int ath5k_hw_get_isr(struct ath5k_hw *ah, enum ath5k_int *interrupt_mask)
478 ATH5K_TRACE(ah->ah_sc);
481 * Read interrupt status from the Interrupt Status register
484 if (ah->ah_version == AR5K_AR5210) {
485 data = ath5k_hw_reg_read(ah, AR5K_ISR);
486 if (unlikely(data == AR5K_INT_NOCARD)) {
487 *interrupt_mask = data;
492 * Read interrupt status from Interrupt
493 * Status Register shadow copy (Read And Clear)
495 * Note: PISR/SISR Not available on 5210
497 data = ath5k_hw_reg_read(ah, AR5K_RAC_PISR);
498 if (unlikely(data == AR5K_INT_NOCARD)) {
499 *interrupt_mask = data;
505 * Get abstract interrupt mask (driver-compatible)
507 *interrupt_mask = (data & AR5K_INT_COMMON) & ah->ah_imr;
509 if (ah->ah_version != AR5K_AR5210) {
510 u32 sisr2 = ath5k_hw_reg_read(ah, AR5K_RAC_SISR2);
512 /*HIU = Host Interface Unit (PCI etc)*/
513 if (unlikely(data & (AR5K_ISR_HIUERR)))
514 *interrupt_mask |= AR5K_INT_FATAL;
517 if (unlikely(data & (AR5K_ISR_BNR)))
518 *interrupt_mask |= AR5K_INT_BNR;
520 if (unlikely(sisr2 & (AR5K_SISR2_SSERR |
523 *interrupt_mask |= AR5K_INT_FATAL;
525 if (data & AR5K_ISR_TIM)
526 *interrupt_mask |= AR5K_INT_TIM;
528 if (data & AR5K_ISR_BCNMISC) {
529 if (sisr2 & AR5K_SISR2_TIM)
530 *interrupt_mask |= AR5K_INT_TIM;
531 if (sisr2 & AR5K_SISR2_DTIM)
532 *interrupt_mask |= AR5K_INT_DTIM;
533 if (sisr2 & AR5K_SISR2_DTIM_SYNC)
534 *interrupt_mask |= AR5K_INT_DTIM_SYNC;
535 if (sisr2 & AR5K_SISR2_BCN_TIMEOUT)
536 *interrupt_mask |= AR5K_INT_BCN_TIMEOUT;
537 if (sisr2 & AR5K_SISR2_CAB_TIMEOUT)
538 *interrupt_mask |= AR5K_INT_CAB_TIMEOUT;
541 if (data & AR5K_ISR_RXDOPPLER)
542 *interrupt_mask |= AR5K_INT_RX_DOPPLER;
543 if (data & AR5K_ISR_QCBRORN) {
544 *interrupt_mask |= AR5K_INT_QCBRORN;
545 ah->ah_txq_isr |= AR5K_REG_MS(
546 ath5k_hw_reg_read(ah, AR5K_RAC_SISR3),
549 if (data & AR5K_ISR_QCBRURN) {
550 *interrupt_mask |= AR5K_INT_QCBRURN;
551 ah->ah_txq_isr |= AR5K_REG_MS(
552 ath5k_hw_reg_read(ah, AR5K_RAC_SISR3),
555 if (data & AR5K_ISR_QTRIG) {
556 *interrupt_mask |= AR5K_INT_QTRIG;
557 ah->ah_txq_isr |= AR5K_REG_MS(
558 ath5k_hw_reg_read(ah, AR5K_RAC_SISR4),
562 if (data & AR5K_ISR_TXOK)
563 ah->ah_txq_isr |= AR5K_REG_MS(
564 ath5k_hw_reg_read(ah, AR5K_RAC_SISR0),
565 AR5K_SISR0_QCU_TXOK);
567 if (data & AR5K_ISR_TXDESC)
568 ah->ah_txq_isr |= AR5K_REG_MS(
569 ath5k_hw_reg_read(ah, AR5K_RAC_SISR0),
570 AR5K_SISR0_QCU_TXDESC);
572 if (data & AR5K_ISR_TXERR)
573 ah->ah_txq_isr |= AR5K_REG_MS(
574 ath5k_hw_reg_read(ah, AR5K_RAC_SISR1),
575 AR5K_SISR1_QCU_TXERR);
577 if (data & AR5K_ISR_TXEOL)
578 ah->ah_txq_isr |= AR5K_REG_MS(
579 ath5k_hw_reg_read(ah, AR5K_RAC_SISR1),
580 AR5K_SISR1_QCU_TXEOL);
582 if (data & AR5K_ISR_TXURN)
583 ah->ah_txq_isr |= AR5K_REG_MS(
584 ath5k_hw_reg_read(ah, AR5K_RAC_SISR2),
585 AR5K_SISR2_QCU_TXURN);
587 if (unlikely(data & (AR5K_ISR_SSERR | AR5K_ISR_MCABT
588 | AR5K_ISR_HIUERR | AR5K_ISR_DPERR)))
589 *interrupt_mask |= AR5K_INT_FATAL;
592 * XXX: BMISS interrupts may occur after association.
593 * I found this on 5210 code but it needs testing. If this is
594 * true we should disable them before assoc and re-enable them
595 * after a successful assoc + some jiffies.
596 interrupt_mask &= ~AR5K_INT_BMISS;
601 * In case we didn't handle anything,
602 * print the register value.
604 if (unlikely(*interrupt_mask == 0 && net_ratelimit()))
605 ATH5K_PRINTF("ISR: 0x%08x IMR: 0x%08x\n", data, ah->ah_imr);
611 * ath5k_hw_set_imr - Set interrupt mask
613 * @ah: The &struct ath5k_hw
614 * @new_mask: The new interrupt mask to be set
616 * Set the interrupt mask in hw to save interrupts. We do that by mapping
617 * ath5k_int bits to hw-specific bits to remove abstraction and writing
618 * Interrupt Mask Register.
620 enum ath5k_int ath5k_hw_set_imr(struct ath5k_hw *ah, enum ath5k_int new_mask)
622 enum ath5k_int old_mask, int_mask;
624 old_mask = ah->ah_imr;
627 * Disable card interrupts to prevent any race conditions
628 * (they will be re-enabled afterwards if AR5K_INT GLOBAL
629 * is set again on the new mask).
631 if (old_mask & AR5K_INT_GLOBAL) {
632 ath5k_hw_reg_write(ah, AR5K_IER_DISABLE, AR5K_IER);
633 ath5k_hw_reg_read(ah, AR5K_IER);
637 * Add additional, chipset-dependent interrupt mask flags
638 * and write them to the IMR (interrupt mask register).
640 int_mask = new_mask & AR5K_INT_COMMON;
642 if (ah->ah_version != AR5K_AR5210) {
643 /* Preserve per queue TXURN interrupt mask */
644 u32 simr2 = ath5k_hw_reg_read(ah, AR5K_SIMR2)
645 & AR5K_SIMR2_QCU_TXURN;
647 if (new_mask & AR5K_INT_FATAL) {
648 int_mask |= AR5K_IMR_HIUERR;
649 simr2 |= (AR5K_SIMR2_MCABT | AR5K_SIMR2_SSERR
654 if (new_mask & AR5K_INT_BNR)
655 int_mask |= AR5K_INT_BNR;
657 if (new_mask & AR5K_INT_TIM)
658 int_mask |= AR5K_IMR_TIM;
660 if (new_mask & AR5K_INT_TIM)
661 simr2 |= AR5K_SISR2_TIM;
662 if (new_mask & AR5K_INT_DTIM)
663 simr2 |= AR5K_SISR2_DTIM;
664 if (new_mask & AR5K_INT_DTIM_SYNC)
665 simr2 |= AR5K_SISR2_DTIM_SYNC;
666 if (new_mask & AR5K_INT_BCN_TIMEOUT)
667 simr2 |= AR5K_SISR2_BCN_TIMEOUT;
668 if (new_mask & AR5K_INT_CAB_TIMEOUT)
669 simr2 |= AR5K_SISR2_CAB_TIMEOUT;
671 if (new_mask & AR5K_INT_RX_DOPPLER)
672 int_mask |= AR5K_IMR_RXDOPPLER;
674 /* Note: Per queue interrupt masks
675 * are set via reset_tx_queue (qcu.c) */
676 ath5k_hw_reg_write(ah, int_mask, AR5K_PIMR);
677 ath5k_hw_reg_write(ah, simr2, AR5K_SIMR2);
680 if (new_mask & AR5K_INT_FATAL)
681 int_mask |= (AR5K_IMR_SSERR | AR5K_IMR_MCABT
682 | AR5K_IMR_HIUERR | AR5K_IMR_DPERR);
684 ath5k_hw_reg_write(ah, int_mask, AR5K_IMR);
687 /* If RXNOFRM interrupt is masked disable it
688 * by setting AR5K_RXNOFRM to zero */
689 if (!(new_mask & AR5K_INT_RXNOFRM))
690 ath5k_hw_reg_write(ah, 0, AR5K_RXNOFRM);
692 /* Store new interrupt mask */
693 ah->ah_imr = new_mask;
695 /* ..re-enable interrupts if AR5K_INT_GLOBAL is set */
696 if (new_mask & AR5K_INT_GLOBAL) {
697 ath5k_hw_reg_write(ah, AR5K_IER_ENABLE, AR5K_IER);
698 ath5k_hw_reg_read(ah, AR5K_IER);