Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
54 theseven 1
/***************************************************************************
2
 *             __________               __   ___.
3
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
4
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
5
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
6
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
7
 *                     \/            \/     \/    \/            \/
8
 * $Id$
9
 *
10
 * Copyright (C) 2008 by Frank Gevaerts
11
 *
12
 * This program is free software; you can redistribute it and/or
13
 * modify it under the terms of the GNU General Public License
14
 * as published by the Free Software Foundation; either version 2
15
 * of the License, or (at your option) any later version.
16
 *
17
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18
 * KIND, either express or implied.
19
 *
20
 ****************************************************************************/
21
#include "global.h"
22
#include "storage.h"
23
 
24
 
25
#define DRIVER_MASK     0xff000000
26
#define DRIVER_OFFSET   24
27
#define DRIVE_MASK      0x00ff0000
28
#define DRIVE_OFFSET    16
29
#define PARTITION_MASK  0x0000ff00
30
 
31
static unsigned int storage_drivers[NUM_DRIVES];
32
static unsigned int num_drives;
33
 
34
 
35
int storage_read_sectors(IF_MD2(int drive,) unsigned long start, int count,
36
                         void* buf)
37
{
38
#ifdef CONFIG_STORAGE_MULTI
39
    int driver=(storage_drivers[drive] & DRIVER_MASK)>>DRIVER_OFFSET;
40
    int ldrive=(storage_drivers[drive] & DRIVE_MASK)>>DRIVE_OFFSET;
41
 
42
    switch (driver)
43
    {
44
#if (CONFIG_STORAGE & STORAGE_ATA)
45
    case STORAGE_ATA:
46
        return ata_read_sectors(IF_MD2(ldrive,) start,count,buf);
47
#endif
48
 
49
#if (CONFIG_STORAGE & STORAGE_MMC)
50
    case STORAGE_MMC:
51
        return mmc_read_sectors(IF_MD2(ldrive,) start,count,buf);
52
#endif
53
 
54
#if (CONFIG_STORAGE & STORAGE_SD)
55
    case STORAGE_SD:
56
        return sd_read_sectors(IF_MD2(ldrive,) start,count,buf);
57
#endif
58
 
59
#if (CONFIG_STORAGE & STORAGE_NAND)
60
    case STORAGE_NAND:
61
        return nand_read_sectors(IF_MD2(ldrive,) start,count,buf);
62
#endif
63
 
64
#if (CONFIG_STORAGE & STORAGE_RAMDISK)
65
    case STORAGE_RAMDISK:
66
        return ramdisk_read_sectors(IF_MD2(ldrive,) start,count,buf);
67
#endif
68
    }
69
 
70
    return -1;
71
#else /* CONFIG_STORAGE_MULTI */
72
    return STORAGE_FUNCTION(read_sectors)(IF_MD2(drive,)start,count,buf);
73
#endif /* CONFIG_STORAGE_MULTI */
74
 
75
}
76
 
116 theseven 77
int storage_read_sectors_md(int drive, unsigned long start, int count, void* buf)
78
{
79
    return storage_read_sectors(IF_MD2(drive,) start, count, buf);
80
}
81
 
54 theseven 82
int storage_write_sectors(IF_MD2(int drive,) unsigned long start, int count,
83
                          const void* buf)
