[1] | 1 | /* |
---|
| 2 | * dev_mmc.c - MMC (Memory Cache Controler) generic device API implementation. |
---|
| 3 | * |
---|
| 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 <soclib_mmc.h> |
---|
| 27 | #include <printk.h> |
---|
[3] | 28 | #include <chdev.h> |
---|
[1] | 29 | #include <thread.h> |
---|
| 30 | #include <dev_mmc.h> |
---|
| 31 | |
---|
| 32 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 33 | // Extern global variables |
---|
| 34 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 35 | |
---|
[3] | 36 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
---|
[1] | 37 | |
---|
[3] | 38 | extern chdev_icu_input_t chdev_icu_input; // allocated in kernel_init.c |
---|
[1] | 39 | |
---|
[3] | 40 | //////////////////////////////////// |
---|
| 41 | void dev_mmc_init( chdev_t * chdev ) |
---|
[1] | 42 | { |
---|
| 43 | // get implementation from device descriptor |
---|
[3] | 44 | uint32_t impl = chdev->impl; |
---|
[1] | 45 | |
---|
[3] | 46 | // set driver specific fields in device descriptor and call driver init function |
---|
[1] | 47 | if( impl == IMPL_MMC_TSR ) |
---|
| 48 | { |
---|
[3] | 49 | chdev->cmd = &soclib_mmc_cmd; |
---|
| 50 | chdev->isr = &soclib_mmc_isr; |
---|
| 51 | soclib_mmc_init( chdev ); |
---|
[1] | 52 | } |
---|
| 53 | else |
---|
| 54 | { |
---|
[3] | 55 | assert( false , __FUNCTION__ , "undefined MMC device implementation" ); |
---|
[1] | 56 | } |
---|
| 57 | } // end dev_mmc_init() |
---|
| 58 | |
---|
| 59 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 60 | // This static function makes some checking, takes the lock granting |
---|
| 61 | // exclusive access to MMC peripheral, call the driver to execute the command |
---|
| 62 | // registered in the calling thread descriptor, and releases the lock. |
---|
| 63 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 64 | static void dev_mmc_access( thread_t * this ) |
---|
| 65 | { |
---|
| 66 | mmc_dmsg("\n[INFO] %s enters for thread %x in process %x : command = %d\n", |
---|
[3] | 67 | __FUNCTION__ , this->trdid , this->process->pid , this->command.mmc.type ); |
---|
[1] | 68 | |
---|
| 69 | // get extended pointer on MMC device |
---|
[3] | 70 | xptr_t dev_xp = this->command.mmc.dev_xp; |
---|
[1] | 71 | |
---|
[3] | 72 | assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined MMC device descriptor" ); |
---|
[1] | 73 | |
---|
| 74 | // get MMC device cluster identifier & local pointer |
---|
[3] | 75 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
---|
| 76 | chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); |
---|
[1] | 77 | |
---|
| 78 | // get driver command function pointer from MMC device descriptor |
---|
| 79 | dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->cmd ) ); |
---|
| 80 | |
---|
| 81 | // get the MMC device remote spinlock |
---|
| 82 | remote_spinlock_lock( XPTR( dev_cxy , &dev_ptr->wait_lock ) ); |
---|
| 83 | |
---|
| 84 | // call driver command |
---|
| 85 | cmd( XPTR( local_cxy , this ) ); |
---|
| 86 | |
---|
| 87 | // release the MMC device remote spinlock |
---|
| 88 | remote_spinlock_unlock( XPTR( dev_cxy , &dev_ptr->wait_lock ) ); |
---|
| 89 | |
---|
| 90 | mmc_dmsg("\n[INFO] %s completes for thread %x in process %x / error = %d\n", |
---|
| 91 | __FUNCTION__ , this->trdid , this->process->pid , this->dev.ioc.error ); |
---|
| 92 | |
---|
| 93 | } // end dev_mmc_access() |
---|
| 94 | |
---|
| 95 | //////////////////////////////////////////// |
---|
| 96 | error_t dev_mmc_inval( paddr_t buf_paddr, |
---|
| 97 | uint32_t buf_size ) |
---|
| 98 | { |
---|
| 99 | // get calling thread local pointer |
---|
| 100 | thread_t * this = CURRENT_THREAD; |
---|
| 101 | |
---|
| 102 | // get target cluster from paddr |
---|
| 103 | cxy_t cxy = CXY_FROM_PADDR( buf_paddr ); |
---|
| 104 | |
---|
[3] | 105 | assert( ((buf_paddr & (CONFIG_CACHE_LINE_SIZE -1)) == 0) , __FUNCTION__ , |
---|
| 106 | "buffer not aligned on cache line" ); |
---|
[1] | 107 | |
---|
| 108 | // get extended pointer on MMC device descriptor |
---|
[3] | 109 | xptr_t dev_xp = chdev_dir.mmc[cxy]; |
---|
[1] | 110 | |
---|
| 111 | // store command arguments in thread descriptor |
---|
[3] | 112 | this->command.mmc.dev_xp = dev_xp; |
---|
| 113 | this->command.mmc.type = MMC_CC_INVAL; |
---|
| 114 | this->command.mmc.buf_paddr = buf_paddr; |
---|
| 115 | this->command.mmc.buf_size = buf_size; |
---|
[1] | 116 | |
---|
| 117 | // execute operation |
---|
| 118 | dev_mmc_access( this ); |
---|
| 119 | |
---|
| 120 | // return operation status |
---|
[3] | 121 | return this->command.mmc.error; |
---|
[1] | 122 | } |
---|
| 123 | |
---|
| 124 | ///////////////////////////////////// |
---|
| 125 | error_t dev_mmc_sync( paddr_t buf_paddr, |
---|
| 126 | uint32_t buf_size ) |
---|
| 127 | { |
---|
| 128 | // get calling thread local pointer |
---|
| 129 | thread_t * this = CURRENT_THREAD; |
---|
| 130 | |
---|
| 131 | // get target cluster from paddr |
---|
| 132 | cxy_t cxy = CXY_FROM_PADDR( buf_paddr ); |
---|
| 133 | |
---|
[3] | 134 | assert( ((buf_paddr & (CONFIG_CACHE_LINE_SIZE -1)) == 0) , __FUNCTION__ , |
---|
| 135 | "buffer not aligned on cache line" ); |
---|
[1] | 136 | |
---|
| 137 | // store command arguments in thread descriptor |
---|
[3] | 138 | this->command.mmc.dev_xp = chdev_dir.mmc[cxy]; |
---|
| 139 | this->command.mmc.type = MMC_CC_SYNC; |
---|
| 140 | this->command.mmc.buf_paddr = buf_paddr; |
---|
| 141 | this->command.mmc.buf_size = buf_size; |
---|
[1] | 142 | |
---|
| 143 | // execute operation |
---|
| 144 | dev_mmc_access( this ); |
---|
| 145 | |
---|
| 146 | // return operation status |
---|
[3] | 147 | return this->command.mmc.error; |
---|
[1] | 148 | } |
---|
| 149 | |
---|
| 150 | ///////////////////////////////////////// |
---|
| 151 | error_t dev_mmc_set_error( cxy_t cxy, |
---|
| 152 | uint32_t index, |
---|
| 153 | uint32_t wdata ) |
---|
| 154 | { |
---|
| 155 | // get calling thread local pointer |
---|
| 156 | thread_t * this = CURRENT_THREAD; |
---|
| 157 | |
---|
| 158 | // store command arguments in thread descriptor |
---|
[3] | 159 | this->command.mmc.dev_xp = chdev_dir.mmc[cxy]; |
---|
| 160 | this->command.mmc.type = MMC_SET_ERROR; |
---|
| 161 | this->command.mmc.reg_index = index; |
---|
| 162 | this->command.mmc.reg_ptr = &wdata; |
---|
[1] | 163 | |
---|
| 164 | // execute operation |
---|
| 165 | dev_mmc_access( this ); |
---|
| 166 | |
---|
| 167 | // return operation status |
---|
[3] | 168 | return this->command.mmc.error; |
---|
[1] | 169 | } |
---|
| 170 | |
---|
| 171 | ////////////////////////////////////////// |
---|
| 172 | error_t dev_mmc_get_error( cxy_t cxy, |
---|
| 173 | uint32_t index, |
---|
| 174 | uint32_t * rdata ) |
---|
| 175 | { |
---|
| 176 | // get calling thread local pointer |
---|
| 177 | thread_t * this = CURRENT_THREAD; |
---|
| 178 | |
---|
| 179 | // store command arguments in thread descriptor |
---|
[3] | 180 | this->command.mmc.dev_xp = chdev_dir.mmc[cxy]; |
---|
| 181 | this->command.mmc.type = MMC_GET_ERROR; |
---|
| 182 | this->command.mmc.reg_index = index; |
---|
| 183 | this->command.mmc.reg_ptr = rdata; |
---|
[1] | 184 | |
---|
| 185 | // execute operation |
---|
| 186 | dev_mmc_access( this ); |
---|
| 187 | |
---|
| 188 | // return operation status |
---|
[3] | 189 | return this->command.mmc.error; |
---|
[1] | 190 | } |
---|
| 191 | |
---|
| 192 | //////////////////////////////////////////////////// |
---|
| 193 | error_t dev_mmc_get_instrumentation( cxy_t cxy, |
---|
| 194 | uint32_t index, |
---|
| 195 | uint32_t * rdata ) |
---|
| 196 | { |
---|
| 197 | // get calling thread local pointer |
---|
| 198 | thread_t * this = CURRENT_THREAD; |
---|
| 199 | |
---|
| 200 | // store command arguments in thread descriptor |
---|
[3] | 201 | this->command.mmc.dev_xp = chdev_dir.mmc[cxy]; |
---|
| 202 | this->command.mmc.type = MMC_GET_INSTRU; |
---|
| 203 | this->command.mmc.reg_index = index; |
---|
| 204 | this->command.mmc.reg_ptr = rdata; |
---|
[1] | 205 | |
---|
| 206 | // execute operation |
---|
| 207 | dev_mmc_access( this ); |
---|
| 208 | |
---|
| 209 | // return operation status |
---|
[3] | 210 | return this->command.mmc.error; |
---|
[1] | 211 | } |
---|
| 212 | |
---|