1*c46d1cc1Smsaitoh /* $NetBSD: lpt_gsc.c,v 1.2 2019/12/27 09:28:41 msaitoh Exp $ */
26d3ceb1dSskrll
36d3ceb1dSskrll /* $OpenBSD: lpt_gsc.c,v 1.6 2000/07/21 17:41:06 mickey Exp $ */
46d3ceb1dSskrll
56d3ceb1dSskrll /*
66d3ceb1dSskrll * Copyright (c) 1998 Michael Shalayeff
76d3ceb1dSskrll * Copyright (c) 1993, 1994 Charles Hannum.
86d3ceb1dSskrll * Copyright (c) 1990 William F. Jolitz, TeleMuse
96d3ceb1dSskrll * All rights reserved.
106d3ceb1dSskrll *
116d3ceb1dSskrll * Redistribution and use in source and binary forms, with or without
126d3ceb1dSskrll * modification, are permitted provided that the following conditions
136d3ceb1dSskrll * are met:
146d3ceb1dSskrll * 1. Redistributions of source code must retain the above copyright
156d3ceb1dSskrll * notice, this list of conditions and the following disclaimer.
166d3ceb1dSskrll * 2. Redistributions in binary form must reproduce the above copyright
176d3ceb1dSskrll * notice, this list of conditions and the following disclaimer in the
186d3ceb1dSskrll * documentation and/or other materials provided with the distribution.
196d3ceb1dSskrll * 3. All advertising materials mentioning features or use of this software
206d3ceb1dSskrll * must display the following acknowledgement:
216d3ceb1dSskrll * This software is a component of "386BSD" developed by
226d3ceb1dSskrll * William F. Jolitz, TeleMuse.
236d3ceb1dSskrll * 4. Neither the name of the developer nor the name "386BSD"
246d3ceb1dSskrll * may be used to endorse or promote products derived from this software
256d3ceb1dSskrll * without specific prior written permission.
266d3ceb1dSskrll *
276d3ceb1dSskrll * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
286d3ceb1dSskrll * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
296d3ceb1dSskrll * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
306d3ceb1dSskrll * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
316d3ceb1dSskrll * NOT MAKE USE OF THIS WORK.
326d3ceb1dSskrll *
336d3ceb1dSskrll * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
346d3ceb1dSskrll * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
356d3ceb1dSskrll * REFERENCES SUCH AS THE "PORTING UNIX TO THE 386" SERIES
366d3ceb1dSskrll * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
376d3ceb1dSskrll * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
386d3ceb1dSskrll * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
396d3ceb1dSskrll * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
406d3ceb1dSskrll * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
416d3ceb1dSskrll *
426d3ceb1dSskrll * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
436d3ceb1dSskrll * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
446d3ceb1dSskrll * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
456d3ceb1dSskrll * ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER BE LIABLE
466d3ceb1dSskrll * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
476d3ceb1dSskrll * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
486d3ceb1dSskrll * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
496d3ceb1dSskrll * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
506d3ceb1dSskrll * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
516d3ceb1dSskrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
526d3ceb1dSskrll * SUCH DAMAGE.
536d3ceb1dSskrll */
546d3ceb1dSskrll
556d3ceb1dSskrll #include <sys/cdefs.h>
56*c46d1cc1Smsaitoh __KERNEL_RCSID(0, "$NetBSD: lpt_gsc.c,v 1.2 2019/12/27 09:28:41 msaitoh Exp $");
576d3ceb1dSskrll
586d3ceb1dSskrll #include <sys/param.h>
596d3ceb1dSskrll #include <sys/systm.h>
606d3ceb1dSskrll #include <sys/device.h>
616d3ceb1dSskrll
626d3ceb1dSskrll #include <sys/bus.h>
636d3ceb1dSskrll #include <machine/intr.h>
646d3ceb1dSskrll #include <machine/iomod.h>
656d3ceb1dSskrll #include <machine/autoconf.h>
666d3ceb1dSskrll
676d3ceb1dSskrll #include <dev/ic/lptreg.h>
686d3ceb1dSskrll #include <dev/ic/lptvar.h>
696d3ceb1dSskrll
706d3ceb1dSskrll #include <hppa/dev/cpudevs.h>
716d3ceb1dSskrll
726d3ceb1dSskrll #include <hppa/gsc/gscbusvar.h>
736d3ceb1dSskrll
746d3ceb1dSskrll #define LPTGSC_OFFSET 0x800
756d3ceb1dSskrll
766d3ceb1dSskrll #ifndef LPTDEBUG
776d3ceb1dSskrll #define LPRINTF(a)
786d3ceb1dSskrll #else
796d3ceb1dSskrll #define LPRINTF(a) if (lpt_isa_debug) printf a
806d3ceb1dSskrll int lpt_isa_debug = 0;
816d3ceb1dSskrll #endif
826d3ceb1dSskrll
836d3ceb1dSskrll int lpt_gsc_probe(device_t, cfdata_t , void *);
846d3ceb1dSskrll void lpt_gsc_attach(device_t, device_t, void *);
856d3ceb1dSskrll
866d3ceb1dSskrll CFATTACH_DECL_NEW(lpt_gsc, sizeof(struct lpt_softc),
876d3ceb1dSskrll lpt_gsc_probe, lpt_gsc_attach, NULL, NULL);
886d3ceb1dSskrll
896d3ceb1dSskrll int lpt_port_test(bus_space_tag_t, bus_space_handle_t, bus_addr_t,
906d3ceb1dSskrll bus_size_t, u_char, u_char);
916d3ceb1dSskrll
926d3ceb1dSskrll /*
936d3ceb1dSskrll * Internal routine to lptprobe to do port tests of one byte value.
946d3ceb1dSskrll */
956d3ceb1dSskrll int
lpt_port_test(bus_space_tag_t iot,bus_space_handle_t ioh,bus_addr_t base,bus_size_t off,u_char data,u_char mask)966d3ceb1dSskrll lpt_port_test(bus_space_tag_t iot, bus_space_handle_t ioh, bus_addr_t base,
976d3ceb1dSskrll bus_size_t off, u_char data, u_char mask)
986d3ceb1dSskrll {
996d3ceb1dSskrll int timeout;
1006d3ceb1dSskrll u_char temp;
1016d3ceb1dSskrll
1026d3ceb1dSskrll data &= mask;
1036d3ceb1dSskrll bus_space_write_1(iot, ioh, off, data);
1046d3ceb1dSskrll timeout = 1000;
1056d3ceb1dSskrll do {
1066d3ceb1dSskrll delay(10);
1076d3ceb1dSskrll temp = bus_space_read_1(iot, ioh, off) & mask;
1086d3ceb1dSskrll } while (temp != data && --timeout);
1096d3ceb1dSskrll LPRINTF(("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n",
1106d3ceb1dSskrll (unsigned)(base + off), (unsigned)data, (unsigned)temp, timeout));
1116d3ceb1dSskrll return (temp == data);
1126d3ceb1dSskrll }
1136d3ceb1dSskrll
1146d3ceb1dSskrll /*
1156d3ceb1dSskrll * Logic:
1166d3ceb1dSskrll * 1) You should be able to write to and read back the same value
1176d3ceb1dSskrll * to the data port. Do an alternating zeros, alternating ones,
1186d3ceb1dSskrll * walking zero, and walking one test to check for stuck bits.
1196d3ceb1dSskrll *
1206d3ceb1dSskrll * 2) You should be able to write to and read back the same value
1216d3ceb1dSskrll * to the control port lower 5 bits, the upper 3 bits are reserved
122*c46d1cc1Smsaitoh * per the IBM PC technical reference manuals and different boards
1236d3ceb1dSskrll * do different things with them. Do an alternating zeros, alternating
1246d3ceb1dSskrll * ones, walking zero, and walking one test to check for stuck bits.
1256d3ceb1dSskrll *
1266d3ceb1dSskrll * Some printers drag the strobe line down when the are powered off
1276d3ceb1dSskrll * so this bit has been masked out of the control port test.
1286d3ceb1dSskrll *
1296d3ceb1dSskrll * XXX Some printers may not like a fast pulse on init or strobe, I
1306d3ceb1dSskrll * don't know at this point, if that becomes a problem these bits
1316d3ceb1dSskrll * should be turned off in the mask byte for the control port test.
1326d3ceb1dSskrll *
1336d3ceb1dSskrll * 3) Set the data and control ports to a value of 0
1346d3ceb1dSskrll */
1356d3ceb1dSskrll int
lpt_gsc_probe(device_t parent,cfdata_t match,void * aux)1366d3ceb1dSskrll lpt_gsc_probe(device_t parent, cfdata_t match, void *aux)
1376d3ceb1dSskrll {
1386d3ceb1dSskrll struct gsc_attach_args *ga = aux;
1396d3ceb1dSskrll bus_space_handle_t ioh;
1406d3ceb1dSskrll bus_addr_t base;
1416d3ceb1dSskrll uint8_t mask, data;
1426d3ceb1dSskrll int i;
1436d3ceb1dSskrll
1446d3ceb1dSskrll if (ga->ga_type.iodc_type != HPPA_TYPE_FIO ||
1456d3ceb1dSskrll ga->ga_type.iodc_sv_model != HPPA_FIO_CENT)
1466d3ceb1dSskrll return 0;
1476d3ceb1dSskrll
1486d3ceb1dSskrll #ifdef DEBUG
1496d3ceb1dSskrll #define ABORT \
1506d3ceb1dSskrll do { \
1516d3ceb1dSskrll printf("lpt_gsc_probe: mask %x data %x failed\n", mask, \
1526d3ceb1dSskrll data); \
1536d3ceb1dSskrll bus_space_unmap(ga->ga_iot, ioh, LPT_NPORTS); \
1546d3ceb1dSskrll return 0; \
1556d3ceb1dSskrll } while (0)
1566d3ceb1dSskrll #else
1576d3ceb1dSskrll #define ABORT \
1586d3ceb1dSskrll do { \
1596d3ceb1dSskrll bus_space_unmap(ga->ga_iot, ioh, LPT_NPORTS); \
1606d3ceb1dSskrll return 0; \
1616d3ceb1dSskrll } while (0)
1626d3ceb1dSskrll #endif
1636d3ceb1dSskrll
1646d3ceb1dSskrll base = ga->ga_hpa + LPTGSC_OFFSET;
1656d3ceb1dSskrll if (bus_space_map(ga->ga_iot, base, LPT_NPORTS, 0, &ioh))
1666d3ceb1dSskrll return 0;
1676d3ceb1dSskrll
1686d3ceb1dSskrll mask = 0xff;
1696d3ceb1dSskrll
1706d3ceb1dSskrll data = 0x55; /* Alternating zeros */
1716d3ceb1dSskrll if (!lpt_port_test(ga->ga_iot, ioh, base, lpt_data, data, mask))
1726d3ceb1dSskrll ABORT;
1736d3ceb1dSskrll
1746d3ceb1dSskrll data = 0xaa; /* Alternating ones */
1756d3ceb1dSskrll if (!lpt_port_test(ga->ga_iot, ioh, base, lpt_data, data, mask))
1766d3ceb1dSskrll ABORT;
1776d3ceb1dSskrll
1786d3ceb1dSskrll for (i = 0; i < CHAR_BIT; i++) { /* Walking zero */
1796d3ceb1dSskrll data = ~(1 << i);
1806d3ceb1dSskrll if (!lpt_port_test(ga->ga_iot, ioh, base, lpt_data, data, mask))
1816d3ceb1dSskrll ABORT;
1826d3ceb1dSskrll }
1836d3ceb1dSskrll
1846d3ceb1dSskrll for (i = 0; i < CHAR_BIT; i++) { /* Walking one */
1856d3ceb1dSskrll data = (1 << i);
1866d3ceb1dSskrll if (!lpt_port_test(ga->ga_iot, ioh, base, lpt_data, data, mask))
1876d3ceb1dSskrll ABORT;
1886d3ceb1dSskrll }
1896d3ceb1dSskrll
1906d3ceb1dSskrll bus_space_write_1(ga->ga_iot, ioh, lpt_data, 0);
1916d3ceb1dSskrll bus_space_write_1(ga->ga_iot, ioh, lpt_control, 0);
1926d3ceb1dSskrll bus_space_unmap(ga->ga_iot, ioh, LPT_NPORTS);
1936d3ceb1dSskrll
1946d3ceb1dSskrll return 1;
1956d3ceb1dSskrll }
1966d3ceb1dSskrll
1976d3ceb1dSskrll void
lpt_gsc_attach(device_t parent,device_t self,void * aux)1986d3ceb1dSskrll lpt_gsc_attach(device_t parent, device_t self, void *aux)
1996d3ceb1dSskrll {
2006d3ceb1dSskrll struct lpt_softc *sc = device_private(self);
2016d3ceb1dSskrll struct gsc_attach_args *ga = aux;
2026d3ceb1dSskrll
2036d3ceb1dSskrll /* sc->sc_flags |= LPT_POLLED; */
2046d3ceb1dSskrll
2056d3ceb1dSskrll sc->sc_dev = self;
2066d3ceb1dSskrll sc->sc_state = 0;
2076d3ceb1dSskrll sc->sc_iot = ga->ga_iot;
2086d3ceb1dSskrll if (bus_space_map(sc->sc_iot, ga->ga_hpa + LPTGSC_OFFSET,
2096d3ceb1dSskrll LPT_NPORTS, 0, &sc->sc_ioh)) {
2106d3ceb1dSskrll aprint_error(": can't map i/o ports\n");
2116d3ceb1dSskrll return;
2126d3ceb1dSskrll }
2136d3ceb1dSskrll
2146d3ceb1dSskrll lpt_attach_subr(sc);
2156d3ceb1dSskrll
2166d3ceb1dSskrll sc->sc_ih = hppa_intr_establish(IPL_TTY, lptintr, sc, ga->ga_ir,
2176d3ceb1dSskrll ga->ga_irq);
2186d3ceb1dSskrll aprint_normal("\n");
2196d3ceb1dSskrll }
220