xref: /netbsd-src/sys/arch/hpcsh/dev/hd64461/hd64461uart.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: hd64461uart.c,v 1.22 2008/04/28 20:23:22 martin 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.22 2008/04/28 20:23:22 martin 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 
45 #include <machine/bus.h>
46 #include <machine/intr.h>
47 #include <machine/console.h>
48 
49 #include <dev/ic/comvar.h>
50 #include <dev/ic/comreg.h>
51 
52 #include <machine/debug.h>
53 
54 #include <hpcsh/dev/hd64461/hd64461var.h>
55 #include <hpcsh/dev/hd64461/hd64461reg.h>
56 #include <hpcsh/dev/hd64461/hd64461intcreg.h>
57 #include <hpcsh/dev/hd64461/hd64461uartvar.h>
58 #include <hpcsh/dev/hd64461/hd64461uartreg.h>
59 
60 STATIC struct hd64461uart_chip {
61 	struct hpcsh_bus_space __tag_body;
62 	bus_space_tag_t io_tag;
63 	int console;
64 } hd64461uart_chip;
65 
66 struct hd64461uart_softc {
67 	struct com_softc sc_com;
68 
69 	struct hd64461uart_chip *sc_chip;
70 	enum hd64461_module_id sc_module_id;
71 };
72 
73 /* boot console */
74 void hd64461uartcnprobe(struct consdev *);
75 void hd64461uartcninit(struct consdev *);
76 
77 STATIC int hd64461uart_match(device_t, cfdata_t , void *);
78 STATIC void hd64461uart_attach(device_t, device_t, void *);
79 
80 CFATTACH_DECL_NEW(hd64461uart, sizeof(struct hd64461uart_softc),
81     hd64461uart_match, hd64461uart_attach, NULL, NULL);
82 
83 STATIC void hd64461uart_init(void);
84 STATIC uint8_t hd64461uart_read_1(void *, bus_space_handle_t, bus_size_t);
85 STATIC void hd64461uart_write_1(void *, bus_space_handle_t, bus_size_t,
86     uint8_t);
87 
88 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
89 #ifndef COMCN_SPEED
90 #define COMCN_SPEED	19200
91 #endif
92 
93 void
94 hd64461uartcnprobe(struct consdev *cp)
95 {
96 	extern const struct cdevsw com_cdevsw;
97 	int maj;
98 
99 	/* locate the major number */
100 	maj = cdevsw_lookup_major(&com_cdevsw);
101 
102 	/* Initialize required fields. */
103 	cp->cn_dev = makedev(maj, 0);
104 	cp->cn_pri = CN_NORMAL;
105 }
106 
107 void
108 hd64461uartcninit(struct consdev *cp)
109 {
110 
111 	hd64461uart_init();
112 
113 	comcnattach(hd64461uart_chip.io_tag, 0x0, COMCN_SPEED, COM_FREQ,
114 	    COM_TYPE_NORMAL, CONMODE);
115 
116 	hd64461uart_chip.console = 1;
117 }
118 
119 #ifdef KGDB
120 int
121 hd64461uart_kgdb_init(void)
122 {
123 
124 	if (strcmp(kgdb_devname, "hd64461uart") != 0)
125 		return (1);
126 
127 	if (hd64461uart_chip.console)
128 		return (1);	/* can't share with console */
129 
130 	hd64461uart_init();
131 
132 	if (com_kgdb_attach(hd64461uart_chip.io_tag, 0x0, kgdb_rate,
133 	    COM_FREQ, COM_TYPE_NORMAL, CONMODE) != 0) {
134 		printf("%s: KGDB console open failed.\n", __func__);
135 		return (1);
136 	}
137 
138 	return (0);
139 }
140 #endif /* KGDB */
141 
142 STATIC int
143 hd64461uart_match(device_t parent, cfdata_t cf, void *aux)
144 {
145 	struct hd64461_attach_args *ha = aux;
146 
147 	return (ha->ha_module_id == HD64461_MODULE_UART);
148 }
149 
150 STATIC void
151 hd64461uart_attach(device_t parent, device_t self, void *aux)
152 {
153 	struct hd64461_attach_args *ha = aux;
154 	struct hd64461uart_softc *sc = device_private(self);
155 	struct com_softc *csc = &sc->sc_com;
156 	uint16_t r16;
157 	bus_space_handle_t ioh;
158 
159 	csc->sc_dev = self;
160 	sc->sc_chip = &hd64461uart_chip;
161 
162 	sc->sc_module_id = ha->ha_module_id;
163 
164 	if (!sc->sc_chip->console)
165 		hd64461uart_init();
166 
167 	bus_space_map(sc->sc_chip->io_tag, 0, 8, 0, &ioh);
168 	csc->sc_frequency = COM_FREQ;
169 	COM_INIT_REGS(csc->sc_regs, sc->sc_chip->io_tag, ioh, 0);
170 
171 	/* switch port to UART */
172 
173 	/* supply clock */
174 	r16 = hd64461_reg_read_2(HD64461_SYSSTBCR_REG16);
175 	r16 &= ~HD64461_SYSSTBCR_SURTSD;
176 	hd64461_reg_write_2(HD64461_SYSSTBCR_REG16, r16);
177 
178 	/* sanity check */
179 	if (!com_probe_subr(&csc->sc_regs)) {
180 		aprint_error(": device problem. don't attach.\n");
181 
182 		/* stop clock */
183 		r16 |= HD64461_SYSSTBCR_SURTSD;
184 		hd64461_reg_write_2(HD64461_SYSSTBCR_REG16, r16);
185 		return;
186 	}
187 
188 	com_attach_subr(csc);
189 
190 	hd6446x_intr_establish(HD64461_INTC_UART, IST_LEVEL, IPL_TTY,
191 	    comintr, self);
192 }
193 
194 STATIC void
195 hd64461uart_init(void)
196 {
197 
198 	if (hd64461uart_chip.io_tag)
199 		return;
200 
201 	hd64461uart_chip.io_tag = bus_space_create(
202 		&hd64461uart_chip.__tag_body, "HD64461 UART I/O",
203 		HD64461_UART_REGBASE, 0); /* no extent */
204 
205 	/* override bus_space_read_1, bus_space_write_1 */
206 	hd64461uart_chip.io_tag->hbs_r_1 = hd64461uart_read_1;
207 	hd64461uart_chip.io_tag->hbs_w_1 = hd64461uart_write_1;
208 }
209 
210 STATIC uint8_t
211 hd64461uart_read_1(void *t, bus_space_handle_t h, bus_size_t ofs)
212 {
213 
214 	return *(volatile uint8_t *)(h + (ofs << 1));
215 }
216 
217 STATIC void
218 hd64461uart_write_1(void *t, bus_space_handle_t h, bus_size_t ofs,
219     uint8_t val)
220 {
221 
222 	*(volatile uint8_t *)(h + (ofs << 1)) = val;
223 }
224