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