[1] | 1 | /* |
---|
| 2 | * dev_txt.c - TXT (Text Terminal) generic device API implementation. |
---|
[49] | 3 | * |
---|
[1] | 4 | * Author Alain Greiner (2016) |
---|
| 5 | * |
---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 7 | * |
---|
| 8 | * This file is part of ALMOS-MK |
---|
| 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-kernel; if not, write to the Free Software Foundation, |
---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | #include <hal_types.h> |
---|
| 25 | #include <hal_special.h> |
---|
| 26 | #include <hal_remote.h> |
---|
[77] | 27 | #include <hal_drivers.h> |
---|
[1] | 28 | #include <thread.h> |
---|
[407] | 29 | #include <chdev.h> |
---|
[1] | 30 | #include <rpc.h> |
---|
| 31 | #include <printk.h> |
---|
| 32 | #include <dev_txt.h> |
---|
| 33 | |
---|
| 34 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 35 | // Extern global variables |
---|
| 36 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 37 | |
---|
[3] | 38 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
---|
[1] | 39 | |
---|
[407] | 40 | #if CONFIG_READ_DEBUG |
---|
| 41 | extern uint32_t enter_txt_read; |
---|
| 42 | extern uint32_t exit_txt_read; |
---|
| 43 | #endif |
---|
| 44 | |
---|
[188] | 45 | ////////////////////////////////// |
---|
| 46 | void dev_txt_init( chdev_t * txt ) |
---|
[1] | 47 | { |
---|
[188] | 48 | // For all TXT channels other than the TXT0 (kernel terminal), |
---|
| 49 | // the PIC chdev must be initialized before the TXT chdev, because |
---|
| 50 | // the TXT chdev initialization requires the routing of an external IRQ |
---|
[1] | 51 | |
---|
[188] | 52 | xptr_t pic_xp = chdev_dir.pic; |
---|
| 53 | uint32_t channel = txt->channel; |
---|
| 54 | uint32_t impl = txt->impl; |
---|
[407] | 55 | bool_t is_rx = txt->is_rx; |
---|
[1] | 56 | |
---|
[188] | 57 | assert( (pic_xp != XPTR_NULL) || (channel == 0) , __FUNCTION__ , |
---|
| 58 | "PIC not initialised before TXT" ); |
---|
| 59 | |
---|
[23] | 60 | // set chdev name |
---|
[407] | 61 | if( is_rx ) snprintf( txt->name , 16 , "txt%d_rx" , channel ); |
---|
| 62 | else snprintf( txt->name , 16 , "txt%d_tx" , channel ); |
---|
[23] | 63 | |
---|
[422] | 64 | // set TXT chdev extension |
---|
| 65 | txt->ext.txt.owner_xp = XPTR_NULL; |
---|
| 66 | xlist_root_init( XPTR( local_cxy, &txt->ext.txt.root ) ); |
---|
| 67 | remote_spinlock_init( XPTR( local_cxy , &txt->ext.txt.lock ) ); |
---|
| 68 | |
---|
[245] | 69 | // call driver init function |
---|
| 70 | hal_drivers_txt_init(txt, impl); |
---|
[1] | 71 | |
---|
[422] | 72 | // no server thread and no IRQ routing for TXT0 |
---|
| 73 | // no server thread for IMPL_TXT_RS2 (multiplexed in software, not hardware) |
---|
[255] | 74 | if( channel != 0 && impl != IMPL_TXT_RS2 ) |
---|
[188] | 75 | { |
---|
[407] | 76 | // select a core to execute the server thread |
---|
[188] | 77 | lid_t lid = cluster_select_local_core(); |
---|
[3] | 78 | |
---|
[407] | 79 | // bind IRQ to the selected core |
---|
[188] | 80 | dev_pic_bind_irq( lid , txt ); |
---|
[3] | 81 | |
---|
[407] | 82 | // enable IRQ |
---|
[238] | 83 | dev_pic_enable_irq( lid , XPTR( local_cxy , txt ) ); |
---|
[3] | 84 | |
---|
[188] | 85 | // create server thread |
---|
| 86 | thread_t * new_thread; |
---|
| 87 | error_t error; |
---|
[3] | 88 | |
---|
[188] | 89 | error = thread_kernel_create( &new_thread, |
---|
| 90 | THREAD_DEV, |
---|
| 91 | &chdev_sequencial_server, |
---|
| 92 | txt, |
---|
| 93 | lid ); |
---|
[3] | 94 | |
---|
[188] | 95 | assert( (error == 0) , __FUNCTION__ , "cannot create server thread" ); |
---|
[1] | 96 | |
---|
[188] | 97 | // set "server" field in chdev descriptor |
---|
| 98 | txt->server = new_thread; |
---|
[1] | 99 | |
---|
[408] | 100 | // set "chdev" field in thread descriptor |
---|
| 101 | new_thread->chdev = txt; |
---|
| 102 | |
---|
| 103 | // unblock server thread |
---|
[188] | 104 | thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL ); |
---|
| 105 | } |
---|
[49] | 106 | } |
---|
[1] | 107 | |
---|
| 108 | ////////////////////////////////////////////////////////////////////////////////// |
---|
[3] | 109 | // This static function is called by dev_txt_read(), dev_txt_write() functions. |
---|
[1] | 110 | ////////////////////////////////////i///////////////////////////////////////////// |
---|
| 111 | static error_t dev_txt_access( uint32_t type, |
---|
| 112 | uint32_t channel, |
---|
| 113 | char * buffer, |
---|
| 114 | uint32_t count ) |
---|
| 115 | { |
---|
[407] | 116 | xptr_t dev_xp; |
---|
[1] | 117 | thread_t * this = CURRENT_THREAD; |
---|
| 118 | |
---|
[433] | 119 | #if CONFIG_DEBUG_DEV_TXT |
---|
| 120 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 121 | if( CONFIG_DEBUG_DEV_TXT < cycle ) |
---|
| 122 | printk("\n[DBG] %s : thread %x enters / cycle %d\n", |
---|
| 123 | __FUNCTION__, CURRENT_THREAD , cycle ); |
---|
| 124 | #endif |
---|
[1] | 125 | |
---|
| 126 | // check channel argument |
---|
[3] | 127 | assert( (channel < CONFIG_MAX_TXT_CHANNELS) , __FUNCTION__ , "illegal channel index" ); |
---|
[1] | 128 | |
---|
[3] | 129 | // get extended pointer on remote TXT chdev descriptor |
---|
[407] | 130 | if( type == TXT_WRITE ) dev_xp = chdev_dir.txt_tx[channel]; |
---|
| 131 | else dev_xp = chdev_dir.txt_rx[channel]; |
---|
[1] | 132 | |
---|
[3] | 133 | assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined TXT chdev descriptor" ); |
---|
[1] | 134 | |
---|
| 135 | // register command in calling thread descriptor |
---|
[279] | 136 | this->txt_cmd.dev_xp = dev_xp; |
---|
| 137 | this->txt_cmd.type = type; |
---|
| 138 | this->txt_cmd.buf_xp = XPTR( local_cxy , buffer ); |
---|
| 139 | this->txt_cmd.count = count; |
---|
[1] | 140 | |
---|
[3] | 141 | // register client thread in waiting queue, activate server thread |
---|
| 142 | // block client thread on THREAD_BLOCKED_IO and deschedule. |
---|
| 143 | // it is re-activated by the ISR signaling IO operation completion. |
---|
[407] | 144 | chdev_register_command( dev_xp ); |
---|
[1] | 145 | |
---|
[433] | 146 | #if CONFIG_DEBUG_DEV_TXT |
---|
| 147 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 148 | if( CONFIG_DEBUG_DEV_TXT < cycle ) |
---|
| 149 | printk("\n[DBG] %s : thread %x exit / cycle %d\n", |
---|
| 150 | __FUNCTION__, CURRENT_THREAD , cycle ); |
---|
| 151 | #endif |
---|
[1] | 152 | |
---|
| 153 | // return I/O operation status from calling thread descriptor |
---|
[279] | 154 | return this->txt_cmd.error; |
---|
[49] | 155 | } |
---|
[1] | 156 | |
---|
| 157 | ///////////////////////////////////////// |
---|
| 158 | error_t dev_txt_write( uint32_t channel, |
---|
| 159 | char * buffer, |
---|
| 160 | uint32_t count ) |
---|
| 161 | { |
---|
[407] | 162 | error_t error = dev_txt_access( TXT_WRITE , channel , buffer , count ); |
---|
| 163 | return error; |
---|
[1] | 164 | } |
---|
[49] | 165 | |
---|
[1] | 166 | ///////////////////////////////////////// |
---|
| 167 | error_t dev_txt_read( uint32_t channel, |
---|
| 168 | char * buffer ) |
---|
| 169 | { |
---|
[407] | 170 | |
---|
| 171 | #if CONFIG_READ_DEBUG |
---|
| 172 | enter_txt_read = hal_time_stamp(); |
---|
| 173 | #endif |
---|
| 174 | |
---|
| 175 | error_t error = dev_txt_access( TXT_READ , channel , buffer , 1 ); |
---|
| 176 | |
---|
| 177 | #if CONFIG_READ_DEBUG |
---|
| 178 | exit_txt_read = hal_time_stamp(); |
---|
| 179 | #endif |
---|
| 180 | |
---|
| 181 | return error; |
---|
| 182 | |
---|
[1] | 183 | } |
---|
| 184 | |
---|
[407] | 185 | ////////////////////////////////////////////// |
---|
| 186 | error_t dev_txt_sync_write( char * buffer, |
---|
[3] | 187 | uint32_t count ) |
---|
[1] | 188 | { |
---|
[49] | 189 | // get extended pointer on TXT[0] chdev |
---|
[407] | 190 | xptr_t dev_xp = chdev_dir.txt_tx[0]; |
---|
[1] | 191 | |
---|
[407] | 192 | assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , |
---|
| 193 | "undefined TXT0 chdev descriptor" ); |
---|
[23] | 194 | |
---|
[407] | 195 | // get TXTO chdev cluster and local pointer |
---|
| 196 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
---|
| 197 | chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); |
---|
[1] | 198 | |
---|
[49] | 199 | // get driver command function |
---|
[407] | 200 | dev_aux_t * aux = (dev_aux_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->aux ) ); |
---|
[1] | 201 | |
---|
[407] | 202 | // build arguments structure |
---|
| 203 | txt_aux_t args; |
---|
| 204 | args.dev_xp = dev_xp; |
---|
| 205 | args.buffer = buffer; |
---|
| 206 | args.count = count; |
---|
| 207 | |
---|
[279] | 208 | // call driver function |
---|
[407] | 209 | aux( &args ); |
---|
[1] | 210 | |
---|
[407] | 211 | return 0; |
---|
[49] | 212 | } |
---|
[1] | 213 | |
---|