Subversion Repositories HelenOS

Rev

Rev 4042 | Rev 4156 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4042 Rev 4055
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
 
42
 
43
#define LSR_DATA_READY  0x01
43
#define LSR_DATA_READY  0x01
44
 
44
 
45
/** Initialize ns16550.
45
/** Initialize ns16550.
46
 *
46
 *
47
 * @param dev       Addrress of the beginning of the device in I/O space.
47
 * @param dev       Addrress of the beginning of the device in I/O space.
48
 * @param devno     Device number.
48
 * @param devno     Device number.
49
 * @param inr       Interrupt number.
49
 * @param inr       Interrupt number.
50
 * @param cir       Clear interrupt function.
50
 * @param cir       Clear interrupt function.
51
 * @param cir_arg   First argument to cir.
51
 * @param cir_arg   First argument to cir.
52
 * @param devout    Output character device.
52
 * @param devout    Output character device.
53
 *
53
 *
54
 * @return      True on success, false on failure.
54
 * @return      True on success, false on failure.
55
 */
55
 */
56
bool
56
bool
57
ns16550_init(ns16550_t *dev, devno_t devno, inr_t inr, cir_t cir, void *cir_arg,
57
ns16550_init(ns16550_t *dev, devno_t devno, inr_t inr, cir_t cir, void *cir_arg,
58
    chardev_t *devout)
58
    chardev_t *devout)
59
{
59
{
60
    ns16550_instance_t *instance;
60
    ns16550_instance_t *instance;
61
   
61
   
62
    instance = malloc(sizeof(ns16550_instance_t), FRAME_ATOMIC);
62
    instance = malloc(sizeof(ns16550_instance_t), FRAME_ATOMIC);
63
    if (!instance)
63
    if (!instance)
64
        return false;
64
        return false;
65
 
65
 
66
    instance->devno = devno;
66
    instance->devno = devno;
67
    instance->ns16550 = dev;
67
    instance->ns16550 = dev;
68
    instance->devout = devout;
68
    instance->devout = devout;
69
   
69
   
70
    irq_initialize(&instance->irq);
70
    irq_initialize(&instance->irq);
71
    instance->irq.devno = devno;
71
    instance->irq.devno = devno;
72
    instance->irq.inr = inr;
72
    instance->irq.inr = inr;
73
    instance->irq.claim = ns16550_claim;
73
    instance->irq.claim = ns16550_claim;
74
    instance->irq.handler = ns16550_irq_handler;
74
    instance->irq.handler = ns16550_irq_handler;
75
    instance->irq.instance = instance;
75
    instance->irq.instance = instance;
76
    instance->irq.cir = cir;
76
    instance->irq.cir = cir;
77
    instance->irq.cir_arg = cir_arg;
77
    instance->irq.cir_arg = cir_arg;
78
    irq_register(&instance->irq);
78
    irq_register(&instance->irq);
79
 
79
 
80
    while ((pio_read_8(&dev->lsr) & LSR_DATA_READY))
80
    while ((pio_read_8(&dev->lsr) & LSR_DATA_READY))
81
        (void) pio_read_8(&dev->rbr);
81
        (void) pio_read_8(&dev->rbr);
82
   
82
   
83
    /* Enable interrupts */
83
    /* Enable interrupts */
84
    pio_write_8(&dev->ier, IER_ERBFI);
84
    pio_write_8(&dev->ier, IER_ERBFI);
85
    pio_write_8(&dev->mcr, MCR_OUT2);
85
    pio_write_8(&dev->mcr, MCR_OUT2);
86
   
86
   
87
    return true;
87
    return true;
88
}
88
}
89
 
89
 
90
irq_ownership_t ns16550_claim(irq_t *irq)
90
irq_ownership_t ns16550_claim(irq_t *irq)
91
{
91
{
92
    ns16550_instance_t *instance = irq->instance;
92
    ns16550_instance_t *instance = irq->instance;
93
    ns16550_t *dev = instance->ns16550;
93
    ns16550_t *dev = instance->ns16550;
94
 
94
 
95
    if (pio_read_8(&dev->lsr) & LSR_DATA_READY)
95
    if (pio_read_8(&dev->lsr) & LSR_DATA_READY)
96
        return IRQ_ACCEPT;
96
        return IRQ_ACCEPT;
97
    else
97
    else
98
        return IRQ_DECLINE;
98
        return IRQ_DECLINE;
99
}
99
}
100
 
100
 
101
void ns16550_irq_handler(irq_t *irq)
101
void ns16550_irq_handler(irq_t *irq)
102
{
102
{
103
    ns16550_instance_t *instance = irq->instance;
103
    ns16550_instance_t *instance = irq->instance;
104
    ns16550_t *dev = instance->ns16550;
104
    ns16550_t *dev = instance->ns16550;
105
 
105
 
106
    if (pio_read_8(&dev->lsr) & LSR_DATA_READY) {
106
    if (pio_read_8(&dev->lsr) & LSR_DATA_READY) {
107
        uint8_t x;
107
        uint8_t x;
108
       
108
       
109
        x = pio_read_8(&dev->rbr);
109
        x = pio_read_8(&dev->rbr);
110
        if (instance->devout)
110
        if (instance->devout)
111
            chardev_push_character(instance->devout, x);
111
            chardev_push_character(instance->devout, x);
112
    }
112
    }
113
}
113
}
114
 
114
 
115
/** @}
115
/** @}
116
 */
116
 */
117
 
117