#include <dev/aic7xxx/aic7xxx_osm.h>
#include <sys/limits.h>
#include <isa/isavar.h>
static struct aic7770_identity *ahc_isa_find_device(bus_space_tag_t tag,
bus_space_handle_t bsh);
static void ahc_isa_identify(driver_t *driver,
device_t parent);
static int ahc_isa_probe(device_t dev);
static int ahc_isa_attach(device_t dev);
static struct aic7770_identity *
ahc_isa_find_device(bus_space_tag_t tag, bus_space_handle_t bsh) {
uint32_t id;
u_int id_size;
int i;
id = 0;
id_size = sizeof(id);
for (i = 0; i < id_size; i++) {
bus_space_write_1(tag, bsh, 0x80, 0x80 + i);
id |= bus_space_read_1(tag, bsh, 0x80 + i)
<< ((id_size - i - 1) * CHAR_BIT);
}
return (aic7770_find_device(id));
}
static void
ahc_isa_identify(driver_t *driver, device_t parent)
{
int slot;
int max_slot;
max_slot = 14;
for (slot = 0; slot <= max_slot; slot++) {
struct aic7770_identity *entry;
bus_space_tag_t tag;
bus_space_handle_t bsh;
struct resource *regs;
uint32_t iobase;
int rid;
rid = 0;
iobase = (slot * AHC_EISA_SLOT_SIZE) + AHC_EISA_SLOT_OFFSET;
regs = bus_alloc_resource(parent, SYS_RES_IOPORT, &rid,
iobase, iobase, AHC_EISA_IOSIZE,
RF_ACTIVE);
if (regs == NULL) {
if (bootverbose)
printf("ahc_isa_identify %d: ioport 0x%x "
"alloc failed\n", slot, iobase);
continue;
}
tag = rman_get_bustag(regs);
bsh = rman_get_bushandle(regs);
entry = ahc_isa_find_device(tag, bsh);
if (entry != NULL) {
device_t child;
child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE,
"ahc", -1);
if (child != NULL) {
device_set_driver(child, driver);
bus_set_resource(child, SYS_RES_IOPORT,
0, iobase, AHC_EISA_IOSIZE);
}
}
bus_release_resource(parent, SYS_RES_IOPORT, rid, regs);
}
}
static int
ahc_isa_probe(device_t dev)
{
struct aic7770_identity *entry;
bus_space_tag_t tag;
bus_space_handle_t bsh;
struct resource *regs;
struct resource *irq;
uint32_t iobase;
u_int intdef;
u_int hcntrl;
int irq_num;
int error;
int zero;
error = ENXIO;
zero = 0;
regs = NULL;
irq = NULL;
if (isa_get_logicalid(dev) != 0)
return (error);
regs = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &zero, RF_ACTIVE);
if (regs == NULL) {
device_printf(dev, "No resources allocated.\n");
return (ENOMEM);
}
iobase = rman_get_start(regs);
tag = rman_get_bustag(regs);
bsh = rman_get_bushandle(regs);
entry = ahc_isa_find_device(tag, bsh);
if (entry == NULL)
goto cleanup;
hcntrl = bus_space_read_1(tag, bsh, HCNTRL) & IRQMS;
bus_space_write_1(tag, bsh, HCNTRL, hcntrl | PAUSE);
while ((bus_space_read_1(tag, bsh, HCNTRL) & PAUSE) == 0)
;
intdef = bus_space_read_1(tag, bsh, INTDEF);
irq_num = intdef & VECTOR;
switch (irq_num) {
case 9:
case 10:
case 11:
case 12:
case 14:
case 15:
break;
default:
device_printf(dev, "@0x%x: illegal irq setting %d\n",
iobase, irq_num);
goto cleanup;
}
if (bus_set_resource(dev, SYS_RES_IRQ, zero, irq_num, 1) != 0)
goto cleanup;
irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &zero,
0 );
if (irq != NULL) {
error = 0;
device_set_desc(dev, entry->name);
} else
device_printf(dev, "@0x%x: irq %d allocation failed\n",
iobase, irq_num);
cleanup:
if (regs != NULL) {
bus_release_resource(dev, SYS_RES_IOPORT, zero, regs);
regs = NULL;
}
if (irq != NULL) {
bus_release_resource(dev, SYS_RES_IRQ, zero, irq);
irq = NULL;
}
return (error);
}
static int
ahc_isa_attach(device_t dev)
{
struct aic7770_identity *entry;
bus_space_tag_t tag;
bus_space_handle_t bsh;
struct resource *regs;
struct ahc_softc *ahc;
char *name;
int zero;
int error;
zero = 0;
regs = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &zero, RF_ACTIVE);
if (regs == NULL)
return (ENOMEM);
tag = rman_get_bustag(regs);
bsh = rman_get_bushandle(regs);
entry = ahc_isa_find_device(tag, bsh);
bus_release_resource(dev, SYS_RES_IOPORT, zero, regs);
if (entry == NULL)
return (ENODEV);
name = malloc(strlen(device_get_nameunit(dev)) + 1, M_DEVBUF, M_NOWAIT);
if (name == NULL)
return (ENOMEM);
strcpy(name, device_get_nameunit(dev));
ahc = ahc_alloc(dev, name);
if (ahc == NULL)
return (ENOMEM);
ahc_set_unit(ahc, device_get_unit(dev));
error = aic_dma_tag_create(ahc, bus_get_dma_tag(dev),
1, 0,
BUS_SPACE_MAXADDR_32BIT,
BUS_SPACE_MAXADDR,
NULL, NULL,
BUS_SPACE_MAXSIZE_32BIT,
AHC_NSEG,
AHC_MAXTRANSFER_SIZE,
0,
&ahc->parent_dmat);
if (error != 0) {
printf("ahc_isa_attach: Could not allocate DMA tag "
"- error %d\n", error);
ahc_free(ahc);
return (ENOMEM);
}
ahc->dev_softc = dev;
error = aic7770_config(ahc, entry, 0);
if (error != 0) {
ahc_free(ahc);
return (error);
}
ahc_attach(ahc);
return (0);
}
static device_method_t ahc_isa_device_methods[] = {
DEVMETHOD(device_identify, ahc_isa_identify),
DEVMETHOD(device_probe, ahc_isa_probe),
DEVMETHOD(device_attach, ahc_isa_attach),
DEVMETHOD(device_detach, ahc_detach),
{ 0, 0 }
};
static driver_t ahc_isa_driver = {
"ahc",
ahc_isa_device_methods,
sizeof(struct ahc_softc)
};
DRIVER_MODULE(ahc_isa, isa, ahc_isa_driver, 0, 0);
MODULE_DEPEND(ahc_isa, ahc, 1, 1, 1);
MODULE_VERSION(ahc_isa, 1);