blob: 8acef3b65a2a998c78b68dcf48c8d8a4550c2536 [file] [log] [blame]
Eric Biederman5cd81732004-03-11 15:01:31 +00001/* 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 Biederman5cd81732004-03-11 15:01:31 +000013/* PNP fundamental operations */
14
15void 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
21uint8_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
27void pnp_set_logical_device(device_t dev)
28{
29 pnp_write_config(dev, 0x07, dev->path.u.pnp.device);
30}
31
32void pnp_set_enable(device_t dev, int enable)
33{
34 pnp_write_config(dev, 0x30, enable?0x1:0x0);
35}
36
37int pnp_read_enable(device_t dev)
38{
39 return !!pnp_read_config(dev, 0x30);
40}
41
42void 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
49void 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. Magnani740bb2a2005-09-09 20:02:52 +000055void pnp_set_drq(device_t dev, unsigned index, unsigned drq)
Eric Biederman5cd81732004-03-11 15:01:31 +000056{
57 /* Index == 0x74 */
58 pnp_write_config(dev, index, drq & 0xff);
59}
60
61/* PNP device operations */
62
63void pnp_read_resources(device_t dev)
64{
65 return;
66}
67
68static void pnp_set_resource(device_t dev, struct resource *resource)
69{
70 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000071 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 Biederman5cd81732004-03-11 15:01:31 +000075 return;
76 }
Li-Ta Lo75337f72004-05-07 21:56:48 +000077
Eric Biederman5cd81732004-03-11 15:01:31 +000078 /* Now store the resource */
Eric Biederman5cd81732004-03-11 15:01:31 +000079 if (resource->flags & IORESOURCE_IO) {
80 pnp_set_iobase(dev, resource->index, resource->base);
Eric Biedermanb78c1972004-10-14 20:54:17 +000081 }
82 else if (resource->flags & IORESOURCE_DRQ) {
Eric Biederman5cd81732004-03-11 15:01:31 +000083 pnp_set_drq(dev, resource->index, resource->base);
Eric Biedermanb78c1972004-10-14 20:54:17 +000084 }
85 else if (resource->flags & IORESOURCE_IRQ) {
Eric Biederman5cd81732004-03-11 15:01:31 +000086 pnp_set_irq(dev, resource->index, resource->base);
Eric Biedermanb78c1972004-10-14 20:54:17 +000087 }
88 else {
Eric Biederman5cd81732004-03-11 15:01:31 +000089 printk_err("ERROR: %s %02x unknown resource type\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +000090 dev_path(dev), resource->index);
Eric Biederman5cd81732004-03-11 15:01:31 +000091 return;
92 }
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +000093 resource->flags |= IORESOURCE_STORED;
Eric Biederman5cd81732004-03-11 15:01:31 +000094
Eric Biederman03acab62004-10-14 21:25:53 +000095 report_resource_stored(dev, resource, "");
Eric Biederman5cd81732004-03-11 15:01:31 +000096}
97
98void 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 Biedermanb78c1972004-10-14 20:54:17 +0000106 for(i = 0; i < dev->resources; i++) {
Eric Biederman5cd81732004-03-11 15:01:31 +0000107 pnp_set_resource(dev, &dev->resource[i]);
108 }
109}
110
111void pnp_enable_resources(device_t dev)
112{
113 pnp_set_logical_device(dev);
114 pnp_set_enable(dev, 1);
Eric Biederman5cd81732004-03-11 15:01:31 +0000115}
116
117void pnp_enable(device_t dev)
118{
Li-Ta Lo75337f72004-05-07 21:56:48 +0000119 if (!dev->enabled) {
Li-Ta Loc6bcedb2004-04-21 16:57:05 +0000120 pnp_set_logical_device(dev);
Eric Biederman5cd81732004-03-11 15:01:31 +0000121 pnp_set_enable(dev, 0);
122 }
123}
124
125struct 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 Biedermanb78c1972004-10-14 20:54:17 +0000134static void pnp_get_ioresource(device_t dev, unsigned index, struct io_info *info)
Eric Biederman5cd81732004-03-11 15:01:31 +0000135{
136 struct resource *resource;
Eric Biedermandbec2d42004-10-21 10:44:08 +0000137 unsigned moving, gran, step;
Li-Ta Lo75337f72004-05-07 21:56:48 +0000138
Eric Biederman03acab62004-10-14 21:25:53 +0000139 resource = new_resource(dev, index);
Eric Biederman5cd81732004-03-11 15:01:31 +0000140
141 /* Initilize the resource */
142 resource->limit = 0xffff;
143 resource->flags |= IORESOURCE_IO;
144
Eric Biedermandbec2d42004-10-21 10:44:08 +0000145 /* 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 Biederman5cd81732004-03-11 15:01:31 +0000166 /* Set the resource size and alignment */
Eric Biedermandbec2d42004-10-21 10:44:08 +0000167 resource->gran = gran;
168 resource->align = gran;
169 resource->limit = info->mask | (step - 1);
170 resource->size = 1 << gran;
Eric Biederman5cd81732004-03-11 15:01:31 +0000171}
172
173static void get_resources(device_t dev, struct pnp_info *info)
174{
175 struct resource *resource;
176
Eric Biederman5cd81732004-03-11 15:01:31 +0000177 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 Biedermanb78c1972004-10-14 20:54:17 +0000183 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 Biederman5cd81732004-03-11 15:01:31 +0000189 if (info->flags & PNP_IRQ0) {
Eric Biederman03acab62004-10-14 21:25:53 +0000190 resource = new_resource(dev, PNP_IDX_IRQ0);
Eric Biederman5cd81732004-03-11 15:01:31 +0000191 resource->size = 1;
192 resource->flags |= IORESOURCE_IRQ;
193 }
194 if (info->flags & PNP_IRQ1) {
Eric Biederman03acab62004-10-14 21:25:53 +0000195 resource = new_resource(dev, PNP_IDX_IRQ1);
Eric Biederman5cd81732004-03-11 15:01:31 +0000196 resource->size = 1;
197 resource->flags |= IORESOURCE_IRQ;
198 }
199 if (info->flags & PNP_DRQ0) {
Eric Biederman03acab62004-10-14 21:25:53 +0000200 resource = new_resource(dev, PNP_IDX_DRQ0);
Eric Biederman5cd81732004-03-11 15:01:31 +0000201 resource->size = 1;
202 resource->flags |= IORESOURCE_DRQ;
203 }
204 if (info->flags & PNP_DRQ1) {
Eric Biederman03acab62004-10-14 21:25:53 +0000205 resource = new_resource(dev, PNP_IDX_DRQ1);
Eric Biederman5cd81732004-03-11 15:01:31 +0000206 resource->size = 1;
207 resource->flags |= IORESOURCE_DRQ;
Li-Ta Lo75337f72004-05-07 21:56:48 +0000208 }
Eric Biederman5cd81732004-03-11 15:01:31 +0000209}
210
Eric Biederman7003ba42004-10-16 06:20:29 +0000211void pnp_enable_devices(device_t base_dev, struct device_operations *ops,
212 unsigned functions, struct pnp_info *info)
Eric Biederman5cd81732004-03-11 15:01:31 +0000213{
214 struct device_path path;
215 device_t dev;
216 int i;
217
Eric Biederman5cd81732004-03-11 15:01:31 +0000218 path.type = DEVICE_PATH_PNP;
Eric Biederman7003ba42004-10-16 06:20:29 +0000219 path.u.pnp.port = base_dev->path.u.pnp.port;
Eric Biederman5cd81732004-03-11 15:01:31 +0000220
221 /* Setup the ops and resources on the newly allocated devices */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000222 for(i = 0; i < functions; i++) {
Eric Biederman5cd81732004-03-11 15:01:31 +0000223 path.u.pnp.device = info[i].function;
Eric Biederman7003ba42004-10-16 06:20:29 +0000224 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 Loc6bcedb2004-04-21 16:57:05 +0000229
Li-Ta Lo75337f72004-05-07 21:56:48 +0000230 if (info[i].ops == 0) {
231 dev->ops = ops;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000232 } else {
Li-Ta Lo75337f72004-05-07 21:56:48 +0000233 dev->ops = info[i].ops;
Li-Ta Loc6bcedb2004-04-21 16:57:05 +0000234 }
Eric Biederman5cd81732004-03-11 15:01:31 +0000235 get_resources(dev, &info[i]);
Eric Biederman5cd81732004-03-11 15:01:31 +0000236 }
237}