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

Last change on this file since 250 was 77, checked in by max@…, 7 years ago

Start hiding the architecture-specific drivers behind the
hal_drivers_xx interface.

File size: 8.7 KB
RevLine 
[75]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{
[77]34    chdev->cmd = &soclib_tty_cmd;
35    chdev->isr = &soclib_tty_isr;
36
[75]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->command.txt.type ) );
57    xptr_t   dev_xp = (xptr_t)hal_remote_lwd( XPTR( th_cxy , &th_ptr->command.txt.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();
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();
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->command.txt.count ) );
106        xptr_t   buf_xp = hal_remote_lwd( XPTR( th_cxy , &th_ptr->command.txt.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}
130
131/////////////////////////////////////////////////////////////////
132void __attribute__ ((noinline)) soclib_tty_isr( chdev_t * chdev )
133{
134    uint32_t   type;         // command type
135    uint32_t   count;        // number of bytes in buffer
136    xptr_t     buf_xp;       // Rextended pointer on buffer
137    uint32_t   status;       // TTY terminal status
138    char       byte;         // read byte
139    uint32_t   i;
140
141    // get extended pointer on client thread
142    xptr_t root      = XPTR( local_cxy , &chdev->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->command.txt.type   ) );
151    count   = hal_remote_lw ( XPTR( client_cxy , &client_ptr->command.txt.count  ) );
152    buf_xp  = hal_remote_lwd( XPTR( client_cxy , &client_ptr->command.txt.buf_xp ) );
153
154    // get SOCLIB_TTY peripheral cluster and local pointer
155    cxy_t      tty_cxy = GET_CXY( chdev->base );
156    uint32_t * tty_ptr = (uint32_t *)GET_PTR( chdev->base );
157
158    // get channel base address
159    uint32_t * base = tty_ptr + TTY_SPAN * chdev->channel;
160
161    if( type == TXT_READ )              // read one single character
162    {
163        // get TTY_STATUS_REG
164        status = hal_remote_lw( XPTR( tty_cxy , base + TTY_STATUS_REG ) );
165
166        if( status & TTY_STATUS_RX_FULL )   // TTY_RX full => transfer one byte
167        {
168            // get a byte from TTY_READ_REG, and acknowledge RX_IRQ
169            byte = (char)hal_remote_lb( XPTR( tty_cxy , base + TTY_READ_REG ) );
170
171            // write it to command buffer
172            hal_remote_sb( buf_xp , byte );
173
174            // update TTY_WRITE_REG if echo mode
175            if( CONFIG_TXT_ECHO_MODE )
176            {
177                if( (byte == '\b') || (byte == 0x7F) )
178                        {
179                                hal_remote_sb( XPTR( tty_cxy , base + TTY_WRITE_REG ) , '\b' );
180                                hal_remote_sb( XPTR( tty_cxy , base + TTY_WRITE_REG ) , ' '  );
181                                hal_remote_sb( XPTR( tty_cxy , base + TTY_WRITE_REG ) , '\b' );
182                        }
183                else
184                {
185                                hal_remote_sw( XPTR( tty_cxy , base + TTY_WRITE_REG ) , byte );
186                        }
187            }
188        }
189        else                               // buffer empty => exit ISR for retry
190        {
191            return;
192        }
193    }
194    else if( type == TXT_WRITE )         // write a string
195    {
196        // loop on characters
197        for( i = 0 ; i < count ; i++ )
198        {
199            // get TTY_STATUS_REG
200            status = hal_remote_lw( XPTR( tty_cxy , base + TTY_STATUS_REG ) );
201
202            if( (status & TTY_STATUS_TX_FULL) == 0 ) // TTY_TX empty => transfer one byte
203            {
204                // get one byte from command buffer
205                byte = (char)hal_remote_lb( buf_xp + i );
206
207                // write byte to TTY_WRITE_REG, and acknowledge TX_IRQ
208                hal_remote_sb( XPTR( tty_cxy , base + TTY_STATUS_REG ) , byte );
209            }
210            else         // TTY_TX full => update command arguments and exit ISR for retry
211            {
212                hal_remote_sw ( XPTR( client_cxy , &client_ptr->command.txt.count ), count-i );
213                hal_remote_swd( XPTR( client_cxy , &client_ptr->command.txt.buf_xp ), buf_xp+i );
214                return;
215            }
216        }
217    }
218
219    // The I/O operation completed when we reach this point
220
221    // mask both TTY_RX_IRQ and TTY_TX_IRQ
222    hal_remote_sw( XPTR( tty_cxy , base + TTY_CONFIG_REG ) , 0 );
223
224    // set I/O operation status in command
225    hal_remote_sw( XPTR( client_cxy , &client_ptr->command.txt.error ) , 0 );
226
227    // unblock server thread
228    thread_unblock( XPTR( local_cxy , &chdev->server ) , THREAD_BLOCKED_DEV_ISR );
229
230    // unblock client thread
231    thread_unblock( client_xp , THREAD_BLOCKED_IO );
232}
233
Note: See TracBrowser for help on using the repository browser.