xref: /netbsd-src/sys/arch/hpcsh/dev/hd64461/hd64461uart.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: hd64461uart.c,v 1.30 2018/12/11 06:34:00 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: hd64461uart.c,v 1.30 2018/12/11 06:34:00 thorpej Exp $");
31 
32 #include "opt_kgdb.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/reboot.h>
37 #include <sys/malloc.h>
38 #include <sys/device.h>
39 #include <sys/kgdb.h>
40 
41 #include <sys/termios.h>
42 #include <dev/cons.h>
43 #include <sys/conf.h>
44 #include <sys/bus.h>
45 
46 #include <machine/intr.h>
47 #include <machine/console.h>
48 #include <machine/platid.h>
49 #include <machine/platid_mask.h>
50 
51 #include <dev/ic/comvar.h>
52 #include <dev/ic/comreg.h>
53 
54 #include <machine/debug.h>
55 
56 #include <hpcsh/dev/hd64461/hd64461var.h>
57 #include <hpcsh/dev/hd64461/hd64461reg.h>
58 #include <hpcsh/dev/hd64461/hd64461intcreg.h>
59 #include <hpcsh/dev/hd64461/hd64461uartvar.h>
60 #include <hpcsh/dev/hd64461/hd64461uartreg.h>
61 
62 STATIC struct hd64461uart_chip {
63 	struct hpcsh_bus_space __tag_body;
64 	bus_space_tag_t io_tag;
65 	int console;
66 } hd64461uart_chip;
67 
68 struct hd64461uart_softc {
69 	struct com_softc sc_com;
70 
71 	struct hd64461uart_chip *sc_chip;
72 	enum hd64461_module_id sc_module_id;
73 };
74 
75 /* boot console */
76 void hd64461uartcnprobe(struct consdev *);
77 void hd64461uartcninit(struct consdev *);
78 
79 STATIC int hd64461uart_match(device_t, cfdata_t , void *);
80 STATIC void hd64461uart_attach(device_t, device_t, void *);
81 
82 CFATTACH_DECL_NEW(hd64461uart, sizeof(struct hd64461uart_softc),
83     hd64461uart_match, hd64461uart_attach, NULL, NULL);
84 
85 STATIC void hd64461uart_init(void);
86 
87 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
88 #ifndef COMCN_SPEED
89 #define COMCN_SPEED	19200
90 #endif
91 
92 static void
93 hd64461uart_init_regs(struct com_regs *regs, bus_space_tag_t tag,
94 		      bus_space_handle_t hdl, bus_addr_t addr)
95 {
96 
97 	com_init_regs_stride(regs, tag, hdl, addr, 1);
98 }
99 
100 void
101 hd64461uartcnprobe(struct consdev *cp)
102 {
103 	extern const struct cdevsw com_cdevsw;
104 	int maj;
105 
106 	/* locate the major number */
107 	maj = cdevsw_lookup_major(&com_cdevsw);
108 
109 	/* Initialize required fields. */
110 	cp->cn_dev = makedev(maj, 0);
111 	cp->cn_pri = CN_NORMAL;
112 }
113 
114 void
115 hd64461uartcninit(struct consdev *cp)
116 {
117 	struct com_regs regs;
118 
119 	hd64461uart_init();
120 
121 	hd64461uart_init_regs(&regs, hd64461uart_chip.io_tag, 0x0, 0x0);
122 	comcnattach1(&regs, COMCN_SPEED, COM_FREQ, COM_TYPE_NORMAL, CONMODE);
123 
124 	hd64461uart_chip.console = 1;
125 	/* Don't stop to suply AFECK */
126 	if (platid_match(&platid, &platid_mask_MACH_HITACHI_PERSONA))
127 		use_afeck = 1;
128 }
129 
130 #ifdef KGDB
131 int
132 hd64461uart_kgdb_init(void)
133 {
134 	struct com_regs regs;
135 
136 	if (strcmp(kgdb_devname, "hd64461uart") != 0)
137 		return 1;
138 
139 	if (hd64461uart_chip.console)
140 		return 1;	/* can't share with console */
141 
142 	hd64461uart_init();
143 
144 	hd64461uart_init_regs(&regs, hd64461uart_chip.io_tag, NULL, 0x0);
145 	if (com_kgdb_attach1(&regs,
146 	    kgdb_rate, COM_FREQ, COM_TYPE_NORMAL, CONMODE) != 0) {
147 		printf("%s: KGDB console open failed.\n", __func__);
148 		return 1;
149 	}
150 
151 	if (platid_match(&platid, &platid_mask_MACH_HITACHI_PERSONA))
152 		use_afeck = 1;
153 	return 0;
154 }
155 #endif /* KGDB */
156 
157 STATIC int
158 hd64461uart_match(device_t parent, cfdata_t cf, void *aux)
159 {
160 	struct hd64461_attach_args *ha = aux;
161 
162 	return ha->ha_module_id == HD64461_MODULE_UART;
163 }
164 
165 STATIC void
166 hd64461uart_attach(device_t parent, device_t self, void *aux)
167 {
168 	struct hd64461_attach_args *ha = aux;
169 	struct hd64461uart_softc *sc = device_private(self);
170 	struct com_softc *csc = &sc->sc_com;
171 	uint16_t r16, or16;
172 	bus_space_handle_t ioh;
173 
174 	csc->sc_dev = self;
175 	sc->sc_chip = &hd64461uart_chip;
176 
177 	sc->sc_module_id = ha->ha_module_id;
178 
179 	if (!sc->sc_chip->console)
180 		hd64461uart_init();
181 
182 	bus_space_map(sc->sc_chip->io_tag, 0x0, 8, 0, &ioh);
183 	csc->sc_frequency = COM_FREQ;
184 	hd64461uart_init_regs(&csc->sc_regs, sc->sc_chip->io_tag, ioh, 0x0);
185 
186 	/* switch port to UART */
187 
188 	/* supply clock */
189 	r16 = or16 = hd64461_reg_read_2(HD64461_SYSSTBCR_REG16);
190 	r16 &= ~HD64461_SYSSTBCR_SURTSD;
191 	if (platid_match(&platid, &platid_mask_MACH_HITACHI_PERSONA))
192 		r16 &= ~(HD64461_SYSSTBCR_SAFECKE_IST |
193 		    HD64461_SYSSTBCR_SAFECKE_OST);
194 	hd64461_reg_write_2(HD64461_SYSSTBCR_REG16, r16);
195 
196 	/* sanity check */
197 	if (!com_probe_subr(&csc->sc_regs)) {
198 		aprint_error(": device problem. don't attach.\n");
199 
200 		/* restore old clock */
201 		hd64461_reg_write_2(HD64461_SYSSTBCR_REG16, or16);
202 		return;
203 	}
204 
205 	com_attach_subr(csc);
206 
207 	hd6446x_intr_establish(HD64461_INTC_UART, IST_LEVEL, IPL_TTY,
208 	    comintr, csc);
209 }
210 
211 STATIC void
212 hd64461uart_init(void)
213 {
214 
215 	if (hd64461uart_chip.io_tag)
216 		return;
217 
218 	hd64461uart_chip.io_tag = bus_space_create(
219 		&hd64461uart_chip.__tag_body, "HD64461 UART I/O",
220 		HD64461_UART_REGBASE, 0); /* no extent */
221 }
222