nbppcon.c revision 1.3.16.1 1 /* $NetBSD: nbppcon.c,v 1.3.16.1 2021/03/20 19:33:35 thorpej Exp $ */
2 /*
3 * Copyright (c) 2011 KIYOHARA Takashi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: nbppcon.c,v 1.3.16.1 2021/03/20 19:33:35 thorpej Exp $");
29
30 #include <sys/param.h>
31 #include <sys/device.h>
32 #include <sys/errno.h>
33
34 #include <machine/platid.h>
35 #include <machine/platid_mask.h>
36
37 #include <arm/xscale/pxa2x0_i2c.h>
38
39 #include <hpcarm/dev/nbppconvar.h>
40
41 #include <dev/i2c/i2cvar.h>
42 #include <dev/i2c/at24cxxvar.h>
43
44 #include "locators.h"
45
46 #define NBPPCON_ADDR 0x13
47
48
49 struct nbppcon_softc {
50 device_t sc_dev;
51 i2c_tag_t sc_tag;
52 int sc_address;
53
54 struct callback {
55 int cb_tag;
56 int (*cb_func)(void *, int, char *);
57 void *cb_arg;
58 } sc_cbs[2];
59 };
60
61 static int nbppcon_match(device_t, cfdata_t, void *);
62 static void nbppcon_attach(device_t, device_t, void *);
63
64 static int nbppcon_search(device_t, cfdata_t, const int *, void *);
65 static int nbppcon_print(void *aux, const char *pnp);
66
67 CFATTACH_DECL_NEW(nbppcon, sizeof(struct nbppcon_softc),
68 nbppcon_match, nbppcon_attach, NULL, NULL);
69
70
71 /* ARGSUSED */
72 static int
73 nbppcon_match(device_t parent, cfdata_t match, void *aux)
74 {
75 struct i2c_attach_args *ia = aux;
76
77 if (ia->ia_addr != NBPPCON_ADDR ||
78 !platid_match(&platid, &platid_mask_MACH_PSIONTEKLOGIX_NETBOOK_PRO))
79 return 0;
80
81 return I2C_MATCH_ADDRESS_AND_PROBE;
82 }
83
84 /* ARGSUSED */
85 static void
86 nbppcon_attach(device_t parent, device_t self, void *aux)
87 {
88 struct nbppcon_softc *sc = device_private(self);
89 struct i2c_attach_args *ia = aux;
90 int rv;
91 char buf[128];
92
93 sc->sc_dev = self;
94 sc->sc_tag = ia->ia_tag;
95 sc->sc_address = ia->ia_addr;
96
97 aprint_naive("\n");
98 aprint_normal(": NETBOOK PRO PCon\n");
99
100 #define NVRAM_ADDR 0x53
101 #define NVRAM_DATA_REV 0x14
102 rv = seeprom_bootstrap_read(sc->sc_tag, NVRAM_ADDR, 0x00, 128,
103 buf, sizeof(buf));
104 if (rv == 0)
105 aprint_normal_dev(self, "NETBOOK PRO Rev.%c\n",
106 buf[NVRAM_DATA_REV] - '0' + '@');
107 else
108 aprint_error_dev(self, "NVRAM read failed\n");
109
110 config_search(self, NULL,
111 CFARG_SUBMATCH, nbppcon_search,
112 CFARG_IATTR, "nbppcon",
113 CFARG_EOL);
114 }
115
116 /* ARGSUSED */
117 static int
118 nbppcon_search(device_t self, cfdata_t cf, const int *ldesc, void *aux)
119 {
120 struct nbppcon_attach_args pcon;
121
122 pcon.aa_name = cf->cf_name;
123 pcon.aa_tag = cf->cf_loc[NBPPCONCF_TAG];
124
125 if (config_match(self, cf, &pcon))
126 config_attach(self, cf, &pcon, nbppcon_print);
127
128 return 0;
129 }
130
131 /* ARGSUSED */
132 static int
133 nbppcon_print(void *aux, const char *pnp)
134 {
135
136 if (pnp != NULL)
137 aprint_normal("%s", pnp);
138 return UNCONF;
139 }
140
141
142 void *
143 nbppcon_regist_callback(device_t self,
144 int tag, int (*func)(void *, int, char *), void *arg)
145 {
146 struct nbppcon_softc *sc = device_private(self);
147 int i;
148
149 for (i = 0; i < __arraycount(sc->sc_cbs); i++)
150 if (sc->sc_cbs[i].cb_func == NULL) {
151 sc->sc_cbs[i].cb_tag = tag;
152 sc->sc_cbs[i].cb_func = func;
153 sc->sc_cbs[i].cb_arg = arg;
154 return func;
155 }
156
157 return NULL;
158 }
159
160 int
161 nbppcon_intr(device_t self, int buflen, char *buf)
162 {
163 struct nbppcon_softc *sc = device_private(self);
164 struct callback *cb;
165 int i;
166
167 for (i = 0; i < __arraycount(sc->sc_cbs); i++) {
168 cb = &sc->sc_cbs[i];
169 if (cb->cb_func != NULL &&
170 cb->cb_tag == buf[0])
171 return cb->cb_func(cb->cb_arg, buflen, buf);
172 }
173 aprint_error_dev(sc->sc_dev, "unknown tag received: 0x%02x\n", buf[0]);
174 return 0;
175 }
176
177 int
178 nbppcon_poll(device_t self, int tag, int buflen, char *buf)
179 {
180 struct nbppcon_softc *sc = device_private(self);
181 int rv;
182
183 if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
184 aprint_error_dev(sc->sc_dev,
185 "%s: acquire bus failed\n", __func__);
186 return -1;
187 }
188
189 do {
190 extern int nbpiic_poll(void *, int, char *);
191
192 rv = nbpiic_poll(sc->sc_tag->ic_cookie, buflen, buf);
193 if (rv > 0 && buf[0] == tag)
194 break;
195 } while (rv == 0 || buf[0] != tag);
196
197 iic_release_bus(sc->sc_tag, I2C_F_POLL);
198
199 return rv;
200 }
201
202 int
203 nbppcon_kbd_ready(device_t self)
204 {
205 struct nbppcon_softc *sc = device_private(self);
206 int rv;
207 char cmd[1], data[1];
208
209 if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
210 aprint_error_dev(sc->sc_dev,
211 "%s: acquire bus failed\n", __func__);
212 return -1;
213 }
214
215 cmd[0] = 0x00; /* Keyboard */
216 data[0] = 0x00; /* 0x00:Ready, 0x01:Busy? */
217 rv = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
218 cmd, sizeof(cmd), data, sizeof(data), I2C_F_POLL);
219
220 cpu_dcache_wbinv_all(); /* XXXX: Why? */
221
222 iic_release_bus(sc->sc_tag, I2C_F_POLL);
223
224 return rv;
225 }
226
227 int
228 nbppcon_pwr_hwreset(device_t self)
229 {
230 struct nbppcon_softc *sc = device_private(self);
231 int rv;
232 char cmd[1], data[1];
233
234 if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
235 aprint_error_dev(sc->sc_dev,
236 "%s: acquire bus failed\n", __func__);
237 return -1;
238 }
239
240 cmd[0] = 0x05; /* POWER */
241 data[0] = 0x00; /* HWReset */
242 #if 0
243 rv = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
244 cmd, sizeof(cmd), data, sizeof(data), I2C_F_POLL);
245 #else /* XXXX: Oops, ensure HWReset, ignore cmd and data. */
246 rv = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address + 1,
247 cmd, sizeof(cmd), data, sizeof(data), I2C_F_POLL);
248 #endif
249
250 iic_release_bus(sc->sc_tag, I2C_F_POLL);
251
252 return rv;
253 }
254