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