xref: /netbsd-src/sys/arch/alpha/sableio/com_sableio.c (revision b7b2b8a3d89bfee25ca9324638c59a92666e2e4e)
1*b7b2b8a3Sthorpej /* $NetBSD: com_sableio.c,v 1.16 2021/05/07 16:58:34 thorpej Exp $ */
2f363b73fSthorpej 
3f363b73fSthorpej /*-
4f363b73fSthorpej  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5f363b73fSthorpej  * All rights reserved.
6f363b73fSthorpej  *
7f363b73fSthorpej  * This code is derived from software contributed to The NetBSD Foundation
8f363b73fSthorpej  * by Jason R. Thorpe.
9f363b73fSthorpej  *
10f363b73fSthorpej  * Redistribution and use in source and binary forms, with or without
11f363b73fSthorpej  * modification, are permitted provided that the following conditions
12f363b73fSthorpej  * are met:
13f363b73fSthorpej  * 1. Redistributions of source code must retain the above copyright
14f363b73fSthorpej  *    notice, this list of conditions and the following disclaimer.
15f363b73fSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
16f363b73fSthorpej  *    notice, this list of conditions and the following disclaimer in the
17f363b73fSthorpej  *    documentation and/or other materials provided with the distribution.
18f363b73fSthorpej  *
19f363b73fSthorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20f363b73fSthorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21f363b73fSthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22f363b73fSthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23f363b73fSthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24f363b73fSthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25f363b73fSthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26f363b73fSthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27f363b73fSthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28f363b73fSthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29f363b73fSthorpej  * POSSIBILITY OF SUCH DAMAGE.
30f363b73fSthorpej  */
31f363b73fSthorpej 
32f363b73fSthorpej #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
33f363b73fSthorpej 
34*b7b2b8a3Sthorpej __KERNEL_RCSID(0, "$NetBSD: com_sableio.c,v 1.16 2021/05/07 16:58:34 thorpej Exp $");
35f363b73fSthorpej 
36f363b73fSthorpej #include <sys/param.h>
37f363b73fSthorpej #include <sys/systm.h>
38f363b73fSthorpej #include <sys/ioctl.h>
39f363b73fSthorpej #include <sys/select.h>
40f363b73fSthorpej #include <sys/tty.h>
41f363b73fSthorpej #include <sys/proc.h>
42f363b73fSthorpej #include <sys/conf.h>
43f363b73fSthorpej #include <sys/file.h>
44f363b73fSthorpej #include <sys/uio.h>
45f363b73fSthorpej #include <sys/kernel.h>
46f363b73fSthorpej #include <sys/syslog.h>
47f363b73fSthorpej #include <sys/types.h>
48f363b73fSthorpej #include <sys/device.h>
49f363b73fSthorpej 
50f363b73fSthorpej #include <machine/intr.h>
51cf10107dSdyoung #include <sys/bus.h>
52f363b73fSthorpej 
53f363b73fSthorpej #include <dev/ic/comreg.h>
54f363b73fSthorpej #include <dev/ic/comvar.h>
55f363b73fSthorpej 
56f363b73fSthorpej #include <dev/pci/pcivar.h>
57f363b73fSthorpej #include <dev/isa/isavar.h>
58f363b73fSthorpej 
59f363b73fSthorpej #include <alpha/sableio/sableiovar.h>
60f363b73fSthorpej 
61f363b73fSthorpej struct com_sableio_softc {
62f363b73fSthorpej 	struct	com_softc sc_com;	/* real "com" softc */
63f363b73fSthorpej 
64f363b73fSthorpej 	/* STDIO-specific goo. */
65f363b73fSthorpej 	void	*sc_ih;			/* interrupt handler */
66f363b73fSthorpej };
67f363b73fSthorpej 
68*b7b2b8a3Sthorpej static int	com_sableio_match(device_t, cfdata_t , void *);
69*b7b2b8a3Sthorpej static void	com_sableio_attach(device_t, device_t, void *);
70f363b73fSthorpej 
71607ead0eScube CFATTACH_DECL_NEW(com_sableio, sizeof(struct com_sableio_softc),
72b96bc0d7Sthorpej     com_sableio_match, com_sableio_attach, NULL, NULL);
73f363b73fSthorpej 
74*b7b2b8a3Sthorpej static int
com_sableio_match(device_t parent,cfdata_t match,void * aux)75607ead0eScube com_sableio_match(device_t parent, cfdata_t match, void *aux)
76f363b73fSthorpej {
77f363b73fSthorpej 	struct sableio_attach_args *sa = aux;
78f363b73fSthorpej 
79f363b73fSthorpej 	/* Always present. */
80d1ad2ac4Sthorpej 	if (strcmp(sa->sa_name, match->cf_name) == 0)
81f363b73fSthorpej 		return (1);
82f363b73fSthorpej 
83f363b73fSthorpej 	return (0);
84f363b73fSthorpej }
85f363b73fSthorpej 
86*b7b2b8a3Sthorpej static void
com_sableio_attach(device_t parent,device_t self,void * aux)87607ead0eScube com_sableio_attach(device_t parent, device_t self, void *aux)
88f363b73fSthorpej {
89607ead0eScube 	struct com_sableio_softc *ssc = device_private(self);
90f363b73fSthorpej 	struct com_softc *sc = &ssc->sc_com;
91f363b73fSthorpej 	struct sableio_attach_args *sa = aux;
92f363b73fSthorpej 	const char *intrstr;
93e58a356cSchristos 	char buf[PCI_INTRSTR_LEN];
9434537908Sgdamore 	bus_space_handle_t ioh;
951099504eSthorpej 	pci_intr_handle_t ih;
96f363b73fSthorpej 
97607ead0eScube 	sc->sc_dev = self;
9834537908Sgdamore 	if (com_is_console(sa->sa_iot, sa->sa_ioaddr, &ioh) == 0 &&
9934537908Sgdamore 	    bus_space_map(sa->sa_iot, sa->sa_ioaddr, COM_NPORTS, 0,
10034537908Sgdamore 		&ioh) != 0) {
101607ead0eScube 		aprint_error(": can't map i/o space\n");
102f363b73fSthorpej 		return;
103f363b73fSthorpej 	}
1046d487047Sthorpej 	com_init_regs(&sc->sc_regs, sa->sa_iot, ioh, sa->sa_ioaddr);
105f363b73fSthorpej 
106f363b73fSthorpej 	sc->sc_frequency = COM_FREQ;
107f363b73fSthorpej 
108f363b73fSthorpej 	com_attach_subr(sc);
109f363b73fSthorpej 
1101099504eSthorpej 	alpha_pci_intr_handle_init(&ih, sa->sa_sableirq[0], 0);
1111099504eSthorpej 
1121099504eSthorpej 	intrstr = pci_intr_string(sa->sa_pc, ih, buf, sizeof(buf));
1131099504eSthorpej 	ssc->sc_ih = pci_intr_establish(sa->sa_pc, ih, IPL_SERIAL,
1141099504eSthorpej 	    comintr, sc);
115f363b73fSthorpej 	if (ssc->sc_ih == NULL) {
116607ead0eScube 		aprint_error_dev(self, "unable to establish interrupt");
117f363b73fSthorpej 		if (intrstr != NULL)
11874341862Snjoly 			aprint_error(" at %s", intrstr);
11974341862Snjoly 		aprint_error("\n");
120f363b73fSthorpej 		return;
121f363b73fSthorpej 	}
122607ead0eScube 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
123f363b73fSthorpej 
124e4f38925Sdyoung 	if (!pmf_device_register1(self, com_suspend, com_resume, com_cleanup)) {
125607ead0eScube 		aprint_error_dev(self, "could not establish shutdown hook");
126e4f38925Sdyoung 	}
127f363b73fSthorpej }
128