Rev 4119 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1844 | jermar | 1 | /* |
3982 | jermar | 2 | * Copyright (c) 2009 Jakub Jermar |
1844 | 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 | |||
4094 | decky | 29 | /** @addtogroup genarch |
1844 | jermar | 30 | * @{ |
31 | */ |
||
32 | /** |
||
33 | * @file |
||
4094 | decky | 34 | * @brief NS 16550 serial controller driver. |
1844 | jermar | 35 | */ |
36 | |||
4042 | jermar | 37 | #include <genarch/drivers/ns16550/ns16550.h> |
1920 | jermar | 38 | #include <ddi/irq.h> |
1844 | jermar | 39 | #include <arch/asm.h> |
40 | #include <console/chardev.h> |
||
3934 | jermar | 41 | #include <mm/slab.h> |
4148 | decky | 42 | #include <ddi/device.h> |
1844 | jermar | 43 | |
4094 | decky | 44 | #define LSR_DATA_READY 0x01 |
1844 | jermar | 45 | |
4119 | decky | 46 | static indev_operations_t kbrdin_ops = { |
4094 | decky | 47 | .poll = NULL |
48 | }; |
||
49 | |||
50 | static irq_ownership_t ns16550_claim(irq_t *irq) |
||
51 | { |
||
52 | ns16550_instance_t *instance = irq->instance; |
||
53 | ns16550_t *dev = instance->ns16550; |
||
54 | |||
55 | if (pio_read_8(&dev->lsr) & LSR_DATA_READY) |
||
56 | return IRQ_ACCEPT; |
||
57 | else |
||
58 | return IRQ_DECLINE; |
||
59 | } |
||
60 | |||
61 | static void ns16550_irq_handler(irq_t *irq) |
||
62 | { |
||
63 | ns16550_instance_t *instance = irq->instance; |
||
64 | ns16550_t *dev = instance->ns16550; |
||
65 | |||
66 | if (pio_read_8(&dev->lsr) & LSR_DATA_READY) { |
||
67 | uint8_t x = pio_read_8(&dev->rbr); |
||
4119 | decky | 68 | indev_push_character(&instance->kbrdin, x); |
4094 | decky | 69 | } |
70 | } |
||
71 | |||
1921 | jermar | 72 | /** Initialize ns16550. |
73 | * |
||
4094 | decky | 74 | * @param dev Addrress of the beginning of the device in I/O space. |
75 | * @param devno Device number. |
||
76 | * @param inr Interrupt number. |
||
77 | * @param cir Clear interrupt function. |
||
78 | * @param cir_arg First argument to cir. |
||
3934 | jermar | 79 | * |
4094 | decky | 80 | * @return Keyboard device pointer or NULL on failure. |
81 | * |
||
1921 | jermar | 82 | */ |
4148 | decky | 83 | indev_t *ns16550_init(ns16550_t *dev, inr_t inr, cir_t cir, void *cir_arg) |
1844 | jermar | 84 | { |
4094 | decky | 85 | ns16550_instance_t *instance |
86 | = malloc(sizeof(ns16550_instance_t), FRAME_ATOMIC); |
||
87 | if (!instance) |
||
88 | return NULL; |
||
1880 | jermar | 89 | |
4094 | decky | 90 | indev_initialize("ns16550", &instance->kbrdin, &kbrdin_ops); |
91 | |||
3934 | jermar | 92 | instance->ns16550 = dev; |
1921 | jermar | 93 | |
3938 | jermar | 94 | irq_initialize(&instance->irq); |
4148 | decky | 95 | instance->irq.devno = device_assign_devno(); |
3938 | jermar | 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 | irq_register(&instance->irq); |
||
4094 | decky | 103 | |
3938 | jermar | 104 | while ((pio_read_8(&dev->lsr) & LSR_DATA_READY)) |
3934 | jermar | 105 | (void) pio_read_8(&dev->rbr); |
1921 | jermar | 106 | |
3646 | jermar | 107 | /* Enable interrupts */ |
3934 | jermar | 108 | pio_write_8(&dev->ier, IER_ERBFI); |
109 | pio_write_8(&dev->mcr, MCR_OUT2); |
||
1911 | jermar | 110 | |
4094 | decky | 111 | return &instance->kbrdin; |
1844 | jermar | 112 | } |
113 | |||
114 | /** @} |
||
115 | */ |