1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup raspberrypi_usart |
---|
5 | * |
---|
6 | * @brief USART support. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2013 Alan Cudmore |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * |
---|
15 | * http://www.rtems.org/license/LICENSE |
---|
16 | * |
---|
17 | */ |
---|
18 | |
---|
19 | #include <libchip/sersupp.h> |
---|
20 | |
---|
21 | #include <bsp.h> |
---|
22 | #include <bsp/irq.h> |
---|
23 | #include <bsp/usart.h> |
---|
24 | #include <bsp/raspberrypi.h> |
---|
25 | |
---|
26 | static void usart_delay(uint32_t n) |
---|
27 | { |
---|
28 | volatile uint32_t i = 0; |
---|
29 | for(i = 0; i < n; i++); |
---|
30 | } |
---|
31 | |
---|
32 | static rtems_vector_number usart_get_irq_number(const console_tbl *ct) |
---|
33 | { |
---|
34 | return ct->ulIntVector; |
---|
35 | } |
---|
36 | |
---|
37 | static uint32_t usart_get_baud(const console_tbl *ct) |
---|
38 | { |
---|
39 | return ct->ulClock; |
---|
40 | } |
---|
41 | |
---|
42 | static void usart_set_baud(int minor, int baud) |
---|
43 | { |
---|
44 | /* |
---|
45 | ** Nothing for now |
---|
46 | */ |
---|
47 | return; |
---|
48 | } |
---|
49 | |
---|
50 | static void usart_initialize(int minor) |
---|
51 | { |
---|
52 | unsigned int gpio_reg; |
---|
53 | |
---|
54 | /* |
---|
55 | ** Program GPIO pins for UART 0 |
---|
56 | */ |
---|
57 | gpio_reg = BCM2835_REG(BCM2835_GPIO_GPFSEL1); |
---|
58 | gpio_reg &= ~(7<<12); /* gpio14 */ |
---|
59 | gpio_reg |= (4<<12); /* alt0 */ |
---|
60 | gpio_reg &= ~(7<<15); /* gpio15 */ |
---|
61 | gpio_reg |= (4<<15); /* alt0 */ |
---|
62 | BCM2835_REG(BCM2835_GPIO_GPFSEL1) = gpio_reg; |
---|
63 | |
---|
64 | BCM2835_REG(BCM2835_GPIO_GPPUD) = 0; |
---|
65 | usart_delay(150); |
---|
66 | BCM2835_REG(BCM2835_GPIO_GPPUDCLK0) = (1<<14)|(1<<15); |
---|
67 | usart_delay(150); |
---|
68 | BCM2835_REG(BCM2835_GPIO_GPPUDCLK0) = 0; |
---|
69 | |
---|
70 | /* |
---|
71 | ** Init the PL011 UART |
---|
72 | */ |
---|
73 | BCM2835_REG(BCM2835_UART0_CR) = 0; |
---|
74 | BCM2835_REG(BCM2835_UART0_ICR) = 0x7FF; |
---|
75 | BCM2835_REG(BCM2835_UART0_IMSC) = 0; |
---|
76 | BCM2835_REG(BCM2835_UART0_IBRD) = 1; |
---|
77 | BCM2835_REG(BCM2835_UART0_FBRD) = 40; |
---|
78 | BCM2835_REG(BCM2835_UART0_LCRH) = 0x70; |
---|
79 | BCM2835_REG(BCM2835_UART0_RSRECR) = 0; |
---|
80 | |
---|
81 | BCM2835_REG(BCM2835_UART0_CR) = 0x301; |
---|
82 | |
---|
83 | BCM2835_REG(BCM2835_UART0_IMSC) = BCM2835_UART0_IMSC_RX; |
---|
84 | |
---|
85 | usart_set_baud(minor, 115000); |
---|
86 | |
---|
87 | } |
---|
88 | |
---|
89 | static int usart_first_open(int major, int minor, void *arg) |
---|
90 | { |
---|
91 | rtems_libio_open_close_args_t *oc = (rtems_libio_open_close_args_t *) arg; |
---|
92 | struct rtems_termios_tty *tty = (struct rtems_termios_tty *) oc->iop->data1; |
---|
93 | const console_tbl *ct = Console_Port_Tbl [minor]; |
---|
94 | console_data *cd = &Console_Port_Data [minor]; |
---|
95 | |
---|
96 | cd->termios_data = tty; |
---|
97 | rtems_termios_set_initial_baud(tty, ct->ulClock); |
---|
98 | |
---|
99 | return 0; |
---|
100 | } |
---|
101 | |
---|
102 | static int usart_last_close(int major, int minor, void *arg) |
---|
103 | { |
---|
104 | return 0; |
---|
105 | } |
---|
106 | |
---|
107 | static int usart_read_polled(int minor) |
---|
108 | { |
---|
109 | if (minor == 0) |
---|
110 | { |
---|
111 | if(((BCM2835_REG(BCM2835_UART0_FR)) & BCM2835_UART0_FR_RXFE) == 0) |
---|
112 | { |
---|
113 | return((BCM2835_REG(BCM2835_UART0_DR)) & 0xFF ); |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | return -1; |
---|
118 | } |
---|
119 | } |
---|
120 | else |
---|
121 | { |
---|
122 | printk("Unknown console minor number: %d\n", minor); |
---|
123 | return -1; |
---|
124 | } |
---|
125 | |
---|
126 | } |
---|
127 | |
---|
128 | static void usart_write_polled(int minor, char c) |
---|
129 | { |
---|
130 | while (1) |
---|
131 | { |
---|
132 | if ((BCM2835_REG(BCM2835_UART0_FR) & BCM2835_UART0_FR_TXFF) == 0) break; |
---|
133 | } |
---|
134 | BCM2835_REG(BCM2835_UART0_DR) = c; |
---|
135 | } |
---|
136 | |
---|
137 | static ssize_t usart_write_support_polled( |
---|
138 | int minor, |
---|
139 | const char *s, |
---|
140 | size_t n |
---|
141 | ) |
---|
142 | { |
---|
143 | ssize_t i = 0; |
---|
144 | |
---|
145 | for (i = 0; i < n; ++i) |
---|
146 | { |
---|
147 | usart_write_polled(minor, s [i]); |
---|
148 | } |
---|
149 | |
---|
150 | return n; |
---|
151 | } |
---|
152 | |
---|
153 | static int usart_set_attributes(int minor, const struct termios *term) |
---|
154 | { |
---|
155 | return -1; |
---|
156 | } |
---|
157 | |
---|
158 | const console_fns bcm2835_usart_fns = { |
---|
159 | .deviceProbe = libchip_serial_default_probe, |
---|
160 | .deviceFirstOpen = usart_first_open, |
---|
161 | .deviceLastClose = usart_last_close, |
---|
162 | .deviceRead = usart_read_polled, |
---|
163 | .deviceWrite = usart_write_support_polled, |
---|
164 | .deviceInitialize = usart_initialize, |
---|
165 | .deviceWritePolled = usart_write_polled, |
---|
166 | .deviceSetAttributes = usart_set_attributes, |
---|
167 | .deviceOutputUsesInterrupts = false |
---|
168 | }; |
---|