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