1 | /* |
---|
2 | * Generic UART Serial driver for SH-4 processors |
---|
3 | * |
---|
4 | * Copyright (C) 2000 OKTET Ltd., St.-Petersburg, Russian Fed. |
---|
5 | * Author: Alexandra Kossovsky <sasha@oktet.ru> |
---|
6 | * |
---|
7 | * COPYRIGHT (c) 1989-2000. |
---|
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 | * http://www.rtems.org/license/LICENSE. |
---|
13 | * |
---|
14 | */ |
---|
15 | |
---|
16 | #include <rtems.h> |
---|
17 | #include <termios.h> |
---|
18 | #include <rtems/libio.h> |
---|
19 | #include <bsp.h> |
---|
20 | #include "sh/sh4uart.h" |
---|
21 | |
---|
22 | #ifndef SH4_UART_INTERRUPT_LEVEL |
---|
23 | #define SH4_UART_INTERRUPT_LEVEL 4 |
---|
24 | #endif |
---|
25 | |
---|
26 | /* Forward function declarations */ |
---|
27 | static rtems_isr |
---|
28 | sh4uart1_interrupt_transmit(rtems_vector_number vec); |
---|
29 | static rtems_isr |
---|
30 | sh4uart1_interrupt_receive(rtems_vector_number vec); |
---|
31 | static rtems_isr |
---|
32 | sh4uart2_interrupt_transmit(rtems_vector_number vec); |
---|
33 | static rtems_isr |
---|
34 | sh4uart2_interrupt_receive(rtems_vector_number vec); |
---|
35 | |
---|
36 | /* |
---|
37 | * sh4uart_init -- |
---|
38 | * This function verifies the input parameters and perform initialization |
---|
39 | * of the SH-4 on-chip UART descriptor structure. |
---|
40 | * |
---|
41 | * PARAMETERS: |
---|
42 | * uart - pointer to the UART channel descriptor structure |
---|
43 | * tty - pointer to termios structure |
---|
44 | * chn - channel number (SH4_SCI/SH4_SCIF -- 1/2) |
---|
45 | * int_driven - interrupt-driven (1) or polled (0) I/O mode |
---|
46 | * |
---|
47 | * RETURNS: |
---|
48 | * RTEMS_SUCCESSFUL if all parameters are valid, or error code |
---|
49 | */ |
---|
50 | rtems_status_code |
---|
51 | sh4uart_init(sh4uart *uart, void *tty, int chn, int int_driven) |
---|
52 | { |
---|
53 | if (uart == NULL) |
---|
54 | return RTEMS_INVALID_ADDRESS; |
---|
55 | |
---|
56 | if ((chn != SH4_SCI) && (chn != SH4_SCIF)) |
---|
57 | return RTEMS_INVALID_NUMBER; |
---|
58 | |
---|
59 | uart->chn = chn; |
---|
60 | uart->tty = tty; |
---|
61 | uart->int_driven = int_driven; |
---|
62 | |
---|
63 | #if 0 |
---|
64 | sh4uart_poll_write(uart, "init", 4); |
---|
65 | #endif |
---|
66 | return RTEMS_SUCCESSFUL; |
---|
67 | } |
---|
68 | |
---|
69 | /* |
---|
70 | * sh4uart_get_Pph -- |
---|
71 | * Get current peripheral module clock. |
---|
72 | * |
---|
73 | * PARAMETERS: none; |
---|
74 | * Cpu clock is get from CPU_CLOCK_RATE_HZ marco |
---|
75 | * (defined in bspopts.h, included from bsp.h) |
---|
76 | * |
---|
77 | * RETURNS: |
---|
78 | * peripheral module clock in Hz. |
---|
79 | */ |
---|
80 | static uint32_t |
---|
81 | sh4uart_get_Pph(void) |
---|
82 | { |
---|
83 | uint16_t frqcr = *(volatile uint16_t*)SH7750_FRQCR; |
---|
84 | uint32_t Pph = CPU_CLOCK_RATE_HZ; |
---|
85 | |
---|
86 | switch (frqcr & SH7750_FRQCR_IFC) { |
---|
87 | case SH7750_FRQCR_IFCDIV1: break; |
---|
88 | case SH7750_FRQCR_IFCDIV2: Pph *= 2; break; |
---|
89 | case SH7750_FRQCR_IFCDIV3: Pph *= 3; break; |
---|
90 | case SH7750_FRQCR_IFCDIV4: Pph *= 4; break; |
---|
91 | case SH7750_FRQCR_IFCDIV6: Pph *= 6; break; |
---|
92 | case SH7750_FRQCR_IFCDIV8: Pph *= 8; break; |
---|
93 | default: /* unreachable */ |
---|
94 | break; |
---|
95 | } |
---|
96 | |
---|
97 | switch (frqcr & SH7750_FRQCR_PFC) { |
---|
98 | case SH7750_FRQCR_PFCDIV2: Pph /= 2; break; |
---|
99 | case SH7750_FRQCR_PFCDIV3: Pph /= 3; break; |
---|
100 | case SH7750_FRQCR_PFCDIV4: Pph /= 4; break; |
---|
101 | case SH7750_FRQCR_PFCDIV6: Pph /= 6; break; |
---|
102 | case SH7750_FRQCR_PFCDIV8: Pph /= 8; break; |
---|
103 | default: /* unreachable */ |
---|
104 | break; |
---|
105 | } |
---|
106 | |
---|
107 | return Pph; |
---|
108 | } |
---|
109 | |
---|
110 | /* |
---|
111 | * sh4uart_set_baudrate -- |
---|
112 | * Program the UART timer to specified baudrate |
---|
113 | * |
---|
114 | * PARAMETERS: |
---|
115 | * uart - pointer to UART descriptor structure |
---|
116 | * baud - termios baud rate (B50, B9600, etc...) |
---|
117 | * |
---|
118 | * ALGORITHM: |
---|
119 | * see SH7750 Hardware Manual. |
---|
120 | * |
---|
121 | * RETURNS: |
---|
122 | * none |
---|
123 | */ |
---|
124 | static void |
---|
125 | sh4uart_set_baudrate(sh4uart *uart, speed_t baud) |
---|
126 | { |
---|
127 | uint32_t rate; |
---|
128 | int16_t div; |
---|
129 | int n; |
---|
130 | uint32_t Pph = sh4uart_get_Pph(); |
---|
131 | |
---|
132 | switch (baud) { |
---|
133 | case B50: rate = 50; break; |
---|
134 | case B75: rate = 75; break; |
---|
135 | case B110: rate = 110; break; |
---|
136 | case B134: rate = 134; break; |
---|
137 | case B150: rate = 150; break; |
---|
138 | case B200: rate = 200; break; |
---|
139 | case B300: rate = 300; break; |
---|
140 | case B600: rate = 600; break; |
---|
141 | case B1200: rate = 1200; break; |
---|
142 | case B2400: rate = 2400; break; |
---|
143 | case B4800: rate = 4800; break; |
---|
144 | case B9600: rate = 9600; break; |
---|
145 | case B19200: rate = 19200; break; |
---|
146 | case B38400: rate = 38400; break; |
---|
147 | case B57600: rate = 57600; break; |
---|
148 | #ifdef B115200 |
---|
149 | case B115200: rate = 115200; break; |
---|
150 | #endif |
---|
151 | #ifdef B230400 |
---|
152 | case B230400: rate = 230400; break; |
---|
153 | #endif |
---|
154 | default: rate = 9600; break; |
---|
155 | } |
---|
156 | |
---|
157 | for (n = 0; n < 4; n++) { |
---|
158 | div = Pph / (32 * (1 << (2 * n)) * rate) - 1; |
---|
159 | if (div < 0x100) |
---|
160 | break; |
---|
161 | } |
---|
162 | |
---|
163 | /* Set default baudrate if specified baudrate is impossible */ |
---|
164 | if (n >= 4) |
---|
165 | sh4uart_set_baudrate(uart, B9600); |
---|
166 | |
---|
167 | if ( uart->chn == 1 ) { |
---|
168 | volatile uint8_t *smr1 = (volatile uint8_t *)SH7750_SCSMR1; |
---|
169 | *smr1 &= ~SH7750_SCSMR_CKS; |
---|
170 | *smr1 |= n << SH7750_SCSMR_CKS_S; |
---|
171 | } else { |
---|
172 | volatile uint16_t *smr2 = (volatile uint16_t *)SH7750_SCSMR2; |
---|
173 | *smr2 &= ~SH7750_SCSMR_CKS; |
---|
174 | *smr2 |= n << SH7750_SCSMR_CKS_S; |
---|
175 | } |
---|
176 | |
---|
177 | SCBRR(uart->chn) = div; |
---|
178 | /* Wait at least 1 bit interwal */ |
---|
179 | rtems_task_wake_after(RTEMS_MILLISECONDS_TO_TICKS(1000 / rate)); |
---|
180 | } |
---|
181 | |
---|
182 | /* |
---|
183 | * sh4uart_reset -- |
---|
184 | * This function perform the hardware initialization of SH-4 |
---|
185 | * on-chip UART controller using parameters |
---|
186 | * filled by the sh4uart_init function. |
---|
187 | * |
---|
188 | * PARAMETERS: |
---|
189 | * uart - pointer to UART channel descriptor structure |
---|
190 | * |
---|
191 | * RETURNS: |
---|
192 | * RTEMS_SUCCESSFUL if channel is initialized successfully, error |
---|
193 | * code in other case |
---|
194 | */ |
---|
195 | rtems_status_code |
---|
196 | sh4uart_reset(sh4uart *uart) |
---|
197 | { |
---|
198 | register int chn; |
---|
199 | register int int_driven; |
---|
200 | rtems_status_code rc; |
---|
201 | uint16_t tmp; |
---|
202 | |
---|
203 | if (uart == NULL) |
---|
204 | return RTEMS_INVALID_ADDRESS; |
---|
205 | |
---|
206 | chn = uart->chn; |
---|
207 | int_driven = uart->int_driven; |
---|
208 | |
---|
209 | if ( chn == 1 ) { |
---|
210 | volatile uint8_t *scr1 = (volatile uint8_t *)SH7750_SCSCR1; |
---|
211 | volatile uint8_t *smr1 = (volatile uint8_t *)SH7750_SCSMR1; |
---|
212 | *scr1 = 0x0; /* Is set properly at the end of this function */ |
---|
213 | *smr1 = 0x0; /* 8-bit, non-parity, 1 stop bit, pf/1 clock */ |
---|
214 | } else { |
---|
215 | volatile uint16_t *scr2 = (volatile uint16_t *)SH7750_SCSCR2; |
---|
216 | volatile uint16_t *smr2 = (volatile uint16_t *)SH7750_SCSMR2; |
---|
217 | *scr2 = 0x0; /* Is set properly at the end of this function */ |
---|
218 | *smr2 = 0x0; /* 8-bit, non-parity, 1 stop bit, pf/1 clock */ |
---|
219 | } |
---|
220 | |
---|
221 | if (chn == SH4_SCIF) |
---|
222 | SCFCR2 = SH7750_SCFCR2_TFRST | SH7750_SCFCR2_RFRST | |
---|
223 | SH7750_SCFCR2_RTRG_1 | SH7750_SCFCR2_TTRG_4; |
---|
224 | |
---|
225 | if (chn == SH4_SCI) |
---|
226 | SCSPTR1 = int_driven ? 0x0 : SH7750_SCSPTR1_EIO; |
---|
227 | else |
---|
228 | SCSPTR2 = SH7750_SCSPTR2_RTSDT; |
---|
229 | |
---|
230 | if (int_driven) { |
---|
231 | uint16_t ipr; |
---|
232 | |
---|
233 | if (chn == SH4_SCI) { |
---|
234 | ipr = IPRB; |
---|
235 | ipr &= ~SH7750_IPRB_SCI1; |
---|
236 | ipr |= SH4_UART_INTERRUPT_LEVEL << SH7750_IPRB_SCI1_S; |
---|
237 | IPRB = ipr; |
---|
238 | |
---|
239 | rc = rtems_interrupt_catch(sh4uart1_interrupt_transmit, |
---|
240 | SH7750_EVT_TO_NUM(SH7750_EVT_SCI_TXI), |
---|
241 | &uart->old_handler_transmit); |
---|
242 | if (rc != RTEMS_SUCCESSFUL) |
---|
243 | return rc; |
---|
244 | |
---|
245 | rc = rtems_interrupt_catch(sh4uart1_interrupt_receive, |
---|
246 | SH7750_EVT_TO_NUM(SH7750_EVT_SCI_RXI), |
---|
247 | &uart->old_handler_receive); |
---|
248 | if (rc != RTEMS_SUCCESSFUL) |
---|
249 | return rc; |
---|
250 | } else { |
---|
251 | ipr = IPRC; |
---|
252 | ipr &= ~SH7750_IPRC_SCIF; |
---|
253 | ipr |= SH4_UART_INTERRUPT_LEVEL << SH7750_IPRC_SCIF_S; |
---|
254 | IPRC = ipr; |
---|
255 | |
---|
256 | rc = rtems_interrupt_catch(sh4uart2_interrupt_transmit, |
---|
257 | SH7750_EVT_TO_NUM(SH7750_EVT_SCIF_TXI), |
---|
258 | &uart->old_handler_transmit); |
---|
259 | if (rc != RTEMS_SUCCESSFUL) |
---|
260 | return rc; |
---|
261 | rc = rtems_interrupt_catch(sh4uart2_interrupt_receive, |
---|
262 | SH7750_EVT_TO_NUM(SH7750_EVT_SCIF_RXI), |
---|
263 | &uart->old_handler_receive); |
---|
264 | if (rc != RTEMS_SUCCESSFUL) |
---|
265 | return rc; |
---|
266 | } |
---|
267 | uart->tx_buf = NULL; |
---|
268 | uart->tx_ptr = uart->tx_buf_len = 0; |
---|
269 | } |
---|
270 | |
---|
271 | sh4uart_set_baudrate(uart, B38400); /* debug defaults (unfortunately, |
---|
272 | it is differ to termios default */ |
---|
273 | |
---|
274 | tmp = SH7750_SCSCR_TE | SH7750_SCSCR_RE | |
---|
275 | (chn == SH4_SCI ? 0x0 : SH7750_SCSCR2_REIE) | |
---|
276 | (int_driven ? (SH7750_SCSCR_RIE | SH7750_SCSCR_TIE) : 0x0); |
---|
277 | |
---|
278 | if ( chn == 1 ) { |
---|
279 | volatile uint8_t *scr = (volatile uint8_t *)SH7750_SCSCR1; |
---|
280 | *scr = tmp; |
---|
281 | } else { |
---|
282 | volatile uint16_t *scr = (volatile uint16_t *)SH7750_SCSCR2; |
---|
283 | *scr = tmp; |
---|
284 | } |
---|
285 | |
---|
286 | return RTEMS_SUCCESSFUL; |
---|
287 | } |
---|
288 | |
---|
289 | /* |
---|
290 | * sh4uart_disable -- |
---|
291 | * This function disable the operations on SH-4 UART controller |
---|
292 | * |
---|
293 | * PARAMETERS: |
---|
294 | * uart - pointer to UART channel descriptor structure |
---|
295 | * disable_port - disable receive and transmit on the port |
---|
296 | * |
---|
297 | * RETURNS: |
---|
298 | * RTEMS_SUCCESSFUL if UART closed successfuly, or error code in |
---|
299 | * other case |
---|
300 | */ |
---|
301 | rtems_status_code |
---|
302 | sh4uart_disable(sh4uart *uart, int disable_port) |
---|
303 | { |
---|
304 | rtems_status_code rc; |
---|
305 | |
---|
306 | if (disable_port) { |
---|
307 | if ( uart->chn == 1 ) { |
---|
308 | volatile uint8_t *scr = (volatile uint8_t *)SH7750_SCSCR1; |
---|
309 | *scr &= ~(SH7750_SCSCR_TE | SH7750_SCSCR_RE); |
---|
310 | } else { |
---|
311 | volatile uint16_t *scr = (volatile uint16_t *)SH7750_SCSCR2; |
---|
312 | *scr &= ~(SH7750_SCSCR_TE | SH7750_SCSCR_RE); |
---|
313 | } |
---|
314 | } |
---|
315 | |
---|
316 | if (uart->int_driven) { |
---|
317 | rc = rtems_interrupt_catch(uart->old_handler_transmit, |
---|
318 | uart->chn == SH4_SCI ? SH7750_EVT_SCI_TXI : SH7750_EVT_SCIF_TXI, |
---|
319 | NULL); |
---|
320 | if (rc != RTEMS_SUCCESSFUL) |
---|
321 | return rc; |
---|
322 | rc = rtems_interrupt_catch(uart->old_handler_receive, |
---|
323 | uart->chn == SH4_SCI ? SH7750_EVT_SCI_RXI : SH7750_EVT_SCIF_RXI, |
---|
324 | NULL); |
---|
325 | if (rc != RTEMS_SUCCESSFUL) |
---|
326 | return rc; |
---|
327 | } |
---|
328 | |
---|
329 | return RTEMS_SUCCESSFUL; |
---|
330 | } |
---|
331 | |
---|
332 | /* |
---|
333 | * sh4uart_set_attributes -- |
---|
334 | * This function parse the termios attributes structure and perform |
---|
335 | * the appropriate settings in hardware. |
---|
336 | * |
---|
337 | * PARAMETERS: |
---|
338 | * uart - pointer to the UART descriptor structure |
---|
339 | * t - pointer to termios parameters |
---|
340 | * |
---|
341 | * RETURNS: |
---|
342 | * RTEMS_SUCCESSFUL |
---|
343 | */ |
---|
344 | rtems_status_code |
---|
345 | sh4uart_set_attributes(sh4uart *uart, const struct termios *t) |
---|
346 | { |
---|
347 | int level; |
---|
348 | speed_t baud; |
---|
349 | uint16_t smr; |
---|
350 | |
---|
351 | smr = (uint16_t)(*(uint8_t*)SH7750_SCSMR(uart->chn)); |
---|
352 | |
---|
353 | baud = cfgetospeed(t); |
---|
354 | |
---|
355 | /* Set flow control XXX*/ |
---|
356 | if ((t->c_cflag & CRTSCTS) != 0) { |
---|
357 | } |
---|
358 | |
---|
359 | /* Set character size -- only 7 or 8 bit */ |
---|
360 | switch (t->c_cflag & CSIZE) { |
---|
361 | case CS5: |
---|
362 | case CS6: |
---|
363 | case CS7: smr |= SH7750_SCSMR_CHR_7; break; |
---|
364 | case CS8: smr &= ~SH7750_SCSMR_CHR_7; break; |
---|
365 | } |
---|
366 | |
---|
367 | /* Set number of stop bits */ |
---|
368 | if ((t->c_cflag & CSTOPB) != 0) |
---|
369 | smr |= SH7750_SCSMR_STOP_2; |
---|
370 | else |
---|
371 | smr &= ~SH7750_SCSMR_STOP_2; |
---|
372 | |
---|
373 | /* Set parity mode */ |
---|
374 | if ((t->c_cflag & PARENB) != 0) { |
---|
375 | smr |= SH7750_SCSMR_PE; |
---|
376 | if ((t->c_cflag & PARODD) != 0) |
---|
377 | smr |= SH7750_SCSMR_PM_ODD; |
---|
378 | else |
---|
379 | smr &= ~SH7750_SCSMR_PM_ODD; |
---|
380 | } else |
---|
381 | smr &= ~SH7750_SCSMR_PE; |
---|
382 | |
---|
383 | rtems_interrupt_disable(level); |
---|
384 | /* wait untill all data is transmitted */ |
---|
385 | /* XXX JOEL says this is broken -- interrupts are OFF so NO ticks */ |
---|
386 | rtems_task_wake_after(RTEMS_MILLISECONDS_TO_TICKS(100)); |
---|
387 | |
---|
388 | if ( uart->chn == 1 ) { |
---|
389 | volatile uint8_t *scrP = (volatile uint8_t *)SH7750_SCSCR1; |
---|
390 | volatile uint8_t *smrP = (volatile uint8_t *)SH7750_SCSMR1; |
---|
391 | |
---|
392 | *scrP &= ~(SH7750_SCSCR_TE | SH7750_SCSCR_RE); /* disable operations */ |
---|
393 | sh4uart_set_baudrate(uart, baud); |
---|
394 | *smrP = (uint8_t)smr; |
---|
395 | *scrP |= SH7750_SCSCR_TE | SH7750_SCSCR_RE; /* enable operations */ |
---|
396 | } else { |
---|
397 | volatile uint16_t *scrP = (volatile uint16_t *)SH7750_SCSCR2; |
---|
398 | volatile uint16_t *smrP = (volatile uint16_t *)SH7750_SCSMR2; |
---|
399 | |
---|
400 | *scrP &= ~(SH7750_SCSCR_TE | SH7750_SCSCR_RE); /* disable operations */ |
---|
401 | sh4uart_set_baudrate(uart, baud); |
---|
402 | *smrP = (uint8_t)smr; |
---|
403 | *scrP |= SH7750_SCSCR_TE | SH7750_SCSCR_RE; /* enable operations */ |
---|
404 | } |
---|
405 | |
---|
406 | rtems_interrupt_enable(level); |
---|
407 | |
---|
408 | return RTEMS_SUCCESSFUL; |
---|
409 | } |
---|
410 | |
---|
411 | /* |
---|
412 | * sh4uart_handle_error -- |
---|
413 | * Perfoms error (Overrun, Framing & Parity) handling |
---|
414 | * |
---|
415 | * PARAMETERS: |
---|
416 | * uart - pointer to UART descriptor structure |
---|
417 | * |
---|
418 | * RETURNS: |
---|
419 | * nothing |
---|
420 | */ |
---|
421 | static void |
---|
422 | sh4uart_handle_error(sh4uart *uart) |
---|
423 | { |
---|
424 | if (uart->chn == SH4_SCI) { |
---|
425 | volatile uint8_t *scr = (volatile uint8_t *)SH7750_SCSCR1; |
---|
426 | *scr &= ~(SH7750_SCSSR1_ORER | SH7750_SCSSR1_FER | SH7750_SCSSR1_PER); |
---|
427 | } else { |
---|
428 | volatile uint16_t *scr = (volatile uint16_t *)SH7750_SCSCR2; |
---|
429 | *scr &= ~(SH7750_SCSSR2_ER | SH7750_SCSSR2_BRK | SH7750_SCSSR2_FER); |
---|
430 | *scr &= ~(SH7750_SCLSR2_ORER); |
---|
431 | } |
---|
432 | } |
---|
433 | |
---|
434 | /* |
---|
435 | * sh4uart_poll_read -- |
---|
436 | * This function tried to read character from SH-4 UART and perform |
---|
437 | * error handling. When parity or framing error occured, return |
---|
438 | * value dependent on termios input mode flags: |
---|
439 | * - received character, if IGNPAR == 1 |
---|
440 | * - 0, if IGNPAR == 0 and PARMRK == 0 |
---|
441 | * - 0xff and 0x00 on next poll_read invocation, if IGNPAR == 0 and |
---|
442 | * PARMRK == 1 |
---|
443 | * |
---|
444 | * PARAMETERS: |
---|
445 | * uart - pointer to UART descriptor structure |
---|
446 | * |
---|
447 | * RETURNS: |
---|
448 | * code of received character or -1 if no characters received. |
---|
449 | */ |
---|
450 | int |
---|
451 | sh4uart_poll_read(sh4uart *uart) |
---|
452 | { |
---|
453 | int chn = uart->chn; |
---|
454 | int parity_error = 0; |
---|
455 | int break_occured = 0; |
---|
456 | int ch; |
---|
457 | |
---|
458 | if (uart->parerr_mark_flag == true) { |
---|
459 | uart->parerr_mark_flag = false; |
---|
460 | return 0; |
---|
461 | } |
---|
462 | |
---|
463 | if (chn == SH4_SCI) { |
---|
464 | if ((SCSSR1 & (SH7750_SCSSR1_PER | SH7750_SCSSR1_FER | |
---|
465 | SH7750_SCSSR1_ORER)) != 0) { |
---|
466 | if (SCSSR1 & (SH7750_SCSSR1_PER | SH7750_SCSSR1_FER)) |
---|
467 | parity_error = 1; |
---|
468 | sh4uart_handle_error(uart); |
---|
469 | } |
---|
470 | if ((SCSSR1 & SH7750_SCSSR1_RDRF) == 0) |
---|
471 | return -1; |
---|
472 | } else { |
---|
473 | if ((SCSSR2 & (SH7750_SCSSR2_ER | SH7750_SCSSR2_DR | |
---|
474 | SH7750_SCSSR2_BRK)) != 0 || |
---|
475 | (SCLSR2 & SH7750_SCLSR2_ORER) != 0) { |
---|
476 | if (SCSSR2 & (SH7750_SCSSR1_PER | SH7750_SCSSR1_FER)) |
---|
477 | parity_error = 1; |
---|
478 | if (SCSSR2 & SH7750_SCSSR2_BRK) |
---|
479 | break_occured = 1; |
---|
480 | sh4uart_handle_error(uart); |
---|
481 | } |
---|
482 | if ((SCSSR2 & SH7750_SCSSR2_RDF) == 0) |
---|
483 | return -1; |
---|
484 | } |
---|
485 | |
---|
486 | if (parity_error && !(uart->c_iflag & IGNPAR)) { |
---|
487 | if (uart->c_iflag & PARMRK) { |
---|
488 | uart->parerr_mark_flag = true; |
---|
489 | return 0xff; |
---|
490 | } else |
---|
491 | return 0; |
---|
492 | } |
---|
493 | |
---|
494 | if (break_occured && !(uart->c_iflag & BRKINT)) { |
---|
495 | if (uart->c_iflag & IGNBRK) |
---|
496 | return 0; |
---|
497 | else |
---|
498 | return 0; /* XXX -- SIGINT */ |
---|
499 | } |
---|
500 | |
---|
501 | ch = SCRDR(chn); |
---|
502 | |
---|
503 | if (uart->chn == SH4_SCI) { |
---|
504 | volatile uint8_t *scr = (volatile uint8_t *)SH7750_SCSCR1; |
---|
505 | *scr &= ~SH7750_SCSSR1_RDRF; |
---|
506 | } else { |
---|
507 | volatile uint16_t *scr = (volatile uint16_t *)SH7750_SCSCR2; |
---|
508 | *scr &= ~SH7750_SCSSR2_RDF; |
---|
509 | } |
---|
510 | |
---|
511 | return ch; |
---|
512 | } |
---|
513 | |
---|
514 | /* |
---|
515 | * sh4uart_poll_write -- |
---|
516 | * This function transmit buffer byte-by-byte in polling mode. |
---|
517 | * |
---|
518 | * PARAMETERS: |
---|
519 | * uart - pointer to the UART descriptor structure |
---|
520 | * buf - pointer to transmit buffer |
---|
521 | * len - transmit buffer length |
---|
522 | * |
---|
523 | * RETURNS: |
---|
524 | * 0 |
---|
525 | */ |
---|
526 | int |
---|
527 | sh4uart_poll_write(sh4uart *uart, const char *buf, int len) |
---|
528 | { |
---|
529 | volatile uint8_t *ssr1 = (volatile uint8_t *)SH7750_SCSSR1; |
---|
530 | volatile uint16_t *ssr2 = (volatile uint16_t *)SH7750_SCSSR2; |
---|
531 | |
---|
532 | while (len) { |
---|
533 | if (uart->chn == SH4_SCI) { |
---|
534 | while ((SCSSR1 & SH7750_SCSSR1_TDRE) != 0) { |
---|
535 | SCTDR1 = *buf++; |
---|
536 | len--; |
---|
537 | *ssr1 &= ~SH7750_SCSSR1_TDRE; |
---|
538 | } |
---|
539 | } else { |
---|
540 | while ((SCSSR2 & SH7750_SCSSR2_TDFE) != 0) { |
---|
541 | int i; |
---|
542 | for (i = 0; |
---|
543 | i < 16 - TRANSMIT_TRIGGER_VALUE(SCFCR2 & |
---|
544 | SH7750_SCFCR2_TTRG); |
---|
545 | i++) { |
---|
546 | SCTDR2 = *buf++; |
---|
547 | len--; |
---|
548 | } |
---|
549 | while ((SCSSR2 & SH7750_SCSSR2_TDFE) == 0 || |
---|
550 | (SCSSR2 & SH7750_SCSSR2_TEND) == 0); |
---|
551 | *ssr2 &= ~(SH7750_SCSSR1_TDRE | SH7750_SCSSR2_TEND); |
---|
552 | } |
---|
553 | } |
---|
554 | } |
---|
555 | return 0; |
---|
556 | } |
---|
557 | |
---|
558 | /********************************** |
---|
559 | * Functions to handle interrupts * |
---|
560 | **********************************/ |
---|
561 | /* sh4uart1_interrupt_receive -- |
---|
562 | * UART interrupt handler routine -- SCI |
---|
563 | * Receiving data |
---|
564 | * |
---|
565 | * PARAMETERS: |
---|
566 | * vec - interrupt vector number |
---|
567 | * |
---|
568 | * RETURNS: |
---|
569 | * none |
---|
570 | */ |
---|
571 | static rtems_isr |
---|
572 | sh4uart1_interrupt_receive(rtems_vector_number vec) |
---|
573 | { |
---|
574 | register int bp = 0; |
---|
575 | char buf[32]; |
---|
576 | volatile uint8_t *ssr1 = (volatile uint8_t *)SH7750_SCSSR1; |
---|
577 | |
---|
578 | |
---|
579 | /* Find UART descriptor from vector number */ |
---|
580 | sh4uart *uart = &sh4_uarts[0]; |
---|
581 | |
---|
582 | while (1) { |
---|
583 | if ((bp < sizeof(buf) - 1) && ((SCSSR1 & SH7750_SCSSR1_RDRF) != 0)) { |
---|
584 | /* Receive character and handle frame/parity errors */ |
---|
585 | if ((SCSSR1 & (SH7750_SCSSR1_PER | SH7750_SCSSR1_FER | |
---|
586 | SH7750_SCSSR1_ORER)) != 0) { |
---|
587 | if (SCSSR1 & (SH7750_SCSSR1_PER | SH7750_SCSSR1_FER)) { |
---|
588 | if (!(uart->c_iflag & IGNPAR)) { |
---|
589 | if (uart->c_iflag & PARMRK) { |
---|
590 | buf[bp++] = 0xff; |
---|
591 | buf[bp++] = 0x00; |
---|
592 | } else |
---|
593 | buf[bp++] = 0x00; |
---|
594 | } else |
---|
595 | buf[bp++] = SCRDR1; |
---|
596 | } |
---|
597 | sh4uart_handle_error(uart); |
---|
598 | } else |
---|
599 | buf[bp++] = SCRDR1; |
---|
600 | *ssr1 &= ~SH7750_SCSSR1_RDRF; |
---|
601 | } else { |
---|
602 | if (bp != 0) |
---|
603 | rtems_termios_enqueue_raw_characters(uart->tty, buf, bp); |
---|
604 | break; |
---|
605 | } |
---|
606 | } |
---|
607 | } |
---|
608 | |
---|
609 | /* sh4uart2_interrupt_receive -- |
---|
610 | * UART interrupt handler routine -- SCIF |
---|
611 | * Receiving data |
---|
612 | * |
---|
613 | * PARAMETERS: |
---|
614 | * vec - interrupt vector number |
---|
615 | * |
---|
616 | * RETURNS: |
---|
617 | * none |
---|
618 | */ |
---|
619 | static rtems_isr |
---|
620 | sh4uart2_interrupt_receive(rtems_vector_number vec) |
---|
621 | { |
---|
622 | register int bp = 0; |
---|
623 | char buf[32]; |
---|
624 | volatile uint16_t *ssr2 = (volatile uint16_t *)SH7750_SCSSR2; |
---|
625 | |
---|
626 | |
---|
627 | /* Find UART descriptor from vector number */ |
---|
628 | sh4uart *uart = &sh4_uarts[1]; |
---|
629 | |
---|
630 | while (1) { |
---|
631 | if ((bp < sizeof(buf) - 1) && ((SCSSR2 & SH7750_SCSSR2_RDF) != 0)) { |
---|
632 | if ((SCSSR2 & (SH7750_SCSSR2_ER | SH7750_SCSSR2_DR | |
---|
633 | SH7750_SCSSR2_BRK)) != 0 || |
---|
634 | (SH7750_SCLSR2 & SH7750_SCLSR2_ORER) != 0) { |
---|
635 | if (SCSSR2 & SH7750_SCSSR2_ER) { |
---|
636 | if (!(uart->c_iflag & IGNPAR)) { |
---|
637 | if (uart->c_iflag & PARMRK) { |
---|
638 | buf[bp++] = 0xff; |
---|
639 | buf[bp++] = 0x00; |
---|
640 | } else |
---|
641 | buf[bp++] = 0x00; |
---|
642 | } else |
---|
643 | buf[bp++] = SCRDR1; |
---|
644 | } |
---|
645 | |
---|
646 | if (SCSSR2 & SH7750_SCSSR2_BRK) { |
---|
647 | if (uart->c_iflag & IGNBRK) |
---|
648 | buf[bp++] = 0x00; |
---|
649 | else |
---|
650 | buf[bp++] = 0x00; /* XXX -- SIGINT */ |
---|
651 | } |
---|
652 | |
---|
653 | sh4uart_handle_error(uart); |
---|
654 | } else |
---|
655 | buf[bp++] = SCRDR1; |
---|
656 | *ssr2 &= ~SH7750_SCSSR2_RDF; |
---|
657 | } else { |
---|
658 | if (bp != 0) |
---|
659 | rtems_termios_enqueue_raw_characters(uart->tty, buf, bp); |
---|
660 | break; |
---|
661 | } |
---|
662 | } |
---|
663 | } |
---|
664 | |
---|
665 | |
---|
666 | /* sh4uart1_interrupt_transmit -- |
---|
667 | * UART interrupt handler routine -- SCI |
---|
668 | * It continues transmit data when old part of data is transmitted |
---|
669 | * |
---|
670 | * PARAMETERS: |
---|
671 | * vec - interrupt vector number |
---|
672 | * |
---|
673 | * RETURNS: |
---|
674 | * none |
---|
675 | */ |
---|
676 | static rtems_isr |
---|
677 | sh4uart1_interrupt_transmit(rtems_vector_number vec) |
---|
678 | { |
---|
679 | volatile uint8_t *scr1 = (volatile uint8_t *)SH7750_SCSCR1; |
---|
680 | volatile uint8_t *ssr1 = (volatile uint8_t *)SH7750_SCSSR1; |
---|
681 | |
---|
682 | /* Find UART descriptor from vector number */ |
---|
683 | sh4uart *uart = &sh4_uarts[0]; |
---|
684 | |
---|
685 | if (uart->tx_buf != NULL && uart->tx_ptr < uart->tx_buf_len) { |
---|
686 | while ((SCSSR1 & SH7750_SCSSR1_TDRE) != 0 && |
---|
687 | uart->tx_ptr < uart->tx_buf_len) { |
---|
688 | SCTDR1 = uart->tx_buf[uart->tx_ptr++]; |
---|
689 | *ssr1 &= ~SH7750_SCSSR1_TDRE; |
---|
690 | } |
---|
691 | } else { |
---|
692 | register int dequeue = uart->tx_buf_len; |
---|
693 | |
---|
694 | uart->tx_buf = NULL; |
---|
695 | uart->tx_ptr = uart->tx_buf_len = 0; |
---|
696 | |
---|
697 | /* Disable interrupts while we do not have any data to transmit */ |
---|
698 | *scr1 &= ~SH7750_SCSCR_TIE; |
---|
699 | |
---|
700 | rtems_termios_dequeue_characters(uart->tty, dequeue); |
---|
701 | } |
---|
702 | } |
---|
703 | |
---|
704 | /* sh4uart2_interrupt_transmit -- |
---|
705 | * UART interrupt handler routine -- SCI |
---|
706 | * It continues transmit data when old part of data is transmitted |
---|
707 | * |
---|
708 | * PARAMETERS: |
---|
709 | * vec - interrupt vector number |
---|
710 | * |
---|
711 | * RETURNS: |
---|
712 | * none |
---|
713 | */ |
---|
714 | static rtems_isr |
---|
715 | sh4uart2_interrupt_transmit(rtems_vector_number vec) |
---|
716 | { |
---|
717 | volatile uint8_t *ssr1 = (volatile uint8_t *)SH7750_SCSSR1; |
---|
718 | volatile uint16_t *scr2 = (volatile uint16_t *)SH7750_SCSCR2; |
---|
719 | |
---|
720 | /* Find UART descriptor from vector number */ |
---|
721 | sh4uart *uart = &sh4_uarts[1]; |
---|
722 | |
---|
723 | if (uart->tx_buf != NULL && uart->tx_ptr < uart->tx_buf_len) { |
---|
724 | while ((SCSSR2 & SH7750_SCSSR2_TDFE) != 0) { |
---|
725 | int i; |
---|
726 | for (i = 0; |
---|
727 | i < 16 - TRANSMIT_TRIGGER_VALUE(SCFCR2 & SH7750_SCFCR2_TTRG); |
---|
728 | i++) |
---|
729 | SCTDR2 = uart->tx_buf[uart->tx_ptr++]; |
---|
730 | while ((SCSSR1 & SH7750_SCSSR1_TDRE) == 0 || |
---|
731 | (SCSSR1 & SH7750_SCSSR1_TEND) == 0); |
---|
732 | *ssr1 &= ~(SH7750_SCSSR1_TDRE | SH7750_SCSSR2_TEND); |
---|
733 | } |
---|
734 | } else { |
---|
735 | register int dequeue = uart->tx_buf_len; |
---|
736 | |
---|
737 | uart->tx_buf = NULL; |
---|
738 | uart->tx_ptr = uart->tx_buf_len = 0; |
---|
739 | |
---|
740 | /* Disable interrupts while we do not have any data to transmit */ |
---|
741 | *scr2 &= ~SH7750_SCSCR_TIE; |
---|
742 | |
---|
743 | rtems_termios_dequeue_characters(uart->tty, dequeue); |
---|
744 | } |
---|
745 | } |
---|
746 | |
---|
747 | /* sh4uart_interrupt_write -- |
---|
748 | * This function initiate transmitting of the buffer in interrupt mode. |
---|
749 | * |
---|
750 | * PARAMETERS: |
---|
751 | * uart - pointer to the UART descriptor structure |
---|
752 | * buf - pointer to transmit buffer |
---|
753 | * len - transmit buffer length |
---|
754 | * |
---|
755 | * RETURNS: |
---|
756 | * 0 |
---|
757 | */ |
---|
758 | rtems_status_code |
---|
759 | sh4uart_interrupt_write(sh4uart *uart, const char *buf, int len) |
---|
760 | { |
---|
761 | if (len > 0) { |
---|
762 | volatile uint8_t *scr1 = (volatile uint8_t *)SH7750_SCSCR1; |
---|
763 | volatile uint16_t *scr2 = (volatile uint16_t *)SH7750_SCSCR2; |
---|
764 | |
---|
765 | while ((SCSSR1 & SH7750_SCSSR1_TEND) == 0); |
---|
766 | |
---|
767 | uart->tx_buf = buf; |
---|
768 | uart->tx_buf_len = len; |
---|
769 | uart->tx_ptr = 0; |
---|
770 | |
---|
771 | if (uart->chn == SH4_SCI) |
---|
772 | *scr1 |= SH7750_SCSCR_TIE; |
---|
773 | else |
---|
774 | *scr2 |= SH7750_SCSCR_TIE; |
---|
775 | } |
---|
776 | |
---|
777 | return RTEMS_SUCCESSFUL; |
---|
778 | } |
---|
779 | |
---|
780 | /* sh4uart_stop_remote_tx -- |
---|
781 | * This function stop data flow from remote device. |
---|
782 | * |
---|
783 | * PARAMETERS: |
---|
784 | * uart - pointer to the UART descriptor structure |
---|
785 | * |
---|
786 | * RETURNS: |
---|
787 | * RTEMS_SUCCESSFUL |
---|
788 | */ |
---|
789 | rtems_status_code |
---|
790 | sh4uart_stop_remote_tx(sh4uart *uart) |
---|
791 | { |
---|
792 | if ( uart->chn == 1 ) { |
---|
793 | volatile uint8_t *scr = (volatile uint8_t *)SH7750_SCSCR1; |
---|
794 | *scr &= ~(SH7750_SCSCR_RIE | SH7750_SCSCR_RE); |
---|
795 | } else { |
---|
796 | volatile uint16_t *scr = (volatile uint16_t *)SH7750_SCSCR2; |
---|
797 | *scr &= ~(SH7750_SCSCR_RIE | SH7750_SCSCR_RE); |
---|
798 | } |
---|
799 | |
---|
800 | return RTEMS_SUCCESSFUL; |
---|
801 | } |
---|
802 | |
---|
803 | /* sh4uart_start_remote_tx -- |
---|
804 | * This function resume data flow from remote device. |
---|
805 | * |
---|
806 | * PARAMETERS: |
---|
807 | * uart - pointer to the UART descriptor structure |
---|
808 | * |
---|
809 | * RETURNS: |
---|
810 | * RTEMS_SUCCESSFUL |
---|
811 | */ |
---|
812 | rtems_status_code |
---|
813 | sh4uart_start_remote_tx(sh4uart *uart) |
---|
814 | { |
---|
815 | if ( uart->chn == 1 ) { |
---|
816 | volatile uint8_t *scr = (volatile uint8_t *)SH7750_SCSCR1; |
---|
817 | *scr |= SH7750_SCSCR_RIE | SH7750_SCSCR_RE; |
---|
818 | } else { |
---|
819 | volatile uint16_t *scr = (volatile uint16_t *)SH7750_SCSCR2; |
---|
820 | *scr |= SH7750_SCSCR_RIE | SH7750_SCSCR_RE; |
---|
821 | } |
---|
822 | |
---|
823 | return RTEMS_SUCCESSFUL; |
---|
824 | } |
---|
825 | |
---|
826 | #ifdef SH4_WITH_IPL |
---|
827 | /********************************* |
---|
828 | * Functions for SH-IPL gdb stub * |
---|
829 | *********************************/ |
---|
830 | |
---|
831 | /* |
---|
832 | * ipl_finish -- |
---|
833 | * Says gdb that program finished to get out from it. |
---|
834 | */ |
---|
835 | extern void ipl_finish(void); |
---|
836 | __asm__ ( |
---|
837 | " .global _ipl_finish\n" |
---|
838 | "_ipl_finish:\n" |
---|
839 | " mov.l __ipl_finish_value, r0\n" |
---|
840 | " trapa #0x3f\n" |
---|
841 | " nop\n" |
---|
842 | " rts\n" |
---|
843 | " nop\n" |
---|
844 | " .align 4\n" |
---|
845 | "__ipl_finish_value:\n" |
---|
846 | " .long 255" |
---|
847 | ); |
---|
848 | |
---|
849 | extern int ipl_serial_input(int poll_count); |
---|
850 | __asm__ ( |
---|
851 | " .global _ipl_serial_input\n" |
---|
852 | "_ipl_serial_input:\n" |
---|
853 | " mov #1,r0\n" |
---|
854 | " trapa #0x3f\n" |
---|
855 | " nop\n" |
---|
856 | " rts\n" |
---|
857 | " nop\n"); |
---|
858 | |
---|
859 | extern void ipl_serial_output(const char *buf, int len); |
---|
860 | __asm__ ( |
---|
861 | " .global _ipl_serial_output\n" |
---|
862 | "_ipl_serial_output:\n" |
---|
863 | " mov #0,r0\n" |
---|
864 | " trapa #0x3f\n" |
---|
865 | " nop\n" |
---|
866 | " rts\n" |
---|
867 | " nop\n"); |
---|
868 | |
---|
869 | /* ipl_console_poll_read -- |
---|
870 | * poll read operation for simulator console through ipl mechanism. |
---|
871 | * |
---|
872 | * PARAMETERS: |
---|
873 | * minor - minor device number |
---|
874 | * |
---|
875 | * RETURNS: |
---|
876 | * character code red from UART, or -1 if there is no characters |
---|
877 | * available |
---|
878 | */ |
---|
879 | int |
---|
880 | ipl_console_poll_read(int minor) |
---|
881 | { |
---|
882 | unsigned char buf; |
---|
883 | buf = ipl_serial_input(0x100000); |
---|
884 | return buf; |
---|
885 | } |
---|
886 | |
---|
887 | /* ipl_console_poll_write -- |
---|
888 | * wrapper for polling mode write function |
---|
889 | * |
---|
890 | * PARAMETERS: |
---|
891 | * minor - minor device number |
---|
892 | * buf - output buffer |
---|
893 | * len - output buffer length |
---|
894 | * |
---|
895 | * RETURNS: |
---|
896 | * result code (0) |
---|
897 | */ |
---|
898 | int |
---|
899 | ipl_console_poll_write(int minor, const char *buf, int len) |
---|
900 | { |
---|
901 | int c; |
---|
902 | while (len > 0) { |
---|
903 | c = (len < 64 ? len : 64); |
---|
904 | ipl_serial_output(buf, c); |
---|
905 | len -= c; |
---|
906 | buf += c; |
---|
907 | } |
---|
908 | return 0; |
---|
909 | } |
---|
910 | #endif |
---|