[1] | 1 | /* |
---|
| 2 | * dev_mmc.c - MMC (Memory Cache Controler) generic device API implementation. |
---|
| 3 | * |
---|
[437] | 4 | * Author Alain Greiner (2016,2017,2018) |
---|
[1] | 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 | |
---|
[457] | 24 | #include <hal_kernel_types.h> |
---|
[1] | 25 | #include <hal_special.h> |
---|
[257] | 26 | #include <hal_drivers.h> |
---|
[1] | 27 | #include <printk.h> |
---|
[3] | 28 | #include <chdev.h> |
---|
[1] | 29 | #include <thread.h> |
---|
[565] | 30 | #include <remote_busylock.h> |
---|
[1] | 31 | #include <dev_mmc.h> |
---|
| 32 | |
---|
| 33 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 34 | // Extern global variables |
---|
| 35 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 36 | |
---|
[3] | 37 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
---|
[1] | 38 | |
---|
[238] | 39 | ////////////////////////////////// |
---|
| 40 | void dev_mmc_init( chdev_t * mmc ) |
---|
[1] | 41 | { |
---|
| 42 | // get implementation from device descriptor |
---|
[238] | 43 | uint32_t impl = mmc->impl; |
---|
[1] | 44 | |
---|
[238] | 45 | // set mmc name |
---|
| 46 | snprintf( mmc->name , 16 , "mmc_%x" , local_cxy ); |
---|
[23] | 47 | |
---|
[257] | 48 | // call driver init function |
---|
| 49 | hal_drivers_mmc_init( mmc , impl ); |
---|
[14] | 50 | |
---|
[188] | 51 | // bind IRQ to CP0 |
---|
[238] | 52 | dev_pic_bind_irq( 0 , mmc ); |
---|
[188] | 53 | |
---|
| 54 | // enable IRQ |
---|
[238] | 55 | dev_pic_enable_irq( 0 , XPTR( local_cxy , mmc ) ); |
---|
[14] | 56 | |
---|
[1] | 57 | } // end dev_mmc_init() |
---|
| 58 | |
---|
| 59 | ///////////////////////////////////////////////////////////////////////////// |
---|
[440] | 60 | // This static function is called by all MMC device access functions. |
---|
[23] | 61 | // It makes some checking, takes the lock granting exclusive |
---|
| 62 | // access to MMC peripheral, call the driver to execute the command |
---|
[1] | 63 | // registered in the calling thread descriptor, and releases the lock. |
---|
| 64 | ///////////////////////////////////////////////////////////////////////////// |
---|
[23] | 65 | static error_t dev_mmc_access( thread_t * this ) |
---|
[1] | 66 | { |
---|
[23] | 67 | // get extended pointer on MMC device descriptor |
---|
[279] | 68 | xptr_t dev_xp = this->mmc_cmd.dev_xp; |
---|
[1] | 69 | |
---|
[492] | 70 | assert( (dev_xp != XPTR_NULL) , "target MMC device undefined" ); |
---|
[1] | 71 | |
---|
| 72 | // get MMC device cluster identifier & local pointer |
---|
[3] | 73 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
---|
[440] | 74 | chdev_t * dev_ptr = GET_PTR( dev_xp ); |
---|
[1] | 75 | |
---|
| 76 | // get driver command function pointer from MMC device descriptor |
---|
| 77 | dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->cmd ) ); |
---|
| 78 | |
---|
[565] | 79 | // get the MMC device remote busylock |
---|
| 80 | remote_busylock_acquire( XPTR( dev_cxy , &dev_ptr->wait_lock ) ); |
---|
[1] | 81 | |
---|
| 82 | // call driver command |
---|
| 83 | cmd( XPTR( local_cxy , this ) ); |
---|
| 84 | |
---|
[565] | 85 | // release the MMC device remote busylock |
---|
| 86 | remote_busylock_release( XPTR( dev_cxy , &dev_ptr->wait_lock ) ); |
---|
[1] | 87 | |
---|
[23] | 88 | // return operation status |
---|
[279] | 89 | return this->mmc_cmd.error; |
---|
[1] | 90 | |
---|
| 91 | } // end dev_mmc_access() |
---|
| 92 | |
---|
[23] | 93 | ///////////////////////////////////////// |
---|
| 94 | error_t dev_mmc_inval( xptr_t buf_xp, |
---|
[1] | 95 | uint32_t buf_size ) |
---|
| 96 | { |
---|
[23] | 97 | error_t error; |
---|
| 98 | |
---|
[1] | 99 | // get calling thread local pointer |
---|
[440] | 100 | thread_t * this = CURRENT_THREAD; |
---|
[1] | 101 | |
---|
[438] | 102 | #if DEBUG_DEV_MMC |
---|
[437] | 103 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 104 | if( DEBUG_DEV_MMC < cycle ) |
---|
[605] | 105 | printk("\n[%s] thread[%x,%x] enters / cluster %x / buffer %x\n", |
---|
| 106 | __FUNCTION__, this->process->pid, this->trdid, GET_CXY(buf_xp), GET_PTR(buf_xp) ); |
---|
[437] | 107 | #endif |
---|
[1] | 108 | |
---|
[23] | 109 | // get buffer cluster and local pointer |
---|
[626] | 110 | cxy_t buf_cxy = GET_CXY( buf_xp ); |
---|
| 111 | uint8_t * buf_ptr = GET_PTR( buf_xp ); |
---|
[23] | 112 | |
---|
[626] | 113 | // force buffer align |
---|
| 114 | uint32_t delta = (uint32_t)buf_ptr & (CONFIG_CACHE_LINE_SIZE - 1); |
---|
| 115 | uint8_t * base = buf_ptr - delta; |
---|
| 116 | uint32_t size = buf_size + delta; |
---|
[1] | 117 | |
---|
| 118 | // store command arguments in thread descriptor |
---|
[279] | 119 | this->mmc_cmd.dev_xp = chdev_dir.mmc[buf_cxy]; |
---|
| 120 | this->mmc_cmd.type = MMC_CC_INVAL; |
---|
[626] | 121 | this->mmc_cmd.buf_ptr = base; |
---|
| 122 | this->mmc_cmd.buf_size = size; |
---|
[1] | 123 | |
---|
[23] | 124 | // call MMC driver |
---|
| 125 | error = dev_mmc_access( this ); |
---|
[1] | 126 | |
---|
[438] | 127 | #if DEBUG_DEV_MMC |
---|
[437] | 128 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 129 | if( DEBUG_DEV_MMC < cycle ) |
---|
[605] | 130 | printk("\n[%s] thread[%x,%x] exit / cluster %x / buffer %x\n", |
---|
| 131 | __FUNCTION__, this->process->pid, this->trdid, GET_CXY(buf_xp), GET_PTR(buf_xp) ); |
---|
[437] | 132 | #endif |
---|
[1] | 133 | |
---|
[23] | 134 | return error; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | //////////////////////////////////////// |
---|
| 138 | error_t dev_mmc_sync( xptr_t buf_xp, |
---|
[1] | 139 | uint32_t buf_size ) |
---|
| 140 | { |
---|
[23] | 141 | error_t error; |
---|
| 142 | |
---|
[440] | 143 | thread_t * this = CURRENT_THREAD; |
---|
[1] | 144 | |
---|
[438] | 145 | #if DEBUG_DEV_MMC |
---|
[437] | 146 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 147 | if( DEBUG_DEV_MMC < cycle ) |
---|
[605] | 148 | printk("\n[%s] thread[%x,%x] enters / cluster %x / buffer %x\n", |
---|
| 149 | __FUNCTION__, this->process->pid, this->trdid, GET_CXY(buf_xp), GET_PTR(buf_xp) ); |
---|
[437] | 150 | #endif |
---|
[1] | 151 | |
---|
[23] | 152 | // get buffer cluster and local pointer |
---|
| 153 | cxy_t buf_cxy = GET_CXY( buf_xp ); |
---|
| 154 | void * buf_ptr = GET_PTR( buf_xp ); |
---|
| 155 | |
---|
[626] | 156 | // force buffer align |
---|
| 157 | uint32_t delta = (uint32_t)buf_ptr & (CONFIG_CACHE_LINE_SIZE - 1); |
---|
| 158 | uint8_t * base = buf_ptr - delta; |
---|
| 159 | uint32_t size = buf_size + delta; |
---|
[1] | 160 | |
---|
| 161 | // store command arguments in thread descriptor |
---|
[279] | 162 | this->mmc_cmd.dev_xp = chdev_dir.mmc[buf_cxy]; |
---|
| 163 | this->mmc_cmd.type = MMC_CC_SYNC; |
---|
[626] | 164 | this->mmc_cmd.buf_ptr = base; |
---|
| 165 | this->mmc_cmd.buf_size = size; |
---|
[1] | 166 | |
---|
[23] | 167 | // call MMC driver |
---|
| 168 | error = dev_mmc_access( this ); |
---|
[1] | 169 | |
---|
[438] | 170 | #if DEBUG_DEV_MMC |
---|
[437] | 171 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 172 | if( DEBUG_DEV_MMC < cycle ) |
---|
[605] | 173 | printk("\n[%s] thread[%x,%x] exit / cluster %x / buffer %x\n", |
---|
| 174 | __FUNCTION__, this->process->pid, this->trdid, GET_CXY(buf_xp), GET_PTR(buf_xp) ); |
---|
[437] | 175 | #endif |
---|
[1] | 176 | |
---|
[23] | 177 | return error; |
---|
| 178 | } |
---|
| 179 | |
---|
[1] | 180 | ///////////////////////////////////////// |
---|
| 181 | error_t dev_mmc_set_error( cxy_t cxy, |
---|
| 182 | uint32_t index, |
---|
| 183 | uint32_t wdata ) |
---|
| 184 | { |
---|
| 185 | // get calling thread local pointer |
---|
| 186 | thread_t * this = CURRENT_THREAD; |
---|
| 187 | |
---|
| 188 | // store command arguments in thread descriptor |
---|
[279] | 189 | this->mmc_cmd.dev_xp = chdev_dir.mmc[cxy]; |
---|
| 190 | this->mmc_cmd.type = MMC_SET_ERROR; |
---|
| 191 | this->mmc_cmd.reg_index = index; |
---|
| 192 | this->mmc_cmd.reg_ptr = &wdata; |
---|
[1] | 193 | |
---|
| 194 | // execute operation |
---|
[23] | 195 | return dev_mmc_access( this ); |
---|
[1] | 196 | } |
---|
| 197 | |
---|
| 198 | ////////////////////////////////////////// |
---|
| 199 | error_t dev_mmc_get_error( cxy_t cxy, |
---|
| 200 | uint32_t index, |
---|
| 201 | uint32_t * rdata ) |
---|
| 202 | { |
---|
| 203 | // get calling thread local pointer |
---|
| 204 | thread_t * this = CURRENT_THREAD; |
---|
| 205 | |
---|
| 206 | // store command arguments in thread descriptor |
---|
[279] | 207 | this->mmc_cmd.dev_xp = chdev_dir.mmc[cxy]; |
---|
| 208 | this->mmc_cmd.type = MMC_GET_ERROR; |
---|
| 209 | this->mmc_cmd.reg_index = index; |
---|
| 210 | this->mmc_cmd.reg_ptr = rdata; |
---|
[1] | 211 | |
---|
| 212 | // execute operation |
---|
[23] | 213 | return dev_mmc_access( this ); |
---|
[1] | 214 | } |
---|
| 215 | |
---|
| 216 | //////////////////////////////////////////////////// |
---|
| 217 | error_t dev_mmc_get_instrumentation( cxy_t cxy, |
---|
| 218 | uint32_t index, |
---|
| 219 | uint32_t * rdata ) |
---|
| 220 | { |
---|
| 221 | // get calling thread local pointer |
---|
| 222 | thread_t * this = CURRENT_THREAD; |
---|
| 223 | |
---|
| 224 | // store command arguments in thread descriptor |
---|
[279] | 225 | this->mmc_cmd.dev_xp = chdev_dir.mmc[cxy]; |
---|
| 226 | this->mmc_cmd.type = MMC_GET_INSTRU; |
---|
| 227 | this->mmc_cmd.reg_index = index; |
---|
| 228 | this->mmc_cmd.reg_ptr = rdata; |
---|
[1] | 229 | |
---|
| 230 | // execute operation |
---|
[23] | 231 | return dev_mmc_access( this ); |
---|
[1] | 232 | } |
---|
| 233 | |
---|