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