[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 |
---|
[4] | 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 ); |
---|
| 40 | |
---|
| 41 | // mask both TTY_RX_IRQ and TTY_TX_IRQ |
---|
| 42 | hal_remote_sw( XPTR( tty_cxy , tty_ptr + TTY_CONFIG_REG ) , 0 ); |
---|
| 43 | |
---|
[4] | 44 | } // soclib_tty_init() |
---|
[1] | 45 | |
---|
[4] | 46 | ////////////////////////////////////////////////////////////// |
---|
| 47 | void __attribute__ ((noinline)) soclib_tty_cmd( xptr_t th_xp ) |
---|
[1] | 48 | { |
---|
| 49 | // get client thread cluster and local pointer |
---|
| 50 | cxy_t th_cxy = GET_CXY( th_xp ); |
---|
| 51 | thread_t * th_ptr = (thread_t *)GET_PTR( th_xp ); |
---|
| 52 | |
---|
| 53 | // get command type and extended pointer on TXT device |
---|
[4] | 54 | uint32_t type = hal_remote_lw ( XPTR( th_cxy , &th_ptr->command.txt.type ) ); |
---|
| 55 | xptr_t dev_xp = (xptr_t)hal_remote_lwd( XPTR( th_cxy , &th_ptr->command.txt.dev_xp ) ); |
---|
[1] | 56 | |
---|
| 57 | // get TXT device cluster and local pointer |
---|
[4] | 58 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
---|
| 59 | chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); |
---|
[1] | 60 | |
---|
| 61 | // get extended pointer on SOCLIB_TTY base segment |
---|
| 62 | xptr_t tty_xp = (xptr_t)hal_remote_lwd( XPTR( dev_cxy , &dev_ptr->base ) ); |
---|
| 63 | |
---|
| 64 | // get SOCLIB_TTY base segment cluster and local pointer |
---|
| 65 | cxy_t tty_cxy = GET_CXY( tty_xp ); |
---|
| 66 | uint32_t * tty_ptr = (uint32_t *)GET_PTR( tty_xp ); |
---|
| 67 | |
---|
[4] | 68 | // get TTY channel index and channel base address |
---|
| 69 | uint32_t channel = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->channel ) ); |
---|
| 70 | uint32_t * base = tty_ptr + TTY_SPAN * channel; |
---|
| 71 | |
---|
[1] | 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) |
---|
[4] | 75 | xptr_t config_xp = XPTR( tty_cxy , base + TTY_CONFIG_REG ); |
---|
[1] | 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) |
---|
[4] | 87 | xptr_t config_xp = XPTR( tty_cxy , base + TTY_CONFIG_REG ); |
---|
[1] | 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 |
---|
[4] | 103 | uint32_t count = hal_remote_lw ( XPTR( th_cxy , &th_ptr->command.txt.count ) ); |
---|
| 104 | xptr_t buf_xp = hal_remote_lwd( XPTR( th_cxy , &th_ptr->command.txt.buf_xp ) ); |
---|
[1] | 105 | |
---|
| 106 | // loop on characters |
---|
| 107 | for( i = 0 ; i < count ; i++ ) |
---|
| 108 | { |
---|
| 109 | do |
---|
| 110 | { |
---|
| 111 | // get TTY_STATUS_REG |
---|
[4] | 112 | status = hal_remote_lw( XPTR( tty_cxy , base + TTY_STATUS_REG ) ); |
---|
[1] | 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 |
---|
[4] | 121 | hal_remote_sb( XPTR( tty_cxy , base + TTY_WRITE_REG ) , byte ); |
---|
[1] | 122 | } |
---|
| 123 | } |
---|
| 124 | while ( empty == false ); |
---|
| 125 | } |
---|
| 126 | } |
---|
[4] | 127 | } // soclib_tty_command() |
---|
[1] | 128 | |
---|
| 129 | |
---|
[4] | 130 | ///////////////////////////////////////////////////////////////// |
---|
| 131 | void __attribute__ ((noinline)) soclib_tty_isr( chdev_t * chdev ) |
---|
[1] | 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 | // get extended pointer on client thread |
---|
[4] | 141 | xptr_t root = XPTR( local_cxy , &chdev->wait_root ); |
---|
[1] | 142 | xptr_t client_xp = XLIST_FIRST_ELEMENT( root , thread_t , wait_list ); |
---|
| 143 | |
---|
| 144 | // get client thread cluster and local pointer |
---|
| 145 | cxy_t client_cxy = GET_CXY( client_xp ); |
---|
| 146 | thread_t * client_ptr = (thread_t *)GET_PTR( client_xp ); |
---|
| 147 | |
---|
| 148 | // get command arguments |
---|
[4] | 149 | type = hal_remote_lw ( XPTR( client_cxy , &client_ptr->command.txt.type ) ); |
---|
| 150 | count = hal_remote_lw ( XPTR( client_cxy , &client_ptr->command.txt.count ) ); |
---|
| 151 | buf_xp = hal_remote_lwd( XPTR( client_cxy , &client_ptr->command.txt.buf_xp ) ); |
---|
[1] | 152 | |
---|
| 153 | // get SOCLIB_TTY peripheral cluster and local pointer |
---|
[4] | 154 | cxy_t tty_cxy = GET_CXY( chdev->base ); |
---|
| 155 | uint32_t * tty_ptr = (uint32_t *)GET_PTR( chdev->base ); |
---|
[1] | 156 | |
---|
[4] | 157 | // get channel base address |
---|
| 158 | uint32_t * base = tty_ptr + TTY_SPAN * chdev->channel; |
---|
| 159 | |
---|
[1] | 160 | if( type == TXT_READ ) // read one single character |
---|
| 161 | { |
---|
| 162 | // get TTY_STATUS_REG |
---|
[4] | 163 | status = hal_remote_lw( XPTR( tty_cxy , base + TTY_STATUS_REG ) ); |
---|
[1] | 164 | |
---|
| 165 | if( status & TTY_STATUS_RX_FULL ) // TTY_RX full => transfer one byte |
---|
| 166 | { |
---|
| 167 | // get a byte from TTY_READ_REG, and acknowledge RX_IRQ |
---|
[4] | 168 | byte = (char)hal_remote_lb( XPTR( tty_cxy , base + TTY_READ_REG ) ); |
---|
[1] | 169 | |
---|
| 170 | // write it to command buffer |
---|
| 171 | hal_remote_sb( buf_xp , byte ); |
---|
| 172 | |
---|
| 173 | // update TTY_WRITE_REG if echo mode |
---|
| 174 | if( CONFIG_TXT_ECHO_MODE ) |
---|
| 175 | { |
---|
| 176 | if( (byte == '\b') || (byte == 0x7F) ) |
---|
| 177 | { |
---|
[4] | 178 | hal_remote_sb( XPTR( tty_cxy , base + TTY_WRITE_REG ) , '\b' ); |
---|
| 179 | hal_remote_sb( XPTR( tty_cxy , base + TTY_WRITE_REG ) , ' ' ); |
---|
| 180 | hal_remote_sb( XPTR( tty_cxy , base + TTY_WRITE_REG ) , '\b' ); |
---|
[1] | 181 | } |
---|
| 182 | else |
---|
| 183 | { |
---|
[4] | 184 | hal_remote_sw( XPTR( tty_cxy , base + TTY_WRITE_REG ) , byte ); |
---|
[1] | 185 | } |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | else // buffer empty => exit ISR for retry |
---|
| 189 | { |
---|
| 190 | return; |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | else if( type == TXT_WRITE ) // write a string |
---|
| 194 | { |
---|
| 195 | // loop on characters |
---|
| 196 | for( i = 0 ; i < count ; i++ ) |
---|
| 197 | { |
---|
| 198 | // get TTY_STATUS_REG |
---|
[4] | 199 | status = hal_remote_lw( XPTR( tty_cxy , base + TTY_STATUS_REG ) ); |
---|
[1] | 200 | |
---|
| 201 | if( (status & TTY_STATUS_TX_FULL) == 0 ) // TTY_TX empty => transfer one byte |
---|
| 202 | { |
---|
| 203 | // get one byte from command buffer |
---|
| 204 | byte = (char)hal_remote_lb( buf_xp + i ); |
---|
| 205 | |
---|
| 206 | // write byte to TTY_WRITE_REG, and acknowledge TX_IRQ |
---|
[4] | 207 | hal_remote_sb( XPTR( tty_cxy , base + TTY_STATUS_REG ) , byte ); |
---|
[1] | 208 | } |
---|
| 209 | else // TTY_TX full => update command arguments and exit ISR for retry |
---|
| 210 | { |
---|
[4] | 211 | hal_remote_sw ( XPTR( client_cxy , &client_ptr->command.txt.count ), count-i ); |
---|
| 212 | hal_remote_swd( XPTR( client_cxy , &client_ptr->command.txt.buf_xp ), buf_xp+i ); |
---|
[1] | 213 | return; |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | // The I/O operation completed when we reach this point |
---|
| 219 | |
---|
| 220 | // mask both TTY_RX_IRQ and TTY_TX_IRQ |
---|
[4] | 221 | hal_remote_sw( XPTR( tty_cxy , base + TTY_CONFIG_REG ) , 0 ); |
---|
[1] | 222 | |
---|
| 223 | // set I/O operation status in command |
---|
[4] | 224 | hal_remote_sw( XPTR( client_cxy , &client_ptr->command.txt.error ) , 0 ); |
---|
[1] | 225 | |
---|
| 226 | // unblock server thread |
---|
[4] | 227 | thread_unblock( XPTR( local_cxy , &chdev->server ) , THREAD_BLOCKED_DEV_ISR ); |
---|
[1] | 228 | |
---|
| 229 | // unblock client thread |
---|
| 230 | thread_unblock( client_xp , THREAD_BLOCKED_IO ); |
---|
| 231 | |
---|
[4] | 232 | } // soclib_tty_isr() |
---|
[1] | 233 | |
---|
| 234 | |
---|