2007-04-22 21:08:13 +02:00
|
|
|
/*
|
|
|
|
* This file is part of the LinuxBIOS project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004 Linux Networx
|
|
|
|
* (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
|
|
|
|
* Copyright (C) 2004 Li-Ta Lo <ollie@lanl.gov>
|
|
|
|
* Copyright (C) 2005 Tyan
|
|
|
|
* (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2004-03-11 16:01:31 +01:00
|
|
|
|
|
|
|
#include <console/console.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <bitops.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <arch/io.h>
|
|
|
|
#include <device/device.h>
|
|
|
|
#include <device/pnp.h>
|
|
|
|
|
|
|
|
/* PNP fundamental operations */
|
|
|
|
|
|
|
|
void pnp_write_config(device_t dev, uint8_t reg, uint8_t value)
|
|
|
|
{
|
|
|
|
outb(reg, dev->path.u.pnp.port);
|
|
|
|
outb(value, dev->path.u.pnp.port + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t pnp_read_config(device_t dev, uint8_t reg)
|
|
|
|
{
|
|
|
|
outb(reg, dev->path.u.pnp.port);
|
|
|
|
return inb(dev->path.u.pnp.port + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pnp_set_logical_device(device_t dev)
|
|
|
|
{
|
|
|
|
pnp_write_config(dev, 0x07, dev->path.u.pnp.device);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pnp_set_enable(device_t dev, int enable)
|
|
|
|
{
|
|
|
|
pnp_write_config(dev, 0x30, enable?0x1:0x0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int pnp_read_enable(device_t dev)
|
|
|
|
{
|
|
|
|
return !!pnp_read_config(dev, 0x30);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pnp_set_iobase(device_t dev, unsigned index, unsigned iobase)
|
|
|
|
{
|
|
|
|
/* Index == 0x60 or 0x62 */
|
|
|
|
pnp_write_config(dev, index + 0, (iobase >> 8) & 0xff);
|
|
|
|
pnp_write_config(dev, index + 1, iobase & 0xff);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pnp_set_irq(device_t dev, unsigned index, unsigned irq)
|
|
|
|
{
|
|
|
|
/* Index == 0x70 or 0x72 */
|
|
|
|
pnp_write_config(dev, index, irq);
|
|
|
|
}
|
|
|
|
|
2005-09-09 22:02:52 +02:00
|
|
|
void pnp_set_drq(device_t dev, unsigned index, unsigned drq)
|
2004-03-11 16:01:31 +01:00
|
|
|
{
|
|
|
|
/* Index == 0x74 */
|
|
|
|
pnp_write_config(dev, index, drq & 0xff);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PNP device operations */
|
|
|
|
|
|
|
|
void pnp_read_resources(device_t dev)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pnp_set_resource(device_t dev, struct resource *resource)
|
|
|
|
{
|
|
|
|
if (!(resource->flags & IORESOURCE_ASSIGNED)) {
|
2005-07-08 04:49:49 +02:00
|
|
|
printk_err("ERROR: %s %02x %s size: 0x%010Lx not assigned\n",
|
|
|
|
dev_path(dev), resource->index,
|
|
|
|
resource_type(resource),
|
|
|
|
resource->size);
|
2004-03-11 16:01:31 +01:00
|
|
|
return;
|
|
|
|
}
|
2004-05-07 23:56:48 +02:00
|
|
|
|
2004-03-11 16:01:31 +01:00
|
|
|
/* Now store the resource */
|
|
|
|
if (resource->flags & IORESOURCE_IO) {
|
|
|
|
pnp_set_iobase(dev, resource->index, resource->base);
|
2004-10-14 22:54:17 +02:00
|
|
|
}
|
|
|
|
else if (resource->flags & IORESOURCE_DRQ) {
|
2004-03-11 16:01:31 +01:00
|
|
|
pnp_set_drq(dev, resource->index, resource->base);
|
2004-10-14 22:54:17 +02:00
|
|
|
}
|
|
|
|
else if (resource->flags & IORESOURCE_IRQ) {
|
2004-03-11 16:01:31 +01:00
|
|
|
pnp_set_irq(dev, resource->index, resource->base);
|
2004-10-14 22:54:17 +02:00
|
|
|
}
|
|
|
|
else {
|
2004-03-11 16:01:31 +01:00
|
|
|
printk_err("ERROR: %s %02x unknown resource type\n",
|
2004-10-14 22:54:17 +02:00
|
|
|
dev_path(dev), resource->index);
|
2004-03-11 16:01:31 +01:00
|
|
|
return;
|
|
|
|
}
|
2004-05-10 18:05:16 +02:00
|
|
|
resource->flags |= IORESOURCE_STORED;
|
2004-03-11 16:01:31 +01:00
|
|
|
|
2004-10-14 23:25:53 +02:00
|
|
|
report_resource_stored(dev, resource, "");
|
2004-03-11 16:01:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void pnp_set_resources(device_t dev)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Select the device */
|
|
|
|
pnp_set_logical_device(dev);
|
|
|
|
|
|
|
|
/* Paranoia says I should disable the device here... */
|
2004-10-14 22:54:17 +02:00
|
|
|
for(i = 0; i < dev->resources; i++) {
|
2004-03-11 16:01:31 +01:00
|
|
|
pnp_set_resource(dev, &dev->resource[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pnp_enable_resources(device_t dev)
|
|
|
|
{
|
|
|
|
pnp_set_logical_device(dev);
|
|
|
|
pnp_set_enable(dev, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pnp_enable(device_t dev)
|
|
|
|
{
|
2004-05-07 23:56:48 +02:00
|
|
|
if (!dev->enabled) {
|
2004-04-21 18:57:05 +02:00
|
|
|
pnp_set_logical_device(dev);
|
2004-03-11 16:01:31 +01:00
|
|
|
pnp_set_enable(dev, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct device_operations pnp_ops = {
|
|
|
|
.read_resources = pnp_read_resources,
|
|
|
|
.set_resources = pnp_set_resources,
|
|
|
|
.enable_resources = pnp_enable_resources,
|
|
|
|
.enable = pnp_enable,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* PNP chip opertations */
|
|
|
|
|
2004-10-14 22:54:17 +02:00
|
|
|
static void pnp_get_ioresource(device_t dev, unsigned index, struct io_info *info)
|
2004-03-11 16:01:31 +01:00
|
|
|
{
|
|
|
|
struct resource *resource;
|
2004-10-21 12:44:08 +02:00
|
|
|
unsigned moving, gran, step;
|
2004-05-07 23:56:48 +02:00
|
|
|
|
2004-10-14 23:25:53 +02:00
|
|
|
resource = new_resource(dev, index);
|
2004-03-11 16:01:31 +01:00
|
|
|
|
|
|
|
/* Initilize the resource */
|
|
|
|
resource->limit = 0xffff;
|
|
|
|
resource->flags |= IORESOURCE_IO;
|
|
|
|
|
2004-10-21 12:44:08 +02:00
|
|
|
/* Get the resource size */
|
|
|
|
moving = info->mask;
|
|
|
|
gran = 15;
|
|
|
|
step = 1 << gran;
|
|
|
|
/* Find the first bit that moves */
|
|
|
|
while((moving & step) == 0) {
|
|
|
|
gran--;
|
|
|
|
step >>= 1;
|
|
|
|
}
|
|
|
|
/* Now find the first bit that does not move */
|
|
|
|
while((moving & step) != 0) {
|
|
|
|
gran--;
|
|
|
|
step >>= 1;
|
|
|
|
}
|
|
|
|
/* Of the moving bits the last bit in the first group,
|
|
|
|
* tells us the size of this resource.
|
|
|
|
*/
|
|
|
|
if ((moving & step) == 0) {
|
|
|
|
gran++;
|
|
|
|
step <<= 1;
|
|
|
|
}
|
2004-03-11 16:01:31 +01:00
|
|
|
/* Set the resource size and alignment */
|
2004-10-21 12:44:08 +02:00
|
|
|
resource->gran = gran;
|
|
|
|
resource->align = gran;
|
|
|
|
resource->limit = info->mask | (step - 1);
|
|
|
|
resource->size = 1 << gran;
|
2004-03-11 16:01:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void get_resources(device_t dev, struct pnp_info *info)
|
|
|
|
{
|
|
|
|
struct resource *resource;
|
|
|
|
|
|
|
|
if (info->flags & PNP_IO0) {
|
|
|
|
pnp_get_ioresource(dev, PNP_IDX_IO0, &info->io0);
|
|
|
|
}
|
|
|
|
if (info->flags & PNP_IO1) {
|
|
|
|
pnp_get_ioresource(dev, PNP_IDX_IO1, &info->io1);
|
|
|
|
}
|
2004-10-14 22:54:17 +02:00
|
|
|
if (info->flags & PNP_IO2) {
|
|
|
|
pnp_get_ioresource(dev, PNP_IDX_IO2, &info->io2);
|
|
|
|
}
|
|
|
|
if (info->flags & PNP_IO3) {
|
|
|
|
pnp_get_ioresource(dev, PNP_IDX_IO3, &info->io3);
|
|
|
|
}
|
2004-03-11 16:01:31 +01:00
|
|
|
if (info->flags & PNP_IRQ0) {
|
2004-10-14 23:25:53 +02:00
|
|
|
resource = new_resource(dev, PNP_IDX_IRQ0);
|
2004-03-11 16:01:31 +01:00
|
|
|
resource->size = 1;
|
|
|
|
resource->flags |= IORESOURCE_IRQ;
|
|
|
|
}
|
|
|
|
if (info->flags & PNP_IRQ1) {
|
2004-10-14 23:25:53 +02:00
|
|
|
resource = new_resource(dev, PNP_IDX_IRQ1);
|
2004-03-11 16:01:31 +01:00
|
|
|
resource->size = 1;
|
|
|
|
resource->flags |= IORESOURCE_IRQ;
|
|
|
|
}
|
|
|
|
if (info->flags & PNP_DRQ0) {
|
2004-10-14 23:25:53 +02:00
|
|
|
resource = new_resource(dev, PNP_IDX_DRQ0);
|
2004-03-11 16:01:31 +01:00
|
|
|
resource->size = 1;
|
|
|
|
resource->flags |= IORESOURCE_DRQ;
|
|
|
|
}
|
|
|
|
if (info->flags & PNP_DRQ1) {
|
2004-10-14 23:25:53 +02:00
|
|
|
resource = new_resource(dev, PNP_IDX_DRQ1);
|
2004-03-11 16:01:31 +01:00
|
|
|
resource->size = 1;
|
|
|
|
resource->flags |= IORESOURCE_DRQ;
|
2004-05-07 23:56:48 +02:00
|
|
|
}
|
2004-03-11 16:01:31 +01:00
|
|
|
}
|
|
|
|
|
2004-10-16 08:20:29 +02:00
|
|
|
void pnp_enable_devices(device_t base_dev, struct device_operations *ops,
|
|
|
|
unsigned functions, struct pnp_info *info)
|
2004-03-11 16:01:31 +01:00
|
|
|
{
|
|
|
|
struct device_path path;
|
|
|
|
device_t dev;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
path.type = DEVICE_PATH_PNP;
|
2004-10-16 08:20:29 +02:00
|
|
|
path.u.pnp.port = base_dev->path.u.pnp.port;
|
2004-03-11 16:01:31 +01:00
|
|
|
|
|
|
|
/* Setup the ops and resources on the newly allocated devices */
|
2004-10-14 22:54:17 +02:00
|
|
|
for(i = 0; i < functions; i++) {
|
2004-03-11 16:01:31 +01:00
|
|
|
path.u.pnp.device = info[i].function;
|
2004-10-16 08:20:29 +02:00
|
|
|
dev = alloc_find_dev(base_dev->bus, &path);
|
|
|
|
|
|
|
|
/* Don't initialize a device multiple times */
|
|
|
|
if (dev->ops)
|
|
|
|
continue;
|
2004-04-21 18:57:05 +02:00
|
|
|
|
2004-05-07 23:56:48 +02:00
|
|
|
if (info[i].ops == 0) {
|
|
|
|
dev->ops = ops;
|
2004-10-14 22:54:17 +02:00
|
|
|
} else {
|
2004-05-07 23:56:48 +02:00
|
|
|
dev->ops = info[i].ops;
|
2004-04-21 18:57:05 +02:00
|
|
|
}
|
2004-03-11 16:01:31 +01:00
|
|
|
get_resources(dev, &info[i]);
|
|
|
|
}
|
|
|
|
}
|