| 1 | /*
|
|---|
| 2 | * Copyright (c) 2006 Josef Cejka
|
|---|
| 3 | * Copyright (c) 2011 Jiri Svoboda
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup kbd_port
|
|---|
| 31 | * @ingroup kbd
|
|---|
| 32 | * @{
|
|---|
| 33 | */
|
|---|
| 34 | /** @file
|
|---|
| 35 | * @brief NS16550 port driver.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <ipc/irc.h>
|
|---|
| 39 | #include <async.h>
|
|---|
| 40 | #include <async_obsolete.h>
|
|---|
| 41 | #include <sysinfo.h>
|
|---|
| 42 | #include <input.h>
|
|---|
| 43 | #include <kbd.h>
|
|---|
| 44 | #include <kbd_port.h>
|
|---|
| 45 | #include <ddi.h>
|
|---|
| 46 | #include <errno.h>
|
|---|
| 47 |
|
|---|
| 48 | static int ns16550_port_init(kbd_dev_t *);
|
|---|
| 49 | static void ns16550_port_yield(void);
|
|---|
| 50 | static void ns16550_port_reclaim(void);
|
|---|
| 51 | static void ns16550_port_write(uint8_t data);
|
|---|
| 52 |
|
|---|
| 53 | kbd_port_ops_t ns16550_port = {
|
|---|
| 54 | .init = ns16550_port_init,
|
|---|
| 55 | .yield = ns16550_port_yield,
|
|---|
| 56 | .reclaim = ns16550_port_reclaim,
|
|---|
| 57 | .write = ns16550_port_write
|
|---|
| 58 | };
|
|---|
| 59 |
|
|---|
| 60 | static kbd_dev_t *kbd_dev;
|
|---|
| 61 |
|
|---|
| 62 | /* NS16550 registers */
|
|---|
| 63 | #define RBR_REG 0 /** Receiver Buffer Register. */
|
|---|
| 64 | #define IER_REG 1 /** Interrupt Enable Register. */
|
|---|
| 65 | #define IIR_REG 2 /** Interrupt Ident Register (read). */
|
|---|
| 66 | #define FCR_REG 2 /** FIFO control register (write). */
|
|---|
| 67 | #define LCR_REG 3 /** Line Control register. */
|
|---|
| 68 | #define MCR_REG 4 /** Modem Control Register. */
|
|---|
| 69 | #define LSR_REG 5 /** Line Status Register. */
|
|---|
| 70 |
|
|---|
| 71 | #define LSR_DATA_READY 0x01
|
|---|
| 72 |
|
|---|
| 73 | static irq_cmd_t ns16550_cmds[] = {
|
|---|
| 74 | {
|
|---|
| 75 | .cmd = CMD_PIO_READ_8,
|
|---|
| 76 | .addr = (void *) 0, /* Will be patched in run-time */
|
|---|
| 77 | .dstarg = 1
|
|---|
| 78 | },
|
|---|
| 79 | {
|
|---|
| 80 | .cmd = CMD_BTEST,
|
|---|
| 81 | .value = LSR_DATA_READY,
|
|---|
| 82 | .srcarg = 1,
|
|---|
| 83 | .dstarg = 3
|
|---|
| 84 | },
|
|---|
| 85 | {
|
|---|
| 86 | .cmd = CMD_PREDICATE,
|
|---|
| 87 | .value = 2,
|
|---|
| 88 | .srcarg = 3
|
|---|
| 89 | },
|
|---|
| 90 | {
|
|---|
| 91 | .cmd = CMD_PIO_READ_8,
|
|---|
| 92 | .addr = (void *) 0, /* Will be patched in run-time */
|
|---|
| 93 | .dstarg = 2
|
|---|
| 94 | },
|
|---|
| 95 | {
|
|---|
| 96 | .cmd = CMD_ACCEPT
|
|---|
| 97 | }
|
|---|
| 98 | };
|
|---|
| 99 |
|
|---|
| 100 | irq_code_t ns16550_kbd = {
|
|---|
| 101 | sizeof(ns16550_cmds) / sizeof(irq_cmd_t),
|
|---|
| 102 | ns16550_cmds
|
|---|
| 103 | };
|
|---|
| 104 |
|
|---|
| 105 | static void ns16550_irq_handler(ipc_callid_t iid, ipc_call_t *call);
|
|---|
| 106 |
|
|---|
| 107 | static uintptr_t ns16550_physical;
|
|---|
| 108 | static uintptr_t ns16550_kernel;
|
|---|
| 109 |
|
|---|
| 110 | static kbd_dev_t *kbd_dev;
|
|---|
| 111 |
|
|---|
| 112 | static int ns16550_port_init(kbd_dev_t *kdev)
|
|---|
| 113 | {
|
|---|
| 114 | void *vaddr;
|
|---|
| 115 |
|
|---|
| 116 | kbd_dev = kdev;
|
|---|
| 117 |
|
|---|
| 118 | sysarg_t ns16550;
|
|---|
| 119 | if (sysinfo_get_value("kbd.type.ns16550", &ns16550) != EOK)
|
|---|
| 120 | return -1;
|
|---|
| 121 | if (!ns16550)
|
|---|
| 122 | return -1;
|
|---|
| 123 |
|
|---|
| 124 | if (sysinfo_get_value("kbd.address.physical", &ns16550_physical) != EOK)
|
|---|
| 125 | return -1;
|
|---|
| 126 |
|
|---|
| 127 | if (sysinfo_get_value("kbd.address.kernel", &ns16550_kernel) != EOK)
|
|---|
| 128 | return -1;
|
|---|
| 129 |
|
|---|
| 130 | sysarg_t inr;
|
|---|
| 131 | if (sysinfo_get_value("kbd.inr", &inr) != EOK)
|
|---|
| 132 | return -1;
|
|---|
| 133 |
|
|---|
| 134 | ns16550_kbd.cmds[0].addr = (void *) (ns16550_kernel + LSR_REG);
|
|---|
| 135 | ns16550_kbd.cmds[3].addr = (void *) (ns16550_kernel + RBR_REG);
|
|---|
| 136 |
|
|---|
| 137 | async_set_interrupt_received(ns16550_irq_handler);
|
|---|
| 138 | register_irq(inr, device_assign_devno(), inr, &ns16550_kbd);
|
|---|
| 139 |
|
|---|
| 140 | return pio_enable((void *) ns16550_physical, 8, &vaddr);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | static void ns16550_port_yield(void)
|
|---|
| 144 | {
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | static void ns16550_port_reclaim(void)
|
|---|
| 148 | {
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | static void ns16550_port_write(uint8_t data)
|
|---|
| 152 | {
|
|---|
| 153 | (void) data;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | static void ns16550_irq_handler(ipc_callid_t iid, ipc_call_t *call)
|
|---|
| 157 | {
|
|---|
| 158 | kbd_push_data(kbd_dev, IPC_GET_ARG2(*call));
|
|---|
| 159 |
|
|---|
| 160 | if (irc_service)
|
|---|
| 161 | async_obsolete_msg_1(irc_phone, IRC_CLEAR_INTERRUPT,
|
|---|
| 162 | IPC_GET_IMETHOD(*call));
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /**
|
|---|
| 166 | * @}
|
|---|
| 167 | */
|
|---|