1999-11-18 07:21:51 +00:00
|
|
|
/*
|
2003-02-17 21:20:35 +00:00
|
|
|
* Copyright (c) 2003 M. Warner Losh. All Rights Reserved.
|
|
|
|
* Copyright (c) 2000,2001 Jonathan Chen. All rights reserved.
|
1999-11-18 07:21:51 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
2000-10-18 03:25:13 +00:00
|
|
|
* notice, this list of conditions, and the following disclaimer,
|
|
|
|
* without modification, immediately at the beginning of the file.
|
1999-11-18 07:21:51 +00:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
2000-10-18 03:25:13 +00:00
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
1999-11-18 07:21:51 +00:00
|
|
|
*
|
2000-10-18 03:25:13 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
1999-11-18 07:21:51 +00:00
|
|
|
*
|
2000-10-18 03:25:13 +00:00
|
|
|
* $FreeBSD$
|
1999-11-18 07:21:51 +00:00
|
|
|
*/
|
2000-10-18 03:25:13 +00:00
|
|
|
|
1999-11-18 07:21:51 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/kernel.h>
|
2002-03-07 08:10:42 +00:00
|
|
|
#include <sys/sysctl.h>
|
1999-11-18 07:21:51 +00:00
|
|
|
|
2000-10-18 03:25:13 +00:00
|
|
|
#include <sys/bus.h>
|
1999-11-18 07:21:51 +00:00
|
|
|
#include <machine/bus.h>
|
2000-10-18 03:25:13 +00:00
|
|
|
#include <sys/rman.h>
|
|
|
|
#include <machine/resource.h>
|
1999-11-18 07:21:51 +00:00
|
|
|
|
2002-02-27 05:09:14 +00:00
|
|
|
#include <sys/pciio.h>
|
2001-08-27 00:09:42 +00:00
|
|
|
#include <dev/pci/pcivar.h>
|
|
|
|
#include <dev/pci/pcireg.h>
|
2003-02-16 02:06:50 +00:00
|
|
|
#include <dev/pci/pci_private.h>
|
1999-11-18 07:21:51 +00:00
|
|
|
|
|
|
|
#include <dev/cardbus/cardbusreg.h>
|
|
|
|
#include <dev/cardbus/cardbusvar.h>
|
2000-10-18 03:25:13 +00:00
|
|
|
#include <dev/cardbus/cardbus_cis.h>
|
2002-05-30 17:38:00 +00:00
|
|
|
#include <dev/pccard/pccardvar.h>
|
1999-11-18 07:21:51 +00:00
|
|
|
|
2000-11-28 00:05:20 +00:00
|
|
|
#include "power_if.h"
|
2000-10-18 03:25:13 +00:00
|
|
|
#include "pcib_if.h"
|
1999-11-18 07:21:51 +00:00
|
|
|
|
2002-03-07 08:10:42 +00:00
|
|
|
/* sysctl vars */
|
|
|
|
SYSCTL_NODE(_hw, OID_AUTO, cardbus, CTLFLAG_RD, 0, "CardBus parameters");
|
|
|
|
|
2002-03-15 06:41:01 +00:00
|
|
|
int cardbus_debug = 0;
|
2002-03-07 08:10:42 +00:00
|
|
|
TUNABLE_INT("hw.cardbus.debug", &cardbus_debug);
|
|
|
|
SYSCTL_INT(_hw_cardbus, OID_AUTO, debug, CTLFLAG_RW,
|
|
|
|
&cardbus_debug, 0,
|
|
|
|
"CardBus debug");
|
|
|
|
|
2002-03-15 06:41:01 +00:00
|
|
|
int cardbus_cis_debug = 0;
|
2002-03-07 08:10:42 +00:00
|
|
|
TUNABLE_INT("hw.cardbus.cis_debug", &cardbus_cis_debug);
|
|
|
|
SYSCTL_INT(_hw_cardbus, OID_AUTO, cis_debug, CTLFLAG_RW,
|
|
|
|
&cardbus_cis_debug, 0,
|
|
|
|
"CardBus CIS debug");
|
|
|
|
|
|
|
|
#define DPRINTF(a) if (cardbus_debug) printf a
|
|
|
|
#define DEVPRINTF(x) if (cardbus_debug) device_printf x
|
1999-11-18 07:21:51 +00:00
|
|
|
|
2002-10-07 23:00:51 +00:00
|
|
|
|
|
|
|
static struct resource *cardbus_alloc_resource(device_t cbdev, device_t child,
|
|
|
|
int type, int *rid, u_long start, u_long end, u_long count,
|
|
|
|
u_int flags);
|
2001-08-26 23:55:34 +00:00
|
|
|
static int cardbus_attach(device_t cbdev);
|
|
|
|
static int cardbus_attach_card(device_t cbdev);
|
2002-10-07 23:00:51 +00:00
|
|
|
static void cardbus_delete_resource(device_t cbdev, device_t child,
|
|
|
|
int type, int rid);
|
|
|
|
static void cardbus_delete_resource_method(device_t cbdev, device_t child,
|
|
|
|
int type, int rid);
|
|
|
|
static int cardbus_detach(device_t cbdev);
|
2002-11-02 23:00:28 +00:00
|
|
|
static int cardbus_detach_card(device_t cbdev);
|
2002-10-07 23:00:51 +00:00
|
|
|
static void cardbus_device_setup_regs(device_t brdev, int b, int s, int f,
|
2002-03-15 06:41:01 +00:00
|
|
|
pcicfgregs *cfg);
|
2002-10-07 23:00:51 +00:00
|
|
|
static void cardbus_driver_added(device_t cbdev, driver_t *driver);
|
2002-03-15 06:41:01 +00:00
|
|
|
static int cardbus_get_resource(device_t cbdev, device_t child, int type,
|
|
|
|
int rid, u_long *startp, u_long *countp);
|
|
|
|
static int cardbus_get_resource_method(device_t cbdev, device_t child,
|
|
|
|
int type, int rid, u_long *startp, u_long *countp);
|
2002-10-07 23:00:51 +00:00
|
|
|
static int cardbus_probe(device_t cbdev);
|
|
|
|
static int cardbus_read_ivar(device_t cbdev, device_t child, int which,
|
2003-02-16 02:06:50 +00:00
|
|
|
uintptr_t *result);
|
2001-08-26 23:55:34 +00:00
|
|
|
static void cardbus_release_all_resources(device_t cbdev,
|
2001-08-27 00:09:42 +00:00
|
|
|
struct cardbus_devinfo *dinfo);
|
2002-03-15 06:41:01 +00:00
|
|
|
static int cardbus_release_resource(device_t cbdev, device_t child,
|
|
|
|
int type, int rid, struct resource *r);
|
2002-10-07 23:00:51 +00:00
|
|
|
static int cardbus_set_resource(device_t cbdev, device_t child, int type,
|
|
|
|
int rid, u_long start, u_long count, struct resource *res);
|
|
|
|
static int cardbus_set_resource_method(device_t cbdev, device_t child,
|
|
|
|
int type, int rid, u_long start, u_long count);
|
2002-03-15 06:41:01 +00:00
|
|
|
static int cardbus_setup_intr(device_t cbdev, device_t child,
|
|
|
|
struct resource *irq, int flags, driver_intr_t *intr,
|
|
|
|
void *arg, void **cookiep);
|
|
|
|
static int cardbus_teardown_intr(device_t cbdev, device_t child,
|
|
|
|
struct resource *irq, void *cookie);
|
2002-10-07 23:00:51 +00:00
|
|
|
static int cardbus_write_ivar(device_t cbdev, device_t child, int which,
|
|
|
|
uintptr_t value);
|
2000-10-18 03:25:13 +00:00
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/* Probe/Attach */
|
|
|
|
/************************************************************************/
|
1999-11-18 07:21:51 +00:00
|
|
|
|
2000-10-18 03:25:13 +00:00
|
|
|
static int
|
2001-08-26 23:55:34 +00:00
|
|
|
cardbus_probe(device_t cbdev)
|
2000-10-18 03:25:13 +00:00
|
|
|
{
|
2001-12-15 05:58:28 +00:00
|
|
|
device_set_desc(cbdev, "CardBus bus");
|
1999-11-18 07:21:51 +00:00
|
|
|
return 0;
|
2000-10-18 03:25:13 +00:00
|
|
|
}
|
1999-11-18 07:21:51 +00:00
|
|
|
|
2000-10-18 03:25:13 +00:00
|
|
|
static int
|
2001-08-26 23:55:34 +00:00
|
|
|
cardbus_attach(device_t cbdev)
|
2000-10-18 03:25:13 +00:00
|
|
|
{
|
1999-11-18 07:21:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-11-28 00:05:20 +00:00
|
|
|
static int
|
2001-08-26 23:55:34 +00:00
|
|
|
cardbus_detach(device_t cbdev)
|
2000-11-28 00:05:20 +00:00
|
|
|
{
|
2002-11-02 23:00:28 +00:00
|
|
|
cardbus_detach_card(cbdev);
|
2000-11-28 00:05:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-12-15 05:58:28 +00:00
|
|
|
static int
|
|
|
|
cardbus_suspend(device_t self)
|
|
|
|
{
|
2002-11-02 23:00:28 +00:00
|
|
|
cardbus_detach_card(self);
|
2001-12-15 05:58:28 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cardbus_resume(device_t self)
|
|
|
|
{
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2000-10-18 03:25:13 +00:00
|
|
|
/************************************************************************/
|
|
|
|
/* Attach/Detach card */
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
static void
|
2002-10-07 23:00:51 +00:00
|
|
|
cardbus_device_setup_regs(device_t brdev, int b, int s, int f, pcicfgregs *cfg)
|
1999-11-18 07:21:51 +00:00
|
|
|
{
|
2001-08-26 23:55:34 +00:00
|
|
|
PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_INTLINE,
|
|
|
|
pci_get_irq(device_get_parent(brdev)), 1);
|
|
|
|
cfg->intline = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_INTLINE, 1);
|
2000-11-28 00:52:40 +00:00
|
|
|
|
2001-08-26 23:55:34 +00:00
|
|
|
PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_CACHELNSZ, 0x08, 1);
|
|
|
|
cfg->cachelnsz = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_CACHELNSZ, 1);
|
2000-10-18 03:25:13 +00:00
|
|
|
|
2001-08-26 23:55:34 +00:00
|
|
|
PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_LATTIMER, 0xa8, 1);
|
|
|
|
cfg->lattimer = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_LATTIMER, 1);
|
2000-10-18 03:25:13 +00:00
|
|
|
|
2001-08-26 23:55:34 +00:00
|
|
|
PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_MINGNT, 0x14, 1);
|
|
|
|
cfg->mingnt = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_MINGNT, 1);
|
2000-10-18 03:25:13 +00:00
|
|
|
|
2001-08-26 23:55:34 +00:00
|
|
|
PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_MAXLAT, 0x14, 1);
|
|
|
|
cfg->maxlat = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_MAXLAT, 1);
|
1999-11-18 07:21:51 +00:00
|
|
|
}
|
|
|
|
|
2000-10-18 03:25:13 +00:00
|
|
|
static int
|
2001-08-26 23:55:34 +00:00
|
|
|
cardbus_attach_card(device_t cbdev)
|
1999-11-18 07:21:51 +00:00
|
|
|
{
|
2001-08-26 23:55:34 +00:00
|
|
|
device_t brdev = device_get_parent(cbdev);
|
2000-10-18 03:25:13 +00:00
|
|
|
int cardattached = 0;
|
|
|
|
static int curr_bus_number = 2; /* XXX EVILE BAD (see below) */
|
|
|
|
int bus, slot, func;
|
|
|
|
|
2002-11-02 23:00:28 +00:00
|
|
|
cardbus_detach_card(cbdev); /* detach existing cards */
|
2000-10-18 03:25:13 +00:00
|
|
|
|
2001-08-26 23:55:34 +00:00
|
|
|
POWER_ENABLE_SOCKET(brdev, cbdev);
|
|
|
|
bus = pcib_get_bus(cbdev);
|
2000-10-18 03:25:13 +00:00
|
|
|
if (bus == 0) {
|
|
|
|
/*
|
|
|
|
* XXX EVILE BAD XXX
|
|
|
|
* Not all BIOSes initialize the secondary bus number properly,
|
|
|
|
* so if the default is bad, we just put one in and hope it
|
|
|
|
* works.
|
|
|
|
*/
|
|
|
|
bus = curr_bus_number;
|
2001-08-26 23:55:34 +00:00
|
|
|
pci_write_config(brdev, PCIR_SECBUS_2, curr_bus_number, 1);
|
|
|
|
pci_write_config(brdev, PCIR_SUBBUS_2, curr_bus_number + 2, 1);
|
2000-10-18 03:25:13 +00:00
|
|
|
curr_bus_number += 3;
|
|
|
|
}
|
2001-08-26 23:55:34 +00:00
|
|
|
/* For each function, set it up and try to attach a driver to it */
|
2000-10-18 03:25:13 +00:00
|
|
|
for (slot = 0; slot <= CARDBUS_SLOTMAX; slot++) {
|
2002-03-15 06:41:01 +00:00
|
|
|
int cardbusfunchigh = 0;
|
2000-10-18 03:25:13 +00:00
|
|
|
for (func = 0; func <= cardbusfunchigh; func++) {
|
2003-02-16 02:06:50 +00:00
|
|
|
struct cardbus_devinfo *dinfo;
|
2002-03-15 06:41:01 +00:00
|
|
|
|
2003-02-16 02:06:50 +00:00
|
|
|
dinfo = (struct cardbus_devinfo *)
|
|
|
|
pci_read_device(brdev, bus, slot, func,
|
|
|
|
sizeof(struct cardbus_devinfo));
|
2001-08-26 23:55:34 +00:00
|
|
|
if (dinfo == NULL)
|
|
|
|
continue;
|
2002-02-27 05:09:14 +00:00
|
|
|
if (dinfo->pci.cfg.mfdev)
|
2000-10-18 03:25:13 +00:00
|
|
|
cardbusfunchigh = CARDBUS_FUNCMAX;
|
2002-11-02 23:00:28 +00:00
|
|
|
|
2002-10-07 23:00:51 +00:00
|
|
|
cardbus_device_setup_regs(brdev, bus, slot, func,
|
|
|
|
&dinfo->pci.cfg);
|
2002-03-15 06:41:01 +00:00
|
|
|
dinfo->pci.cfg.dev = device_add_child(cbdev, NULL, -1);
|
|
|
|
if (!dinfo->pci.cfg.dev) {
|
2001-08-26 23:55:34 +00:00
|
|
|
DEVPRINTF((cbdev, "Cannot add child!\n"));
|
2003-02-16 02:06:50 +00:00
|
|
|
pci_freecfg((struct pci_devinfo *)dinfo);
|
2000-10-18 03:25:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
2002-03-15 06:41:01 +00:00
|
|
|
resource_list_init(&dinfo->pci.resources);
|
|
|
|
device_set_ivars(dinfo->pci.cfg.dev, dinfo);
|
|
|
|
cardbus_do_cis(cbdev, dinfo->pci.cfg.dev);
|
2003-02-17 04:13:44 +00:00
|
|
|
pci_print_verbose(&dinfo->pci);
|
2002-11-02 23:00:28 +00:00
|
|
|
if (device_probe_and_attach(dinfo->pci.cfg.dev) != 0)
|
2001-08-27 00:09:42 +00:00
|
|
|
cardbus_release_all_resources(cbdev, dinfo);
|
2002-11-02 23:00:28 +00:00
|
|
|
else
|
2000-10-18 03:25:13 +00:00
|
|
|
cardattached++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-26 23:55:34 +00:00
|
|
|
if (cardattached > 0)
|
2002-11-02 23:00:28 +00:00
|
|
|
return (0);
|
2001-08-26 23:55:34 +00:00
|
|
|
POWER_DISABLE_SOCKET(brdev, cbdev);
|
2002-11-02 23:00:28 +00:00
|
|
|
return (ENOENT);
|
1999-11-18 07:21:51 +00:00
|
|
|
}
|
|
|
|
|
2000-10-18 03:25:13 +00:00
|
|
|
static int
|
2002-11-02 23:00:28 +00:00
|
|
|
cardbus_detach_card(device_t cbdev)
|
1999-11-18 07:21:51 +00:00
|
|
|
{
|
2000-10-18 03:25:13 +00:00
|
|
|
int numdevs;
|
|
|
|
device_t *devlist;
|
|
|
|
int tmp;
|
2001-08-26 23:55:34 +00:00
|
|
|
int err = 0;
|
2000-10-18 03:25:13 +00:00
|
|
|
|
2001-08-26 23:55:34 +00:00
|
|
|
device_get_children(cbdev, &devlist, &numdevs);
|
1999-11-18 07:21:51 +00:00
|
|
|
|
2000-10-18 03:25:13 +00:00
|
|
|
if (numdevs == 0) {
|
2001-02-08 21:47:45 +00:00
|
|
|
free(devlist, M_TEMP);
|
2002-11-02 23:00:28 +00:00
|
|
|
return (ENOENT);
|
2000-10-18 03:25:13 +00:00
|
|
|
}
|
1999-11-18 07:21:51 +00:00
|
|
|
|
2000-10-18 03:25:13 +00:00
|
|
|
for (tmp = 0; tmp < numdevs; tmp++) {
|
2002-03-15 06:41:01 +00:00
|
|
|
struct cardbus_devinfo *dinfo = device_get_ivars(devlist[tmp]);
|
|
|
|
int status = device_get_state(devlist[tmp]);
|
2001-01-06 18:04:55 +00:00
|
|
|
|
2002-11-14 05:12:02 +00:00
|
|
|
if (dinfo->pci.cfg.dev != devlist[tmp])
|
|
|
|
device_printf(cbdev, "devinfo dev mismatch\n");
|
|
|
|
if (status == DS_ATTACHED || status == DS_BUSY)
|
|
|
|
device_detach(devlist[tmp]);
|
|
|
|
cardbus_release_all_resources(cbdev, dinfo);
|
|
|
|
device_delete_child(cbdev, devlist[tmp]);
|
2003-02-16 02:06:50 +00:00
|
|
|
pci_freecfg((struct pci_devinfo *)dinfo);
|
2000-10-18 03:25:13 +00:00
|
|
|
}
|
2002-11-02 23:00:28 +00:00
|
|
|
POWER_DISABLE_SOCKET(device_get_parent(cbdev), cbdev);
|
2001-02-08 21:47:45 +00:00
|
|
|
free(devlist, M_TEMP);
|
2002-11-02 23:00:28 +00:00
|
|
|
return (err);
|
2000-10-18 03:25:13 +00:00
|
|
|
}
|
1999-11-18 07:21:51 +00:00
|
|
|
|
2000-11-28 00:05:20 +00:00
|
|
|
static void
|
2001-08-26 23:55:34 +00:00
|
|
|
cardbus_driver_added(device_t cbdev, driver_t *driver)
|
2000-11-28 00:05:20 +00:00
|
|
|
{
|
2001-01-06 18:04:55 +00:00
|
|
|
int numdevs;
|
|
|
|
device_t *devlist;
|
2003-02-11 05:31:35 +00:00
|
|
|
device_t dev;
|
|
|
|
int i;
|
2002-11-02 23:00:28 +00:00
|
|
|
struct cardbus_devinfo *dinfo;
|
2001-01-06 18:04:55 +00:00
|
|
|
|
2001-08-26 23:55:34 +00:00
|
|
|
DEVICE_IDENTIFY(driver, cbdev);
|
2003-02-11 05:31:35 +00:00
|
|
|
device_get_children(cbdev, &devlist, &numdevs);
|
2003-02-12 05:57:02 +00:00
|
|
|
/*
|
|
|
|
* If there are no drivers attached, but there are children,
|
|
|
|
* then power the card up.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < numdevs; i++) {
|
|
|
|
dev = devlist[i];
|
|
|
|
if (device_get_state(dev) != DS_NOTPRESENT)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i > 0 && i == numdevs)
|
|
|
|
POWER_ENABLE_SOCKET(device_get_parent(cbdev), cbdev);
|
2003-02-11 05:31:35 +00:00
|
|
|
for (i = 0; i < numdevs; i++) {
|
|
|
|
dev = devlist[i];
|
|
|
|
if (device_get_state(dev) != DS_NOTPRESENT)
|
|
|
|
continue;
|
|
|
|
dinfo = device_get_ivars(dev);
|
2003-02-16 02:06:50 +00:00
|
|
|
pci_print_verbose(&dinfo->pci);
|
2003-02-11 05:31:35 +00:00
|
|
|
resource_list_init(&dinfo->pci.resources);
|
|
|
|
cardbus_do_cis(cbdev, dev);
|
|
|
|
if (device_probe_and_attach(dev) != 0)
|
|
|
|
cardbus_release_all_resources(cbdev, dinfo);
|
2001-01-06 18:04:55 +00:00
|
|
|
}
|
2001-02-08 21:47:45 +00:00
|
|
|
free(devlist, M_TEMP);
|
2000-11-28 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
2000-10-18 03:25:13 +00:00
|
|
|
/************************************************************************/
|
|
|
|
/* Resources */
|
|
|
|
/************************************************************************/
|
1999-11-18 07:21:51 +00:00
|
|
|
|
2002-03-15 06:41:01 +00:00
|
|
|
static int
|
|
|
|
cardbus_set_resource(device_t cbdev, device_t child, int type, int rid,
|
|
|
|
u_long start, u_long count, struct resource *res)
|
|
|
|
{
|
|
|
|
struct cardbus_devinfo *dinfo;
|
|
|
|
struct resource_list *rl;
|
|
|
|
struct resource_list_entry *rle;
|
|
|
|
|
|
|
|
if (device_get_parent(child) != cbdev)
|
|
|
|
return ENOENT;
|
|
|
|
|
|
|
|
dinfo = device_get_ivars(child);
|
|
|
|
rl = &dinfo->pci.resources;
|
|
|
|
rle = resource_list_find(rl, type, rid);
|
|
|
|
if (rle == NULL) {
|
|
|
|
resource_list_add(rl, type, rid, start, start + count - 1,
|
|
|
|
count);
|
|
|
|
if (res != NULL) {
|
|
|
|
rle = resource_list_find(rl, type, rid);
|
|
|
|
rle->res = res;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (rle->res == NULL) {
|
2003-02-12 05:54:22 +00:00
|
|
|
} else if (rman_get_device(rle->res) == cbdev &&
|
2002-03-15 06:41:01 +00:00
|
|
|
(!(rman_get_flags(rle->res) & RF_ACTIVE))) {
|
|
|
|
int f;
|
|
|
|
f = rman_get_flags(rle->res);
|
|
|
|
bus_release_resource(cbdev, type, rid, res);
|
|
|
|
rle->res = bus_alloc_resource(cbdev, type, &rid,
|
|
|
|
start, start + count - 1,
|
|
|
|
count, f);
|
|
|
|
} else {
|
|
|
|
device_printf(cbdev, "set_resource: resource busy\n");
|
|
|
|
return EBUSY;
|
|
|
|
}
|
|
|
|
rle->start = start;
|
|
|
|
rle->end = start + count - 1;
|
|
|
|
rle->count = count;
|
|
|
|
if (res != NULL)
|
|
|
|
rle->res = res;
|
|
|
|
}
|
|
|
|
if (device_get_parent(child) == cbdev)
|
|
|
|
pci_write_config(child, rid, start, 4);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cardbus_get_resource(device_t cbdev, device_t child, int type, int rid,
|
|
|
|
u_long *startp, u_long *countp)
|
|
|
|
{
|
|
|
|
struct cardbus_devinfo *dinfo;
|
|
|
|
struct resource_list *rl;
|
|
|
|
struct resource_list_entry *rle;
|
|
|
|
|
|
|
|
if (device_get_parent(child) != cbdev)
|
|
|
|
return ENOENT;
|
|
|
|
|
|
|
|
dinfo = device_get_ivars(child);
|
|
|
|
rl = &dinfo->pci.resources;
|
|
|
|
rle = resource_list_find(rl, type, rid);
|
|
|
|
if (!rle)
|
|
|
|
return ENOENT;
|
|
|
|
if (startp)
|
|
|
|
*startp = rle->start;
|
|
|
|
if (countp)
|
|
|
|
*countp = rle->count;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cardbus_delete_resource(device_t cbdev, device_t child, int type, int rid)
|
|
|
|
{
|
|
|
|
struct cardbus_devinfo *dinfo;
|
|
|
|
struct resource_list *rl;
|
|
|
|
struct resource_list_entry *rle;
|
|
|
|
|
|
|
|
if (device_get_parent(child) != cbdev)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dinfo = device_get_ivars(child);
|
|
|
|
rl = &dinfo->pci.resources;
|
|
|
|
rle = resource_list_find(rl, type, rid);
|
|
|
|
if (rle) {
|
|
|
|
if (rle->res) {
|
2003-02-12 05:54:22 +00:00
|
|
|
if (rman_get_device(rle->res) != cbdev ||
|
2002-03-15 06:41:01 +00:00
|
|
|
rman_get_flags(rle->res) & RF_ACTIVE) {
|
|
|
|
device_printf(cbdev, "delete_resource: "
|
|
|
|
"Resource still owned by child, oops. "
|
|
|
|
"(type=%d, rid=%d, addr=%lx)\n",
|
|
|
|
rle->type, rle->rid,
|
|
|
|
rman_get_start(rle->res));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bus_release_resource(cbdev, type, rid, rle->res);
|
|
|
|
}
|
|
|
|
resource_list_delete(rl, type, rid);
|
|
|
|
}
|
|
|
|
if (device_get_parent(child) == cbdev)
|
|
|
|
pci_write_config(child, rid, 0, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cardbus_set_resource_method(device_t cbdev, device_t child, int type, int rid,
|
|
|
|
u_long start, u_long count)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = cardbus_set_resource(cbdev, child, type, rid, start, count, NULL);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
return BUS_SET_RESOURCE(device_get_parent(cbdev), child, type, rid,
|
|
|
|
start, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cardbus_get_resource_method(device_t cbdev, device_t child, int type, int rid,
|
|
|
|
u_long *startp, u_long *countp)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = cardbus_get_resource(cbdev, child, type, rid, startp, countp);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
return BUS_GET_RESOURCE(device_get_parent(cbdev), child, type, rid,
|
|
|
|
startp, countp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cardbus_delete_resource_method(device_t cbdev, device_t child,
|
|
|
|
int type, int rid)
|
|
|
|
{
|
|
|
|
cardbus_delete_resource(cbdev, child, type, rid);
|
|
|
|
BUS_DELETE_RESOURCE(device_get_parent(cbdev), child, type, rid);
|
|
|
|
}
|
|
|
|
|
2000-10-18 03:25:13 +00:00
|
|
|
static void
|
2001-08-27 00:09:42 +00:00
|
|
|
cardbus_release_all_resources(device_t cbdev, struct cardbus_devinfo *dinfo)
|
2000-10-18 03:25:13 +00:00
|
|
|
{
|
2002-03-15 06:41:01 +00:00
|
|
|
struct resource_list_entry *rle;
|
|
|
|
|
|
|
|
/* Free all allocated resources */
|
|
|
|
SLIST_FOREACH(rle, &dinfo->pci.resources, link) {
|
|
|
|
if (rle->res) {
|
2003-02-12 05:54:22 +00:00
|
|
|
if (rman_get_device(rle->res) != cbdev)
|
2002-03-15 06:41:01 +00:00
|
|
|
device_printf(cbdev, "release_all_resource: "
|
|
|
|
"Resource still owned by child, oops. "
|
|
|
|
"(type=%d, rid=%d, addr=%lx)\n",
|
|
|
|
rle->type, rle->rid,
|
|
|
|
rman_get_start(rle->res));
|
|
|
|
BUS_RELEASE_RESOURCE(device_get_parent(cbdev),
|
2003-02-12 05:54:22 +00:00
|
|
|
cbdev, rle->type, rle->rid, rle->res);
|
2002-03-15 06:41:01 +00:00
|
|
|
rle->res = NULL;
|
|
|
|
/*
|
|
|
|
* zero out config so the card won't acknowledge
|
|
|
|
* access to the space anymore
|
|
|
|
*/
|
|
|
|
pci_write_config(dinfo->pci.cfg.dev, rle->rid, 0, 4);
|
|
|
|
}
|
|
|
|
}
|
2002-02-27 05:09:14 +00:00
|
|
|
resource_list_free(&dinfo->pci.resources);
|
2001-08-27 00:09:42 +00:00
|
|
|
}
|
|
|
|
|
2002-03-15 06:41:01 +00:00
|
|
|
static struct resource *
|
|
|
|
cardbus_alloc_resource(device_t cbdev, device_t child, int type,
|
|
|
|
int *rid, u_long start, u_long end, u_long count, u_int flags)
|
|
|
|
{
|
|
|
|
struct cardbus_devinfo *dinfo;
|
|
|
|
struct resource_list_entry *rle = 0;
|
|
|
|
int passthrough = (device_get_parent(child) != cbdev);
|
|
|
|
|
|
|
|
if (passthrough) {
|
|
|
|
return (BUS_ALLOC_RESOURCE(device_get_parent(cbdev), child,
|
|
|
|
type, rid, start, end, count, flags));
|
|
|
|
}
|
|
|
|
|
|
|
|
dinfo = device_get_ivars(child);
|
|
|
|
rle = resource_list_find(&dinfo->pci.resources, type, *rid);
|
|
|
|
|
|
|
|
if (!rle)
|
|
|
|
return NULL; /* no resource of that type/rid */
|
|
|
|
|
|
|
|
if (!rle->res) {
|
|
|
|
device_printf(cbdev, "WARNING: Resource not reserved by bus\n");
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
/* Release the cardbus hold on the resource */
|
2003-02-12 05:54:22 +00:00
|
|
|
if (rman_get_device(rle->res) != cbdev)
|
2002-03-15 06:41:01 +00:00
|
|
|
return NULL;
|
|
|
|
bus_release_resource(cbdev, type, *rid, rle->res);
|
|
|
|
rle->res = NULL;
|
|
|
|
switch (type) {
|
|
|
|
case SYS_RES_IOPORT:
|
|
|
|
case SYS_RES_MEMORY:
|
|
|
|
if (!(flags & RF_ALIGNMENT_MASK))
|
|
|
|
flags |= rman_make_alignment_flags(rle->count);
|
|
|
|
break;
|
|
|
|
case SYS_RES_IRQ:
|
|
|
|
flags |= RF_SHAREABLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Allocate the resource to the child */
|
|
|
|
return resource_list_alloc(&dinfo->pci.resources, cbdev, child,
|
|
|
|
type, rid, rle->start, rle->end, rle->count, flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cardbus_release_resource(device_t cbdev, device_t child, int type, int rid,
|
|
|
|
struct resource *r)
|
|
|
|
{
|
|
|
|
struct cardbus_devinfo *dinfo;
|
|
|
|
int passthrough = (device_get_parent(child) != cbdev);
|
|
|
|
struct resource_list_entry *rle = 0;
|
|
|
|
int flags;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (passthrough) {
|
|
|
|
return BUS_RELEASE_RESOURCE(device_get_parent(cbdev), child,
|
|
|
|
type, rid, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
dinfo = device_get_ivars(child);
|
|
|
|
/*
|
|
|
|
* According to the PCI 2.2 spec, devices may share an address
|
|
|
|
* decoder between memory mapped ROM access and memory
|
|
|
|
* mapped register access. To be safe, disable ROM access
|
|
|
|
* whenever it is released.
|
|
|
|
*/
|
|
|
|
if (rid == CARDBUS_ROM_REG) {
|
|
|
|
uint32_t rom_reg;
|
|
|
|
|
|
|
|
rom_reg = pci_read_config(child, rid, 4);
|
|
|
|
rom_reg &= ~CARDBUS_ROM_ENABLE;
|
|
|
|
pci_write_config(child, rid, rom_reg, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
rle = resource_list_find(&dinfo->pci.resources, type, rid);
|
|
|
|
|
|
|
|
if (!rle) {
|
|
|
|
device_printf(cbdev, "Allocated resource not found\n");
|
|
|
|
return ENOENT;
|
|
|
|
}
|
|
|
|
if (!rle->res) {
|
|
|
|
device_printf(cbdev, "Allocated resource not recorded\n");
|
|
|
|
return ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = BUS_RELEASE_RESOURCE(device_get_parent(cbdev), child,
|
|
|
|
type, rid, r);
|
|
|
|
switch (type) {
|
|
|
|
case SYS_RES_IOPORT:
|
|
|
|
case SYS_RES_MEMORY:
|
|
|
|
flags = rman_make_alignment_flags(rle->count);
|
|
|
|
break;
|
|
|
|
case SYS_RES_IRQ:
|
|
|
|
flags = RF_SHAREABLE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
flags = 0;
|
|
|
|
}
|
|
|
|
/* Restore cardbus hold on the resource */
|
|
|
|
rle->res = bus_alloc_resource(cbdev, type, &rid,
|
|
|
|
rle->start, rle->end, rle->count, flags);
|
|
|
|
if (rle->res == NULL)
|
|
|
|
device_printf(cbdev, "release_resource: "
|
|
|
|
"unable to reacquire resource\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cardbus_setup_intr(device_t cbdev, device_t child, struct resource *irq,
|
|
|
|
int flags, driver_intr_t *intr, void *arg, void **cookiep)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
device_t cdev;
|
|
|
|
struct cardbus_devinfo *dinfo;
|
|
|
|
|
|
|
|
ret = bus_generic_setup_intr(cbdev, child, irq, flags, intr, arg,
|
|
|
|
cookiep);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
for (cdev = child; cbdev != device_get_parent(cdev);
|
|
|
|
cdev = device_get_parent(cdev))
|
|
|
|
/* NOTHING */;
|
|
|
|
dinfo = device_get_ivars(cdev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cardbus_teardown_intr(device_t cbdev, device_t child, struct resource *irq,
|
|
|
|
void *cookie)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
device_t cdev;
|
|
|
|
struct cardbus_devinfo *dinfo;
|
|
|
|
|
|
|
|
ret = bus_generic_teardown_intr(cbdev, child, irq, cookie);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
for (cdev = child; cbdev != device_get_parent(cdev);
|
|
|
|
cdev = device_get_parent(cdev))
|
|
|
|
/* NOTHING */;
|
|
|
|
dinfo = device_get_ivars(cdev);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/* Other Bus Methods */
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
static int
|
2003-02-16 02:06:50 +00:00
|
|
|
cardbus_read_ivar(device_t cbdev, device_t child, int which, uintptr_t *result)
|
2002-03-15 06:41:01 +00:00
|
|
|
{
|
|
|
|
struct cardbus_devinfo *dinfo;
|
|
|
|
pcicfgregs *cfg;
|
|
|
|
|
|
|
|
dinfo = device_get_ivars(child);
|
|
|
|
cfg = &dinfo->pci.cfg;
|
|
|
|
|
|
|
|
switch (which) {
|
2002-11-27 06:56:29 +00:00
|
|
|
case PCI_IVAR_ETHADDR:
|
|
|
|
/*
|
|
|
|
* The generic accessor doesn't deal with failure, so
|
|
|
|
* we set the return value, then return an error.
|
|
|
|
*/
|
2002-11-27 17:30:41 +00:00
|
|
|
if ((dinfo->fepresent & (1 << TPL_FUNCE_LAN_NID)) == 0) {
|
2003-01-27 05:47:01 +00:00
|
|
|
*((uint8_t **) result) = NULL;
|
2002-11-27 06:56:29 +00:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
2003-01-27 05:47:01 +00:00
|
|
|
*((uint8_t **) result) = dinfo->funce.lan.nid;
|
2002-11-27 06:56:29 +00:00
|
|
|
break;
|
2002-03-15 06:41:01 +00:00
|
|
|
default:
|
2003-02-16 02:06:50 +00:00
|
|
|
return (pci_read_ivar(cbdev, child, which, result));
|
2002-03-15 06:41:01 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cardbus_write_ivar(device_t cbdev, device_t child, int which, uintptr_t value)
|
|
|
|
{
|
2003-02-16 02:06:50 +00:00
|
|
|
return(pci_write_ivar(cbdev, child, which, value));
|
2002-03-15 06:41:01 +00:00
|
|
|
}
|
|
|
|
|
2000-10-18 03:25:13 +00:00
|
|
|
static device_method_t cardbus_methods[] = {
|
|
|
|
/* Device interface */
|
|
|
|
DEVMETHOD(device_probe, cardbus_probe),
|
|
|
|
DEVMETHOD(device_attach, cardbus_attach),
|
2000-11-28 00:05:20 +00:00
|
|
|
DEVMETHOD(device_detach, cardbus_detach),
|
2000-10-18 03:25:13 +00:00
|
|
|
DEVMETHOD(device_shutdown, bus_generic_shutdown),
|
2001-12-15 05:58:28 +00:00
|
|
|
DEVMETHOD(device_suspend, cardbus_suspend),
|
|
|
|
DEVMETHOD(device_resume, cardbus_resume),
|
2000-10-18 03:25:13 +00:00
|
|
|
|
|
|
|
/* Bus interface */
|
2003-02-16 02:06:50 +00:00
|
|
|
DEVMETHOD(bus_print_child, pci_print_child),
|
|
|
|
DEVMETHOD(bus_probe_nomatch, pci_probe_nomatch),
|
2002-03-15 06:41:01 +00:00
|
|
|
DEVMETHOD(bus_read_ivar, cardbus_read_ivar),
|
|
|
|
DEVMETHOD(bus_write_ivar, cardbus_write_ivar),
|
2000-11-28 00:05:20 +00:00
|
|
|
DEVMETHOD(bus_driver_added, cardbus_driver_added),
|
2002-03-15 06:41:01 +00:00
|
|
|
DEVMETHOD(bus_alloc_resource, cardbus_alloc_resource),
|
|
|
|
DEVMETHOD(bus_release_resource, cardbus_release_resource),
|
2000-10-18 03:25:13 +00:00
|
|
|
DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
|
|
|
|
DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
|
2002-03-15 06:41:01 +00:00
|
|
|
DEVMETHOD(bus_setup_intr, cardbus_setup_intr),
|
|
|
|
DEVMETHOD(bus_teardown_intr, cardbus_teardown_intr),
|
|
|
|
|
|
|
|
DEVMETHOD(bus_set_resource, cardbus_set_resource_method),
|
|
|
|
DEVMETHOD(bus_get_resource, cardbus_get_resource_method),
|
|
|
|
DEVMETHOD(bus_delete_resource, cardbus_delete_resource_method),
|
2003-02-17 21:20:35 +00:00
|
|
|
DEVMETHOD(bus_child_pnpinfo_str, pci_child_pnpinfo_str_method),
|
|
|
|
DEVMETHOD(bus_child_location_str, pci_child_location_str_method),
|
2000-10-18 03:25:13 +00:00
|
|
|
|
|
|
|
/* Card Interface */
|
|
|
|
DEVMETHOD(card_attach_card, cardbus_attach_card),
|
|
|
|
DEVMETHOD(card_detach_card, cardbus_detach_card),
|
2001-01-06 18:04:55 +00:00
|
|
|
DEVMETHOD(card_cis_read, cardbus_cis_read),
|
|
|
|
DEVMETHOD(card_cis_free, cardbus_cis_free),
|
2000-10-18 03:25:13 +00:00
|
|
|
|
|
|
|
/* Cardbus/PCI interface */
|
2003-02-16 02:06:50 +00:00
|
|
|
DEVMETHOD(pci_read_config, pci_read_config_method),
|
|
|
|
DEVMETHOD(pci_write_config, pci_write_config_method),
|
|
|
|
DEVMETHOD(pci_enable_busmaster, pci_enable_busmaster_method),
|
|
|
|
DEVMETHOD(pci_disable_busmaster, pci_disable_busmaster_method),
|
|
|
|
DEVMETHOD(pci_enable_io, pci_enable_io_method),
|
|
|
|
DEVMETHOD(pci_disable_io, pci_disable_io_method),
|
|
|
|
DEVMETHOD(pci_get_powerstate, pci_get_powerstate_method),
|
|
|
|
DEVMETHOD(pci_set_powerstate, pci_set_powerstate_method),
|
2000-10-18 03:25:13 +00:00
|
|
|
|
|
|
|
{0,0}
|
|
|
|
};
|
|
|
|
|
|
|
|
static driver_t cardbus_driver = {
|
|
|
|
"cardbus",
|
|
|
|
cardbus_methods,
|
|
|
|
0 /* no softc */
|
|
|
|
};
|
|
|
|
|
2000-11-28 00:05:20 +00:00
|
|
|
static devclass_t cardbus_devclass;
|
2000-10-18 03:25:13 +00:00
|
|
|
|
2002-08-15 08:02:23 +00:00
|
|
|
DRIVER_MODULE(cardbus, cbb, cardbus_driver, cardbus_devclass, 0, 0);
|
2002-05-30 17:38:00 +00:00
|
|
|
MODULE_VERSION(cardbus, 1);
|
|
|
|
MODULE_DEPEND(cardbus, exca, 1, 1, 1);
|
2001-01-06 18:04:55 +00:00
|
|
|
/*
|
2000-11-28 00:05:20 +00:00
|
|
|
MODULE_DEPEND(cardbus, pccbb, 1, 1, 1);
|
2001-01-06 18:04:55 +00:00
|
|
|
*/
|