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

Last change on this file since 1 was 1, checked in by alain, 5 years ago

First import

File size: 7.7 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 <thread.h>
29#include <dev_mmc.h>
30
31/////////////////////////////////////////////////////////////////////////////////////////
32// Extern global variables
33/////////////////////////////////////////////////////////////////////////////////////////
34
35extern devices_directory_t  devices_dir;         // allocated in kernel_init.c
36
37extern devices_input_irq_t  devices_input_irq;   // allocated in kernel_init.c
38
39//////////////////////////////////
40void dev_mmc_init( xptr_t dev_xp )
41{
42    // get MMC device descriptor cluster and local pointer
43    cxy_t      dev_cxy = GET_CXY( dev_xp );
44    device_t * dev_ptr = (device_t *)GET_PTR( dev_xp );
45
46    // get implementation from device descriptor
47    uint32_t  impl = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->impl ) );
48
49    // set driver specific fields in device descriptor
50    // and call driver init function
51    if( impl == IMPL_MMC_TSR )
52    {
53        hal_remote_spt( XPTR( dev_cxy , &dev_ptr->cmd ) , &soclib_mmc_command );
54        hal_remote_spt( XPTR( dev_cxy , &dev_ptr->isr ) , &soclib_mmc_isr );
55        hal_remote_memcpy( XPTR( dev_cxy , &dev_ptr->name ),
56                           XPTR( local_cxy , "MMC_TSR" ) , 16 );
57        soclib_mmc_init( dev_xp );
58    }
59    else
60    {
61        printk("\n[PANIC] in %s: undefined MMC device implementation\n", __FUNCTION__ );
62        hal_core_sleep();
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->dev.mmc.type );
75
76    // get extended pointer on MMC device
77    xptr_t  dev_xp  = this->dev.mmc.dev_xp;
78
79    if ( dev_xp == XPTR_NULL )
80    {
81        printk("\n[PANIC] in %s : undefined MMC device descriptor "
82               "in cluster %x for thread %x in process %x\n",
83               __FUNCTION__ , local_cxy , this->trdid , this->process->pid );
84        hal_core_sleep();
85    }
86
87    // get MMC device cluster identifier & local pointer
88    cxy_t      dev_cxy = GET_CXY( dev_xp );
89    device_t * dev_ptr = (device_t *)GET_PTR( dev_xp );
90
91    // get driver command function pointer from MMC device descriptor
92    dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->cmd ) );
93
94    // get the MMC device remote spinlock
95    remote_spinlock_lock( XPTR( dev_cxy , &dev_ptr->wait_lock ) ); 
96
97    // call driver command
98    cmd( XPTR( local_cxy , this ) );
99   
100    // release the MMC device remote spinlock
101    remote_spinlock_unlock( XPTR( dev_cxy , &dev_ptr->wait_lock ) ); 
102
103    mmc_dmsg("\n[INFO] %s completes for thread %x in process %x / error = %d\n", 
104                 __FUNCTION__ , this->trdid , this->process->pid , this->dev.ioc.error );
105
106}  // end dev_mmc_access()
107
108////////////////////////////////////////////
109error_t dev_mmc_inval( paddr_t    buf_paddr,
110                       uint32_t   buf_size )
111{
112    // get calling thread local pointer
113    thread_t * this = CURRENT_THREAD;
114
115    // get target cluster from paddr
116    cxy_t cxy = CXY_FROM_PADDR( buf_paddr );
117
118    if ( buf_paddr & (CONFIG_CACHE_LINE_SIZE -1) )
119    {
120        printk("\n[PANIC] in %s : buffer not aligned on cache line "
121               "for thread %x / target cluster %x\n",
122               __FUNCTION__ , this->trdid , cxy );
123        hal_core_sleep();
124    }
125
126    // get extended pointer on MMC device descriptor
127    xptr_t  dev_xp = devices_dir.mmc[cxy];
128
129    // store command arguments in thread descriptor
130    this->dev.mmc.dev_xp    = dev_xp;
131    this->dev.mmc.type      = MMC_CC_INVAL;
132    this->dev.mmc.buf_paddr = buf_paddr;
133    this->dev.mmc.buf_size  = buf_size;
134
135    // execute operation
136    dev_mmc_access( this ); 
137
138    // return operation status
139    return this->dev.mmc.error; 
140} 
141
142/////////////////////////////////////
143error_t dev_mmc_sync( paddr_t    buf_paddr,
144                      uint32_t   buf_size )
145{
146    // get calling thread local pointer
147    thread_t * this = CURRENT_THREAD;
148
149    // get target cluster from paddr
150    cxy_t cxy = CXY_FROM_PADDR( buf_paddr );
151
152    if ( buf_paddr & (CONFIG_CACHE_LINE_SIZE -1) )
153    {
154        printk("\n[PANIC] in %s : buffer not aligned on cache line "
155               "for thread %x / target cluster %x\n",
156               __FUNCTION__ , this->trdid , cxy );
157        hal_core_sleep();
158    }
159
160    // store command arguments in thread descriptor
161    this->dev.mmc.dev_xp    = devices_dir.mmc[cxy];
162    this->dev.mmc.type      = MMC_CC_SYNC;
163    this->dev.mmc.buf_paddr = buf_paddr;
164    this->dev.mmc.buf_size  = buf_size;
165
166    // execute operation
167    dev_mmc_access( this ); 
168
169    // return operation status
170    return this->dev.mmc.error; 
171} 
172
173/////////////////////////////////////////
174error_t dev_mmc_set_error( cxy_t     cxy,
175                           uint32_t  index,
176                           uint32_t  wdata )
177{
178    // get calling thread local pointer
179    thread_t * this = CURRENT_THREAD;
180
181    // store command arguments in thread descriptor
182    this->dev.mmc.dev_xp    = devices_dir.mmc[cxy];
183    this->dev.mmc.type      = MMC_SET_ERROR;
184    this->dev.mmc.reg_index = index;
185    this->dev.mmc.reg_ptr   = &wdata;
186
187    // execute operation
188    dev_mmc_access( this ); 
189
190    // return operation status
191    return this->dev.mmc.error; 
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->dev.mmc.dev_xp    = devices_dir.mmc[cxy];
204    this->dev.mmc.type      = MMC_GET_ERROR;
205    this->dev.mmc.reg_index = index;
206    this->dev.mmc.reg_ptr   = rdata;
207
208    // execute operation
209    dev_mmc_access( this ); 
210
211    // return operation status
212    return this->dev.mmc.error; 
213}
214                       
215////////////////////////////////////////////////////
216error_t dev_mmc_get_instrumentation( cxy_t      cxy,
217                                     uint32_t   index,
218                                     uint32_t * rdata )
219{
220    // get calling thread local pointer
221    thread_t * this = CURRENT_THREAD;
222
223    // store command arguments in thread descriptor
224    this->dev.mmc.dev_xp    = devices_dir.mmc[cxy];
225    this->dev.mmc.type      = MMC_GET_INSTRU;
226    this->dev.mmc.reg_index = index;
227    this->dev.mmc.reg_ptr   = rdata;
228
229    // execute operation
230    dev_mmc_access( this ); 
231
232    // return operation status
233    return this->dev.mmc.error; 
234}
235
Note: See TracBrowser for help on using the repository browser.