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

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

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

File size: 7.2 KB
Line 
1/*
2 * dev_mmc.c - MMC (Memory Cache Controler) generic device API implementation.
3 *
4 * Author  Alain Greiner    (2016,2017,2018)
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
37extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c
38
39//////////////////////////////////
40void dev_mmc_init( chdev_t * mmc )
41{
42    // get implementation from device descriptor
43    uint32_t  impl = mmc->impl;
44
45    // set mmc name
46    snprintf( mmc->name , 16 , "mmc_%x" , local_cxy );
47
48    // call driver init function
49    hal_drivers_mmc_init( mmc , impl );
50
51    // bind IRQ to CP0
52    dev_pic_bind_irq( 0 , mmc );
53   
54    // enable IRQ
55    dev_pic_enable_irq( 0 , XPTR( local_cxy , mmc ) );
56
57}  // end dev_mmc_init()
58
59/////////////////////////////////////////////////////////////////////////////
60// This static function is called by all MMC device access functions.
61// It makes some checking, takes the lock granting exclusive
62// access to MMC peripheral, call the driver to execute the command
63// registered in the calling thread descriptor, and releases the lock.
64/////////////////////////////////////////////////////////////////////////////
65static error_t dev_mmc_access( thread_t * this )
66{
67    // get extended pointer on MMC device descriptor
68    xptr_t  dev_xp = this->mmc_cmd.dev_xp;
69
70    assert( (dev_xp != XPTR_NULL) , "target MMC device undefined" );
71
72    // get MMC device cluster identifier & local pointer
73    cxy_t     dev_cxy = GET_CXY( dev_xp );
74    chdev_t * dev_ptr = GET_PTR( dev_xp );
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
79    // get the MMC device remote busylock
80    remote_busylock_acquire( XPTR( dev_cxy , &dev_ptr->wait_lock ) ); 
81
82    // call driver command
83    cmd( XPTR( local_cxy , this ) );
84   
85    // release the MMC device remote busylock
86    remote_busylock_release( XPTR( dev_cxy , &dev_ptr->wait_lock ) ); 
87
88    // return operation status
89    return this->mmc_cmd.error; 
90
91}  // end dev_mmc_access()
92
93/////////////////////////////////////////
94error_t dev_mmc_inval( xptr_t     buf_xp,
95                       uint32_t   buf_size )
96{
97    error_t error;
98
99    // get calling thread local pointer
100    thread_t  * this    = CURRENT_THREAD;
101
102#if DEBUG_DEV_MMC
103uint32_t cycle = (uint32_t)hal_get_cycles();
104if( DEBUG_DEV_MMC < cycle )
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) );
107#endif
108
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   
113    assert( (((intptr_t)buf_ptr & (CONFIG_CACHE_LINE_SIZE -1)) == 0) ,
114             "buffer not aligned on cache line" );
115
116    // store command arguments in thread descriptor
117    this->mmc_cmd.dev_xp    = chdev_dir.mmc[buf_cxy];
118    this->mmc_cmd.type      = MMC_CC_INVAL;
119    this->mmc_cmd.buf_ptr   = buf_ptr;
120    this->mmc_cmd.buf_size  = buf_size;
121
122    // call MMC driver
123    error = dev_mmc_access( this );
124
125#if DEBUG_DEV_MMC
126cycle = (uint32_t)hal_get_cycles();
127if( DEBUG_DEV_MMC < cycle )
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) );
130#endif
131
132    return error;
133}
134
135////////////////////////////////////////
136error_t dev_mmc_sync( xptr_t     buf_xp,
137                      uint32_t   buf_size )
138{
139    error_t error;
140
141    thread_t  * this    = CURRENT_THREAD;
142
143#if DEBUG_DEV_MMC
144uint32_t cycle = (uint32_t)hal_get_cycles();
145if( DEBUG_DEV_MMC < cycle )
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) );
148#endif
149
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   
154    assert( (((intptr_t)buf_ptr & (CONFIG_CACHE_LINE_SIZE -1)) == 0) ,
155             "buffer not aligned on cache line" );
156
157    // store command arguments in thread descriptor
158    this->mmc_cmd.dev_xp    = chdev_dir.mmc[buf_cxy];
159    this->mmc_cmd.type      = MMC_CC_SYNC;
160    this->mmc_cmd.buf_ptr   = buf_ptr;
161    this->mmc_cmd.buf_size  = buf_size;
162
163    // call MMC driver
164    error = dev_mmc_access( this );
165
166#if DEBUG_DEV_MMC
167cycle = (uint32_t)hal_get_cycles();
168if( DEBUG_DEV_MMC < cycle )
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) );
171#endif
172
173    return error;
174}
175
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
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;
189
190    // execute operation
191    return dev_mmc_access( this ); 
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
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;
207
208    // execute operation
209    return dev_mmc_access( this ); 
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
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;
225
226    // execute operation
227    return dev_mmc_access( this ); 
228}
229
Note: See TracBrowser for help on using the repository browser.