xref: /netbsd-src/sys/dev/marvell/com_mv.c (revision d91f98a8715141154279122ae81737cb65179572)
1*d91f98a8Spgoyette /*	$NetBSD: com_mv.c,v 1.12 2019/01/27 02:08:42 pgoyette Exp $	*/
2f99b65b3Skiyohara /*
3f99b65b3Skiyohara  * Copyright (c) 2007, 2010 KIYOHARA Takashi
4f99b65b3Skiyohara  * All rights reserved.
5f99b65b3Skiyohara  *
6f99b65b3Skiyohara  * Redistribution and use in source and binary forms, with or without
7f99b65b3Skiyohara  * modification, are permitted provided that the following conditions
8f99b65b3Skiyohara  * are met:
9f99b65b3Skiyohara  * 1. Redistributions of source code must retain the above copyright
10f99b65b3Skiyohara  *    notice, this list of conditions and the following disclaimer.
11f99b65b3Skiyohara  * 2. Redistributions in binary form must reproduce the above copyright
12f99b65b3Skiyohara  *    notice, this list of conditions and the following disclaimer in the
13f99b65b3Skiyohara  *    documentation and/or other materials provided with the distribution.
14f99b65b3Skiyohara  *
15f99b65b3Skiyohara  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16f99b65b3Skiyohara  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17f99b65b3Skiyohara  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18f99b65b3Skiyohara  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19f99b65b3Skiyohara  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20f99b65b3Skiyohara  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21f99b65b3Skiyohara  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22f99b65b3Skiyohara  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23f99b65b3Skiyohara  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24f99b65b3Skiyohara  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25f99b65b3Skiyohara  * POSSIBILITY OF SUCH DAMAGE.
26f99b65b3Skiyohara  */
27f99b65b3Skiyohara 
28f99b65b3Skiyohara #include <sys/cdefs.h>
29*d91f98a8Spgoyette __KERNEL_RCSID(0, "$NetBSD: com_mv.c,v 1.12 2019/01/27 02:08:42 pgoyette Exp $");
30f99b65b3Skiyohara 
31f99b65b3Skiyohara #include <sys/param.h>
32f99b65b3Skiyohara #include <sys/bus.h>
33f99b65b3Skiyohara #include <sys/device.h>
34f99b65b3Skiyohara #include <sys/errno.h>
35f99b65b3Skiyohara #include <sys/termios.h>
36f99b65b3Skiyohara 
37f99b65b3Skiyohara #include <dev/marvell/gtvar.h>
38f99b65b3Skiyohara #include <dev/marvell/marvellreg.h>
39f99b65b3Skiyohara #include <dev/marvell/marvellvar.h>
40f99b65b3Skiyohara 
41f99b65b3Skiyohara #include <dev/ic/comvar.h>
42f99b65b3Skiyohara 
43f99b65b3Skiyohara #include <prop/proplib.h>
44f99b65b3Skiyohara 
45b1b7c6a2Skiyohara #define MVUART_SIZE		0x20
46f99b65b3Skiyohara 
47f99b65b3Skiyohara 
48f99b65b3Skiyohara static int mvuart_match(device_t, struct cfdata *, void *);
49f99b65b3Skiyohara static void mvuart_attach(device_t, device_t, void *);
50f99b65b3Skiyohara 
51f99b65b3Skiyohara CFATTACH_DECL_NEW(mvuart_gt, sizeof(struct com_softc),
52f99b65b3Skiyohara     mvuart_match, mvuart_attach, NULL, NULL);
53f99b65b3Skiyohara CFATTACH_DECL_NEW(mvuart_mbus, sizeof(struct com_softc),
54f99b65b3Skiyohara     mvuart_match, mvuart_attach, NULL, NULL);
55f99b65b3Skiyohara 
566d487047Sthorpej static void
mvuart_init_regs(struct com_regs * regs,bus_space_tag_t tag,bus_space_handle_t hdl,bus_addr_t addr)576d487047Sthorpej mvuart_init_regs(struct com_regs *regs, bus_space_tag_t tag,
580173341cSmartin 		 bus_space_handle_t hdl, bus_addr_t addr)
596d487047Sthorpej {
606d487047Sthorpej 
61f5ab653cSthorpej 	com_init_regs_stride(regs, tag, hdl, addr, 2);
626d487047Sthorpej }
63f99b65b3Skiyohara 
64f99b65b3Skiyohara 
65f99b65b3Skiyohara /* ARGSUSED */
66f99b65b3Skiyohara static int
mvuart_match(device_t parent,struct cfdata * match,void * aux)67f99b65b3Skiyohara mvuart_match(device_t parent, struct cfdata *match, void *aux)
68f99b65b3Skiyohara {
69f99b65b3Skiyohara 	struct marvell_attach_args *mva = aux;
70bb67784cSkiyohara 	struct com_regs regs;
71f99b65b3Skiyohara 	bus_space_handle_t ioh;
72f99b65b3Skiyohara 
73bb67784cSkiyohara 	if (strcmp(mva->mva_name, match->cf_name) != 0)
74f99b65b3Skiyohara 		return 0;
75bb67784cSkiyohara 	if (mva->mva_offset == MVA_OFFSET_DEFAULT ||
76bb67784cSkiyohara 	    mva->mva_irq == MVA_IRQ_DEFAULT)
77bb67784cSkiyohara 		return 0;
78f99b65b3Skiyohara 
79f99b65b3Skiyohara 	if (com_is_console(mva->mva_iot, mva->mva_addr + mva->mva_offset, NULL))
80ecf43b93Sjakllsch 		goto console;
81f99b65b3Skiyohara 
82f99b65b3Skiyohara 	if (bus_space_subregion(mva->mva_iot, mva->mva_ioh, mva->mva_offset,
83bb67784cSkiyohara 	    MVUART_SIZE, &ioh))
84f99b65b3Skiyohara 		return 0;
85f5ab653cSthorpej 	mvuart_init_regs(&regs, mva->mva_iot, ioh, mva->mva_offset);
86bb67784cSkiyohara 	if (!com_probe_subr(&regs))
87f99b65b3Skiyohara 		return 0;
88bb67784cSkiyohara 
89ecf43b93Sjakllsch console:
90f99b65b3Skiyohara 	mva->mva_size = MVUART_SIZE;
91f99b65b3Skiyohara 	return 1;
92f99b65b3Skiyohara }
93f99b65b3Skiyohara 
94f99b65b3Skiyohara /* ARGSUSED */
95f99b65b3Skiyohara static void
mvuart_attach(device_t parent,device_t self,void * aux)96f99b65b3Skiyohara mvuart_attach(device_t parent, device_t self, void *aux)
97f99b65b3Skiyohara {
98f99b65b3Skiyohara 	struct com_softc *sc = device_private(self);
99f99b65b3Skiyohara 	struct marvell_attach_args *mva = aux;
100f99b65b3Skiyohara 	bus_space_tag_t iot;
101f99b65b3Skiyohara 	bus_space_handle_t ioh;
102f99b65b3Skiyohara 	prop_dictionary_t dict = device_properties(self);
103f99b65b3Skiyohara 
104f99b65b3Skiyohara 	sc->sc_dev = self;
105f99b65b3Skiyohara 
106f99b65b3Skiyohara 	if (!prop_dictionary_get_uint32(dict, "frequency", &sc->sc_frequency)) {
107f99b65b3Skiyohara 		aprint_error(": no frequency property\n");
108f99b65b3Skiyohara 		return;
109f99b65b3Skiyohara 	}
110f99b65b3Skiyohara 
111f99b65b3Skiyohara 	iot = mva->mva_iot;
112f99b65b3Skiyohara 	if (!com_is_console(iot, mva->mva_addr + mva->mva_offset, &ioh)) {
113f99b65b3Skiyohara 		if (bus_space_subregion(iot, mva->mva_ioh, mva->mva_offset,
114f99b65b3Skiyohara 		    mva->mva_size, &ioh)) {
115f99b65b3Skiyohara 			aprint_error(": can't map registers\n");
116f99b65b3Skiyohara 			return;
117f99b65b3Skiyohara 		}
118f99b65b3Skiyohara 	}
1196d487047Sthorpej 	mvuart_init_regs(&sc->sc_regs,
120f5ab653cSthorpej 	    iot, ioh, mva->mva_addr + mva->mva_offset);
121f99b65b3Skiyohara 
122f99b65b3Skiyohara 	com_attach_subr(sc);
123f99b65b3Skiyohara 
124f99b65b3Skiyohara 	marvell_intr_establish(mva->mva_irq, IPL_SERIAL, comintr, sc);
125f99b65b3Skiyohara }
126f99b65b3Skiyohara 
127f99b65b3Skiyohara int mvuart_cnattach(bus_space_tag_t, bus_addr_t, int, uint32_t, int);
128f99b65b3Skiyohara 
129f99b65b3Skiyohara int
mvuart_cnattach(bus_space_tag_t iot,bus_addr_t addr,int baud,uint32_t sysfreq,int mode)130f99b65b3Skiyohara mvuart_cnattach(bus_space_tag_t iot, bus_addr_t addr, int baud,
131f99b65b3Skiyohara 		uint32_t sysfreq, int mode)
132f99b65b3Skiyohara {
133f99b65b3Skiyohara 	struct com_regs regs;
134f99b65b3Skiyohara 
135f5ab653cSthorpej 	mvuart_init_regs(&regs, iot, 0x0, addr);
136f99b65b3Skiyohara 
137f99b65b3Skiyohara 	return comcnattach1(&regs, baud, sysfreq, COM_TYPE_16550_NOERS, mode);
138f99b65b3Skiyohara }
139