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