1 | /* |
---|
2 | * Console driver for pxa255 full function port by Yang Xi <hiyangxi@gmail.com> |
---|
3 | * Copyright (c) 2004 by Jay Monkman <jtm@lopingdog.com> |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.org/license/LICENSE. |
---|
8 | */ |
---|
9 | |
---|
10 | #include <bsp.h> |
---|
11 | #include <rtems/libio.h> |
---|
12 | #include <termios.h> |
---|
13 | |
---|
14 | #include <pxa255.h> |
---|
15 | #include <ffuart.h> |
---|
16 | #include <rtems/bspIo.h> |
---|
17 | #include <libchip/serial.h> |
---|
18 | #include <libchip/sersupp.h> |
---|
19 | |
---|
20 | volatile int dbg_dly; |
---|
21 | |
---|
22 | /* static function prototypes */ |
---|
23 | static int ffuart_first_open(int major, int minor, void *arg); |
---|
24 | static int ffuart_last_close(int major, int minor, void *arg); |
---|
25 | static int ffuart_read(int minor); |
---|
26 | static ssize_t ffuart_write(int minor, const char *buf, size_t len); |
---|
27 | static void ffuart_init(int minor); |
---|
28 | static void ffuart_write_polled(int minor, char c); |
---|
29 | static int ffuart_set_attributes(int minor, const struct termios *t); |
---|
30 | |
---|
31 | /* Pointers to functions for handling the UART. */ |
---|
32 | const console_fns ffuart_fns = |
---|
33 | { |
---|
34 | libchip_serial_default_probe, |
---|
35 | ffuart_first_open, |
---|
36 | ffuart_last_close, |
---|
37 | ffuart_read, |
---|
38 | ffuart_write, |
---|
39 | ffuart_init, |
---|
40 | ffuart_write_polled, /* not used in this driver */ |
---|
41 | ffuart_set_attributes, |
---|
42 | FALSE /* TRUE if interrupt driven, FALSE if not. */ |
---|
43 | }; |
---|
44 | |
---|
45 | |
---|
46 | /* |
---|
47 | * This is called the first time each device is opened. Since |
---|
48 | * the driver is polled, we don't have to do anything. If the driver |
---|
49 | * were interrupt driven, we'd enable interrupts here. |
---|
50 | */ |
---|
51 | static int ffuart_first_open(int major, int minor, void *arg) |
---|
52 | { |
---|
53 | return 0; |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | /* |
---|
58 | * This is called the last time each device is closed. Since |
---|
59 | * the driver is polled, we don't have to do anything. If the driver |
---|
60 | * were interrupt driven, we'd disable interrupts here. |
---|
61 | */ |
---|
62 | static int ffuart_last_close(int major, int minor, void *arg) |
---|
63 | { |
---|
64 | return 0; |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | /* |
---|
69 | * Read one character from UART. |
---|
70 | * |
---|
71 | * return -1 if there's no data, otherwise return |
---|
72 | * the character in lowest 8 bits of returned int. |
---|
73 | */ |
---|
74 | static int ffuart_read(int minor) |
---|
75 | { |
---|
76 | char c; |
---|
77 | console_tbl *console_entry; |
---|
78 | ffuart_reg_t *ffuart; |
---|
79 | |
---|
80 | console_entry = BSP_get_uart_from_minor(minor); |
---|
81 | |
---|
82 | if (console_entry == NULL) { |
---|
83 | return -1; |
---|
84 | } |
---|
85 | |
---|
86 | ffuart = (ffuart_reg_t *)console_entry->ulCtrlPort1; |
---|
87 | |
---|
88 | if (!(ffuart->lsr & FULL_RECEIVE)) { |
---|
89 | return -1; |
---|
90 | } |
---|
91 | |
---|
92 | c = ffuart->rbr & 0xff; |
---|
93 | |
---|
94 | return c; |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | /* |
---|
99 | * Write buffer to UART |
---|
100 | * |
---|
101 | * return 1 on success, -1 on error |
---|
102 | */ |
---|
103 | static ssize_t ffuart_write(int minor, const char *buf, size_t len) |
---|
104 | { |
---|
105 | size_t i, x; |
---|
106 | char c; |
---|
107 | console_tbl *console_entry; |
---|
108 | ffuart_reg_t *ffuart; |
---|
109 | |
---|
110 | console_entry = BSP_get_uart_from_minor(minor); |
---|
111 | |
---|
112 | if (console_entry == NULL) { |
---|
113 | return -1; |
---|
114 | } |
---|
115 | |
---|
116 | ffuart = (ffuart_reg_t *)console_entry->ulCtrlPort1; |
---|
117 | |
---|
118 | for (i = 0; i < len; i++) { |
---|
119 | |
---|
120 | while(1) { |
---|
121 | if (ffuart->lsr & SEND_EMPTY) { |
---|
122 | break; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | c = (char) buf[i]; |
---|
127 | #if ON_SKYEYE != 1 |
---|
128 | if(c=='\n'){ |
---|
129 | ffuart->rbr = '\r'; |
---|
130 | for (x = 0; x < 100; x++) { |
---|
131 | dbg_dly++; /* using a global so this doesn't get optimized out */ |
---|
132 | } |
---|
133 | while(1){ |
---|
134 | if(ffuart->lsr & SEND_EMPTY){ |
---|
135 | break; |
---|
136 | } |
---|
137 | } |
---|
138 | } |
---|
139 | #endif |
---|
140 | ffuart->rbr = c; |
---|
141 | |
---|
142 | /* the TXRDY flag does not seem to update right away (is this true?) */ |
---|
143 | /* so we wait a bit before continuing */ |
---|
144 | for (x = 0; x < 100; x++) { |
---|
145 | dbg_dly++; /* using a global so this doesn't get optimized out */ |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | return 1; |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | static void ffuart_init(int minor) |
---|
154 | { |
---|
155 | |
---|
156 | |
---|
157 | console_tbl *console_entry; |
---|
158 | ffuart_reg_t *ffuart; |
---|
159 | unsigned int divisor; |
---|
160 | |
---|
161 | console_entry = BSP_get_uart_from_minor(minor); |
---|
162 | |
---|
163 | |
---|
164 | |
---|
165 | if (console_entry == NULL) { |
---|
166 | return; |
---|
167 | } |
---|
168 | |
---|
169 | ffuart = (ffuart_reg_t *)console_entry->ulCtrlPort1; |
---|
170 | ffuart->lcr |= DLAB; |
---|
171 | /*Set the Bound*/ |
---|
172 | ffuart->lcr |= DLAB; |
---|
173 | divisor = FREQUENCY_UART / (115200*16); |
---|
174 | ffuart->rbr = divisor & 0xff; |
---|
175 | ffuart->ier = (divisor >> 8)&0xff; |
---|
176 | /*Disable FIFO*/ |
---|
177 | ffuart->iir = 0; |
---|
178 | ffuart->lcr &=~DLAB; |
---|
179 | /*Enable UART*/ |
---|
180 | ffuart->ier = 0x40; |
---|
181 | ffuart->lcr = EIGHT_BITS_NOPARITY_1STOPBIT; |
---|
182 | |
---|
183 | } |
---|
184 | |
---|
185 | /* I'm not sure this is needed for the shared console driver. */ |
---|
186 | static void ffuart_write_polled(int minor, char c) |
---|
187 | { |
---|
188 | ffuart_write(minor, &c, 1); |
---|
189 | } |
---|
190 | |
---|
191 | /* This is for setting baud rate, bits, etc. */ |
---|
192 | static int ffuart_set_attributes(int minor, const struct termios *t) |
---|
193 | { |
---|
194 | return 0; |
---|
195 | } |
---|
196 | |
---|
197 | /***********************************************************************/ |
---|
198 | /* |
---|
199 | * The following functions are not used by TERMIOS, but other RTEMS |
---|
200 | * functions use them instead. |
---|
201 | */ |
---|
202 | /***********************************************************************/ |
---|
203 | /* |
---|
204 | * Read from UART. This is used in the exit code, and can't |
---|
205 | * rely on interrupts. |
---|
206 | */ |
---|
207 | static int ffuart_poll_read(int minor) |
---|
208 | { |
---|
209 | return ffuart_read(minor); |
---|
210 | } |
---|
211 | |
---|
212 | |
---|
213 | /* |
---|
214 | * Write a character to the console. This is used by printk() and |
---|
215 | * maybe other low level functions. It should not use interrupts or any |
---|
216 | * RTEMS system calls. It needs to be very simple |
---|
217 | */ |
---|
218 | static void _BSP_put_char( char c ) { |
---|
219 | ffuart_write_polled(0, c); |
---|
220 | } |
---|
221 | |
---|
222 | static int _BSP_poll_char(void) { |
---|
223 | return ffuart_poll_read(0); |
---|
224 | } |
---|
225 | |
---|
226 | BSP_output_char_function_type BSP_output_char = _BSP_put_char; |
---|
227 | BSP_polling_getchar_function_type BSP_poll_char = _BSP_poll_char; |
---|