ipaq_atmelgpio.c revision 1.2.8.2 1 1.2.8.2 nathanw /* $NetBSD: ipaq_atmelgpio.c,v 1.2.8.2 2002/10/18 02:36:59 nathanw Exp $ */
2 1.2.8.2 nathanw
3 1.2.8.2 nathanw /*-
4 1.2.8.2 nathanw * Copyright (c) 2001 The NetBSD Foundation, Inc. All rights reserved.
5 1.2.8.2 nathanw *
6 1.2.8.2 nathanw * This code is derived from software contributed to The NetBSD Foundation
7 1.2.8.2 nathanw * by Ichiro FUKUHARA (ichiro (at) ichiro.org).
8 1.2.8.2 nathanw *
9 1.2.8.2 nathanw * Redistribution and use in source and binary forms, with or without
10 1.2.8.2 nathanw * modification, are permitted provided that the following conditions
11 1.2.8.2 nathanw * are met:
12 1.2.8.2 nathanw * 1. Redistributions of source code must retain the above copyright
13 1.2.8.2 nathanw * notice, this list of conditions and the following disclaimer.
14 1.2.8.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
15 1.2.8.2 nathanw * notice, this list of conditions and the following disclaimer in the
16 1.2.8.2 nathanw * documentation and/or other materials provided with the distribution.
17 1.2.8.2 nathanw * 3. All advertising materials mentioning features or use of this software
18 1.2.8.2 nathanw * must display the following acknowledgement:
19 1.2.8.2 nathanw * This product includes software developed by the NetBSD
20 1.2.8.2 nathanw * Foundation, Inc. and its contributors.
21 1.2.8.2 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
22 1.2.8.2 nathanw * contributors may be used to endorse or promote products derived
23 1.2.8.2 nathanw * from this software without specific prior written permission.
24 1.2.8.2 nathanw *
25 1.2.8.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 1.2.8.2 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.2.8.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.2.8.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 1.2.8.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.2.8.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.2.8.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.2.8.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.2.8.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.2.8.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.2.8.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
36 1.2.8.2 nathanw */
37 1.2.8.2 nathanw /*
38 1.2.8.2 nathanw * iPAQ uses Atmel microcontroller to service a few of peripheral devices.
39 1.2.8.2 nathanw * This controller connect to UART1 of SA11x0.
40 1.2.8.2 nathanw */
41 1.2.8.2 nathanw
42 1.2.8.2 nathanw #include <sys/param.h>
43 1.2.8.2 nathanw #include <sys/systm.h>
44 1.2.8.2 nathanw #include <sys/types.h>
45 1.2.8.2 nathanw #include <sys/conf.h>
46 1.2.8.2 nathanw #include <sys/file.h>
47 1.2.8.2 nathanw #include <sys/device.h>
48 1.2.8.2 nathanw #include <sys/kernel.h>
49 1.2.8.2 nathanw #include <sys/kthread.h>
50 1.2.8.2 nathanw #include <sys/malloc.h>
51 1.2.8.2 nathanw
52 1.2.8.2 nathanw #include <machine/bus.h>
53 1.2.8.2 nathanw
54 1.2.8.2 nathanw #include <hpcarm/dev/ipaq_saipvar.h>
55 1.2.8.2 nathanw #include <hpcarm/dev/ipaq_gpioreg.h>
56 1.2.8.2 nathanw #include <hpcarm/dev/ipaq_atmel.h>
57 1.2.8.2 nathanw #include <hpcarm/dev/ipaq_atmelvar.h>
58 1.2.8.2 nathanw #include <hpcarm/sa11x0/sa11x0_gpioreg.h>
59 1.2.8.2 nathanw #include <hpcarm/sa11x0/sa11x0_comreg.h>
60 1.2.8.2 nathanw #include <hpcarm/sa11x0/sa11x0_reg.h>
61 1.2.8.2 nathanw
62 1.2.8.2 nathanw #ifdef ATMEL_DEBUG
63 1.2.8.2 nathanw #define DPRINTF(x) printf x
64 1.2.8.2 nathanw #else
65 1.2.8.2 nathanw #define DPRINTF(x)
66 1.2.8.2 nathanw #endif
67 1.2.8.2 nathanw
68 1.2.8.2 nathanw static int atmelgpio_match(struct device *, struct cfdata *, void *);
69 1.2.8.2 nathanw static void atmelgpio_attach(struct device *, struct device *, void *);
70 1.2.8.2 nathanw static int atmelgpio_print(void *, const char *);
71 1.2.8.2 nathanw static int atmelgpio_search(struct device *, struct cfdata *, void *);
72 1.2.8.2 nathanw static void atmelgpio_init(struct atmelgpio_softc *);
73 1.2.8.2 nathanw
74 1.2.8.2 nathanw static void rxtx_data(struct atmelgpio_softc *, int, int,
75 1.2.8.2 nathanw u_int8_t *, struct atmel_rx *);
76 1.2.8.2 nathanw
77 1.2.8.2 nathanw CFATTACH_DECL(atmelgpio, sizeof(struct atmelgpio_softc),
78 1.2.8.2 nathanw atmelgpio_match, atmelgpio_attach, NULL, NULL);
79 1.2.8.2 nathanw
80 1.2.8.2 nathanw static int
81 1.2.8.2 nathanw atmelgpio_match(parent, cf, aux)
82 1.2.8.2 nathanw struct device *parent;
83 1.2.8.2 nathanw struct cfdata *cf;
84 1.2.8.2 nathanw void *aux;
85 1.2.8.2 nathanw {
86 1.2.8.2 nathanw return (1);
87 1.2.8.2 nathanw }
88 1.2.8.2 nathanw
89 1.2.8.2 nathanw static void
90 1.2.8.2 nathanw atmelgpio_attach(parent, self, aux)
91 1.2.8.2 nathanw struct device *parent;
92 1.2.8.2 nathanw struct device *self;
93 1.2.8.2 nathanw void *aux;
94 1.2.8.2 nathanw {
95 1.2.8.2 nathanw struct atmelgpio_softc *sc = (struct atmelgpio_softc *)self;
96 1.2.8.2 nathanw struct ipaq_softc *psc = (struct ipaq_softc *)parent;
97 1.2.8.2 nathanw
98 1.2.8.2 nathanw struct atmel_rx *rxbuf;
99 1.2.8.2 nathanw
100 1.2.8.2 nathanw printf("\n");
101 1.2.8.2 nathanw printf("%s: Atmel microcontroller GPIO\n", sc->sc_dev.dv_xname);
102 1.2.8.2 nathanw
103 1.2.8.2 nathanw sc->sc_iot = psc->sc_iot;
104 1.2.8.2 nathanw sc->sc_ioh = psc->sc_ioh;
105 1.2.8.2 nathanw sc->sc_parent = (struct ipaq_softc *)parent;
106 1.2.8.2 nathanw
107 1.2.8.2 nathanw if (bus_space_map(sc->sc_iot, SACOM1_BASE, SACOM_NPORTS, 0,
108 1.2.8.2 nathanw &sc->sc_ioh)) {
109 1.2.8.2 nathanw printf("%s: unable to map of UART1 registers\n", sc->sc_dev.dv_xname);
110 1.2.8.2 nathanw return;
111 1.2.8.2 nathanw }
112 1.2.8.2 nathanw
113 1.2.8.2 nathanw atmelgpio_init(sc);
114 1.2.8.2 nathanw
115 1.2.8.2 nathanw #if 1 /* this is sample */
116 1.2.8.2 nathanw rxtx_data(sc, STATUS_BATTERY, 0, NULL, rxbuf);
117 1.2.8.2 nathanw
118 1.2.8.2 nathanw printf("ac_status = %x\n", rxbuf->data[0]);
119 1.2.8.2 nathanw printf("Battery kind = %x\n", rxbuf->data[1]);
120 1.2.8.2 nathanw printf("Voltage = %d mV\n",
121 1.2.8.2 nathanw 1000 * (rxbuf->data[3] << 8 | rxbuf->data[2]) /228);
122 1.2.8.2 nathanw printf("Battery Status = %x\n", rxbuf->data[4]);
123 1.2.8.2 nathanw printf("Battery percentage = %d\n",
124 1.2.8.2 nathanw 425 * (rxbuf->data[3] << 8 | rxbuf->data[2]) /1000 - 298);
125 1.2.8.2 nathanw #endif
126 1.2.8.2 nathanw
127 1.2.8.2 nathanw /*
128 1.2.8.2 nathanw * Attach each devices
129 1.2.8.2 nathanw */
130 1.2.8.2 nathanw
131 1.2.8.2 nathanw config_search(atmelgpio_search, self, NULL);
132 1.2.8.2 nathanw }
133 1.2.8.2 nathanw
134 1.2.8.2 nathanw static int
135 1.2.8.2 nathanw atmelgpio_search(parent, cf, aux)
136 1.2.8.2 nathanw struct device *parent;
137 1.2.8.2 nathanw struct cfdata *cf;
138 1.2.8.2 nathanw void *aux;
139 1.2.8.2 nathanw {
140 1.2.8.2 nathanw if (config_match(parent, cf, NULL) > 0)
141 1.2.8.2 nathanw config_attach(parent, cf, NULL, atmelgpio_print);
142 1.2.8.2 nathanw return 0;
143 1.2.8.2 nathanw }
144 1.2.8.2 nathanw
145 1.2.8.2 nathanw
146 1.2.8.2 nathanw static int
147 1.2.8.2 nathanw atmelgpio_print(aux, name)
148 1.2.8.2 nathanw void *aux;
149 1.2.8.2 nathanw const char *name;
150 1.2.8.2 nathanw {
151 1.2.8.2 nathanw return (UNCONF);
152 1.2.8.2 nathanw }
153 1.2.8.2 nathanw
154 1.2.8.2 nathanw static void
155 1.2.8.2 nathanw atmelgpio_init(sc)
156 1.2.8.2 nathanw struct atmelgpio_softc *sc;
157 1.2.8.2 nathanw {
158 1.2.8.2 nathanw /* 8 bits no parity 1 stop bit */
159 1.2.8.2 nathanw bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR0, CR0_DSS);
160 1.2.8.2 nathanw
161 1.2.8.2 nathanw /* Set baud rate 115k */
162 1.2.8.2 nathanw bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR1, 0);
163 1.2.8.2 nathanw bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR2, SACOMSPEED(115200));
164 1.2.8.2 nathanw
165 1.2.8.2 nathanw /* RX/TX enable, RX/TX FIFO interrupt enable */
166 1.2.8.2 nathanw bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3,
167 1.2.8.2 nathanw (CR3_RXE | CR3_TXE | CR3_RIE | CR3_TIE));
168 1.2.8.2 nathanw }
169 1.2.8.2 nathanw
170 1.2.8.2 nathanw static void
171 1.2.8.2 nathanw rxtx_data(sc, id, size, buf, rxbuf)
172 1.2.8.2 nathanw struct atmelgpio_softc *sc;
173 1.2.8.2 nathanw int id, size;
174 1.2.8.2 nathanw u_int8_t *buf;
175 1.2.8.2 nathanw struct atmel_rx *rxbuf;
176 1.2.8.2 nathanw {
177 1.2.8.2 nathanw int i, checksum, length, rx_data;
178 1.2.8.2 nathanw u_int8_t data[MAX_SENDSIZE];
179 1.2.8.2 nathanw
180 1.2.8.2 nathanw length = size + FRAME_OVERHEAD_SIZE;
181 1.2.8.2 nathanw
182 1.2.8.2 nathanw while (! (bus_space_read_4(sc->sc_iot, sc->sc_ioh, SACOM_SR0) & SR0_TFS))
183 1.2.8.2 nathanw ;
184 1.2.8.2 nathanw
185 1.2.8.2 nathanw data[0] = (u_int8_t)FRAME_SOF;
186 1.2.8.2 nathanw data[1] = (u_int8_t)((id << 4) | size);
187 1.2.8.2 nathanw checksum = data[1];
188 1.2.8.2 nathanw i = 2;
189 1.2.8.2 nathanw while (size--) {
190 1.2.8.2 nathanw data[i++] = *buf;
191 1.2.8.2 nathanw checksum += (u_int8_t)(*buf++);
192 1.2.8.2 nathanw }
193 1.2.8.2 nathanw data[length-1] = checksum;
194 1.2.8.2 nathanw
195 1.2.8.2 nathanw while (! (bus_space_read_4(sc->sc_iot, sc->sc_ioh, SACOM_SR1) & SR1_TNF))
196 1.2.8.2 nathanw ;
197 1.2.8.2 nathanw i = 0;
198 1.2.8.2 nathanw while (i < length)
199 1.2.8.2 nathanw bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_DR, data[i++]);
200 1.2.8.2 nathanw
201 1.2.8.2 nathanw delay(10000);
202 1.2.8.2 nathanw #if 0
203 1.2.8.2 nathanw while (! (bus_space_read_4(sc->sc_iot, sc->sc_ioh, SACOM_SR0) &
204 1.2.8.2 nathanw (SR0_RID | SR0_RFS)))
205 1.2.8.2 nathanw #endif
206 1.2.8.2 nathanw rxbuf->state = STATE_SOF;
207 1.2.8.2 nathanw while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, SACOM_SR1) & SR1_RNE) {
208 1.2.8.2 nathanw
209 1.2.8.2 nathanw rx_data = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SACOM_DR);
210 1.2.8.2 nathanw DPRINTF(("DATA = %x\n", rx_data));
211 1.2.8.2 nathanw
212 1.2.8.2 nathanw switch (rxbuf->state) {
213 1.2.8.2 nathanw case STATE_SOF:
214 1.2.8.2 nathanw if (rx_data == FRAME_SOF)
215 1.2.8.2 nathanw rxbuf->state = STATE_ID;
216 1.2.8.2 nathanw break;
217 1.2.8.2 nathanw case STATE_ID:
218 1.2.8.2 nathanw rxbuf->id = (rx_data & 0xf0) >> 4;
219 1.2.8.2 nathanw rxbuf->len = rx_data & 0x0f;
220 1.2.8.2 nathanw rxbuf->idx = 0;
221 1.2.8.2 nathanw rxbuf->checksum = rx_data;
222 1.2.8.2 nathanw rxbuf->state = (rxbuf->len > 0 ) ? STATE_DATA : STATE_EOF;
223 1.2.8.2 nathanw break;
224 1.2.8.2 nathanw case STATE_DATA:
225 1.2.8.2 nathanw rxbuf->checksum += rx_data;
226 1.2.8.2 nathanw rxbuf->data[rxbuf->idx] = rx_data;
227 1.2.8.2 nathanw if (++rxbuf->idx == rxbuf->len)
228 1.2.8.2 nathanw rxbuf->state = STATE_EOF;
229 1.2.8.2 nathanw break;
230 1.2.8.2 nathanw case STATE_EOF:
231 1.2.8.2 nathanw rxbuf->state = STATE_SOF;
232 1.2.8.2 nathanw if (rx_data == FRAME_EOF || rx_data == rxbuf->checksum)
233 1.2.8.2 nathanw DPRINTF(("frame EOF\n"));
234 1.2.8.2 nathanw else
235 1.2.8.2 nathanw DPRINTF(("BadFrame\n"));
236 1.2.8.2 nathanw break;
237 1.2.8.2 nathanw default:
238 1.2.8.2 nathanw break;
239 1.2.8.2 nathanw }
240 1.2.8.2 nathanw }
241 1.2.8.2 nathanw }
242