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

Last change on this file since 613 was 605, checked in by alain, 6 years ago

Improve the FAT32 file system to support cat, rm, cp commands.

File size: 7.2 KB
RevLine 
[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]37extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c
[1]38
[238]39//////////////////////////////////
40void 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]65static 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/////////////////////////////////////////
94error_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]103uint32_t cycle = (uint32_t)hal_get_cycles();
[438]104if( DEBUG_DEV_MMC < cycle )
[605]105printk("\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
110    cxy_t  buf_cxy = GET_CXY( buf_xp );
111    void * buf_ptr = GET_PTR( buf_xp );
112   
[492]113    assert( (((intptr_t)buf_ptr & (CONFIG_CACHE_LINE_SIZE -1)) == 0) ,
[3]114             "buffer not aligned on cache line" );
[1]115
116    // store command arguments in thread descriptor
[279]117    this->mmc_cmd.dev_xp    = chdev_dir.mmc[buf_cxy];
118    this->mmc_cmd.type      = MMC_CC_INVAL;
[440]119    this->mmc_cmd.buf_ptr   = buf_ptr;
[279]120    this->mmc_cmd.buf_size  = buf_size;
[1]121
[23]122    // call MMC driver
123    error = dev_mmc_access( this );
[1]124
[438]125#if DEBUG_DEV_MMC
[437]126cycle = (uint32_t)hal_get_cycles();
[438]127if( DEBUG_DEV_MMC < cycle )
[605]128printk("\n[%s] thread[%x,%x] exit / cluster %x / buffer %x\n", 
129__FUNCTION__, this->process->pid, this->trdid, GET_CXY(buf_xp), GET_PTR(buf_xp) );
[437]130#endif
[1]131
[23]132    return error;
133}
134
135////////////////////////////////////////
136error_t dev_mmc_sync( xptr_t     buf_xp,
[1]137                      uint32_t   buf_size )
138{
[23]139    error_t error;
140
[440]141    thread_t  * this    = CURRENT_THREAD;
[1]142
[438]143#if DEBUG_DEV_MMC
[437]144uint32_t cycle = (uint32_t)hal_get_cycles();
[438]145if( DEBUG_DEV_MMC < cycle )
[605]146printk("\n[%s] thread[%x,%x] enters / cluster %x / buffer %x\n", 
147__FUNCTION__, this->process->pid, this->trdid, GET_CXY(buf_xp), GET_PTR(buf_xp) );
[437]148#endif
[1]149
[23]150    // get buffer cluster and local pointer
151    cxy_t  buf_cxy = GET_CXY( buf_xp );
152    void * buf_ptr = GET_PTR( buf_xp );
153   
[492]154    assert( (((intptr_t)buf_ptr & (CONFIG_CACHE_LINE_SIZE -1)) == 0) ,
[3]155             "buffer not aligned on cache line" );
[1]156
157    // store command arguments in thread descriptor
[279]158    this->mmc_cmd.dev_xp    = chdev_dir.mmc[buf_cxy];
159    this->mmc_cmd.type      = MMC_CC_SYNC;
[440]160    this->mmc_cmd.buf_ptr   = buf_ptr;
[279]161    this->mmc_cmd.buf_size  = buf_size;
[1]162
[23]163    // call MMC driver
164    error = dev_mmc_access( this );
[1]165
[438]166#if DEBUG_DEV_MMC
[437]167cycle = (uint32_t)hal_get_cycles();
[438]168if( DEBUG_DEV_MMC < cycle )
[605]169printk("\n[%s] thread[%x,%x] exit / cluster %x / buffer %x\n", 
170__FUNCTION__, this->process->pid, this->trdid, GET_CXY(buf_xp), GET_PTR(buf_xp) );
[437]171#endif
[1]172
[23]173    return error;
174}
175
[1]176/////////////////////////////////////////
177error_t dev_mmc_set_error( cxy_t     cxy,
178                           uint32_t  index,
179                           uint32_t  wdata )
180{
181    // get calling thread local pointer
182    thread_t * this = CURRENT_THREAD;
183
184    // store command arguments in thread descriptor
[279]185    this->mmc_cmd.dev_xp    = chdev_dir.mmc[cxy];
186    this->mmc_cmd.type      = MMC_SET_ERROR;
187    this->mmc_cmd.reg_index = index;
188    this->mmc_cmd.reg_ptr   = &wdata;
[1]189
190    // execute operation
[23]191    return dev_mmc_access( this ); 
[1]192}
193                       
194//////////////////////////////////////////
195error_t dev_mmc_get_error( cxy_t      cxy,
196                           uint32_t   index,
197                           uint32_t * rdata )
198{
199    // get calling thread local pointer
200    thread_t * this = CURRENT_THREAD;
201
202    // store command arguments in thread descriptor
[279]203    this->mmc_cmd.dev_xp    = chdev_dir.mmc[cxy];
204    this->mmc_cmd.type      = MMC_GET_ERROR;
205    this->mmc_cmd.reg_index = index;
206    this->mmc_cmd.reg_ptr   = rdata;
[1]207
208    // execute operation
[23]209    return dev_mmc_access( this ); 
[1]210}
211                       
212////////////////////////////////////////////////////
213error_t dev_mmc_get_instrumentation( cxy_t      cxy,
214                                     uint32_t   index,
215                                     uint32_t * rdata )
216{
217    // get calling thread local pointer
218    thread_t * this = CURRENT_THREAD;
219
220    // store command arguments in thread descriptor
[279]221    this->mmc_cmd.dev_xp    = chdev_dir.mmc[cxy];
222    this->mmc_cmd.type      = MMC_GET_INSTRU;
223    this->mmc_cmd.reg_index = index;
224    this->mmc_cmd.reg_ptr   = rdata;
[1]225
226    // execute operation
[23]227    return dev_mmc_access( this ); 
[1]228}
229
Note: See TracBrowser for help on using the repository browser.