Minor clean up in playback.c
[kugel-rb.git] / firmware / storage.c
blobd6700d1148f2d21a7df583beb8f370f54fcf4077
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 by Frank Gevaerts
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "storage.h"
23 #define DRIVER_MASK 0xff000000
24 #define DRIVER_OFFSET 24
25 #define DRIVE_MASK 0x00ff0000
26 #define DRIVE_OFFSET 16
27 #define PARTITION_MASK 0x0000ff00
29 static unsigned int storage_drivers[NUM_DRIVES];
30 static unsigned int num_drives;
32 int storage_num_drives(void)
34 return num_drives;
37 int storage_init(void)
39 int rc=0;
40 int i;
41 num_drives=0;
43 #if (CONFIG_STORAGE & STORAGE_ATA)
44 if ((rc=ata_init())) return rc;
46 int ata_drives = ata_num_drives(num_drives);
47 for (i=0; i<ata_drives; i++)
49 storage_drivers[num_drives++] =
50 (STORAGE_ATA<<DRIVER_OFFSET) | (i << DRIVE_OFFSET);
52 #endif
54 #if (CONFIG_STORAGE & STORAGE_MMC)
55 if ((rc=mmc_init())) return rc;
57 int mmc_drives = mmc_num_drives(num_drives);
58 for (i=0; i<mmc_drives ;i++)
60 storage_drivers[num_drives++] =
61 (STORAGE_MMC<<DRIVER_OFFSET) | (i << DRIVE_OFFSET);
63 #endif
65 #if (CONFIG_STORAGE & STORAGE_SD)
66 if ((rc=sd_init())) return rc;
68 int sd_drives = sd_num_drives(num_drives);
69 for (i=0; i<sd_drives; i++)
71 storage_drivers[num_drives++] =
72 (STORAGE_SD<<DRIVER_OFFSET) | (i << DRIVE_OFFSET);
74 #endif
76 #if (CONFIG_STORAGE & STORAGE_NAND)
77 if ((rc=nand_init())) return rc;
79 int nand_drives = nand_num_drives(num_drives);
80 for (i=0; i<nand_drives; i++)
82 storage_drivers[num_drives++] =
83 (STORAGE_NAND<<DRIVER_OFFSET) | (i << DRIVE_OFFSET);
85 #endif
87 #if (CONFIG_STORAGE & STORAGE_RAMDISK)
88 if ((rc=ramdisk_init())) return rc;
90 int ramdisk_drives = ramdisk_num_drives(num_drives);
91 for (i=0; i<ramdisk_drives; i++)
93 storage_drivers[num_drives++] =
94 (STORAGE_RAMDISK<<DRIVER_OFFSET) | (i << DRIVE_OFFSET);
96 #endif
98 return 0;
101 int storage_read_sectors(int drive, unsigned long start, int count,
102 void* buf)
104 int driver=(storage_drivers[drive] & DRIVER_MASK)>>DRIVER_OFFSET;
105 int ldrive=(storage_drivers[drive] & DRIVE_MASK)>>DRIVE_OFFSET;
107 switch (driver)
109 #if (CONFIG_STORAGE & STORAGE_ATA)
110 case STORAGE_ATA:
111 return ata_read_sectors(ldrive,start,count,buf);
112 #endif
114 #if (CONFIG_STORAGE & STORAGE_MMC)
115 case STORAGE_MMC:
116 return mmc_read_sectors(ldrive,start,count,buf);
117 #endif
119 #if (CONFIG_STORAGE & STORAGE_SD)
120 case STORAGE_SD:
121 return sd_read_sectors(ldrive,start,count,buf);
122 #endif
124 #if (CONFIG_STORAGE & STORAGE_NAND)
125 case STORAGE_NAND:
126 return nand_read_sectors(ldrive,start,count,buf);
127 #endif
129 #if (CONFIG_STORAGE & STORAGE_RAMDISK)
130 case STORAGE_RAMDISK:
131 return ramdisk_read_sectors(ldrive,start,count,buf);
132 #endif
135 return -1;
138 int storage_write_sectors(int drive, unsigned long start, int count,
139 const void* buf)
141 int driver=(storage_drivers[drive] & DRIVER_MASK)>>DRIVER_OFFSET;
142 int ldrive=(storage_drivers[drive] & DRIVE_MASK)>>DRIVE_OFFSET;
144 switch (driver)
146 #if (CONFIG_STORAGE & STORAGE_ATA)
147 case STORAGE_ATA:
148 return ata_write_sectors(ldrive,start,count,buf);
149 #endif
151 #if (CONFIG_STORAGE & STORAGE_MMC)
152 case STORAGE_MMC:
153 return mmc_write_sectors(ldrive,start,count,buf);
154 #endif
156 #if (CONFIG_STORAGE & STORAGE_SD)
157 case STORAGE_SD:
158 return sd_write_sectors(ldrive,start,count,buf);
159 #endif
161 #if (CONFIG_STORAGE & STORAGE_NAND)
162 case STORAGE_NAND:
163 return nand_write_sectors(ldrive,start,count,buf);
164 #endif
166 #if (CONFIG_STORAGE & STORAGE_RAMDISK)
167 case STORAGE_RAMDISK:
168 return ramdisk_write_sectors(ldrive,start,count,buf);
169 #endif
172 return -1;
175 void storage_enable(bool on)
177 #if (CONFIG_STORAGE & STORAGE_ATA)
178 ata_enable(on);
179 #endif
181 #if (CONFIG_STORAGE & STORAGE_MMC)
182 mmc_enable(on);
183 #endif
185 #if (CONFIG_STORAGE & STORAGE_SD)
186 sd_enable(on);
187 #endif
189 #if (CONFIG_STORAGE & STORAGE_NAND)
190 nand_enable(on);
191 #endif
193 #if (CONFIG_STORAGE & STORAGE_RAMDISK)
194 ramdisk_enable(on);
195 #endif
198 void storage_sleep(void)
200 #if (CONFIG_STORAGE & STORAGE_ATA)
201 ata_sleep();
202 #endif
204 #if (CONFIG_STORAGE & STORAGE_MMC)
205 mmc_sleep();
206 #endif
208 #if (CONFIG_STORAGE & STORAGE_SD)
209 sd_sleep();
210 #endif
212 #if (CONFIG_STORAGE & STORAGE_NAND)
213 nand_sleep();
214 #endif
216 #if (CONFIG_STORAGE & STORAGE_RAMDISK)
217 ramdisk_sleep();
218 #endif
221 void storage_sleepnow(void)
223 #if (CONFIG_STORAGE & STORAGE_ATA)
224 ata_sleepnow();
225 #endif
227 #if (CONFIG_STORAGE & STORAGE_MMC)
228 mmc_sleepnow();
229 #endif
231 #if (CONFIG_STORAGE & STORAGE_SD)
232 //sd_sleepnow();
233 #endif
235 #if (CONFIG_STORAGE & STORAGE_NAND)
236 nand_sleepnow();
237 #endif
239 #if (CONFIG_STORAGE & STORAGE_RAMDISK)
240 ramdisk_sleepnow();
241 #endif
244 bool storage_disk_is_active(void)
246 #if (CONFIG_STORAGE & STORAGE_ATA)
247 if (ata_disk_is_active()) return true;
248 #endif
250 #if (CONFIG_STORAGE & STORAGE_MMC)
251 if (mmc_disk_is_active()) return true;
252 #endif
254 #if (CONFIG_STORAGE & STORAGE_SD)
255 //if (sd_disk_is_active()) return true;
256 #endif
258 #if (CONFIG_STORAGE & STORAGE_NAND)
259 //if (nand_disk_is_active()) return true;
260 #endif
262 #if (CONFIG_STORAGE & STORAGE_RAMDISK)
263 if (ramdisk_disk_is_active()) return true;
264 #endif
266 return false;
269 int storage_soft_reset(void)
271 int rc=0;
273 #if (CONFIG_STORAGE & STORAGE_ATA)
274 if ((rc=ata_soft_reset())) return rc;
275 #endif
277 #if (CONFIG_STORAGE & STORAGE_MMC)
278 if ((rc=mmc_soft_reset())) return rc;
279 #endif
281 #if (CONFIG_STORAGE & STORAGE_SD)
282 //if ((rc=sd_soft_reset())) return rc;
283 #endif
285 #if (CONFIG_STORAGE & STORAGE_NAND)
286 //if ((rc=nand_soft_reset())) return rc;
287 #endif
289 #if (CONFIG_STORAGE & STORAGE_RAMDISK)
290 if ((rc=ramdisk_soft_reset())) return rc;
291 #endif
293 return rc;
296 #ifdef HAVE_STORAGE_FLUSH
297 int storage_flush(void)
299 int rc=0;
301 #if (CONFIG_STORAGE & STORAGE_ATA)
302 //if ((rc=ata_flush())) return rc;
303 #endif
305 #if (CONFIG_STORAGE & STORAGE_MMC)
306 //if ((rc=mmc_flush())) return rc;
307 #endif
309 #if (CONFIG_STORAGE & STORAGE_SD)
310 //if ((rc=sd_flush())) return rc;
311 #endif
313 #if (CONFIG_STORAGE & STORAGE_NAND)
314 if ((rc=nand_flush())) return rc;
315 #endif
317 #if (CONFIG_STORAGE & STORAGE_RAMDISK)
318 //if ((rc=ramdisk_flush())) return rc;
319 #endif
321 return rc;
323 #endif
325 void storage_spin(void)
327 #if (CONFIG_STORAGE & STORAGE_ATA)
328 ata_spin();
329 #endif
331 #if (CONFIG_STORAGE & STORAGE_MMC)
332 mmc_spin();
333 #endif
335 #if (CONFIG_STORAGE & STORAGE_SD)
336 sd_spin();
337 #endif
339 #if (CONFIG_STORAGE & STORAGE_NAND)
340 nand_spin();
341 #endif
343 #if (CONFIG_STORAGE & STORAGE_RAMDISK)
344 ramdisk_spin();
345 #endif
348 void storage_spindown(int seconds)
350 #if (CONFIG_STORAGE & STORAGE_ATA)
351 ata_spindown(seconds);
352 #endif
354 #if (CONFIG_STORAGE & STORAGE_MMC)
355 mmc_spindown(seconds);
356 #endif
358 #if (CONFIG_STORAGE & STORAGE_SD)
359 sd_spindown(seconds);
360 #endif
362 #if (CONFIG_STORAGE & STORAGE_NAND)
363 nand_spindown(seconds);
364 #endif
366 #if (CONFIG_STORAGE & STORAGE_RAMDISK)
367 ramdisk_spindown(seconds);
368 #endif
371 #if (CONFIG_LED == LED_REAL)
372 void storage_set_led_enabled(bool enabled)
374 #if (CONFIG_STORAGE & STORAGE_ATA)
375 ata_set_led_enabled(enabled);
376 #endif
378 #if (CONFIG_STORAGE & STORAGE_MMC)
379 mmc_set_led_enabled(enabled);
380 #endif
382 #if (CONFIG_STORAGE & STORAGE_SD)
383 sd_set_led_enabled(enabled);
384 #endif
386 #if (CONFIG_STORAGE & STORAGE_NAND)
387 nand_set_led_enabled(enabled);
388 #endif
390 #if (CONFIG_STORAGE & STORAGE_RAMDISK)
391 ramdisk_set_led_enabled(enabled);
392 #endif
394 #endif /* CONFIG_LED == LED_REAL */
396 long storage_last_disk_activity(void)
398 long max=0;
399 long t;
401 #if (CONFIG_STORAGE & STORAGE_ATA)
402 t=ata_last_disk_activity();
403 if (t>max) max=t;
404 #endif
406 #if (CONFIG_STORAGE & STORAGE_MMC)
407 t=mmc_last_disk_activity();
408 if (t>max) max=t;
409 #endif
411 #if (CONFIG_STORAGE & STORAGE_SD)
412 t=sd_last_disk_activity();
413 if (t>max) max=t;
414 #endif
416 #if (CONFIG_STORAGE & STORAGE_NAND)
417 t=nand_last_disk_activity();
418 if (t>max) max=t;
419 #endif
421 #if (CONFIG_STORAGE & STORAGE_RAMDISK)
422 t=ramdisk_last_disk_activity();
423 if (t>max) max=t;
424 #endif
426 return max;
429 int storage_spinup_time(void)
431 int max=0;
432 int t;
434 #if (CONFIG_STORAGE & STORAGE_ATA)
435 t=ata_spinup_time();
436 if (t>max) max=t;
437 #endif
439 #if (CONFIG_STORAGE & STORAGE_MMC)
440 t=mmc_spinup_time();
441 if (t>max) max=t;
442 #endif
444 #if (CONFIG_STORAGE & STORAGE_SD)
445 //t=sd_spinup_time();
446 //if (t>max) max=t;
447 #endif
449 #if (CONFIG_STORAGE & STORAGE_NAND)
450 t=nand_spinup_time();
451 if (t>max) max=t;
452 #endif
454 #if (CONFIG_STORAGE & STORAGE_RAMDISK)
455 t=ramdisk_spinup_time();
456 if (t>max) max=t;
457 #endif
459 return max;
462 #ifdef STORAGE_GET_INFO
463 void storage_get_info(int drive, struct storage_info *info)
465 int driver=(storage_drivers[drive] & DRIVER_MASK)>>DRIVER_OFFSET;
466 int ldrive=(storage_drivers[drive] & DRIVE_MASK)>>DRIVE_OFFSET;
468 switch(driver)
470 #if (CONFIG_STORAGE & STORAGE_ATA)
471 case STORAGE_ATA:
472 return ata_get_info(ldrive,info);
473 #endif
475 #if (CONFIG_STORAGE & STORAGE_MMC)
476 case STORAGE_MMC:
477 return mmc_get_info(ldrive,info);
478 #endif
480 #if (CONFIG_STORAGE & STORAGE_SD)
481 case STORAGE_SD:
482 return sd_get_info(ldrive,info);
483 #endif
485 #if (CONFIG_STORAGE & STORAGE_NAND)
486 case STORAGE_NAND:
487 return nand_get_info(ldrive,info);
488 #endif
490 #if (CONFIG_STORAGE & STORAGE_RAMDISK)
491 case STORAGE_RAMDISK:
492 return ramdisk_get_info(ldrive,info);
493 #endif
496 #endif /* STORAGE_GET_INFO */
498 #ifdef HAVE_HOTSWAP
499 bool storage_removable(int drive)
501 bool ret = false;
503 int driver=(storage_drivers[drive] & DRIVER_MASK)>>DRIVER_OFFSET;
504 int ldrive=(storage_drivers[drive] & DRIVE_MASK)>>DRIVE_OFFSET;
506 switch(driver)
508 #if (CONFIG_STORAGE & STORAGE_ATA)
509 case STORAGE_ATA:
510 ret = ata_removable(ldrive);
511 #endif
513 #if (CONFIG_STORAGE & STORAGE_MMC)
514 case STORAGE_MMC:
515 ret = mmc_removable(ldrive);
516 #endif
518 #if (CONFIG_STORAGE & STORAGE_SD)
519 case STORAGE_SD:
520 ret = sd_removable(ldrive);
521 #endif
523 #if (CONFIG_STORAGE & STORAGE_NAND)
524 case STORAGE_NAND:
525 ret = false;
526 #endif
528 #if (CONFIG_STORAGE & STORAGE_RAMDISK)
529 case STORAGE_RAMDISK:
530 ret = false;
531 #endif
534 return ret;
537 bool storage_present(int drive)
539 bool ret = false;
541 int driver=(storage_drivers[drive] & DRIVER_MASK)>>DRIVER_OFFSET;
542 int ldrive=(storage_drivers[drive] & DRIVE_MASK)>>DRIVE_OFFSET;
544 switch(driver)
546 #if (CONFIG_STORAGE & STORAGE_ATA)
547 case STORAGE_ATA:
548 ret = ata_present(ldrive);
549 #endif
551 #if (CONFIG_STORAGE & STORAGE_MMC)
552 case STORAGE_MMC:
553 ret = mmc_present(ldrive);
554 #endif
556 #if (CONFIG_STORAGE & STORAGE_SD)
557 case STORAGE_SD:
558 ret = sd_present(ldrive);
559 #endif
561 #if (CONFIG_STORAGE & STORAGE_NAND)
562 case STORAGE_NAND:
563 ret = true;
564 #endif
566 #if (CONFIG_STORAGE & STORAGE_RAMDISK)
567 case STORAGE_RAMDISK:
568 ret = true;
569 #endif
572 return ret;
574 #endif