source: mainline/kernel/genarch/src/drivers/grlib/uart.c@ 208b5f5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 208b5f5 was e47ed05, checked in by Martin Decky <martin@…>, 12 years ago

code review
coding style changes, removal of debugging prints
simplify directory structure

  • Property mode set to 100644
File size: 4.4 KB
Line 
1/*
2 * Copyright (c) 2009 Martin Decky
3 * Copyright (c) 2010 Jiri Svoboda
4 * Copyright (c) 2013 Jakub Klama
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/** @addtogroup genarch
32 * @{
33 */
34/**
35 * @file
36 * @brief Gaisler GRLIB UART IP-Core driver.
37 */
38
39#include <genarch/drivers/grlib/uart.h>
40#include <console/chardev.h>
41#include <console/console.h>
42#include <ddi/device.h>
43#include <arch/asm.h>
44#include <mm/slab.h>
45#include <mm/page.h>
46#include <mm/km.h>
47#include <sysinfo/sysinfo.h>
48#include <str.h>
49
50static void grlib_uart_sendb(outdev_t *dev, uint8_t byte)
51{
52 grlib_uart_status_t *status;
53 grlib_uart_t *uart = (grlib_uart_t *) dev->data;
54
55 /* Wait for space becoming available in Tx FIFO. */
56 do {
57 uint32_t reg = pio_read_32(&uart->io->status);
58 status = (grlib_uart_status_t *) &reg;
59 } while (status->tf != 0);
60
61 pio_write_32(&uart->io->data, byte);
62}
63
64static void grlib_uart_putchar(outdev_t *dev, wchar_t ch)
65{
66 grlib_uart_t *uart = (grlib_uart_t *) dev->data;
67
68 if ((!uart->parea.mapped) || (console_override)) {
69 if (!ascii_check(ch)) {
70 grlib_uart_sendb(dev, U_SPECIAL);
71 } else {
72 if (ch == '\n')
73 grlib_uart_sendb(dev, (uint8_t) '\r');
74
75 grlib_uart_sendb(dev, (uint8_t) ch);
76 }
77 }
78}
79
80static irq_ownership_t grlib_uart_claim(irq_t *irq)
81{
82 return IRQ_ACCEPT;
83}
84
85static void grlib_uart_irq_handler(irq_t *irq)
86{
87 grlib_uart_t *uart = irq->instance;
88
89 uint32_t reg = pio_read_32(&uart->io->status);
90 grlib_uart_status_t *status = (grlib_uart_status_t *) &reg;
91
92 while (status->dr != 0) {
93 uint32_t data = pio_read_32(&uart->io->data);
94 reg = pio_read_32(&uart->io->status);
95 status = (grlib_uart_status_t *) &reg;
96 indev_push_character(uart->indev, data & 0xff);
97 }
98}
99
100static outdev_operations_t grlib_uart_ops = {
101 .write = grlib_uart_putchar,
102 .redraw = NULL
103};
104
105outdev_t *grlib_uart_init(uintptr_t paddr, inr_t inr)
106{
107 outdev_t *uart_dev = malloc(sizeof(outdev_t), FRAME_ATOMIC);
108 if (!uart_dev)
109 return NULL;
110
111 grlib_uart_t *uart = malloc(sizeof(grlib_uart_t), FRAME_ATOMIC);
112 if (!uart) {
113 free(uart_dev);
114 return NULL;
115 }
116
117 outdev_initialize("grlib_uart_dev", uart_dev, &grlib_uart_ops);
118 uart_dev->data = uart;
119
120 uart->io = (grlib_uart_io_t *) km_map(paddr, PAGE_SIZE,
121 PAGE_WRITE | PAGE_NOT_CACHEABLE);
122 uart->indev = NULL;
123
124 /* Initialize IRQ structure. */
125 irq_initialize(&uart->irq);
126 uart->irq.devno = device_assign_devno();
127 uart->irq.inr = inr;
128 uart->irq.claim = grlib_uart_claim;
129 uart->irq.handler = grlib_uart_irq_handler;
130 uart->irq.instance = uart;
131
132 /* Enable FIFO, Tx trigger level: empty, Rx trigger level: 1 byte. */
133 grlib_uart_control_t control = {
134 .fa = 1,
135 .rf = 1,
136 .tf = 1,
137 .ri = 1,
138 .te = 1,
139 .re = 1
140 };
141
142 uint32_t *reg = (uint32_t *) &control;
143 pio_write_32(&uart->io->control, *reg);
144
145 link_initialize(&uart->parea.link);
146 uart->parea.pbase = paddr;
147 uart->parea.frames = 1;
148 uart->parea.unpriv = false;
149 uart->parea.mapped = false;
150 ddi_parea_register(&uart->parea);
151
152 return uart_dev;
153}
154
155void grlib_uart_input_wire(grlib_uart_t *uart, indev_t *indev)
156{
157 ASSERT(uart);
158 ASSERT(indev);
159
160 uart->indev = indev;
161 irq_register(&uart->irq);
162}
163
164/** @}
165 */
Note: See TracBrowser for help on using the repository browser.