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

Last change on this file since 21 was 14, checked in by alain, 7 years ago

Bugs fix.

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