blob: da691f8efecd76596c24716a4cb707b8c35d3251 [file] [log] [blame]
Uwe Hermannb80dbf02007-04-22 19:08:13 +00001/*
Stefan Reinauer7e61e452008-01-18 10:35:56 +00002 * This file is part of the coreboot project.
Uwe Hermannb80dbf02007-04-22 19:08:13 +00003 *
4 * Copyright (C) 2004 Linux Networx
5 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
6 * Copyright (C) 2004 Li-Ta Lo <ollie@lanl.gov>
7 * Copyright (C) 2005 Tyan
8 * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
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
Paul Menzela46a7122013-02-23 18:37:27 +010021 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Uwe Hermannb80dbf02007-04-22 19:08:13 +000022 */
Eric Biederman5cd81732004-03-11 15:01:31 +000023
24#include <console/console.h>
25#include <stdlib.h>
26#include <stdint.h>
Eric Biederman5cd81732004-03-11 15:01:31 +000027#include <string.h>
28#include <arch/io.h>
29#include <device/device.h>
30#include <device/pnp.h>
31
Eric Biederman5cd81732004-03-11 15:01:31 +000032/* PNP fundamental operations */
33
Uwe Hermannd453dd02010-10-18 00:00:57 +000034void pnp_write_config(device_t dev, u8 reg, u8 value)
Eric Biederman5cd81732004-03-11 15:01:31 +000035{
Stefan Reinauer2b34db82009-02-28 20:10:20 +000036 outb(reg, dev->path.pnp.port);
37 outb(value, dev->path.pnp.port + 1);
Eric Biederman5cd81732004-03-11 15:01:31 +000038}
39
Uwe Hermannd453dd02010-10-18 00:00:57 +000040u8 pnp_read_config(device_t dev, u8 reg)
Eric Biederman5cd81732004-03-11 15:01:31 +000041{
Stefan Reinauer2b34db82009-02-28 20:10:20 +000042 outb(reg, dev->path.pnp.port);
43 return inb(dev->path.pnp.port + 1);
Eric Biederman5cd81732004-03-11 15:01:31 +000044}
45
46void pnp_set_logical_device(device_t dev)
47{
Stefan Reinauer2b34db82009-02-28 20:10:20 +000048 pnp_write_config(dev, 0x07, dev->path.pnp.device & 0xff);
Eric Biederman5cd81732004-03-11 15:01:31 +000049}
50
51void pnp_set_enable(device_t dev, int enable)
52{
Uwe Hermannd453dd02010-10-18 00:00:57 +000053 u8 tmp, bitpos;
Rudolf Marek623df672008-02-18 20:32:46 +000054
55 tmp = pnp_read_config(dev, 0x30);
Uwe Hermannd453dd02010-10-18 00:00:57 +000056
57 /* Handle virtual devices, which share the same LDN register. */
Stefan Reinauer2b34db82009-02-28 20:10:20 +000058 bitpos = (dev->path.pnp.device >> 8) & 0x7;
Rudolf Marek623df672008-02-18 20:32:46 +000059
Uwe Hermannd453dd02010-10-18 00:00:57 +000060 if (enable)
Rudolf Marek623df672008-02-18 20:32:46 +000061 tmp |= (1 << bitpos);
Uwe Hermannd453dd02010-10-18 00:00:57 +000062 else
Rudolf Marek623df672008-02-18 20:32:46 +000063 tmp &= ~(1 << bitpos);
Uwe Hermannd453dd02010-10-18 00:00:57 +000064
Rudolf Marek623df672008-02-18 20:32:46 +000065 pnp_write_config(dev, 0x30, tmp);
Eric Biederman5cd81732004-03-11 15:01:31 +000066}
67
68int pnp_read_enable(device_t dev)
69{
Uwe Hermannd453dd02010-10-18 00:00:57 +000070 u8 tmp, bitpos;
71
Rudolf Marek623df672008-02-18 20:32:46 +000072 tmp = pnp_read_config(dev, 0x30);
Uwe Hermannd453dd02010-10-18 00:00:57 +000073
74 /* Handle virtual devices, which share the same LDN register. */
Stefan Reinauer2b34db82009-02-28 20:10:20 +000075 bitpos = (dev->path.pnp.device >> 8) & 0x7;
Uwe Hermannd453dd02010-10-18 00:00:57 +000076
Rudolf Marekb05d4bb2008-02-19 20:30:25 +000077 return !!(tmp & (1 << bitpos));
Eric Biederman5cd81732004-03-11 15:01:31 +000078}
79
Uwe Hermannd453dd02010-10-18 00:00:57 +000080void pnp_set_iobase(device_t dev, u8 index, u16 iobase)
Eric Biederman5cd81732004-03-11 15:01:31 +000081{
Uwe Hermannd453dd02010-10-18 00:00:57 +000082 /* Index == 0x60 or 0x62. */
Eric Biederman5cd81732004-03-11 15:01:31 +000083 pnp_write_config(dev, index + 0, (iobase >> 8) & 0xff);
84 pnp_write_config(dev, index + 1, iobase & 0xff);
85}
86
Uwe Hermannd453dd02010-10-18 00:00:57 +000087void pnp_set_irq(device_t dev, u8 index, u8 irq)
Eric Biederman5cd81732004-03-11 15:01:31 +000088{
Uwe Hermannd453dd02010-10-18 00:00:57 +000089 /* Index == 0x70 or 0x72. */
Eric Biederman5cd81732004-03-11 15:01:31 +000090 pnp_write_config(dev, index, irq);
91}
92
Uwe Hermannd453dd02010-10-18 00:00:57 +000093void pnp_set_drq(device_t dev, u8 index, u8 drq)
Eric Biederman5cd81732004-03-11 15:01:31 +000094{
Uwe Hermannd453dd02010-10-18 00:00:57 +000095 /* Index == 0x74. */
Eric Biederman5cd81732004-03-11 15:01:31 +000096 pnp_write_config(dev, index, drq & 0xff);
97}
98
99/* PNP device operations */
100
101void pnp_read_resources(device_t dev)
102{
103 return;
104}
105
106static void pnp_set_resource(device_t dev, struct resource *resource)
107{
108 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
Patrick Georgi51615092012-03-11 19:31:03 +0100109 printk(BIOS_ERR, "ERROR: %s %02lx %s size: 0x%010llx "
Uwe Hermannd453dd02010-10-18 00:00:57 +0000110 "not assigned\n", dev_path(dev), resource->index,
111 resource_type(resource), resource->size);
Eric Biederman5cd81732004-03-11 15:01:31 +0000112 return;
113 }
Li-Ta Lo75337f72004-05-07 21:56:48 +0000114
Uwe Hermannd453dd02010-10-18 00:00:57 +0000115 /* Now store the resource. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000116 if (resource->flags & IORESOURCE_IO) {
117 pnp_set_iobase(dev, resource->index, resource->base);
Uwe Hermannd453dd02010-10-18 00:00:57 +0000118 } else if (resource->flags & IORESOURCE_DRQ) {
Eric Biederman5cd81732004-03-11 15:01:31 +0000119 pnp_set_drq(dev, resource->index, resource->base);
Uwe Hermannd453dd02010-10-18 00:00:57 +0000120 } else if (resource->flags & IORESOURCE_IRQ) {
Eric Biederman5cd81732004-03-11 15:01:31 +0000121 pnp_set_irq(dev, resource->index, resource->base);
Uwe Hermannd453dd02010-10-18 00:00:57 +0000122 } else {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000123 printk(BIOS_ERR, "ERROR: %s %02lx unknown resource type\n",
Uwe Hermannd453dd02010-10-18 00:00:57 +0000124 dev_path(dev), resource->index);
Eric Biederman5cd81732004-03-11 15:01:31 +0000125 return;
126 }
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +0000127 resource->flags |= IORESOURCE_STORED;
Eric Biederman5cd81732004-03-11 15:01:31 +0000128
Eric Biederman03acab62004-10-14 21:25:53 +0000129 report_resource_stored(dev, resource, "");
Eric Biederman5cd81732004-03-11 15:01:31 +0000130}
131
132void pnp_set_resources(device_t dev)
133{
Myles Watsonc25cc112010-05-21 14:33:48 +0000134 struct resource *res;
Eric Biederman5cd81732004-03-11 15:01:31 +0000135
Uwe Hermannd453dd02010-10-18 00:00:57 +0000136 /* Select the logical device (LDN). */
Eric Biederman5cd81732004-03-11 15:01:31 +0000137 pnp_set_logical_device(dev);
138
139 /* Paranoia says I should disable the device here... */
Uwe Hermannd453dd02010-10-18 00:00:57 +0000140 for (res = dev->resource_list; res; res = res->next)
Myles Watsonc25cc112010-05-21 14:33:48 +0000141 pnp_set_resource(dev, res);
Eric Biederman5cd81732004-03-11 15:01:31 +0000142}
143
144void pnp_enable_resources(device_t dev)
145{
146 pnp_set_logical_device(dev);
147 pnp_set_enable(dev, 1);
Eric Biederman5cd81732004-03-11 15:01:31 +0000148}
149
150void pnp_enable(device_t dev)
151{
Li-Ta Lo75337f72004-05-07 21:56:48 +0000152 if (!dev->enabled) {
Li-Ta Loc6bcedb2004-04-21 16:57:05 +0000153 pnp_set_logical_device(dev);
Eric Biederman5cd81732004-03-11 15:01:31 +0000154 pnp_set_enable(dev, 0);
155 }
156}
157
158struct device_operations pnp_ops = {
159 .read_resources = pnp_read_resources,
160 .set_resources = pnp_set_resources,
161 .enable_resources = pnp_enable_resources,
162 .enable = pnp_enable,
163};
164
Uwe Hermanne4870472010-11-04 23:23:47 +0000165/* PNP chip operations */
Eric Biederman5cd81732004-03-11 15:01:31 +0000166
Uwe Hermannd453dd02010-10-18 00:00:57 +0000167static void pnp_get_ioresource(device_t dev, u8 index, struct io_info *info)
Eric Biederman5cd81732004-03-11 15:01:31 +0000168{
169 struct resource *resource;
Eric Biedermandbec2d42004-10-21 10:44:08 +0000170 unsigned moving, gran, step;
Li-Ta Lo75337f72004-05-07 21:56:48 +0000171
Stefan Reinaueraeead272011-01-31 21:16:48 +0000172 if (!info->mask) {
173 printk(BIOS_ERR, "ERROR: device %s index %d has no mask.\n",
174 dev_path(dev), index);
175 return;
176 }
177
Eric Biederman03acab62004-10-14 21:25:53 +0000178 resource = new_resource(dev, index);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000179
Uwe Hermannd453dd02010-10-18 00:00:57 +0000180 /* Initilize the resource. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000181 resource->limit = 0xffff;
182 resource->flags |= IORESOURCE_IO;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000183
Uwe Hermannd453dd02010-10-18 00:00:57 +0000184 /* Get the resource size... */
185
Eric Biedermandbec2d42004-10-21 10:44:08 +0000186 moving = info->mask;
187 gran = 15;
188 step = 1 << gran;
Uwe Hermannd453dd02010-10-18 00:00:57 +0000189
190 /* Find the first bit that moves. */
191 while ((moving & step) == 0) {
Eric Biedermandbec2d42004-10-21 10:44:08 +0000192 gran--;
193 step >>= 1;
194 }
Uwe Hermannd453dd02010-10-18 00:00:57 +0000195
196 /* Now find the first bit that does not move. */
197 while ((moving & step) != 0) {
Eric Biedermandbec2d42004-10-21 10:44:08 +0000198 gran--;
199 step >>= 1;
200 }
Uwe Hermannd453dd02010-10-18 00:00:57 +0000201
202 /*
203 * Of the moving bits the last bit in the first group,
Eric Biedermandbec2d42004-10-21 10:44:08 +0000204 * tells us the size of this resource.
205 */
206 if ((moving & step) == 0) {
207 gran++;
208 step <<= 1;
209 }
Uwe Hermannd453dd02010-10-18 00:00:57 +0000210
211 /* Set the resource size and alignment. */
Eric Biedermandbec2d42004-10-21 10:44:08 +0000212 resource->gran = gran;
213 resource->align = gran;
214 resource->limit = info->mask | (step - 1);
215 resource->size = 1 << gran;
Eric Biederman5cd81732004-03-11 15:01:31 +0000216}
217
218static void get_resources(device_t dev, struct pnp_info *info)
219{
220 struct resource *resource;
221
Uwe Hermanne4870472010-11-04 23:23:47 +0000222 if (info->flags & PNP_IO0)
Eric Biederman5cd81732004-03-11 15:01:31 +0000223 pnp_get_ioresource(dev, PNP_IDX_IO0, &info->io0);
Uwe Hermanne4870472010-11-04 23:23:47 +0000224 if (info->flags & PNP_IO1)
Eric Biederman5cd81732004-03-11 15:01:31 +0000225 pnp_get_ioresource(dev, PNP_IDX_IO1, &info->io1);
Uwe Hermanne4870472010-11-04 23:23:47 +0000226 if (info->flags & PNP_IO2)
Eric Biedermanb78c1972004-10-14 20:54:17 +0000227 pnp_get_ioresource(dev, PNP_IDX_IO2, &info->io2);
Uwe Hermanne4870472010-11-04 23:23:47 +0000228 if (info->flags & PNP_IO3)
Eric Biedermanb78c1972004-10-14 20:54:17 +0000229 pnp_get_ioresource(dev, PNP_IDX_IO3, &info->io3);
Uwe Hermanne4870472010-11-04 23:23:47 +0000230
Eric Biederman5cd81732004-03-11 15:01:31 +0000231 if (info->flags & PNP_IRQ0) {
Eric Biederman03acab62004-10-14 21:25:53 +0000232 resource = new_resource(dev, PNP_IDX_IRQ0);
Eric Biederman5cd81732004-03-11 15:01:31 +0000233 resource->size = 1;
234 resource->flags |= IORESOURCE_IRQ;
235 }
236 if (info->flags & PNP_IRQ1) {
Eric Biederman03acab62004-10-14 21:25:53 +0000237 resource = new_resource(dev, PNP_IDX_IRQ1);
Eric Biederman5cd81732004-03-11 15:01:31 +0000238 resource->size = 1;
239 resource->flags |= IORESOURCE_IRQ;
240 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000241
Eric Biederman5cd81732004-03-11 15:01:31 +0000242 if (info->flags & PNP_DRQ0) {
Eric Biederman03acab62004-10-14 21:25:53 +0000243 resource = new_resource(dev, PNP_IDX_DRQ0);
Eric Biederman5cd81732004-03-11 15:01:31 +0000244 resource->size = 1;
245 resource->flags |= IORESOURCE_DRQ;
246 }
247 if (info->flags & PNP_DRQ1) {
Eric Biederman03acab62004-10-14 21:25:53 +0000248 resource = new_resource(dev, PNP_IDX_DRQ1);
Eric Biederman5cd81732004-03-11 15:01:31 +0000249 resource->size = 1;
250 resource->flags |= IORESOURCE_DRQ;
Stefan Reinauerde3206a2010-02-22 06:09:43 +0000251 }
Uwe Hermannd453dd02010-10-18 00:00:57 +0000252
253 /*
254 * These are not IRQs, but set the flag to have the
255 * resource allocator do the right thing.
Stefan Reinauerde3206a2010-02-22 06:09:43 +0000256 */
257 if (info->flags & PNP_EN) {
258 resource = new_resource(dev, PNP_IDX_EN);
259 resource->size = 1;
260 resource->flags |= IORESOURCE_IRQ;
261 }
262 if (info->flags & PNP_MSC0) {
263 resource = new_resource(dev, PNP_IDX_MSC0);
264 resource->size = 1;
265 resource->flags |= IORESOURCE_IRQ;
266 }
267 if (info->flags & PNP_MSC1) {
268 resource = new_resource(dev, PNP_IDX_MSC1);
269 resource->size = 1;
270 resource->flags |= IORESOURCE_IRQ;
271 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000272}
Eric Biederman5cd81732004-03-11 15:01:31 +0000273
Stefan Reinauer14e22772010-04-27 06:56:47 +0000274void pnp_enable_devices(device_t base_dev, struct device_operations *ops,
Uwe Hermannd453dd02010-10-18 00:00:57 +0000275 unsigned int functions, struct pnp_info *info)
Eric Biederman5cd81732004-03-11 15:01:31 +0000276{
277 struct device_path path;
278 device_t dev;
279 int i;
280
Uwe Hermannd453dd02010-10-18 00:00:57 +0000281 path.type = DEVICE_PATH_PNP;
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000282 path.pnp.port = base_dev->path.pnp.port;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000283
Uwe Hermannd453dd02010-10-18 00:00:57 +0000284 /* Setup the ops and resources on the newly allocated devices. */
285 for (i = 0; i < functions; i++) {
Uwe Hermannaa4bedf2007-07-12 13:12:47 +0000286 /* Skip logical devices this Super I/O doesn't have. */
287 if (info[i].function == -1)
288 continue;
289
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000290 path.pnp.device = info[i].function;
Eric Biederman7003ba42004-10-16 06:20:29 +0000291 dev = alloc_find_dev(base_dev->bus, &path);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000292
Uwe Hermannd453dd02010-10-18 00:00:57 +0000293 /* Don't initialize a device multiple times. */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000294 if (dev->ops)
Eric Biederman7003ba42004-10-16 06:20:29 +0000295 continue;
Li-Ta Loc6bcedb2004-04-21 16:57:05 +0000296
Uwe Hermannd453dd02010-10-18 00:00:57 +0000297 if (info[i].ops == 0)
Li-Ta Lo75337f72004-05-07 21:56:48 +0000298 dev->ops = ops;
Uwe Hermannd453dd02010-10-18 00:00:57 +0000299 else
Li-Ta Lo75337f72004-05-07 21:56:48 +0000300 dev->ops = info[i].ops;
Uwe Hermannd453dd02010-10-18 00:00:57 +0000301
Eric Biederman5cd81732004-03-11 15:01:31 +0000302 get_resources(dev, &info[i]);
Eric Biederman5cd81732004-03-11 15:01:31 +0000303 }
304}