1 | /* |
---|
2 | * Console driver for Motorola MCF5206E UART modules |
---|
3 | */ |
---|
4 | |
---|
5 | /* |
---|
6 | * Copyright (C) 2000 OKTET Ltd., St.-Petersburg, Russia |
---|
7 | * Author: Victor V. Vengerov <vvv@oktet.ru> |
---|
8 | * |
---|
9 | * COPYRIGHT (c) 1989-1998. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #include <termios.h> |
---|
18 | #include <bsp.h> |
---|
19 | #include <rtems/libio.h> |
---|
20 | #include <rtems/console.h> |
---|
21 | #include "mcf5206/mcf5206e.h" |
---|
22 | #include "mcf5206/mcfuart.h" |
---|
23 | |
---|
24 | /* Descriptor structures for two on-chip UART channels */ |
---|
25 | static mcfuart uart[2]; |
---|
26 | |
---|
27 | /* Console operations mode: |
---|
28 | * 0 - raw (non-termios) polled input/output |
---|
29 | * 1 - termios-based polled input/output |
---|
30 | * 2 - termios-based interrupt-driven input/output |
---|
31 | */ |
---|
32 | int console_mode = 2; |
---|
33 | #define CONSOLE_MODE_RAW (0) |
---|
34 | #define CONSOLE_MODE_POLL (1) |
---|
35 | #define CONSOLE_MODE_INT (2) |
---|
36 | |
---|
37 | /* Wrapper functions for MCF UART generic driver */ |
---|
38 | |
---|
39 | /* console_poll_read -- |
---|
40 | * wrapper for poll read function |
---|
41 | * |
---|
42 | * PARAMETERS: |
---|
43 | * minor - minor device number |
---|
44 | * |
---|
45 | * RETURNS: |
---|
46 | * character code readed from UART, or -1 if there is no characters |
---|
47 | * available |
---|
48 | */ |
---|
49 | static int |
---|
50 | console_poll_read(int minor) |
---|
51 | { |
---|
52 | return mcfuart_poll_read(&uart[minor]); |
---|
53 | } |
---|
54 | |
---|
55 | /* console_interrupt_write -- |
---|
56 | * wrapper for interrupt write function |
---|
57 | * |
---|
58 | * PARAMETERS: |
---|
59 | * minor - minor device number |
---|
60 | * buf - output buffer |
---|
61 | * len - output buffer length |
---|
62 | * |
---|
63 | * RETURNS: |
---|
64 | * result code |
---|
65 | */ |
---|
66 | static ssize_t |
---|
67 | console_interrupt_write(int minor, const char *buf, size_t len) |
---|
68 | { |
---|
69 | return mcfuart_interrupt_write(&uart[minor], buf, len); |
---|
70 | } |
---|
71 | |
---|
72 | /* console_poll_write -- |
---|
73 | * wrapper for polling mode write function |
---|
74 | * |
---|
75 | * PARAMETERS: |
---|
76 | * minor - minor device number |
---|
77 | * buf - output buffer |
---|
78 | * len - output buffer length |
---|
79 | * |
---|
80 | * RETURNS: |
---|
81 | * result code |
---|
82 | */ |
---|
83 | static ssize_t |
---|
84 | console_poll_write(int minor, const char *buf, size_t len) |
---|
85 | { |
---|
86 | return mcfuart_poll_write(&uart[minor], buf, len); |
---|
87 | } |
---|
88 | |
---|
89 | /* console_set_attributes -- |
---|
90 | * wrapper for hardware-dependent termios attributes setting |
---|
91 | * |
---|
92 | * PARAMETERS: |
---|
93 | * minor - minor device number |
---|
94 | * t - pointer to the termios structure |
---|
95 | * |
---|
96 | * RETURNS: |
---|
97 | * result code |
---|
98 | */ |
---|
99 | static int |
---|
100 | console_set_attributes(int minor, const struct termios *t) |
---|
101 | { |
---|
102 | return mcfuart_set_attributes(&uart[minor], t); |
---|
103 | } |
---|
104 | |
---|
105 | /* console_stop_remote_tx -- |
---|
106 | * wrapper for stopping data flow from remote party. |
---|
107 | * |
---|
108 | * PARAMETERS: |
---|
109 | * minor - minor device number |
---|
110 | * |
---|
111 | * RETURNS: |
---|
112 | * result code |
---|
113 | */ |
---|
114 | static int |
---|
115 | console_stop_remote_tx(int minor) |
---|
116 | { |
---|
117 | if (minor < sizeof(uart)/sizeof(uart[0])) |
---|
118 | return mcfuart_stop_remote_tx(&uart[minor]); |
---|
119 | else |
---|
120 | return RTEMS_INVALID_NUMBER; |
---|
121 | } |
---|
122 | |
---|
123 | /* console_start_remote_tx -- |
---|
124 | * wrapper for resuming data flow from remote party. |
---|
125 | * |
---|
126 | * PARAMETERS: |
---|
127 | * minor - minor device number |
---|
128 | * |
---|
129 | */ |
---|
130 | static int |
---|
131 | console_start_remote_tx(int minor) |
---|
132 | { |
---|
133 | if (minor < sizeof(uart)/sizeof(uart[0])) |
---|
134 | return mcfuart_start_remote_tx(&uart[minor]); |
---|
135 | else |
---|
136 | return RTEMS_INVALID_NUMBER; |
---|
137 | } |
---|
138 | |
---|
139 | /* console_first_open -- |
---|
140 | * wrapper for UART controller initialization functions |
---|
141 | * |
---|
142 | * PARAMETERS: |
---|
143 | * major - major device number |
---|
144 | * minor - minor device number |
---|
145 | * arg - libio device open argument |
---|
146 | * |
---|
147 | * RETURNS: |
---|
148 | * error code |
---|
149 | */ |
---|
150 | static int |
---|
151 | console_first_open(int major, int minor, void *arg) |
---|
152 | { |
---|
153 | rtems_libio_open_close_args_t *args = arg; |
---|
154 | rtems_status_code sc; |
---|
155 | uint8_t intvec; |
---|
156 | |
---|
157 | switch (minor) { |
---|
158 | case 0: intvec = BSP_INTVEC_UART1; break; |
---|
159 | case 1: intvec = BSP_INTVEC_UART2; break; |
---|
160 | default: |
---|
161 | return RTEMS_INVALID_NUMBER; |
---|
162 | } |
---|
163 | |
---|
164 | if (console_mode != CONSOLE_MODE_INT) { |
---|
165 | intvec = 0; |
---|
166 | } |
---|
167 | |
---|
168 | sc = mcfuart_init( |
---|
169 | &uart[minor], /* uart */ |
---|
170 | args->iop->data1, /* tty */ |
---|
171 | intvec, /* interrupt vector number */ |
---|
172 | minor+1); |
---|
173 | |
---|
174 | if (sc == RTEMS_SUCCESSFUL) |
---|
175 | sc = mcfuart_reset(&uart[minor]); |
---|
176 | |
---|
177 | return sc; |
---|
178 | } |
---|
179 | |
---|
180 | /* console_last_close -- |
---|
181 | * wrapper for UART controller close function |
---|
182 | * |
---|
183 | * PARAMETERS: |
---|
184 | * major - major device number |
---|
185 | * minor - minor device number |
---|
186 | * arg - libio device close argument |
---|
187 | * |
---|
188 | * RETURNS: |
---|
189 | * error code |
---|
190 | */ |
---|
191 | static int |
---|
192 | console_last_close(int major, int minor, void *arg) |
---|
193 | { |
---|
194 | return mcfuart_disable(&uart[minor]); |
---|
195 | } |
---|
196 | |
---|
197 | /* console_initialize -- |
---|
198 | * This routine initializes the console IO drivers and register devices |
---|
199 | * in RTEMS I/O system. |
---|
200 | * |
---|
201 | * PARAMETERS: |
---|
202 | * major - major console device number |
---|
203 | * minor - minor console device number (not used) |
---|
204 | * arg - device initialize argument |
---|
205 | * |
---|
206 | * RETURNS: |
---|
207 | * RTEMS error code (RTEMS_SUCCESSFUL if device initialized successfuly) |
---|
208 | */ |
---|
209 | rtems_device_driver console_initialize( |
---|
210 | rtems_device_major_number major, |
---|
211 | rtems_device_minor_number minor, |
---|
212 | void *arg) |
---|
213 | { |
---|
214 | rtems_status_code status; |
---|
215 | |
---|
216 | /* |
---|
217 | * Set up TERMIOS |
---|
218 | */ |
---|
219 | if (console_mode != CONSOLE_MODE_RAW) |
---|
220 | rtems_termios_initialize (); |
---|
221 | |
---|
222 | /* |
---|
223 | * Register the devices |
---|
224 | */ |
---|
225 | status = rtems_io_register_name ("/dev/console", major, 0); |
---|
226 | if (status != RTEMS_SUCCESSFUL) |
---|
227 | rtems_fatal_error_occurred (status); |
---|
228 | |
---|
229 | status = rtems_io_register_name ("/dev/aux", major, 1); |
---|
230 | if (status != RTEMS_SUCCESSFUL) |
---|
231 | rtems_fatal_error_occurred (status); |
---|
232 | |
---|
233 | if (console_mode == CONSOLE_MODE_RAW) { |
---|
234 | rtems_status_code sc; |
---|
235 | sc = mcfuart_init(&uart[0], /* uart */ |
---|
236 | NULL, /* tty */ |
---|
237 | 0, /* interrupt vector number */ |
---|
238 | 1); /* UART channel number */ |
---|
239 | |
---|
240 | if (sc == RTEMS_SUCCESSFUL) |
---|
241 | sc = mcfuart_reset(&uart[0]); |
---|
242 | |
---|
243 | sc = mcfuart_init(&uart[1], /* uart */ |
---|
244 | NULL, /* tty */ |
---|
245 | 0, /* interrupt vector number */ |
---|
246 | 2); /* UART channel number */ |
---|
247 | |
---|
248 | if (sc == RTEMS_SUCCESSFUL) |
---|
249 | sc = mcfuart_reset(&uart[1]); |
---|
250 | return sc; |
---|
251 | } |
---|
252 | |
---|
253 | return RTEMS_SUCCESSFUL; |
---|
254 | } |
---|
255 | |
---|
256 | /* console_open -- |
---|
257 | * Open console device driver. Pass appropriate termios callback |
---|
258 | * functions to termios library. |
---|
259 | * |
---|
260 | * PARAMETERS: |
---|
261 | * major - major device number for console devices |
---|
262 | * minor - minor device number for console |
---|
263 | * arg - device opening argument |
---|
264 | * |
---|
265 | * RETURNS: |
---|
266 | * RTEMS error code |
---|
267 | */ |
---|
268 | rtems_device_driver |
---|
269 | console_open(rtems_device_major_number major, |
---|
270 | rtems_device_minor_number minor, |
---|
271 | void *arg) |
---|
272 | { |
---|
273 | static const rtems_termios_callbacks intr_callbacks = { |
---|
274 | console_first_open, /* firstOpen */ |
---|
275 | console_last_close, /* lastClose */ |
---|
276 | NULL, /* pollRead */ |
---|
277 | console_interrupt_write, /* write */ |
---|
278 | console_set_attributes, /* setAttributes */ |
---|
279 | console_stop_remote_tx, /* stopRemoteTx */ |
---|
280 | console_start_remote_tx, /* startRemoteTx */ |
---|
281 | 1 /* outputUsesInterrupts */ |
---|
282 | }; |
---|
283 | static const rtems_termios_callbacks poll_callbacks = { |
---|
284 | console_first_open, /* firstOpen */ |
---|
285 | console_last_close, /* lastClose */ |
---|
286 | console_poll_read, /* pollRead */ |
---|
287 | console_poll_write, /* write */ |
---|
288 | console_set_attributes, /* setAttributes */ |
---|
289 | console_stop_remote_tx, /* stopRemoteTx */ |
---|
290 | console_start_remote_tx, /* startRemoteTx */ |
---|
291 | 0 /* outputUsesInterrupts */ |
---|
292 | }; |
---|
293 | |
---|
294 | switch (console_mode) { |
---|
295 | case CONSOLE_MODE_RAW: |
---|
296 | return RTEMS_SUCCESSFUL; |
---|
297 | |
---|
298 | case CONSOLE_MODE_INT: |
---|
299 | return rtems_termios_open(major, minor, arg, &intr_callbacks); |
---|
300 | |
---|
301 | case CONSOLE_MODE_POLL: |
---|
302 | return rtems_termios_open(major, minor, arg, &poll_callbacks); |
---|
303 | |
---|
304 | default: |
---|
305 | rtems_fatal_error_occurred(0xC07A1310); |
---|
306 | } |
---|
307 | return RTEMS_INTERNAL_ERROR; |
---|
308 | } |
---|
309 | |
---|
310 | /* console_close -- |
---|
311 | * Close console device. |
---|
312 | * |
---|
313 | * PARAMETERS: |
---|
314 | * major - major device number for console devices |
---|
315 | * minor - minor device number for console |
---|
316 | * arg - device close argument |
---|
317 | * |
---|
318 | * RETURNS: |
---|
319 | * RTEMS error code |
---|
320 | */ |
---|
321 | rtems_device_driver |
---|
322 | console_close(rtems_device_major_number major, |
---|
323 | rtems_device_minor_number minor, |
---|
324 | void *arg) |
---|
325 | { |
---|
326 | if (console_mode != CONSOLE_MODE_RAW) |
---|
327 | return rtems_termios_close (arg); |
---|
328 | else |
---|
329 | return RTEMS_SUCCESSFUL; |
---|
330 | } |
---|
331 | |
---|
332 | /* console_read -- |
---|
333 | * Read from the console device |
---|
334 | * |
---|
335 | * PARAMETERS: |
---|
336 | * major - major device number for console devices |
---|
337 | * minor - minor device number for console |
---|
338 | * arg - device read argument |
---|
339 | * |
---|
340 | * RETURNS: |
---|
341 | * RTEMS error code |
---|
342 | */ |
---|
343 | rtems_device_driver |
---|
344 | console_read(rtems_device_major_number major, |
---|
345 | rtems_device_minor_number minor, |
---|
346 | void *arg) |
---|
347 | { |
---|
348 | if (console_mode != CONSOLE_MODE_RAW) { |
---|
349 | return rtems_termios_read (arg); |
---|
350 | } else { |
---|
351 | rtems_libio_rw_args_t *argp = arg; |
---|
352 | char *buf = argp->buffer; |
---|
353 | int count = argp->count; |
---|
354 | int n = 0; |
---|
355 | int c; |
---|
356 | |
---|
357 | while (n < count) { |
---|
358 | do { |
---|
359 | c = mcfuart_poll_read(&uart[minor]); |
---|
360 | } while (c == -1); |
---|
361 | if (c == '\r') |
---|
362 | c = '\n'; |
---|
363 | *(buf++) = c; |
---|
364 | n++; |
---|
365 | if (c == '\n') |
---|
366 | break; |
---|
367 | } |
---|
368 | argp->bytes_moved = n; |
---|
369 | return RTEMS_SUCCESSFUL; |
---|
370 | } |
---|
371 | } |
---|
372 | |
---|
373 | /* console_write -- |
---|
374 | * Write to the console device |
---|
375 | * |
---|
376 | * PARAMETERS: |
---|
377 | * major - major device number for console devices |
---|
378 | * minor - minor device number for console |
---|
379 | * arg - device write argument |
---|
380 | * |
---|
381 | * RETURNS: |
---|
382 | * RTEMS error code |
---|
383 | */ |
---|
384 | rtems_device_driver |
---|
385 | console_write(rtems_device_major_number major, |
---|
386 | rtems_device_minor_number minor, |
---|
387 | void *arg |
---|
388 | ) |
---|
389 | { |
---|
390 | if (console_mode != CONSOLE_MODE_RAW) { |
---|
391 | return rtems_termios_write (arg); |
---|
392 | } else { |
---|
393 | rtems_libio_rw_args_t *argp = arg; |
---|
394 | char cr = '\r'; |
---|
395 | char *buf = argp->buffer; |
---|
396 | int count = argp->count; |
---|
397 | int i; |
---|
398 | |
---|
399 | for (i = 0; i < count; i++) { |
---|
400 | if (*buf == '\n') |
---|
401 | mcfuart_poll_write(&uart[minor], &cr, 1); |
---|
402 | mcfuart_poll_write(&uart[minor], buf, 1); |
---|
403 | buf++; |
---|
404 | } |
---|
405 | argp->bytes_moved = count; |
---|
406 | return RTEMS_SUCCESSFUL; |
---|
407 | } |
---|
408 | } |
---|
409 | |
---|
410 | /* console_control -- |
---|
411 | * Handle console device I/O control (IOCTL) |
---|
412 | * |
---|
413 | * PARAMETERS: |
---|
414 | * major - major device number for console devices |
---|
415 | * minor - minor device number for console |
---|
416 | * arg - device ioctl argument |
---|
417 | * |
---|
418 | * RETURNS: |
---|
419 | * RTEMS error code |
---|
420 | */ |
---|
421 | rtems_device_driver |
---|
422 | console_control(rtems_device_major_number major, |
---|
423 | rtems_device_minor_number minor, |
---|
424 | void *arg) |
---|
425 | { |
---|
426 | if (console_mode != CONSOLE_MODE_RAW) { |
---|
427 | return rtems_termios_ioctl (arg); |
---|
428 | } else { |
---|
429 | return RTEMS_SUCCESSFUL; |
---|
430 | } |
---|
431 | } |
---|