xref: /freebsd/sys/dev/ppc/ppc_isa.c (revision 61e21613)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1997-2000 Nicolas Souchu
5  * Copyright (c) 2001 Alcove - Nicolas Souchu
6  * Copyright (c) 2006 Marcel Moolenaar
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/bus.h>
38 #include <machine/bus.h>
39 #include <sys/malloc.h>
40 #include <sys/rman.h>
41 
42 #include <isa/isavar.h>
43 
44 #include <dev/ppbus/ppbconf.h>
45 #include <dev/ppbus/ppb_msq.h>
46 #include <dev/ppc/ppcvar.h>
47 #include <dev/ppc/ppcreg.h>
48 
49 #include "ppbus_if.h"
50 
51 static int ppc_isa_probe(device_t dev);
52 
53 int ppc_isa_attach(device_t dev);
54 int ppc_isa_write(device_t, char *, int, int);
55 
56 static device_method_t ppc_isa_methods[] = {
57 	/* device interface */
58 	DEVMETHOD(device_probe,		ppc_isa_probe),
59 	DEVMETHOD(device_attach,	ppc_isa_attach),
60 	DEVMETHOD(device_detach,	ppc_detach),
61 
62 	/* bus interface */
63 	DEVMETHOD(bus_read_ivar,	ppc_read_ivar),
64 	DEVMETHOD(bus_write_ivar,	ppc_write_ivar),
65 	DEVMETHOD(bus_alloc_resource,	ppc_alloc_resource),
66 	DEVMETHOD(bus_release_resource,	ppc_release_resource),
67 
68 	/* ppbus interface */
69 	DEVMETHOD(ppbus_io,		ppc_io),
70 	DEVMETHOD(ppbus_exec_microseq,	ppc_exec_microseq),
71 	DEVMETHOD(ppbus_reset_epp,	ppc_reset_epp),
72 	DEVMETHOD(ppbus_setmode,	ppc_setmode),
73 	DEVMETHOD(ppbus_ecp_sync,	ppc_ecp_sync),
74 	DEVMETHOD(ppbus_read,		ppc_read),
75 	DEVMETHOD(ppbus_write,		ppc_isa_write),
76 	{ 0, 0 }
77 };
78 
79 static driver_t ppc_isa_driver = {
80 	ppc_driver_name,
81 	ppc_isa_methods,
82 	sizeof(struct ppc_data),
83 };
84 
85 static struct isa_pnp_id lpc_ids[] = {
86 	{ 0x0004d041, "Standard parallel printer port" },	/* PNP0400 */
87 	{ 0x0104d041, "ECP parallel printer port" },		/* PNP0401 */
88 	{ 0 }
89 };
90 
91 static void
92 ppc_isa_dmadone(struct ppc_data *ppc)
93 {
94 	isa_dmadone(ppc->ppc_dmaflags, ppc->ppc_dmaddr, ppc->ppc_dmacnt,
95 	    ppc->ppc_dmachan);
96 }
97 
98 int
99 ppc_isa_attach(device_t dev)
100 {
101 	struct ppc_data *ppc = device_get_softc(dev);
102 
103 	if ((ppc->ppc_avm & PPB_ECP) && (ppc->ppc_dmachan > 0)) {
104 		/* acquire the DMA channel forever */   /* XXX */
105 		isa_dma_acquire(ppc->ppc_dmachan);
106 		isa_dmainit(ppc->ppc_dmachan, 1024); /* nlpt.BUFSIZE */
107 		ppc->ppc_dmadone = ppc_isa_dmadone;
108 	}
109 
110 	return (ppc_attach(dev));
111 }
112 
113 static int
114 ppc_isa_probe(device_t dev)
115 {
116 	device_t parent;
117 	int error;
118 
119 	parent = device_get_parent(dev);
120 
121 	error = ISA_PNP_PROBE(parent, dev, lpc_ids);
122 	if (error == ENXIO)
123 		return (ENXIO);
124 	if (error != 0)		/* XXX shall be set after detection */
125 		device_set_desc(dev, "Parallel port");
126 
127 	return (ppc_probe(dev, 0));
128 }
129 
130 /*
131  * Call this function if you want to send data in any advanced mode
132  * of your parallel port: FIFO, DMA
133  *
134  * If what you want is not possible (no ECP, no DMA...),
135  * EINVAL is returned
136  */
137 int
138 ppc_isa_write(device_t dev, char *buf, int len, int how)
139 {
140 	struct ppc_data *ppc = device_get_softc(dev);
141 	char ecr, ecr_sav, ctr, ctr_sav;
142 	int error = 0;
143 	int spin;
144 
145 	PPC_ASSERT_LOCKED(ppc);
146 	if (!(ppc->ppc_avm & PPB_ECP))
147 		return (EINVAL);
148 	if (ppc->ppc_dmachan == 0)
149 		return (EINVAL);
150 
151 #ifdef PPC_DEBUG
152 	printf("w");
153 #endif
154 
155 	ecr_sav = r_ecr(ppc);
156 	ctr_sav = r_ctr(ppc);
157 
158 	/*
159 	 * Send buffer with DMA, FIFO and interrupts
160 	 */
161 
162 	/* byte mode, no intr, no DMA, dir=0, flush fifo */
163 	ecr = PPC_ECR_STD | PPC_DISABLE_INTR;
164 	w_ecr(ppc, ecr);
165 
166 	/* disable nAck interrupts */
167 	ctr = r_ctr(ppc);
168 	ctr &= ~IRQENABLE;
169 	w_ctr(ppc, ctr);
170 
171 	ppc->ppc_dmaflags = 0;
172 	ppc->ppc_dmaddr = (caddr_t)buf;
173 	ppc->ppc_dmacnt = (u_int)len;
174 
175 	switch (ppc->ppc_mode) {
176 	case PPB_COMPATIBLE:
177 		/* compatible mode with FIFO, no intr, DMA, dir=0 */
178 		ecr = PPC_ECR_FIFO | PPC_DISABLE_INTR | PPC_ENABLE_DMA;
179 		break;
180 	case PPB_ECP:
181 		ecr = PPC_ECR_ECP | PPC_DISABLE_INTR | PPC_ENABLE_DMA;
182 		break;
183 	default:
184 		error = EINVAL;
185 		goto error;
186 	}
187 
188 	w_ecr(ppc, ecr);
189 	ecr = r_ecr(ppc);
190 
191 	ppc->ppc_dmastat = PPC_DMA_INIT;
192 
193 	/* enable interrupts */
194 	ecr &= ~PPC_SERVICE_INTR;
195 	ppc->ppc_irqstat = PPC_IRQ_DMA;
196 	w_ecr(ppc, ecr);
197 
198 	isa_dmastart(ppc->ppc_dmaflags, ppc->ppc_dmaddr, ppc->ppc_dmacnt,
199 		     ppc->ppc_dmachan);
200 	ppc->ppc_dmastat = PPC_DMA_STARTED;
201 
202 #ifdef PPC_DEBUG
203 	printf("s%d", ppc->ppc_dmacnt);
204 #endif
205 
206 	/* Wait for the DMA completed interrupt. We hope we won't
207 	 * miss it, otherwise a signal will be necessary to unlock the
208 	 * process.
209 	 */
210 	do {
211 		/* release CPU */
212 		error = mtx_sleep(ppc, &ppc->ppc_lock, PPBPRI | PCATCH,
213 		    "ppcdma", 0);
214 	} while (error == EWOULDBLOCK);
215 
216 	if (error) {
217 #ifdef PPC_DEBUG
218 		printf("i");
219 #endif
220 		/* stop DMA */
221 		isa_dmadone(ppc->ppc_dmaflags, ppc->ppc_dmaddr,
222 			    ppc->ppc_dmacnt, ppc->ppc_dmachan);
223 
224 		/* no dma, no interrupt, flush the fifo */
225 		w_ecr(ppc, PPC_ECR_RESET);
226 
227 		ppc->ppc_dmastat = PPC_DMA_INTERRUPTED;
228 		goto error;
229 	}
230 
231 	/* wait for an empty fifo */
232 	while (!(r_ecr(ppc) & PPC_FIFO_EMPTY)) {
233 		for (spin=100; spin; spin--)
234 			if (r_ecr(ppc) & PPC_FIFO_EMPTY)
235 				goto fifo_empty;
236 #ifdef PPC_DEBUG
237 		printf("Z");
238 #endif
239 		error = mtx_sleep(ppc, &ppc->ppc_lock, PPBPRI | PCATCH,
240 		    "ppcfifo", hz / 100);
241 		if (error != EWOULDBLOCK) {
242 #ifdef PPC_DEBUG
243 			printf("I");
244 #endif
245 			/* no dma, no interrupt, flush the fifo */
246 			w_ecr(ppc, PPC_ECR_RESET);
247 
248 			ppc->ppc_dmastat = PPC_DMA_INTERRUPTED;
249 			error = EINTR;
250 			goto error;
251 		}
252 	}
253 
254 fifo_empty:
255 	/* no dma, no interrupt, flush the fifo */
256 	w_ecr(ppc, PPC_ECR_RESET);
257 
258 error:
259 	/* PDRQ must be kept unasserted until nPDACK is
260 	 * deasserted for a minimum of 350ns (SMC datasheet)
261 	 *
262 	 * Consequence may be a FIFO that never empty
263 	 */
264 	DELAY(1);
265 
266 	w_ecr(ppc, ecr_sav);
267 	w_ctr(ppc, ctr_sav);
268 	return (error);
269 }
270 
271 DRIVER_MODULE(ppc, isa, ppc_isa_driver, 0, 0);
272 ISA_PNP_INFO(lpc_ids);
273