T60: Add support for Ultrabay Legacy I/O devices (40Y8122)
[coreboot.git] / src / ec / lenovo / pmh7 / pmh7.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2011 Sven Schnelle <svens@stackframe.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #include <arch/io.h>
21 #include <console/console.h>
22 #include <device/device.h>
23 #include <device/pnp.h>
24 #include <stdlib.h>
25 #include "pmh7.h"
26 #include "chip.h"
27 #include <pc80/mc146818rtc.h>
28
29 void pmh7_backlight_enable(int onoff)
30 {
31         if (onoff)
32                 pmh7_register_set_bit(0x50, 5);
33         else
34                 pmh7_register_clear_bit(0x50, 5);
35 }
36
37 void pmh7_dock_event_enable(int onoff)
38 {
39         if (onoff)
40                 pmh7_register_set_bit(0x60, 3);
41         else
42                 pmh7_register_clear_bit(0x60, 3);
43
44 }
45
46 void pmh7_touchpad_enable(int onoff)
47 {
48         if (onoff)
49                 pmh7_register_clear_bit(0x51, 2);
50         else
51                 pmh7_register_set_bit(0x51, 2);
52 }
53
54 void pmh7_ultrabay_power_enable(int onoff)
55 {
56         if (onoff)
57                 pmh7_register_clear_bit(0x62, 0);
58         else
59                 pmh7_register_set_bit(0x62, 0);
60 }
61
62 void pmh7_register_set_bit(int reg, int bit)
63 {
64         char val;
65
66         outb(reg, EC_LENOVO_PMH7_ADDR);
67         val = inb(EC_LENOVO_PMH7_DATA);
68         outb(reg, EC_LENOVO_PMH7_ADDR);
69         outb(val | (1 << bit), EC_LENOVO_PMH7_DATA);
70 }
71
72 void pmh7_register_clear_bit(int reg, int bit)
73 {
74         char val;
75
76         outb(reg, EC_LENOVO_PMH7_ADDR);
77         val = inb(EC_LENOVO_PMH7_DATA);
78         outb(reg, EC_LENOVO_PMH7_ADDR);
79         outb(val &= ~(1 << bit), EC_LENOVO_PMH7_DATA);
80 }
81
82 char pmh7_register_read(int reg)
83 {
84         outb(reg, EC_LENOVO_PMH7_ADDR);
85         return inb(EC_LENOVO_PMH7_DATA);
86 }
87
88 void pmh7_register_write(int reg, int val)
89 {
90         outb(reg, EC_LENOVO_PMH7_ADDR);
91         outb(val, EC_LENOVO_PMH7_DATA);
92 }
93
94 #ifndef __PRE_RAM__
95 #ifndef __SMM__
96 static void enable_dev(device_t dev)
97 {
98         struct ec_lenovo_pmh7_config *conf = dev->chip_info;
99         struct resource *resource;
100         u8 val;
101
102         resource = new_resource(dev, EC_LENOVO_PMH7_INDEX);
103         resource->flags = IORESOURCE_IO | IORESOURCE_FIXED;
104         resource->base = EC_LENOVO_PMH7_BASE;
105         resource->size = 16;
106         resource->align = 5;
107         resource->gran = 5;
108
109         pmh7_backlight_enable(conf->backlight_enable);
110         pmh7_dock_event_enable(conf->dock_event_enable);
111
112         if (!get_option(&val, "touchpad"))
113                 pmh7_touchpad_enable(val);
114 }
115
116 struct chip_operations ec_lenovo_pmh7_ops = {
117         CHIP_NAME("Lenovo Power Management Hardware Hub 7")
118         .enable_dev = enable_dev,
119 };
120 #endif
121 #endif