nbppcon.c revision 1.5 1 1.5 thorpej /* $NetBSD: nbppcon.c,v 1.5 2021/08/07 16:18:53 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.5 thorpej __KERNEL_RCSID(0, "$NetBSD: nbppcon.c,v 1.5 2021/08/07 16:18:53 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.4 thorpej config_search(self, NULL,
111 1.5 thorpej CFARGS(.search = nbppcon_search));
112 1.1 kiyohara }
113 1.1 kiyohara
114 1.1 kiyohara /* ARGSUSED */
115 1.1 kiyohara static int
116 1.1 kiyohara nbppcon_search(device_t self, cfdata_t cf, const int *ldesc, void *aux)
117 1.1 kiyohara {
118 1.1 kiyohara struct nbppcon_attach_args pcon;
119 1.1 kiyohara
120 1.1 kiyohara pcon.aa_name = cf->cf_name;
121 1.1 kiyohara pcon.aa_tag = cf->cf_loc[NBPPCONCF_TAG];
122 1.1 kiyohara
123 1.4 thorpej if (config_probe(self, cf, &pcon))
124 1.5 thorpej config_attach(self, cf, &pcon, nbppcon_print, CFARGS_NONE);
125 1.1 kiyohara
126 1.1 kiyohara return 0;
127 1.1 kiyohara }
128 1.1 kiyohara
129 1.1 kiyohara /* ARGSUSED */
130 1.1 kiyohara static int
131 1.1 kiyohara nbppcon_print(void *aux, const char *pnp)
132 1.1 kiyohara {
133 1.1 kiyohara
134 1.1 kiyohara if (pnp != NULL)
135 1.1 kiyohara aprint_normal("%s", pnp);
136 1.1 kiyohara return UNCONF;
137 1.1 kiyohara }
138 1.1 kiyohara
139 1.1 kiyohara
140 1.1 kiyohara void *
141 1.1 kiyohara nbppcon_regist_callback(device_t self,
142 1.1 kiyohara int tag, int (*func)(void *, int, char *), void *arg)
143 1.1 kiyohara {
144 1.1 kiyohara struct nbppcon_softc *sc = device_private(self);
145 1.1 kiyohara int i;
146 1.1 kiyohara
147 1.1 kiyohara for (i = 0; i < __arraycount(sc->sc_cbs); i++)
148 1.1 kiyohara if (sc->sc_cbs[i].cb_func == NULL) {
149 1.1 kiyohara sc->sc_cbs[i].cb_tag = tag;
150 1.1 kiyohara sc->sc_cbs[i].cb_func = func;
151 1.1 kiyohara sc->sc_cbs[i].cb_arg = arg;
152 1.1 kiyohara return func;
153 1.1 kiyohara }
154 1.1 kiyohara
155 1.1 kiyohara return NULL;
156 1.1 kiyohara }
157 1.1 kiyohara
158 1.1 kiyohara int
159 1.1 kiyohara nbppcon_intr(device_t self, int buflen, char *buf)
160 1.1 kiyohara {
161 1.1 kiyohara struct nbppcon_softc *sc = device_private(self);
162 1.1 kiyohara struct callback *cb;
163 1.1 kiyohara int i;
164 1.1 kiyohara
165 1.1 kiyohara for (i = 0; i < __arraycount(sc->sc_cbs); i++) {
166 1.1 kiyohara cb = &sc->sc_cbs[i];
167 1.1 kiyohara if (cb->cb_func != NULL &&
168 1.1 kiyohara cb->cb_tag == buf[0])
169 1.1 kiyohara return cb->cb_func(cb->cb_arg, buflen, buf);
170 1.1 kiyohara }
171 1.1 kiyohara aprint_error_dev(sc->sc_dev, "unknown tag received: 0x%02x\n", buf[0]);
172 1.1 kiyohara return 0;
173 1.1 kiyohara }
174 1.1 kiyohara
175 1.1 kiyohara int
176 1.1 kiyohara nbppcon_poll(device_t self, int tag, int buflen, char *buf)
177 1.1 kiyohara {
178 1.1 kiyohara struct nbppcon_softc *sc = device_private(self);
179 1.1 kiyohara int rv;
180 1.1 kiyohara
181 1.1 kiyohara if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
182 1.1 kiyohara aprint_error_dev(sc->sc_dev,
183 1.1 kiyohara "%s: acquire bus failed\n", __func__);
184 1.1 kiyohara return -1;
185 1.1 kiyohara }
186 1.1 kiyohara
187 1.1 kiyohara do {
188 1.1 kiyohara extern int nbpiic_poll(void *, int, char *);
189 1.1 kiyohara
190 1.1 kiyohara rv = nbpiic_poll(sc->sc_tag->ic_cookie, buflen, buf);
191 1.1 kiyohara if (rv > 0 && buf[0] == tag)
192 1.1 kiyohara break;
193 1.1 kiyohara } while (rv == 0 || buf[0] != tag);
194 1.1 kiyohara
195 1.1 kiyohara iic_release_bus(sc->sc_tag, I2C_F_POLL);
196 1.1 kiyohara
197 1.1 kiyohara return rv;
198 1.1 kiyohara }
199 1.1 kiyohara
200 1.1 kiyohara int
201 1.1 kiyohara nbppcon_kbd_ready(device_t self)
202 1.1 kiyohara {
203 1.1 kiyohara struct nbppcon_softc *sc = device_private(self);
204 1.1 kiyohara int rv;
205 1.1 kiyohara char cmd[1], data[1];
206 1.1 kiyohara
207 1.1 kiyohara if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
208 1.1 kiyohara aprint_error_dev(sc->sc_dev,
209 1.1 kiyohara "%s: acquire bus failed\n", __func__);
210 1.1 kiyohara return -1;
211 1.1 kiyohara }
212 1.1 kiyohara
213 1.1 kiyohara cmd[0] = 0x00; /* Keyboard */
214 1.1 kiyohara data[0] = 0x00; /* 0x00:Ready, 0x01:Busy? */
215 1.1 kiyohara rv = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
216 1.1 kiyohara cmd, sizeof(cmd), data, sizeof(data), I2C_F_POLL);
217 1.1 kiyohara
218 1.1 kiyohara cpu_dcache_wbinv_all(); /* XXXX: Why? */
219 1.1 kiyohara
220 1.1 kiyohara iic_release_bus(sc->sc_tag, I2C_F_POLL);
221 1.1 kiyohara
222 1.1 kiyohara return rv;
223 1.1 kiyohara }
224 1.1 kiyohara
225 1.1 kiyohara int
226 1.1 kiyohara nbppcon_pwr_hwreset(device_t self)
227 1.1 kiyohara {
228 1.1 kiyohara struct nbppcon_softc *sc = device_private(self);
229 1.1 kiyohara int rv;
230 1.1 kiyohara char cmd[1], data[1];
231 1.1 kiyohara
232 1.1 kiyohara if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
233 1.1 kiyohara aprint_error_dev(sc->sc_dev,
234 1.1 kiyohara "%s: acquire bus failed\n", __func__);
235 1.1 kiyohara return -1;
236 1.1 kiyohara }
237 1.1 kiyohara
238 1.1 kiyohara cmd[0] = 0x05; /* POWER */
239 1.1 kiyohara data[0] = 0x00; /* HWReset */
240 1.1 kiyohara #if 0
241 1.1 kiyohara rv = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
242 1.1 kiyohara cmd, sizeof(cmd), data, sizeof(data), I2C_F_POLL);
243 1.1 kiyohara #else /* XXXX: Oops, ensure HWReset, ignore cmd and data. */
244 1.1 kiyohara rv = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address + 1,
245 1.1 kiyohara cmd, sizeof(cmd), data, sizeof(data), I2C_F_POLL);
246 1.1 kiyohara #endif
247 1.1 kiyohara
248 1.1 kiyohara iic_release_bus(sc->sc_tag, I2C_F_POLL);
249 1.1 kiyohara
250 1.1 kiyohara return rv;
251 1.1 kiyohara }
252