1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup QorIQUartBridge |
---|
5 | * |
---|
6 | * @brief UART bridge slave implementation. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2011 embedded brains GmbH. All rights reserved. |
---|
11 | * |
---|
12 | * embedded brains GmbH |
---|
13 | * Obere Lagerstr. 30 |
---|
14 | * 82178 Puchheim |
---|
15 | * Germany |
---|
16 | * <rtems@embedded-brains.de> |
---|
17 | * |
---|
18 | * The license and distribution terms for this file may be |
---|
19 | * found in the file LICENSE in this distribution or at |
---|
20 | * http://www.rtems.org/license/LICENSE. |
---|
21 | */ |
---|
22 | |
---|
23 | #include <assert.h> |
---|
24 | |
---|
25 | #include <libchip/sersupp.h> |
---|
26 | |
---|
27 | #include <bspopts.h> |
---|
28 | #include <bsp/uart-bridge.h> |
---|
29 | |
---|
30 | #define TRANSMIT_EVENT RTEMS_EVENT_13 |
---|
31 | |
---|
32 | static rtems_mode disable_preemption(void) |
---|
33 | { |
---|
34 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
35 | rtems_mode prev_mode = 0; |
---|
36 | |
---|
37 | sc = rtems_task_mode (RTEMS_NO_PREEMPT, RTEMS_PREEMPT_MASK, &prev_mode); |
---|
38 | assert(sc == RTEMS_SUCCESSFUL); |
---|
39 | |
---|
40 | return prev_mode; |
---|
41 | } |
---|
42 | |
---|
43 | static void restore_preemption(rtems_mode prev_mode) |
---|
44 | { |
---|
45 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
46 | |
---|
47 | sc = rtems_task_mode (prev_mode, RTEMS_PREEMPT_MASK, &prev_mode); |
---|
48 | assert(sc == RTEMS_SUCCESSFUL); |
---|
49 | } |
---|
50 | |
---|
51 | static void uart_bridge_slave_service(intercom_packet *packet, void *arg) |
---|
52 | { |
---|
53 | uart_bridge_slave_context *ctx = arg; |
---|
54 | struct rtems_termios_tty *tty = ctx->tty; |
---|
55 | |
---|
56 | /* Workaround for https://www.rtems.org/bugzilla/show_bug.cgi?id=1736 */ |
---|
57 | rtems_mode prev_mode = disable_preemption(); |
---|
58 | |
---|
59 | rtems_termios_enqueue_raw_characters(tty, packet->data, (int) packet->size); |
---|
60 | qoriq_intercom_free_packet(packet); |
---|
61 | |
---|
62 | restore_preemption(prev_mode); |
---|
63 | } |
---|
64 | |
---|
65 | static void transmit_task(rtems_task_argument arg) |
---|
66 | { |
---|
67 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
68 | uart_bridge_slave_context *ctx = (uart_bridge_slave_context *) arg; |
---|
69 | rtems_chain_control *fifo = &ctx->transmit_fifo; |
---|
70 | struct rtems_termios_tty *tty = ctx->tty; |
---|
71 | |
---|
72 | while (true) { |
---|
73 | intercom_packet *packet = NULL; |
---|
74 | sc = rtems_chain_get_with_wait( |
---|
75 | fifo, |
---|
76 | TRANSMIT_EVENT, |
---|
77 | RTEMS_NO_TIMEOUT, |
---|
78 | (rtems_chain_node **) &packet |
---|
79 | ); |
---|
80 | assert(sc == RTEMS_SUCCESSFUL); |
---|
81 | |
---|
82 | /* Workaround for https://www.rtems.org/bugzilla/show_bug.cgi?id=1736 */ |
---|
83 | rtems_mode prev_mode = disable_preemption(); |
---|
84 | |
---|
85 | size_t size = packet->size; |
---|
86 | qoriq_intercom_send_packet(QORIQ_UART_BRIDGE_MASTER_CORE, packet); |
---|
87 | rtems_termios_dequeue_characters(tty, (int) size); |
---|
88 | |
---|
89 | restore_preemption(prev_mode); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | static void create_transmit_task( |
---|
94 | uart_bridge_slave_context *ctx |
---|
95 | ) |
---|
96 | { |
---|
97 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
98 | rtems_id task = RTEMS_ID_NONE; |
---|
99 | char index = (char) ('0' + ctx->type - INTERCOM_TYPE_UART_0); |
---|
100 | |
---|
101 | sc = rtems_task_create( |
---|
102 | rtems_build_name('U', 'B', 'T', index), |
---|
103 | QORIQ_UART_BRIDGE_TASK_PRIORITY, |
---|
104 | 0, |
---|
105 | RTEMS_DEFAULT_MODES, |
---|
106 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
107 | &task |
---|
108 | ); |
---|
109 | assert(sc == RTEMS_SUCCESSFUL); |
---|
110 | |
---|
111 | sc = rtems_task_start( |
---|
112 | task, |
---|
113 | transmit_task, |
---|
114 | (rtems_task_argument) ctx |
---|
115 | ); |
---|
116 | assert(sc == RTEMS_SUCCESSFUL); |
---|
117 | |
---|
118 | ctx->transmit_task = task; |
---|
119 | } |
---|
120 | |
---|
121 | static bool first_open( |
---|
122 | struct rtems_termios_tty *tty, |
---|
123 | rtems_termios_device_context *base, |
---|
124 | struct termios *term, |
---|
125 | rtems_libio_open_close_args_t *args |
---|
126 | ) |
---|
127 | { |
---|
128 | uart_bridge_slave_context *ctx = (uart_bridge_slave_context *) base; |
---|
129 | intercom_type type = ctx->type; |
---|
130 | |
---|
131 | ctx->tty = tty; |
---|
132 | rtems_termios_set_initial_baud(tty, 115200); |
---|
133 | create_transmit_task(ctx); |
---|
134 | qoriq_intercom_service_install(type, uart_bridge_slave_service, ctx); |
---|
135 | |
---|
136 | return true; |
---|
137 | } |
---|
138 | |
---|
139 | static void last_close( |
---|
140 | struct rtems_termios_tty *tty, |
---|
141 | rtems_termios_device_context *base, |
---|
142 | rtems_libio_open_close_args_t *args |
---|
143 | ) |
---|
144 | { |
---|
145 | uart_bridge_slave_context *ctx = (uart_bridge_slave_context *) base; |
---|
146 | |
---|
147 | qoriq_intercom_service_remove(ctx->type); |
---|
148 | } |
---|
149 | |
---|
150 | static void write_with_interrupts( |
---|
151 | rtems_termios_device_context *base, |
---|
152 | const char *buf, |
---|
153 | size_t len |
---|
154 | ) |
---|
155 | { |
---|
156 | if (len > 0) { |
---|
157 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
158 | uart_bridge_slave_context *ctx = (uart_bridge_slave_context *) base; |
---|
159 | intercom_packet *packet = qoriq_intercom_allocate_packet( |
---|
160 | ctx->type, |
---|
161 | INTERCOM_SIZE_64 |
---|
162 | ); |
---|
163 | |
---|
164 | packet->size = len; |
---|
165 | memcpy(packet->data, buf, len); |
---|
166 | |
---|
167 | /* |
---|
168 | * Due to the lovely Termios implementation we have to hand this over to |
---|
169 | * another context. |
---|
170 | */ |
---|
171 | sc = rtems_chain_append_with_notification( |
---|
172 | &ctx->transmit_fifo, |
---|
173 | &packet->glue.node, |
---|
174 | ctx->transmit_task, |
---|
175 | TRANSMIT_EVENT |
---|
176 | ); |
---|
177 | assert(sc == RTEMS_SUCCESSFUL); |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | static bool set_attributes( |
---|
182 | rtems_termios_device_context *base, |
---|
183 | const struct termios *term |
---|
184 | ) |
---|
185 | { |
---|
186 | return false; |
---|
187 | } |
---|
188 | |
---|
189 | const rtems_termios_device_handler qoriq_uart_bridge_slave = { |
---|
190 | .first_open = first_open, |
---|
191 | .last_close = last_close, |
---|
192 | .write = write_with_interrupts, |
---|
193 | .set_attributes = set_attributes, |
---|
194 | .mode = TERMIOS_IRQ_DRIVEN |
---|
195 | }; |
---|