source: mainline/kernel/genarch/src/drivers/omap/uart.c@ 24abb85d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 24abb85d was 24abb85d, checked in by Jakub Jermar <jakub@…>, 8 years ago

Remove SYS_DEVICE_ASSIGN_DEVNO

  • Property mode set to 100644
File size: 5.3 KB
Line 
1/*
2 * Copyright (c) 2012 Jan Vesely
3 * Copyright (c) 2013 Maurizio Lombardi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29/** @addtogroup genarch
30 * @{
31 */
32/**
33 * @file
34 * @brief Texas Instruments OMAP on-chip uart serial line driver.
35 */
36
37#include <assert.h>
38#include <genarch/drivers/omap/uart.h>
39#include <str.h>
40#include <mm/km.h>
41
42static void omap_uart_txb(omap_uart_t *uart, uint8_t b)
43{
44 /* Wait for buffer */
45 while (uart->regs->ssr & OMAP_UART_SSR_TX_FIFO_FULL_FLAG);
46 /* Write to the outgoing fifo */
47 uart->regs->thr = b;
48}
49
50static void omap_uart_putchar(outdev_t *dev, wchar_t ch)
51{
52 omap_uart_t *uart = dev->data;
53 if (!ascii_check(ch)) {
54 omap_uart_txb(uart, U_SPECIAL);
55 } else {
56 if (ch == '\n')
57 omap_uart_txb(uart, '\r');
58 omap_uart_txb(uart, ch);
59 }
60}
61
62static outdev_operations_t omap_uart_ops = {
63 .write = omap_uart_putchar,
64 .redraw = NULL,
65 .scroll_up = NULL,
66 .scroll_down = NULL
67};
68
69static irq_ownership_t omap_uart_claim(irq_t *irq)
70{
71 return IRQ_ACCEPT;
72}
73
74static void omap_uart_handler(irq_t *irq)
75{
76 omap_uart_t *uart = irq->instance;
77 while ((uart->regs->rx_fifo_lvl)) {
78 const uint8_t val = uart->regs->rhr;
79 if (uart->indev && val) {
80 indev_push_character(uart->indev, val);
81 }
82 }
83}
84
85bool omap_uart_init(
86 omap_uart_t *uart, inr_t interrupt, uintptr_t addr, size_t size)
87{
88 assert(uart);
89 uart->regs = (void *)km_map(addr, size, PAGE_NOT_CACHEABLE);
90
91 assert(uart->regs);
92
93 /* Soft reset the port */
94 uart->regs->sysc = OMAP_UART_SYSC_SOFTRESET_FLAG;
95 while (!(uart->regs->syss & OMAP_UART_SYSS_RESETDONE_FLAG));
96
97 /* Disable the UART module */
98 uart->regs->mdr1 |= OMAP_UART_MDR_MS_DISABLE;
99
100 /* Enable access to EFR register */
101 uart->regs->lcr = 0xbf; /* Sets config mode B */
102
103 /* Enable access to TCL_TLR register */
104 const bool enhanced = uart->regs->efr & OMAP_UART_EFR_ENH_FLAG;
105 uart->regs->efr |= OMAP_UART_EFR_ENH_FLAG; /* Turn on enh. */
106 uart->regs->lcr = 0x80; /* Config mode A */
107
108 /* Set default (val 0) triggers, disable DMA enable FIFOs */
109 const bool tcl_tlr = uart->regs->mcr & OMAP_UART_MCR_TCR_TLR_FLAG;
110 /* Enable access to tcr and tlr registers */
111 uart->regs->mcr |= OMAP_UART_MCR_TCR_TLR_FLAG;
112
113 /* Enable FIFOs */
114 uart->regs->fcr = OMAP_UART_FCR_FIFO_EN_FLAG;
115
116 /* Enable fine granularity for RX FIFO and set trigger level to 1,
117 * TX FIFO, trigger level is irrelevant*/
118 uart->regs->lcr = 0xBF; /* Sets config mode B */
119 uart->regs->scr = OMAP_UART_SCR_RX_TRIG_GRANU1_FLAG;
120 uart->regs->tlr = 1 << OMAP_UART_TLR_RX_FIFO_TRIG_SHIFT;
121
122 /* Sets config mode A */
123 uart->regs->lcr = 0x80;
124 /* Restore tcl_tlr access flag */
125 if (!tcl_tlr)
126 uart->regs->mcr &= ~OMAP_UART_MCR_TCR_TLR_FLAG;
127 /* Sets config mode B */
128 uart->regs->lcr = 0xBF;
129
130 /* Set the divisor value to get a baud rate of 115200 bps */
131 uart->regs->dll = 0x1A;
132 uart->regs->dlh = 0x00;
133
134 /* Restore enhanced */
135 if (!enhanced)
136 uart->regs->efr &= ~OMAP_UART_EFR_ENH_FLAG;
137
138 /* Set the DIV_EN bit to 0 */
139 uart->regs->lcr &= ~OMAP_UART_LCR_DIV_EN_FLAG;
140 /* Set the BREAK_EN bit to 0 */
141 uart->regs->lcr &= ~OMAP_UART_LCR_BREAK_EN_FLAG;
142 /* No parity */
143 uart->regs->lcr &= ~OMAP_UART_LCR_PARITY_EN_FLAG;
144 /* Stop = 1 bit */
145 uart->regs->lcr &= ~OMAP_UART_LCR_NB_STOP_FLAG;
146 /* Char length = 8 bits */
147 uart->regs->lcr |= OMAP_UART_LCR_CHAR_LENGTH_8BITS;
148
149 /* Enable the UART module */
150 uart->regs->mdr1 &= (OMAP_UART_MDR_MS_UART16 &
151 ~OMAP_UART_MDR_MS_MASK);
152
153 /* Disable interrupts */
154 uart->regs->ier = 0;
155
156 /* Setup outdev */
157 outdev_initialize("omap_uart_dev", &uart->outdev, &omap_uart_ops);
158 uart->outdev.data = uart;
159
160 /* Initialize IRQ */
161 irq_initialize(&uart->irq);
162 uart->irq.inr = interrupt;
163 uart->irq.claim = omap_uart_claim;
164 uart->irq.handler = omap_uart_handler;
165 uart->irq.instance = uart;
166
167 return true;
168}
169
170void omap_uart_input_wire(omap_uart_t *uart, indev_t *indev)
171{
172 assert(uart);
173 /* Set indev */
174 uart->indev = indev;
175 /* Register interrupt. */
176 irq_register(&uart->irq);
177 /* Enable interrupt on receive */
178 uart->regs->ier |= OMAP_UART_IER_RHR_IRQ_FLAG;
179}
180
181/**
182 * @}
183 */
184
Note: See TracBrowser for help on using the repository browser.