84
{
85
#ifdef CONFIG_STORAGE_MULTI
86
    int driver=(storage_drivers[drive] & DRIVER_MASK)>>DRIVER_OFFSET;
87
    int ldrive=(storage_drivers[drive] & DRIVE_MASK)>>DRIVE_OFFSET;
88
 
89
    switch (driver)
90
    {
91
#if (CONFIG_STORAGE & STORAGE_ATA)
92
    case STORAGE_ATA:
93
        return ata_write_sectors(IF_MD2(ldrive,)start,count,buf);
94
#endif
95
 
96
#if (CONFIG_STORAGE & STORAGE_MMC)
97
    case STORAGE_MMC:
98
        return mmc_write_sectors(IF_MD2(ldrive,)start,count,buf);
99
#endif
100
 
101
#if (CONFIG_STORAGE & STORAGE_SD)
102
    case STORAGE_SD:
103
        return sd_write_sectors(IF_MD2(ldrive,)start,count,buf);
104
#endif
105
 
106
#if (CONFIG_STORAGE & STORAGE_NAND)
107
    case STORAGE_NAND:
108
        return nand_write_sectors(IF_MD2(ldrive,)start,count,buf);
109
#endif
110
 
111
#if (CONFIG_STORAGE & STORAGE_RAMDISK)
112
    case STORAGE_RAMDISK:
113
        return ramdisk_write_sectors(IF_MD2(ldrive,)start,count,buf);
114
#endif
115
    }
116
 
117
    return -1;
118
#else /* CONFIG_STORAGE_MULTI */
119
    return STORAGE_FUNCTION(write_sectors)(IF_MD2(drive,)start,count,buf);
120
#endif /* CONFIG_STORAGE_MULTI */
121
}
122
 
116 theseven 123
int storage_write_sectors_md(int drive, unsigned long start, int count, const void* buf)
124
{
125
    return storage_write_sectors(IF_MD2(drive,) start, count, buf);
126
}
127
 
153 theseven 128
#ifdef STORAGE_GET_INFO
129
void storage_get_info(int drive, struct storage_info *info)
130
{
54 theseven 131
#ifdef CONFIG_STORAGE_MULTI
153 theseven 132
    int driver=(storage_drivers[drive] & DRIVER_MASK)>>DRIVER_OFFSET;
133
    int ldrive=(storage_drivers[drive] & DRIVE_MASK)>>DRIVE_OFFSET;
134
 
135
    switch(driver)
136
    {
137
#if (CONFIG_STORAGE & STORAGE_ATA)
138
    case STORAGE_ATA:
139
        return ata_get_info(ldrive,info);
140
#endif
54 theseven 141
 
153 theseven 142
#if (CONFIG_STORAGE & STORAGE_MMC)
143
    case STORAGE_MMC:
144
        return mmc_get_info(ldrive,info);
145
#endif
146
 
147
#if (CONFIG_STORAGE & STORAGE_SD)
148
    case STORAGE_SD:
149
        return sd_get_info(ldrive,info);
150
#endif
151
 
152
#if (CONFIG_STORAGE & STORAGE_NAND)
153
    case STORAGE_NAND:
154
        return nand_get_info(ldrive,info);
155
#endif
156
 
157
#if (CONFIG_STORAGE & STORAGE_RAMDISK)
158
    case STORAGE_RAMDISK:
159
        return ramdisk_get_info(ldrive,info);
160
#endif
161
    }
162
#else /* CONFIG_STORAGE_MULTI */
163
    return STORAGE_FUNCTION(get_info)(IF_MD2(drive,)info);
164
#endif /* CONFIG_STORAGE_MULTI */
165
}
166
#endif /* STORAGE_GET_INFO */
167
 
54 theseven 168
int storage_num_drives(void)
169
{
170
    return num_drives;
171
}
172
 
