cy_pci.c revision 90101
118901Sdg/*
218901Sdg * Copyright (c) 1996, David Greenman
318901Sdg * All rights reserved.
418901Sdg *
518901Sdg * Redistribution and use in source and binary forms, with or without
618901Sdg * modification, are permitted provided that the following conditions
718901Sdg * are met:
818901Sdg * 1. Redistributions of source code must retain the above copyright
918901Sdg *    notice unmodified, this list of conditions, and the following
1018901Sdg *    disclaimer.
1118901Sdg * 2. Redistributions in binary form must reproduce the above copyright
1218901Sdg *    notice, this list of conditions and the following disclaimer in the
1318901Sdg *    documentation and/or other materials provided with the distribution.
1418901Sdg *
1518901Sdg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1618901Sdg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1718901Sdg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1818901Sdg * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1918901Sdg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2018901Sdg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2118901Sdg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2218901Sdg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2318901Sdg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2418901Sdg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2518901Sdg * SUCH DAMAGE.
2618901Sdg *
2750477Speter * $FreeBSD: head/sys/dev/cy/cy_pci.c 90101 2002-02-02 02:05:44Z bde $
2818901Sdg */
2918901Sdg
3018901Sdg/*
3118901Sdg * Cyclades Y PCI serial interface driver
3218901Sdg */
3318901Sdg
3442691Sbde#include "opt_cy_pci_fastintr.h"
3542691Sbde
3618901Sdg#include <sys/param.h>
3718901Sdg#include <sys/systm.h>
3818901Sdg#include <sys/kernel.h>
3965930Sphk#include <sys/bus.h>
4071892Sbde
4171892Sbde#include <machine/bus.h>
4271892Sbde#include <sys/rman.h>
4371892Sbde#include <machine/resource.h>
4471892Sbde
4518901Sdg#include <vm/vm.h>
4618901Sdg#include <vm/pmap.h>
4718901Sdg
4871892Sbde#include <dev/pci/pcivar.h>
4918901Sdg
5018901Sdg#include <pci/cy_pcireg.h>
5118901Sdg
5218901Sdgextern int cyattach_common(void *, int); /* Not exactly correct */
5318901Sdgextern void cyintr(int);
5418901Sdg
5571892Sbdestatic int	cy_pci_attach __P((device_t dev));
5671892Sbdestatic int	cy_pci_probe __P((device_t dev));
5718901Sdg
5871892Sbdestatic device_method_t cy_pci_methods[] = {
5971892Sbde	/* Device interface. */
6071892Sbde	DEVMETHOD(device_probe,		cy_pci_probe),
6171892Sbde	DEVMETHOD(device_attach,	cy_pci_attach),
6271892Sbde
6371892Sbde	{ 0, 0 }
6418901Sdg};
6518901Sdg
6671892Sbdestatic driver_t cy_pci_driver = {
6771892Sbde	"cy",
6871892Sbde	cy_pci_methods,
6971892Sbde	0,
7071892Sbde};
7171892Sbde
7271892Sbdestatic devclass_t	cy_devclass;
7371892Sbde
7471892SbdeDRIVER_MODULE(cy, pci, cy_pci_driver, cy_devclass, 0, 0);
7571892Sbde
7671892Sbdestatic int
7771892Sbdecy_pci_probe(dev)
7871892Sbde	device_t dev;
7918901Sdg{
8071892Sbde	u_int32_t device_id;
8171892Sbde
8271892Sbde	device_id = pci_get_devid(dev);
8346879Sbde	device_id &= ~0x00060000;
8471892Sbde	if (device_id != 0x0100120e && device_id != 0x0101120e)
8571892Sbde		return (ENXIO);
8671892Sbde	device_set_desc(dev, "Cyclades Cyclom-Y Serial Adapter");
8771892Sbde	return (0);
8818901Sdg}
8918901Sdg
9071892Sbdestatic int
9171892Sbdecy_pci_attach(dev)
9271892Sbde	device_t dev;
9318901Sdg{
9471892Sbde	struct resource *ioport_res, *irq_res, *mem_res;
9571892Sbde	void *irq_cookie, *vaddr;
9618901Sdg	u_int32_t ioport;
9771892Sbde	int adapter, irq_setup, ioport_rid, irq_rid, mem_rid;
9842541Sbde	u_char plx_ver;
9918901Sdg
10071892Sbde	ioport_res = NULL;
10171892Sbde	irq_res = NULL;
10271892Sbde	mem_res = NULL;
10318901Sdg
10471892Sbde	ioport_rid = CY_PCI_BASE_ADDR1;
10571892Sbde	ioport_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &ioport_rid,
10671892Sbde	    0ul, ~0ul, 0ul, RF_ACTIVE);
10771892Sbde	if (ioport_res == NULL) {
10871892Sbde		device_printf(dev, "ioport resource allocation failed\n");
10971892Sbde		goto fail;
11071892Sbde	}
11171892Sbde	ioport = rman_get_start(ioport_res);
11271892Sbde
11371892Sbde	mem_rid = CY_PCI_BASE_ADDR2;
11471892Sbde	mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &mem_rid,
11571892Sbde	     0ul, ~0ul, 0ul, RF_ACTIVE);
11671892Sbde	if (mem_res == NULL) {
11771892Sbde		device_printf(dev, "memory resource allocation failed\n");
11871892Sbde		goto fail;
11971892Sbde	}
12071892Sbde	vaddr = rman_get_virtual(mem_res);
12171892Sbde
12218901Sdg	adapter = cyattach_common(vaddr, 1);
12318901Sdg	if (adapter < 0) {
12471892Sbde		device_printf(dev, "no ports found!\n");
12518901Sdg		goto fail;
12618901Sdg	}
12718901Sdg
12818901Sdg	/*
12918901Sdg	 * Allocate our interrupt.
13018901Sdg	 * XXX	Using the ISA interrupt handler directly is a bit of a violation
13118901Sdg	 *	since it doesn't actually take the same argument. For PCI, the
13218901Sdg	 *	argument is a void * token, but for ISA it is a unit. Since
13318901Sdg	 *	there is no overlap in PCI/ISA unit numbers for this driver, and
13418901Sdg	 *	since the ISA driver must handle the interrupt anyway, we use
13518901Sdg	 *	the unit number as the token even for PCI.
13618901Sdg	 */
13771892Sbde	irq_rid = 0;
13871892Sbde	irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &irq_rid, 0ul, ~0ul, 0ul,
13971892Sbde	    RF_SHAREABLE | RF_ACTIVE);
14071892Sbde	if (irq_res == NULL) {
14171892Sbde		device_printf(dev, "interrupt resource allocation failed\n");
14271892Sbde		goto fail;
14371892Sbde	}
14442691Sbde#ifdef CY_PCI_FASTINTR
14571892Sbde	irq_setup = bus_setup_intr(dev, irq_res, INTR_TYPE_TTY | INTR_FAST,
14671892Sbde	    (driver_intr_t *)cyintr, (void *)adapter, &irq_cookie);
14790101Sbde#else
14890101Sbde	irq_setup = ENXIO;
14942691Sbde#endif
15071892Sbde	if (irq_setup != 0)
15171892Sbde		irq_setup = bus_setup_intr(dev, irq_res, INTR_TYPE_TTY,
15271892Sbde		    (driver_intr_t *)cyintr, (void *)adapter, &irq_cookie);
15371892Sbde	if (irq_setup != 0) {
15471892Sbde		device_printf(dev, "interrupt setup failed\n");
15518901Sdg		goto fail;
15618901Sdg	}
15742691Sbde
15818901Sdg	/*
15918901Sdg	 * Enable the "local" interrupt input to generate a
16018901Sdg	 * PCI interrupt.
16118901Sdg	 */
16242541Sbde	plx_ver = *((u_char *)vaddr + PLX_VER) & 0x0f;
16342540Sbde	switch (plx_ver) {
16442541Sbde	case PLX_9050:
16542540Sbde		outw(ioport + CY_PLX_9050_ICS,
16642540Sbde		    inw(ioport + CY_PLX_9050_ICS) | CY_PLX_9050_ICS_IENABLE |
16742540Sbde		    CY_PLX_9050_ICS_LOCAL_IENABLE);
16842540Sbde		break;
16942541Sbde	case PLX_9060:
17042541Sbde	case PLX_9080:
17142541Sbde	default:		/* Old board, use PLX_9060 values. */
17242541Sbde		outw(ioport + CY_PLX_9060_ICS,
17342541Sbde		    inw(ioport + CY_PLX_9060_ICS) | CY_PLX_9060_ICS_IENABLE |
17442541Sbde		    CY_PLX_9060_ICS_LOCAL_IENABLE);
17542540Sbde		break;
17642540Sbde	}
17742540Sbde
17871892Sbde	return (0);
17918901Sdg
18018901Sdgfail:
18171892Sbde	if (ioport_res != NULL)
18271892Sbde		bus_release_resource(dev, SYS_RES_IOPORT, ioport_rid,
18371892Sbde		    ioport_res);
18471892Sbde	if (irq_res != NULL)
18571892Sbde		bus_release_resource(dev, SYS_RES_IRQ, irq_rid, irq_res);
18671892Sbde	if (mem_res != NULL)
18771892Sbde		bus_release_resource(dev, SYS_RES_MEMORY, mem_rid, mem_res);
18871892Sbde	return (ENXIO);
18918901Sdg}
190