cy_pci.c revision 92467
1/*
2 * Copyright (c) 1996, David Greenman
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sys/dev/cy/cy_pci.c 92467 2002-03-17 04:10:38Z bde $
28 */
29
30/*
31 * Cyclades Y PCI serial interface driver
32 */
33
34#include "opt_cy_pci_fastintr.h"
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/kernel.h>
39#include <sys/bus.h>
40
41#include <machine/bus.h>
42#include <sys/rman.h>
43#include <machine/resource.h>
44
45#include <vm/vm.h>
46#include <vm/pmap.h>
47
48#include <dev/pci/pcivar.h>
49
50#define CY_PCI_BASE_ADDR0		0x10
51#define CY_PCI_BASE_ADDR1		0x14
52#define CY_PCI_BASE_ADDR2		0x18
53
54#define CY_PLX_9050_ICS			0x4c
55#define CY_PLX_9060_ICS			0x68
56#define CY_PLX_9050_ICS_IENABLE		0x040
57#define CY_PLX_9050_ICS_LOCAL_IENABLE	0x001
58#define CY_PLX_9050_ICS_LOCAL_IPOLARITY	0x002
59#define CY_PLX_9060_ICS_IENABLE		0x100
60#define CY_PLX_9060_ICS_LOCAL_IENABLE	0x800
61
62/* Cyclom-Y Custom Register for PLX ID. */
63#define	PLX_VER				0x3400
64#define	PLX_9050			0x0b
65#define	PLX_9060			0x0c
66#define	PLX_9080			0x0d
67
68extern int cyattach_common(void *, int); /* Not exactly correct */
69extern void cyintr(int);
70
71static int	cy_pci_attach __P((device_t dev));
72static int	cy_pci_probe __P((device_t dev));
73
74static device_method_t cy_pci_methods[] = {
75	/* Device interface. */
76	DEVMETHOD(device_probe,		cy_pci_probe),
77	DEVMETHOD(device_attach,	cy_pci_attach),
78
79	{ 0, 0 }
80};
81
82static driver_t cy_pci_driver = {
83	"cy",
84	cy_pci_methods,
85	0,
86};
87
88static devclass_t	cy_devclass;
89
90DRIVER_MODULE(cy, pci, cy_pci_driver, cy_devclass, 0, 0);
91
92static int
93cy_pci_probe(dev)
94	device_t dev;
95{
96	u_int32_t device_id;
97
98	device_id = pci_get_devid(dev);
99	device_id &= ~0x00060000;
100	if (device_id != 0x0100120e && device_id != 0x0101120e)
101		return (ENXIO);
102	device_set_desc(dev, "Cyclades Cyclom-Y Serial Adapter");
103	return (0);
104}
105
106static int
107cy_pci_attach(dev)
108	device_t dev;
109{
110	struct resource *ioport_res, *irq_res, *mem_res;
111	void *irq_cookie, *vaddr;
112	u_int32_t ioport;
113	int adapter, irq_setup, ioport_rid, irq_rid, mem_rid;
114	u_char plx_ver;
115
116	ioport_res = NULL;
117	irq_res = NULL;
118	mem_res = NULL;
119
120	ioport_rid = CY_PCI_BASE_ADDR1;
121	ioport_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &ioport_rid,
122	    0ul, ~0ul, 0ul, RF_ACTIVE);
123	if (ioport_res == NULL) {
124		device_printf(dev, "ioport resource allocation failed\n");
125		goto fail;
126	}
127	ioport = rman_get_start(ioport_res);
128
129	mem_rid = CY_PCI_BASE_ADDR2;
130	mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &mem_rid,
131	     0ul, ~0ul, 0ul, RF_ACTIVE);
132	if (mem_res == NULL) {
133		device_printf(dev, "memory resource allocation failed\n");
134		goto fail;
135	}
136	vaddr = rman_get_virtual(mem_res);
137
138	adapter = cyattach_common(vaddr, 1);
139	if (adapter < 0) {
140		device_printf(dev, "no ports found!\n");
141		goto fail;
142	}
143
144	/*
145	 * Allocate our interrupt.
146	 * XXX	Using the ISA interrupt handler directly is a bit of a violation
147	 *	since it doesn't actually take the same argument. For PCI, the
148	 *	argument is a void * token, but for ISA it is a unit. Since
149	 *	there is no overlap in PCI/ISA unit numbers for this driver, and
150	 *	since the ISA driver must handle the interrupt anyway, we use
151	 *	the unit number as the token even for PCI.
152	 */
153	irq_rid = 0;
154	irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &irq_rid, 0ul, ~0ul, 0ul,
155	    RF_SHAREABLE | RF_ACTIVE);
156	if (irq_res == NULL) {
157		device_printf(dev, "interrupt resource allocation failed\n");
158		goto fail;
159	}
160#ifdef CY_PCI_FASTINTR
161	irq_setup = bus_setup_intr(dev, irq_res, INTR_TYPE_TTY | INTR_FAST,
162	    (driver_intr_t *)cyintr, (void *)adapter, &irq_cookie);
163#else
164	irq_setup = ENXIO;
165#endif
166	if (irq_setup != 0)
167		irq_setup = bus_setup_intr(dev, irq_res, INTR_TYPE_TTY,
168		    (driver_intr_t *)cyintr, (void *)adapter, &irq_cookie);
169	if (irq_setup != 0) {
170		device_printf(dev, "interrupt setup failed\n");
171		goto fail;
172	}
173
174	/*
175	 * Enable the "local" interrupt input to generate a
176	 * PCI interrupt.
177	 */
178	plx_ver = *((u_char *)vaddr + PLX_VER) & 0x0f;
179	switch (plx_ver) {
180	case PLX_9050:
181		outw(ioport + CY_PLX_9050_ICS,
182		    CY_PLX_9050_ICS_IENABLE | CY_PLX_9050_ICS_LOCAL_IENABLE |
183		    CY_PLX_9050_ICS_LOCAL_IPOLARITY);
184		break;
185	case PLX_9060:
186	case PLX_9080:
187	default:		/* Old board, use PLX_9060 values. */
188		outw(ioport + CY_PLX_9060_ICS,
189		    inw(ioport + CY_PLX_9060_ICS) | CY_PLX_9060_ICS_IENABLE |
190		    CY_PLX_9060_ICS_LOCAL_IENABLE);
191		break;
192	}
193
194	return (0);
195
196fail:
197	if (ioport_res != NULL)
198		bus_release_resource(dev, SYS_RES_IOPORT, ioport_rid,
199		    ioport_res);
200	if (irq_res != NULL)
201		bus_release_resource(dev, SYS_RES_IRQ, irq_rid, irq_res);
202	if (mem_res != NULL)
203		bus_release_resource(dev, SYS_RES_MEMORY, mem_rid, mem_res);
204	return (ENXIO);
205}
206