Rev 2089 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | jermar | 1 | /* |
| 2071 | jermar | 2 | * Copyright (c) 2001-2004 Jakub Jermar |
| 1 | jermar | 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 | |||
| 1754 | jermar | 29 | /** @addtogroup genarch |
| 1702 | cejka | 30 | * @{ |
| 31 | */ |
||
| 1754 | jermar | 32 | /** |
| 1757 | jermar | 33 | * @file |
| 1842 | jermar | 34 | * @brief Zilog 8530 serial port / keyboard driver. |
| 1702 | cejka | 35 | */ |
| 36 | |||
| 1842 | jermar | 37 | #include <genarch/kbd/z8530.h> |
| 1843 | jermar | 38 | #include <genarch/kbd/key.h> |
| 1842 | jermar | 39 | #include <genarch/kbd/scanc.h> |
| 40 | #include <genarch/kbd/scanc_sun.h> |
||
| 41 | #include <arch/drivers/z8530.h> |
||
| 1920 | jermar | 42 | #include <ddi/irq.h> |
| 1923 | jermar | 43 | #include <ipc/irq.h> |
| 1 | jermar | 44 | #include <arch/interrupt.h> |
| 1875 | jermar | 45 | #include <arch/drivers/kbd.h> |
| 1919 | jermar | 46 | #include <arch/drivers/fhc.h> |
| 1 | jermar | 47 | #include <cpu.h> |
| 48 | #include <arch/asm.h> |
||
| 49 | #include <arch.h> |
||
| 511 | jermar | 50 | #include <console/chardev.h> |
| 51 | #include <console/console.h> |
||
| 576 | palkovsky | 52 | #include <interrupt.h> |
| 1875 | jermar | 53 | #include <sysinfo/sysinfo.h> |
| 54 | #include <print.h> |
||
| 1 | jermar | 55 | |
| 670 | vana | 56 | /* |
| 1842 | jermar | 57 | * These codes read from z8530 data register are silently ignored. |
| 895 | jermar | 58 | */ |
| 1842 | jermar | 59 | #define IGNORE_CODE 0x7f /* all keys up */ |
| 895 | jermar | 60 | |
| 1921 | jermar | 61 | static z8530_t z8530; /**< z8530 device structure. */ |
| 62 | static irq_t z8530_irq; /**< z8530's IRQ. */ |
||
| 63 | |||
| 1842 | jermar | 64 | static void z8530_suspend(chardev_t *); |
| 65 | static void z8530_resume(chardev_t *); |
||
| 511 | jermar | 66 | |
| 67 | static chardev_operations_t ops = { |
||
| 1842 | jermar | 68 | .suspend = z8530_suspend, |
| 69 | .resume = z8530_resume, |
||
| 1896 | jermar | 70 | .read = z8530_key_read |
| 511 | jermar | 71 | }; |
| 72 | |||
| 1923 | jermar | 73 | /** Initialize keyboard and service interrupts using kernel routine. */ |
| 1842 | jermar | 74 | void z8530_grab(void) |
| 1 | jermar | 75 | { |
| 1944 | jermar | 76 | ipl_t ipl = interrupts_disable(); |
| 77 | |||
| 1925 | jermar | 78 | (void) z8530_read_a(&z8530, RR8); |
| 79 | |||
| 80 | /* |
||
| 81 | * Clear any pending TX interrupts or we never manage |
||
| 82 | * to set FHC UART interrupt state to idle. |
||
| 83 | */ |
||
| 84 | z8530_write_a(&z8530, WR0, WR0_TX_IP_RST); |
||
| 85 | |||
| 86 | z8530_write_a(&z8530, WR1, WR1_IARCSC); /* interrupt on all characters */ |
||
| 87 | |||
| 88 | /* 8 bits per character and enable receiver */ |
||
| 89 | z8530_write_a(&z8530, WR3, WR3_RX8BITSCH | WR3_RX_ENABLE); |
||
| 90 | |||
| 91 | z8530_write_a(&z8530, WR9, WR9_MIE); /* Master Interrupt Enable. */ |
||
| 92 | |||
| 1944 | jermar | 93 | spinlock_lock(&z8530_irq.lock); |
| 1932 | jermar | 94 | z8530_irq.notif_cfg.notify = false; |
| 1944 | jermar | 95 | spinlock_unlock(&z8530_irq.lock); |
| 96 | interrupts_restore(ipl); |
||
| 1474 | palkovsky | 97 | } |
| 1849 | jermar | 98 | |
| 1923 | jermar | 99 | /** Resume the former IPC notification behavior. */ |
| 1842 | jermar | 100 | void z8530_release(void) |
| 1474 | palkovsky | 101 | { |
| 1944 | jermar | 102 | ipl_t ipl = interrupts_disable(); |
| 103 | spinlock_lock(&z8530_irq.lock); |
||
| 1932 | jermar | 104 | if (z8530_irq.notif_cfg.answerbox) |
| 105 | z8530_irq.notif_cfg.notify = true; |
||
| 1944 | jermar | 106 | spinlock_unlock(&z8530_irq.lock); |
| 107 | interrupts_restore(ipl); |
||
| 1474 | palkovsky | 108 | } |
| 670 | vana | 109 | |
| 1842 | jermar | 110 | /** Initialize z8530. */ |
| 1921 | jermar | 111 | void z8530_init(devno_t devno, inr_t inr, uintptr_t vaddr) |
| 1474 | palkovsky | 112 | { |
| 1842 | jermar | 113 | chardev_initialize("z8530_kbd", &kbrd, &ops); |
| 511 | jermar | 114 | stdin = &kbrd; |
| 1195 | jermar | 115 | |
| 1921 | jermar | 116 | z8530.devno = devno; |
| 117 | z8530.reg = (uint8_t *) vaddr; |
||
| 118 | |||
| 119 | irq_initialize(&z8530_irq); |
||
| 120 | z8530_irq.devno = devno; |
||
| 121 | z8530_irq.inr = inr; |
||
| 122 | z8530_irq.claim = z8530_claim; |
||
| 123 | z8530_irq.handler = z8530_irq_handler; |
||
| 124 | irq_register(&z8530_irq); |
||
| 125 | |||
| 1875 | jermar | 126 | sysinfo_set_item_val("kbd", NULL, true); |
| 1931 | jermar | 127 | sysinfo_set_item_val("kbd.type", NULL, KBD_Z8530); |
| 1921 | jermar | 128 | sysinfo_set_item_val("kbd.devno", NULL, devno); |
| 129 | sysinfo_set_item_val("kbd.inr", NULL, inr); |
||
| 130 | sysinfo_set_item_val("kbd.address.virtual", NULL, vaddr); |
||
| 1875 | jermar | 131 | |
| 1925 | jermar | 132 | z8530_grab(); |
| 1 | jermar | 133 | } |
| 134 | |||
| 1842 | jermar | 135 | /** Process z8530 interrupt. |
| 508 | jermar | 136 | * |
| 137 | * @param n Interrupt vector. |
||
| 1708 | jermar | 138 | * @param istate Interrupted state. |
| 508 | jermar | 139 | */ |
| 1849 | jermar | 140 | void z8530_interrupt(void) |
| 1 | jermar | 141 | { |
| 1849 | jermar | 142 | z8530_poll(); |
| 1 | jermar | 143 | } |
| 508 | jermar | 144 | |
| 511 | jermar | 145 | /* Called from getc(). */ |
| 1842 | jermar | 146 | void z8530_resume(chardev_t *d) |
| 511 | jermar | 147 | { |
| 148 | } |
||
| 149 | |||
| 150 | /* Called from getc(). */ |
||
| 1842 | jermar | 151 | void z8530_suspend(chardev_t *d) |
| 511 | jermar | 152 | { |
| 153 | } |
||
| 878 | vana | 154 | |
| 1896 | jermar | 155 | char z8530_key_read(chardev_t *d) |
| 878 | vana | 156 | { |
| 157 | char ch; |
||
| 158 | |||
| 894 | jermar | 159 | while(!(ch = active_read_buff_read())) { |
| 1780 | jermar | 160 | uint8_t x; |
| 1921 | jermar | 161 | while (!(z8530_read_a(&z8530, RR0) & RR0_RCA)) |
| 894 | jermar | 162 | ; |
| 1921 | jermar | 163 | x = z8530_read_a(&z8530, RR8); |
| 895 | jermar | 164 | if (x != IGNORE_CODE) { |
| 165 | if (x & KEY_RELEASE) |
||
| 166 | key_released(x ^ KEY_RELEASE); |
||
| 167 | else |
||
| 168 | active_read_key_pressed(x); |
||
| 169 | } |
||
| 878 | vana | 170 | } |
| 171 | return ch; |
||
| 172 | } |
||
| 895 | jermar | 173 | |
| 174 | /** Poll for key press and release events. |
||
| 175 | * |
||
| 176 | * This function can be used to implement keyboard polling. |
||
| 177 | */ |
||
| 1842 | jermar | 178 | void z8530_poll(void) |
| 895 | jermar | 179 | { |
| 1780 | jermar | 180 | uint8_t x; |
| 895 | jermar | 181 | |
| 1921 | jermar | 182 | while (z8530_read_a(&z8530, RR0) & RR0_RCA) { |
| 183 | x = z8530_read_a(&z8530, RR8); |
||
| 895 | jermar | 184 | if (x != IGNORE_CODE) { |
| 185 | if (x & KEY_RELEASE) |
||
| 186 | key_released(x ^ KEY_RELEASE); |
||
| 187 | else |
||
| 188 | key_pressed(x); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | } |
||
| 1702 | cejka | 192 | |
| 1919 | jermar | 193 | irq_ownership_t z8530_claim(void) |
| 194 | { |
||
| 1921 | jermar | 195 | return (z8530_read_a(&z8530, RR0) & RR0_RCA); |
| 1919 | jermar | 196 | } |
| 197 | |||
| 198 | void z8530_irq_handler(irq_t *irq, void *arg, ...) |
||
| 199 | { |
||
| 200 | /* |
||
| 201 | * So far, we know we got this interrupt through the FHC. |
||
| 1923 | jermar | 202 | * Since we don't have enough documentation about the FHC |
| 203 | * and because the interrupt looks like level sensitive, |
||
| 1919 | jermar | 204 | * we cannot handle it by scheduling one of the level |
| 205 | * interrupt traps. Process the interrupt directly. |
||
| 206 | */ |
||
| 1932 | jermar | 207 | if (irq->notif_cfg.notify && irq->notif_cfg.answerbox) |
| 1923 | jermar | 208 | ipc_irq_send_notif(irq); |
| 209 | else |
||
| 1919 | jermar | 210 | z8530_interrupt(); |
| 211 | fhc_clear_interrupt(central_fhc, irq->inr); |
||
| 212 | } |
||
| 213 | |||
| 1754 | jermar | 214 | /** @} |
| 1702 | cejka | 215 | */ |