xref: /onnv-gate/usr/src/uts/sun4u/io/pmugpio.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 2004 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 #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/conf.h>
31*0Sstevel@tonic-gate #include <sys/ddi.h>
32*0Sstevel@tonic-gate #include <sys/sunddi.h>
33*0Sstevel@tonic-gate #include <sys/modctl.h>
34*0Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
35*0Sstevel@tonic-gate #include <sys/kmem.h>
36*0Sstevel@tonic-gate #include <sys/devops.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate /*
39*0Sstevel@tonic-gate  * Time periods, in nanoseconds
40*0Sstevel@tonic-gate  */
41*0Sstevel@tonic-gate #define	PMUGPIO_TWO_SEC		2000000000LL
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate static	dev_info_t	*pmugpio_dip;
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate typedef struct pmugpio_state {
46*0Sstevel@tonic-gate 	uint8_t			*pmugpio_reset_reg;
47*0Sstevel@tonic-gate 	ddi_acc_handle_t	pmugpio_reset_reg_handle;
48*0Sstevel@tonic-gate 	uint8_t			*pmugpio_watchdog_reg;
49*0Sstevel@tonic-gate 	ddi_acc_handle_t	pmugpio_watchdog_reg_handle;
50*0Sstevel@tonic-gate 	hrtime_t		hw_last_pat;
51*0Sstevel@tonic-gate } pmugpio_state_t;
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate static void *pmugpio_statep;
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate static int pmugpio_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
56*0Sstevel@tonic-gate static int pmugpio_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
57*0Sstevel@tonic-gate static int pmugpio_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
58*0Sstevel@tonic-gate 		void **result);
59*0Sstevel@tonic-gate static int pmugpio_map_regs(dev_info_t *, pmugpio_state_t *);
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate struct cb_ops pmugpio_cb_ops = {
62*0Sstevel@tonic-gate 	nulldev,	/* open  */
63*0Sstevel@tonic-gate 	nulldev,	/* close */
64*0Sstevel@tonic-gate 	nulldev,	/* strategy */
65*0Sstevel@tonic-gate 	nulldev,	/* print */
66*0Sstevel@tonic-gate 	nulldev,	/* dump */
67*0Sstevel@tonic-gate 	nulldev,	/* read */
68*0Sstevel@tonic-gate 	nulldev,	/* write */
69*0Sstevel@tonic-gate 	nulldev,	/* ioctl */
70*0Sstevel@tonic-gate 	nulldev,	/* devmap */
71*0Sstevel@tonic-gate 	nulldev,	/* mmap */
72*0Sstevel@tonic-gate 	nulldev,	/* segmap */
73*0Sstevel@tonic-gate 	nochpoll,	/* poll */
74*0Sstevel@tonic-gate 	ddi_prop_op,	/* cb_prop_op */
75*0Sstevel@tonic-gate 	NULL,		/* streamtab  */
76*0Sstevel@tonic-gate 	D_MP | D_NEW
77*0Sstevel@tonic-gate };
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate static struct dev_ops pmugpio_ops = {
80*0Sstevel@tonic-gate 	DEVO_REV,		/* Devo_rev */
81*0Sstevel@tonic-gate 	0,			/* Refcnt */
82*0Sstevel@tonic-gate 	pmugpio_info,		/* Info */
83*0Sstevel@tonic-gate 	nulldev,		/* Identify */
84*0Sstevel@tonic-gate 	nulldev,		/* Probe */
85*0Sstevel@tonic-gate 	pmugpio_attach,		/* Attach */
86*0Sstevel@tonic-gate 	pmugpio_detach,		/* Detach */
87*0Sstevel@tonic-gate 	nodev,			/* Reset */
88*0Sstevel@tonic-gate 	&pmugpio_cb_ops,		/* Driver operations */
89*0Sstevel@tonic-gate 	0,			/* Bus operations */
90*0Sstevel@tonic-gate 	NULL			/* Power */
91*0Sstevel@tonic-gate };
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate static struct modldrv modldrv = {
94*0Sstevel@tonic-gate 	&mod_driverops, 		/* This one is a driver */
95*0Sstevel@tonic-gate 	"Pmugpio Driver %I%", 		/* Name of the module. */
96*0Sstevel@tonic-gate 	&pmugpio_ops,			/* Driver ops */
97*0Sstevel@tonic-gate };
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
100*0Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
101*0Sstevel@tonic-gate };
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate int
104*0Sstevel@tonic-gate _init(void)
105*0Sstevel@tonic-gate {
106*0Sstevel@tonic-gate 	int error;
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	/* Initialize the soft state structures */
109*0Sstevel@tonic-gate 	if ((error = ddi_soft_state_init(&pmugpio_statep,
110*0Sstevel@tonic-gate 	    sizeof (pmugpio_state_t), 1)) != 0) {
111*0Sstevel@tonic-gate 		return (error);
112*0Sstevel@tonic-gate 	}
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	/* Install the loadable module */
115*0Sstevel@tonic-gate 	if ((error = mod_install(&modlinkage)) != 0) {
116*0Sstevel@tonic-gate 		ddi_soft_state_fini(&pmugpio_statep);
117*0Sstevel@tonic-gate 	}
118*0Sstevel@tonic-gate 	return (error);
119*0Sstevel@tonic-gate }
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate int
122*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
123*0Sstevel@tonic-gate {
124*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate int
128*0Sstevel@tonic-gate _fini(void)
129*0Sstevel@tonic-gate {
130*0Sstevel@tonic-gate 	int error;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
133*0Sstevel@tonic-gate 	if (error == 0) {
134*0Sstevel@tonic-gate 		/* Release per module resources */
135*0Sstevel@tonic-gate 		ddi_soft_state_fini(&pmugpio_statep);
136*0Sstevel@tonic-gate 	}
137*0Sstevel@tonic-gate 	return (error);
138*0Sstevel@tonic-gate }
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate static int
141*0Sstevel@tonic-gate pmugpio_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
142*0Sstevel@tonic-gate {
143*0Sstevel@tonic-gate 	int		instance;
144*0Sstevel@tonic-gate 	pmugpio_state_t	*pmugpio_ptr = NULL;
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	switch (cmd) {
147*0Sstevel@tonic-gate 	case DDI_ATTACH:
148*0Sstevel@tonic-gate 		break;
149*0Sstevel@tonic-gate 	case DDI_RESUME:
150*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
151*0Sstevel@tonic-gate 	default:
152*0Sstevel@tonic-gate 		return (DDI_FAILURE);
153*0Sstevel@tonic-gate 	}
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	/* Get the instance and create soft state */
156*0Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
157*0Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(pmugpio_statep, instance) != 0) {
158*0Sstevel@tonic-gate 		return (DDI_FAILURE);
159*0Sstevel@tonic-gate 	}
160*0Sstevel@tonic-gate 	pmugpio_ptr = ddi_get_soft_state(pmugpio_statep, instance);
161*0Sstevel@tonic-gate 	if (pmugpio_ptr == NULL) {
162*0Sstevel@tonic-gate 		return (DDI_FAILURE);
163*0Sstevel@tonic-gate 	}
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	if (pmugpio_map_regs(dip, pmugpio_ptr) != DDI_SUCCESS) {
166*0Sstevel@tonic-gate 		ddi_soft_state_free(pmugpio_statep, instance);
167*0Sstevel@tonic-gate 		return (DDI_FAILURE);
168*0Sstevel@tonic-gate 	}
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	/* Display information in the banner */
171*0Sstevel@tonic-gate 	ddi_report_dev(dip);
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	/* Save the dip */
174*0Sstevel@tonic-gate 	pmugpio_dip = dip;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
177*0Sstevel@tonic-gate }
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate /* ARGSUSED */
180*0Sstevel@tonic-gate static int
181*0Sstevel@tonic-gate pmugpio_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
182*0Sstevel@tonic-gate {
183*0Sstevel@tonic-gate 	/* Pointer to soft state */
184*0Sstevel@tonic-gate 	switch (cmd) {
185*0Sstevel@tonic-gate 	case DDI_SUSPEND:
186*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
187*0Sstevel@tonic-gate 	default:
188*0Sstevel@tonic-gate 		return (DDI_FAILURE);
189*0Sstevel@tonic-gate 	}
190*0Sstevel@tonic-gate }
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate /* ARGSUSED */
193*0Sstevel@tonic-gate static int
194*0Sstevel@tonic-gate pmugpio_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
195*0Sstevel@tonic-gate 		void *arg, void **result)
196*0Sstevel@tonic-gate {
197*0Sstevel@tonic-gate 	dev_t dev;
198*0Sstevel@tonic-gate 	int instance, error;
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	switch (infocmd) {
201*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
202*0Sstevel@tonic-gate 		*result = (void *)pmugpio_dip;
203*0Sstevel@tonic-gate 		error = DDI_SUCCESS;
204*0Sstevel@tonic-gate 		break;
205*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
206*0Sstevel@tonic-gate 		dev = (dev_t)arg;
207*0Sstevel@tonic-gate 		instance = getminor(dev);
208*0Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
209*0Sstevel@tonic-gate 		error = DDI_SUCCESS;
210*0Sstevel@tonic-gate 		break;
211*0Sstevel@tonic-gate 	default:
212*0Sstevel@tonic-gate 		error = DDI_FAILURE;
213*0Sstevel@tonic-gate 	}
214*0Sstevel@tonic-gate 	return (error);
215*0Sstevel@tonic-gate }
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate void
218*0Sstevel@tonic-gate pmugpio_watchdog_pat(void)
219*0Sstevel@tonic-gate {
220*0Sstevel@tonic-gate 	dev_info_t *dip = pmugpio_dip;
221*0Sstevel@tonic-gate 	int instance;
222*0Sstevel@tonic-gate 	pmugpio_state_t *pmugpio_ptr;
223*0Sstevel@tonic-gate 	hrtime_t now;
224*0Sstevel@tonic-gate 	uint8_t value;
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 	if (dip == NULL) {
227*0Sstevel@tonic-gate 		return;
228*0Sstevel@tonic-gate 	}
229*0Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
230*0Sstevel@tonic-gate 	pmugpio_ptr = ddi_get_soft_state(pmugpio_statep, instance);
231*0Sstevel@tonic-gate 	if (pmugpio_ptr == NULL) {
232*0Sstevel@tonic-gate 		return;
233*0Sstevel@tonic-gate 	}
234*0Sstevel@tonic-gate 	/*
235*0Sstevel@tonic-gate 	 * The RMC can read interrupts either high to low OR low to high. As
236*0Sstevel@tonic-gate 	 * a result all that needs to happen is that when we hit the time to
237*0Sstevel@tonic-gate 	 * send an signal we simply need to change the state.
238*0Sstevel@tonic-gate 	 */
239*0Sstevel@tonic-gate 	now = gethrtime();
240*0Sstevel@tonic-gate 	if ((now - pmugpio_ptr->hw_last_pat) >= PMUGPIO_TWO_SEC) {
241*0Sstevel@tonic-gate 		/*
242*0Sstevel@tonic-gate 		 * fetch current reg value and invert it
243*0Sstevel@tonic-gate 		 */
244*0Sstevel@tonic-gate 		value = (uint8_t)(0xff ^
245*0Sstevel@tonic-gate 		    ddi_get8(pmugpio_ptr->pmugpio_watchdog_reg_handle,
246*0Sstevel@tonic-gate 		    pmugpio_ptr->pmugpio_watchdog_reg));
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 		ddi_put8(pmugpio_ptr->pmugpio_watchdog_reg_handle,
249*0Sstevel@tonic-gate 		    pmugpio_ptr->pmugpio_watchdog_reg, value);
250*0Sstevel@tonic-gate 		pmugpio_ptr->hw_last_pat = now;
251*0Sstevel@tonic-gate 	}
252*0Sstevel@tonic-gate }
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate void
255*0Sstevel@tonic-gate pmugpio_reset(void)
256*0Sstevel@tonic-gate {
257*0Sstevel@tonic-gate 	dev_info_t *dip = pmugpio_dip;
258*0Sstevel@tonic-gate 	int instance;
259*0Sstevel@tonic-gate 	pmugpio_state_t *pmugpio_ptr;
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	if (dip == NULL) {
262*0Sstevel@tonic-gate 		return;
263*0Sstevel@tonic-gate 	}
264*0Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
265*0Sstevel@tonic-gate 	pmugpio_ptr = ddi_get_soft_state(pmugpio_statep, instance);
266*0Sstevel@tonic-gate 	if (pmugpio_ptr == NULL) {
267*0Sstevel@tonic-gate 		return;
268*0Sstevel@tonic-gate 	}
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	/*
271*0Sstevel@tonic-gate 	 * turn all bits on then off again - pmubus nexus will ensure
272*0Sstevel@tonic-gate 	 * that only unmasked bit is affected
273*0Sstevel@tonic-gate 	 */
274*0Sstevel@tonic-gate 	ddi_put8(pmugpio_ptr->pmugpio_reset_reg_handle,
275*0Sstevel@tonic-gate 	    pmugpio_ptr->pmugpio_reset_reg, ~0);
276*0Sstevel@tonic-gate 	ddi_put8(pmugpio_ptr->pmugpio_reset_reg_handle,
277*0Sstevel@tonic-gate 	    pmugpio_ptr->pmugpio_reset_reg, 0);
278*0Sstevel@tonic-gate }
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate static int
281*0Sstevel@tonic-gate pmugpio_map_regs(dev_info_t *dip, pmugpio_state_t *pmugpio_ptr)
282*0Sstevel@tonic-gate {
283*0Sstevel@tonic-gate 	ddi_device_acc_attr_t attr;
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 	/* The host controller will be little endian */
286*0Sstevel@tonic-gate 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
287*0Sstevel@tonic-gate 	attr.devacc_attr_endian_flags  = DDI_STRUCTURE_LE_ACC;
288*0Sstevel@tonic-gate 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 	if (ddi_regs_map_setup(dip, 1,
291*0Sstevel@tonic-gate 	    (caddr_t *)&pmugpio_ptr->pmugpio_watchdog_reg, 0, 1, &attr,
292*0Sstevel@tonic-gate 	    &pmugpio_ptr->pmugpio_watchdog_reg_handle) != DDI_SUCCESS) {
293*0Sstevel@tonic-gate 		return (DDI_FAILURE);
294*0Sstevel@tonic-gate 	}
295*0Sstevel@tonic-gate 	if (ddi_regs_map_setup(dip, 0,
296*0Sstevel@tonic-gate 	    (caddr_t *)&pmugpio_ptr->pmugpio_reset_reg, 0, 1, &attr,
297*0Sstevel@tonic-gate 	    &pmugpio_ptr->pmugpio_reset_reg_handle) != DDI_SUCCESS) {
298*0Sstevel@tonic-gate 		ddi_regs_map_free(&pmugpio_ptr->pmugpio_watchdog_reg_handle);
299*0Sstevel@tonic-gate 		return (DDI_FAILURE);
300*0Sstevel@tonic-gate 	}
301*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
302*0Sstevel@tonic-gate }
303