Rev 4346 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4346 | Rev 4347 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2009 Jakub Jermar |
2 | * Copyright (c) 2009 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup genarch |
29 | /** @addtogroup genarch |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** |
32 | /** |
33 | * @file |
33 | * @file |
34 | * @brief NS 16550 serial controller driver. |
34 | * @brief NS 16550 serial controller driver. |
35 | */ |
35 | */ |
36 | 36 | ||
37 | #include <genarch/drivers/ns16550/ns16550.h> |
37 | #include <genarch/drivers/ns16550/ns16550.h> |
38 | #include <ddi/irq.h> |
38 | #include <ddi/irq.h> |
39 | #include <arch/asm.h> |
39 | #include <arch/asm.h> |
40 | #include <console/chardev.h> |
40 | #include <console/chardev.h> |
41 | #include <mm/slab.h> |
41 | #include <mm/slab.h> |
- | 42 | #include <ddi/device.h> |
|
42 | 43 | ||
43 | #define LSR_DATA_READY 0x01 |
44 | #define LSR_DATA_READY 0x01 |
44 | 45 | ||
45 | static indev_operations_t kbrdin_ops = { |
46 | static indev_operations_t kbrdin_ops = { |
46 | .poll = NULL |
47 | .poll = NULL |
47 | }; |
48 | }; |
48 | 49 | ||
49 | static irq_ownership_t ns16550_claim(irq_t *irq) |
50 | static irq_ownership_t ns16550_claim(irq_t *irq) |
50 | { |
51 | { |
51 | ns16550_instance_t *instance = irq->instance; |
52 | ns16550_instance_t *instance = irq->instance; |
52 | ns16550_t *dev = instance->ns16550; |
53 | ns16550_t *dev = instance->ns16550; |
53 | 54 | ||
54 | if (pio_read_8(&dev->lsr) & LSR_DATA_READY) |
55 | if (pio_read_8(&dev->lsr) & LSR_DATA_READY) |
55 | return IRQ_ACCEPT; |
56 | return IRQ_ACCEPT; |
56 | else |
57 | else |
57 | return IRQ_DECLINE; |
58 | return IRQ_DECLINE; |
58 | } |
59 | } |
59 | 60 | ||
60 | static void ns16550_irq_handler(irq_t *irq) |
61 | static void ns16550_irq_handler(irq_t *irq) |
61 | { |
62 | { |
62 | ns16550_instance_t *instance = irq->instance; |
63 | ns16550_instance_t *instance = irq->instance; |
63 | ns16550_t *dev = instance->ns16550; |
64 | ns16550_t *dev = instance->ns16550; |
64 | 65 | ||
65 | if (pio_read_8(&dev->lsr) & LSR_DATA_READY) { |
66 | if (pio_read_8(&dev->lsr) & LSR_DATA_READY) { |
66 | uint8_t x = pio_read_8(&dev->rbr); |
67 | uint8_t x = pio_read_8(&dev->rbr); |
67 | indev_push_character(&instance->kbrdin, x); |
68 | indev_push_character(&instance->kbrdin, x); |
68 | } |
69 | } |
69 | } |
70 | } |
70 | 71 | ||
71 | /** Initialize ns16550. |
72 | /** Initialize ns16550. |
72 | * |
73 | * |
73 | * @param dev Addrress of the beginning of the device in I/O space. |
74 | * @param dev Addrress of the beginning of the device in I/O space. |
74 | * @param devno Device number. |
75 | * @param devno Device number. |
75 | * @param inr Interrupt number. |
76 | * @param inr Interrupt number. |
76 | * @param cir Clear interrupt function. |
77 | * @param cir Clear interrupt function. |
77 | * @param cir_arg First argument to cir. |
78 | * @param cir_arg First argument to cir. |
78 | * |
79 | * |
79 | * @return Keyboard device pointer or NULL on failure. |
80 | * @return Keyboard device pointer or NULL on failure. |
80 | * |
81 | * |
81 | */ |
82 | */ |
82 | indev_t *ns16550_init(ns16550_t *dev, devno_t devno, inr_t inr, cir_t cir, void *cir_arg) |
83 | indev_t *ns16550_init(ns16550_t *dev, inr_t inr, cir_t cir, void *cir_arg) |
83 | { |
84 | { |
84 | ns16550_instance_t *instance |
85 | ns16550_instance_t *instance |
85 | = malloc(sizeof(ns16550_instance_t), FRAME_ATOMIC); |
86 | = malloc(sizeof(ns16550_instance_t), FRAME_ATOMIC); |
86 | if (!instance) |
87 | if (!instance) |
87 | return NULL; |
88 | return NULL; |
88 | 89 | ||
89 | indev_initialize("ns16550", &instance->kbrdin, &kbrdin_ops); |
90 | indev_initialize("ns16550", &instance->kbrdin, &kbrdin_ops); |
90 | 91 | ||
91 | instance->devno = devno; |
- | |
92 | instance->ns16550 = dev; |
92 | instance->ns16550 = dev; |
93 | 93 | ||
94 | irq_initialize(&instance->irq); |
94 | irq_initialize(&instance->irq); |
95 | instance->irq.devno = devno; |
95 | instance->irq.devno = device_assign_devno(); |
96 | instance->irq.inr = inr; |
96 | instance->irq.inr = inr; |
97 | instance->irq.claim = ns16550_claim; |
97 | instance->irq.claim = ns16550_claim; |
98 | instance->irq.handler = ns16550_irq_handler; |
98 | instance->irq.handler = ns16550_irq_handler; |
99 | instance->irq.instance = instance; |
99 | instance->irq.instance = instance; |
100 | instance->irq.cir = cir; |
100 | instance->irq.cir = cir; |
101 | instance->irq.cir_arg = cir_arg; |
101 | instance->irq.cir_arg = cir_arg; |
102 | irq_register(&instance->irq); |
102 | irq_register(&instance->irq); |
103 | 103 | ||
104 | while ((pio_read_8(&dev->lsr) & LSR_DATA_READY)) |
104 | while ((pio_read_8(&dev->lsr) & LSR_DATA_READY)) |
105 | (void) pio_read_8(&dev->rbr); |
105 | (void) pio_read_8(&dev->rbr); |
106 | 106 | ||
107 | /* Enable interrupts */ |
107 | /* Enable interrupts */ |
108 | pio_write_8(&dev->ier, IER_ERBFI); |
108 | pio_write_8(&dev->ier, IER_ERBFI); |
109 | pio_write_8(&dev->mcr, MCR_OUT2); |
109 | pio_write_8(&dev->mcr, MCR_OUT2); |
110 | 110 | ||
111 | return &instance->kbrdin; |
111 | return &instance->kbrdin; |
112 | } |
112 | } |
113 | 113 | ||
114 | /** @} |
114 | /** @} |
115 | */ |
115 | */ |
116 | 116 |