173
int storage_init(void)
174
{
175
    int rc=0;
176
    int i;
177
    num_drives=0;
178
 
179
#if (CONFIG_STORAGE & STORAGE_ATA)
180
    if ((rc=ata_init())) return rc;
181
 
182
    int ata_drives = ata_num_drives(num_drives);
183
    for (i=0; i<ata_drives; i++)
184
    {
185
        storage_drivers[num_drives++] = 
186
            (STORAGE_ATA<<DRIVER_OFFSET) | (i << DRIVE_OFFSET);
187
    }
188
#endif
189
 
190
#if (CONFIG_STORAGE & STORAGE_MMC)
191
    if ((rc=mmc_init())) return rc;
192
 
193
    int mmc_drives = mmc_num_drives(num_drives);
194
    for (i=0; i<mmc_drives ;i++)
195
    {
196
        storage_drivers[num_drives++] =
197
            (STORAGE_MMC<<DRIVER_OFFSET) | (i << DRIVE_OFFSET);
198
    }
199
#endif
200
 
201
#if (CONFIG_STORAGE & STORAGE_SD)
202
    if ((rc=sd_init())) return rc;
203
 
204
    int sd_drives = sd_num_drives(num_drives);
205
    for (i=0; i<sd_drives; i++)
206
    {
207
        storage_drivers[num_drives++] =
208
            (STORAGE_SD<<DRIVER_OFFSET) | (i << DRIVE_OFFSET);
209
    }
210
#endif
211
 
212
#if (CONFIG_STORAGE & STORAGE_NAND)
213
    if ((rc=nand_init())) return rc;
214
 
215
    int nand_drives = nand_num_drives(num_drives);
216
    for (i=0; i<nand_drives; i++)
217
    {
218
        storage_drivers[num_drives++] =
219
            (STORAGE_NAND<<DRIVER_OFFSET) | (i << DRIVE_OFFSET);
220
    }
221
#endif
222
 
223
#if (CONFIG_STORAGE & STORAGE_RAMDISK)
224
    if ((rc=ramdisk_init())) return rc;
225
 
226
    int ramdisk_drives = ramdisk_num_drives(num_drives);
227
    for (i=0; i<ramdisk_drives; i++)
228
    {
229
        storage_drivers[num_drives++] =
230
            (STORAGE_RAMDISK<<DRIVER_OFFSET) | (i << DRIVE_OFFSET);
231
    }
232
#endif
233
 
234
    return 0;
235
}
236
 
237
 
238
void storage_enable(bool on)
239
{
240
#if (CONFIG_STORAGE & STORAGE_ATA)
241
    ata_enable(on);
242
#endif
243
 
244
#if (CONFIG_STORAGE & STORAGE_MMC)
245
    mmc_enable(on);
246
#endif
247
 
248
#if (CONFIG_STORAGE & STORAGE_SD)
249
    sd_enable(on);
250
#endif
251
 
252
#if (CONFIG_STORAGE & STORAGE_NAND)
253
    nand_enable(on);
254
#endif
255
 
256
#if (CONFIG_STORAGE & STORAGE_RAMDISK)
257
    ramdisk_enable(on);
258
#endif
259
}
260
 
261
void storage_sleep(void)
262
{
263
#if (CONFIG_STORAGE & STORAGE_ATA)
264
    ata_sleep();
265
#endif
266
 
267
#if (CONFIG_STORAGE & STORAGE_MMC)
268
    mmc_sleep();
269
#endif
270
 
271
#if (CONFIG_STORAGE & STORAGE_SD)
272
    sd_sleep();
273
#endif
274
 
275
#if (CONFIG_STORAGE & STORAGE_NAND)
276
    nand_sleep();
277
#endif
278
 
279
#if (CONFIG_STORAGE & STORAGE_RAMDISK)
280
    ramdisk_sleep();
281
#endif
282
}
283
 
284
void storage_sleepnow(void)
285
{
286
#if (CONFIG_STORAGE & STORAGE_ATA)
287
    ata_sleepnow();
288
#endif
289
 
290
#if (CONFIG_STORAGE & STORAGE_MMC)
291
    mmc_sleepnow();
292
#endif
293
 
294
#if (CONFIG_STORAGE & STORAGE_SD)
295
    //sd_sleepnow();
296
#endif
297
 
298
#if (CONFIG_STORAGE & STORAGE_NAND)
299
    nand_sleepnow();
300
#endif
301
 
302
#if (CONFIG_STORAGE & STORAGE_RAMDISK)
303
    ramdisk_sleepnow();
304
#endif
305
}
306
 
307
bool storage_disk_is_active(void)
308
{
309
#if (CONFIG_STORAGE & STORAGE_ATA)
310
    if (ata_disk_is_active()) return true;
311
#endif
312
 
313
#if (CONFIG_STORAGE & STORAGE_MMC)
314
    if (mmc_disk_is_active()) return true;
315
#endif
316
 
317
#if (CONFIG_STORAGE & STORAGE_SD)
318
    //if (sd_disk_is_active()) return true;
319
#endif
320
 
321
#if (CONFIG_STORAGE & STORAGE_NAND)
322
    //if (nand_disk_is_active()) return true;
323
#endif
324
 
325
#if (CONFIG_STORAGE & STORAGE_RAMDISK)
326
    if (ramdisk_disk_is_active()) return true;
327
#endif
328
 
329
    return false;
330
}
331
 
