Enable hardware fan control for m57sli.
[coreboot.git] / src / superio / ite / it8716f / superio.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2006 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2007 AMD
6  * (Written by Yinghai Lu <yinghai.lu@amd.com> for AMD)
7  * Copyright (C) 2007 Ward Vandewege <ward@gnu.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
22  */
23
24 #include <device/device.h>
25 #include <device/pnp.h>
26 #include <console/console.h>
27 #include <uart8250.h>
28 #include <pc80/keyboard.h>
29 #include <arch/io.h>
30 #include "chip.h"
31 #include "it8716f.h"
32
33 /* Base address 0x2e: 0x87 0x01 0x55 0x55. */
34 /* Base address 0x4e: 0x87 0x01 0x55 0xaa. */
35 static void pnp_enter_ext_func_mode(device_t dev)
36 {
37         outb(0x87, dev->path.u.pnp.port);
38         outb(0x01, dev->path.u.pnp.port);
39         outb(0x55, dev->path.u.pnp.port);
40
41         if (dev->path.u.pnp.port == 0x4e) {
42                 outb(0xaa, dev->path.u.pnp.port);
43         } else {
44                 outb(0x55, dev->path.u.pnp.port);
45         }
46 }
47
48 static void pnp_exit_ext_func_mode(device_t dev)
49 {
50         pnp_write_config(dev, 0x02, 0x02);
51 }
52
53 #ifdef HAVE_FANCTL
54 extern void init_ec(uint16_t base);
55 #else
56 static void pnp_write_index(uint16_t port_base, uint8_t reg, uint8_t value)
57 {
58         outb(reg, port_base);
59         outb(value, port_base + 1);
60 }
61
62 static uint8_t pnp_read_index(uint16_t port_base, uint8_t reg)
63 {
64         outb(reg, port_base);
65         return inb(port_base + 1);
66 }
67
68 static void init_ec(uint16_t base)
69 {
70         uint8_t value;
71
72         /* Read out current value of FAN_CTL control register (0x14). */
73         value = pnp_read_index(base, 0x14);
74         printk_debug("FAN_CTL: reg = 0x%04x, read value = 0x%02x\r\n",
75                      base + 0x14, value);
76
77         /* Set FAN_CTL control register (0x14) polarity to high, and
78            activate fans 1, 2 and 3. */
79         pnp_write_index(base, 0x14, value | 0x87);
80         printk_debug("FAN_CTL: reg = 0x%04x, writing value = 0x%02x\r\n",
81                      base + 0x14, value | 0x87);
82 }
83 #endif
84
85 static void it8716f_init(device_t dev)
86 {
87         struct superio_ite_it8716f_config *conf;
88         struct resource *res0, *res1;
89
90         if (!dev->enabled) {
91                 return;
92         }
93
94         conf = dev->chip_info;
95
96         /* TODO: FDC, PP, KBCM, MIDI, GAME, IR. */
97         switch (dev->path.u.pnp.device) {
98         case IT8716F_SP1:
99                 res0 = find_resource(dev, PNP_IDX_IO0);
100                 init_uart8250(res0->base, &conf->com1);
101                 break;
102         case IT8716F_SP2:
103                 res0 = find_resource(dev, PNP_IDX_IO0);
104                 init_uart8250(res0->base, &conf->com2);
105                 break;
106         case IT8716F_EC:
107                 res0 = find_resource(dev, PNP_IDX_IO0);
108 #define EC_INDEX_PORT 5
109                 init_ec(res0->base + EC_INDEX_PORT);
110                 break;
111         case IT8716F_KBCK:
112                 res0 = find_resource(dev, PNP_IDX_IO0);
113                 res1 = find_resource(dev, PNP_IDX_IO1);
114                 init_pc_keyboard(res0->base, res1->base, &conf->keyboard);
115                 break;
116         }
117 }
118
119 static void it8716f_pnp_set_resources(device_t dev)
120 {
121         pnp_enter_ext_func_mode(dev);
122         pnp_set_resources(dev);
123         pnp_exit_ext_func_mode(dev);
124 }
125
126 static void it8716f_pnp_enable_resources(device_t dev)
127 {
128         pnp_enter_ext_func_mode(dev);
129         pnp_enable_resources(dev);
130         pnp_exit_ext_func_mode(dev);
131 }
132
133 static void it8716f_pnp_enable(device_t dev)
134 {
135         pnp_enter_ext_func_mode(dev);
136         pnp_set_logical_device(dev);
137         pnp_set_enable(dev, dev->enabled);
138         pnp_exit_ext_func_mode(dev);
139 }
140
141 static struct device_operations ops = {
142         .read_resources   = pnp_read_resources,
143         .set_resources    = it8716f_pnp_set_resources,
144         .enable_resources = it8716f_pnp_enable_resources,
145         .enable           = it8716f_pnp_enable,
146         .init             = it8716f_init,
147 };
148
149 static struct pnp_info pnp_dev_info[] = {
150         {&ops, IT8716F_FDC, PNP_IO0 | PNP_IRQ0 | PNP_DRQ0, {0x07f8, 0},},
151         {&ops, IT8716F_SP1, PNP_IO0 | PNP_IRQ0, {0x7f8, 0},},
152         {&ops, IT8716F_SP2, PNP_IO0 | PNP_IRQ0, {0x7f8, 0},},
153         {&ops, IT8716F_PP, PNP_IO0 | PNP_IRQ0 | PNP_DRQ0, {0x07f8, 0},},
154         {&ops, IT8716F_EC, PNP_IO0 | PNP_IO1 | PNP_IRQ0, {0x7f8, 0},
155          {0x7f8, 0x4},},
156         {&ops, IT8716F_KBCK, PNP_IO0 | PNP_IO1 | PNP_IRQ0, {0x7ff, 0},
157          {0x7ff, 0x4},},
158         {&ops, IT8716F_KBCM, PNP_IRQ0,},
159         {&ops, IT8716F_GPIO, PNP_IO1 | PNP_IO2, {0, 0}, {0x7f8, 0}, {0x7f8, 0},},
160         {&ops, IT8716F_MIDI, PNP_IO0 | PNP_IRQ0, {0x7fe, 0x4},},
161         {&ops, IT8716F_GAME, PNP_IO0, {0x7ff, 0},},
162         {&ops, IT8716F_IR,},
163 };
164
165 static void enable_dev(struct device *dev)
166 {
167         pnp_enable_devices(dev, &ops,
168                 sizeof(pnp_dev_info) / sizeof(pnp_dev_info[0]), pnp_dev_info);
169 }
170
171 struct chip_operations superio_ite_it8716f_ops = {
172         CHIP_NAME("ITE IT8716F Super I/O")
173         .enable_dev = enable_dev,
174 };