nbppcon.c revision 1.3 1 1.3 thorpej /* $NetBSD: nbppcon.c,v 1.3 2018/06/16 21:22:13 thorpej 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 #include <sys/cdefs.h>
28 1.3 thorpej __KERNEL_RCSID(0, "$NetBSD: nbppcon.c,v 1.3 2018/06/16 21:22:13 thorpej Exp $");
29 1.1 kiyohara
30 1.1 kiyohara #include <sys/param.h>
31 1.1 kiyohara #include <sys/device.h>
32 1.1 kiyohara #include <sys/errno.h>
33 1.1 kiyohara
34 1.1 kiyohara #include <machine/platid.h>
35 1.1 kiyohara #include <machine/platid_mask.h>
36 1.1 kiyohara
37 1.1 kiyohara #include <arm/xscale/pxa2x0_i2c.h>
38 1.1 kiyohara
39 1.1 kiyohara #include <hpcarm/dev/nbppconvar.h>
40 1.1 kiyohara
41 1.1 kiyohara #include <dev/i2c/i2cvar.h>
42 1.1 kiyohara #include <dev/i2c/at24cxxvar.h>
43 1.1 kiyohara
44 1.1 kiyohara #include "locators.h"
45 1.1 kiyohara
46 1.1 kiyohara #define NBPPCON_ADDR 0x13
47 1.1 kiyohara
48 1.1 kiyohara
49 1.1 kiyohara struct nbppcon_softc {
50 1.1 kiyohara device_t sc_dev;
51 1.1 kiyohara i2c_tag_t sc_tag;
52 1.1 kiyohara int sc_address;
53 1.1 kiyohara
54 1.1 kiyohara struct callback {
55 1.1 kiyohara int cb_tag;
56 1.1 kiyohara int (*cb_func)(void *, int, char *);
57 1.1 kiyohara void *cb_arg;
58 1.1 kiyohara } sc_cbs[2];
59 1.1 kiyohara };
60 1.1 kiyohara
61 1.1 kiyohara static int nbppcon_match(device_t, cfdata_t, void *);
62 1.1 kiyohara static void nbppcon_attach(device_t, device_t, void *);
63 1.1 kiyohara
64 1.1 kiyohara static int nbppcon_search(device_t, cfdata_t, const int *, void *);
65 1.1 kiyohara static int nbppcon_print(void *aux, const char *pnp);
66 1.1 kiyohara
67 1.1 kiyohara CFATTACH_DECL_NEW(nbppcon, sizeof(struct nbppcon_softc),
68 1.1 kiyohara nbppcon_match, nbppcon_attach, NULL, NULL);
69 1.1 kiyohara
70 1.1 kiyohara
71 1.1 kiyohara /* ARGSUSED */
72 1.1 kiyohara static int
73 1.1 kiyohara nbppcon_match(device_t parent, cfdata_t match, void *aux)
74 1.1 kiyohara {
75 1.1 kiyohara struct i2c_attach_args *ia = aux;
76 1.1 kiyohara
77 1.1 kiyohara if (ia->ia_addr != NBPPCON_ADDR ||
78 1.1 kiyohara !platid_match(&platid, &platid_mask_MACH_PSIONTEKLOGIX_NETBOOK_PRO))
79 1.1 kiyohara return 0;
80 1.1 kiyohara
81 1.3 thorpej return I2C_MATCH_ADDRESS_AND_PROBE;
82 1.1 kiyohara }
83 1.1 kiyohara
84 1.1 kiyohara /* ARGSUSED */
85 1.1 kiyohara static void
86 1.1 kiyohara nbppcon_attach(device_t parent, device_t self, void *aux)
87 1.1 kiyohara {
88 1.1 kiyohara struct nbppcon_softc *sc = device_private(self);
89 1.1 kiyohara struct i2c_attach_args *ia = aux;
90 1.1 kiyohara int rv;
91 1.1 kiyohara char buf[128];
92 1.1 kiyohara
93 1.1 kiyohara sc->sc_dev = self;
94 1.1 kiyohara sc->sc_tag = ia->ia_tag;
95 1.1 kiyohara sc->sc_address = ia->ia_addr;
96 1.1 kiyohara
97 1.1 kiyohara aprint_naive("\n");
98 1.1 kiyohara aprint_normal(": NETBOOK PRO PCon\n");
99 1.1 kiyohara
100 1.1 kiyohara #define NVRAM_ADDR 0x53
101 1.1 kiyohara #define NVRAM_DATA_REV 0x14
102 1.1 kiyohara rv = seeprom_bootstrap_read(sc->sc_tag, NVRAM_ADDR, 0x00, 128,
103 1.1 kiyohara buf, sizeof(buf));
104 1.1 kiyohara if (rv == 0)
105 1.2 kiyohara aprint_normal_dev(self, "NETBOOK PRO Rev.%c\n",
106 1.1 kiyohara buf[NVRAM_DATA_REV] - '0' + '@');
107 1.1 kiyohara else
108 1.1 kiyohara aprint_error_dev(self, "NVRAM read failed\n");
109 1.1 kiyohara
110 1.1 kiyohara config_search_ia(nbppcon_search, self, "nbppcon", NULL);
111 1.1 kiyohara }
112 1.1 kiyohara
113 1.1 kiyohara /* ARGSUSED */
114 1.1 kiyohara static int
115 1.1 kiyohara nbppcon_search(device_t self, cfdata_t cf, const int *ldesc, void *aux)
116 1.1 kiyohara {
117 1.1 kiyohara struct nbppcon_attach_args pcon;
118 1.1 kiyohara
119 1.1 kiyohara pcon.aa_name = cf->cf_name;
120 1.1 kiyohara pcon.aa_tag = cf->cf_loc[NBPPCONCF_TAG];
121 1.1 kiyohara
122 1.1 kiyohara if (config_match(self, cf, &pcon))
123 1.1 kiyohara config_attach(self, cf, &pcon, nbppcon_print);
124 1.1 kiyohara
125 1.1 kiyohara return 0;
126 1.1 kiyohara }
127 1.1 kiyohara
128 1.1 kiyohara /* ARGSUSED */
129 1.1 kiyohara static int
130 1.1 kiyohara nbppcon_print(void *aux, const char *pnp)
131 1.1 kiyohara {
132 1.1 kiyohara
133 1.1 kiyohara if (pnp != NULL)
134 1.1 kiyohara aprint_normal("%s", pnp);
135 1.1 kiyohara return UNCONF;
136 1.1 kiyohara }
137 1.1 kiyohara
138 1.1 kiyohara
139 1.1 kiyohara void *
140 1.1 kiyohara nbppcon_regist_callback(device_t self,
141 1.1 kiyohara int tag, int (*func)(void *, int, char *), void *arg)
142 1.1 kiyohara {
143 1.1 kiyohara struct nbppcon_softc *sc = device_private(self);
144 1.1 kiyohara int i;
145 1.1 kiyohara
146 1.1 kiyohara for (i = 0; i < __arraycount(sc->sc_cbs); i++)
147 1.1 kiyohara if (sc->sc_cbs[i].cb_func == NULL) {
148 1.1 kiyohara sc->sc_cbs[i].cb_tag = tag;
149 1.1 kiyohara sc->sc_cbs[i].cb_func = func;
150 1.1 kiyohara sc->sc_cbs[i].cb_arg = arg;
151 1.1 kiyohara return func;
152 1.1 kiyohara }
153 1.1 kiyohara
154 1.1 kiyohara return NULL;
155 1.1 kiyohara }
156 1.1 kiyohara
157 1.1 kiyohara int
158 1.1 kiyohara nbppcon_intr(device_t self, int buflen, char *buf)
159 1.1 kiyohara {
160 1.1 kiyohara struct nbppcon_softc *sc = device_private(self);
161 1.1 kiyohara struct callback *cb;
162 1.1 kiyohara int i;
163 1.1 kiyohara
164 1.1 kiyohara for (i = 0; i < __arraycount(sc->sc_cbs); i++) {
165 1.1 kiyohara cb = &sc->sc_cbs[i];
166 1.1 kiyohara if (cb->cb_func != NULL &&
167 1.1 kiyohara cb->cb_tag == buf[0])
168 1.1 kiyohara return cb->cb_func(cb->cb_arg, buflen, buf);
169 1.1 kiyohara }
170 1.1 kiyohara aprint_error_dev(sc->sc_dev, "unknown tag received: 0x%02x\n", buf[0]);
171 1.1 kiyohara return 0;
172 1.1 kiyohara }
173 1.1 kiyohara
174 1.1 kiyohara int
175 1.1 kiyohara nbppcon_poll(device_t self, int tag, int buflen, char *buf)
176 1.1 kiyohara {
177 1.1 kiyohara struct nbppcon_softc *sc = device_private(self);
178 1.1 kiyohara int rv;
179 1.1 kiyohara
180 1.1 kiyohara if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
181 1.1 kiyohara aprint_error_dev(sc->sc_dev,
182 1.1 kiyohara "%s: acquire bus failed\n", __func__);
183 1.1 kiyohara return -1;
184 1.1 kiyohara }
185 1.1 kiyohara
186 1.1 kiyohara do {
187 1.1 kiyohara extern int nbpiic_poll(void *, int, char *);
188 1.1 kiyohara
189 1.1 kiyohara rv = nbpiic_poll(sc->sc_tag->ic_cookie, buflen, buf);
190 1.1 kiyohara if (rv > 0 && buf[0] == tag)
191 1.1 kiyohara break;
192 1.1 kiyohara } while (rv == 0 || buf[0] != tag);
193 1.1 kiyohara
194 1.1 kiyohara iic_release_bus(sc->sc_tag, I2C_F_POLL);
195 1.1 kiyohara
196 1.1 kiyohara return rv;
197 1.1 kiyohara }
198 1.1 kiyohara
199 1.1 kiyohara int
200 1.1 kiyohara nbppcon_kbd_ready(device_t self)
201 1.1 kiyohara {
202 1.1 kiyohara struct nbppcon_softc *sc = device_private(self);
203 1.1 kiyohara int rv;
204 1.1 kiyohara char cmd[1], data[1];
205 1.1 kiyohara
206 1.1 kiyohara if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
207 1.1 kiyohara aprint_error_dev(sc->sc_dev,
208 1.1 kiyohara "%s: acquire bus failed\n", __func__);
209 1.1 kiyohara return -1;
210 1.1 kiyohara }
211 1.1 kiyohara
212 1.1 kiyohara cmd[0] = 0x00; /* Keyboard */
213 1.1 kiyohara data[0] = 0x00; /* 0x00:Ready, 0x01:Busy? */
214 1.1 kiyohara rv = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
215 1.1 kiyohara cmd, sizeof(cmd), data, sizeof(data), I2C_F_POLL);
216 1.1 kiyohara
217 1.1 kiyohara cpu_dcache_wbinv_all(); /* XXXX: Why? */
218 1.1 kiyohara
219 1.1 kiyohara iic_release_bus(sc->sc_tag, I2C_F_POLL);
220 1.1 kiyohara
221 1.1 kiyohara return rv;
222 1.1 kiyohara }
223 1.1 kiyohara
224 1.1 kiyohara int
225 1.1 kiyohara nbppcon_pwr_hwreset(device_t self)
226 1.1 kiyohara {
227 1.1 kiyohara struct nbppcon_softc *sc = device_private(self);
228 1.1 kiyohara int rv;
229 1.1 kiyohara char cmd[1], data[1];
230 1.1 kiyohara
231 1.1 kiyohara if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
232 1.1 kiyohara aprint_error_dev(sc->sc_dev,
233 1.1 kiyohara "%s: acquire bus failed\n", __func__);
234 1.1 kiyohara return -1;
235 1.1 kiyohara }
236 1.1 kiyohara
237 1.1 kiyohara cmd[0] = 0x05; /* POWER */
238 1.1 kiyohara data[0] = 0x00; /* HWReset */
239 1.1 kiyohara #if 0
240 1.1 kiyohara rv = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
241 1.1 kiyohara cmd, sizeof(cmd), data, sizeof(data), I2C_F_POLL);
242 1.1 kiyohara #else /* XXXX: Oops, ensure HWReset, ignore cmd and data. */
243 1.1 kiyohara rv = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address + 1,
244 1.1 kiyohara cmd, sizeof(cmd), data, sizeof(data), I2C_F_POLL);
245 1.1 kiyohara #endif
246 1.1 kiyohara
247 1.1 kiyohara iic_release_bus(sc->sc_tag, I2C_F_POLL);
248 1.1 kiyohara
249 1.1 kiyohara return rv;
250 1.1 kiyohara }
251