1 | /* |
---|
2 | * Multi UART console serial I/O. |
---|
3 | * |
---|
4 | * TO DO: Add DMA input/output |
---|
5 | */ |
---|
6 | |
---|
7 | #include <stdio.h> |
---|
8 | #include <fcntl.h> |
---|
9 | #include <termios.h> |
---|
10 | #include <malloc.h> |
---|
11 | |
---|
12 | #include <rtems/libio.h> |
---|
13 | #include <rtems/termiostypes.h> |
---|
14 | #include <rtems/bspIo.h> |
---|
15 | #include <rtems/console.h> |
---|
16 | #include <bsp.h> |
---|
17 | |
---|
18 | #define UART_INTC0_IRQ_VECTOR(x) (64+13+(x)) |
---|
19 | |
---|
20 | #define MCF5235_UART_USR_ERROR ( MCF5235_UART_USR_RB | \ |
---|
21 | MCF5235_UART_USR_FE | \ |
---|
22 | MCF5235_UART_USR_PE | \ |
---|
23 | MCF5235_UART_USR_OE ) |
---|
24 | |
---|
25 | static ssize_t IntUartPollWrite(int minor, const char *buf, size_t len); |
---|
26 | static ssize_t IntUartInterruptWrite (int minor, const char *buf, size_t len); |
---|
27 | |
---|
28 | static void |
---|
29 | _BSP_null_char( char c ) |
---|
30 | { |
---|
31 | int level; |
---|
32 | |
---|
33 | rtems_interrupt_disable(level); |
---|
34 | while ( (MCF5235_UART_USR(CONSOLE_PORT) & MCF5235_UART_USR_TXRDY) == 0 ) |
---|
35 | continue; |
---|
36 | MCF5235_UART_UTB(CONSOLE_PORT) = c; |
---|
37 | while ( (MCF5235_UART_USR(CONSOLE_PORT) & MCF5235_UART_USR_TXRDY) == 0 ) |
---|
38 | continue; |
---|
39 | rtems_interrupt_enable(level); |
---|
40 | } |
---|
41 | BSP_output_char_function_type BSP_output_char = _BSP_null_char; |
---|
42 | BSP_polling_getchar_function_type BSP_poll_char = NULL; |
---|
43 | |
---|
44 | #define MAX_UART_INFO 3 |
---|
45 | #define RX_BUFFER_SIZE 512 |
---|
46 | |
---|
47 | struct IntUartInfoStruct |
---|
48 | { |
---|
49 | int iomode; |
---|
50 | volatile int uimr; |
---|
51 | int baud; |
---|
52 | int databits; |
---|
53 | int parity; |
---|
54 | int stopbits; |
---|
55 | int hwflow; |
---|
56 | int rx_in; |
---|
57 | int rx_out; |
---|
58 | char rx_buffer[RX_BUFFER_SIZE]; |
---|
59 | void *ttyp; |
---|
60 | }; |
---|
61 | |
---|
62 | struct IntUartInfoStruct IntUartInfo[MAX_UART_INFO]; |
---|
63 | |
---|
64 | /*************************************************************************** |
---|
65 | Function : IntUartSet |
---|
66 | |
---|
67 | Description : This updates the hardware UART settings. |
---|
68 | ***************************************************************************/ |
---|
69 | static void |
---|
70 | IntUartSet(int minor, int baud, int databits, int parity, int stopbits, int hwflow) |
---|
71 | { |
---|
72 | int divisor; |
---|
73 | uint32_t clock_speed; |
---|
74 | uint8_t umr1 = 0; |
---|
75 | uint8_t umr2 = 0; |
---|
76 | struct IntUartInfoStruct *info = &IntUartInfo[minor]; |
---|
77 | int level; |
---|
78 | |
---|
79 | rtems_interrupt_disable(level); |
---|
80 | |
---|
81 | |
---|
82 | /* disable interrupts, clear RTS line, and disable the UARTS */ |
---|
83 | MCF5235_UART_UIMR(minor) = 0; |
---|
84 | MCF5235_UART_UOP0(minor) = 1; |
---|
85 | MCF5235_UART_UCR(minor) = (MCF5235_UART_UCR_TX_DISABLED | MCF5235_UART_UCR_RX_DISABLED); |
---|
86 | |
---|
87 | /* save the current values */ |
---|
88 | info->uimr = 0; |
---|
89 | info->baud = baud; |
---|
90 | info->databits = databits; |
---|
91 | info->parity = parity; |
---|
92 | info->stopbits = stopbits; |
---|
93 | info->hwflow = hwflow; |
---|
94 | |
---|
95 | clock_speed = get_CPU_clock_speed(); |
---|
96 | /* determine the baud divisor value */ |
---|
97 | divisor = ((clock_speed/2) / ( 32 * baud )); |
---|
98 | if ( divisor < 2 ) { |
---|
99 | divisor = 2; |
---|
100 | } |
---|
101 | |
---|
102 | /* check to see if doing hardware flow control */ |
---|
103 | if ( hwflow ) |
---|
104 | { |
---|
105 | /* set hardware flow options */ |
---|
106 | umr1 |= MCF5235_UART_UMR_RXRTS; |
---|
107 | umr2 |= MCF5235_UART_UMR_TXCTS; |
---|
108 | } |
---|
109 | |
---|
110 | /* determine the new umr values */ |
---|
111 | umr1 |= (parity | databits); |
---|
112 | umr2 |= (stopbits); |
---|
113 | |
---|
114 | /* reset the uart */ |
---|
115 | MCF5235_UART_UCR(minor) = MCF5235_UART_UCR_RESET_ERROR; |
---|
116 | MCF5235_UART_UCR(minor) = MCF5235_UART_UCR_RESET_RX; |
---|
117 | MCF5235_UART_UCR(minor) = MCF5235_UART_UCR_RESET_TX; |
---|
118 | |
---|
119 | /* reset the uart mode register and update values */ |
---|
120 | MCF5235_UART_UCR(minor) = MCF5235_UART_UCR_RESET_MR; |
---|
121 | MCF5235_UART_UMR(minor) = umr1; |
---|
122 | MCF5235_UART_UMR(minor) = umr2; |
---|
123 | |
---|
124 | /* set the baud rate values */ |
---|
125 | MCF5235_UART_UCSR(minor) = (MCF5235_UART_UCSR_RCS_SYS_CLK | MCF5235_UART_UCSR_TCS_SYS_CLK); |
---|
126 | MCF5235_UART_UBG1(minor) = (divisor & 0xff00) >> 8; |
---|
127 | MCF5235_UART_UBG2(minor) = (divisor & 0x00ff); |
---|
128 | |
---|
129 | /* enable the uart */ |
---|
130 | MCF5235_UART_UCR(minor) = (MCF5235_UART_UCR_TX_ENABLED | MCF5235_UART_UCR_RX_ENABLED); |
---|
131 | |
---|
132 | /* check to see if interrupts need to be enabled */ |
---|
133 | if ( info->iomode != TERMIOS_POLLED ) |
---|
134 | { |
---|
135 | /* enable rx interrupts */ |
---|
136 | info->uimr |= MCF5235_UART_UIMR_FFULL; |
---|
137 | MCF5235_UART_UIMR(minor) = info->uimr; |
---|
138 | } |
---|
139 | |
---|
140 | /* check to see if doing hardware flow control */ |
---|
141 | if ( hwflow ) |
---|
142 | { |
---|
143 | /* assert the RTS line */ |
---|
144 | MCF5235_UART_UOP1(minor) = 1; |
---|
145 | } |
---|
146 | |
---|
147 | rtems_interrupt_enable(level); |
---|
148 | |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | /*************************************************************************** |
---|
153 | Function : IntUartSetAttributes |
---|
154 | |
---|
155 | Description : This provides the hardware-dependent portion of tcsetattr(). |
---|
156 | value and sets it. At the moment this just sets the baud rate. |
---|
157 | |
---|
158 | Note: The highest baudrate is 115200 as this stays within |
---|
159 | an error of +/- 5% at 25MHz processor clock |
---|
160 | ***************************************************************************/ |
---|
161 | static int |
---|
162 | IntUartSetAttributes(int minor, const struct termios *t) |
---|
163 | { |
---|
164 | /* set default index values */ |
---|
165 | int baud = (int)19200; |
---|
166 | int databits = (int)MCF5235_UART_UMR_BC_8; |
---|
167 | int parity = (int)MCF5235_UART_UMR_PM_NONE; |
---|
168 | int stopbits = (int)MCF5235_UART_UMR_STOP_BITS_1; |
---|
169 | int hwflow = (int)0; |
---|
170 | struct IntUartInfoStruct *info = &IntUartInfo[minor]; |
---|
171 | |
---|
172 | /* check to see if input is valid */ |
---|
173 | if ( t != (const struct termios *)0 ) |
---|
174 | { |
---|
175 | /* determine baud rate index */ |
---|
176 | baud = rtems_termios_baud_to_number(t->c_ospeed); |
---|
177 | |
---|
178 | /* determine data bits */ |
---|
179 | switch ( t->c_cflag & CSIZE ) |
---|
180 | { |
---|
181 | case CS5: |
---|
182 | databits = (int)MCF5235_UART_UMR_BC_5; |
---|
183 | break; |
---|
184 | case CS6: |
---|
185 | databits = (int)MCF5235_UART_UMR_BC_6; |
---|
186 | break; |
---|
187 | case CS7: |
---|
188 | databits = (int)MCF5235_UART_UMR_BC_7; |
---|
189 | break; |
---|
190 | case CS8: |
---|
191 | databits = (int)MCF5235_UART_UMR_BC_8; |
---|
192 | break; |
---|
193 | } |
---|
194 | |
---|
195 | /* determine if parity is enabled */ |
---|
196 | if ( t->c_cflag & PARENB ) |
---|
197 | { |
---|
198 | if ( t->c_cflag & PARODD ) |
---|
199 | { |
---|
200 | /* odd parity */ |
---|
201 | parity = (int)MCF5235_UART_UMR_PM_ODD; |
---|
202 | } |
---|
203 | else |
---|
204 | { |
---|
205 | /* even parity */ |
---|
206 | parity = (int)MCF5235_UART_UMR_PM_EVEN; |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | /* determine stop bits */ |
---|
211 | if ( t->c_cflag & CSTOPB ) |
---|
212 | { |
---|
213 | /* two stop bits */ |
---|
214 | stopbits = (int)MCF5235_UART_UMR_STOP_BITS_2; |
---|
215 | } |
---|
216 | |
---|
217 | /* check to see if hardware flow control */ |
---|
218 | if ( t->c_cflag & CRTSCTS ) |
---|
219 | { |
---|
220 | hwflow = 1; |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | /* check to see if values have changed */ |
---|
225 | if ( ( baud != info->baud ) || |
---|
226 | ( databits != info->databits ) || |
---|
227 | ( parity != info->parity ) || |
---|
228 | ( stopbits != info->stopbits ) || |
---|
229 | ( hwflow != info->hwflow ) ) |
---|
230 | { |
---|
231 | |
---|
232 | /* call function to set values */ |
---|
233 | IntUartSet(minor, baud, databits, parity, stopbits, hwflow); |
---|
234 | } |
---|
235 | |
---|
236 | return( RTEMS_SUCCESSFUL ); |
---|
237 | |
---|
238 | } |
---|
239 | |
---|
240 | /*************************************************************************** |
---|
241 | Function : IntUartInterruptHandler |
---|
242 | |
---|
243 | Description : This is the interrupt handler for the internal uart. It |
---|
244 | determines which channel caused the interrupt before queueing any received |
---|
245 | chars and dequeueing chars waiting for transmission. |
---|
246 | ***************************************************************************/ |
---|
247 | static rtems_isr |
---|
248 | IntUartInterruptHandler(rtems_vector_number v) |
---|
249 | { |
---|
250 | unsigned int chan = v - UART_INTC0_IRQ_VECTOR(0); |
---|
251 | struct IntUartInfoStruct *info = &IntUartInfo[chan]; |
---|
252 | |
---|
253 | /* check to see if received data */ |
---|
254 | if ( MCF5235_UART_UISR(chan) & MCF5235_UART_UISR_RXRDY ) |
---|
255 | { |
---|
256 | /* read data and put into the receive buffer */ |
---|
257 | while ( MCF5235_UART_USR(chan) & MCF5235_UART_USR_RXRDY ) |
---|
258 | { |
---|
259 | |
---|
260 | if ( MCF5235_UART_USR(chan) & MCF5235_UART_USR_ERROR ) |
---|
261 | { |
---|
262 | /* clear the error */ |
---|
263 | MCF5235_UART_UCR(chan) = MCF5235_UART_UCR_RESET_ERROR; |
---|
264 | } |
---|
265 | /* put data in rx buffer and check for errors */ |
---|
266 | info->rx_buffer[info->rx_in] = MCF5235_UART_URB(chan); |
---|
267 | |
---|
268 | /* update buffer values */ |
---|
269 | info->rx_in++; |
---|
270 | |
---|
271 | if ( info->rx_in >= RX_BUFFER_SIZE ) |
---|
272 | { |
---|
273 | info->rx_in = 0; |
---|
274 | } |
---|
275 | } |
---|
276 | /* Make sure the port has been opened */ |
---|
277 | if ( info->ttyp ) |
---|
278 | { |
---|
279 | |
---|
280 | /* check to see if task driven */ |
---|
281 | if ( info->iomode == TERMIOS_TASK_DRIVEN ) |
---|
282 | { |
---|
283 | /* notify rx task that rx buffer has data */ |
---|
284 | rtems_termios_rxirq_occured(info->ttyp); |
---|
285 | } |
---|
286 | else |
---|
287 | { |
---|
288 | /* Push up the received data */ |
---|
289 | rtems_termios_enqueue_raw_characters(info->ttyp, info->rx_buffer, info->rx_in); |
---|
290 | info->rx_in = 0; |
---|
291 | } |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|
295 | /* check to see if data needs to be transmitted */ |
---|
296 | if ( ( info->uimr & MCF5235_UART_UIMR_TXRDY ) && |
---|
297 | ( MCF5235_UART_UISR(chan) & MCF5235_UART_UISR_TXRDY ) ) |
---|
298 | { |
---|
299 | |
---|
300 | /* disable tx interrupts */ |
---|
301 | info->uimr &= ~MCF5235_UART_UIMR_TXRDY; |
---|
302 | MCF5235_UART_UIMR(chan) = info->uimr; |
---|
303 | |
---|
304 | /* tell upper level that character has been sent */ |
---|
305 | if ( info->ttyp ) |
---|
306 | rtems_termios_dequeue_characters(info->ttyp, 1); |
---|
307 | } |
---|
308 | } |
---|
309 | |
---|
310 | |
---|
311 | |
---|
312 | /*************************************************************************** |
---|
313 | Function : IntUartInitialize |
---|
314 | |
---|
315 | Description : This initialises the internal uart hardware for all |
---|
316 | internal uarts. If the internal uart is to be interrupt driven then the |
---|
317 | interrupt vectors are hooked. |
---|
318 | ***************************************************************************/ |
---|
319 | static void |
---|
320 | IntUartInitialize(void) |
---|
321 | { |
---|
322 | unsigned int chan; |
---|
323 | struct IntUartInfoStruct *info; |
---|
324 | rtems_isr_entry old_handler; |
---|
325 | int level; |
---|
326 | |
---|
327 | for ( chan = 0; chan < MAX_UART_INFO; chan++ ) |
---|
328 | { |
---|
329 | info = &IntUartInfo[chan]; |
---|
330 | |
---|
331 | info->ttyp = NULL; |
---|
332 | info->rx_in = 0; |
---|
333 | info->rx_out = 0; |
---|
334 | info->baud = -1; |
---|
335 | info->databits = -1; |
---|
336 | info->parity = -1; |
---|
337 | info->stopbits = -1; |
---|
338 | info->hwflow = -1; |
---|
339 | info->iomode = TERMIOS_POLLED; /*polled console io */ |
---|
340 | |
---|
341 | MCF5235_UART_UACR(chan) = 0; |
---|
342 | MCF5235_UART_UIMR(chan) = 0; |
---|
343 | if ( info->iomode != TERMIOS_POLLED ) |
---|
344 | { |
---|
345 | rtems_interrupt_catch (IntUartInterruptHandler, |
---|
346 | UART_INTC0_IRQ_VECTOR(chan), |
---|
347 | &old_handler); |
---|
348 | } |
---|
349 | |
---|
350 | /* set uart default values */ |
---|
351 | IntUartSetAttributes(chan, NULL); |
---|
352 | |
---|
353 | /* unmask interrupt */ |
---|
354 | rtems_interrupt_disable(level); |
---|
355 | switch(chan) { |
---|
356 | case 0: |
---|
357 | MCF5235_INTC0_ICR13 = MCF5235_INTC_ICR_IL(UART0_IRQ_LEVEL) | |
---|
358 | MCF5235_INTC_ICR_IP(UART0_IRQ_PRIORITY); |
---|
359 | MCF5235_INTC0_IMRL &= ~(MCF5235_INTC0_IMRL_INT13 | |
---|
360 | MCF5235_INTC0_IMRL_MASKALL); |
---|
361 | break; |
---|
362 | |
---|
363 | case 1: |
---|
364 | MCF5235_INTC0_ICR14 = MCF5235_INTC_ICR_IL(UART1_IRQ_LEVEL) | |
---|
365 | MCF5235_INTC_ICR_IP(UART1_IRQ_PRIORITY); |
---|
366 | MCF5235_INTC0_IMRL &= ~(MCF5235_INTC0_IMRL_INT14 | |
---|
367 | MCF5235_INTC0_IMRL_MASKALL); |
---|
368 | break; |
---|
369 | |
---|
370 | case 2: |
---|
371 | MCF5235_INTC0_ICR15 = MCF5235_INTC_ICR_IL(UART2_IRQ_LEVEL) | |
---|
372 | MCF5235_INTC_ICR_IP(UART2_IRQ_PRIORITY); |
---|
373 | MCF5235_INTC0_IMRL &= ~(MCF5235_INTC0_IMRL_INT15 | |
---|
374 | MCF5235_INTC0_IMRL_MASKALL); |
---|
375 | break; |
---|
376 | } |
---|
377 | rtems_interrupt_enable(level); |
---|
378 | |
---|
379 | } /* of chan loop */ |
---|
380 | |
---|
381 | |
---|
382 | } /* IntUartInitialise */ |
---|
383 | |
---|
384 | |
---|
385 | /*************************************************************************** |
---|
386 | Function : IntUartInterruptWrite |
---|
387 | |
---|
388 | Description : This writes a single character to the appropriate uart |
---|
389 | channel. This is either called during an interrupt or in the user's task |
---|
390 | to initiate a transmit sequence. Calling this routine enables Tx |
---|
391 | interrupts. |
---|
392 | ***************************************************************************/ |
---|
393 | static ssize_t |
---|
394 | IntUartInterruptWrite (int minor, const char *buf, size_t len) |
---|
395 | { |
---|
396 | if (len > 0) { |
---|
397 | /* write out character */ |
---|
398 | MCF5235_UART_UTB(minor) = *buf; |
---|
399 | |
---|
400 | /* enable tx interrupt */ |
---|
401 | IntUartInfo[minor].uimr |= MCF5235_UART_UIMR_TXRDY; |
---|
402 | MCF5235_UART_UIMR(minor) = IntUartInfo[minor].uimr; |
---|
403 | } |
---|
404 | |
---|
405 | return( 0 ); |
---|
406 | } |
---|
407 | |
---|
408 | /*************************************************************************** |
---|
409 | Function : IntUartInterruptOpen |
---|
410 | |
---|
411 | Description : This enables interrupts when the tty is opened. |
---|
412 | ***************************************************************************/ |
---|
413 | static int |
---|
414 | IntUartInterruptOpen(int major, int minor, void *arg) |
---|
415 | { |
---|
416 | struct IntUartInfoStruct *info = &IntUartInfo[minor]; |
---|
417 | |
---|
418 | /* enable the uart */ |
---|
419 | MCF5235_UART_UCR(minor) = (MCF5235_UART_UCR_TX_ENABLED | MCF5235_UART_UCR_RX_ENABLED); |
---|
420 | |
---|
421 | /* check to see if interrupts need to be enabled */ |
---|
422 | if ( info->iomode != TERMIOS_POLLED ) |
---|
423 | { |
---|
424 | /* enable rx interrupts */ |
---|
425 | info->uimr |= MCF5235_UART_UIMR_FFULL; |
---|
426 | MCF5235_UART_UIMR(minor) = info->uimr; |
---|
427 | } |
---|
428 | |
---|
429 | /* check to see if doing hardware flow control */ |
---|
430 | if ( info->hwflow ) |
---|
431 | { |
---|
432 | /* assert the RTS line */ |
---|
433 | MCF5235_UART_UOP1(minor) = 1; |
---|
434 | } |
---|
435 | |
---|
436 | return( 0 ); |
---|
437 | } |
---|
438 | |
---|
439 | |
---|
440 | |
---|
441 | /*************************************************************************** |
---|
442 | Function : IntUartInterruptClose |
---|
443 | |
---|
444 | Description : This disables interrupts when the tty is closed. |
---|
445 | ***************************************************************************/ |
---|
446 | static int |
---|
447 | IntUartInterruptClose(int major, int minor, void *arg) |
---|
448 | { |
---|
449 | struct IntUartInfoStruct *info = &IntUartInfo[minor]; |
---|
450 | |
---|
451 | /* disable the interrupts and the uart */ |
---|
452 | MCF5235_UART_UIMR(minor) = 0; |
---|
453 | MCF5235_UART_UCR(minor) = (MCF5235_UART_UCR_TX_DISABLED | MCF5235_UART_UCR_RX_DISABLED); |
---|
454 | |
---|
455 | /* reset values */ |
---|
456 | info->ttyp = NULL; |
---|
457 | info->uimr = 0; |
---|
458 | info->rx_in = 0; |
---|
459 | info->rx_out = 0; |
---|
460 | |
---|
461 | return( 0 ); |
---|
462 | } |
---|
463 | |
---|
464 | /*************************************************************************** |
---|
465 | Function : IntUartTaskRead |
---|
466 | |
---|
467 | Description : This reads all available characters from the internal uart |
---|
468 | and places them into the termios buffer. The rx interrupts will be |
---|
469 | re-enabled after all data has been read. |
---|
470 | ***************************************************************************/ |
---|
471 | static int |
---|
472 | IntUartTaskRead(int minor) |
---|
473 | { |
---|
474 | char buffer[RX_BUFFER_SIZE]; |
---|
475 | int count; |
---|
476 | int rx_in; |
---|
477 | int index = 0; |
---|
478 | struct IntUartInfoStruct *info = &IntUartInfo[minor]; |
---|
479 | |
---|
480 | /* determine number of values to copy out */ |
---|
481 | rx_in = info->rx_in; |
---|
482 | if ( info->rx_out <= rx_in ) |
---|
483 | { |
---|
484 | count = rx_in - info->rx_out; |
---|
485 | } |
---|
486 | else |
---|
487 | { |
---|
488 | count = (RX_BUFFER_SIZE - info->rx_out) + rx_in; |
---|
489 | } |
---|
490 | |
---|
491 | /* copy data into local buffer from rx buffer */ |
---|
492 | while ( ( index < count ) && ( index < RX_BUFFER_SIZE ) ) |
---|
493 | { |
---|
494 | /* copy data byte */ |
---|
495 | buffer[index] = info->rx_buffer[info->rx_out]; |
---|
496 | index++; |
---|
497 | |
---|
498 | /* increment rx buffer values */ |
---|
499 | info->rx_out++; |
---|
500 | if ( info->rx_out >= RX_BUFFER_SIZE ) |
---|
501 | { |
---|
502 | info->rx_out = 0; |
---|
503 | } |
---|
504 | } |
---|
505 | |
---|
506 | /* check to see if buffer is not empty */ |
---|
507 | if ( count > 0 ) |
---|
508 | { |
---|
509 | /* set characters into termios buffer */ |
---|
510 | rtems_termios_enqueue_raw_characters(info->ttyp, buffer, count); |
---|
511 | } |
---|
512 | |
---|
513 | return( EOF ); |
---|
514 | } |
---|
515 | |
---|
516 | |
---|
517 | |
---|
518 | /*************************************************************************** |
---|
519 | Function : IntUartPollRead |
---|
520 | |
---|
521 | Description : This reads a character from the internal uart. It returns |
---|
522 | to the caller without blocking if not character is waiting. |
---|
523 | ***************************************************************************/ |
---|
524 | static int |
---|
525 | IntUartPollRead (int minor) |
---|
526 | { |
---|
527 | if ( (MCF5235_UART_USR(minor) & MCF5235_UART_USR_RXRDY) == 0 ) |
---|
528 | return(-1); |
---|
529 | |
---|
530 | return(MCF5235_UART_URB(minor)); |
---|
531 | } |
---|
532 | |
---|
533 | |
---|
534 | /*************************************************************************** |
---|
535 | Function : IntUartPollWrite |
---|
536 | |
---|
537 | Description : This writes out each character in the buffer to the |
---|
538 | appropriate internal uart channel waiting till each one is sucessfully |
---|
539 | transmitted. |
---|
540 | ***************************************************************************/ |
---|
541 | static ssize_t |
---|
542 | IntUartPollWrite (int minor, const char *buf, size_t len) |
---|
543 | { |
---|
544 | size_t retval = len; |
---|
545 | /* loop over buffer */ |
---|
546 | while ( len-- ) |
---|
547 | { |
---|
548 | /* block until we can transmit */ |
---|
549 | while ( (MCF5235_UART_USR(minor) & MCF5235_UART_USR_TXRDY) == 0 ) |
---|
550 | continue; |
---|
551 | /* transmit data byte */ |
---|
552 | MCF5235_UART_UTB(minor) = *buf++; |
---|
553 | } |
---|
554 | return retval; |
---|
555 | } |
---|
556 | |
---|
557 | /*************************************************************************** |
---|
558 | Function : console_initialize |
---|
559 | |
---|
560 | Description : This initialises termios, both sets of uart hardware before |
---|
561 | registering /dev/tty devices for each channel and the system /dev/console. |
---|
562 | ***************************************************************************/ |
---|
563 | rtems_device_driver console_initialize( |
---|
564 | rtems_device_major_number major, |
---|
565 | rtems_device_minor_number minor, |
---|
566 | void *arg ) |
---|
567 | { |
---|
568 | rtems_status_code status; |
---|
569 | |
---|
570 | |
---|
571 | /* Set up TERMIOS */ |
---|
572 | rtems_termios_initialize (); |
---|
573 | |
---|
574 | /* set io modes for the different channels and initialize device */ |
---|
575 | IntUartInfo[minor].iomode = TERMIOS_IRQ_DRIVEN; |
---|
576 | IntUartInitialize(); |
---|
577 | |
---|
578 | /* Register the console port */ |
---|
579 | status = rtems_io_register_name ("/dev/console", major, CONSOLE_PORT); |
---|
580 | if ( status != RTEMS_SUCCESSFUL ) |
---|
581 | { |
---|
582 | rtems_fatal_error_occurred (status); |
---|
583 | } |
---|
584 | |
---|
585 | /* Register the other port */ |
---|
586 | if ( CONSOLE_PORT != 0 ) |
---|
587 | { |
---|
588 | status = rtems_io_register_name ("/dev/tty00", major, 0); |
---|
589 | if ( status != RTEMS_SUCCESSFUL ) |
---|
590 | { |
---|
591 | rtems_fatal_error_occurred (status); |
---|
592 | } |
---|
593 | } |
---|
594 | if ( CONSOLE_PORT != 1 ) |
---|
595 | { |
---|
596 | status = rtems_io_register_name ("/dev/tty01", major, 1); |
---|
597 | if ( status != RTEMS_SUCCESSFUL ) |
---|
598 | { |
---|
599 | rtems_fatal_error_occurred (status); |
---|
600 | } |
---|
601 | } |
---|
602 | |
---|
603 | return(RTEMS_SUCCESSFUL); |
---|
604 | } |
---|
605 | |
---|
606 | /*************************************************************************** |
---|
607 | Function : console_open |
---|
608 | |
---|
609 | Description : This actually opens the device depending on the minor |
---|
610 | number set during initialisation. The device specific access routines are |
---|
611 | passed to termios when the devices is opened depending on whether it is |
---|
612 | polled or not. |
---|
613 | ***************************************************************************/ |
---|
614 | rtems_device_driver console_open( |
---|
615 | rtems_device_major_number major, |
---|
616 | rtems_device_minor_number minor, |
---|
617 | void * arg) |
---|
618 | { |
---|
619 | rtems_status_code status = RTEMS_INVALID_NUMBER; |
---|
620 | rtems_libio_open_close_args_t *args = (rtems_libio_open_close_args_t *)arg; |
---|
621 | struct IntUartInfoStruct *info; |
---|
622 | |
---|
623 | static const rtems_termios_callbacks IntUartPollCallbacks = { |
---|
624 | NULL, /* firstOpen */ |
---|
625 | NULL, /* lastClose */ |
---|
626 | IntUartPollRead, /* pollRead */ |
---|
627 | IntUartPollWrite, /* write */ |
---|
628 | IntUartSetAttributes, /* setAttributes */ |
---|
629 | NULL, /* stopRemoteTx */ |
---|
630 | NULL, /* startRemoteTx */ |
---|
631 | TERMIOS_POLLED /* mode */ |
---|
632 | }; |
---|
633 | static const rtems_termios_callbacks IntUartIntrCallbacks = { |
---|
634 | IntUartInterruptOpen, /* firstOpen */ |
---|
635 | IntUartInterruptClose, /* lastClose */ |
---|
636 | NULL, /* pollRead */ |
---|
637 | IntUartInterruptWrite, /* write */ |
---|
638 | IntUartSetAttributes, /* setAttributes */ |
---|
639 | NULL, /* stopRemoteTx */ |
---|
640 | NULL, /* startRemoteTx */ |
---|
641 | TERMIOS_IRQ_DRIVEN /* mode */ |
---|
642 | }; |
---|
643 | |
---|
644 | static const rtems_termios_callbacks IntUartTaskCallbacks = { |
---|
645 | IntUartInterruptOpen, /* firstOpen */ |
---|
646 | IntUartInterruptClose, /* lastClose */ |
---|
647 | IntUartTaskRead, /* pollRead */ |
---|
648 | IntUartInterruptWrite, /* write */ |
---|
649 | IntUartSetAttributes, /* setAttributes */ |
---|
650 | NULL, /* stopRemoteTx */ |
---|
651 | NULL, /* startRemoteTx */ |
---|
652 | TERMIOS_TASK_DRIVEN /* mode */ |
---|
653 | }; |
---|
654 | |
---|
655 | /* open the port depending on the minor device number */ |
---|
656 | if ( ( minor >= 0 ) && ( minor < MAX_UART_INFO ) ) |
---|
657 | { |
---|
658 | info = &IntUartInfo[minor]; |
---|
659 | switch ( info->iomode ) |
---|
660 | { |
---|
661 | case TERMIOS_POLLED: |
---|
662 | status = rtems_termios_open(major, minor, arg, &IntUartPollCallbacks); |
---|
663 | break; |
---|
664 | case TERMIOS_IRQ_DRIVEN: |
---|
665 | status = rtems_termios_open(major, minor, arg, &IntUartIntrCallbacks); |
---|
666 | info->ttyp = args->iop->data1; |
---|
667 | break; |
---|
668 | case TERMIOS_TASK_DRIVEN: |
---|
669 | status = rtems_termios_open(major, minor, arg, &IntUartTaskCallbacks); |
---|
670 | info->ttyp = args->iop->data1; |
---|
671 | break; |
---|
672 | } |
---|
673 | } |
---|
674 | |
---|
675 | if (status == RTEMS_SUCCESSFUL) |
---|
676 | { |
---|
677 | /* |
---|
678 | * Reset the default baudrate. |
---|
679 | */ |
---|
680 | struct termios term; |
---|
681 | if (tcgetattr (STDIN_FILENO, &term) >= 0) |
---|
682 | { |
---|
683 | term.c_cflag &= ~(CSIZE); |
---|
684 | term.c_cflag |= CS8; |
---|
685 | term.c_ispeed = B19200; |
---|
686 | term.c_ospeed = B19200; |
---|
687 | tcsetattr (STDIN_FILENO, TCSANOW, &term); |
---|
688 | } |
---|
689 | } |
---|
690 | |
---|
691 | return( status ); |
---|
692 | } |
---|
693 | |
---|
694 | /*************************************************************************** |
---|
695 | Function : console_close |
---|
696 | |
---|
697 | Description : This closes the device via termios |
---|
698 | ***************************************************************************/ |
---|
699 | rtems_device_driver console_close( |
---|
700 | rtems_device_major_number major, |
---|
701 | rtems_device_minor_number minor, |
---|
702 | void * arg) |
---|
703 | { |
---|
704 | return(rtems_termios_close (arg)); |
---|
705 | } |
---|
706 | |
---|
707 | /****************** |
---|
708 | ********************************************************* |
---|
709 | Function : console_read |
---|
710 | |
---|
711 | Description : Read from the device via termios |
---|
712 | ***************************************************************************/ |
---|
713 | rtems_device_driver console_read( |
---|
714 | rtems_device_major_number major, |
---|
715 | rtems_device_minor_number minor, |
---|
716 | void * arg) |
---|
717 | { |
---|
718 | return(rtems_termios_read (arg)); |
---|
719 | } |
---|
720 | |
---|
721 | /*************************************************************************** |
---|
722 | Function : console_write |
---|
723 | |
---|
724 | Description : Write to the device via termios |
---|
725 | ***************************************************************************/ |
---|
726 | rtems_device_driver console_write( |
---|
727 | rtems_device_major_number major, |
---|
728 | rtems_device_minor_number minor, |
---|
729 | void * arg) |
---|
730 | { |
---|
731 | return(rtems_termios_write (arg)); |
---|
732 | } |
---|
733 | |
---|
734 | /*************************************************************************** |
---|
735 | Function : console_ioctl |
---|
736 | |
---|
737 | Description : Pass the IOCtl call to termios |
---|
738 | ***************************************************************************/ |
---|
739 | rtems_device_driver console_control( |
---|
740 | rtems_device_major_number major, |
---|
741 | rtems_device_minor_number minor, |
---|
742 | void * arg) |
---|
743 | { |
---|
744 | return( rtems_termios_ioctl (arg) ); |
---|
745 | } |
---|