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