xref: /onnv-gate/usr/src/uts/sun4u/io/pic16f747.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Driver to map the PIC for the chicago platform.
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate #include <sys/types.h>
33*0Sstevel@tonic-gate #include <sys/time.h>
34*0Sstevel@tonic-gate #include <sys/errno.h>
35*0Sstevel@tonic-gate #include <sys/cmn_err.h>
36*0Sstevel@tonic-gate #include <sys/param.h>
37*0Sstevel@tonic-gate #include <sys/modctl.h>
38*0Sstevel@tonic-gate #include <sys/conf.h>
39*0Sstevel@tonic-gate #include <sys/open.h>
40*0Sstevel@tonic-gate #include <sys/stat.h>
41*0Sstevel@tonic-gate #include <sys/clock.h>
42*0Sstevel@tonic-gate #include <sys/pic.h>
43*0Sstevel@tonic-gate #include <sys/pic16f747.h>
44*0Sstevel@tonic-gate #include <sys/ddi.h>
45*0Sstevel@tonic-gate #include <sys/sunddi.h>
46*0Sstevel@tonic-gate #include <sys/file.h>
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate /* dev_ops and cb_ops entry point function declarations */
49*0Sstevel@tonic-gate static int	pic_attach(dev_info_t *, ddi_attach_cmd_t);
50*0Sstevel@tonic-gate static int	pic_detach(dev_info_t *, ddi_detach_cmd_t);
51*0Sstevel@tonic-gate static int	pic_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
52*0Sstevel@tonic-gate static int	pic_open(dev_t *, int, int, cred_t *);
53*0Sstevel@tonic-gate static int	pic_close(dev_t, int, int, cred_t *);
54*0Sstevel@tonic-gate static int	pic_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate struct cb_ops pic_cb_ops = {
57*0Sstevel@tonic-gate 	pic_open,
58*0Sstevel@tonic-gate 	pic_close,
59*0Sstevel@tonic-gate 	nodev,
60*0Sstevel@tonic-gate 	nodev,
61*0Sstevel@tonic-gate 	nodev,			/* dump */
62*0Sstevel@tonic-gate 	nodev,
63*0Sstevel@tonic-gate 	nodev,
64*0Sstevel@tonic-gate 	pic_ioctl,
65*0Sstevel@tonic-gate 	nodev,			/* devmap */
66*0Sstevel@tonic-gate 	nodev,
67*0Sstevel@tonic-gate 	ddi_segmap,		/* segmap */
68*0Sstevel@tonic-gate 	nochpoll,
69*0Sstevel@tonic-gate 	ddi_prop_op,
70*0Sstevel@tonic-gate 	NULL,			/* for STREAMS drivers */
71*0Sstevel@tonic-gate 	D_NEW | D_MP		/* driver compatibility flag */
72*0Sstevel@tonic-gate };
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate static struct dev_ops pic_dev_ops = {
75*0Sstevel@tonic-gate 	DEVO_REV,		/* driver build version */
76*0Sstevel@tonic-gate 	0,			/* device reference count */
77*0Sstevel@tonic-gate 	pic_getinfo,
78*0Sstevel@tonic-gate 	nulldev,
79*0Sstevel@tonic-gate 	nulldev,		/* probe */
80*0Sstevel@tonic-gate 	pic_attach,
81*0Sstevel@tonic-gate 	pic_detach,
82*0Sstevel@tonic-gate 	nulldev,		/* reset */
83*0Sstevel@tonic-gate 	&pic_cb_ops,
84*0Sstevel@tonic-gate 	(struct bus_ops *)NULL,
85*0Sstevel@tonic-gate 	nulldev			/* power */
86*0Sstevel@tonic-gate };
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate /*
89*0Sstevel@tonic-gate  * Fans' and sensors' node names and register offsets
90*0Sstevel@tonic-gate  */
91*0Sstevel@tonic-gate static struct minor_node_info pic_nodes[N_PIC_NODES] = {
92*0Sstevel@tonic-gate 	{NULL, 0, 0},				/* Reserved */
93*0Sstevel@tonic-gate 	{"fan_0", RF_FAN0_PERIOD, 0},		/* System Fan 0 */
94*0Sstevel@tonic-gate 	{"fan_1", RF_FAN1_PERIOD, 1},		/* System Fan 1 */
95*0Sstevel@tonic-gate 	{"fan_2", RF_FAN2_PERIOD, 2},		/* System Fan 2 */
96*0Sstevel@tonic-gate 	{"fan_3", RF_FAN3_PERIOD, 3},		/* System Fan 3 */
97*0Sstevel@tonic-gate 	{"fan_4", RF_FAN4_PERIOD, 4},		/* System Fan 4 in P0.1 */
98*0Sstevel@tonic-gate 	{"adt7462", RF_LOCAL_TEMP, 0},		/* ADT7462 Local Temperature */
99*0Sstevel@tonic-gate 	{"cpu_0", RF_REMOTE1_TEMP, 0},		/* CPU 0 temp */
100*0Sstevel@tonic-gate 	{"cpu_1", RF_REMOTE2_TEMP, 0},		/* CPU 1 temp */
101*0Sstevel@tonic-gate 	{"mb", RF_REMOTE3_TEMP, 0},		/* Motherboard temp */
102*0Sstevel@tonic-gate 	{"lm95221", RF_LM95221_TEMP, 0},	/* LM95221 Local Temperature */
103*0Sstevel@tonic-gate 	{"fire", RF_FIRE_TEMP, 0},		/* FIRE Temp */
104*0Sstevel@tonic-gate 	{"lsi1064", RF_LSI1064_TEMP, 0},	/* LSI1064 Temp */
105*0Sstevel@tonic-gate 	{"front_panel", RF_FRONT_TEMP, 0}	/* Front Panel Temperature */
106*0Sstevel@tonic-gate };
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate /*
109*0Sstevel@tonic-gate  * Soft state
110*0Sstevel@tonic-gate  */
111*0Sstevel@tonic-gate struct pic_softc {
112*0Sstevel@tonic-gate 	dev_info_t	*dip;
113*0Sstevel@tonic-gate 	kmutex_t	mutex;
114*0Sstevel@tonic-gate 	uint8_t		*cmd_reg;
115*0Sstevel@tonic-gate 	ddi_acc_handle_t cmd_handle;
116*0Sstevel@tonic-gate };
117*0Sstevel@tonic-gate #define	getsoftc(inst)	((struct pic_softc *)ddi_get_soft_state(statep, (inst)))
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate /* module configuration stuff */
120*0Sstevel@tonic-gate static void    *statep;
121*0Sstevel@tonic-gate extern struct mod_ops mod_driverops;
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate static struct modldrv modldrv = {
124*0Sstevel@tonic-gate 	&mod_driverops,
125*0Sstevel@tonic-gate 	"pic_client driver (v.%I%) ",
126*0Sstevel@tonic-gate 	&pic_dev_ops
127*0Sstevel@tonic-gate };
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
130*0Sstevel@tonic-gate 	MODREV_1,
131*0Sstevel@tonic-gate 	&modldrv,
132*0Sstevel@tonic-gate 	0
133*0Sstevel@tonic-gate };
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate int
136*0Sstevel@tonic-gate _init(void)
137*0Sstevel@tonic-gate {
138*0Sstevel@tonic-gate 	int e;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	if (e = ddi_soft_state_init(&statep, sizeof (struct pic_softc),
141*0Sstevel@tonic-gate 	    MAX_PIC_INSTANCES)) {
142*0Sstevel@tonic-gate 		return (e);
143*0Sstevel@tonic-gate 	}
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	if ((e = mod_install(&modlinkage)) != 0)
146*0Sstevel@tonic-gate 		ddi_soft_state_fini(&statep);
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	return (e);
149*0Sstevel@tonic-gate }
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate int
152*0Sstevel@tonic-gate _fini(void)
153*0Sstevel@tonic-gate {
154*0Sstevel@tonic-gate 	int e;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	if ((e = mod_remove(&modlinkage)) != 0)
157*0Sstevel@tonic-gate 		return (e);
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	ddi_soft_state_fini(&statep);
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
162*0Sstevel@tonic-gate }
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate int
165*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
166*0Sstevel@tonic-gate {
167*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
168*0Sstevel@tonic-gate }
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate /*ARGSUSED*/
171*0Sstevel@tonic-gate static int
172*0Sstevel@tonic-gate pic_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
173*0Sstevel@tonic-gate {
174*0Sstevel@tonic-gate 	int	inst;
175*0Sstevel@tonic-gate 	int	retval = DDI_SUCCESS;
176*0Sstevel@tonic-gate 	struct pic_softc *softc;
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	inst = PIC_MINOR_TO_INST(getminor((dev_t)arg));
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	switch (cmd) {
181*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
182*0Sstevel@tonic-gate 		if ((softc = getsoftc(inst)) == NULL) {
183*0Sstevel@tonic-gate 			*result = (void *)NULL;
184*0Sstevel@tonic-gate 			retval = DDI_FAILURE;
185*0Sstevel@tonic-gate 		} else
186*0Sstevel@tonic-gate 			*result = (void *)softc->dip;
187*0Sstevel@tonic-gate 		break;
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
190*0Sstevel@tonic-gate 		*result = (void *)inst;
191*0Sstevel@tonic-gate 		break;
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 	default:
194*0Sstevel@tonic-gate 		retval = DDI_FAILURE;
195*0Sstevel@tonic-gate 	}
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	return (retval);
198*0Sstevel@tonic-gate }
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate static int
201*0Sstevel@tonic-gate pic_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
202*0Sstevel@tonic-gate {
203*0Sstevel@tonic-gate 	int inst;
204*0Sstevel@tonic-gate 	int i;
205*0Sstevel@tonic-gate 	struct pic_softc *softc = NULL;
206*0Sstevel@tonic-gate 	char		*minor_name;
207*0Sstevel@tonic-gate 	int minor;
208*0Sstevel@tonic-gate 	char name[80];
209*0Sstevel@tonic-gate 	ddi_device_acc_attr_t dev_attr;
210*0Sstevel@tonic-gate 	int res;
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	switch (cmd) {
213*0Sstevel@tonic-gate 	case DDI_ATTACH:
214*0Sstevel@tonic-gate 		inst = ddi_get_instance(dip);
215*0Sstevel@tonic-gate 		if (inst >= MAX_PIC_INSTANCES) {
216*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "attach failed, too many instances\n");
217*0Sstevel@tonic-gate 			return (DDI_FAILURE);
218*0Sstevel@tonic-gate 		}
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 		(void) sprintf(name, "env-monitor%d", inst);
221*0Sstevel@tonic-gate 		minor = PIC_INST_TO_MINOR(inst) | PIC_UNIT_TO_MINOR(0);
222*0Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, name, S_IFCHR, minor,
223*0Sstevel@tonic-gate 		    DDI_PSEUDO, NULL) == DDI_FAILURE) {
224*0Sstevel@tonic-gate 			cmn_err(CE_WARN,
225*0Sstevel@tonic-gate 			    "ddi_create_minor_node() failed for inst %d\n",
226*0Sstevel@tonic-gate 			    inst);
227*0Sstevel@tonic-gate 			return (DDI_FAILURE);
228*0Sstevel@tonic-gate 		}
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 		/* Allocate a soft state structure for this instance */
231*0Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(statep, inst) != DDI_SUCCESS) {
232*0Sstevel@tonic-gate 			cmn_err(CE_WARN, " ddi_soft_state_zalloc() failed "
233*0Sstevel@tonic-gate 			    "for inst %d\n", inst);
234*0Sstevel@tonic-gate 			goto attach_failed;
235*0Sstevel@tonic-gate 		}
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 		/* Setup soft state */
238*0Sstevel@tonic-gate 		softc = getsoftc(inst);
239*0Sstevel@tonic-gate 		softc->dip = dip;
240*0Sstevel@tonic-gate 		mutex_init(&softc->mutex, NULL, MUTEX_DRIVER, NULL);
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 		/* Setup device attributes */
243*0Sstevel@tonic-gate 		dev_attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
244*0Sstevel@tonic-gate 		dev_attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
245*0Sstevel@tonic-gate 		dev_attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 		/*
248*0Sstevel@tonic-gate 		 * The RF_COMMAND/RF_STATUS and RF_IND_DATA/RF_IND_ADDR
249*0Sstevel@tonic-gate 		 * register pairs are mapped as one register set starting
250*0Sstevel@tonic-gate 		 * from 0x0 and length 0x42.
251*0Sstevel@tonic-gate 		 */
252*0Sstevel@tonic-gate 		res = ddi_regs_map_setup(dip, 0, (caddr_t *)&softc->cmd_reg,
253*0Sstevel@tonic-gate 		    0, 0x42, &dev_attr, &softc->cmd_handle);
254*0Sstevel@tonic-gate 		if (res != DDI_SUCCESS) {
255*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "ddi_regs_map_setup() failed\n");
256*0Sstevel@tonic-gate 			goto attach_failed;
257*0Sstevel@tonic-gate 		}
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 		/* Set up fans' and sensors' device minor nodes */
260*0Sstevel@tonic-gate 		for (i = 1; i < N_PIC_NODES; i++) {
261*0Sstevel@tonic-gate 			minor_name = pic_nodes[i].minor_name;
262*0Sstevel@tonic-gate 			minor = PIC_INST_TO_MINOR(inst) | PIC_UNIT_TO_MINOR(i);
263*0Sstevel@tonic-gate 			if (ddi_create_minor_node(dip, minor_name, S_IFCHR,
264*0Sstevel@tonic-gate 			    minor, PICDEV_NODE_TYPE, NULL) == DDI_FAILURE) {
265*0Sstevel@tonic-gate 				cmn_err(CE_WARN,
266*0Sstevel@tonic-gate 				    "%s:%d ddi_create_minor_node failed",
267*0Sstevel@tonic-gate 				    ddi_driver_name(dip), inst);
268*0Sstevel@tonic-gate 				(void) pic_detach(dip, DDI_DETACH);
269*0Sstevel@tonic-gate 				return (DDI_FAILURE);
270*0Sstevel@tonic-gate 			}
271*0Sstevel@tonic-gate 		}
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 		/* Create main environmental node */
274*0Sstevel@tonic-gate 		ddi_report_dev(dip);
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	case DDI_RESUME:
279*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 	default:
282*0Sstevel@tonic-gate 		return (DDI_FAILURE);
283*0Sstevel@tonic-gate 	}
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate attach_failed:
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 	/* Free soft state, if allocated. remove minor node if added earlier */
288*0Sstevel@tonic-gate 	if (softc)
289*0Sstevel@tonic-gate 		ddi_soft_state_free(statep, inst);
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	return (DDI_FAILURE);
294*0Sstevel@tonic-gate }
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate static int
297*0Sstevel@tonic-gate pic_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
298*0Sstevel@tonic-gate {
299*0Sstevel@tonic-gate 	int inst;
300*0Sstevel@tonic-gate 	struct pic_softc *softc;
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	switch (cmd) {
303*0Sstevel@tonic-gate 	case DDI_DETACH:
304*0Sstevel@tonic-gate 		inst = ddi_get_instance(dip);
305*0Sstevel@tonic-gate 		if ((softc = getsoftc(inst)) == NULL)
306*0Sstevel@tonic-gate 			return (ENXIO);
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 		(void) ddi_regs_map_free(&softc->cmd_handle);
309*0Sstevel@tonic-gate 		/* Free the soft state and remove minor node added earlier */
310*0Sstevel@tonic-gate 		mutex_destroy(&softc->mutex);
311*0Sstevel@tonic-gate 		ddi_soft_state_free(statep, inst);
312*0Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
313*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 	case DDI_SUSPEND:
316*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 	default:
319*0Sstevel@tonic-gate 		return (DDI_FAILURE);
320*0Sstevel@tonic-gate 	}
321*0Sstevel@tonic-gate }
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate /*ARGSUSED*/
324*0Sstevel@tonic-gate static int
325*0Sstevel@tonic-gate pic_open(dev_t *devp, int flag, int otyp, cred_t *credp)
326*0Sstevel@tonic-gate {
327*0Sstevel@tonic-gate 	int	inst = PIC_MINOR_TO_INST(getminor(*devp));
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 	return (getsoftc(inst) == NULL ? ENXIO : 0);
330*0Sstevel@tonic-gate }
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate /*ARGSUSED*/
333*0Sstevel@tonic-gate static int
334*0Sstevel@tonic-gate pic_close(dev_t dev, int flag, int otyp, cred_t *credp)
335*0Sstevel@tonic-gate {
336*0Sstevel@tonic-gate 	int	inst = PIC_MINOR_TO_INST(getminor(dev));
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 	return (getsoftc(inst) == NULL ? ENXIO : 0);
339*0Sstevel@tonic-gate }
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate /*ARGSUSED*/
342*0Sstevel@tonic-gate static int
343*0Sstevel@tonic-gate pic_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
344*0Sstevel@tonic-gate {
345*0Sstevel@tonic-gate 	int	inst;
346*0Sstevel@tonic-gate 	int	node;
347*0Sstevel@tonic-gate 	int	ntries;
348*0Sstevel@tonic-gate 	struct pic_softc *softc;
349*0Sstevel@tonic-gate 	uint8_t	in_command;
350*0Sstevel@tonic-gate 	uint8_t	status;
351*0Sstevel@tonic-gate 	int16_t	tempr;
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate 	inst = PIC_MINOR_TO_INST(getminor(dev));
354*0Sstevel@tonic-gate 	if ((softc = getsoftc(inst)) == NULL)
355*0Sstevel@tonic-gate 		return (ENXIO);
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	mutex_enter(&softc->mutex);
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 	if (ddi_copyin((caddr_t)arg, &in_command, sizeof (in_command),
360*0Sstevel@tonic-gate 	    mode) != DDI_SUCCESS) {
361*0Sstevel@tonic-gate 		mutex_exit(&softc->mutex);
362*0Sstevel@tonic-gate 		return (EFAULT);
363*0Sstevel@tonic-gate 	}
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 	node = PIC_MINOR_TO_UNIT(getminor(dev));
366*0Sstevel@tonic-gate 	if ((node >= N_PIC_NODES) || (node < 1)) {
367*0Sstevel@tonic-gate 		mutex_exit(&softc->mutex);
368*0Sstevel@tonic-gate 		return (ENXIO);
369*0Sstevel@tonic-gate 	}
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate 	/* Check status register */
372*0Sstevel@tonic-gate 	ntries = 0;
373*0Sstevel@tonic-gate 	do {
374*0Sstevel@tonic-gate 		if (++ntries > MAX_RETRIES) {
375*0Sstevel@tonic-gate 			mutex_exit(&softc->mutex);
376*0Sstevel@tonic-gate 			return (EBUSY);
377*0Sstevel@tonic-gate 		}
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 		status = ddi_get8(softc->cmd_handle,
380*0Sstevel@tonic-gate 		    (uint8_t *)softc->cmd_reg + RF_STATUS);
381*0Sstevel@tonic-gate 		/*
382*0Sstevel@tonic-gate 		 * We need 5us delay between 2 register reads
383*0Sstevel@tonic-gate 		 * this give enough time for the pic to be updated.
384*0Sstevel@tonic-gate 		 * we are waiting 10us to give us some breathing room.
385*0Sstevel@tonic-gate 		 */
386*0Sstevel@tonic-gate 		drv_usecwait(10);
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 		/*
389*0Sstevel@tonic-gate 		 * If we're asked to return status, we simply
390*0Sstevel@tonic-gate 		 * return it as is.
391*0Sstevel@tonic-gate 		 */
392*0Sstevel@tonic-gate 		if (cmd == PIC_GET_STATUS)
393*0Sstevel@tonic-gate 			break;
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 	} while ((status & ST_ENV_BUSY) || (status & ST_STALE_ADT_DATA) ||
396*0Sstevel@tonic-gate 	    (status & ST_STALE_LM_DATA));
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 	switch (cmd) {
399*0Sstevel@tonic-gate 	case PIC_GET_TEMPERATURE:
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 		/* select the temp sensor */
402*0Sstevel@tonic-gate 		(void) ddi_put8(softc->cmd_handle, (uint8_t *)softc->cmd_reg +
403*0Sstevel@tonic-gate 		    RF_IND_ADDR, pic_nodes[node].reg_offset);
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 		/* retrieve temperature data */
406*0Sstevel@tonic-gate 		tempr =  (int16_t)ddi_get8(softc->cmd_handle,
407*0Sstevel@tonic-gate 		    (uint8_t *)softc->cmd_reg + RF_IND_DATA);
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 		drv_usecwait(10);
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 		if (tempr == 0xff) {
412*0Sstevel@tonic-gate 			mutex_exit(&softc->mutex);
413*0Sstevel@tonic-gate 			return (EIO);
414*0Sstevel@tonic-gate 		}
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate 		/*
417*0Sstevel@tonic-gate 		 * The temp is passed in as a uint8 value, we need to convert
418*0Sstevel@tonic-gate 		 * it to a signed 16 bit value to be able to handle the range
419*0Sstevel@tonic-gate 		 * of -64 to 190 degrees.
420*0Sstevel@tonic-gate 		 */
421*0Sstevel@tonic-gate 		tempr -= 64;
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate 		/* In this case we need to return a signed int value */
424*0Sstevel@tonic-gate 		mutex_exit(&softc->mutex);
425*0Sstevel@tonic-gate 		(void) ddi_copyout(&tempr, (caddr_t)arg, sizeof (tempr), mode);
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate 		return (0);
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate 	case PIC_GET_FAN_SPEED:
430*0Sstevel@tonic-gate 		/* select fan */
431*0Sstevel@tonic-gate 		(void) ddi_put8(softc->cmd_handle, (uint8_t *)softc->cmd_reg +
432*0Sstevel@tonic-gate 		    RF_IND_ADDR, pic_nodes[node].reg_offset);
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate 		/* retrieve fan data */
435*0Sstevel@tonic-gate 		in_command =  ddi_get8(softc->cmd_handle,
436*0Sstevel@tonic-gate 		    (uint8_t *)softc->cmd_reg + RF_IND_DATA);
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate 		drv_usecwait(10);
439*0Sstevel@tonic-gate 		break;
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 	case PIC_SET_FAN_SPEED:
442*0Sstevel@tonic-gate 		/* select fan */
443*0Sstevel@tonic-gate 		(void) ddi_put8(softc->cmd_handle, (uint8_t *)softc->cmd_reg +
444*0Sstevel@tonic-gate 		    RF_IND_ADDR, pic_nodes[node].reg_offset);
445*0Sstevel@tonic-gate 
446*0Sstevel@tonic-gate 		/* send the fan data */
447*0Sstevel@tonic-gate 		(void) ddi_put8(softc->cmd_handle,
448*0Sstevel@tonic-gate 		    (uint8_t *)softc->cmd_reg + RF_IND_DATA, in_command);
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate 		drv_usecwait(10);
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 		break;
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate 	case PIC_GET_STATUS:
455*0Sstevel@tonic-gate 		in_command = status;
456*0Sstevel@tonic-gate 		break;
457*0Sstevel@tonic-gate 
458*0Sstevel@tonic-gate 	case PIC_GET_FAN_STATUS:
459*0Sstevel@tonic-gate 		/* read ffault register */
460*0Sstevel@tonic-gate 		(void) ddi_put8(softc->cmd_handle, (uint8_t *)softc->cmd_reg +
461*0Sstevel@tonic-gate 		    RF_IND_ADDR, RF_FAN_STATUS);
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate 		/* retrieve fan failure status */
464*0Sstevel@tonic-gate 		in_command = ddi_get8(softc->cmd_handle,
465*0Sstevel@tonic-gate 		    (uint8_t *)softc->cmd_reg + RF_IND_DATA);
466*0Sstevel@tonic-gate 		in_command = (in_command >> pic_nodes[node].ff_shift) & 0x1;
467*0Sstevel@tonic-gate 
468*0Sstevel@tonic-gate 		drv_usecwait(10);
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 		break;
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate 	case PIC_SET_ESTAR_MODE:
473*0Sstevel@tonic-gate 		(void) ddi_put8(softc->cmd_handle,
474*0Sstevel@tonic-gate 		    (uint8_t *)softc->cmd_reg + RF_COMMAND, CMD_TO_ESTAR);
475*0Sstevel@tonic-gate 		break;
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 	default:
478*0Sstevel@tonic-gate 		mutex_exit(&softc->mutex);
479*0Sstevel@tonic-gate 		cmn_err(CE_NOTE, "cmd %d isnt valid", cmd);
480*0Sstevel@tonic-gate 		return (EINVAL);
481*0Sstevel@tonic-gate 	}
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate 	mutex_exit(&softc->mutex);
484*0Sstevel@tonic-gate 	(void) ddi_copyout(&in_command, (caddr_t)arg, 1, mode);
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate 	/*
487*0Sstevel@tonic-gate 	 * 0xFF indicates an error when trying to read from the device,
488*0Sstevel@tonic-gate 	 * ie. it can be busy, or being updated.
489*0Sstevel@tonic-gate 	 * This will force picl to retry the read at another time.
490*0Sstevel@tonic-gate 	 */
491*0Sstevel@tonic-gate 	if (in_command == 0xff) {
492*0Sstevel@tonic-gate 		return (EIO);
493*0Sstevel@tonic-gate 	} else {
494*0Sstevel@tonic-gate 		return (0);
495*0Sstevel@tonic-gate 	}
496*0Sstevel@tonic-gate }
497