Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 1 | /* |
Stefan Reinauer | 7e61e45 | 2008-01-18 10:35:56 +0000 | [diff] [blame] | 2 | * This file is part of the coreboot project. |
Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 3 | * |
| 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 |
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 23 | |
| 24 | #include <console/console.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <stdint.h> |
| 27 | #include <bitops.h> |
| 28 | #include <string.h> |
| 29 | #include <arch/io.h> |
| 30 | #include <device/device.h> |
| 31 | #include <device/pnp.h> |
| 32 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 33 | /* PNP fundamental operations */ |
| 34 | |
| 35 | void pnp_write_config(device_t dev, uint8_t reg, uint8_t value) |
| 36 | { |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 37 | outb(reg, dev->path.pnp.port); |
| 38 | outb(value, dev->path.pnp.port + 1); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | uint8_t pnp_read_config(device_t dev, uint8_t reg) |
| 42 | { |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 43 | outb(reg, dev->path.pnp.port); |
| 44 | return inb(dev->path.pnp.port + 1); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void pnp_set_logical_device(device_t dev) |
| 48 | { |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 49 | pnp_write_config(dev, 0x07, dev->path.pnp.device & 0xff); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void pnp_set_enable(device_t dev, int enable) |
| 53 | { |
Rudolf Marek | b05d4bb | 2008-02-19 20:30:25 +0000 | [diff] [blame] | 54 | uint8_t tmp, bitpos; |
Rudolf Marek | 623df67 | 2008-02-18 20:32:46 +0000 | [diff] [blame] | 55 | |
| 56 | tmp = pnp_read_config(dev, 0x30); |
| 57 | /* handle the virtual devices, which share same LDN register */ |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 58 | bitpos = (dev->path.pnp.device >> 8) & 0x7; |
Rudolf Marek | 623df67 | 2008-02-18 20:32:46 +0000 | [diff] [blame] | 59 | |
| 60 | if (enable) { |
| 61 | tmp |= (1 << bitpos); |
| 62 | } else { |
| 63 | tmp &= ~(1 << bitpos); |
| 64 | } |
| 65 | pnp_write_config(dev, 0x30, tmp); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | int pnp_read_enable(device_t dev) |
| 69 | { |
Rudolf Marek | b05d4bb | 2008-02-19 20:30:25 +0000 | [diff] [blame] | 70 | uint8_t tmp, bitpos; |
Rudolf Marek | 623df67 | 2008-02-18 20:32:46 +0000 | [diff] [blame] | 71 | tmp = pnp_read_config(dev, 0x30); |
| 72 | /* handle the virtual devices, which share same LDN register */ |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 73 | bitpos = (dev->path.pnp.device >> 8) & 0x7; |
Rudolf Marek | b05d4bb | 2008-02-19 20:30:25 +0000 | [diff] [blame] | 74 | return !!(tmp & (1 << bitpos)); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void pnp_set_iobase(device_t dev, unsigned index, unsigned iobase) |
| 78 | { |
| 79 | /* Index == 0x60 or 0x62 */ |
| 80 | pnp_write_config(dev, index + 0, (iobase >> 8) & 0xff); |
| 81 | pnp_write_config(dev, index + 1, iobase & 0xff); |
| 82 | } |
| 83 | |
| 84 | void pnp_set_irq(device_t dev, unsigned index, unsigned irq) |
| 85 | { |
| 86 | /* Index == 0x70 or 0x72 */ |
| 87 | pnp_write_config(dev, index, irq); |
| 88 | } |
| 89 | |
Steven J. Magnani | 740bb2a | 2005-09-09 20:02:52 +0000 | [diff] [blame] | 90 | void pnp_set_drq(device_t dev, unsigned index, unsigned drq) |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 91 | { |
| 92 | /* Index == 0x74 */ |
| 93 | pnp_write_config(dev, index, drq & 0xff); |
| 94 | } |
| 95 | |
| 96 | /* PNP device operations */ |
| 97 | |
| 98 | void pnp_read_resources(device_t dev) |
| 99 | { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | static void pnp_set_resource(device_t dev, struct resource *resource) |
| 104 | { |
| 105 | if (!(resource->flags & IORESOURCE_ASSIGNED)) { |
Myles Watson | c4ddbff | 2009-02-09 17:52:54 +0000 | [diff] [blame] | 106 | printk_err("ERROR: %s %02lx %s size: 0x%010Lx not assigned\n", |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 107 | dev_path(dev), resource->index, |
| 108 | resource_type(resource), |
| 109 | resource->size); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 110 | return; |
| 111 | } |
Li-Ta Lo | 75337f7 | 2004-05-07 21:56:48 +0000 | [diff] [blame] | 112 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 113 | /* Now store the resource */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 114 | if (resource->flags & IORESOURCE_IO) { |
| 115 | pnp_set_iobase(dev, resource->index, resource->base); |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 116 | } |
| 117 | else if (resource->flags & IORESOURCE_DRQ) { |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 118 | pnp_set_drq(dev, resource->index, resource->base); |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 119 | } |
| 120 | else if (resource->flags & IORESOURCE_IRQ) { |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 121 | pnp_set_irq(dev, resource->index, resource->base); |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 122 | } |
| 123 | else { |
Myles Watson | c4ddbff | 2009-02-09 17:52:54 +0000 | [diff] [blame] | 124 | printk_err("ERROR: %s %02lx unknown resource type\n", |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 125 | dev_path(dev), resource->index); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 126 | return; |
| 127 | } |
Li-Ta Lo | 9f0d0f9 | 2004-05-10 16:05:16 +0000 | [diff] [blame] | 128 | resource->flags |= IORESOURCE_STORED; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 129 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 130 | report_resource_stored(dev, resource, ""); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void pnp_set_resources(device_t dev) |
| 134 | { |
| 135 | int i; |
| 136 | |
| 137 | /* Select the device */ |
| 138 | pnp_set_logical_device(dev); |
| 139 | |
| 140 | /* Paranoia says I should disable the device here... */ |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 141 | for(i = 0; i < dev->resources; i++) { |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 142 | pnp_set_resource(dev, &dev->resource[i]); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void pnp_enable_resources(device_t dev) |
| 147 | { |
| 148 | pnp_set_logical_device(dev); |
| 149 | pnp_set_enable(dev, 1); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void pnp_enable(device_t dev) |
| 153 | { |
Li-Ta Lo | 75337f7 | 2004-05-07 21:56:48 +0000 | [diff] [blame] | 154 | if (!dev->enabled) { |
Li-Ta Lo | c6bcedb | 2004-04-21 16:57:05 +0000 | [diff] [blame] | 155 | pnp_set_logical_device(dev); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 156 | pnp_set_enable(dev, 0); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | struct device_operations pnp_ops = { |
| 161 | .read_resources = pnp_read_resources, |
| 162 | .set_resources = pnp_set_resources, |
| 163 | .enable_resources = pnp_enable_resources, |
| 164 | .enable = pnp_enable, |
| 165 | }; |
| 166 | |
| 167 | /* PNP chip opertations */ |
| 168 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 169 | static void pnp_get_ioresource(device_t dev, unsigned index, struct io_info *info) |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 170 | { |
| 171 | struct resource *resource; |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 172 | unsigned moving, gran, step; |
Li-Ta Lo | 75337f7 | 2004-05-07 21:56:48 +0000 | [diff] [blame] | 173 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 174 | resource = new_resource(dev, index); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 175 | |
| 176 | /* Initilize the resource */ |
| 177 | resource->limit = 0xffff; |
| 178 | resource->flags |= IORESOURCE_IO; |
| 179 | |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 180 | /* Get the resource size */ |
| 181 | moving = info->mask; |
| 182 | gran = 15; |
| 183 | step = 1 << gran; |
| 184 | /* Find the first bit that moves */ |
| 185 | while((moving & step) == 0) { |
| 186 | gran--; |
| 187 | step >>= 1; |
| 188 | } |
| 189 | /* Now find the first bit that does not move */ |
| 190 | while((moving & step) != 0) { |
| 191 | gran--; |
| 192 | step >>= 1; |
| 193 | } |
| 194 | /* Of the moving bits the last bit in the first group, |
| 195 | * tells us the size of this resource. |
| 196 | */ |
| 197 | if ((moving & step) == 0) { |
| 198 | gran++; |
| 199 | step <<= 1; |
| 200 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 201 | /* Set the resource size and alignment */ |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 202 | resource->gran = gran; |
| 203 | resource->align = gran; |
| 204 | resource->limit = info->mask | (step - 1); |
| 205 | resource->size = 1 << gran; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static void get_resources(device_t dev, struct pnp_info *info) |
| 209 | { |
| 210 | struct resource *resource; |
| 211 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 212 | if (info->flags & PNP_IO0) { |
| 213 | pnp_get_ioresource(dev, PNP_IDX_IO0, &info->io0); |
| 214 | } |
| 215 | if (info->flags & PNP_IO1) { |
| 216 | pnp_get_ioresource(dev, PNP_IDX_IO1, &info->io1); |
| 217 | } |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 218 | if (info->flags & PNP_IO2) { |
| 219 | pnp_get_ioresource(dev, PNP_IDX_IO2, &info->io2); |
| 220 | } |
| 221 | if (info->flags & PNP_IO3) { |
| 222 | pnp_get_ioresource(dev, PNP_IDX_IO3, &info->io3); |
| 223 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 224 | if (info->flags & PNP_IRQ0) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 225 | resource = new_resource(dev, PNP_IDX_IRQ0); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 226 | resource->size = 1; |
| 227 | resource->flags |= IORESOURCE_IRQ; |
| 228 | } |
| 229 | if (info->flags & PNP_IRQ1) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 230 | resource = new_resource(dev, PNP_IDX_IRQ1); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 231 | resource->size = 1; |
| 232 | resource->flags |= IORESOURCE_IRQ; |
| 233 | } |
| 234 | if (info->flags & PNP_DRQ0) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 235 | resource = new_resource(dev, PNP_IDX_DRQ0); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 236 | resource->size = 1; |
| 237 | resource->flags |= IORESOURCE_DRQ; |
| 238 | } |
| 239 | if (info->flags & PNP_DRQ1) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 240 | resource = new_resource(dev, PNP_IDX_DRQ1); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 241 | resource->size = 1; |
| 242 | resource->flags |= IORESOURCE_DRQ; |
Li-Ta Lo | 75337f7 | 2004-05-07 21:56:48 +0000 | [diff] [blame] | 243 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 246 | void pnp_enable_devices(device_t base_dev, struct device_operations *ops, |
| 247 | unsigned functions, struct pnp_info *info) |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 248 | { |
| 249 | struct device_path path; |
| 250 | device_t dev; |
| 251 | int i; |
| 252 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 253 | path.type = DEVICE_PATH_PNP; |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 254 | path.pnp.port = base_dev->path.pnp.port; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 255 | |
| 256 | /* Setup the ops and resources on the newly allocated devices */ |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 257 | for(i = 0; i < functions; i++) { |
Uwe Hermann | aa4bedf | 2007-07-12 13:12:47 +0000 | [diff] [blame] | 258 | /* Skip logical devices this Super I/O doesn't have. */ |
| 259 | if (info[i].function == -1) |
| 260 | continue; |
| 261 | |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 262 | path.pnp.device = info[i].function; |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 263 | dev = alloc_find_dev(base_dev->bus, &path); |
| 264 | |
| 265 | /* Don't initialize a device multiple times */ |
| 266 | if (dev->ops) |
| 267 | continue; |
Li-Ta Lo | c6bcedb | 2004-04-21 16:57:05 +0000 | [diff] [blame] | 268 | |
Li-Ta Lo | 75337f7 | 2004-05-07 21:56:48 +0000 | [diff] [blame] | 269 | if (info[i].ops == 0) { |
| 270 | dev->ops = ops; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 271 | } else { |
Li-Ta Lo | 75337f7 | 2004-05-07 21:56:48 +0000 | [diff] [blame] | 272 | dev->ops = info[i].ops; |
Li-Ta Lo | c6bcedb | 2004-04-21 16:57:05 +0000 | [diff] [blame] | 273 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 274 | get_resources(dev, &info[i]); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 275 | } |
| 276 | } |