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

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

Several modifs in the generic scheduler and in the hal_context to
fix the context switch mechanism.

File size: 8.7 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 <hal_special.h>
30
31///////////////////////////////////////
32void soclib_tty_init( chdev_t * chdev )
33{
34    chdev->cmd = &soclib_tty_cmd;
35    chdev->isr = &soclib_tty_isr;
36
37    // get extended pointer on TTY-SOCLIB peripheral base address
38    xptr_t tty_xp = chdev->base;
39
40    // get SOCLIB_TTY device cluster and local pointer
41    cxy_t      tty_cxy = GET_CXY( tty_xp );
42    uint32_t * tty_ptr = (uint32_t *)GET_PTR( tty_xp );
43
44    // mask both TTY_RX_IRQ and TTY_TX_IRQ
45    hal_remote_sw( XPTR( tty_cxy , tty_ptr + TTY_CONFIG_REG ) , 0 );
46}
47
48//////////////////////////////////////////////////////////////
49void __attribute__ ((noinline)) soclib_tty_cmd( xptr_t th_xp )
50{
51    // get client thread cluster and local pointer
52    cxy_t      th_cxy = GET_CXY( th_xp );
53    thread_t * th_ptr = (thread_t *)GET_PTR( th_xp );
54
55    // get command type and extended pointer on TXT device
56    uint32_t type   =         hal_remote_lw ( XPTR( th_cxy , &th_ptr->txt_cmd.type ) );
57    xptr_t   dev_xp = (xptr_t)hal_remote_lwd( XPTR( th_cxy , &th_ptr->txt_cmd.dev_xp ) );
58
59    // get TXT device cluster and local pointer
60    cxy_t     dev_cxy = GET_CXY( dev_xp );
61    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
62
63    // get extended pointer on SOCLIB_TTY base segment
64    xptr_t tty_xp = (xptr_t)hal_remote_lwd( XPTR( dev_cxy , &dev_ptr->base ) );
65
66    // get SOCLIB_TTY base segment cluster and local pointer
67    cxy_t      tty_cxy = GET_CXY( tty_xp );
68    uint32_t * tty_ptr = (uint32_t *)GET_PTR( tty_xp );
69
70    // get TTY channel index and channel base address
71    uint32_t   channel = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->channel ) );
72    uint32_t * base    = tty_ptr + TTY_SPAN * channel;
73
74    if( type == TXT_READ )              // descheduling strategy for calling thread
75    {
76        // unmask RX_IRQ (data transfer will be done by the TTY_RX ISR)
77        xptr_t config_xp = XPTR( tty_cxy , base + TTY_CONFIG_REG );
78        uint32_t old = hal_remote_lw( config_xp );
79        uint32_t new = old | TTY_CONFIG_RX_ENABLE;
80        hal_remote_atomic_cas( config_xp , old , new );
81
82        // Block and deschedule server thread
83        thread_block( CURRENT_THREAD , THREAD_BLOCKED_DEV_ISR );
84        sched_yield( NULL );
85    }
86    else if( type == TXT_WRITE )        // descheduling strategy for calling thread
87    {
88        // unmask TX_IRQ (data transfer will be done by the TTY_TX ISR)
89        xptr_t config_xp = XPTR( tty_cxy , base + TTY_CONFIG_REG );
90        uint32_t old = hal_remote_lw( config_xp );
91        uint32_t new = old | TTY_CONFIG_TX_ENABLE;
92        hal_remote_atomic_cas( config_xp , old , new );
93
94        // Block and deschedule server thread
95        thread_block( CURRENT_THREAD , THREAD_BLOCKED_DEV_ISR );
96        sched_yield( NULL );
97    }
98    else if( type == TXT_SYNC_WRITE )  // busy waiting strategy for calling thread
99    {
100        uint32_t   status;
101        bool_t     empty;
102        uint32_t   i;
103
104        // get source buffer extended pointer & bytes count
105        uint32_t count  = hal_remote_lw ( XPTR( th_cxy , &th_ptr->txt_cmd.count ) );
106        xptr_t   buf_xp = hal_remote_lwd( XPTR( th_cxy , &th_ptr->txt_cmd.buf_xp ) );
107
108        // loop on characters
109        for( i = 0 ; i < count ; i++ )
110        {
111            do
112            {
113                // get TTY_STATUS_REG
114                status = hal_remote_lw( XPTR( tty_cxy , base + TTY_STATUS_REG ) );
115                empty  = ( (status & TTY_STATUS_TX_FULL) == 0 );
116
117                if ( empty )  // TTY_TX empty => transfer one byte
118                {
119                    // get one byte from command buffer in client cluster
120                    char byte = (char)hal_remote_lb( buf_xp + i );
121
122                    // write byte to TTY_WRITE_REG in TTY cluster
123                    hal_remote_sb( XPTR( tty_cxy , base + TTY_WRITE_REG ) , byte );
124                }
125            }
126            while ( empty == false );
127        }
128    }
129}  // end soclib_tty_cmd()
130
131
132/////////////////////////////////////////////////////////////////
133void __attribute__ ((noinline)) soclib_tty_isr( chdev_t * chdev )
134{
135    uint32_t   type;         // command type
136    uint32_t   count;        // number of bytes in buffer
137    xptr_t     buf_xp;       // Rextended pointer on buffer
138    uint32_t   status;       // TTY terminal status
139    char       byte;         // read byte
140    uint32_t   i;
141
142    // get extended pointer on client thread
143    xptr_t root      = XPTR( local_cxy , &chdev->wait_root );
144    xptr_t client_xp = XLIST_FIRST_ELEMENT( root , thread_t , wait_list );
145
146    // get client thread cluster and local pointer
147    cxy_t      client_cxy = GET_CXY( client_xp );
148    thread_t * client_ptr = (thread_t *)GET_PTR( client_xp );
149
150    // get command arguments
151    type    = hal_remote_lw ( XPTR( client_cxy , &client_ptr->txt_cmd.type   ) );
152    count   = hal_remote_lw ( XPTR( client_cxy , &client_ptr->txt_cmd.count  ) );
153    buf_xp  = hal_remote_lwd( XPTR( client_cxy , &client_ptr->txt_cmd.buf_xp ) );
154
155    // get SOCLIB_TTY peripheral cluster and local pointer
156    cxy_t      tty_cxy = GET_CXY( chdev->base );
157    uint32_t * tty_ptr = (uint32_t *)GET_PTR( chdev->base );
158
159    // get channel base address
160    uint32_t * base = tty_ptr + TTY_SPAN * chdev->channel;
161
162    if( type == TXT_READ )              // read one single character
163    {
164        // get TTY_STATUS_REG
165        status = hal_remote_lw( XPTR( tty_cxy , base + TTY_STATUS_REG ) );
166
167        if( status & TTY_STATUS_RX_FULL )   // TTY_RX full => transfer one byte
168        {
169            // get a byte from TTY_READ_REG, and acknowledge RX_IRQ
170            byte = (char)hal_remote_lb( XPTR( tty_cxy , base + TTY_READ_REG ) );
171
172            // write it to command buffer
173            hal_remote_sb( buf_xp , byte );
174
175            // update TTY_WRITE_REG if echo mode
176            if( CONFIG_TXT_ECHO_MODE )
177            {
178                if( (byte == '\b') || (byte == 0x7F) )
179                        {
180                                hal_remote_sb( XPTR( tty_cxy , base + TTY_WRITE_REG ) , '\b' );
181                                hal_remote_sb( XPTR( tty_cxy , base + TTY_WRITE_REG ) , ' '  );
182                                hal_remote_sb( XPTR( tty_cxy , base + TTY_WRITE_REG ) , '\b' );
183                        }
184                else
185                {
186                                hal_remote_sw( XPTR( tty_cxy , base + TTY_WRITE_REG ) , byte );
187                        }
188            }
189        }
190        else                               // buffer empty => exit ISR for retry
191        {
192            return;
193        }
194    }
195    else if( type == TXT_WRITE )         // write a string
196    {
197        // loop on characters
198        for( i = 0 ; i < count ; i++ )
199        {
200            // get TTY_STATUS_REG
201            status = hal_remote_lw( XPTR( tty_cxy , base + TTY_STATUS_REG ) );
202
203            if( (status & TTY_STATUS_TX_FULL) == 0 ) // TTY_TX empty => transfer one byte
204            {
205                // get one byte from command buffer
206                byte = (char)hal_remote_lb( buf_xp + i );
207
208                // write byte to TTY_WRITE_REG, and acknowledge TX_IRQ
209                hal_remote_sb( XPTR( tty_cxy , base + TTY_STATUS_REG ) , byte );
210            }
211            else         // TTY_TX full => update command arguments and exit ISR for retry
212            {
213                hal_remote_sw ( XPTR( client_cxy , &client_ptr->txt_cmd.count ), count-i );
214                hal_remote_swd( XPTR( client_cxy , &client_ptr->txt_cmd.buf_xp ), buf_xp+i );
215                return;
216            }
217        }
218    }
219
220    // The I/O operation completed when we reach this point
221
222    // mask both TTY_RX_IRQ and TTY_TX_IRQ
223    hal_remote_sw( XPTR( tty_cxy , base + TTY_CONFIG_REG ) , 0 );
224
225    // set I/O operation status in command
226    hal_remote_sw( XPTR( client_cxy , &client_ptr->txt_cmd.error ) , 0 );
227
228    // unblock server thread
229    thread_unblock( XPTR( local_cxy , &chdev->server ) , THREAD_BLOCKED_DEV_ISR );
230
231    // unblock client thread
232    thread_unblock( client_xp , THREAD_BLOCKED_IO );
233
234}  // end soclib_tty_isr()
235
Note: See TracBrowser for help on using the repository browser.