1 | /* |
---|
2 | * soclib_tty.c - soclib tty driver implementation. |
---|
3 | * |
---|
4 | * Author Alain Greiner (2016) |
---|
5 | * |
---|
6 | * Copyright (c) UPMC Sorbonne Universites |
---|
7 | * |
---|
8 | * This file is part of ALMOS-MKH.. |
---|
9 | * |
---|
10 | * ALMOS-MKH. is free software; you can redistribute it and/or modify it |
---|
11 | * under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation; version 2.0 of the License. |
---|
13 | * |
---|
14 | * ALMOS-MKH. is distributed in the hope that it will be useful, but |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | * General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with ALMOS-MKH.; if not, write to the Free Software Foundation, |
---|
21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #include <dev_txt.h> |
---|
25 | #include <device.h> |
---|
26 | #include <soclib_tty.h> |
---|
27 | #include <remote_spinlock.h> |
---|
28 | #include <thread.h> |
---|
29 | #include <hal_special.h> |
---|
30 | |
---|
31 | ///////////////////////////////////// |
---|
32 | void soclib_tty_init( xptr_t dev_xp ) |
---|
33 | { |
---|
34 | // get TXT device cluster and local pointer |
---|
35 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
---|
36 | device_t * dev_ptr = (device_t *)GET_PTR( dev_xp ); |
---|
37 | |
---|
38 | // get extended pointer on TTY-SOCLIB peripheral base address |
---|
39 | xptr_t tty_xp = (xptr_t)hal_remote_lwd( XPTR( dev_cxy , &dev_ptr->base ) ); |
---|
40 | |
---|
41 | // get SOCLIB_TTY device cluster and local pointer |
---|
42 | cxy_t tty_cxy = GET_CXY( tty_xp ); |
---|
43 | uint32_t * tty_ptr = (uint32_t *)GET_PTR( tty_xp ); |
---|
44 | |
---|
45 | // mask both TTY_RX_IRQ and TTY_TX_IRQ |
---|
46 | hal_remote_sw( XPTR( tty_cxy , tty_ptr + TTY_CONFIG_REG ) , 0 ); |
---|
47 | |
---|
48 | } // end soclib_tty_init() |
---|
49 | |
---|
50 | ////////////////////////////////////////////////////////////////// |
---|
51 | void __attribute__ ((noinline)) soclib_tty_command( xptr_t th_xp ) |
---|
52 | { |
---|
53 | // get client thread cluster and local pointer |
---|
54 | cxy_t th_cxy = GET_CXY( th_xp ); |
---|
55 | thread_t * th_ptr = (thread_t *)GET_PTR( th_xp ); |
---|
56 | |
---|
57 | // get command type and extended pointer on TXT device |
---|
58 | uint32_t type = hal_remote_lw ( XPTR( th_cxy , &th_ptr->dev.txt.type ) ); |
---|
59 | xptr_t dev_xp = (xptr_t)hal_remote_lwd( XPTR( th_cxy , &th_ptr->dev.txt.dev_xp ) ); |
---|
60 | |
---|
61 | // get TXT device cluster and local pointer |
---|
62 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
---|
63 | device_t * dev_ptr = (device_t *)GET_PTR( dev_xp ); |
---|
64 | |
---|
65 | // get extended pointer on SOCLIB_TTY base segment |
---|
66 | xptr_t tty_xp = (xptr_t)hal_remote_lwd( XPTR( dev_cxy , &dev_ptr->base ) ); |
---|
67 | |
---|
68 | // get SOCLIB_TTY base segment cluster and local pointer |
---|
69 | cxy_t tty_cxy = GET_CXY( tty_xp ); |
---|
70 | uint32_t * tty_ptr = (uint32_t *)GET_PTR( tty_xp ); |
---|
71 | |
---|
72 | if( type == TXT_READ ) // descheduling strategy for calling thead |
---|
73 | { |
---|
74 | // unmask RX_IRQ (data transfer will be done by the TTY_RX ISR) |
---|
75 | xptr_t config_xp = XPTR( tty_cxy , tty_ptr + TTY_CONFIG_REG ); |
---|
76 | uint32_t old = hal_remote_lw( config_xp ); |
---|
77 | uint32_t new = old | TTY_CONFIG_RX_ENABLE; |
---|
78 | hal_remote_atomic_cas( config_xp , old , new ); |
---|
79 | |
---|
80 | // Block and deschedule server thread |
---|
81 | thread_block( CURRENT_THREAD , THREAD_BLOCKED_DEV_ISR ); |
---|
82 | sched_yield(); |
---|
83 | } |
---|
84 | else if( type == TXT_WRITE ) // descheduling strategy for calling thread |
---|
85 | { |
---|
86 | // unmask TX_IRQ (data transfer will be done by the TTY_TX ISR) |
---|
87 | xptr_t config_xp = XPTR( tty_cxy , tty_ptr + TTY_CONFIG_REG ); |
---|
88 | uint32_t old = hal_remote_lw( config_xp ); |
---|
89 | uint32_t new = old | TTY_CONFIG_TX_ENABLE; |
---|
90 | hal_remote_atomic_cas( config_xp , old , new ); |
---|
91 | |
---|
92 | // Block and deschedule server thread |
---|
93 | thread_block( CURRENT_THREAD , THREAD_BLOCKED_DEV_ISR ); |
---|
94 | sched_yield(); |
---|
95 | } |
---|
96 | else if( type == TXT_SYNC_WRITE ) // busy waiting strategy for calling thread |
---|
97 | { |
---|
98 | uint32_t status; |
---|
99 | bool_t empty; |
---|
100 | uint32_t i; |
---|
101 | |
---|
102 | // get source buffer extended pointer & bytes count |
---|
103 | uint32_t count = hal_remote_lw ( XPTR( th_cxy , &th_ptr->dev.txt.count ) ); |
---|
104 | xptr_t buf_xp = hal_remote_lwd( XPTR( th_cxy , &th_ptr->dev.txt.buf_xp ) ); |
---|
105 | |
---|
106 | // loop on characters |
---|
107 | for( i = 0 ; i < count ; i++ ) |
---|
108 | { |
---|
109 | do |
---|
110 | { |
---|
111 | // get TTY_STATUS_REG |
---|
112 | status = hal_remote_lw( XPTR( tty_cxy , tty_ptr + TTY_STATUS_REG ) ); |
---|
113 | empty = ( (status & TTY_STATUS_TX_FULL) == 0 ); |
---|
114 | |
---|
115 | if ( empty ) // TTY_TX empty => transfer one byte |
---|
116 | { |
---|
117 | // get one byte from command buffer in client cluster |
---|
118 | char byte = (char)hal_remote_lb( buf_xp + i ); |
---|
119 | |
---|
120 | // write byte to TTY_WRITE_REG in TTY cluster |
---|
121 | hal_remote_sb( XPTR( tty_cxy , tty_ptr + TTY_WRITE_REG ) , byte ); |
---|
122 | } |
---|
123 | } |
---|
124 | while ( empty == false ); |
---|
125 | } |
---|
126 | } |
---|
127 | } // end soclib_tty_command() |
---|
128 | |
---|
129 | |
---|
130 | //////////////////////////////////////////////////////////////// |
---|
131 | void __attribute__ ((noinline)) soclib_tty_isr( device_t * dev ) |
---|
132 | { |
---|
133 | uint32_t type; // command type |
---|
134 | uint32_t count; // number of bytes in buffer |
---|
135 | xptr_t buf_xp; // Rextended pointer on buffer |
---|
136 | uint32_t status; // TTY terminal status |
---|
137 | char byte; // read byte |
---|
138 | uint32_t i; |
---|
139 | |
---|
140 | |
---|
141 | // get extended pointer on client thread |
---|
142 | xptr_t root = XPTR( local_cxy , &dev->wait_root ); |
---|
143 | xptr_t client_xp = XLIST_FIRST_ELEMENT( root , thread_t , wait_list ); |
---|
144 | |
---|
145 | // get client thread cluster and local pointer |
---|
146 | cxy_t client_cxy = GET_CXY( client_xp ); |
---|
147 | thread_t * client_ptr = (thread_t *)GET_PTR( client_xp ); |
---|
148 | |
---|
149 | // get command arguments |
---|
150 | type = hal_remote_lw ( XPTR( client_cxy , &client_ptr->dev.txt.type ) ); |
---|
151 | count = hal_remote_lw ( XPTR( client_cxy , &client_ptr->dev.txt.count ) ); |
---|
152 | buf_xp = hal_remote_lwd( XPTR( client_cxy , &client_ptr->dev.txt.buf_xp ) ); |
---|
153 | |
---|
154 | // get SOCLIB_TTY peripheral cluster and local pointer |
---|
155 | cxy_t tty_cxy = GET_CXY( dev->base ); |
---|
156 | uint32_t * tty_ptr = (uint32_t *)GET_PTR( dev->base ); |
---|
157 | |
---|
158 | if( type == TXT_READ ) // read one single character |
---|
159 | { |
---|
160 | // get TTY_STATUS_REG |
---|
161 | status = hal_remote_lw( XPTR( tty_cxy , tty_ptr + TTY_STATUS_REG ) ); |
---|
162 | |
---|
163 | if( status & TTY_STATUS_RX_FULL ) // TTY_RX full => transfer one byte |
---|
164 | { |
---|
165 | // get a byte from TTY_READ_REG, and acknowledge RX_IRQ |
---|
166 | byte = (char)hal_remote_lb( XPTR( tty_cxy , tty_ptr + TTY_READ_REG ) ); |
---|
167 | |
---|
168 | // write it to command buffer |
---|
169 | hal_remote_sb( buf_xp , byte ); |
---|
170 | |
---|
171 | // update TTY_WRITE_REG if echo mode |
---|
172 | if( CONFIG_TXT_ECHO_MODE ) |
---|
173 | { |
---|
174 | if( (byte == '\b') || (byte == 0x7F) ) |
---|
175 | { |
---|
176 | hal_remote_sb( XPTR( tty_cxy , tty_ptr + TTY_WRITE_REG ) , '\b' ); |
---|
177 | hal_remote_sb( XPTR( tty_cxy , tty_ptr + TTY_WRITE_REG ) , ' ' ); |
---|
178 | hal_remote_sb( XPTR( tty_cxy , tty_ptr + TTY_WRITE_REG ) , '\b' ); |
---|
179 | } |
---|
180 | else |
---|
181 | { |
---|
182 | hal_remote_sw( XPTR( tty_cxy , tty_ptr + TTY_WRITE_REG ) , byte ); |
---|
183 | } |
---|
184 | } |
---|
185 | } |
---|
186 | else // buffer empty => exit ISR for retry |
---|
187 | { |
---|
188 | return; |
---|
189 | } |
---|
190 | } |
---|
191 | else if( type == TXT_WRITE ) // write a string |
---|
192 | { |
---|
193 | // loop on characters |
---|
194 | for( i = 0 ; i < count ; i++ ) |
---|
195 | { |
---|
196 | // get TTY_STATUS_REG |
---|
197 | status = hal_remote_lw( XPTR( tty_cxy , tty_ptr + TTY_STATUS_REG ) ); |
---|
198 | |
---|
199 | if( (status & TTY_STATUS_TX_FULL) == 0 ) // TTY_TX empty => transfer one byte |
---|
200 | { |
---|
201 | // get one byte from command buffer |
---|
202 | byte = (char)hal_remote_lb( buf_xp + i ); |
---|
203 | |
---|
204 | // write byte to TTY_WRITE_REG, and acknowledge TX_IRQ |
---|
205 | hal_remote_sb( XPTR( tty_cxy , tty_ptr + TTY_STATUS_REG ) , byte ); |
---|
206 | } |
---|
207 | else // TTY_TX full => update command arguments and exit ISR for retry |
---|
208 | { |
---|
209 | // update arguments in command for retry |
---|
210 | hal_remote_sw ( XPTR( client_cxy , &client_ptr->dev.txt.count ) , count - i ); |
---|
211 | hal_remote_swd( XPTR( client_cxy , &client_ptr->dev.txt.buf_xp ) , buf_xp + i ); |
---|
212 | return; |
---|
213 | } |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | // The I/O operation completed when we reach this point |
---|
218 | |
---|
219 | // mask both TTY_RX_IRQ and TTY_TX_IRQ |
---|
220 | hal_remote_sw( XPTR( tty_cxy , tty_ptr + TTY_CONFIG_REG ) , 0 ); |
---|
221 | |
---|
222 | // set I/O operation status in command |
---|
223 | hal_remote_sw( XPTR( client_cxy , &client_ptr->dev.txt.error ) , 0 ); |
---|
224 | |
---|
225 | // unblock server thread |
---|
226 | thread_unblock( XPTR( local_cxy , &dev->server ) , THREAD_BLOCKED_DEV_ISR ); |
---|
227 | |
---|
228 | // unblock client thread |
---|
229 | thread_unblock( client_xp , THREAD_BLOCKED_IO ); |
---|
230 | |
---|
231 | } // end soclib_tty_isr() |
---|
232 | |
---|
233 | |
---|