1*594f789aSthorpej /* $NetBSD: com_dio.c,v 1.10 2024/01/16 05:48:28 thorpej Exp $ */
263b14eb5Stsutsui
363b14eb5Stsutsui /*-
463b14eb5Stsutsui * Copyright (c) 1998 The NetBSD Foundation, Inc.
563b14eb5Stsutsui * All rights reserved.
663b14eb5Stsutsui *
763b14eb5Stsutsui * This code is derived from software contributed to The NetBSD Foundation
863b14eb5Stsutsui * by Charles M. Hannum.
963b14eb5Stsutsui *
1063b14eb5Stsutsui * Redistribution and use in source and binary forms, with or without
1163b14eb5Stsutsui * modification, are permitted provided that the following conditions
1263b14eb5Stsutsui * are met:
1363b14eb5Stsutsui * 1. Redistributions of source code must retain the above copyright
1463b14eb5Stsutsui * notice, this list of conditions and the following disclaimer.
1563b14eb5Stsutsui * 2. Redistributions in binary form must reproduce the above copyright
1663b14eb5Stsutsui * notice, this list of conditions and the following disclaimer in the
1763b14eb5Stsutsui * documentation and/or other materials provided with the distribution.
1863b14eb5Stsutsui *
1963b14eb5Stsutsui * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2063b14eb5Stsutsui * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2163b14eb5Stsutsui * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2263b14eb5Stsutsui * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2363b14eb5Stsutsui * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2463b14eb5Stsutsui * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2563b14eb5Stsutsui * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2663b14eb5Stsutsui * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2763b14eb5Stsutsui * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2863b14eb5Stsutsui * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2963b14eb5Stsutsui * POSSIBILITY OF SUCH DAMAGE.
3063b14eb5Stsutsui */
3163b14eb5Stsutsui
3263b14eb5Stsutsui /*-
3363b14eb5Stsutsui * Copyright (c) 1991 The Regents of the University of California.
3463b14eb5Stsutsui * All rights reserved.
3563b14eb5Stsutsui *
3663b14eb5Stsutsui * Redistribution and use in source and binary forms, with or without
3763b14eb5Stsutsui * modification, are permitted provided that the following conditions
3863b14eb5Stsutsui * are met:
3963b14eb5Stsutsui * 1. Redistributions of source code must retain the above copyright
4063b14eb5Stsutsui * notice, this list of conditions and the following disclaimer.
4163b14eb5Stsutsui * 2. Redistributions in binary form must reproduce the above copyright
4263b14eb5Stsutsui * notice, this list of conditions and the following disclaimer in the
4363b14eb5Stsutsui * documentation and/or other materials provided with the distribution.
4463b14eb5Stsutsui * 3. Neither the name of the University nor the names of its contributors
4563b14eb5Stsutsui * may be used to endorse or promote products derived from this software
4663b14eb5Stsutsui * without specific prior written permission.
4763b14eb5Stsutsui *
4863b14eb5Stsutsui * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4963b14eb5Stsutsui * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5063b14eb5Stsutsui * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5163b14eb5Stsutsui * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5263b14eb5Stsutsui * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5363b14eb5Stsutsui * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5463b14eb5Stsutsui * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5563b14eb5Stsutsui * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5663b14eb5Stsutsui * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5763b14eb5Stsutsui * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5863b14eb5Stsutsui * SUCH DAMAGE.
5963b14eb5Stsutsui *
6063b14eb5Stsutsui * @(#)com.c 7.5 (Berkeley) 5/16/91
6163b14eb5Stsutsui */
6263b14eb5Stsutsui
6363b14eb5Stsutsui #include <sys/cdefs.h>
64*594f789aSthorpej __KERNEL_RCSID(0, "$NetBSD: com_dio.c,v 1.10 2024/01/16 05:48:28 thorpej Exp $");
6563b14eb5Stsutsui
6663b14eb5Stsutsui #include <sys/param.h>
6763b14eb5Stsutsui #include <sys/systm.h>
6863b14eb5Stsutsui #include <sys/device.h>
6963b14eb5Stsutsui #include <sys/termios.h>
7063b14eb5Stsutsui #include <sys/ttydefaults.h>
7163b14eb5Stsutsui
7263b14eb5Stsutsui #include <machine/bus.h>
7363b14eb5Stsutsui
7463b14eb5Stsutsui #include <dev/ic/comreg.h>
7563b14eb5Stsutsui #include <dev/ic/comvar.h>
7663b14eb5Stsutsui
7763b14eb5Stsutsui #include <hp300/dev/diovar.h>
7863b14eb5Stsutsui #include <hp300/dev/diodevs.h>
7963b14eb5Stsutsui #include <hp300/dev/com_dioreg.h>
8063b14eb5Stsutsui #include <hp300/dev/com_diovar.h>
8163b14eb5Stsutsui
8263b14eb5Stsutsui struct com_dio_softc {
8363b14eb5Stsutsui struct com_softc sc_com; /* real "com" softc */
8463b14eb5Stsutsui
8563b14eb5Stsutsui /* DIO-specific goo. */
8663b14eb5Stsutsui struct bus_space_tag sc_tag; /* device specific bus space tag */
8763b14eb5Stsutsui void *sc_ih; /* interrupt handler */
8863b14eb5Stsutsui };
8963b14eb5Stsutsui
90607ead0eScube static int com_dio_match(device_t, cfdata_t , void *);
91607ead0eScube static void com_dio_attach(device_t, device_t, void *);
9263b14eb5Stsutsui
93607ead0eScube CFATTACH_DECL_NEW(com_dio, sizeof(struct com_dio_softc),
9463b14eb5Stsutsui com_dio_match, com_dio_attach, NULL, NULL);
9563b14eb5Stsutsui
9663b14eb5Stsutsui static int com_dio_speed = TTYDEF_SPEED;
9763b14eb5Stsutsui static struct bus_space_tag comcntag;
9863b14eb5Stsutsui
9963b14eb5Stsutsui #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
10063b14eb5Stsutsui
1018ca927b4Sthorpej static int
com_dio_match(device_t parent,cfdata_t match,void * aux)102607ead0eScube com_dio_match(device_t parent, cfdata_t match, void *aux)
10363b14eb5Stsutsui {
10463b14eb5Stsutsui struct dio_attach_args *da = aux;
10563b14eb5Stsutsui
10663b14eb5Stsutsui switch (da->da_id) {
10763b14eb5Stsutsui case DIO_DEVICE_ID_DCA0:
10863b14eb5Stsutsui case DIO_DEVICE_ID_DCA0REM:
10963b14eb5Stsutsui case DIO_DEVICE_ID_DCA1:
11063b14eb5Stsutsui case DIO_DEVICE_ID_DCA1REM:
11163b14eb5Stsutsui return 1;
11263b14eb5Stsutsui }
11363b14eb5Stsutsui
11463b14eb5Stsutsui return 0;
11563b14eb5Stsutsui }
11663b14eb5Stsutsui
1178ca927b4Sthorpej static void
com_dio_attach(device_t parent,device_t self,void * aux)118607ead0eScube com_dio_attach(device_t parent, device_t self, void *aux)
11963b14eb5Stsutsui {
120607ead0eScube struct com_dio_softc *dsc = device_private(self);
12163b14eb5Stsutsui struct com_softc *sc = &dsc->sc_com;
12263b14eb5Stsutsui bus_space_tag_t iot;
12363b14eb5Stsutsui bus_space_handle_t iohdca, iohcom;
12463b14eb5Stsutsui struct dio_attach_args *da = aux;
12563b14eb5Stsutsui int isconsole;
12663b14eb5Stsutsui
127607ead0eScube sc->sc_dev = self;
12863b14eb5Stsutsui isconsole = com_is_console(&comcntag, da->da_addr + DCA_COM_OFFSET,
12963b14eb5Stsutsui &iohcom);
13063b14eb5Stsutsui
13163b14eb5Stsutsui if (isconsole) {
13263b14eb5Stsutsui iot = &comcntag;
13363b14eb5Stsutsui bus_space_unmap(iot, iohcom, COM_NPORTS);
13463b14eb5Stsutsui } else {
13563b14eb5Stsutsui iot = &dsc->sc_tag;
13663b14eb5Stsutsui memcpy(iot, da->da_bst, sizeof(struct bus_space_tag));
13763b14eb5Stsutsui dio_set_bus_space_oddbyte(iot);
13863b14eb5Stsutsui }
13963b14eb5Stsutsui
14063b14eb5Stsutsui if (bus_space_map(iot, da->da_addr, DCA_SIZE, 0, &iohdca) ||
14163b14eb5Stsutsui bus_space_subregion(iot, iohdca, DCA_COM_OFFSET,
14263b14eb5Stsutsui COM_NPORTS << 1, &iohcom)) {
143607ead0eScube aprint_error(": can't map i/o space\n");
14463b14eb5Stsutsui return;
14563b14eb5Stsutsui }
14663b14eb5Stsutsui
14763b14eb5Stsutsui if (!isconsole) {
14863b14eb5Stsutsui bus_space_write_1(iot, iohdca, DCA_RESET, 0xff);
14963b14eb5Stsutsui DELAY(1000);
15063b14eb5Stsutsui }
15163b14eb5Stsutsui
1526d487047Sthorpej com_init_regs(&sc->sc_regs, iot, iohcom,
15334537908Sgdamore da->da_addr + DCA_COM_OFFSET);
15434537908Sgdamore
15563b14eb5Stsutsui sc->sc_frequency = COM_DIO_FREQ;
15663b14eb5Stsutsui
15763b14eb5Stsutsui com_attach_subr(sc);
15863b14eb5Stsutsui
159*594f789aSthorpej dsc->sc_ih = dio_intr_establish(comintr, sc, da->da_ipl, ISRPRI_TTY);
16063b14eb5Stsutsui
16163b14eb5Stsutsui /* Enable interrupts. */
16263b14eb5Stsutsui bus_space_write_1(iot, iohdca, DCA_IC, IC_IE);
16363b14eb5Stsutsui }
16463b14eb5Stsutsui
16563b14eb5Stsutsui int
com_dio_cnattach(bus_space_tag_t bst,bus_addr_t addr,int scode)16663b14eb5Stsutsui com_dio_cnattach(bus_space_tag_t bst, bus_addr_t addr, int scode)
16763b14eb5Stsutsui {
16863b14eb5Stsutsui bus_space_tag_t iot = &comcntag;
16963b14eb5Stsutsui bus_space_handle_t iohdca;
1702be3acc2Stsutsui uint8_t id;
17163b14eb5Stsutsui
17263b14eb5Stsutsui memcpy(iot, bst, sizeof(struct bus_space_tag));
17363b14eb5Stsutsui dio_set_bus_space_oddbyte(iot);
17463b14eb5Stsutsui
17563b14eb5Stsutsui if (bus_space_map(iot, addr, DCA_SIZE, 0, &iohdca))
17663b14eb5Stsutsui return 1;
17763b14eb5Stsutsui bus_space_write_1(iot, iohdca, DCA_RESET, 0xff);
17863b14eb5Stsutsui DELAY(100);
17963b14eb5Stsutsui bus_space_write_1(iot, iohdca, DCA_IC, IC_IE);
18063b14eb5Stsutsui
18163b14eb5Stsutsui id = bus_space_read_1(iot, iohdca, DCA_ID);
18263b14eb5Stsutsui bus_space_unmap(iot, iohdca, DCA_SIZE);
18363b14eb5Stsutsui
18463b14eb5Stsutsui switch (id) {
18563b14eb5Stsutsui #ifdef CONSCODE
18663b14eb5Stsutsui case DCAID0:
18763b14eb5Stsutsui case DCAID1:
18863b14eb5Stsutsui #endif
18963b14eb5Stsutsui case DCAREMID0:
19063b14eb5Stsutsui case DCAREMID1:
19163b14eb5Stsutsui break;
19263b14eb5Stsutsui default:
19363b14eb5Stsutsui return 1;
19463b14eb5Stsutsui }
19563b14eb5Stsutsui
19663b14eb5Stsutsui comcnattach(iot, addr + DCA_COM_OFFSET, com_dio_speed, COM_DIO_FREQ,
19763b14eb5Stsutsui COM_TYPE_NORMAL, CONMODE);
19863b14eb5Stsutsui
19963b14eb5Stsutsui return 0;
20063b14eb5Stsutsui }
201