[258] | 1 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 2 | // File : tty_driver.c |
---|
| 3 | // Date : 23/05/2013 |
---|
| 4 | // Author : alain greiner |
---|
| 5 | // Copyright (c) UPMC-LIP6 |
---|
| 6 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 7 | // The tty_driver.c and tty_drivers.h files are part ot the GIET-VM kernel. |
---|
| 8 | // This driver supports the SocLib vci_multi_tty component. |
---|
| 9 | // |
---|
| 10 | // The total number of TTY terminals must be defined by the configuration |
---|
| 11 | // parameter NB_TTY_CHANNELS in the hard_config.h file. |
---|
| 12 | // |
---|
| 13 | // The "system" terminal is TTY[0]. |
---|
| 14 | // The "user" TTYs are allocated to applications by the GIET in the boot phase, |
---|
| 15 | // as defined in the mapping_info data structure. The corresponding tty_id must |
---|
| 16 | // be stored in the context of the task by the boot code. |
---|
| 17 | // |
---|
[295] | 18 | // The seg_tty_base must be defined in giet_vsegs.ld file. |
---|
[258] | 19 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[295] | 20 | // Implementation note: |
---|
| 21 | // |
---|
| 22 | // All physical accesses to device registers are done by the two |
---|
| 23 | // _tty_get_register(), _tty_set_register() low-level functions, |
---|
| 24 | // that are handling virtual / physical addressing. |
---|
| 25 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[258] | 26 | |
---|
| 27 | #include <giet_config.h> |
---|
| 28 | #include <tty_driver.h> |
---|
[295] | 29 | #include <xcu_driver.h> |
---|
[258] | 30 | #include <ctx_handler.h> |
---|
| 31 | #include <utils.h> |
---|
| 32 | |
---|
| 33 | #if !defined(NB_TTY_CHANNELS) |
---|
| 34 | # error: You must define NB_TTY_CHANNELS in the hard_config.h file |
---|
| 35 | #endif |
---|
| 36 | |
---|
| 37 | #if (NB_TTY_CHANNELS < 1) |
---|
| 38 | # error: NB_TTY_CHANNELS cannot be smaller than 1! |
---|
| 39 | #endif |
---|
| 40 | |
---|
| 41 | #define in_unckdata __attribute__((section (".unckdata"))) |
---|
[313] | 42 | #define in_kdata __attribute__((section (".kdata"))) |
---|
[258] | 43 | |
---|
[295] | 44 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 45 | // TTY global variables |
---|
| 46 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 47 | |
---|
| 48 | in_unckdata volatile unsigned int _tty_rx_buf[NB_TTY_CHANNELS]; |
---|
[313] | 49 | |
---|
[295] | 50 | in_unckdata volatile unsigned int _tty_rx_full[NB_TTY_CHANNELS] |
---|
[258] | 51 | = { [0 ... NB_TTY_CHANNELS - 1] = 0 }; |
---|
| 52 | |
---|
[313] | 53 | in_kdata unsigned int _tty_lock[NB_TTY_CHANNELS] |
---|
| 54 | = { [0 ... NB_TTY_CHANNELS - 1] = 0 }; |
---|
| 55 | |
---|
[295] | 56 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 57 | // This low level function returns the value of register (channel / index) |
---|
| 58 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 59 | unsigned int _tty_get_register( unsigned int channel, |
---|
| 60 | unsigned int index ) |
---|
| 61 | { |
---|
| 62 | unsigned int* vaddr = (unsigned int*)&seg_tty_base + channel*TTY_SPAN + index; |
---|
| 63 | return _io_extended_read( vaddr ); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 67 | // This low level function set a new value in register (channel / index) |
---|
| 68 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 69 | void _tty_set_register( unsigned int channel, |
---|
| 70 | unsigned int index, |
---|
| 71 | unsigned int value ) |
---|
| 72 | { |
---|
| 73 | unsigned int* vaddr = (unsigned int*)&seg_tty_base + channel*TTY_SPAN + index; |
---|
| 74 | _io_extended_write( vaddr, value ); |
---|
| 75 | } |
---|
| 76 | |
---|
[258] | 77 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 78 | // This non-blocking function writes a character string from a fixed-length |
---|
[295] | 79 | // buffer to a TTY terminal identified by the channel argument. |
---|
| 80 | // This function is intended to be used to handle a system call, and should |
---|
| 81 | // not be used by the kernel for log messages on TTY 0. |
---|
| 82 | // protecting exclusive access to the selected terminal. |
---|
| 83 | // If channel argument is 0xFFFFFFFF, the TTY index is found in the task context. |
---|
[258] | 84 | // This is a non blocking call: it tests the TTY_STATUS register, and stops |
---|
| 85 | // the transfer as soon as the TTY_STATUS[WRITE] bit is set. |
---|
| 86 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 87 | // Returns the number of characters that have been written. |
---|
| 88 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 89 | unsigned int _tty_write( const char* buffer, |
---|
| 90 | unsigned int length, // number of characters |
---|
| 91 | unsigned int channel) // channel index |
---|
| 92 | { |
---|
[295] | 93 | unsigned int nwritten; |
---|
[258] | 94 | |
---|
[295] | 95 | // compute and check tty channel |
---|
| 96 | if( channel == 0xFFFFFFFF ) channel = _get_context_slot(CTX_TTY_ID); |
---|
| 97 | if( channel >= NB_TTY_CHANNELS ) return -1; |
---|
[258] | 98 | |
---|
| 99 | // write string to TTY channel |
---|
| 100 | for (nwritten = 0; nwritten < length; nwritten++) |
---|
| 101 | { |
---|
| 102 | // check tty's status |
---|
[295] | 103 | if ( _tty_get_register( channel, TTY_STATUS ) & 0x2 ) break; |
---|
| 104 | |
---|
| 105 | // write one byte |
---|
[315] | 106 | if (buffer[nwritten] == '\n') { |
---|
| 107 | _tty_set_register( channel, TTY_WRITE, (unsigned int)'\r' ); |
---|
| 108 | } |
---|
[295] | 109 | _tty_set_register( channel, TTY_WRITE, (unsigned int)buffer[nwritten] ); |
---|
[258] | 110 | } |
---|
[295] | 111 | |
---|
[258] | 112 | return nwritten; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 116 | // This non-blocking function fetches one character from the |
---|
| 117 | // terminal identified by the channel argument. If the channel argument |
---|
| 118 | // is 0xFFFFFFFF, the channel index is obtained from the current task context. |
---|
| 119 | // It uses the TTY_GET_IRQ[tty_id] interrupt and the buffer must have been |
---|
| 120 | // filled by the TTY_ISR. |
---|
[295] | 121 | // It test the _tty_rx_full[tty_id] variable, read the _tty_rx_buf[tty_id] |
---|
[258] | 122 | // buffer, writes this character to the target buffer, and resets the |
---|
[295] | 123 | // _tty_rx_full[tty_id] register. |
---|
[258] | 124 | // The length argument is not used. |
---|
| 125 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 126 | // Returns the number of characters that have been read (0/1). |
---|
| 127 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 128 | unsigned int _tty_read( char* buffer, |
---|
| 129 | unsigned int length, // unused |
---|
| 130 | unsigned int channel) // channel index |
---|
| 131 | { |
---|
[295] | 132 | // compute and check tty channel |
---|
| 133 | if( channel == 0xFFFFFFFF ) channel = _get_context_slot(CTX_TTY_ID); |
---|
| 134 | if( channel >= NB_TTY_CHANNELS ) return -1; |
---|
[258] | 135 | |
---|
| 136 | // read one character from TTY channel |
---|
[295] | 137 | if (_tty_rx_full[channel] == 0) |
---|
[258] | 138 | { |
---|
| 139 | return 0; |
---|
| 140 | } |
---|
| 141 | else |
---|
| 142 | { |
---|
[295] | 143 | *buffer = _tty_rx_buf[channel]; |
---|
| 144 | _tty_rx_full[channel] = 0; |
---|
[258] | 145 | return 1; |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | ////////////////////////////////////////////////////////////////////////////// |
---|
[295] | 150 | // This function try to take the hardwired lock protecting |
---|
| 151 | // exclusive access to TTY terminal identified by the "channel" argument. |
---|
| 152 | // It enters a critical section before taking the lock, and save the SR value |
---|
| 153 | // at address defined by the "save_sr_ptr" argument. |
---|
[258] | 154 | // It returns only when the lock has been successfully taken. |
---|
| 155 | ////////////////////////////////////////////////////////////////////////////// |
---|
[295] | 156 | void _tty_get_lock( unsigned int channel, |
---|
| 157 | unsigned int * save_sr_ptr ) |
---|
[258] | 158 | { |
---|
[295] | 159 | if( channel >= NB_TTY_CHANNELS ) _exit(); |
---|
[313] | 160 | |
---|
[295] | 161 | _it_disable( save_sr_ptr ); |
---|
[313] | 162 | |
---|
| 163 | // while ( _tty_get_register( channel, TTY_CONFIG ) ); // busy waiting |
---|
| 164 | |
---|
| 165 | _get_lock( &_tty_lock[channel] ); |
---|
[258] | 166 | } |
---|
| 167 | |
---|
| 168 | ////////////////////////////////////////////////////////////////////////////// |
---|
[295] | 169 | // This function releases the hardwired lock protecting |
---|
| 170 | // exclusive access to TTY terminal identified by the channel argument. |
---|
| 171 | // It exit the critical section after lock release, and restore SR value |
---|
| 172 | // from address defined by the "save_sr_ptr" argument. |
---|
[258] | 173 | ////////////////////////////////////////////////////////////////////////////// |
---|
[295] | 174 | void _tty_release_lock( unsigned int channel, |
---|
| 175 | unsigned int * save_sr_ptr ) |
---|
[258] | 176 | { |
---|
[295] | 177 | if( channel >= NB_TTY_CHANNELS ) _exit(); |
---|
[258] | 178 | |
---|
[313] | 179 | // _tty_set_register( channel, TTY_CONFIG, 0 ); |
---|
| 180 | |
---|
| 181 | _release_lock( &_tty_lock[channel] ); |
---|
| 182 | |
---|
[295] | 183 | _it_restore( save_sr_ptr ); |
---|
[258] | 184 | } |
---|
| 185 | |
---|
[295] | 186 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[297] | 187 | // This ISR handles the IRQ signaling that the RX buffer is not empty. |
---|
[295] | 188 | // IT can be an HWI or an SWI. |
---|
| 189 | // There is one communication buffer _tty_rx_buf[i] and one synchronisation |
---|
| 190 | // variable _tty_rx_full[i] per channel. |
---|
[297] | 191 | // Does nothing if the TTY_RX buffer is empty, or if the kernel buffer is full |
---|
| 192 | // when the ISR is called. |
---|
[295] | 193 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 194 | void _tty_rx_isr( unsigned int irq_type, // HWI / WTI |
---|
| 195 | unsigned int irq_id, // index returned by XCU |
---|
| 196 | unsigned int channel ) // TTY channel |
---|
[258] | 197 | { |
---|
[295] | 198 | unsigned int cluster_xy = _get_procid() / NB_PROCS_MAX; |
---|
[258] | 199 | |
---|
[297] | 200 | // get TTY status |
---|
| 201 | unsigned int status = _tty_get_register( channel, TTY_STATUS ); |
---|
| 202 | |
---|
| 203 | // check both TTY status and kernel buffer status: |
---|
| 204 | // does nothing if kernel buffer full or tty_buffer empty |
---|
| 205 | if ( ((status & 0x1) == 0) || |
---|
| 206 | (_tty_rx_full[channel] != 0) ) return; |
---|
| 207 | |
---|
| 208 | // reset WTI in XCU if WTI type |
---|
| 209 | if ( irq_type == IRQ_TYPE_WTI ) |
---|
[258] | 210 | { |
---|
[295] | 211 | unsigned int value; |
---|
| 212 | _xcu_get_wti_value( cluster_xy, irq_id, &value ); |
---|
[258] | 213 | } |
---|
| 214 | |
---|
[297] | 215 | // transfer character to kernel buffer and acknowledge TTY IRQ |
---|
| 216 | _tty_rx_buf[channel] = _tty_get_register( channel, TTY_READ ); |
---|
| 217 | |
---|
| 218 | // set kernel buffer status |
---|
| 219 | asm volatile( "sync" ); |
---|
| 220 | _tty_rx_full[channel] = 1; |
---|
| 221 | |
---|
[295] | 222 | #if GIET_DEBUG_IRQS // we don't take the TTY lock to avoid deadlock |
---|
| 223 | unsigned int x = cluster_xy >> Y_WIDTH; |
---|
| 224 | unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1); |
---|
| 225 | unsigned int lpid = _get_procid() % NB_PROCS_MAX; |
---|
| 226 | _puts("\n[IRQS DEBUG] Processor["); |
---|
| 227 | _putd(x ); |
---|
| 228 | _puts(","); |
---|
| 229 | _putd(y ); |
---|
| 230 | _puts(","); |
---|
| 231 | _putd(lpid ); |
---|
| 232 | _puts("] enters _tty_rx_isr() at cycle "); |
---|
| 233 | _putd(_get_proctime() ); |
---|
| 234 | _puts("\n read byte = "); |
---|
| 235 | _putx(_tty_rx_buf[channel] ); |
---|
| 236 | _puts("\n"); |
---|
| 237 | #endif |
---|
[258] | 238 | |
---|
| 239 | } |
---|
| 240 | |
---|
[295] | 241 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 242 | // This ISR handles the IRQ signaling that the TX buffer is empty. |
---|
| 243 | // IT can be an HWI or an SWI. |
---|
| 244 | // There is one single multi_tty component controling all channels. |
---|
| 245 | // There is one communication buffer _tty_rx_buf[i] and one synchronisation |
---|
| 246 | // variable _tty_rx_full[i] per channel. |
---|
| 247 | // A character is lost if the buffer is full when the ISR is executed. |
---|
| 248 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 249 | void _tty_tx_isr( unsigned int irq_type, // HWI / WTI |
---|
| 250 | unsigned int irq_id, // index returned by XCU |
---|
| 251 | unsigned int channel ) // TTY channel |
---|
[258] | 252 | { |
---|
[295] | 253 | _puts("\n[GIET ERROR] the _tty_tx_isr() is not implemented\n"); |
---|
| 254 | _exit(); |
---|
[258] | 255 | } |
---|
| 256 | |
---|
| 257 | // Local Variables: |
---|
| 258 | // tab-width: 4 |
---|
| 259 | // c-basic-offset: 4 |
---|
| 260 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 261 | // indent-tabs-mode: nil |
---|
| 262 | // End: |
---|
| 263 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 264 | |
---|