xref: /netbsd-src/sys/arch/hpcsh/dev/hd64461/hd64461uart.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: hd64461uart.c,v 1.20 2007/12/15 00:39:19 perry 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  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *        This product includes software developed by the NetBSD
18  *        Foundation, Inc. and its contributors.
19  * 4. Neither the name of The NetBSD Foundation nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: hd64461uart.c,v 1.20 2007/12/15 00:39:19 perry Exp $");
38 
39 #include "opt_kgdb.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/reboot.h>
44 #include <sys/malloc.h>
45 #include <sys/device.h>
46 #include <sys/kgdb.h>
47 
48 #include <sys/termios.h>
49 #include <dev/cons.h>
50 #include <sys/conf.h>
51 
52 #include <machine/bus.h>
53 #include <machine/intr.h>
54 #include <machine/console.h>
55 
56 #include <dev/ic/comvar.h>
57 #include <dev/ic/comreg.h>
58 
59 #include <machine/debug.h>
60 
61 #include <hpcsh/dev/hd64461/hd64461var.h>
62 #include <hpcsh/dev/hd64461/hd64461reg.h>
63 #include <hpcsh/dev/hd64461/hd64461intcreg.h>
64 #include <hpcsh/dev/hd64461/hd64461uartvar.h>
65 #include <hpcsh/dev/hd64461/hd64461uartreg.h>
66 
67 STATIC struct hd64461uart_chip {
68 	struct hpcsh_bus_space __tag_body;
69 	bus_space_tag_t io_tag;
70 	int console;
71 } hd64461uart_chip;
72 
73 struct hd64461uart_softc {
74 	struct com_softc sc_com;
75 
76 	struct hd64461uart_chip *sc_chip;
77 	enum hd64461_module_id sc_module_id;
78 };
79 
80 /* boot console */
81 void hd64461uartcnprobe(struct consdev *);
82 void hd64461uartcninit(struct consdev *);
83 
84 STATIC int hd64461uart_match(struct device *, struct cfdata *, void *);
85 STATIC void hd64461uart_attach(struct device *, struct device *, void *);
86 
87 CFATTACH_DECL(hd64461uart, sizeof(struct hd64461uart_softc),
88     hd64461uart_match, hd64461uart_attach, NULL, NULL);
89 
90 STATIC void hd64461uart_init(void);
91 STATIC uint8_t hd64461uart_read_1(void *, bus_space_handle_t, bus_size_t);
92 STATIC void hd64461uart_write_1(void *, bus_space_handle_t, bus_size_t,
93     uint8_t);
94 
95 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
96 #ifndef COMCN_SPEED
97 #define COMCN_SPEED	19200
98 #endif
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 
118 	hd64461uart_init();
119 
120 	comcnattach(hd64461uart_chip.io_tag, 0x0, COMCN_SPEED, COM_FREQ,
121 	    COM_TYPE_NORMAL, CONMODE);
122 
123 	hd64461uart_chip.console = 1;
124 }
125 
126 #ifdef KGDB
127 int
128 hd64461uart_kgdb_init(void)
129 {
130 
131 	if (strcmp(kgdb_devname, "hd64461uart") != 0)
132 		return (1);
133 
134 	if (hd64461uart_chip.console)
135 		return (1);	/* can't share with console */
136 
137 	hd64461uart_init();
138 
139 	if (com_kgdb_attach(hd64461uart_chip.io_tag, 0x0, kgdb_rate,
140 	    COM_FREQ, COM_TYPE_NORMAL, CONMODE) != 0) {
141 		printf("%s: KGDB console open failed.\n", __func__);
142 		return (1);
143 	}
144 
145 	return (0);
146 }
147 #endif /* KGDB */
148 
149 STATIC int
150 hd64461uart_match(struct device *parent, struct cfdata *cf, void *aux)
151 {
152 	struct hd64461_attach_args *ha = aux;
153 
154 	return (ha->ha_module_id == HD64461_MODULE_UART);
155 }
156 
157 STATIC void
158 hd64461uart_attach(struct device *parent, struct device *self, void *aux)
159 {
160 	struct hd64461_attach_args *ha = aux;
161 	struct hd64461uart_softc *sc = (struct hd64461uart_softc *)self;
162 	struct com_softc *csc = &sc->sc_com;
163 	uint16_t r16;
164 	bus_space_handle_t ioh;
165 
166 	sc->sc_chip = &hd64461uart_chip;
167 
168 	sc->sc_module_id = ha->ha_module_id;
169 
170 	if (!sc->sc_chip->console)
171 		hd64461uart_init();
172 
173 	bus_space_map(sc->sc_chip->io_tag, 0, 8, 0, &ioh);
174 	csc->sc_frequency = COM_FREQ;
175 	COM_INIT_REGS(csc->sc_regs, sc->sc_chip->io_tag, ioh, 0);
176 
177 	/* switch port to UART */
178 
179 	/* supply clock */
180 	r16 = hd64461_reg_read_2(HD64461_SYSSTBCR_REG16);
181 	r16 &= ~HD64461_SYSSTBCR_SURTSD;
182 	hd64461_reg_write_2(HD64461_SYSSTBCR_REG16, r16);
183 
184 	/* sanity check */
185 	if (!com_probe_subr(&csc->sc_regs)) {
186 		printf(": device problem. don't attach.\n");
187 
188 		/* stop clock */
189 		r16 |= HD64461_SYSSTBCR_SURTSD;
190 		hd64461_reg_write_2(HD64461_SYSSTBCR_REG16, r16);
191 		return;
192 	}
193 
194 	com_attach_subr(csc);
195 
196 	hd6446x_intr_establish(HD64461_INTC_UART, IST_LEVEL, IPL_TTY,
197 	    comintr, self);
198 }
199 
200 STATIC void
201 hd64461uart_init(void)
202 {
203 
204 	if (hd64461uart_chip.io_tag)
205 		return;
206 
207 	hd64461uart_chip.io_tag = bus_space_create(
208 		&hd64461uart_chip.__tag_body, "HD64461 UART I/O",
209 		HD64461_UART_REGBASE, 0); /* no extent */
210 
211 	/* override bus_space_read_1, bus_space_write_1 */
212 	hd64461uart_chip.io_tag->hbs_r_1 = hd64461uart_read_1;
213 	hd64461uart_chip.io_tag->hbs_w_1 = hd64461uart_write_1;
214 }
215 
216 STATIC uint8_t
217 hd64461uart_read_1(void *t, bus_space_handle_t h, bus_size_t ofs)
218 {
219 
220 	return *(volatile uint8_t *)(h + (ofs << 1));
221 }
222 
223 STATIC void
224 hd64461uart_write_1(void *t, bus_space_handle_t h, bus_size_t ofs,
225     uint8_t val)
226 {
227 
228 	*(volatile uint8_t *)(h + (ofs << 1)) = val;
229 }
230