lpc_com.c revision 1.2 1 /* $NetBSD: lpc_com.c,v 1.2 2008/11/10 04:07:30 cliff Exp $ */
2
3 /* adapted from:
4 * NetBSD: gemini_com.c,v 1.1 2008/10/24 04:23:18 matt Exp
5 */
6
7 /*
8 * Based on arch/arm/xscale/pxa2x0_com.c
9 *
10 * Copyright 2003 Wasabi Systems, Inc.
11 * All rights reserved.
12 *
13 * Written by Steve C. Woodford for Wasabi Systems, Inc.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed for the NetBSD Project by
26 * Wasabi Systems, Inc.
27 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
28 * or promote products derived from this software without specific prior
29 * written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
33 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
35 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: lpc_com.c,v 1.2 2008/11/10 04:07:30 cliff Exp $");
46
47 #include "opt_com.h"
48 #include "locators.h"
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/device.h>
53 #include <sys/termios.h>
54 #include <sys/callout.h>
55 #include <sys/kernel.h>
56
57 #include <machine/intr.h>
58 #include <machine/bus.h>
59
60 #include <dev/ic/comreg.h>
61 #include <dev/ic/comvar.h>
62
63 #include <arm/gemini/gemini_obiovar.h>
64 #include <arm/gemini/gemini_lpchcvar.h>
65 #include <arm/gemini/gemini_lpcvar.h>
66 #include <arm/gemini/gemini_reg.h>
67
68 #include <arm/gemini/lpc_com.h>
69
70 typedef struct lpc_com_softc {
71 struct com_softc sc_com;
72 bus_addr_t sc_addr;
73 bus_size_t sc_size;
74 int sc_intr;
75 bus_space_tag_t sc_iot;
76 bus_space_handle_t sc_ioh;
77 struct callout sc_callout;
78 } lpc_com_softc_t;
79
80 static int lpc_com_match(device_t, cfdata_t , void *);
81 static void lpc_com_attach(device_t, device_t, void *);
82 static int lpc_com_intr(void *);
83 static void lpc_com_time(void *);
84
85 CFATTACH_DECL_NEW(lpc_com, sizeof(struct lpc_com_softc),
86 lpc_com_match, lpc_com_attach, NULL, NULL);
87
88 static int
89 lpc_com_match(device_t parent, cfdata_t cf, void *aux)
90 {
91 struct gemini_lpc_attach_args *lpc = aux;
92 gemini_lpc_bus_ops_t *ops;
93 lpctag_t lpctag;
94 bus_space_tag_t iot;
95 bus_space_handle_t ioh;
96 bus_addr_t iobase;
97 int rv;
98
99 if (lpc->lpc_addr == LPCCF_LDN_DEFAULT
100 || lpc->lpc_addr == LPCCF_ADDR_DEFAULT)
101 panic("lpc_com must have ldn and addr"
102 " in config.");
103
104 if ((lpc->lpc_intr != LPCCF_INTR_DEFAULT) && (lpc->lpc_intr > 0xff))
105 panic("lpc_com: bad intr %d", lpc->lpc_intr);
106
107 if (lpc->lpc_size == LPCCF_SIZE_DEFAULT)
108 lpc->lpc_size = IT8712F_UART_SIZE;
109
110 iobase = lpc->lpc_base + lpc->lpc_addr;
111 if (com_is_console(lpc->lpc_iot, iobase, NULL))
112 return 1;
113
114 lpctag = lpc->lpc_tag;
115 ops = lpc->lpc_bus_ops;
116 (*ops->lpc_pnp_enter)(lpctag);
117
118 /* Activate */
119 (*ops->lpc_pnp_write)(lpctag, lpc->lpc_ldn, 0x30, 0x01);
120
121 /* Set address */
122 (*ops->lpc_pnp_write)(lpctag, lpc->lpc_ldn, 0x60,
123 (lpc->lpc_addr % 0xff00) >> 8);
124 (*ops->lpc_pnp_write)(lpctag, lpc->lpc_ldn, 0x61,
125 (lpc->lpc_addr % 0x00ff) >> 0);
126
127 /* Set Interrupt Level */
128 (*ops->lpc_pnp_write)(lpctag, lpc->lpc_ldn, 0x70, lpc->lpc_intr);
129
130 /* Set Special Configuration Regs */
131 (*ops->lpc_pnp_write)(lpctag, lpc->lpc_ldn, 0xf0, 0x00);
132 #if 0
133 (*ops->lpc_pnp_write)(lpctag, lpc->lpc_ldn, 0xf1, 0x50);
134 #else
135 (*ops->lpc_pnp_write)(lpctag, lpc->lpc_ldn, 0xf1, 0x58); /* LO */
136 #endif
137 (*ops->lpc_pnp_write)(lpctag, lpc->lpc_ldn, 0xf2, 0x00);
138 (*ops->lpc_pnp_write)(lpctag, lpc->lpc_ldn, 0xf3, 0x7f);
139
140 (*ops->lpc_pnp_exit)(lpctag);
141
142 iot = lpc->lpc_iot;
143 if (bus_space_map(iot, iobase, lpc->lpc_size, 0, &ioh))
144 return 0;
145
146 rv = comprobe1(iot, ioh);
147
148 bus_space_unmap(iot, ioh, lpc->lpc_size);
149
150 return rv;
151 }
152
153 static void
154 lpc_com_attach(device_t parent, device_t self, void *aux)
155 {
156 struct lpc_com_softc *sc = device_private(self);
157 struct gemini_lpc_attach_args *lpc = aux;
158 gemini_lpc_bus_ops_t *ops = lpc->lpc_bus_ops;
159 bus_space_tag_t iot;
160 bus_space_handle_t ioh;
161 bus_addr_t iobase;
162
163 sc->sc_com.sc_dev = self;
164 iot = lpc->lpc_iot;
165 iobase = lpc->lpc_base + lpc->lpc_addr;
166 sc->sc_com.sc_frequency = IT8712F_COM_FREQ;
167 sc->sc_com.sc_type = COM_TYPE_NORMAL;
168
169 if (com_is_console(iot, iobase, &ioh) == 0 &&
170 bus_space_map(iot, iobase, lpc->lpc_size, 0, &ioh)) {
171 panic(": can't map registers\n");
172 return;
173 }
174
175 COM_INIT_REGS(sc->sc_com.sc_regs, iot, ioh, iobase);
176
177 com_attach_subr(&sc->sc_com);
178 aprint_naive("\n");
179
180 if (lpc->lpc_intr == LPCCF_INTR_DEFAULT) {
181 /* callout based polliung */
182 callout_init(&sc->sc_callout, 0);
183 callout_setfunc(&sc->sc_callout, lpc_com_time, sc);
184 callout_schedule(&sc->sc_callout, hz/16);
185 aprint_normal("%s: callout polling mode\n", device_xname(self));
186 } else {
187 /* interrupting */
188 #if 0
189 (*ops->lpc_intr_establish)(lpc->lpc_intrtag, lpc->lpc_intr,
190 IPL_SERIAL, IST_LEVEL_HIGH, comintr, &sc->sc_com);
191 #else
192 (*ops->lpc_intr_establish)(lpc->lpc_intrtag, lpc->lpc_intr,
193 IPL_SERIAL, IST_LEVEL_LOW, lpc_com_intr, &sc->sc_com);
194 #endif
195 }
196 }
197
198 int
199 lpc_com_intr(void *arg)
200 {
201 printf(".");
202 return comintr(arg);
203 }
204
205 void
206 lpc_com_time(void *arg)
207 {
208 lpc_com_softc_t *sc = arg;
209 int s;
210
211 s = splserial();
212 (void)comintr(&sc->sc_com);
213 callout_schedule(&sc->sc_callout, hz/16);
214 splx(s);
215 }
216