source: mainline/kernel/genarch/src/drivers/pl011/pl011.c@ 3009164

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3009164 was 63e27ef, checked in by Jiri Svoboda <jiri@…>, 8 years ago

ASSERT → assert

  • Property mode set to 100644
File size: 4.4 KB
Line 
1/*
2 * Copyright (c) 2012 Jan Vesely
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
29/** @addtogroup genarch
30 * @{
31 */
32/**
33 * @file
34 * @brief ARM PrimeCell PL011 UART driver.
35 */
36
37#include <assert.h>
38#include <genarch/drivers/pl011/pl011.h>
39#include <console/chardev.h>
40#include <console/console.h>
41#include <ddi/device.h>
42#include <arch/asm.h>
43#include <mm/slab.h>
44#include <mm/page.h>
45#include <mm/km.h>
46#include <sysinfo/sysinfo.h>
47#include <str.h>
48
49static void pl011_uart_sendb(pl011_uart_t *uart, uint8_t byte)
50{
51 /* Wait for space becoming available in Tx FIFO. */
52 // TODO make pio_read accept consts pointers and remove the cast
53 while ((pio_read_32((ioport32_t*)&uart->regs->flag) & PL011_UART_FLAG_TXFF_FLAG) != 0)
54 ;
55
56 pio_write_32(&uart->regs->data, byte);
57}
58
59static void pl011_uart_putchar(outdev_t *dev, wchar_t ch)
60{
61 pl011_uart_t *uart = dev->data;
62
63 if (!ascii_check(ch)) {
64 pl011_uart_sendb(uart, U_SPECIAL);
65 } else {
66 if (ch == '\n')
67 pl011_uart_sendb(uart, (uint8_t) '\r');
68 pl011_uart_sendb(uart, (uint8_t) ch);
69 }
70}
71
72static outdev_operations_t pl011_uart_ops = {
73 .write = pl011_uart_putchar,
74 .redraw = NULL,
75 .scroll_up = NULL,
76 .scroll_down = NULL
77};
78
79static irq_ownership_t pl011_uart_claim(irq_t *irq)
80{
81 return IRQ_ACCEPT;
82}
83
84static void pl011_uart_irq_handler(irq_t *irq)
85{
86 pl011_uart_t *uart = irq->instance;
87
88 // TODO make pio_read accept const pointers and remove the cast
89 while ((pio_read_32((ioport32_t*)&uart->regs->flag) & PL011_UART_FLAG_RXFE_FLAG) == 0) {
90 /* We ignore all error flags here */
91 const uint8_t data = pio_read_32(&uart->regs->data);
92 if (uart->indev)
93 indev_push_character(uart->indev, data);
94 }
95 /* Ack interrupts */
96 pio_write_32(&uart->regs->interrupt_clear, PL011_UART_INTERRUPT_ALL);
97}
98
99bool pl011_uart_init(pl011_uart_t *uart, inr_t interrupt, uintptr_t addr)
100{
101 assert(uart);
102 uart->regs = (void*)km_map(addr, sizeof(pl011_uart_regs_t),
103 PAGE_NOT_CACHEABLE);
104 assert(uart->regs);
105
106 /* Disable UART */
107 uart->regs->control &= ~ PL011_UART_CONTROL_UARTEN_FLAG;
108
109 /* Enable hw flow control */
110 uart->regs->control |=
111 PL011_UART_CONTROL_RTSE_FLAG |
112 PL011_UART_CONTROL_CTSE_FLAG;
113
114 /* Mask all interrupts */
115 uart->regs->interrupt_mask = 0;
116 /* Clear interrupts */
117 uart->regs->interrupt_clear = PL011_UART_INTERRUPT_ALL;
118 /* Enable UART, TX and RX */
119 uart->regs->control |=
120 PL011_UART_CONTROL_UARTEN_FLAG |
121 PL011_UART_CONTROL_TXE_FLAG |
122 PL011_UART_CONTROL_RXE_FLAG;
123
124 outdev_initialize("pl011_uart_dev", &uart->outdev, &pl011_uart_ops);
125 uart->outdev.data = uart;
126
127 /* Initialize IRQ */
128 irq_initialize(&uart->irq);
129 uart->irq.devno = device_assign_devno();
130 uart->irq.inr = interrupt;
131 uart->irq.claim = pl011_uart_claim;
132 uart->irq.handler = pl011_uart_irq_handler;
133 uart->irq.instance = uart;
134
135 return true;
136}
137
138void pl011_uart_input_wire(pl011_uart_t *uart, indev_t *indev)
139{
140 assert(uart);
141 assert(indev);
142
143 uart->indev = indev;
144 irq_register(&uart->irq);
145 /* Enable receive interrupts */
146 uart->regs->interrupt_mask |=
147 PL011_UART_INTERRUPT_RX_FLAG |
148 PL011_UART_INTERRUPT_RT_FLAG;
149}
150
151/** @}
152 */
153
Note: See TracBrowser for help on using the repository browser.