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