blob: 003fe0abb3527ce00563243bf2ce59a8703bbb08 [file] [log] [blame]
Kalle Valobdcd8172011-07-18 00:22:30 +03001/*
2 * Copyright (c) 2007-2011 Atheros Communications Inc.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include "core.h"
18#include "target.h"
19#include "hif-ops.h"
20#include "htc_hif.h"
21#include "debug.h"
22
23#define MAILBOX_FOR_BLOCK_SIZE 1
24
25#define ATH6KL_TIME_QUANTUM 10 /* in ms */
26
Kalle Valobdcd8172011-07-18 00:22:30 +030027static int ath6kldev_cp_scat_dma_buf(struct hif_scatter_req *req, bool from_dma)
28{
29 u8 *buf;
30 int i;
31
32 buf = req->virt_dma_buf;
33
34 for (i = 0; i < req->scat_entries; i++) {
35
36 if (from_dma)
37 memcpy(req->scat_list[i].buf, buf,
38 req->scat_list[i].len);
39 else
40 memcpy(buf, req->scat_list[i].buf,
41 req->scat_list[i].len);
42
43 buf += req->scat_list[i].len;
44 }
45
46 return 0;
47}
48
49int ath6kldev_rw_comp_handler(void *context, int status)
50{
51 struct htc_packet *packet = context;
52
53 ath6kl_dbg(ATH6KL_DBG_HTC_RECV,
54 "ath6kldev_rw_comp_handler (pkt:0x%p , status: %d\n",
55 packet, status);
56
57 packet->status = status;
58 packet->completion(packet->context, packet);
59
60 return 0;
61}
62
63static int ath6kldev_proc_dbg_intr(struct ath6kl_device *dev)
64{
65 u32 dummy;
66 int status;
67
68 ath6kl_err("target debug interrupt\n");
69
70 ath6kl_target_failure(dev->ar);
71
72 /*
73 * read counter to clear the interrupt, the debug error interrupt is
74 * counter 0.
75 */
76 status = hif_read_write_sync(dev->ar, COUNT_DEC_ADDRESS,
77 (u8 *)&dummy, 4, HIF_RD_SYNC_BYTE_INC);
78 if (status)
79 WARN_ON(1);
80
81 return status;
82}
83
84/* mailbox recv message polling */
85int ath6kldev_poll_mboxmsg_rx(struct ath6kl_device *dev, u32 *lk_ahd,
86 int timeout)
87{
88 struct ath6kl_irq_proc_registers *rg;
89 int status = 0, i;
90 u8 htc_mbox = 1 << HTC_MAILBOX;
91
92 for (i = timeout / ATH6KL_TIME_QUANTUM; i > 0; i--) {
93 /* this is the standard HIF way, load the reg table */
94 status = hif_read_write_sync(dev->ar, HOST_INT_STATUS_ADDRESS,
95 (u8 *) &dev->irq_proc_reg,
96 sizeof(dev->irq_proc_reg),
97 HIF_RD_SYNC_BYTE_INC);
98
99 if (status) {
100 ath6kl_err("failed to read reg table\n");
101 return status;
102 }
103
104 /* check for MBOX data and valid lookahead */
105 if (dev->irq_proc_reg.host_int_status & htc_mbox) {
106 if (dev->irq_proc_reg.rx_lkahd_valid &
107 htc_mbox) {
108 /*
109 * Mailbox has a message and the look ahead
110 * is valid.
111 */
112 rg = &dev->irq_proc_reg;
113 *lk_ahd =
114 le32_to_cpu(rg->rx_lkahd[HTC_MAILBOX]);
115 break;
116 }
117 }
118
119 /* delay a little */
120 mdelay(ATH6KL_TIME_QUANTUM);
121 ath6kl_dbg(ATH6KL_DBG_HTC_RECV, "retry mbox poll : %d\n", i);
122 }
123
124 if (i == 0) {
125 ath6kl_err("timeout waiting for recv message\n");
126 status = -ETIME;
127 /* check if the target asserted */
128 if (dev->irq_proc_reg.counter_int_status &
129 ATH6KL_TARGET_DEBUG_INTR_MASK)
130 /*
131 * Target failure handler will be called in case of
132 * an assert.
133 */
134 ath6kldev_proc_dbg_intr(dev);
135 }
136
137 return status;
138}
139
140/*
141 * Disable packet reception (used in case the host runs out of buffers)
142 * using the interrupt enable registers through the host I/F
143 */
144int ath6kldev_rx_control(struct ath6kl_device *dev, bool enable_rx)
145{
146 struct ath6kl_irq_enable_reg regs;
147 int status = 0;
148
149 /* take the lock to protect interrupt enable shadows */
150 spin_lock_bh(&dev->lock);
151
152 if (enable_rx)
153 dev->irq_en_reg.int_status_en |=
154 SM(INT_STATUS_ENABLE_MBOX_DATA, 0x01);
155 else
156 dev->irq_en_reg.int_status_en &=
157 ~SM(INT_STATUS_ENABLE_MBOX_DATA, 0x01);
158
159 memcpy(&regs, &dev->irq_en_reg, sizeof(regs));
160
161 spin_unlock_bh(&dev->lock);
162
163 status = hif_read_write_sync(dev->ar, INT_STATUS_ENABLE_ADDRESS,
164 &regs.int_status_en,
165 sizeof(struct ath6kl_irq_enable_reg),
166 HIF_WR_SYNC_BYTE_INC);
167
168 return status;
169}
170
Kalle Valobdcd8172011-07-18 00:22:30 +0300171int ath6kldev_submit_scat_req(struct ath6kl_device *dev,
172 struct hif_scatter_req *scat_req, bool read)
173{
174 int status = 0;
175
176 if (read) {
177 scat_req->req = HIF_RD_SYNC_BLOCK_FIX;
178 scat_req->addr = dev->ar->mbox_info.htc_addr;
179 } else {
180 scat_req->req = HIF_WR_ASYNC_BLOCK_INC;
181
182 scat_req->addr =
183 (scat_req->len > HIF_MBOX_WIDTH) ?
184 dev->ar->mbox_info.htc_ext_addr :
185 dev->ar->mbox_info.htc_addr;
186 }
187
188 ath6kl_dbg((ATH6KL_DBG_HTC_RECV | ATH6KL_DBG_HTC_SEND),
189 "ath6kldev_submit_scat_req, entries: %d, total len: %d mbox:0x%X (mode: %s : %s)\n",
190 scat_req->scat_entries, scat_req->len,
191 scat_req->addr, !read ? "async" : "sync",
192 (read) ? "rd" : "wr");
193
Vasanthakumar Thiagarajan4a005c32011-07-16 20:29:15 +0530194 if (!read && scat_req->virt_scat)
Kalle Valobdcd8172011-07-18 00:22:30 +0300195 status = ath6kldev_cp_scat_dma_buf(scat_req, false);
196
197 if (status) {
198 if (!read) {
199 scat_req->status = status;
Vasanthakumar Thiagarajane041c7f2011-07-16 20:29:09 +0530200 scat_req->complete(dev->ar->htc_target, scat_req);
Kalle Valobdcd8172011-07-18 00:22:30 +0300201 return 0;
202 }
203 return status;
204 }
205
Vasanthakumar Thiagarajan348a8fb2011-07-16 20:29:17 +0530206 status = ath6kl_hif_scat_req_rw(dev->ar, scat_req);
Kalle Valobdcd8172011-07-18 00:22:30 +0300207
208 if (read) {
209 /* in sync mode, we can touch the scatter request */
210 scat_req->status = status;
Vasanthakumar Thiagarajan4a005c32011-07-16 20:29:15 +0530211 if (!status && scat_req->virt_scat)
Kalle Valobdcd8172011-07-18 00:22:30 +0300212 scat_req->status =
213 ath6kldev_cp_scat_dma_buf(scat_req, true);
214 }
215
216 return status;
217}
218
Kalle Valobdcd8172011-07-18 00:22:30 +0300219static int ath6kldev_proc_counter_intr(struct ath6kl_device *dev)
220{
221 u8 counter_int_status;
222
223 ath6kl_dbg(ATH6KL_DBG_IRQ, "counter interrupt\n");
224
225 counter_int_status = dev->irq_proc_reg.counter_int_status &
226 dev->irq_en_reg.cntr_int_status_en;
227
228 ath6kl_dbg(ATH6KL_DBG_IRQ,
229 "valid interrupt source(s) in COUNTER_INT_STATUS: 0x%x\n",
230 counter_int_status);
231
232 /*
233 * NOTE: other modules like GMBOX may use the counter interrupt for
234 * credit flow control on other counters, we only need to check for
235 * the debug assertion counter interrupt.
236 */
237 if (counter_int_status & ATH6KL_TARGET_DEBUG_INTR_MASK)
238 return ath6kldev_proc_dbg_intr(dev);
239
240 return 0;
241}
242
243static int ath6kldev_proc_err_intr(struct ath6kl_device *dev)
244{
245 int status;
246 u8 error_int_status;
247 u8 reg_buf[4];
248
249 ath6kl_dbg(ATH6KL_DBG_IRQ, "error interrupt\n");
250
251 error_int_status = dev->irq_proc_reg.error_int_status & 0x0F;
252 if (!error_int_status) {
253 WARN_ON(1);
254 return -EIO;
255 }
256
257 ath6kl_dbg(ATH6KL_DBG_IRQ,
258 "valid interrupt source(s) in ERROR_INT_STATUS: 0x%x\n",
259 error_int_status);
260
261 if (MS(ERROR_INT_STATUS_WAKEUP, error_int_status))
262 ath6kl_dbg(ATH6KL_DBG_IRQ, "error : wakeup\n");
263
264 if (MS(ERROR_INT_STATUS_RX_UNDERFLOW, error_int_status))
265 ath6kl_err("rx underflow\n");
266
267 if (MS(ERROR_INT_STATUS_TX_OVERFLOW, error_int_status))
268 ath6kl_err("tx overflow\n");
269
270 /* Clear the interrupt */
271 dev->irq_proc_reg.error_int_status &= ~error_int_status;
272
273 /* set W1C value to clear the interrupt, this hits the register first */
274 reg_buf[0] = error_int_status;
275 reg_buf[1] = 0;
276 reg_buf[2] = 0;
277 reg_buf[3] = 0;
278
279 status = hif_read_write_sync(dev->ar, ERROR_INT_STATUS_ADDRESS,
280 reg_buf, 4, HIF_WR_SYNC_BYTE_FIX);
281
282 if (status)
283 WARN_ON(1);
284
285 return status;
286}
287
288static int ath6kldev_proc_cpu_intr(struct ath6kl_device *dev)
289{
290 int status;
291 u8 cpu_int_status;
292 u8 reg_buf[4];
293
294 ath6kl_dbg(ATH6KL_DBG_IRQ, "cpu interrupt\n");
295
296 cpu_int_status = dev->irq_proc_reg.cpu_int_status &
297 dev->irq_en_reg.cpu_int_status_en;
298 if (!cpu_int_status) {
299 WARN_ON(1);
300 return -EIO;
301 }
302
303 ath6kl_dbg(ATH6KL_DBG_IRQ,
304 "valid interrupt source(s) in CPU_INT_STATUS: 0x%x\n",
305 cpu_int_status);
306
307 /* Clear the interrupt */
308 dev->irq_proc_reg.cpu_int_status &= ~cpu_int_status;
309
310 /*
311 * Set up the register transfer buffer to hit the register 4 times ,
312 * this is done to make the access 4-byte aligned to mitigate issues
313 * with host bus interconnects that restrict bus transfer lengths to
314 * be a multiple of 4-bytes.
315 */
316
317 /* set W1C value to clear the interrupt, this hits the register first */
318 reg_buf[0] = cpu_int_status;
319 /* the remaining are set to zero which have no-effect */
320 reg_buf[1] = 0;
321 reg_buf[2] = 0;
322 reg_buf[3] = 0;
323
324 status = hif_read_write_sync(dev->ar, CPU_INT_STATUS_ADDRESS,
325 reg_buf, 4, HIF_WR_SYNC_BYTE_FIX);
326
327 if (status)
328 WARN_ON(1);
329
330 return status;
331}
332
333/* process pending interrupts synchronously */
334static int proc_pending_irqs(struct ath6kl_device *dev, bool *done)
335{
336 struct ath6kl_irq_proc_registers *rg;
337 int status = 0;
338 u8 host_int_status = 0;
339 u32 lk_ahd = 0;
340 u8 htc_mbox = 1 << HTC_MAILBOX;
341
342 ath6kl_dbg(ATH6KL_DBG_IRQ, "proc_pending_irqs: (dev: 0x%p)\n", dev);
343
344 /*
345 * NOTE: HIF implementation guarantees that the context of this
346 * call allows us to perform SYNCHRONOUS I/O, that is we can block,
347 * sleep or call any API that can block or switch thread/task
348 * contexts. This is a fully schedulable context.
349 */
350
351 /*
352 * Process pending intr only when int_status_en is clear, it may
353 * result in unnecessary bus transaction otherwise. Target may be
354 * unresponsive at the time.
355 */
356 if (dev->irq_en_reg.int_status_en) {
357 /*
358 * Read the first 28 bytes of the HTC register table. This
359 * will yield us the value of different int status
360 * registers and the lookahead registers.
361 *
362 * length = sizeof(int_status) + sizeof(cpu_int_status)
363 * + sizeof(error_int_status) +
364 * sizeof(counter_int_status) +
365 * sizeof(mbox_frame) + sizeof(rx_lkahd_valid)
366 * + sizeof(hole) + sizeof(rx_lkahd) +
367 * sizeof(int_status_en) +
368 * sizeof(cpu_int_status_en) +
369 * sizeof(err_int_status_en) +
370 * sizeof(cntr_int_status_en);
371 */
372 status = hif_read_write_sync(dev->ar, HOST_INT_STATUS_ADDRESS,
373 (u8 *) &dev->irq_proc_reg,
374 sizeof(dev->irq_proc_reg),
375 HIF_RD_SYNC_BYTE_INC);
376 if (status)
377 goto out;
378
379 if (AR_DBG_LVL_CHECK(ATH6KL_DBG_IRQ))
380 ath6kl_dump_registers(dev, &dev->irq_proc_reg,
381 &dev->irq_en_reg);
382
383 /* Update only those registers that are enabled */
384 host_int_status = dev->irq_proc_reg.host_int_status &
385 dev->irq_en_reg.int_status_en;
386
387 /* Look at mbox status */
388 if (host_int_status & htc_mbox) {
389 /*
390 * Mask out pending mbox value, we use "lookAhead as
391 * the real flag for mbox processing.
392 */
393 host_int_status &= ~htc_mbox;
394 if (dev->irq_proc_reg.rx_lkahd_valid &
395 htc_mbox) {
396 rg = &dev->irq_proc_reg;
397 lk_ahd = le32_to_cpu(rg->rx_lkahd[HTC_MAILBOX]);
398 if (!lk_ahd)
399 ath6kl_err("lookAhead is zero!\n");
400 }
401 }
402 }
403
404 if (!host_int_status && !lk_ahd) {
405 *done = true;
406 goto out;
407 }
408
409 if (lk_ahd) {
410 int fetched = 0;
411
412 ath6kl_dbg(ATH6KL_DBG_IRQ,
413 "pending mailbox msg, lk_ahd: 0x%X\n", lk_ahd);
414 /*
415 * Mailbox Interrupt, the HTC layer may issue async
416 * requests to empty the mailbox. When emptying the recv
417 * mailbox we use the async handler above called from the
418 * completion routine of the callers read request. This can
419 * improve performance by reducing context switching when
420 * we rapidly pull packets.
421 */
422 status = dev->msg_pending(dev->htc_cnxt, &lk_ahd, &fetched);
423 if (status)
424 goto out;
425
426 if (!fetched)
427 /*
428 * HTC could not pull any messages out due to lack
429 * of resources.
430 */
431 dev->chk_irq_status_cnt = 0;
432 }
433
434 /* now handle the rest of them */
435 ath6kl_dbg(ATH6KL_DBG_IRQ,
436 "valid interrupt source(s) for other interrupts: 0x%x\n",
437 host_int_status);
438
439 if (MS(HOST_INT_STATUS_CPU, host_int_status)) {
440 /* CPU Interrupt */
441 status = ath6kldev_proc_cpu_intr(dev);
442 if (status)
443 goto out;
444 }
445
446 if (MS(HOST_INT_STATUS_ERROR, host_int_status)) {
447 /* Error Interrupt */
448 status = ath6kldev_proc_err_intr(dev);
449 if (status)
450 goto out;
451 }
452
453 if (MS(HOST_INT_STATUS_COUNTER, host_int_status))
454 /* Counter Interrupt */
455 status = ath6kldev_proc_counter_intr(dev);
456
457out:
458 /*
459 * An optimization to bypass reading the IRQ status registers
460 * unecessarily which can re-wake the target, if upper layers
461 * determine that we are in a low-throughput mode, we can rely on
462 * taking another interrupt rather than re-checking the status
463 * registers which can re-wake the target.
464 *
465 * NOTE : for host interfaces that makes use of detecting pending
466 * mbox messages at hif can not use this optimization due to
467 * possible side effects, SPI requires the host to drain all
468 * messages from the mailbox before exiting the ISR routine.
469 */
470
471 ath6kl_dbg(ATH6KL_DBG_IRQ,
472 "bypassing irq status re-check, forcing done\n");
473
474 *done = true;
475
476 ath6kl_dbg(ATH6KL_DBG_IRQ,
477 "proc_pending_irqs: (done:%d, status=%d\n", *done, status);
478
479 return status;
480}
481
482/* interrupt handler, kicks off all interrupt processing */
483int ath6kldev_intr_bh_handler(struct ath6kl *ar)
484{
485 struct ath6kl_device *dev = ar->htc_target->dev;
486 int status = 0;
487 bool done = false;
488
489 /*
490 * Reset counter used to flag a re-scan of IRQ status registers on
491 * the target.
492 */
493 dev->chk_irq_status_cnt = 0;
494
495 /*
496 * IRQ processing is synchronous, interrupt status registers can be
497 * re-read.
498 */
499 while (!done) {
500 status = proc_pending_irqs(dev, &done);
501 if (status)
502 break;
503 }
504
505 return status;
506}
507
508static int ath6kldev_enable_intrs(struct ath6kl_device *dev)
509{
510 struct ath6kl_irq_enable_reg regs;
511 int status;
512
513 spin_lock_bh(&dev->lock);
514
515 /* Enable all but ATH6KL CPU interrupts */
516 dev->irq_en_reg.int_status_en =
517 SM(INT_STATUS_ENABLE_ERROR, 0x01) |
518 SM(INT_STATUS_ENABLE_CPU, 0x01) |
519 SM(INT_STATUS_ENABLE_COUNTER, 0x01);
520
521 /*
522 * NOTE: There are some cases where HIF can do detection of
523 * pending mbox messages which is disabled now.
524 */
525 dev->irq_en_reg.int_status_en |= SM(INT_STATUS_ENABLE_MBOX_DATA, 0x01);
526
527 /* Set up the CPU Interrupt status Register */
528 dev->irq_en_reg.cpu_int_status_en = 0;
529
530 /* Set up the Error Interrupt status Register */
531 dev->irq_en_reg.err_int_status_en =
532 SM(ERROR_STATUS_ENABLE_RX_UNDERFLOW, 0x01) |
533 SM(ERROR_STATUS_ENABLE_TX_OVERFLOW, 0x1);
534
535 /*
536 * Enable Counter interrupt status register to get fatal errors for
537 * debugging.
538 */
539 dev->irq_en_reg.cntr_int_status_en = SM(COUNTER_INT_STATUS_ENABLE_BIT,
540 ATH6KL_TARGET_DEBUG_INTR_MASK);
541 memcpy(&regs, &dev->irq_en_reg, sizeof(regs));
542
543 spin_unlock_bh(&dev->lock);
544
545 status = hif_read_write_sync(dev->ar, INT_STATUS_ENABLE_ADDRESS,
546 &regs.int_status_en, sizeof(regs),
547 HIF_WR_SYNC_BYTE_INC);
548
549 if (status)
550 ath6kl_err("failed to update interrupt ctl reg err: %d\n",
551 status);
552
553 return status;
554}
555
556int ath6kldev_disable_intrs(struct ath6kl_device *dev)
557{
558 struct ath6kl_irq_enable_reg regs;
559
560 spin_lock_bh(&dev->lock);
561 /* Disable all interrupts */
562 dev->irq_en_reg.int_status_en = 0;
563 dev->irq_en_reg.cpu_int_status_en = 0;
564 dev->irq_en_reg.err_int_status_en = 0;
565 dev->irq_en_reg.cntr_int_status_en = 0;
566 memcpy(&regs, &dev->irq_en_reg, sizeof(regs));
567 spin_unlock_bh(&dev->lock);
568
569 return hif_read_write_sync(dev->ar, INT_STATUS_ENABLE_ADDRESS,
570 &regs.int_status_en, sizeof(regs),
571 HIF_WR_SYNC_BYTE_INC);
572}
573
574/* enable device interrupts */
575int ath6kldev_unmask_intrs(struct ath6kl_device *dev)
576{
577 int status = 0;
578
579 /*
580 * Make sure interrupt are disabled before unmasking at the HIF
581 * layer. The rationale here is that between device insertion
582 * (where we clear the interrupts the first time) and when HTC
583 * is finally ready to handle interrupts, other software can perform
584 * target "soft" resets. The ATH6KL interrupt enables reset back to an
585 * "enabled" state when this happens.
586 */
587 ath6kldev_disable_intrs(dev);
588
589 /* unmask the host controller interrupts */
590 ath6kl_hif_irq_enable(dev->ar);
591 status = ath6kldev_enable_intrs(dev);
592
593 return status;
594}
595
596/* disable all device interrupts */
597int ath6kldev_mask_intrs(struct ath6kl_device *dev)
598{
599 /*
600 * Mask the interrupt at the HIF layer to avoid any stray interrupt
601 * taken while we zero out our shadow registers in
602 * ath6kldev_disable_intrs().
603 */
604 ath6kl_hif_irq_disable(dev->ar);
605
606 return ath6kldev_disable_intrs(dev);
607}
608
609int ath6kldev_setup(struct ath6kl_device *dev)
610{
611 int status = 0;
Kalle Valobdcd8172011-07-18 00:22:30 +0300612
Kalle Valobdcd8172011-07-18 00:22:30 +0300613 spin_lock_init(&dev->lock);
614
Kalle Valobdcd8172011-07-18 00:22:30 +0300615 /*
616 * NOTE: we actually get the block size of a mailbox other than 0,
617 * for SDIO the block size on mailbox 0 is artificially set to 1.
618 * So we use the block size that is set for the other 3 mailboxes.
619 */
620 dev->block_sz = dev->ar->mbox_info.block_size;
621
622 /* must be a power of 2 */
623 if ((dev->block_sz & (dev->block_sz - 1)) != 0) {
624 WARN_ON(1);
625 goto fail_setup;
626 }
627
628 /* assemble mask, used for padding to a block */
629 dev->block_mask = dev->block_sz - 1;
630
631 ath6kl_dbg(ATH6KL_DBG_TRC, "block size: %d, mbox addr:0x%X\n",
632 dev->block_sz, dev->ar->mbox_info.htc_addr);
633
634 ath6kl_dbg(ATH6KL_DBG_TRC,
635 "hif interrupt processing is sync only\n");
636
637 status = ath6kldev_disable_intrs(dev);
638
639fail_setup:
640 return status;
641
642}