xref: /netbsd-src/sys/arch/mips/alchemy/dev/com_aubus.c (revision 5a2bf51a9284dd2f2be4e95401e3ad283e2b378b)
1*5a2bf51aSthorpej /* $NetBSD: com_aubus.c,v 1.9 2019/01/11 23:10:40 thorpej Exp $ */
234537908Sgdamore 
334537908Sgdamore /*
434537908Sgdamore  * Copyright 2001 Wasabi Systems, Inc.
534537908Sgdamore  * All rights reserved.
634537908Sgdamore  *
734537908Sgdamore  * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
834537908Sgdamore  *
934537908Sgdamore  * Redistribution and use in source and binary forms, with or without
1034537908Sgdamore  * modification, are permitted provided that the following conditions
1134537908Sgdamore  * are met:
1234537908Sgdamore  * 1. Redistributions of source code must retain the above copyright
1334537908Sgdamore  *    notice, this list of conditions and the following disclaimer.
1434537908Sgdamore  * 2. Redistributions in binary form must reproduce the above copyright
1534537908Sgdamore  *    notice, this list of conditions and the following disclaimer in the
1634537908Sgdamore  *    documentation and/or other materials provided with the distribution.
1734537908Sgdamore  * 3. All advertising materials mentioning features or use of this software
1834537908Sgdamore  *    must display the following acknowledgement:
1934537908Sgdamore  *      This product includes software developed for the NetBSD Project by
2034537908Sgdamore  *      Wasabi Systems, Inc.
2134537908Sgdamore  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
2234537908Sgdamore  *    or promote products derived from this software without specific prior
2334537908Sgdamore  *    written permission.
2434537908Sgdamore  *
2534537908Sgdamore  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
2634537908Sgdamore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2734537908Sgdamore  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2834537908Sgdamore  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
2934537908Sgdamore  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3034537908Sgdamore  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3134537908Sgdamore  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3234537908Sgdamore  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3334537908Sgdamore  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3434537908Sgdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3534537908Sgdamore  * POSSIBILITY OF SUCH DAMAGE.
3634537908Sgdamore  */
3734537908Sgdamore 
3834537908Sgdamore #include <sys/cdefs.h>
39*5a2bf51aSthorpej __KERNEL_RCSID(0, "$NetBSD: com_aubus.c,v 1.9 2019/01/11 23:10:40 thorpej Exp $");
4034537908Sgdamore 
4134537908Sgdamore #include <sys/param.h>
4234537908Sgdamore #include <sys/device.h>
43e9e5beefShe #include <sys/lwp.h>
4434537908Sgdamore #include <sys/systm.h>
4534537908Sgdamore #include <sys/tty.h>
4634537908Sgdamore 
47e265f67bSdyoung #include <sys/bus.h>
4834537908Sgdamore #include <dev/ic/comvar.h>
4934537908Sgdamore 
5034537908Sgdamore #include <mips/alchemy/include/aureg.h>
5134537908Sgdamore #include <mips/alchemy/include/auvar.h>
5234537908Sgdamore #include <mips/alchemy/include/aubusvar.h>
5334537908Sgdamore #include <mips/alchemy/dev/com_aubus_reg.h>
5434537908Sgdamore 
5534537908Sgdamore struct com_aubus_softc {
5634537908Sgdamore 	struct com_softc sc_com;
5734537908Sgdamore 	int sc_irq;
5834537908Sgdamore 	void *sc_ih;
5934537908Sgdamore };
6034537908Sgdamore 
61607ead0eScube static int	com_aubus_probe(device_t, cfdata_t , void *);
62607ead0eScube static void	com_aubus_attach(device_t, device_t, void *);
6334537908Sgdamore static int	com_aubus_enable(struct com_softc *);
6434537908Sgdamore static void	com_aubus_disable(struct com_softc *);
656d487047Sthorpej static void	com_aubus_init_regs(struct com_regs *, bus_space_tag_t,
666d487047Sthorpej 				    bus_space_handle_t, bus_addr_t);
6734537908Sgdamore 
68607ead0eScube CFATTACH_DECL_NEW(com_aubus, sizeof(struct com_aubus_softc),
6934537908Sgdamore     com_aubus_probe, com_aubus_attach, NULL, NULL);
7034537908Sgdamore 
7134537908Sgdamore #define CONMODE	((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
7234537908Sgdamore 
7334537908Sgdamore int
com_aubus_probe(device_t parent,cfdata_t cf,void * aux)74607ead0eScube com_aubus_probe(device_t parent, cfdata_t cf, void *aux)
7534537908Sgdamore {
7634537908Sgdamore 	struct aubus_attach_args *aa = aux;
7734537908Sgdamore 
7834537908Sgdamore 	/* match only aucom devices */
7934537908Sgdamore 	if (strcmp(aa->aa_name, cf->cf_name) == 0)
8034537908Sgdamore 		return (1);
8134537908Sgdamore 
8234537908Sgdamore 	return (0);
8334537908Sgdamore }
8434537908Sgdamore 
8534537908Sgdamore void
com_aubus_attach(device_t parent,device_t self,void * aux)86607ead0eScube com_aubus_attach(device_t parent, device_t self, void *aux)
8734537908Sgdamore {
88607ead0eScube 	struct com_aubus_softc *asc = device_private(self);
8934537908Sgdamore 	struct com_softc *sc = &asc->sc_com;
9034537908Sgdamore 	struct aubus_attach_args *aa = aux;
916d487047Sthorpej 	bus_space_handle_t bsh;
9234537908Sgdamore 	int addr = aa->aa_addr;
9334537908Sgdamore 
94607ead0eScube 	sc->sc_dev = self;
9534537908Sgdamore 	asc->sc_irq = aa->aa_irq[0];
9634537908Sgdamore 
976d487047Sthorpej 	if (com_is_console(aa->aa_st, addr, &bsh) == 0 &&
9834537908Sgdamore 	    bus_space_map(aa->aa_st, addr, AUCOM_NPORTS, 0,
9934537908Sgdamore 		&sc->sc_regs.cr_ioh) != 0) {
100607ead0eScube 		aprint_error(": can't map i/o space\n");
10134537908Sgdamore 		return;
10234537908Sgdamore 	}
1036d487047Sthorpej 
1046d487047Sthorpej 	com_aubus_init_regs(&sc->sc_regs, aa->aa_st, bsh, addr);
10534537908Sgdamore 
10634537908Sgdamore 	/*
10734537908Sgdamore 	 * The input to the clock divider is the internal pbus clock (1/4 the
10834537908Sgdamore 	 * processor frequency).  The actual baud rate of the interface will
10934537908Sgdamore 	 * be pbus_freq / CLKDIV.
11034537908Sgdamore 	 */
11134537908Sgdamore 	sc->sc_frequency = curcpu()->ci_cpu_freq / 4;
11234537908Sgdamore 
11334537908Sgdamore 	sc->sc_hwflags = COM_HW_NO_TXPRELOAD;
11434537908Sgdamore 	sc->sc_type = COM_TYPE_AU1x00;
11534537908Sgdamore 
11634537908Sgdamore 	sc->enable = com_aubus_enable;
11734537908Sgdamore 	sc->disable = com_aubus_disable;
11834537908Sgdamore 
11934537908Sgdamore 	/* Enable UART so we can access it. */
12034537908Sgdamore 	com_aubus_enable(sc);
12134537908Sgdamore 	sc->enabled = 1;
12234537908Sgdamore 
12334537908Sgdamore 	/* Attach MI com driver. */
12434537908Sgdamore 	com_attach_subr(sc);
12534537908Sgdamore 
12634537908Sgdamore 	/* Disable UART if it's not the console. (XXX kgdb?) */
12734537908Sgdamore 	if (!ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
12834537908Sgdamore 		com_aubus_disable(sc);
12934537908Sgdamore 		sc->enabled = 0;
13034537908Sgdamore 	}
13134537908Sgdamore }
13234537908Sgdamore 
13334537908Sgdamore int
com_aubus_enable(struct com_softc * sc)13434537908Sgdamore com_aubus_enable(struct com_softc *sc)
13534537908Sgdamore {
13634537908Sgdamore 	struct com_aubus_softc *asc = (void *)sc; /* XXX mi prototype */
13734537908Sgdamore 
13834537908Sgdamore 	/* Ignore requests to enable an already enabled console. */
13934537908Sgdamore 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE) && (asc->sc_ih != NULL))
14034537908Sgdamore 		return (0);
14134537908Sgdamore 
14234537908Sgdamore 	/* Enable the UART module. */
14334537908Sgdamore 	bus_space_write_1(sc->sc_regs.cr_iot, sc->sc_regs.cr_ioh, AUCOM_MODCTL,
14434537908Sgdamore 	    UMC_ME | UMC_CE);
14534537908Sgdamore 
14634537908Sgdamore 	/* Establish the interrupt. */
14734537908Sgdamore 	asc->sc_ih = au_intr_establish(asc->sc_irq, 0, IPL_SERIAL, IST_LEVEL,
14834537908Sgdamore 	    comintr, sc);
14934537908Sgdamore 	if (asc->sc_ih == NULL) {
150607ead0eScube 		aprint_error_dev(sc->sc_dev,
151607ead0eScube 		    "unable to establish interrupt\n");
15234537908Sgdamore 		return (1);
15334537908Sgdamore 	}
15434537908Sgdamore 
15534537908Sgdamore 	return (0);
15634537908Sgdamore }
15734537908Sgdamore 
15834537908Sgdamore void
com_aubus_disable(struct com_softc * sc)15934537908Sgdamore com_aubus_disable(struct com_softc *sc)
16034537908Sgdamore {
16134537908Sgdamore 	struct com_aubus_softc *asc = (void *)sc; /* XXX mi prototype */
16234537908Sgdamore 
16334537908Sgdamore 	/* Ignore requests to disable the console. */
16434537908Sgdamore 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
16534537908Sgdamore 		return;
16634537908Sgdamore 
16734537908Sgdamore 	/* Disestablish the interrupt. */
16834537908Sgdamore 	au_intr_disestablish(asc->sc_ih);
16934537908Sgdamore 
17034537908Sgdamore 	/* Disable the UART module. */
17134537908Sgdamore 	bus_space_write_1(sc->sc_regs.cr_iot, sc->sc_regs.cr_ioh,
17234537908Sgdamore 	    AUCOM_MODCTL, 0);
17334537908Sgdamore }
17434537908Sgdamore 
175*5a2bf51aSthorpej static const bus_size_t com_aubus_regmap[COM_REGMAP_NENTRIES] = {
176*5a2bf51aSthorpej 	[COM_REG_RXDATA]	=	AUCOM_RXDATA,
177*5a2bf51aSthorpej 	[COM_REG_TXDATA]	=	AUCOM_TXDATA,
178*5a2bf51aSthorpej 	[COM_REG_DLBL]		=	AUCOM_DLB,
179*5a2bf51aSthorpej 	[COM_REG_DLBH]		=	AUCOM_DLB,
180*5a2bf51aSthorpej 	[COM_REG_IER]		=	AUCOM_IER,
181*5a2bf51aSthorpej 	[COM_REG_IIR]		=	AUCOM_IIR,
182*5a2bf51aSthorpej 	[COM_REG_FIFO]		=	AUCOM_FIFO,
183*5a2bf51aSthorpej 	[COM_REG_TCR]		=	AUCOM_FIFO,
184*5a2bf51aSthorpej 	[COM_REG_LCR]		=	AUCOM_LCTL,
185*5a2bf51aSthorpej 	[COM_REG_MCR]		=	AUCOM_MCR,
186*5a2bf51aSthorpej 	[COM_REG_LSR]		=	AUCOM_LSR,
187*5a2bf51aSthorpej 	[COM_REG_MSR]		=	AUCOM_MSR,
188*5a2bf51aSthorpej };
189*5a2bf51aSthorpej 
19034537908Sgdamore void
com_aubus_init_regs(struct com_regs * regsp,bus_space_tag_t bst,bus_space_handle_t bsh,bus_addr_t addr)1916d487047Sthorpej com_aubus_init_regs(struct com_regs *regsp, bus_space_tag_t bst,
1926d487047Sthorpej 		    bus_space_handle_t bsh, bus_addr_t addr)
19334537908Sgdamore {
1946d487047Sthorpej 
1956d487047Sthorpej 	com_init_regs(regsp, bst, bsh, addr);
1966d487047Sthorpej 
197*5a2bf51aSthorpej 	memcpy(regsp->cr_map, com_aubus_regmap, sizeof(regsp->cr_map));
19834537908Sgdamore 	regsp->cr_nports = AUCOM_NPORTS;
19934537908Sgdamore }
20034537908Sgdamore 
20134537908Sgdamore int
com_aubus_cnattach(bus_addr_t addr,int baud)20234537908Sgdamore com_aubus_cnattach(bus_addr_t addr, int baud)
20334537908Sgdamore {
20434537908Sgdamore 	struct com_regs		regs;
20534537908Sgdamore 	uint32_t		sysfreq;
20634537908Sgdamore 
2076d487047Sthorpej 	com_aubus_init_regs(&regs, aubus_st, (bus_space_handle_t)0/*XXX*/,
2086d487047Sthorpej 			    addr);
20934537908Sgdamore 
21034537908Sgdamore 	sysfreq = curcpu()->ci_cpu_freq / 4;
21134537908Sgdamore 
21234537908Sgdamore 	return comcnattach1(&regs, baud, sysfreq, COM_TYPE_AU1x00, CONMODE);
21334537908Sgdamore }
214