[67bcd81] | 1 | /*
|
---|
| 2 | * Copyright (c) 2019 Jiri Svoboda
|
---|
| 3 | * Copyright (c) 2012 Jan Vesely
|
---|
| 4 | * Copyright (c) 2013 Beniamino Galvani
|
---|
| 5 | * All rights reserved.
|
---|
| 6 | *
|
---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
---|
| 8 | * modification, are permitted provided that the following conditions
|
---|
| 9 | * are met:
|
---|
| 10 | *
|
---|
| 11 | * - Redistributions of source code must retain the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
---|
| 13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 15 | * documentation and/or other materials provided with the distribution.
|
---|
| 16 | * - The name of the author may not be used to endorse or promote products
|
---|
| 17 | * derived from this software without specific prior written permission.
|
---|
| 18 | *
|
---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 29 | */
|
---|
| 30 | /** @addtogroup kernel_genarch
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | * @brief Broadcom BCM2835 on-chip interrupt controller driver.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <assert.h>
|
---|
| 39 | #include <genarch/drivers/bcm2835/irc.h>
|
---|
| 40 | #include <stdio.h>
|
---|
| 41 |
|
---|
| 42 | unsigned shortcut_inums[] = { 7, 9, 10, 18, 19, 53, 54, 55, 56, 57, 62 };
|
---|
| 43 |
|
---|
| 44 | static void bcm2835_irc_dump(bcm2835_irc_t *regs)
|
---|
| 45 | {
|
---|
| 46 | #define DUMP_REG(name) \
|
---|
| 47 | printf("%s : %08x\n", #name, regs->name);
|
---|
| 48 |
|
---|
| 49 | DUMP_REG(irq_basic_pending);
|
---|
| 50 | DUMP_REG(irq_pending1);
|
---|
| 51 | DUMP_REG(irq_pending2);
|
---|
| 52 | DUMP_REG(fiq_control);
|
---|
| 53 |
|
---|
| 54 | for (int i = 0; i < 3; ++i) {
|
---|
| 55 | DUMP_REG(irq_enable[i]);
|
---|
| 56 | DUMP_REG(irq_disable[i]);
|
---|
| 57 | }
|
---|
| 58 | #undef DUMP_REG
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | void bcm2835_irc_init(bcm2835_irc_t *regs)
|
---|
| 62 | {
|
---|
| 63 | /* Disable all interrupts */
|
---|
| 64 | regs->irq_disable[BANK_GPU0] = 0xffffffff;
|
---|
| 65 | regs->irq_disable[BANK_GPU1] = 0xffffffff;
|
---|
| 66 | regs->irq_disable[BANK_ARM] = 0xffffffff;
|
---|
| 67 |
|
---|
| 68 | /* Disable FIQ generation */
|
---|
| 69 | regs->fiq_control = 0;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | static int ffs(unsigned int x)
|
---|
| 73 | {
|
---|
| 74 | int ret;
|
---|
| 75 |
|
---|
| 76 | asm volatile (
|
---|
| 77 | "clz r0, %[x]\n"
|
---|
| 78 | "rsb %[ret], r0, #32\n"
|
---|
| 79 | : [ret] "=r" (ret)
|
---|
| 80 | : [x] "r" (x)
|
---|
| 81 | : "r0"
|
---|
| 82 | );
|
---|
| 83 |
|
---|
| 84 | return ret;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | unsigned bcm2835_irc_inum_get(bcm2835_irc_t *regs)
|
---|
| 88 | {
|
---|
| 89 | uint32_t pending;
|
---|
| 90 | int inum = -1;
|
---|
| 91 |
|
---|
| 92 | pending = regs->irq_basic_pending;
|
---|
| 93 |
|
---|
| 94 | /*
|
---|
| 95 | * The basic pending register shows interrupts pending from ARM
|
---|
| 96 | * peripherals and it also contains, in order to speed up processing,
|
---|
| 97 | * additional information about pending GPU interrupts:
|
---|
| 98 | *
|
---|
| 99 | * - bits 0-7 are associated to ARM peripherals
|
---|
| 100 | * - bit 8 is 1 when at least one bit is set in pending register 1
|
---|
| 101 | * - bit 9 is 1 when at least one bit is set in pending register 2
|
---|
| 102 | * - bits 10-20 indicate pending status of selected GPU peripherals
|
---|
| 103 | *
|
---|
| 104 | * Reference: BCM2835 ARM Peripherals, p.113
|
---|
| 105 | */
|
---|
| 106 |
|
---|
| 107 | if (pending & IRQ_PEND_ARM_M) {
|
---|
| 108 | inum = MAKE_IRQ(BANK_ARM, ffs(pending & IRQ_PEND_ARM_M) - 1);
|
---|
| 109 | } else if (pending & IRQ_PEND_SHORT_M) {
|
---|
| 110 | int pos = (pending & IRQ_PEND_SHORT_M) >> IRQ_PEND_SHORT_S;
|
---|
| 111 | inum = shortcut_inums[ffs(pos) - 1];
|
---|
| 112 | } else if (pending & IRQ_PEND_GPU0_M) {
|
---|
| 113 | inum = MAKE_IRQ(BANK_GPU0, ffs(regs->irq_pending1) - 1);
|
---|
| 114 | } else if (pending & IRQ_PEND_GPU1_M) {
|
---|
| 115 | inum = MAKE_IRQ(BANK_GPU1, ffs(regs->irq_pending2) - 1);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | if (inum < 0) {
|
---|
| 119 | printf("Spurious interrupt!\n");
|
---|
| 120 | bcm2835_irc_dump(regs);
|
---|
| 121 | inum = 0;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | return inum;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | void bcm2835_irc_enable(bcm2835_irc_t *regs, unsigned inum)
|
---|
| 128 | {
|
---|
| 129 | assert(inum < BCM2835_IRQ_COUNT);
|
---|
| 130 | regs->irq_enable[IRQ_TO_BANK(inum)] |= (1 << IRQ_TO_NUM(inum));
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | void bcm2835_irc_disable(bcm2835_irc_t *regs, unsigned inum)
|
---|
| 134 | {
|
---|
| 135 | assert(inum < BCM2835_IRQ_COUNT);
|
---|
| 136 | regs->irq_disable[IRQ_TO_BANK(inum)] |= (1 << IRQ_TO_NUM(inum));
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | /**
|
---|
| 140 | * @}
|
---|
| 141 | */
|
---|