xref: /netbsd-src/sys/arch/hpcsh/dev/hd64465/hd64465uart.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: hd64465uart.c,v 1.19 2012/02/12 16:34:08 matt 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: hd64465uart.c,v 1.19 2012/02/12 16:34:08 matt 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 #include <sys/bus.h>
41 
42 #include <sys/termios.h>
43 #include <dev/cons.h>
44 #include <sys/conf.h>
45 
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/hd64465/hd64465var.h>
55 #include <hpcsh/dev/hd64465/hd64465intcreg.h>
56 #include <hpcsh/dev/hd64465/hd64465uartvar.h>
57 #include <hpcsh/dev/hd64465/hd64465uartreg.h>
58 
59 STATIC struct hd64465uart_chip {
60 	struct hpcsh_bus_space __tag_body;
61 	bus_space_tag_t io_tag;
62 	int console;
63 } hd64465uart_chip;
64 
65 struct hd64465uart_softc {
66 	struct com_softc sc_com;
67 
68 	struct hd64465uart_chip *sc_chip;
69 	enum hd64465_module_id sc_module_id;
70 };
71 
72 /* boot console */
73 void hd64465uartcnprobe(struct consdev *);
74 void hd64465uartcninit(struct consdev *);
75 
76 STATIC int hd64465uart_match(device_t, cfdata_t , void *);
77 STATIC void hd64465uart_attach(device_t, device_t, void *);
78 
79 CFATTACH_DECL_NEW(hd64465uart, sizeof(struct hd64465uart_softc),
80     hd64465uart_match, hd64465uart_attach, NULL, NULL);
81 
82 STATIC void hd64465uart_init(void);
83 STATIC uint8_t hd64465uart_read_1(void *, bus_space_handle_t, bus_size_t);
84 STATIC void hd64465uart_write_1(void *, bus_space_handle_t, bus_size_t,
85     uint8_t);
86 
87 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
88 #ifndef COMCN_SPEED
89 #define COMCN_SPEED	19200
90 #endif
91 
92 void
93 hd64465uartcnprobe(struct consdev *cp)
94 {
95 	extern struct cdevsw com_cdevsw;
96 	int maj;
97 
98 	/* locate the major number */
99 	maj = cdevsw_lookup_major(&com_cdevsw);
100 
101 	/* Initialize required fields. */
102 	cp->cn_dev = makedev(maj, 0);
103 	cp->cn_pri = CN_NORMAL;
104 }
105 
106 void
107 hd64465uartcninit(struct consdev *cp)
108 {
109 
110 	hd64465uart_init();
111 
112 	comcnattach(hd64465uart_chip.io_tag, 0x0, COMCN_SPEED, COM_FREQ,
113 	    COM_TYPE_NORMAL, CONMODE);
114 
115 	hd64465uart_chip.console = 1;
116 }
117 
118 #ifdef KGDB
119 int
120 hd64465uart_kgdb_init(void)
121 {
122 
123 	if (strcmp(kgdb_devname, "hd64465uart") != 0)
124 		return (1);
125 
126 	if (hd64465uart_chip.console)
127 		return (1);	/* can't share with console */
128 
129 	hd64465uart_init();
130 
131 	if (com_kgdb_attach(hd64465uart_chip.io_tag, 0x0, kgdb_rate,
132 	    COM_FREQ, COM_TYPE_NORMAL, CONMODE) != 0) {
133 		printf("%s: KGDB console open failed.\n", __func__);
134 		return (1);
135 	}
136 
137 	return (0);
138 }
139 #endif /* KGDB */
140 
141 int
142 hd64465uart_match(device_t parent, cfdata_t cf, void *aux)
143 {
144 	struct hd64465_attach_args *ha = aux;
145 
146 	return (ha->ha_module_id == HD64465_MODULE_UART);
147 }
148 
149 void
150 hd64465uart_attach(device_t parent, device_t self, void *aux)
151 {
152 	struct hd64465_attach_args *ha = aux;
153 	struct hd64465uart_softc *sc = device_private(self);
154 	struct com_softc *csc = &sc->sc_com;
155 	bus_space_handle_t ioh;
156 
157 	csc->sc_dev = self;
158 	sc->sc_chip = &hd64465uart_chip;
159 
160 	sc->sc_module_id = ha->ha_module_id;
161 
162 	if (!sc->sc_chip->console)
163 		hd64465uart_init();
164 
165 	bus_space_map(sc->sc_chip->io_tag, 0, 8, 0, &ioh);
166 	COM_INIT_REGS(csc->sc_regs, sc->sc_chip->io_tag, ioh, 0);
167 	csc->sc_frequency = COM_FREQ;
168 
169 	/* supply clock XXX notyet */
170 
171 	/* sanity check */
172 	if (!com_probe_subr(&csc->sc_regs)) {
173 		aprint_error(": device problem. don't attach.\n");
174 
175 		/* stop clock XXX notyet */
176 		return;
177 	}
178 
179 	com_attach_subr(csc);
180 
181 	/* register interrupt handler */
182 	hd64465_intr_establish(HD64465_UART, IST_LEVEL, IPL_TTY,
183 	    comintr, self);
184 }
185 
186 void
187 hd64465uart_init(void)
188 {
189 
190 	if (hd64465uart_chip.io_tag)
191 		return;
192 
193 	hd64465uart_chip.io_tag = bus_space_create(
194 		&hd64465uart_chip.__tag_body, "HD64465 UART I/O",
195 		0xb0008000, 0); /* no extent */
196 
197 	/* override bus_space_read_1, bus_space_write_1 */
198 	hd64465uart_chip.io_tag->hbs_r_1 = hd64465uart_read_1;
199 	hd64465uart_chip.io_tag->hbs_w_1 = hd64465uart_write_1;
200 }
201 
202 uint8_t
203 hd64465uart_read_1(void *t, bus_space_handle_t h, bus_size_t ofs)
204 {
205 
206 	return *(volatile uint8_t *)(h + (ofs << 1));
207 }
208 
209 void
210 hd64465uart_write_1(void *t, bus_space_handle_t h, bus_size_t ofs,
211     uint8_t val)
212 {
213 
214 	*(volatile uint8_t *)(h + (ofs << 1)) = val;
215 }
216