332
int storage_soft_reset(void)
333
{
334
    int rc=0;
335
 
336
#if (CONFIG_STORAGE & STORAGE_ATA)
965 theseven 337
    if ((rc=ata_reset())) return rc;
54 theseven 338
#endif
339
 
340
#if (CONFIG_STORAGE & STORAGE_MMC)
341
    if ((rc=mmc_soft_reset())) return rc;
342
#endif
343
 
344
#if (CONFIG_STORAGE & STORAGE_SD)
345
    //if ((rc=sd_soft_reset())) return rc;
346
#endif
347
 
348
#if (CONFIG_STORAGE & STORAGE_NAND)
349
    //if ((rc=nand_soft_reset())) return rc;
350
#endif
351
 
352
#if (CONFIG_STORAGE & STORAGE_RAMDISK)
353
    if ((rc=ramdisk_soft_reset())) return rc;
354
#endif
355
 
356
    return rc;
357
}
358
 
359
#ifdef HAVE_STORAGE_FLUSH
360
int storage_flush(void)
361
{
362
    int rc=0;
363
 
364
#if (CONFIG_STORAGE & STORAGE_ATA)
365
    //if ((rc=ata_flush())) return rc;
366
#endif
367
 
368
#if (CONFIG_STORAGE & STORAGE_MMC)
369
    //if ((rc=mmc_flush())) return rc;
370
#endif
371
 
372
#if (CONFIG_STORAGE & STORAGE_SD)
373
    //if ((rc=sd_flush())) return rc;
374
#endif
375
 
376
#if (CONFIG_STORAGE & STORAGE_NAND)
377
    if ((rc=nand_flush())) return rc;
378
#endif
379
 
380
#if (CONFIG_STORAGE & STORAGE_RAMDISK)
381
    //if ((rc=ramdisk_flush())) return rc;
382
#endif
383
 
384
    return rc;
385
}
386
#endif
387
 
388
void storage_spin(void)
389
{
390
#if (CONFIG_STORAGE & STORAGE_ATA)
391
    ata_spin();
392
#endif
393
 
394
#if (CONFIG_STORAGE & STORAGE_MMC)
395
    mmc_spin();
396
#endif
397
 
398
#if (CONFIG_STORAGE & STORAGE_SD)
399
    sd_spin();
400
#endif
401
 
402
#if (CONFIG_STORAGE & STORAGE_NAND)
403
    nand_spin();
404
#endif
405
 
406
#if (CONFIG_STORAGE & STORAGE_RAMDISK)
407
    ramdisk_spin();
408
#endif
409
}
410
 
411
void storage_spindown(int seconds)
412
{
413
#if (CONFIG_STORAGE & STORAGE_ATA)
414
    ata_spindown(seconds);
415
#endif
416
 
417
#if (CONFIG_STORAGE & STORAGE_MMC)
418
    mmc_spindown(seconds);
419
#endif
420
 
421
#if (CONFIG_STORAGE & STORAGE_SD)
422
    sd_spindown(seconds);
423
#endif
424
 
425
#if (CONFIG_STORAGE & STORAGE_NAND)
426
    nand_spindown(seconds);
427
#endif
428
 
429
#if (CONFIG_STORAGE & STORAGE_RAMDISK)
430
    ramdisk_spindown(seconds);
431
#endif
432
}
433
 
