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