hd64465uart.c revision 1.2 1 1.2 uch /* $NetBSD: hd64465uart.c,v 1.2 2002/03/02 22:26:26 uch Exp $ */
2 1.1 uch
3 1.1 uch /*-
4 1.1 uch * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
5 1.1 uch * All rights reserved.
6 1.1 uch *
7 1.1 uch * Redistribution and use in source and binary forms, with or without
8 1.1 uch * modification, are permitted provided that the following conditions
9 1.1 uch * are met:
10 1.1 uch * 1. Redistributions of source code must retain the above copyright
11 1.1 uch * notice, this list of conditions and the following disclaimer.
12 1.1 uch * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 uch * notice, this list of conditions and the following disclaimer in the
14 1.1 uch * documentation and/or other materials provided with the distribution.
15 1.1 uch * 3. All advertising materials mentioning features or use of this software
16 1.1 uch * must display the following acknowledgement:
17 1.1 uch * This product includes software developed by the NetBSD
18 1.1 uch * Foundation, Inc. and its contributors.
19 1.1 uch * 4. Neither the name of The NetBSD Foundation nor the names of its
20 1.1 uch * contributors may be used to endorse or promote products derived
21 1.1 uch * from this software without specific prior written permission.
22 1.1 uch *
23 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 1.1 uch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.1 uch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.1 uch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 1.1 uch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.1 uch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.1 uch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.1 uch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.1 uch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.1 uch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.1 uch * POSSIBILITY OF SUCH DAMAGE.
34 1.1 uch */
35 1.1 uch
36 1.1 uch #include "opt_kgdb.h"
37 1.1 uch
38 1.1 uch #include <sys/param.h>
39 1.1 uch #include <sys/systm.h>
40 1.1 uch #include <sys/reboot.h>
41 1.1 uch #include <sys/malloc.h>
42 1.1 uch #include <sys/device.h>
43 1.2 uch #include <sys/kgdb.h>
44 1.1 uch
45 1.1 uch #include <sys/termios.h>
46 1.1 uch #include <dev/cons.h>
47 1.1 uch #include <sys/conf.h>
48 1.1 uch
49 1.1 uch #include <machine/bus.h>
50 1.1 uch #include <machine/intr.h>
51 1.1 uch #include <machine/console.h>
52 1.1 uch
53 1.1 uch #include <dev/ic/comvar.h>
54 1.1 uch #include <dev/ic/comreg.h>
55 1.1 uch
56 1.1 uch #include <machine/debug.h>
57 1.1 uch
58 1.1 uch #include <hpcsh/dev/hd64465/hd64465var.h>
59 1.1 uch #include <hpcsh/dev/hd64465/hd64465uartvar.h>
60 1.1 uch #include <hpcsh/dev/hd64465/hd64465uartreg.h>
61 1.1 uch
62 1.1 uch STATIC struct hd64465uart_chip {
63 1.1 uch struct hpcsh_bus_space __tag_body;
64 1.1 uch bus_space_tag_t io_tag;
65 1.1 uch int console;
66 1.1 uch } hd64465uart_chip;
67 1.1 uch
68 1.1 uch struct hd64465uart_softc {
69 1.1 uch struct com_softc sc_com;
70 1.1 uch
71 1.1 uch struct hd64465uart_chip *sc_chip;
72 1.1 uch enum hd64465_module_id sc_module_id;
73 1.1 uch };
74 1.1 uch
75 1.1 uch /* boot console */
76 1.1 uch cdev_decl(com);
77 1.2 uch void hd64465uartcnprobe(struct consdev *);
78 1.2 uch void hd64465uartcninit(struct consdev *);
79 1.1 uch
80 1.1 uch STATIC int hd64465uart_match(struct device *, struct cfdata *, void *);
81 1.1 uch STATIC void hd64465uart_attach(struct device *, struct device *, void *);
82 1.1 uch
83 1.1 uch struct cfattach hd64465uart_ca = {
84 1.1 uch sizeof(struct hd64465uart_softc), hd64465uart_match,
85 1.1 uch hd64465uart_attach
86 1.1 uch };
87 1.1 uch
88 1.1 uch STATIC void hd64465uart_init(void);
89 1.1 uch STATIC u_int8_t hd64465uart_read_1(void *, bus_space_handle_t, bus_size_t);
90 1.1 uch STATIC void hd64465uart_write_1(void *, bus_space_handle_t, bus_size_t,
91 1.1 uch u_int8_t);
92 1.1 uch
93 1.1 uch #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
94 1.1 uch #ifndef COMCN_SPEED
95 1.1 uch #define COMCN_SPEED 19200
96 1.1 uch #endif
97 1.1 uch
98 1.1 uch void
99 1.2 uch hd64465uartcnprobe(struct consdev *cp)
100 1.1 uch {
101 1.1 uch int maj;
102 1.1 uch
103 1.1 uch /* locate the major number */
104 1.1 uch for (maj = 0; maj < nchrdev; maj++)
105 1.1 uch if (cdevsw[maj].d_open == comopen)
106 1.1 uch break;
107 1.1 uch
108 1.1 uch /* Initialize required fields. */
109 1.1 uch cp->cn_dev = makedev(maj, 0);
110 1.1 uch cp->cn_pri = CN_NORMAL;
111 1.1 uch }
112 1.1 uch
113 1.1 uch void
114 1.2 uch hd64465uartcninit(struct consdev *cp)
115 1.1 uch {
116 1.1 uch
117 1.1 uch hd64465uart_init();
118 1.1 uch
119 1.1 uch comcnattach(hd64465uart_chip.io_tag, 0x0, COMCN_SPEED, COM_FREQ,
120 1.1 uch CONMODE);
121 1.1 uch
122 1.1 uch hd64465uart_chip.console = 1;
123 1.1 uch }
124 1.2 uch
125 1.2 uch #ifdef KGDB
126 1.2 uch int
127 1.2 uch hd64465uart_kgdb_init()
128 1.2 uch {
129 1.2 uch
130 1.2 uch if (strcmp(kgdb_devname, "hd64465uart") != 0)
131 1.2 uch return (1);
132 1.2 uch
133 1.2 uch hd64465uart_init();
134 1.2 uch
135 1.2 uch if (com_kgdb_attach(hd64465uart_chip.io_tag, 0x0, kgdb_rate,
136 1.2 uch COM_FREQ, CONMODE) != 0) {
137 1.2 uch printf("%s: KGDB console open failed.\n", __FUNCTION__);
138 1.2 uch return (1);
139 1.2 uch }
140 1.2 uch
141 1.2 uch return (0);
142 1.2 uch }
143 1.2 uch #endif /* KGDB */
144 1.1 uch
145 1.1 uch int
146 1.1 uch hd64465uart_match(struct device *parent, struct cfdata *cf, void *aux)
147 1.1 uch {
148 1.1 uch struct hd64465_attach_args *ha = aux;
149 1.1 uch
150 1.1 uch return (ha->ha_module_id == HD64465_MODULE_UART);
151 1.1 uch }
152 1.1 uch
153 1.1 uch void
154 1.1 uch hd64465uart_attach(struct device *parent, struct device *self, void *aux)
155 1.1 uch {
156 1.1 uch struct hd64465_attach_args *ha = aux;
157 1.1 uch struct hd64465uart_softc *sc = (struct hd64465uart_softc *)self;
158 1.1 uch struct com_softc *csc = &sc->sc_com;
159 1.1 uch
160 1.1 uch sc->sc_chip = &hd64465uart_chip;
161 1.1 uch
162 1.1 uch sc->sc_module_id = ha->ha_module_id;
163 1.1 uch
164 1.1 uch hd64465uart_init();
165 1.1 uch
166 1.1 uch csc->sc_iot = sc->sc_chip->io_tag;
167 1.1 uch bus_space_map(csc->sc_iot, 0, 8, 0, &csc->sc_ioh);
168 1.1 uch csc->sc_iobase = 0;
169 1.1 uch csc->sc_frequency = COM_FREQ;
170 1.1 uch
171 1.1 uch /* supply clock XXX notyet */
172 1.1 uch
173 1.1 uch /* sanity check */
174 1.1 uch if (!comprobe1(csc->sc_iot, csc->sc_ioh)) {
175 1.1 uch printf(": device problem. don't attach.\n");
176 1.1 uch
177 1.1 uch /* stop clock XXX notyet */
178 1.1 uch return;
179 1.1 uch }
180 1.1 uch
181 1.1 uch com_attach_subr(csc);
182 1.1 uch
183 1.1 uch /* register interrupt handler */
184 1.1 uch hd64465_intr_establish(HD64465_IRQ_UART, IST_EDGE, IPL_TTY,
185 1.1 uch comintr, self);
186 1.1 uch }
187 1.1 uch
188 1.1 uch void
189 1.1 uch hd64465uart_init()
190 1.1 uch {
191 1.1 uch
192 1.1 uch if (hd64465uart_chip.io_tag)
193 1.1 uch return;
194 1.1 uch
195 1.1 uch hd64465uart_chip.io_tag = bus_space_create(
196 1.1 uch &hd64465uart_chip.__tag_body, "HD64465 UART I/O",
197 1.1 uch 0xb0008000, 0); /* no extent */
198 1.1 uch
199 1.1 uch /* override bus_space_read_1, bus_space_write_1 */
200 1.1 uch hd64465uart_chip.io_tag->hbs_r_1 = hd64465uart_read_1;
201 1.1 uch hd64465uart_chip.io_tag->hbs_w_1 = hd64465uart_write_1;
202 1.1 uch }
203 1.1 uch
204 1.1 uch u_int8_t
205 1.1 uch hd64465uart_read_1(void *t, bus_space_handle_t h, bus_size_t ofs)
206 1.1 uch {
207 1.1 uch
208 1.1 uch return *(__volatile__ u_int8_t *)(h + (ofs << 1));
209 1.1 uch }
210 1.1 uch
211 1.1 uch void
212 1.1 uch hd64465uart_write_1(void *t, bus_space_handle_t h, bus_size_t ofs,
213 1.1 uch u_int8_t val)
214 1.1 uch {
215 1.1 uch
216 1.1 uch *(__volatile__ u_int8_t *)(h + (ofs << 1)) = val;
217 1.1 uch }
218