source: trunk/kernel/devices/dev_mmc.c @ 231

Last change on this file since 231 was 188, checked in by alain, 7 years ago

Redefine the PIC device API.

File size: 7.7 KB
RevLine 
[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]36extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c
[1]37
[3]38////////////////////////////////////
39void dev_mmc_init( chdev_t * chdev )
[1]40{
41    // get implementation from device descriptor
[3]42    uint32_t  impl = chdev->impl;
[1]43
[23]44    // set chdev name
45    snprintf( chdev->name , 16 , "mmc_%x" , local_cxy );
46
[3]47    // set driver specific fields in device descriptor and call driver init function
[1]48    if( impl == IMPL_MMC_TSR )
49    {
[3]50        chdev->cmd = &soclib_mmc_cmd;
51        chdev->isr = &soclib_mmc_isr;
52        soclib_mmc_init( chdev );
[1]53    }
54    else
55    {
[3]56        assert( false , __FUNCTION__ , "undefined MMC device implementation" );
[1]57    }
[14]58
[188]59    // bind IRQ to CP0
60    dev_pic_bind_irq( 0 , chdev );
61   
62    // enable IRQ
63    dev_pic_enable_irq( 0 , chdev );
[14]64
[1]65}  // end dev_mmc_init()
66
67/////////////////////////////////////////////////////////////////////////////
[23]68// This static function is called by all MMC device functions.
69// It makes some checking, takes the lock granting exclusive
70// access to MMC peripheral, call the driver to execute the command
[1]71// registered in the calling thread descriptor, and releases the lock.
72/////////////////////////////////////////////////////////////////////////////
[23]73static error_t dev_mmc_access( thread_t * this )
[1]74{
[23]75    // get extended pointer on MMC device descriptor
76    xptr_t  dev_xp = this->command.mmc.dev_xp;
[1]77
[23]78    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "target MMC device undefined" );
[1]79
80    // get MMC device cluster identifier & local pointer
[3]81    cxy_t     dev_cxy = GET_CXY( dev_xp );
82    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
[1]83
84    // get driver command function pointer from MMC device descriptor
85    dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->cmd ) );
86
87    // get the MMC device remote spinlock
88    remote_spinlock_lock( XPTR( dev_cxy , &dev_ptr->wait_lock ) ); 
89
90    // call driver command
91    cmd( XPTR( local_cxy , this ) );
92   
93    // release the MMC device remote spinlock
94    remote_spinlock_unlock( XPTR( dev_cxy , &dev_ptr->wait_lock ) ); 
95
[23]96    // return operation status
97    return this->command.mmc.error; 
[1]98
99}  // end dev_mmc_access()
100
[23]101/////////////////////////////////////////
102error_t dev_mmc_inval( xptr_t     buf_xp,
[1]103                       uint32_t   buf_size )
104{
[23]105    error_t error;
106
[1]107    // get calling thread local pointer
108    thread_t * this = CURRENT_THREAD;
109
[23]110    mmc_dmsg("\n[INFO] %s enters for thread %x in process %x / buf_xp = %l\n", 
111                 __FUNCTION__ , this->trdid , this->process->pid , buf_xp );
[1]112
[23]113    // get buffer cluster and local pointer
114    cxy_t  buf_cxy = GET_CXY( buf_xp );
115    void * buf_ptr = GET_PTR( buf_xp );
116   
117    assert( (((intptr_t)buf_ptr & (CONFIG_CACHE_LINE_SIZE -1)) == 0) , __FUNCTION__ ,
[3]118             "buffer not aligned on cache line" );
[1]119
[23]120    // get buffer physical address
121    paddr_t  buf_paddr;
122    error = vmm_v2p_translate( CONFIG_KERNEL_IDENTITY_MAP , buf_ptr , &buf_paddr ); 
[1]123
[23]124    assert( (error == 0) , __FUNCTION__ , "cannot get buffer paddr" );
125
[1]126    // store command arguments in thread descriptor
[23]127    this->command.mmc.dev_xp    = chdev_dir.mmc[buf_cxy];
[3]128    this->command.mmc.type      = MMC_CC_INVAL;
129    this->command.mmc.buf_paddr = buf_paddr;
130    this->command.mmc.buf_size  = buf_size;
[1]131
[23]132    // call MMC driver
133    error = dev_mmc_access( this );
[1]134
[23]135    mmc_dmsg("\n[INFO] %s completes for thread %x in process %x / error = %d\n", 
136             __FUNCTION__ , this->trdid , this->process->pid , error );
[1]137
[23]138    return error;
139}
140
141////////////////////////////////////////
142error_t dev_mmc_sync( xptr_t     buf_xp,
[1]143                      uint32_t   buf_size )
144{
[23]145    error_t error;
146
[1]147    // get calling thread local pointer
148    thread_t * this = CURRENT_THREAD;
149
[23]150    mmc_dmsg("\n[INFO] %s enters for thread %x in process %x / buf_xp = %l\n", 
151                 __FUNCTION__ , this->trdid , this->process->pid , buf_xp );
[1]152
[23]153    // get buffer cluster and local pointer
154    cxy_t  buf_cxy = GET_CXY( buf_xp );
155    void * buf_ptr = GET_PTR( buf_xp );
156   
157    assert( (((intptr_t)buf_ptr & (CONFIG_CACHE_LINE_SIZE -1)) == 0) , __FUNCTION__ ,
[3]158             "buffer not aligned on cache line" );
[1]159
[23]160    // get  buffer physical address
161    paddr_t  buf_paddr;
162    error = vmm_v2p_translate( CONFIG_KERNEL_IDENTITY_MAP , buf_ptr , &buf_paddr ); 
163
164    assert( (error == 0) , __FUNCTION__ , "cannot get buffer paddr" );
165
[1]166    // store command arguments in thread descriptor
[23]167    this->command.mmc.dev_xp    = chdev_dir.mmc[buf_cxy];
[3]168    this->command.mmc.type      = MMC_CC_SYNC;
169    this->command.mmc.buf_paddr = buf_paddr;
170    this->command.mmc.buf_size  = buf_size;
[1]171
[23]172    // call MMC driver
173    error = dev_mmc_access( this );
[1]174
[23]175    mmc_dmsg("\n[INFO] %s completes for thread %x in process %x / error = %d\n", 
176                 __FUNCTION__ , this->trdid , this->process->pid , error );
[1]177
[23]178    return error;
179}
180
[1]181/////////////////////////////////////////
182error_t dev_mmc_set_error( cxy_t     cxy,
183                           uint32_t  index,
184                           uint32_t  wdata )
185{
186    // get calling thread local pointer
187    thread_t * this = CURRENT_THREAD;
188
189    // store command arguments in thread descriptor
[3]190    this->command.mmc.dev_xp    = chdev_dir.mmc[cxy];
191    this->command.mmc.type      = MMC_SET_ERROR;
192    this->command.mmc.reg_index = index;
193    this->command.mmc.reg_ptr   = &wdata;
[1]194
195    // execute operation
[23]196    return dev_mmc_access( this ); 
[1]197}
198                       
199//////////////////////////////////////////
200error_t dev_mmc_get_error( cxy_t      cxy,
201                           uint32_t   index,
202                           uint32_t * rdata )
203{
204    // get calling thread local pointer
205    thread_t * this = CURRENT_THREAD;
206
207    // store command arguments in thread descriptor
[3]208    this->command.mmc.dev_xp    = chdev_dir.mmc[cxy];
209    this->command.mmc.type      = MMC_GET_ERROR;
210    this->command.mmc.reg_index = index;
211    this->command.mmc.reg_ptr   = rdata;
[1]212
213    // execute operation
[23]214    return dev_mmc_access( this ); 
[1]215}
216                       
217////////////////////////////////////////////////////
218error_t dev_mmc_get_instrumentation( cxy_t      cxy,
219                                     uint32_t   index,
220                                     uint32_t * rdata )
221{
222    // get calling thread local pointer
223    thread_t * this = CURRENT_THREAD;
224
225    // store command arguments in thread descriptor
[3]226    this->command.mmc.dev_xp    = chdev_dir.mmc[cxy];
227    this->command.mmc.type      = MMC_GET_INSTRU;
228    this->command.mmc.reg_index = index;
229    this->command.mmc.reg_ptr   = rdata;
[1]230
231    // execute operation
[23]232    return dev_mmc_access( this ); 
[1]233}
234
Note: See TracBrowser for help on using the repository browser.