1 /* $NetBSD: pic16lc.c,v 1.14 2008/05/05 00:21:04 jmcneill Exp $ */
4 * Copyright (c) 2007, 2008 Jared D. McNeill <jmcneill@invisible.ca>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 * Microsoft XBOX System Management Controller
32 * Documentation lives here:
33 * http://www.xbox-linux.org/wiki/PIC
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: pic16lc.c,v 1.14 2008/05/05 00:21:04 jmcneill Exp $");
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 #include <sys/malloc.h>
44 #include <machine/xbox.h>
46 #include <dev/sysmon/sysmonvar.h>
47 #include <dev/i2c/i2cvar.h>
48 #include <dev/i2c/pic16lcreg.h>
50 static int pic16lc_match(device_t
, cfdata_t
, void *);
51 static void pic16lc_attach(device_t
, device_t
, void *);
53 static int pic16lc_intr(void *);
55 void pic16lc_reboot(void);
56 void pic16lc_poweroff(void);
57 void pic16lc_setled(uint8_t);
59 struct pic16lc_softc
{
66 envsys_data_t sc_sensor
[1];
67 struct sysmon_envsys
*sc_sme
;
70 static struct pic16lc_softc
*pic16lc
= NULL
;
72 #define XBOX_SENSOR_CPU 0
73 #define XBOX_SENSOR_BOARD 1
74 #define XBOX_NSENSORS 2
76 static void pic16lc_update(struct pic16lc_softc
*, envsys_data_t
*);
77 static void pic16lc_refresh(struct sysmon_envsys
*, envsys_data_t
*);
79 static void pic16lc_write_1(struct pic16lc_softc
*, uint8_t, uint8_t);
80 static void pic16lc_read_1(struct pic16lc_softc
*, uint8_t, uint8_t *);
82 CFATTACH_DECL_NEW(pic16lc
, sizeof(struct pic16lc_softc
),
83 pic16lc_match
, pic16lc_attach
, NULL
, NULL
);
86 pic16lc_match(device_t parent
, cfdata_t cf
, void *opaque
)
88 struct i2c_attach_args
*ia
= opaque
;
90 if (pic16lc
!= NULL
) /* we can only have one... */
92 if (ia
->ia_addr
!= 0x10) /* must live at 0x10 on xbox */
99 pic16lc_attach(device_t parent
, device_t self
, void *opaque
)
101 struct pic16lc_softc
*sc
= device_private(self
);
102 struct i2c_attach_args
*ia
= opaque
;
107 sc
->sc_tag
= ia
->ia_tag
;
108 sc
->sc_addr
= ia
->ia_addr
;
112 sc
->sc_sme
= sysmon_envsys_create();
114 /* initialize CPU sensor */
115 sc
->sc_sensor
[XBOX_SENSOR_CPU
].units
= ENVSYS_STEMP
;
116 (void)strlcpy(sc
->sc_sensor
[XBOX_SENSOR_CPU
].desc
, "Xbox CPU Temp",
117 sizeof(sc
->sc_sensor
[XBOX_SENSOR_CPU
]));
118 /* initialize board sensor */
119 sc
->sc_sensor
[XBOX_SENSOR_BOARD
].units
= ENVSYS_STEMP
;
120 (void)strlcpy(sc
->sc_sensor
[XBOX_SENSOR_BOARD
].desc
, "Xbox Board Temp",
121 sizeof(sc
->sc_sensor
[XBOX_SENSOR_BOARD
]));
123 for (i
= 0; i
< XBOX_NSENSORS
; i
++) {
124 if (sysmon_envsys_sensor_attach(sc
->sc_sme
,
125 &sc
->sc_sensor
[i
])) {
126 sysmon_envsys_destroy(sc
->sc_sme
);
131 /* hook into sysmon */
132 sc
->sc_sme
->sme_name
= device_xname(sc
->sc_dev
);
133 sc
->sc_sme
->sme_cookie
= sc
;
134 sc
->sc_sme
->sme_refresh
= pic16lc_refresh
;
136 if (sysmon_envsys_register(sc
->sc_sme
)) {
137 aprint_error_dev(self
, "unable to register with sysmon\n");
138 sysmon_envsys_destroy(sc
->sc_sme
);
142 if (iic_acquire_bus(sc
->sc_tag
, 0) != 0) {
143 aprint_error(": unable to acquire i2c bus\n");
147 aprint_normal(": Xbox System Management Controller");
149 /* find the PIC version */
150 pic16lc_write_1(sc
, PIC16LC_REG_VER
, 0x00);
151 pic16lc_read_1(sc
, PIC16LC_REG_VER
, &ver
[0]);
152 pic16lc_read_1(sc
, PIC16LC_REG_VER
, &ver
[1]);
153 pic16lc_read_1(sc
, PIC16LC_REG_VER
, &ver
[2]);
156 aprint_normal(" (rev. %s)\n", ver
);
158 pic16lc_setled(0xff); /* orange */
160 iic_release_bus(sc
->sc_tag
, 0);
162 /* disable reset on eject */
163 pic16lc_write_1(sc
, PIC16LC_REG_RESETONEJECT
, 0x01);
165 sc
->sc_ih
= iic_smbus_intr_establish_proc(sc
->sc_tag
, pic16lc_intr
, sc
);
166 if (sc
->sc_ih
== NULL
)
167 aprint_error_dev(self
, "couldn't establish interrupt\n");
173 pic16lc_write_1(struct pic16lc_softc
*sc
, uint8_t cmd
, uint8_t val
)
175 iic_exec(sc
->sc_tag
, I2C_OP_WRITE_WITH_STOP
,
176 sc
->sc_addr
, &cmd
, 1, &val
, 1, 0);
180 pic16lc_read_1(struct pic16lc_softc
*sc
, uint8_t cmd
, uint8_t *valp
)
182 iic_exec(sc
->sc_tag
, I2C_OP_READ_WITH_STOP
,
183 sc
->sc_addr
, &cmd
, 1, valp
, 1, 0);
187 pic16lc_update(struct pic16lc_softc
*sc
, envsys_data_t
*edata
)
189 uint8_t cputemp
, boardtemp
;
191 if (iic_acquire_bus(sc
->sc_tag
, 0) != 0) {
192 aprint_error(": unable to acquire i2c bus\n");
196 if (edata
->sensor
== XBOX_SENSOR_CPU
) {
197 pic16lc_read_1(sc
, PIC16LC_REG_CPUTEMP
, &cputemp
);
198 edata
->state
= ENVSYS_SVALID
;
199 edata
->value_cur
= (int)cputemp
* 1000000 + 273150000;
201 pic16lc_read_1(sc
, PIC16LC_REG_BOARDTEMP
, &boardtemp
);
202 edata
->state
= ENVSYS_SVALID
;
203 edata
->value_cur
= (int)boardtemp
* 1000000 + 273150000;
206 iic_release_bus(sc
->sc_tag
, 0);
212 pic16lc_refresh(struct sysmon_envsys
*sme
, envsys_data_t
*edata
)
214 struct pic16lc_softc
*sc
= sme
->sme_cookie
;
216 pic16lc_update(sc
, edata
);
220 pic16lc_intr(void *opaque
)
222 struct pic16lc_softc
*sc
= opaque
;
226 pic16lc_read_1(sc
, PIC16LC_REG_INTSTATUS
, &val
);
230 if (val
& PIC16LC_REG_INTSTATUS_POWER
)
231 aprint_normal_dev(sc
->sc_dev
, "power button pressed\n");
232 if (val
& PIC16LC_REG_INTSTATUS_TRAYCLOSED
)
233 aprint_normal_dev(sc
->sc_dev
, "tray closed\n");
234 if (val
& PIC16LC_REG_INTSTATUS_TRAYOPENING
) {
235 aprint_normal_dev(sc
->sc_dev
, "tray opening\n");
236 pic16lc_write_1(sc
, PIC16LC_REG_INTACK
, 0x02);
238 if (val
& PIC16LC_REG_INTSTATUS_AVPACK_UNPLUG
)
239 aprint_normal_dev(sc
->sc_dev
, "A/V pack removed\n");
240 if (val
& PIC16LC_REG_INTSTATUS_AVPACK_PLUG
)
241 aprint_normal_dev(sc
->sc_dev
, "A/V pack inserted\n");
242 if (val
& PIC16LC_REG_INTSTATUS_EJECT_BUTTON
) {
243 pic16lc_write_1(sc
, PIC16LC_REG_INTACK
, 0x04);
244 pic16lc_write_1(sc
, PIC16LC_REG_TRAYEJECT
, 0x00);
247 if (val
& PIC16LC_REG_INTSTATUS_TRAYCLOSING
)
248 aprint_normal_dev(sc
->sc_dev
, "tray closing\n");
256 if (pic16lc
== NULL
) {
257 printf("pic16lc_reboot: driver not attached!\n");
260 pic16lc_write_1(pic16lc
, PIC16LC_REG_POWER
, PIC16LC_REG_POWER_RESET
);
264 pic16lc_poweroff(void)
266 if (pic16lc
== NULL
) {
267 printf("pic16lc_poweroff: driver not attached!\n");
270 pic16lc_write_1(pic16lc
, PIC16LC_REG_POWER
, PIC16LC_REG_POWER_SHUTDOWN
);
274 pic16lc_setled(uint8_t val
)
276 if (pic16lc
== NULL
) {
277 printf("pic16lc_setled: driver not attached!\n");
280 pic16lc_write_1(pic16lc
, PIC16LC_REG_LEDSEQ
, val
);
281 pic16lc_write_1(pic16lc
, PIC16LC_REG_LEDMODE
, 0x01);