434
#if (CONFIG_LED == LED_REAL)
435
void storage_set_led_enabled(bool enabled)
436
{
437
#if (CONFIG_STORAGE & STORAGE_ATA)
438
    ata_set_led_enabled(enabled);
439
#endif
440
 
441
#if (CONFIG_STORAGE & STORAGE_MMC)
442
    mmc_set_led_enabled(enabled);
443
#endif
444
 
445
#if (CONFIG_STORAGE & STORAGE_SD)
446
    sd_set_led_enabled(enabled);
447
#endif
448
 
449
#if (CONFIG_STORAGE & STORAGE_NAND)
450
    nand_set_led_enabled(enabled);
451
#endif
452
 
453
#if (CONFIG_STORAGE & STORAGE_RAMDISK)
454
    ramdisk_set_led_enabled(enabled);
455
#endif
456
}
457
#endif /* CONFIG_LED == LED_REAL */
458
 
459
long storage_last_disk_activity(void)
460
{
461
    long max=0;
462
    long t;
463
 
464
#if (CONFIG_STORAGE & STORAGE_ATA)
465
    t=ata_last_disk_activity();
466
    if (t>max) max=t;
467
#endif
468
 
469
#if (CONFIG_STORAGE & STORAGE_MMC)
470
    t=mmc_last_disk_activity();
471
    if (t>max) max=t;
472
#endif
473
 
474
#if (CONFIG_STORAGE & STORAGE_SD)
475
    t=sd_last_disk_activity();
476
    if (t>max) max=t;
477
#endif
478
 
479
#if (CONFIG_STORAGE & STORAGE_NAND)
480
    t=nand_last_disk_activity();
481
    if (t>max) max=t;
482
#endif
483
 
484
#if (CONFIG_STORAGE & STORAGE_RAMDISK)
485
    t=ramdisk_last_disk_activity();
486
    if (t>max) max=t;
487
#endif
488
 
489
    return max;
490
}
491
 
492
#ifdef HAVE_HOTSWAP
493
bool storage_removable(int drive)
494
{
495
    int driver=(storage_drivers[drive] & DRIVER_MASK)>>DRIVER_OFFSET;
496
    int ldrive=(storage_drivers[drive] & DRIVE_MASK)>>DRIVE_OFFSET;
497
 
498
    switch(driver)
499
    {
500
#if (CONFIG_STORAGE & STORAGE_ATA)
501
    case STORAGE_ATA:
502
        return ata_removable(ldrive);
503
#endif
504
 
505
#if (CONFIG_STORAGE & STORAGE_MMC)
506
    case STORAGE_MMC:
507
        return mmc_removable(ldrive);
508
#endif
509
 
510
#if (CONFIG_STORAGE & STORAGE_SD)
511
    case STORAGE_SD:
512
        return sd_removable(ldrive);
513
#endif
514
 
515
#if (CONFIG_STORAGE & STORAGE_NAND)
516
    case STORAGE_NAND:
517
        return false;
518
#endif
519
 
520
#if (CONFIG_STORAGE & STORAGE_RAMDISK)
521
    case STORAGE_RAMDISK:
522
        return false;
523
#endif
524
 
525
    default:
526
        return false;
527
    }
528
}
529
 
530
bool storage_present(int drive)
531
{
532
    int driver=(storage_drivers[drive] & DRIVER_MASK)>>DRIVER_OFFSET;
533
    int ldrive=(storage_drivers[drive] & DRIVE_MASK)>>DRIVE_OFFSET;
534
 
535
    switch(driver)
536
    {
537
#if (CONFIG_STORAGE & STORAGE_ATA)
538
    case STORAGE_ATA:
539
        return ata_present(ldrive);
540
#endif
541
 
542
#if (CONFIG_STORAGE & STORAGE_MMC)
543
    case STORAGE_MMC:
544
        return mmc_present(ldrive);
545
#endif
546
 
547
#if (CONFIG_STORAGE & STORAGE_SD)
548
    case STORAGE_SD:
549
        return sd_present(ldrive);
550
#endif
551
 
552
#if (CONFIG_STORAGE & STORAGE_NAND)
553
    case STORAGE_NAND:
554
        return true;
555
#endif
556
 
557
#if (CONFIG_STORAGE & STORAGE_RAMDISK)
558
    case STORAGE_RAMDISK:
559
        return true;
560
#endif
561
 
562
    default:
563
        return false;
564
    }
565
}
566
#endif