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