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
28/*
29 * Cyclades Y PCI serial interface driver
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD$");
34
35#include "opt_cy_pci_fastintr.h"
36
37#include <sys/param.h>
38#include <sys/bus.h>
39#include <sys/kernel.h>
40#include <sys/module.h>
41
42#include <machine/bus.h>
43#include <sys/rman.h>
44#include <machine/resource.h>
45
46#include <dev/pci/pcivar.h>
47
48#include <dev/cy/cyvar.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
68static int	cy_pci_attach(device_t dev);
69static int	cy_pci_probe(device_t dev);
70
71static device_method_t cy_pci_methods[] = {
72	/* Device interface. */
73	DEVMETHOD(device_probe,		cy_pci_probe),
74	DEVMETHOD(device_attach,	cy_pci_attach),
75
76	{ 0, 0 }
77};
78
79static driver_t cy_pci_driver = {
80	cy_driver_name,
81	cy_pci_methods,
82	0,
83};
84
85DRIVER_MODULE(cy, pci, cy_pci_driver, cy_devclass, 0, 0);
86MODULE_DEPEND(cy, pci, 1, 1, 1);
87
88static int
89cy_pci_probe(dev)
90	device_t dev;
91{
92	u_int32_t device_id;
93
94	device_id = pci_get_devid(dev);
95	device_id &= ~0x00060000;
96	if (device_id != 0x0100120e && device_id != 0x0101120e)
97		return (ENXIO);
98	device_set_desc(dev, "Cyclades Cyclom-Y Serial Adapter");
99	return (BUS_PROBE_DEFAULT);
100}
101
102static int
103cy_pci_attach(dev)
104	device_t dev;
105{
106	struct resource *ioport_res, *irq_res, *mem_res;
107	void *irq_cookie, *vaddr, *vsc;
108	u_int32_t ioport;
109	int irq_setup, ioport_rid, irq_rid, mem_rid;
110	u_char plx_ver;
111
112	ioport_res = NULL;
113	irq_res = NULL;
114	mem_res = NULL;
115
116	ioport_rid = CY_PCI_BASE_ADDR1;
117	ioport_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &ioport_rid,
118	    RF_ACTIVE);
119	if (ioport_res == NULL) {
120		device_printf(dev, "ioport resource allocation failed\n");
121		goto fail;
122	}
123	ioport = rman_get_start(ioport_res);
124
125	mem_rid = CY_PCI_BASE_ADDR2;
126	mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &mem_rid,
127	    RF_ACTIVE);
128	if (mem_res == NULL) {
129		device_printf(dev, "memory resource allocation failed\n");
130		goto fail;
131	}
132	vaddr = rman_get_virtual(mem_res);
133
134	vsc = cyattach_common(vaddr, 1);
135	if (vsc == NULL) {
136		device_printf(dev, "no ports found!\n");
137		goto fail;
138	}
139
140	irq_rid = 0;
141	irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &irq_rid,
142	    RF_SHAREABLE | RF_ACTIVE);
143	if (irq_res == NULL) {
144		device_printf(dev, "interrupt resource allocation failed\n");
145		goto fail;
146	}
147#ifdef CY_PCI_FASTINTR
148	irq_setup = bus_setup_intr(dev, irq_res, INTR_TYPE_TTY,
149	    cyintr, NULL, vsc, &irq_cookie);
150#else
151	irq_setup = ENXIO;
152#endif
153	if (irq_setup != 0)
154		irq_setup = bus_setup_intr(dev, irq_res, INTR_TYPE_TTY,
155		    NULL, (driver_intr_t *)cyintr, vsc, &irq_cookie);
156	if (irq_setup != 0) {
157		device_printf(dev, "interrupt setup failed\n");
158		goto fail;
159	}
160
161	/*
162	 * Enable the "local" interrupt input to generate a
163	 * PCI interrupt.
164	 */
165	plx_ver = *((u_char *)vaddr + PLX_VER) & 0x0f;
166	switch (plx_ver) {
167	case PLX_9050:
168		outw(ioport + CY_PLX_9050_ICS,
169		    CY_PLX_9050_ICS_IENABLE | CY_PLX_9050_ICS_LOCAL_IENABLE |
170		    CY_PLX_9050_ICS_LOCAL_IPOLARITY);
171		break;
172	case PLX_9060:
173	case PLX_9080:
174	default:		/* Old board, use PLX_9060 values. */
175		outw(ioport + CY_PLX_9060_ICS,
176		    inw(ioport + CY_PLX_9060_ICS) | CY_PLX_9060_ICS_IENABLE |
177		    CY_PLX_9060_ICS_LOCAL_IENABLE);
178		break;
179	}
180
181	return (0);
182
183fail:
184	if (ioport_res != NULL)
185		bus_release_resource(dev, SYS_RES_IOPORT, ioport_rid,
186		    ioport_res);
187	if (irq_res != NULL)
188		bus_release_resource(dev, SYS_RES_IRQ, irq_rid, irq_res);
189	if (mem_res != NULL)
190		bus_release_resource(dev, SYS_RES_MEMORY, mem_rid, mem_res);
191	return (ENXIO);
192}
193