1 | /* |
---|
2 | * Console driver for SH-4 UART modules |
---|
3 | * |
---|
4 | * Copyright (C) 2000 OKTET Ltd., St.-Petersburg, Russia |
---|
5 | * Author: Alexandra Kossovsky <sasha@oktet.ru> |
---|
6 | * |
---|
7 | * COPYRIGHT (c) 1989-1998. |
---|
8 | * On-Line Applications Research Corporation (OAR). |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * |
---|
13 | * http://www.OARcorp.com/rtems/license.html. |
---|
14 | * |
---|
15 | * $Id$ |
---|
16 | */ |
---|
17 | |
---|
18 | #include <termios.h> |
---|
19 | #include <rtems/libio.h> |
---|
20 | #include "sh/sh4uart.h" |
---|
21 | |
---|
22 | /* Descriptor structures for two on-chip UART channels */ |
---|
23 | sh4uart sh4_uarts[2]; |
---|
24 | |
---|
25 | /* Console operations mode: |
---|
26 | * 0 - raw (non-termios) polled input/output |
---|
27 | * 1 - termios-based polled input/output |
---|
28 | * 2 - termios-based interrupt-driven input/output |
---|
29 | * 3 - non-termios over gdb stub |
---|
30 | */ |
---|
31 | int console_mode = 3; |
---|
32 | #define CONSOLE_MODE_RAW (0) |
---|
33 | #define CONSOLE_MODE_POLL (1) |
---|
34 | #define CONSOLE_MODE_INT (2) |
---|
35 | #define CONSOLE_MODE_IPL (3) |
---|
36 | |
---|
37 | /* Wrapper functions for SH-4 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 sh4uart_poll_read(&sh4_uarts[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 int |
---|
67 | console_interrupt_write(int minor, const char *buf, int len) |
---|
68 | { |
---|
69 | return sh4uart_interrupt_write(&sh4_uarts[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 int |
---|
84 | console_poll_write(int minor, const char *buf, int len) |
---|
85 | { |
---|
86 | return sh4uart_poll_write(&sh4_uarts[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 sh4uart_set_attributes(&sh4_uarts[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(sh4_uarts)/sizeof(sh4_uarts[0])) |
---|
118 | return sh4uart_stop_remote_tx(&sh4_uarts[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(sh4_uarts)/sizeof(sh4_uarts[0])) |
---|
134 | return sh4uart_start_remote_tx(&sh4_uarts[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 | |
---|
156 | sc = sh4uart_init(&sh4_uarts[minor], /* uart */ |
---|
157 | args->iop->data1, /* tty */ |
---|
158 | minor+1, /* channel */ |
---|
159 | (console_mode == CONSOLE_MODE_INT)); |
---|
160 | |
---|
161 | if (sc == RTEMS_SUCCESSFUL) |
---|
162 | sc = sh4uart_reset(&sh4_uarts[minor]); |
---|
163 | |
---|
164 | return sc; |
---|
165 | } |
---|
166 | |
---|
167 | /* console_last_close -- |
---|
168 | * wrapper for UART controller close function |
---|
169 | * |
---|
170 | * PARAMETERS: |
---|
171 | * major - major device number |
---|
172 | * minor - minor device number |
---|
173 | * arg - libio device close argument |
---|
174 | * |
---|
175 | * RETURNS: |
---|
176 | * error code |
---|
177 | */ |
---|
178 | static int |
---|
179 | console_last_close(int major, int minor, void *arg) |
---|
180 | { |
---|
181 | return sh4uart_disable(&sh4_uarts[minor]); |
---|
182 | } |
---|
183 | |
---|
184 | /* console_reserve_resources -- |
---|
185 | * reserve termios resources for 2 UART channels |
---|
186 | * |
---|
187 | * PARAMETERS: |
---|
188 | * configuration -- pointer to the RTEMS configuration table |
---|
189 | * |
---|
190 | * RETURNS: |
---|
191 | * none |
---|
192 | */ |
---|
193 | void |
---|
194 | console_reserve_resources(rtems_configuration_table *configuration) |
---|
195 | { |
---|
196 | if ((console_mode != CONSOLE_MODE_RAW) && |
---|
197 | (console_mode != CONSOLE_MODE_IPL)) |
---|
198 | rtems_termios_reserve_resources (configuration, 2); |
---|
199 | } |
---|
200 | |
---|
201 | /* console_initialize -- |
---|
202 | * This routine initializes the console IO drivers and register devices |
---|
203 | * in RTEMS I/O system. |
---|
204 | * |
---|
205 | * PARAMETERS: |
---|
206 | * major - major console device number |
---|
207 | * minor - minor console device number (not used) |
---|
208 | * arg - device initialize argument |
---|
209 | * |
---|
210 | * RETURNS: |
---|
211 | * RTEMS error code (RTEMS_SUCCESSFUL if device initialized successfuly) |
---|
212 | */ |
---|
213 | rtems_device_driver |
---|
214 | console_initialize(rtems_device_major_number major, |
---|
215 | rtems_device_minor_number minor, |
---|
216 | void *arg) |
---|
217 | { |
---|
218 | rtems_status_code status; |
---|
219 | |
---|
220 | #ifdef SH4_WITH_IPL |
---|
221 | /* booting from flash we cannot have IPL console */ |
---|
222 | if (boot_mode != SH4_BOOT_MODE_IPL && console_mode == CONSOLE_MODE_IPL) |
---|
223 | console_mode = CONSOLE_MODE_INT; |
---|
224 | |
---|
225 | /* break out from gdb if neccessary */ |
---|
226 | if (boot_mode == SH4_BOOT_MODE_IPL && console_mode != CONSOLE_MODE_IPL) |
---|
227 | ipl_finish(); |
---|
228 | #endif |
---|
229 | |
---|
230 | /* |
---|
231 | * Set up TERMIOS |
---|
232 | */ |
---|
233 | if ((console_mode != CONSOLE_MODE_RAW) && |
---|
234 | (console_mode != CONSOLE_MODE_IPL)) |
---|
235 | rtems_termios_initialize (); |
---|
236 | |
---|
237 | /* |
---|
238 | * Register the devices |
---|
239 | */ |
---|
240 | status = rtems_io_register_name ("/dev/console", major, 0); |
---|
241 | if (status != RTEMS_SUCCESSFUL) |
---|
242 | rtems_fatal_error_occurred (status); |
---|
243 | |
---|
244 | status = rtems_io_register_name ("/dev/aux", major, 1); |
---|
245 | if (status != RTEMS_SUCCESSFUL) |
---|
246 | rtems_fatal_error_occurred (status); |
---|
247 | |
---|
248 | if (console_mode == CONSOLE_MODE_RAW) |
---|
249 | { |
---|
250 | rtems_status_code sc; |
---|
251 | sc = sh4uart_init(&sh4_uarts[0], /* uart */ |
---|
252 | NULL, /* tty */ |
---|
253 | 1, /* UART channel number */ |
---|
254 | 0); /* Poll-mode */ |
---|
255 | |
---|
256 | if (sc == RTEMS_SUCCESSFUL) |
---|
257 | sc = sh4uart_reset(&sh4_uarts[0]); |
---|
258 | |
---|
259 | sc = sh4uart_init(&sh4_uarts[1], /* uart */ |
---|
260 | NULL, /* tty */ |
---|
261 | 2, /* UART channel number */ |
---|
262 | 0); /* Poll-mode */ |
---|
263 | |
---|
264 | if (sc == RTEMS_SUCCESSFUL) |
---|
265 | sc = sh4uart_reset(&sh4_uarts[1]); |
---|
266 | |
---|
267 | return sc; |
---|
268 | } |
---|
269 | |
---|
270 | return RTEMS_SUCCESSFUL; |
---|
271 | } |
---|
272 | |
---|
273 | /* console_open -- |
---|
274 | * Open console device driver. Pass appropriate termios callback |
---|
275 | * functions to termios library. |
---|
276 | * |
---|
277 | * PARAMETERS: |
---|
278 | * major - major device number for console devices |
---|
279 | * minor - minor device number for console |
---|
280 | * arg - device opening argument |
---|
281 | * |
---|
282 | * RETURNS: |
---|
283 | * RTEMS error code |
---|
284 | */ |
---|
285 | rtems_device_driver |
---|
286 | console_open(rtems_device_major_number major, |
---|
287 | rtems_device_minor_number minor, |
---|
288 | void *arg) |
---|
289 | { |
---|
290 | static const rtems_termios_callbacks intr_callbacks = { |
---|
291 | console_first_open, /* firstOpen */ |
---|
292 | console_last_close, /* lastClose */ |
---|
293 | NULL, /* pollRead */ |
---|
294 | console_interrupt_write, /* write */ |
---|
295 | console_set_attributes, /* setAttributes */ |
---|
296 | console_stop_remote_tx, /* stopRemoteTx */ |
---|
297 | console_start_remote_tx, /* startRemoteTx */ |
---|
298 | 1 /* outputUsesInterrupts */ |
---|
299 | }; |
---|
300 | static const rtems_termios_callbacks poll_callbacks = { |
---|
301 | console_first_open, /* firstOpen */ |
---|
302 | console_last_close, /* lastClose */ |
---|
303 | console_poll_read, /* pollRead */ |
---|
304 | console_poll_write, /* write */ |
---|
305 | console_set_attributes, /* setAttributes */ |
---|
306 | console_stop_remote_tx, /* stopRemoteTx */ |
---|
307 | console_start_remote_tx, /* startRemoteTx */ |
---|
308 | 0 /* outputUsesInterrupts */ |
---|
309 | }; |
---|
310 | |
---|
311 | switch (console_mode) |
---|
312 | { |
---|
313 | case CONSOLE_MODE_RAW: |
---|
314 | case CONSOLE_MODE_IPL: |
---|
315 | return RTEMS_SUCCESSFUL; |
---|
316 | |
---|
317 | case CONSOLE_MODE_INT: |
---|
318 | return rtems_termios_open(major, minor, arg, &intr_callbacks); |
---|
319 | |
---|
320 | case CONSOLE_MODE_POLL: |
---|
321 | return rtems_termios_open(major, minor, arg, &poll_callbacks); |
---|
322 | |
---|
323 | default: |
---|
324 | rtems_fatal_error_occurred(0xC07A1310); |
---|
325 | } |
---|
326 | |
---|
327 | return RTEMS_INTERNAL_ERROR; |
---|
328 | } |
---|
329 | |
---|
330 | /* console_close -- |
---|
331 | * Close console device. |
---|
332 | * |
---|
333 | * PARAMETERS: |
---|
334 | * major - major device number for console devices |
---|
335 | * minor - minor device number for console |
---|
336 | * arg - device close argument |
---|
337 | * |
---|
338 | * RETURNS: |
---|
339 | * RTEMS error code |
---|
340 | */ |
---|
341 | rtems_device_driver |
---|
342 | console_close(rtems_device_major_number major, |
---|
343 | rtems_device_minor_number minor, |
---|
344 | void *arg) |
---|
345 | { |
---|
346 | if ((console_mode != CONSOLE_MODE_RAW) && |
---|
347 | (console_mode != CONSOLE_MODE_IPL)) |
---|
348 | return rtems_termios_close (arg); |
---|
349 | else |
---|
350 | return RTEMS_SUCCESSFUL; |
---|
351 | } |
---|
352 | |
---|
353 | /* console_read -- |
---|
354 | * Read from the console device |
---|
355 | * |
---|
356 | * PARAMETERS: |
---|
357 | * major - major device number for console devices |
---|
358 | * minor - minor device number for console |
---|
359 | * arg - device read argument |
---|
360 | * |
---|
361 | * RETURNS: |
---|
362 | * RTEMS error code |
---|
363 | */ |
---|
364 | rtems_device_driver |
---|
365 | console_read(rtems_device_major_number major, |
---|
366 | rtems_device_minor_number minor, |
---|
367 | void *arg) |
---|
368 | { |
---|
369 | if ((console_mode != CONSOLE_MODE_RAW) && |
---|
370 | (console_mode != CONSOLE_MODE_IPL)) |
---|
371 | { |
---|
372 | return rtems_termios_read (arg); |
---|
373 | } |
---|
374 | else |
---|
375 | { |
---|
376 | rtems_libio_rw_args_t *argp = arg; |
---|
377 | char *buf = argp->buffer; |
---|
378 | int count = argp->count; |
---|
379 | int n = 0; |
---|
380 | int c; |
---|
381 | while (n < count) |
---|
382 | { |
---|
383 | do { |
---|
384 | c = (console_mode == CONSOLE_MODE_RAW) ? |
---|
385 | sh4uart_poll_read(&sh4_uarts[minor]) : |
---|
386 | ipl_console_poll_read(minor); |
---|
387 | } while (c == -1); |
---|
388 | if (c == '\r') |
---|
389 | c = '\n'; |
---|
390 | *(buf++) = c; |
---|
391 | n++; |
---|
392 | if (c == '\n') |
---|
393 | break; |
---|
394 | } |
---|
395 | argp->bytes_moved = n; |
---|
396 | return RTEMS_SUCCESSFUL; |
---|
397 | } |
---|
398 | } |
---|
399 | |
---|
400 | /* console_write -- |
---|
401 | * Write to the console device |
---|
402 | * |
---|
403 | * PARAMETERS: |
---|
404 | * major - major device number for console devices |
---|
405 | * minor - minor device number for console |
---|
406 | * arg - device write argument |
---|
407 | * |
---|
408 | * RETURNS: |
---|
409 | * RTEMS error code |
---|
410 | */ |
---|
411 | rtems_device_driver |
---|
412 | console_write(rtems_device_major_number major, |
---|
413 | rtems_device_minor_number minor, |
---|
414 | void *arg) |
---|
415 | { |
---|
416 | switch (console_mode) |
---|
417 | { |
---|
418 | case CONSOLE_MODE_POLL: |
---|
419 | case CONSOLE_MODE_INT: |
---|
420 | return rtems_termios_write (arg); |
---|
421 | case CONSOLE_MODE_RAW: |
---|
422 | { |
---|
423 | rtems_libio_rw_args_t *argp = arg; |
---|
424 | char cr = '\r'; |
---|
425 | char *buf = argp->buffer; |
---|
426 | int count = argp->count; |
---|
427 | int i; |
---|
428 | |
---|
429 | for (i = 0; i < count; i++) |
---|
430 | { |
---|
431 | if (*buf == '\n') |
---|
432 | sh4uart_poll_write(&sh4_uarts[minor], &cr, 1); |
---|
433 | sh4uart_poll_write(&sh4_uarts[minor], buf, 1); |
---|
434 | buf++; |
---|
435 | } |
---|
436 | argp->bytes_moved = count; |
---|
437 | return RTEMS_SUCCESSFUL; |
---|
438 | } |
---|
439 | #ifdef SH4_WITH_IPL |
---|
440 | case CONSOLE_MODE_IPL: |
---|
441 | { |
---|
442 | rtems_libio_rw_args_t *argp = arg; |
---|
443 | char *buf = argp->buffer; |
---|
444 | int count = argp->count; |
---|
445 | ipl_console_poll_write(minor, buf, count); |
---|
446 | argp->bytes_moved = count; |
---|
447 | return RTEMS_SUCCESSFUL; |
---|
448 | } |
---|
449 | #endif |
---|
450 | default: /* Unreachable */ |
---|
451 | return RTEMS_NOT_DEFINED; |
---|
452 | } |
---|
453 | } |
---|
454 | |
---|
455 | /* console_control -- |
---|
456 | * Handle console device I/O control (IOCTL) |
---|
457 | * |
---|
458 | * PARAMETERS: |
---|
459 | * major - major device number for console devices |
---|
460 | * minor - minor device number for console |
---|
461 | * arg - device ioctl argument |
---|
462 | * |
---|
463 | * RETURNS: |
---|
464 | * RTEMS error code |
---|
465 | */ |
---|
466 | rtems_device_driver |
---|
467 | console_control(rtems_device_major_number major, |
---|
468 | rtems_device_minor_number minor, |
---|
469 | void *arg) |
---|
470 | { |
---|
471 | if ((console_mode != CONSOLE_MODE_RAW) && |
---|
472 | (console_mode != CONSOLE_MODE_IPL)) |
---|
473 | { |
---|
474 | return rtems_termios_ioctl (arg); |
---|
475 | } |
---|
476 | else |
---|
477 | { |
---|
478 | return RTEMS_SUCCESSFUL; |
---|
479 | } |
---|
480 | } |
---|
481 | |
---|
482 | |
---|
483 | |
---|