| [42742c5a] | 1 | /*
|
|---|
| [acc7ce4] | 2 | * Copyright (c) 2011 Martin Decky
|
|---|
| [42742c5a] | 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| [acc7ce4] | 29 | /** @addtogroup i8259
|
|---|
| [42742c5a] | 30 | * @{
|
|---|
| [acc7ce4] | 31 | */
|
|---|
| [42742c5a] | 32 |
|
|---|
| 33 | /**
|
|---|
| [acc7ce4] | 34 | * @file i8259.c
|
|---|
| 35 | * @brief i8259 driver.
|
|---|
| [42742c5a] | 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <ipc/ipc.h>
|
|---|
| 39 | #include <ipc/services.h>
|
|---|
| [acc7ce4] | 40 | #include <ipc/irc.h>
|
|---|
| [42742c5a] | 41 | #include <ipc/ns.h>
|
|---|
| 42 | #include <sysinfo.h>
|
|---|
| 43 | #include <as.h>
|
|---|
| 44 | #include <ddi.h>
|
|---|
| [acc7ce4] | 45 | #include <libarch/ddi.h>
|
|---|
| [42742c5a] | 46 | #include <align.h>
|
|---|
| 47 | #include <bool.h>
|
|---|
| 48 | #include <errno.h>
|
|---|
| 49 | #include <async.h>
|
|---|
| 50 | #include <align.h>
|
|---|
| 51 | #include <async.h>
|
|---|
| 52 | #include <stdio.h>
|
|---|
| 53 | #include <ipc/devmap.h>
|
|---|
| 54 |
|
|---|
| [acc7ce4] | 55 | #define NAME "i8259"
|
|---|
| 56 |
|
|---|
| 57 | #define IO_RANGE0_START ((ioport8_t *) 0x0020U)
|
|---|
| 58 | #define IO_RANGE0_SIZE 2
|
|---|
| [42742c5a] | 59 |
|
|---|
| [acc7ce4] | 60 | #define IO_RANGE1_START ((ioport8_t *) 0x00a0U)
|
|---|
| 61 | #define IO_RANGE1_SIZE 2
|
|---|
| [42742c5a] | 62 |
|
|---|
| [acc7ce4] | 63 | static ioport8_t *io_range0;
|
|---|
| 64 | static ioport8_t *io_range1;
|
|---|
| [42742c5a] | 65 |
|
|---|
| [acc7ce4] | 66 | #define PIC_PIC0PORT1 0
|
|---|
| 67 | #define PIC_PIC0PORT2 1
|
|---|
| [42742c5a] | 68 |
|
|---|
| [acc7ce4] | 69 | #define PIC_PIC1PORT1 0
|
|---|
| 70 | #define PIC_PIC1PORT2 1
|
|---|
| [42742c5a] | 71 |
|
|---|
| [acc7ce4] | 72 | #define PIC_MAX_IRQ 15
|
|---|
| 73 |
|
|---|
| 74 | static int pic_enable_irq(sysarg_t irq)
|
|---|
| 75 | {
|
|---|
| 76 | if (irq > PIC_MAX_IRQ)
|
|---|
| 77 | return ENOENT;
|
|---|
| 78 |
|
|---|
| 79 | uint16_t irqmask = 1 << irq;
|
|---|
| 80 | uint8_t val;
|
|---|
| 81 |
|
|---|
| 82 | if (irqmask & 0xff) {
|
|---|
| 83 | val = pio_read_8(io_range0 + PIC_PIC0PORT2);
|
|---|
| 84 | pio_write_8(io_range0 + PIC_PIC0PORT2,
|
|---|
| 85 | (uint8_t) (val & (~(irqmask & 0xff))));
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | if (irqmask >> 8) {
|
|---|
| 89 | val = pio_read_8(io_range1 + PIC_PIC1PORT2);
|
|---|
| 90 | pio_write_8(io_range1 + PIC_PIC1PORT2,
|
|---|
| 91 | (uint8_t) (val & (~(irqmask >> 8))));
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | return EOK;
|
|---|
| 95 | }
|
|---|
| [42742c5a] | 96 |
|
|---|
| [acc7ce4] | 97 | /** Handle one connection to i8259.
|
|---|
| 98 | *
|
|---|
| 99 | * @param iid Hash of the request that opened the connection.
|
|---|
| 100 | * @param icall Call data of the request that opened the connection.
|
|---|
| [42742c5a] | 101 | *
|
|---|
| 102 | */
|
|---|
| [acc7ce4] | 103 | static void i8259_connection(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| [42742c5a] | 104 | {
|
|---|
| 105 | ipc_callid_t callid;
|
|---|
| 106 | ipc_call_t call;
|
|---|
| [acc7ce4] | 107 |
|
|---|
| [42742c5a] | 108 | /*
|
|---|
| 109 | * Answer the first IPC_M_CONNECT_ME_TO call.
|
|---|
| 110 | */
|
|---|
| 111 | ipc_answer_0(iid, EOK);
|
|---|
| 112 |
|
|---|
| [acc7ce4] | 113 | while (true) {
|
|---|
| [42742c5a] | 114 | callid = async_get_call(&call);
|
|---|
| [acc7ce4] | 115 |
|
|---|
| [228e490] | 116 | switch (IPC_GET_IMETHOD(call)) {
|
|---|
| [acc7ce4] | 117 | case IRC_ENABLE_INTERRUPT:
|
|---|
| 118 | ipc_answer_0(callid, pic_enable_irq(IPC_GET_ARG1(call)));
|
|---|
| 119 | break;
|
|---|
| 120 | case IRC_CLEAR_INTERRUPT:
|
|---|
| 121 | /* Noop */
|
|---|
| [42742c5a] | 122 | ipc_answer_0(callid, EOK);
|
|---|
| 123 | break;
|
|---|
| 124 | default:
|
|---|
| 125 | ipc_answer_0(callid, EINVAL);
|
|---|
| 126 | break;
|
|---|
| 127 | }
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| [acc7ce4] | 131 | /** Initialize the i8259 driver.
|
|---|
| [42742c5a] | 132 | *
|
|---|
| 133 | */
|
|---|
| [acc7ce4] | 134 | static bool i8259_init(void)
|
|---|
| [42742c5a] | 135 | {
|
|---|
| [acc7ce4] | 136 | sysarg_t i8259;
|
|---|
| [42742c5a] | 137 |
|
|---|
| [acc7ce4] | 138 | if ((sysinfo_get_value("i8259", &i8259) != EOK) || (!i8259)) {
|
|---|
| 139 | printf(NAME ": No i8259 found\n");
|
|---|
| [42742c5a] | 140 | return false;
|
|---|
| 141 | }
|
|---|
| [d9fae235] | 142 |
|
|---|
| [acc7ce4] | 143 | if ((pio_enable((void *) IO_RANGE0_START, IO_RANGE0_SIZE,
|
|---|
| 144 | (void **) &io_range0) != EOK) ||
|
|---|
| 145 | (pio_enable((void *) IO_RANGE1_START, IO_RANGE1_SIZE,
|
|---|
| 146 | (void **) &io_range1) != EOK)) {
|
|---|
| 147 | printf(NAME ": i8259 not accessible\n");
|
|---|
| [42742c5a] | 148 | return false;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| [acc7ce4] | 151 | async_set_client_connection(i8259_connection);
|
|---|
| [96b02eb9] | 152 | sysarg_t phonead;
|
|---|
| [acc7ce4] | 153 | ipc_connect_to_me(PHONE_NS, SERVICE_I8259, 0, 0, &phonead);
|
|---|
| [42742c5a] | 154 |
|
|---|
| 155 | return true;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | int main(int argc, char **argv)
|
|---|
| 159 | {
|
|---|
| [acc7ce4] | 160 | printf(NAME ": HelenOS i8259 driver\n");
|
|---|
| [42742c5a] | 161 |
|
|---|
| [acc7ce4] | 162 | if (!i8259_init())
|
|---|
| [42742c5a] | 163 | return -1;
|
|---|
| 164 |
|
|---|
| 165 | printf(NAME ": Accepting connections\n");
|
|---|
| 166 | async_manager();
|
|---|
| [acc7ce4] | 167 |
|
|---|
| [42742c5a] | 168 | /* Never reached */
|
|---|
| 169 | return 0;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | /**
|
|---|
| 173 | * @}
|
|---|
| [acc7ce4] | 174 | */
|
|---|