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