rtciic.c revision 1.1 1 1.1 kiyohara /* $NetBSD: rtciic.c,v 1.1 2011/02/19 10:46:28 kiyohara Exp $ */
2 1.1 kiyohara /*
3 1.1 kiyohara * Copyright (c) 2011 KIYOHARA Takashi
4 1.1 kiyohara * All rights reserved.
5 1.1 kiyohara *
6 1.1 kiyohara * Redistribution and use in source and binary forms, with or without
7 1.1 kiyohara * modification, are permitted provided that the following conditions
8 1.1 kiyohara * are met:
9 1.1 kiyohara * 1. Redistributions of source code must retain the above copyright
10 1.1 kiyohara * notice, this list of conditions and the following disclaimer.
11 1.1 kiyohara * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 kiyohara * notice, this list of conditions and the following disclaimer in the
13 1.1 kiyohara * documentation and/or other materials provided with the distribution.
14 1.1 kiyohara *
15 1.1 kiyohara * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 kiyohara * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 kiyohara * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 kiyohara * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 1.1 kiyohara * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 1.1 kiyohara * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 kiyohara * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 kiyohara * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 1.1 kiyohara * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 1.1 kiyohara * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 kiyohara * POSSIBILITY OF SUCH DAMAGE.
26 1.1 kiyohara *
27 1.1 kiyohara */
28 1.1 kiyohara #include <sys/cdefs.h>
29 1.1 kiyohara __KERNEL_RCSID(0, "$NetBSD: rtciic.c,v 1.1 2011/02/19 10:46:28 kiyohara Exp $");
30 1.1 kiyohara
31 1.1 kiyohara #include <sys/param.h>
32 1.1 kiyohara #include <sys/bus.h>
33 1.1 kiyohara #include <sys/device.h>
34 1.1 kiyohara #include <sys/errno.h>
35 1.1 kiyohara
36 1.1 kiyohara #include <machine/autoconf.h>
37 1.1 kiyohara
38 1.1 kiyohara #include <dev/i2c/i2cvar.h>
39 1.1 kiyohara #include <dev/i2c/i2c_bitbang.h>
40 1.1 kiyohara
41 1.1 kiyohara #include "locators.h"
42 1.1 kiyohara
43 1.1 kiyohara #ifdef RTCIIC_DEBUG
44 1.1 kiyohara #define DPRINTF(x) printf x
45 1.1 kiyohara #else
46 1.1 kiyohara #define DPRINTF(x)
47 1.1 kiyohara #endif
48 1.1 kiyohara
49 1.1 kiyohara #define RTCIIC_SDAR (1 << 3) /* recived serial data */
50 1.1 kiyohara #define RTCIIC_SDAW (1 << 2) /* sended serial data */
51 1.1 kiyohara #define RTCIIC_SCL (1 << 1) /* serial clock */
52 1.1 kiyohara #define RTCIIC_RW (1 << 0) /* data direction (0:write, 1:read) */
53 1.1 kiyohara
54 1.1 kiyohara /* This device is Big Endian */
55 1.1 kiyohara #define RTCIIC_READ(sc) \
56 1.1 kiyohara bswap16(bus_space_read_2((sc)->sc_iot, (sc)->sc_ioh, 0))
57 1.1 kiyohara #define RTCIIC_WRITE(sc, val) \
58 1.1 kiyohara bus_space_write_2((sc)->sc_iot, (sc)->sc_ioh, 0, bswap16(val))
59 1.1 kiyohara
60 1.1 kiyohara struct rtciic_softc {
61 1.1 kiyohara device_t sc_dev;
62 1.1 kiyohara
63 1.1 kiyohara bus_space_tag_t sc_iot;
64 1.1 kiyohara bus_space_handle_t sc_ioh;
65 1.1 kiyohara
66 1.1 kiyohara struct i2c_controller sc_i2c;
67 1.1 kiyohara struct i2c_bitbang_ops sc_bops;
68 1.1 kiyohara
69 1.1 kiyohara int sc_rw;
70 1.1 kiyohara };
71 1.1 kiyohara
72 1.1 kiyohara static int rtciic_match(device_t, cfdata_t , void *);
73 1.1 kiyohara static void rtciic_attach(device_t, device_t, void *);
74 1.1 kiyohara
75 1.1 kiyohara static int rtciic_acquire_bus(void *, int);
76 1.1 kiyohara static void rtciic_release_bus(void *, int);
77 1.1 kiyohara static int rtciic_send_start(void *, int);
78 1.1 kiyohara static int rtciic_send_stop(void *, int);
79 1.1 kiyohara static int rtciic_initiate_xfer(void *, i2c_addr_t, int);
80 1.1 kiyohara static int rtciic_read_byte(void *, uint8_t *, int);
81 1.1 kiyohara static int rtciic_write_byte(void *, uint8_t, int);
82 1.1 kiyohara
83 1.1 kiyohara static void rtciic_set_dir(void *, uint32_t);
84 1.1 kiyohara static void rtciic_set_bits(void *, uint32_t);
85 1.1 kiyohara static uint32_t rtciic_read_bits(void *);
86 1.1 kiyohara
87 1.1 kiyohara CFATTACH_DECL_NEW(rtciic, sizeof(struct rtciic_softc),
88 1.1 kiyohara rtciic_match, rtciic_attach, NULL, NULL);
89 1.1 kiyohara
90 1.1 kiyohara static int
91 1.1 kiyohara rtciic_match(device_t parent, cfdata_t match, void *aux)
92 1.1 kiyohara {
93 1.1 kiyohara struct mainbus_attach_args *ma = aux;
94 1.1 kiyohara
95 1.1 kiyohara if (strcmp(ma->ma_name, match->cf_name) != 0)
96 1.1 kiyohara return 0;
97 1.1 kiyohara
98 1.1 kiyohara /* Disallow wildcarded values. */
99 1.1 kiyohara if (ma->ma_addr1 == MAINBUSCF_ADDR1_DEFAULT)
100 1.1 kiyohara return 0;
101 1.1 kiyohara
102 1.1 kiyohara /* no irq */
103 1.1 kiyohara if (ma->ma_irq1 != MAINBUSCF_IRQ1_DEFAULT)
104 1.1 kiyohara return 0;
105 1.1 kiyohara
106 1.1 kiyohara return 1;
107 1.1 kiyohara }
108 1.1 kiyohara
109 1.1 kiyohara void
110 1.1 kiyohara rtciic_attach(device_t parent, device_t self, void *aux)
111 1.1 kiyohara {
112 1.1 kiyohara struct rtciic_softc *sc = device_private(self);
113 1.1 kiyohara struct mainbus_attach_args *ma = aux;
114 1.1 kiyohara struct i2cbus_attach_args iba;
115 1.1 kiyohara
116 1.1 kiyohara sc->sc_dev = self;
117 1.1 kiyohara
118 1.1 kiyohara aprint_normal("\n");
119 1.1 kiyohara aprint_naive("\n");
120 1.1 kiyohara
121 1.1 kiyohara /* Map I/O space(16bit). */
122 1.1 kiyohara sc->sc_iot = 0;
123 1.1 kiyohara if (bus_space_map(sc->sc_iot, ma->ma_addr1, 2, 0, &sc->sc_ioh)) {
124 1.1 kiyohara aprint_error_dev(self, "can't map registers\n");
125 1.1 kiyohara return;
126 1.1 kiyohara }
127 1.1 kiyohara sc->sc_rw = RTCIIC_READ(sc) & RTCIIC_RW;
128 1.1 kiyohara
129 1.1 kiyohara /* register with iic */
130 1.1 kiyohara sc->sc_i2c.ic_cookie = sc;
131 1.1 kiyohara sc->sc_i2c.ic_acquire_bus = rtciic_acquire_bus;
132 1.1 kiyohara sc->sc_i2c.ic_release_bus = rtciic_release_bus;
133 1.1 kiyohara sc->sc_i2c.ic_exec = NULL;
134 1.1 kiyohara sc->sc_i2c.ic_send_start = rtciic_send_start;
135 1.1 kiyohara sc->sc_i2c.ic_send_stop = rtciic_send_stop;
136 1.1 kiyohara sc->sc_i2c.ic_initiate_xfer = rtciic_initiate_xfer;
137 1.1 kiyohara sc->sc_i2c.ic_read_byte = rtciic_read_byte;
138 1.1 kiyohara sc->sc_i2c.ic_write_byte = rtciic_write_byte;
139 1.1 kiyohara
140 1.1 kiyohara sc->sc_bops.ibo_set_dir = rtciic_set_dir;
141 1.1 kiyohara sc->sc_bops.ibo_set_bits = rtciic_set_bits;
142 1.1 kiyohara sc->sc_bops.ibo_read_bits = rtciic_read_bits;
143 1.1 kiyohara sc->sc_bops.ibo_bits[I2C_BIT_SDA] = RTCIIC_SDAW;
144 1.1 kiyohara sc->sc_bops.ibo_bits[I2C_BIT_SCL] = RTCIIC_SCL;
145 1.1 kiyohara sc->sc_bops.ibo_bits[I2C_BIT_OUTPUT] = 0;
146 1.1 kiyohara sc->sc_bops.ibo_bits[I2C_BIT_INPUT] = RTCIIC_RW;
147 1.1 kiyohara
148 1.1 kiyohara memset(&iba, 0, sizeof(iba));
149 1.1 kiyohara iba.iba_tag = &sc->sc_i2c;
150 1.1 kiyohara (void) config_found_ia(sc->sc_dev, "i2cbus", &iba, iicbus_print);
151 1.1 kiyohara }
152 1.1 kiyohara
153 1.1 kiyohara
154 1.1 kiyohara static int
155 1.1 kiyohara rtciic_acquire_bus(void *cookie, int flags)
156 1.1 kiyohara {
157 1.1 kiyohara
158 1.1 kiyohara return 0;
159 1.1 kiyohara }
160 1.1 kiyohara
161 1.1 kiyohara static void
162 1.1 kiyohara rtciic_release_bus(void *cookie, int flags)
163 1.1 kiyohara {
164 1.1 kiyohara /* nothing */
165 1.1 kiyohara }
166 1.1 kiyohara
167 1.1 kiyohara static int
168 1.1 kiyohara rtciic_send_start(void *arg, int flags)
169 1.1 kiyohara {
170 1.1 kiyohara struct rtciic_softc *sc = arg;
171 1.1 kiyohara
172 1.1 kiyohara return i2c_bitbang_send_start(sc, flags, &sc->sc_bops);
173 1.1 kiyohara }
174 1.1 kiyohara
175 1.1 kiyohara static int
176 1.1 kiyohara rtciic_send_stop(void *arg, int flags)
177 1.1 kiyohara {
178 1.1 kiyohara struct rtciic_softc *sc = arg;
179 1.1 kiyohara
180 1.1 kiyohara return i2c_bitbang_send_stop(sc, flags, &sc->sc_bops);
181 1.1 kiyohara }
182 1.1 kiyohara
183 1.1 kiyohara static int
184 1.1 kiyohara rtciic_initiate_xfer(void *arg, i2c_addr_t addr, int flags)
185 1.1 kiyohara {
186 1.1 kiyohara struct rtciic_softc *sc = arg;
187 1.1 kiyohara
188 1.1 kiyohara return i2c_bitbang_initiate_xfer(sc, addr, flags, &sc->sc_bops);
189 1.1 kiyohara }
190 1.1 kiyohara
191 1.1 kiyohara static int
192 1.1 kiyohara rtciic_read_byte(void *arg, uint8_t *vp, int flags)
193 1.1 kiyohara {
194 1.1 kiyohara struct rtciic_softc *sc = arg;
195 1.1 kiyohara
196 1.1 kiyohara return i2c_bitbang_read_byte(sc, vp, flags, &sc->sc_bops);
197 1.1 kiyohara }
198 1.1 kiyohara
199 1.1 kiyohara static int
200 1.1 kiyohara rtciic_write_byte(void *arg, uint8_t v, int flags)
201 1.1 kiyohara {
202 1.1 kiyohara struct rtciic_softc *sc = arg;
203 1.1 kiyohara
204 1.1 kiyohara return i2c_bitbang_write_byte(sc, v, flags, &sc->sc_bops);
205 1.1 kiyohara }
206 1.1 kiyohara
207 1.1 kiyohara
208 1.1 kiyohara static void
209 1.1 kiyohara rtciic_set_dir(void *arg, uint32_t bits)
210 1.1 kiyohara {
211 1.1 kiyohara struct rtciic_softc *sc = arg;
212 1.1 kiyohara uint16_t reg;
213 1.1 kiyohara
214 1.1 kiyohara DPRINTF(("%s: set dir %s\n",
215 1.1 kiyohara device_xname(sc->sc_dev), (bits & RTCIIC_RW) ? "READ" : "WRITE"));
216 1.1 kiyohara
217 1.1 kiyohara if (sc->sc_rw != (bits & RTCIIC_RW)) {
218 1.1 kiyohara reg = RTCIIC_READ(sc);
219 1.1 kiyohara reg &= ~RTCIIC_RW;
220 1.1 kiyohara reg |= bits;
221 1.1 kiyohara RTCIIC_WRITE(sc, reg);
222 1.1 kiyohara delay(30);
223 1.1 kiyohara sc->sc_rw = bits & RTCIIC_RW;
224 1.1 kiyohara }
225 1.1 kiyohara }
226 1.1 kiyohara
227 1.1 kiyohara static void
228 1.1 kiyohara rtciic_set_bits(void *arg, uint32_t bits)
229 1.1 kiyohara {
230 1.1 kiyohara struct rtciic_softc *sc = arg;
231 1.1 kiyohara
232 1.1 kiyohara DPRINTF(("%s: %s\n",
233 1.1 kiyohara device_xname(sc->sc_dev),
234 1.1 kiyohara (bits == (RTCIIC_SDAW | RTCIIC_SCL)) ? "set SDA/SCL" :
235 1.1 kiyohara ((bits == RTCIIC_SDAW) ? "set SDA" :
236 1.1 kiyohara ((bits == RTCIIC_SCL) ? "set SCL" : "reset"))));
237 1.1 kiyohara
238 1.1 kiyohara if (sc->sc_rw & RTCIIC_RW) {
239 1.1 kiyohara bits &= RTCIIC_SCL;
240 1.1 kiyohara bits |= RTCIIC_RW;
241 1.1 kiyohara }
242 1.1 kiyohara RTCIIC_WRITE(sc, bits);
243 1.1 kiyohara delay(40);
244 1.1 kiyohara }
245 1.1 kiyohara
246 1.1 kiyohara static uint32_t
247 1.1 kiyohara rtciic_read_bits(void *arg)
248 1.1 kiyohara {
249 1.1 kiyohara struct rtciic_softc *sc = arg;
250 1.1 kiyohara uint8_t rv, v;
251 1.1 kiyohara
252 1.1 kiyohara v = RTCIIC_READ(sc);
253 1.1 kiyohara rv = v & RTCIIC_SCL;
254 1.1 kiyohara if (v & RTCIIC_SDAR)
255 1.1 kiyohara rv |= RTCIIC_SDAW;
256 1.1 kiyohara
257 1.1 kiyohara DPRINTF(("%s: read %s\n",
258 1.1 kiyohara device_xname(sc->sc_dev),
259 1.1 kiyohara (rv == (RTCIIC_SDAW | RTCIIC_SCL)) ? "SDA/SCL" :
260 1.1 kiyohara ((rv == RTCIIC_SDAW) ? "SDA" :
261 1.1 kiyohara ((rv == RTCIIC_SCL) ? "SCL" : "no"))));
262 1.1 kiyohara
263 1.1 kiyohara return (uint32_t)rv;
264 1.1 kiyohara }
265