source: mainline/kernel/genarch/src/drivers/grlib_uart/grlib_uart.c@ b77207e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b77207e was b77207e, checked in by Jakub Klama <jakub.klama@…>, 12 years ago

Add initial version of in-kernel driver for GRLIB APBUART
based on s3c24xx_uart driver.

  • Property mode set to 100644
File size: 4.3 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/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 =
54 (grlib_uart_t *) dev->data;
55
56 /* Wait for space becoming available in Tx FIFO. */
57 do {
58 status = pio_read_32(&uart->io->status);
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 =
67 (grlib_uart_t *) dev->data;
68
69 if ((!uart->parea.mapped) || (console_override)) {
70 if (!ascii_check(ch)) {
71 grlib_uart_sendb(dev, U_SPECIAL);
72 } else {
73 if (ch == '\n')
74 grlib_uart_sendb(dev, (uint8_t) '\r');
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 grlib_status_t status;
89
90 status = (grlib_status_t)pio_read_32(&uart->io->status);
91
92 while (status->dr != 0) {
93 uint32_t data = pio_read_32(&uart->io->data);
94 status = (grlib_status_t)pio_read_32(&uart->io->status);
95 indev_push_character(uart->indev, data & 0xff);
96 }
97}
98
99static outdev_operations_t grlib_uart_ops = {
100 .write = grlib_uart_putchar,
101 .redraw = NULL
102};
103
104outdev_t *grlib_uart_init(uintptr_t paddr, inr_t inr)
105{
106 outdev_t *uart_dev = malloc(sizeof(outdev_t), FRAME_ATOMIC);
107 if (!uart_dev)
108 return NULL;
109
110 grlib_uart_t *uart =
111 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_control_t control =
134 { .fa = 1, .rf = 1, .tf = 1, .ri = 1,
135 .te = 1, .re = 1};
136
137 pio_write_32(&uart->io->control, control);
138
139 link_initialize(&uart->parea.link);
140 uart->parea.pbase = paddr;
141 uart->parea.frames = 1;
142 uart->parea.unpriv = false;
143 uart->parea.mapped = false;
144 ddi_parea_register(&uart->parea);
145
146 return uart_dev;
147}
148
149void grlib_uart_input_wire(grlib_uart_t *uart, indev_t *indev)
150{
151 ASSERT(uart);
152 ASSERT(indev);
153
154 uart->indev = indev;
155 irq_register(&uart->irq);
156}
157
158/** @}
159 */
Note: See TracBrowser for help on using the repository browser.