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 | /** @addtogroup genarch
|
---|
29 | * @{
|
---|
30 | */
|
---|
31 | /**
|
---|
32 | * @file
|
---|
33 | * @brief Texas Instruments AMDM37x on-chip uart serial line driver.
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <genarch/drivers/amdm37x_uart/amdm37x_uart.h>
|
---|
37 | #include <str.h>
|
---|
38 | #include <mm/km.h>
|
---|
39 |
|
---|
40 | static void amdm37x_uart_txb(amdm37x_uart_t *uart, uint8_t b)
|
---|
41 | {
|
---|
42 | /* Wait for buffer */
|
---|
43 | while (uart->regs->ssr & AMDM37x_UART_SSR_TX_FIFO_FULL_FLAG);
|
---|
44 | /* Write to the outgoing fifo */
|
---|
45 | uart->regs->thr = b;
|
---|
46 | }
|
---|
47 |
|
---|
48 | static void amdm37x_uart_putchar(outdev_t *dev, wchar_t ch)
|
---|
49 | {
|
---|
50 | amdm37x_uart_t *uart = dev->data;
|
---|
51 | if (!ascii_check(ch)) {
|
---|
52 | amdm37x_uart_txb(uart, U_SPECIAL);
|
---|
53 | } else {
|
---|
54 | if (ch == '\n')
|
---|
55 | amdm37x_uart_txb(uart, '\r');
|
---|
56 | amdm37x_uart_txb(uart, ch);
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | static outdev_operations_t amdm37x_uart_ops = {
|
---|
61 | .redraw = NULL,
|
---|
62 | .write = amdm37x_uart_putchar,
|
---|
63 | };
|
---|
64 |
|
---|
65 | bool amdm37x_uart_init(
|
---|
66 | amdm37x_uart_t *uart, inr_t interrupt, uintptr_t addr, size_t size)
|
---|
67 | {
|
---|
68 | ASSERT(uart);
|
---|
69 | uart->regs = (void *)km_map(addr, size, PAGE_NOT_CACHEABLE);
|
---|
70 |
|
---|
71 | /* See TI OMAP35X TRM ch 17.5.1.1 p. 2732 for startup routine */
|
---|
72 |
|
---|
73 | /* Soft reset the port */
|
---|
74 | uart->regs->sysc = AMDM37x_UART_SYSC_SOFTRESET_FLAG;
|
---|
75 | while (uart->regs->syss & AMDM37x_UART_SYSS_RESETDONE_FLAG) ;
|
---|
76 |
|
---|
77 | /* Enable access to EFR register */
|
---|
78 | const uint8_t lcr = uart->regs->lcr; /* Save old value */
|
---|
79 | uart->regs->lcr = 0xbf; /* Sets config mode B */
|
---|
80 |
|
---|
81 | /* Enable access to TCL_TLR register */
|
---|
82 | const bool enhanced = uart->regs->efr & AMDM37x_UART_EFR_ENH_FLAG;
|
---|
83 | uart->regs->efr |= AMDM37x_UART_EFR_ENH_FLAG; /* Turn on enh. */
|
---|
84 | uart->regs->lcr = 0x80; /* Config mode A */
|
---|
85 |
|
---|
86 | /* Set default (val 0) triggers, disable DMA enable FIFOs */
|
---|
87 | const bool tcl_tlr = uart->regs->mcr & AMDM37x_UART_MCR_TCR_TLR_FLAG;
|
---|
88 | uart->regs->fcr = AMDM37x_UART_FCR_FIFO_EN_FLAG;
|
---|
89 |
|
---|
90 | /* Enable fine granularity for rx trigger */
|
---|
91 | uart->regs->lcr = 0xbf; /* Sets config mode B */
|
---|
92 | uart->regs->scr = AMDM37x_UART_SCR_RX_TRIG_GRANU1_FLAG;
|
---|
93 |
|
---|
94 | /* Restore enhanced */
|
---|
95 | if (!enhanced)
|
---|
96 | uart->regs->efr &= ~AMDM37x_UART_EFR_ENH_FLAG;
|
---|
97 |
|
---|
98 | uart->regs->lcr = 0x80; /* Config mode A */
|
---|
99 | /* Restore tcl_lcr */
|
---|
100 | if (!tcl_tlr)
|
---|
101 | uart->regs->mcr &= ~AMDM37x_UART_MCR_TCR_TLR_FLAG;
|
---|
102 |
|
---|
103 | /* Restore tcl_lcr */
|
---|
104 | uart->regs->lcr = lcr;
|
---|
105 |
|
---|
106 | /* Disable interrupts */
|
---|
107 | uart->regs->ier = 0;
|
---|
108 |
|
---|
109 | /* Setup outdev */
|
---|
110 | outdev_initialize("amdm37x_uart_dev", &uart->outdev, &amdm37x_uart_ops);
|
---|
111 | uart->outdev.data = uart;
|
---|
112 | return false;
|
---|
113 | }
|
---|
114 |
|
---|
115 | void amdm37x_uart_input_wire(amdm37x_uart_t *uart, indev_t *indev)
|
---|
116 | {
|
---|
117 | // TODO implement
|
---|
118 | // register interrupt
|
---|
119 | // set rx fifo
|
---|
120 | // set rx fifo threshold to 1
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * @}
|
---|
125 | */
|
---|