source: trunk/hal/tsar_mips32/drivers/soclib_tty.c @ 418

Last change on this file since 418 was 418, checked in by alain, 7 years ago

Fix a bug in hal_kentry.S : the "uzone" pointer in the thread descriptor
must not be modified in case of interrupt.

File size: 9.8 KB
Line 
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 <chdev.h>
26#include <soclib_tty.h>
27#include <remote_spinlock.h>
28#include <thread.h>
29#include <printk.h>
30#include <hal_special.h>
31
32#if CONFIG_READ_DEBUG
33extern uint32_t  enter_tty_cmd;
34extern uint32_t  exit_tty_cmd;
35
36extern uint32_t  enter_tty_isr;
37extern uint32_t  exit_tty_isr;
38#endif
39
40///////////////////////////////////////
41void soclib_tty_init( chdev_t * chdev )
42{
43    xptr_t reg_xp;
44
45    chdev->cmd = &soclib_tty_cmd;
46    chdev->isr = &soclib_tty_isr;
47    chdev->aux = &soclib_tty_aux;
48
49    // get TTY channel and extended pointer on TTY peripheral base address
50    xptr_t   tty_xp  = chdev->base;
51    uint32_t channel = chdev->channel;
52
53    // get SOCLIB_TTY device cluster and local pointer
54    cxy_t      tty_cxy = GET_CXY( tty_xp );
55    uint32_t * tty_ptr = (uint32_t *)GET_PTR( tty_xp );
56
57    // reset TTY_RX_IRQ_ENABLE
58    reg_xp = XPTR( tty_cxy , tty_ptr + (channel * TTY_SPAN) + TTY_RX_IRQ_ENABLE );
59    hal_remote_sw( reg_xp , 0 );
60
61    // reset TTY_TX_IRQ_ENABLE
62    reg_xp = XPTR( tty_cxy , tty_ptr + (channel * TTY_SPAN) + TTY_TX_IRQ_ENABLE );
63    hal_remote_sw( reg_xp , 0 );
64}
65
66//////////////////////////////////////////////////////////////
67void __attribute__ ((noinline)) soclib_tty_cmd( xptr_t th_xp )
68{
69
70#if CONFIG_READ_DEBUG
71enter_tty_cmd = hal_time_stamp();
72#endif
73
74txt_dmsg("\n[DBG] %s : core[%x,%d] / DEV thread enter / cycle %d\n", 
75__FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , hal_time_stamp() );
76
77    // get client thread cluster and local pointer
78    cxy_t      th_cxy = GET_CXY( th_xp );
79    thread_t * th_ptr = (thread_t *)GET_PTR( th_xp );
80
81    // get command type and extended pointer on TXT device
82    uint32_t type   =         hal_remote_lw ( XPTR( th_cxy , &th_ptr->txt_cmd.type ) );
83    xptr_t   dev_xp = (xptr_t)hal_remote_lwd( XPTR( th_cxy , &th_ptr->txt_cmd.dev_xp ) );
84
85    assert( (type == TXT_READ) || (type == TXT_WRITE) , __FUNCTION__, "illegal command type");
86
87    // get TXT device cluster and local pointer
88    cxy_t     dev_cxy = GET_CXY( dev_xp );
89    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
90
91    // get extended pointer on SOCLIB_TTY base segment
92    xptr_t tty_xp = (xptr_t)hal_remote_lwd( XPTR( dev_cxy , &dev_ptr->base ) );
93
94    // get SOCLIB_TTY base segment cluster and local pointer
95    cxy_t      tty_cxy = GET_CXY( tty_xp );
96    uint32_t * tty_ptr = (uint32_t *)GET_PTR( tty_xp );
97
98    // get TTY channel index and channel base address
99    uint32_t   channel = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->channel ) );
100    uint32_t * base    = tty_ptr + TTY_SPAN * channel;
101
102    // compute extended pointer on relevant TTY register
103    xptr_t reg_xp; 
104    if( type == TXT_READ )  reg_xp = XPTR( tty_cxy , base + TTY_RX_IRQ_ENABLE );
105    else                    reg_xp = XPTR( tty_cxy , base + TTY_TX_IRQ_ENABLE );
106
107    // enable relevant IRQ : data transfer will be done by the TTY_RX ISR)
108    hal_remote_sw( reg_xp , 1 );
109
110txt_dmsg("\n[DBG] %s : core[%x,%d] DEV thread deschedule / cycle %d\n",
111__FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , hal_time_stamp() );
112
113    // Block and deschedule server thread
114    thread_block( CURRENT_THREAD , THREAD_BLOCKED_DEV_ISR );
115    sched_yield("blocked on ISR");
116
117txt_dmsg("\n[DBG] %s : core[%x,%d] / DEV thread resume / cycle %d\n",
118__FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , hal_time_stamp() );
119
120#if CONFIG_READ_DEBUG
121exit_tty_cmd = hal_time_stamp();
122#endif
123
124}  // end soclib_tty_cmd()
125
126/////////////////////////////////////////////////////////////
127void __attribute__ ((noinline)) soclib_tty_aux( void * args )
128{
129    uint32_t   status;
130    bool_t     empty;
131    uint32_t   i;
132
133    xptr_t     dev_xp = ((txt_aux_t *)args)->dev_xp;
134    char     * buffer = ((txt_aux_t *)args)->buffer;
135    uint32_t   count  = ((txt_aux_t *)args)->count;
136   
137    // get TXT0 chdev cluster and local pointer
138    cxy_t     dev_cxy = GET_CXY( dev_xp );
139    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
140
141    // get extended pointer on TTY channel base address
142    xptr_t tty_xp = (xptr_t)hal_remote_lwd( XPTR( dev_cxy , &dev_ptr->base ) );
143
144    // get TTY channel segment cluster and local pointer
145    cxy_t      tty_cxy = GET_CXY( tty_xp );
146    uint32_t * tty_ptr = (uint32_t *)GET_PTR( tty_xp );
147
148    // get extended pointers on TTY_WRITE & TTY_STATUS registers
149    xptr_t write_xp  = XPTR( tty_cxy , tty_ptr + TTY_WRITE );
150    xptr_t status_xp = XPTR( tty_cxy , tty_ptr + TTY_STATUS );
151
152    // loop on characters (busy waiting strategy)
153    for( i = 0 ; i < count ; i++ )
154    {
155        do
156        {
157            // get TTY_STATUS
158            status = hal_remote_lw( status_xp );
159            empty  = ( (status & TTY_STATUS_TX_FULL) == 0 );
160
161            // transfer one byte if TX buffer empty
162            if ( empty )  hal_remote_sb( write_xp , buffer[i] );
163        }
164        while ( empty == false );
165    }
166}  // end soclib_tty_aux()
167
168
169/////////////////////////////////////////////////////////////////
170void __attribute__ ((noinline)) soclib_tty_isr( chdev_t * chdev )
171{
172    uint32_t   type;         // command type
173    uint32_t   count;        // number of bytes in buffer
174    xptr_t     buf_xp;       // extended pointer on buffer
175    xptr_t     status_xp;    // extended pointer on TTY_STATUS register
176    xptr_t     write_xp;     // extended pointer on TTY_WRITE register
177    xptr_t     read_xp;      // extended pointer on TTY_READ register
178    uint32_t   status;       // TTY terminal status
179    char       byte;         // read byte
180    uint32_t   i;
181
182#if CONFIG_READ_DEBUG
183enter_tty_isr = hal_time_stamp();
184#endif
185
186    // get extended pointer on client thread
187    xptr_t root      = XPTR( local_cxy , &chdev->wait_root );
188    xptr_t client_xp = XLIST_FIRST_ELEMENT( root , thread_t , wait_list );
189
190    // get client thread cluster and local pointer
191    cxy_t      client_cxy = GET_CXY( client_xp );
192    thread_t * client_ptr = (thread_t *)GET_PTR( client_xp );
193
194    // get command arguments
195    type    = hal_remote_lw ( XPTR( client_cxy , &client_ptr->txt_cmd.type   ) );
196    count   = hal_remote_lw ( XPTR( client_cxy , &client_ptr->txt_cmd.count  ) );
197    buf_xp  = hal_remote_lwd( XPTR( client_cxy , &client_ptr->txt_cmd.buf_xp ) );
198
199txt_dmsg("\n[DBG] %s : core[%x,%d] enter / cycle %d\n", 
200__FUNCTION__ , local_cxy, CURRENT_THREAD->core->lid , hal_time_stamp() );
201
202    // get SOCLIB_TTY peripheral cluster and local pointer
203    cxy_t      tty_cxy = GET_CXY( chdev->base );
204    uint32_t * tty_ptr = (uint32_t *)GET_PTR( chdev->base );
205
206    // get channel base address
207    uint32_t * base = tty_ptr + TTY_SPAN * chdev->channel;
208
209    // get extended pointer on TTY registers
210    status_xp = XPTR( tty_cxy , base + TTY_STATUS );
211    write_xp  = XPTR( tty_cxy , base + TTY_WRITE );
212    read_xp   = XPTR( tty_cxy , base + TTY_READ );
213
214    if( type == TXT_READ )              // read one single character
215    {
216        // get TTY_STATUS
217        status = hal_remote_lw( status_xp );
218
219        if( status & TTY_STATUS_RX_FULL )   // TTY_RX full => move one byte
220        {
221            // get a byte from TTY_READ, and acknowledge RX_IRQ
222            byte = (char)hal_remote_lb( read_xp );
223
224            // write it to command buffer
225            hal_remote_sb( buf_xp , byte );
226        }
227        else                               // buffer empty => exit ISR for retry
228        {
229            return;
230        }
231
232        // disable RX_IRQ
233        xptr_t reg_xp = XPTR( tty_cxy , base + TTY_RX_IRQ_ENABLE );
234        hal_remote_sw( reg_xp , 0 );
235    }
236    else if( type == TXT_WRITE )         // write all characters in string
237    {
238        // loop on characters
239        for( i = 0 ; i < count ; i++ )
240        {
241            // get TTY_STATUS
242            status = hal_remote_lw( status_xp );
243
244            if( (status & TTY_STATUS_TX_FULL) == 0 ) // TTY_TX empty => move one byte
245            {
246                // get one byte from command buffer
247                byte = (char)hal_remote_lb( buf_xp + i );
248
249                // write byte to TTY_WRITE, and acknowledge TX_IRQ
250                hal_remote_sb( write_xp , byte );
251            }
252            else         // TTY_TX full => update command arguments and exit ISR for retry
253            {
254                hal_remote_sw ( XPTR( client_cxy , &client_ptr->txt_cmd.count ), count-i );
255                hal_remote_swd( XPTR( client_cxy , &client_ptr->txt_cmd.buf_xp ), buf_xp+i );
256                return;
257            }
258        }
259
260        // disable TX_IRQ
261        xptr_t reg_xp = XPTR( tty_cxy , base + TTY_TX_IRQ_ENABLE );
262        hal_remote_sw( reg_xp , 0 );
263    }
264
265    // The I/O operation completed when we reach this point
266
267    // set I/O operation status in command
268    hal_remote_sw( XPTR( client_cxy , &client_ptr->txt_cmd.error ) , 0 );
269
270    // unblock server thread
271    thread_unblock( XPTR( local_cxy , chdev->server ) , THREAD_BLOCKED_DEV_ISR );
272
273    // unblock client thread
274    // thread_unblock( client_xp , THREAD_BLOCKED_IO );
275
276    hal_fence();
277
278txt_dmsg("\n[DBG] %s : core[%x,%d] exit / cycle %d\n", 
279__FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , hal_time_stamp() );
280
281#if CONFIG_READ_DEBUG
282exit_tty_isr = hal_time_stamp();
283#endif
284
285}  // end soclib_tty_isr()
286
Note: See TracBrowser for help on using the repository browser.