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