| 1 | /* | 
|---|
| 2 | * Copyright (c) 2011 Martin Decky | 
|---|
| 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 |  | 
|---|
| 29 | /** @addtogroup i8259 | 
|---|
| 30 | * @{ | 
|---|
| 31 | */ | 
|---|
| 32 |  | 
|---|
| 33 | /** | 
|---|
| 34 | * @file i8259.c | 
|---|
| 35 | * @brief i8259 driver. | 
|---|
| 36 | */ | 
|---|
| 37 |  | 
|---|
| 38 | #include <ipc/irc.h> | 
|---|
| 39 | #include <loc.h> | 
|---|
| 40 | #include <sysinfo.h> | 
|---|
| 41 | #include <as.h> | 
|---|
| 42 | #include <ddf/log.h> | 
|---|
| 43 | #include <ddi.h> | 
|---|
| 44 | #include <align.h> | 
|---|
| 45 | #include <stdbool.h> | 
|---|
| 46 | #include <errno.h> | 
|---|
| 47 | #include <str_error.h> | 
|---|
| 48 | #include <async.h> | 
|---|
| 49 | #include <stdio.h> | 
|---|
| 50 |  | 
|---|
| 51 | #include "i8259.h" | 
|---|
| 52 |  | 
|---|
| 53 | #define NAME  "i8259" | 
|---|
| 54 |  | 
|---|
| 55 | #define IO_RANGE0_SIZE   2 | 
|---|
| 56 | #define IO_RANGE1_SIZE   2 | 
|---|
| 57 |  | 
|---|
| 58 | #define PIC_PIC0PORT1  0 | 
|---|
| 59 | #define PIC_PIC0PORT2  1 | 
|---|
| 60 |  | 
|---|
| 61 | #define PIC_PIC1PORT1  0 | 
|---|
| 62 | #define PIC_PIC1PORT2  1 | 
|---|
| 63 |  | 
|---|
| 64 | #define PIC_MAX_IRQ  15 | 
|---|
| 65 |  | 
|---|
| 66 | static errno_t pic_enable_irq(i8259_t *i8259, sysarg_t irq) | 
|---|
| 67 | { | 
|---|
| 68 | if (irq > PIC_MAX_IRQ) | 
|---|
| 69 | return ENOENT; | 
|---|
| 70 |  | 
|---|
| 71 | uint16_t irqmask = 1 << irq; | 
|---|
| 72 | uint8_t val; | 
|---|
| 73 |  | 
|---|
| 74 | if (irqmask & 0xff) { | 
|---|
| 75 | val = pio_read_8(i8259->regs0 + PIC_PIC0PORT2); | 
|---|
| 76 | pio_write_8(i8259->regs0 + PIC_PIC0PORT2, | 
|---|
| 77 | (uint8_t) (val & (~(irqmask & 0xff)))); | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | if (irqmask >> 8) { | 
|---|
| 81 | val = pio_read_8(i8259->regs1 + PIC_PIC1PORT2); | 
|---|
| 82 | pio_write_8(i8259->regs1 + PIC_PIC1PORT2, | 
|---|
| 83 | (uint8_t) (val & (~(irqmask >> 8)))); | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | return EOK; | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | /** Handle one connection to i8259. | 
|---|
| 90 | * | 
|---|
| 91 | * @param iid   Hash of the request that opened the connection. | 
|---|
| 92 | * @param icall Call data of the request that opened the connection. | 
|---|
| 93 | * @param arg   Local argument. | 
|---|
| 94 | */ | 
|---|
| 95 | static void i8259_connection(cap_call_handle_t icall_handle, ipc_call_t *icall, void *arg) | 
|---|
| 96 | { | 
|---|
| 97 | cap_call_handle_t chandle; | 
|---|
| 98 | ipc_call_t call; | 
|---|
| 99 | i8259_t *i8259 = NULL /* XXX */; | 
|---|
| 100 |  | 
|---|
| 101 | /* | 
|---|
| 102 | * Answer the first IPC_M_CONNECT_ME_TO call. | 
|---|
| 103 | */ | 
|---|
| 104 | async_answer_0(icall_handle, EOK); | 
|---|
| 105 |  | 
|---|
| 106 | i8259 = (i8259_t *)ddf_dev_data_get(ddf_fun_get_dev((ddf_fun_t *)arg)); | 
|---|
| 107 |  | 
|---|
| 108 | while (true) { | 
|---|
| 109 | chandle = async_get_call(&call); | 
|---|
| 110 |  | 
|---|
| 111 | if (!IPC_GET_IMETHOD(call)) { | 
|---|
| 112 | /* The other side has hung up. */ | 
|---|
| 113 | async_answer_0(chandle, EOK); | 
|---|
| 114 | return; | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | switch (IPC_GET_IMETHOD(call)) { | 
|---|
| 118 | case IRC_ENABLE_INTERRUPT: | 
|---|
| 119 | async_answer_0(chandle, pic_enable_irq(i8259, | 
|---|
| 120 | IPC_GET_ARG1(call))); | 
|---|
| 121 | break; | 
|---|
| 122 | case IRC_DISABLE_INTERRUPT: | 
|---|
| 123 | /* XXX TODO */ | 
|---|
| 124 | async_answer_0(chandle, EOK); | 
|---|
| 125 | break; | 
|---|
| 126 | case IRC_CLEAR_INTERRUPT: | 
|---|
| 127 | /* Noop */ | 
|---|
| 128 | async_answer_0(chandle, EOK); | 
|---|
| 129 | break; | 
|---|
| 130 | default: | 
|---|
| 131 | async_answer_0(chandle, EINVAL); | 
|---|
| 132 | break; | 
|---|
| 133 | } | 
|---|
| 134 | } | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | /** Add i8259 device. */ | 
|---|
| 138 | errno_t i8259_add(i8259_t *i8259, i8259_res_t *res) | 
|---|
| 139 | { | 
|---|
| 140 | sysarg_t have_i8259; | 
|---|
| 141 | ioport8_t *regs0; | 
|---|
| 142 | ioport8_t *regs1; | 
|---|
| 143 | ddf_fun_t *fun_a = NULL; | 
|---|
| 144 | errno_t rc; | 
|---|
| 145 |  | 
|---|
| 146 | if ((sysinfo_get_value("i8259", &have_i8259) != EOK) || (!have_i8259)) { | 
|---|
| 147 | printf("%s: No i8259 found\n", NAME); | 
|---|
| 148 | return ENOTSUP; | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | if ((pio_enable((void *) res->base0, IO_RANGE0_SIZE, | 
|---|
| 152 | (void **) ®s0) != EOK) || | 
|---|
| 153 | (pio_enable((void *) res->base1, IO_RANGE1_SIZE, | 
|---|
| 154 | (void **) ®s1) != EOK)) { | 
|---|
| 155 | printf("%s: i8259 not accessible\n", NAME); | 
|---|
| 156 | return EIO; | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | i8259->regs0 = regs0; | 
|---|
| 160 | i8259->regs1 = regs1; | 
|---|
| 161 |  | 
|---|
| 162 | fun_a = ddf_fun_create(i8259->dev, fun_exposed, "a"); | 
|---|
| 163 | if (fun_a == NULL) { | 
|---|
| 164 | ddf_msg(LVL_ERROR, "Failed creating function 'a'."); | 
|---|
| 165 | rc = ENOMEM; | 
|---|
| 166 | goto error; | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | ddf_fun_set_conn_handler(fun_a, i8259_connection); | 
|---|
| 170 |  | 
|---|
| 171 | rc = ddf_fun_bind(fun_a); | 
|---|
| 172 | if (rc != EOK) { | 
|---|
| 173 | ddf_msg(LVL_ERROR, "Failed binding function 'a': %s", str_error(rc)); | 
|---|
| 174 | goto error; | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | rc = ddf_fun_add_to_category(fun_a, "irc"); | 
|---|
| 178 | if (rc != EOK) | 
|---|
| 179 | goto error; | 
|---|
| 180 |  | 
|---|
| 181 | return EOK; | 
|---|
| 182 | error: | 
|---|
| 183 | if (fun_a != NULL) | 
|---|
| 184 | ddf_fun_destroy(fun_a); | 
|---|
| 185 | return rc; | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | /** Remove i8259 device */ | 
|---|
| 189 | errno_t i8259_remove(i8259_t *i8259) | 
|---|
| 190 | { | 
|---|
| 191 | return ENOTSUP; | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | /** i8259 device gone */ | 
|---|
| 195 | errno_t i8259_gone(i8259_t *i8259) | 
|---|
| 196 | { | 
|---|
| 197 | return ENOTSUP; | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|
| 200 | /** | 
|---|
| 201 | * @} | 
|---|
| 202 | */ | 
|---|