[8b4be29] | 1 | /*
|
---|
[4c7257b] | 2 | * Copyright (c) 2009 Jakub Jermar
|
---|
[8b4be29] | 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 |
|
---|
[3c79afe] | 29 | /** @addtogroup genarch
|
---|
[8b4be29] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
[3c79afe] | 34 | * @brief NS 16550 serial controller driver.
|
---|
[8b4be29] | 35 | */
|
---|
| 36 |
|
---|
[411b6a6] | 37 | #include <genarch/drivers/ns16550/ns16550.h>
|
---|
[7dcf22a] | 38 | #include <ddi/irq.h>
|
---|
[8b4be29] | 39 | #include <arch/asm.h>
|
---|
| 40 | #include <console/chardev.h>
|
---|
[013c4d6] | 41 | #include <mm/slab.h>
|
---|
[84afc7b] | 42 | #include <ddi/device.h>
|
---|
[8b4be29] | 43 |
|
---|
[3c79afe] | 44 | #define LSR_DATA_READY 0x01
|
---|
| 45 |
|
---|
| 46 | static irq_ownership_t ns16550_claim(irq_t *irq)
|
---|
| 47 | {
|
---|
| 48 | ns16550_instance_t *instance = irq->instance;
|
---|
| 49 | ns16550_t *dev = instance->ns16550;
|
---|
| 50 |
|
---|
| 51 | if (pio_read_8(&dev->lsr) & LSR_DATA_READY)
|
---|
| 52 | return IRQ_ACCEPT;
|
---|
| 53 | else
|
---|
| 54 | return IRQ_DECLINE;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | static void ns16550_irq_handler(irq_t *irq)
|
---|
| 58 | {
|
---|
| 59 | ns16550_instance_t *instance = irq->instance;
|
---|
| 60 | ns16550_t *dev = instance->ns16550;
|
---|
| 61 |
|
---|
| 62 | if (pio_read_8(&dev->lsr) & LSR_DATA_READY) {
|
---|
[c2417bc] | 63 | uint8_t data = pio_read_8(&dev->rbr);
|
---|
| 64 | indev_push_character(instance->kbrdin, data);
|
---|
[3c79afe] | 65 | }
|
---|
| 66 | }
|
---|
[8b4be29] | 67 |
|
---|
[c2417bc] | 68 | /**< Clear input buffer. */
|
---|
| 69 | static void ns16550_clear_buffer(ns16550_t *dev)
|
---|
| 70 | {
|
---|
| 71 | while ((pio_read_8(&dev->lsr) & LSR_DATA_READY))
|
---|
| 72 | (void) pio_read_8(&dev->rbr);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[63530c62] | 75 | /** Initialize ns16550.
|
---|
| 76 | *
|
---|
[3c79afe] | 77 | * @param dev Addrress of the beginning of the device in I/O space.
|
---|
| 78 | * @param devno Device number.
|
---|
| 79 | * @param inr Interrupt number.
|
---|
| 80 | * @param cir Clear interrupt function.
|
---|
| 81 | * @param cir_arg First argument to cir.
|
---|
| 82 | *
|
---|
[c2417bc] | 83 | * @return Keyboard instance or NULL on failure.
|
---|
[013c4d6] | 84 | *
|
---|
[63530c62] | 85 | */
|
---|
[c2417bc] | 86 | ns16550_instance_t *ns16550_init(ns16550_t *dev, inr_t inr, cir_t cir, void *cir_arg)
|
---|
[8b4be29] | 87 | {
|
---|
[3c79afe] | 88 | ns16550_instance_t *instance
|
---|
| 89 | = malloc(sizeof(ns16550_instance_t), FRAME_ATOMIC);
|
---|
[c2417bc] | 90 | if (instance) {
|
---|
| 91 | instance->ns16550 = dev;
|
---|
| 92 | instance->kbrdin = NULL;
|
---|
| 93 |
|
---|
| 94 | irq_initialize(&instance->irq);
|
---|
| 95 | instance->irq.devno = device_assign_devno();
|
---|
| 96 | instance->irq.inr = inr;
|
---|
| 97 | instance->irq.claim = ns16550_claim;
|
---|
| 98 | instance->irq.handler = ns16550_irq_handler;
|
---|
| 99 | instance->irq.instance = instance;
|
---|
| 100 | instance->irq.cir = cir;
|
---|
| 101 | instance->irq.cir_arg = cir_arg;
|
---|
| 102 | }
|
---|
[3c79afe] | 103 |
|
---|
[c2417bc] | 104 | return instance;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | void ns16550_wire(ns16550_instance_t *instance, indev_t *kbrdin)
|
---|
| 108 | {
|
---|
| 109 | ASSERT(instance);
|
---|
| 110 | ASSERT(kbrdin);
|
---|
[63530c62] | 111 |
|
---|
[c2417bc] | 112 | instance->kbrdin = kbrdin;
|
---|
[dc22844] | 113 | irq_register(&instance->irq);
|
---|
[3c79afe] | 114 |
|
---|
[c2417bc] | 115 | ns16550_clear_buffer(instance->ns16550);
|
---|
[63530c62] | 116 |
|
---|
[3e53ab7] | 117 | /* Enable interrupts */
|
---|
[c2417bc] | 118 | pio_write_8(&instance->ns16550->ier, IER_ERBFI);
|
---|
| 119 | pio_write_8(&instance->ns16550->mcr, MCR_OUT2);
|
---|
[0d107f31] | 120 | }
|
---|
| 121 |
|
---|
[8b4be29] | 122 | /** @}
|
---|
| 123 | */
|
---|