source: mainline/uspace/drv/intctl/i8259/i8259.c

Last change on this file was fafb8e5, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 years ago

Mechanically lowercase IPC_SET_*/IPC_GET_*

  • Property mode set to 100644
File size: 4.7 KB
RevLine 
[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
[acc7ce4]38#include <ipc/irc.h>
[9a2eb14]39#include <loc.h>
[42742c5a]40#include <sysinfo.h>
41#include <as.h>
[b446b02]42#include <ddf/log.h>
[42742c5a]43#include <ddi.h>
44#include <align.h>
[3e6a98c5]45#include <stdbool.h>
[42742c5a]46#include <errno.h>
[c1694b6b]47#include <str_error.h>
[42742c5a]48#include <async.h>
49#include <stdio.h>
50
[b446b02]51#include "i8259.h"
52
[acc7ce4]53#define NAME "i8259"
54
55#define IO_RANGE0_SIZE 2
56#define IO_RANGE1_SIZE 2
[42742c5a]57
[acc7ce4]58#define PIC_PIC0PORT1 0
59#define PIC_PIC0PORT2 1
[42742c5a]60
[acc7ce4]61#define PIC_PIC1PORT1 0
62#define PIC_PIC1PORT2 1
[42742c5a]63
[acc7ce4]64#define PIC_MAX_IRQ 15
65
[b7fd2a0]66static errno_t pic_enable_irq(i8259_t *i8259, sysarg_t irq)
[acc7ce4]67{
68 if (irq > PIC_MAX_IRQ)
69 return ENOENT;
[a35b458]70
[acc7ce4]71 uint16_t irqmask = 1 << irq;
72 uint8_t val;
[a35b458]73
[acc7ce4]74 if (irqmask & 0xff) {
[b446b02]75 val = pio_read_8(i8259->regs0 + PIC_PIC0PORT2);
76 pio_write_8(i8259->regs0 + PIC_PIC0PORT2,
[acc7ce4]77 (uint8_t) (val & (~(irqmask & 0xff))));
78 }
[a35b458]79
[acc7ce4]80 if (irqmask >> 8) {
[b446b02]81 val = pio_read_8(i8259->regs1 + PIC_PIC1PORT2);
82 pio_write_8(i8259->regs1 + PIC_PIC1PORT2,
[acc7ce4]83 (uint8_t) (val & (~(irqmask >> 8))));
84 }
[a35b458]85
[acc7ce4]86 return EOK;
87}
[42742c5a]88
[acc7ce4]89/** Handle one connection to i8259.
90 *
91 * @param icall Call data of the request that opened the connection.
[984a9ba]92 * @param arg Local argument.
93 *
[42742c5a]94 */
[984a9ba]95static void i8259_connection(ipc_call_t *icall, void *arg)
[42742c5a]96{
97 ipc_call_t call;
[b446b02]98 i8259_t *i8259 = NULL /* XXX */;
[a35b458]99
[42742c5a]100 /*
101 * Answer the first IPC_M_CONNECT_ME_TO call.
102 */
[beb83c1]103 async_accept_0(icall);
[a35b458]104
[984a9ba]105 i8259 = (i8259_t *) ddf_dev_data_get(ddf_fun_get_dev((ddf_fun_t *) arg));
[a35b458]106
[acc7ce4]107 while (true) {
[984a9ba]108 async_get_call(&call);
[a35b458]109
[fafb8e5]110 if (!ipc_get_imethod(&call)) {
[79ae36dd]111 /* The other side has hung up. */
[984a9ba]112 async_answer_0(&call, EOK);
[79ae36dd]113 return;
114 }
[a35b458]115
[fafb8e5]116 switch (ipc_get_imethod(&call)) {
[acc7ce4]117 case IRC_ENABLE_INTERRUPT:
[984a9ba]118 async_answer_0(&call, pic_enable_irq(i8259,
[fafb8e5]119 ipc_get_arg1(&call)));
[acc7ce4]120 break;
[07cb0108]121 case IRC_DISABLE_INTERRUPT:
122 /* XXX TODO */
[984a9ba]123 async_answer_0(&call, EOK);
[07cb0108]124 break;
[acc7ce4]125 case IRC_CLEAR_INTERRUPT:
126 /* Noop */
[984a9ba]127 async_answer_0(&call, EOK);
[42742c5a]128 break;
129 default:
[984a9ba]130 async_answer_0(&call, EINVAL);
[42742c5a]131 break;
132 }
133 }
134}
135
[b446b02]136/** Add i8259 device. */
[b7fd2a0]137errno_t i8259_add(i8259_t *i8259, i8259_res_t *res)
[42742c5a]138{
[b446b02]139 sysarg_t have_i8259;
140 ioport8_t *regs0;
141 ioport8_t *regs1;
142 ddf_fun_t *fun_a = NULL;
[b7fd2a0]143 errno_t rc;
[4f87a85a]144 bool bound = false;
[a35b458]145
[b446b02]146 if ((sysinfo_get_value("i8259", &have_i8259) != EOK) || (!have_i8259)) {
[79ae36dd]147 printf("%s: No i8259 found\n", NAME);
[b446b02]148 return ENOTSUP;
[42742c5a]149 }
[a35b458]150
[b446b02]151 if ((pio_enable((void *) res->base0, IO_RANGE0_SIZE,
152 (void **) &regs0) != EOK) ||
153 (pio_enable((void *) res->base1, IO_RANGE1_SIZE,
154 (void **) &regs1) != EOK)) {
[79ae36dd]155 printf("%s: i8259 not accessible\n", NAME);
[b446b02]156 return EIO;
[42742c5a]157 }
[a35b458]158
[b446b02]159 i8259->regs0 = regs0;
160 i8259->regs1 = regs1;
[a35b458]161
[b446b02]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;
[9a2eb14]167 }
[a35b458]168
[b446b02]169 ddf_fun_set_conn_handler(fun_a, i8259_connection);
[a35b458]170
[b446b02]171 rc = ddf_fun_bind(fun_a);
[9a2eb14]172 if (rc != EOK) {
[c1694b6b]173 ddf_msg(LVL_ERROR, "Failed binding function 'a': %s", str_error(rc));
[b446b02]174 goto error;
[9a2eb14]175 }
[a35b458]176
[4f87a85a]177 bound = true;
178
[b446b02]179 rc = ddf_fun_add_to_category(fun_a, "irc");
180 if (rc != EOK)
181 goto error;
[a35b458]182
[b446b02]183 return EOK;
184error:
[4f87a85a]185 if (bound)
186 ddf_fun_unbind(fun_a);
[b446b02]187 if (fun_a != NULL)
188 ddf_fun_destroy(fun_a);
189 return rc;
[42742c5a]190}
191
[b446b02]192/** Remove i8259 device */
[b7fd2a0]193errno_t i8259_remove(i8259_t *i8259)
[42742c5a]194{
[b446b02]195 return ENOTSUP;
196}
197
198/** i8259 device gone */
[b7fd2a0]199errno_t i8259_gone(i8259_t *i8259)
[b446b02]200{
201 return ENOTSUP;
[42742c5a]202}
203
204/**
205 * @}
[acc7ce4]206 */
Note: See TracBrowser for help on using the repository browser.