OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [libdev/] [uart/] [omap/] [uart.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
/*
2
 * UART driver used by OMAP devices
3
 *
4
 * Copyright (C) 2007 Bahadir Balban
5
 */
6
 
7
#include <libdev/uart.h>
8
#include <libdev/io.h>
9
#include "uart.h"
10
 
11
#define OMAP_UART_TXFE          0x20
12
void uart_tx_char(unsigned long uart_base, char c)
13
{
14
        volatile u32 reg;
15
 
16
        /* Check if there is space for tx */
17
        do {
18
                reg = read(uart_base + OMAP_UART_LSR);
19
        } while(!(reg & OMAP_UART_TXFE));
20
 
21
        write(c, uart_base + OMAP_UART_THR);
22
}
23
 
24
#define OMAP_UART_RXFNE                 0x1
25
#define OMAP_UART_RX_FIFO_STATUS        0x8
26
char uart_rx_char(unsigned long uart_base)
27
{
28
        volatile u32 reg;
29
 
30
        /* Check if pending data is there */
31
        do {
32
                reg = read(uart_base + OMAP_UART_LSR);
33
        } while(!(reg & OMAP_UART_RXFNE));
34
 
35
#if 0
36
        /* Check if there is some error in recieve */
37
        if(reg & OMAP_UART_RX_FIFO_STATUS)
38
                return -1;
39
#endif
40
        return (char)read(uart_base + OMAP_UART_RHR);
41
 
42
}
43
 
44
void uart_set_baudrate(unsigned long uart_base, u32 baudrate)
45
{
46
        u32 clk_div;
47
 
48
        /* 48Mhz clock fixed on beagleboard */
49
        const u32 clkrate = 48000000;
50
 
51
        /* If baud out of range, set default rate */
52
        if(baudrate > 3686400 || baudrate < 300)
53
                baudrate = 115200;
54
 
55
        clk_div = clkrate/(16 * baudrate);
56
 
57
        /* Set clockrate in DLH and DLL */
58
        write((clk_div & 0xff), uart_base + OMAP_UART_DLL);
59
        write(((clk_div >> 8) & 0xff ), uart_base + OMAP_UART_DLH);
60
}
61
 
62
void uart_init(unsigned long uart_base)
63
{
64
        /* Disable UART */
65
        uart_select_mode(uart_base, OMAP_UART_MODE_DEFAULT);
66
 
67
        /* Disable interrupts */
68
        uart_disable_interrupt(uart_base);
69
 
70
        /* Change to config mode, to set baud divisor */
71
        uart_set_link_control(uart_base, OMAP_UART_BANKED_MODE_CONFIG_A);
72
 
73
        /* Set the baud rate */
74
        uart_set_baudrate(uart_base, 115200);
75
 
76
        /* Switch to operational mode */
77
        uart_set_link_control(uart_base, OMAP_UART_BANKED_MODE_OPERATIONAL);
78
 
79
        /* Set up the link- parity, data bits stop bits to 8N1 */
80
        uart_disable_parity(uart_base);
81
        uart_set_data_bits(uart_base, OMAP_UART_DATA_BITS_8);
82
        uart_set_stop_bits(uart_base, OMAP_UART_STOP_BITS_1);
83
 
84
        /* Disable Fifos */
85
        uart_disable_fifo(uart_base);
86
 
87
        /* Enable modem Rx/Tx */
88
        uart_enable_tx(uart_base);
89
        uart_enable_rx(uart_base);
90
 
91
        /* Enable UART in 16x mode */
92
        uart_select_mode(uart_base, OMAP_UART_MODE_UART16X);
93
}
94
 
95
unsigned long uart_print_base;
96
 
97
void platform_init(void)
98
{
99
        uart_print_base = OMAP_UART_BASE;
100
 
101
        /*
102
         * We dont need to initialize uart here for variant-userspace,
103
         * as this is the same uart as used by kernel and hence
104
         * already initialized, we just need
105
         * a uart struct instance with proper base address.
106
         *
107
         * But in case of baremetal like loader, no one has done
108
         * initialization, so we need to do it.
109
         */
110
#if defined(VARIANT_BAREMETAL)
111
        uart_init(uart_print_base);
112
#endif
113
}
114
 
115
 

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.