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