blob: 3034bab82d90817d28efdbe2a492b9dc23605731 [file] [log] [blame]
Marcin Bukat28d54c62010-04-26 21:40:16 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
Marcin Bukat082c7d32010-10-22 12:28:43 +00008 * $Id$
Marcin Bukat28d54c62010-04-26 21:40:16 +00009 *
10 * Copyright (C) 2010 Marcin Bukat
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
22#include "config.h"
23#include "cpu.h"
24#include <stdbool.h>
25#include "kernel.h"
26#include "system.h"
27#include "lcd.h"
28#include "power.h"
29
30#if CONFIG_TUNER
31bool tuner_power(bool status)
32{
Marcin Bukat28d54c62010-04-26 21:40:16 +000033 if (status)
Marcin Bukat28d54c62010-04-26 21:40:16 +000034 and_l(~(1<<17), &GPIO1_OUT);
Marcin Bukat28d54c62010-04-26 21:40:16 +000035 else
Marcin Bukat28d54c62010-04-26 21:40:16 +000036 or_l((1<<17), &GPIO1_OUT);
Marcin Bukat28d54c62010-04-26 21:40:16 +000037
38 return status;
39}
40#endif /* #if CONFIG_TUNER */
41
42void power_init(void)
43{
Marcin Bukat2f7bd4d2010-06-11 11:41:29 +000044 /* GPIO53 has to be high - low resets device
45 * it is setup in crt0.S
46 */
Marcin Bukat28d54c62010-04-26 21:40:16 +000047
Marcin Bukat2f7bd4d2010-06-11 11:41:29 +000048 /* GPIO50 is ATA power (default OFF) */
49 or_l((1<<18), &GPIO1_OUT);
50 or_l((1<<18), &GPIO1_ENABLE);
51 or_l((1<<18), &GPIO1_FUNCTION);
Marcin Bukat28d54c62010-04-26 21:40:16 +000052
Marcin Bukat2f7bd4d2010-06-11 11:41:29 +000053 /* GPIO49 is FM power (default OFF) */
54 or_l((1<<17), &GPIO1_OUT);
55 or_l((1<<17), &GPIO1_ENABLE);
56 or_l((1<<17), &GPIO1_FUNCTION);
57
58 /* GPIO46 is wall charger detect (input) */
59 and_l(~(1<<14), &GPIO1_ENABLE);
60 or_l((1<<14), &GPIO1_FUNCTION);
61
62 /* GPIO31 needs to be low */
63 and_l(~(1<<31), &GPIO_OUT);
64 or_l((1<<31),&GPIO_ENABLE);
65 or_l((1<<31),&GPIO_FUNCTION);
66
67 /* turn off charger by default*/
Marcin Bukat28d54c62010-04-26 21:40:16 +000068 or_l((1<<23), &GPIO_OUT);
Marcin Bukatf99e4fb2010-07-06 17:30:05 +000069 or_l((1<<23), &GPIO_ENABLE);
Marcin Bukat28d54c62010-04-26 21:40:16 +000070 or_l((1<<23), &GPIO_FUNCTION);
71
Marcin Bukat2f7bd4d2010-06-11 11:41:29 +000072 /* high current charge mode */
73 or_l((1<<15), &GPIO_OUT);
74 or_l((1<<15),&GPIO_ENABLE);
75 or_l((1<<15),&GPIO_FUNCTION);
76
Marcin Bukat28d54c62010-04-26 21:40:16 +000077#ifndef BOOTLOADER
78 /* The boot loader controls the power */
79 ide_power_enable(true);
80#endif
81}
82
83unsigned int power_input_status(void)
84{
85 unsigned int status = POWER_INPUT_NONE;
86/* GPIO46 is AC plug detect (low = AC plugged) */
87 if (!(GPIO1_READ & (1<<14)))
Marcin Bukat2f7bd4d2010-06-11 11:41:29 +000088 {
89 status |= POWER_INPUT_MAIN_CHARGER;
90 /* tristate GPIO23 to start charging cycle */
91 and_l(~(1<<23), &GPIO_ENABLE);
92 }
93 else
94 {
95 /* drive GPIO23 high to enter LTC1733 shutdown mode */
96 or_l((1<<23), &GPIO_ENABLE);
97 }
Marcin Bukat28d54c62010-04-26 21:40:16 +000098 return status;
99}
100
101/* Returns true if the unit is charging the batteries. */
102bool charging_state(void)
103{
104 if (!(GPIO1_READ & (1<<14)))
105 return (GPIO_READ & (1<<30) )?false:true;
106 else
107 return false;
108}
109
110void ide_power_enable(bool on)
111{
Marcin Bukat2f7bd4d2010-06-11 11:41:29 +0000112 if (on)
113 and_l(~(1<<18),&GPIO1_OUT);
Marcin Bukat28d54c62010-04-26 21:40:16 +0000114 else
Marcin Bukat2f7bd4d2010-06-11 11:41:29 +0000115 or_l((1<<18),&GPIO1_OUT);
Marcin Bukat28d54c62010-04-26 21:40:16 +0000116}
117
118bool ide_powered(void)
119{
Marcin Bukat2f7bd4d2010-06-11 11:41:29 +0000120 return (GPIO1_OUT & (1<<18))?false:true;
Marcin Bukat28d54c62010-04-26 21:40:16 +0000121}
122
123void power_off(void)
124{
125 lcd_shutdown();
126 set_irq_level(DISABLE_INTERRUPTS);
127 and_l(~(1<<21), &GPIO1_OUT); /* pull KEEPACT low */
128 asm("halt");
129 while(1);
130}