summaryrefslogtreecommitdiff
path: root/usr/src/uts/sun4u/io/pmc.c
blob: d6a10f5afd873af36c1f4ce2f26be489959188e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Driver for the Power Management Controller (logical unit 8) of the
 * PC87317 SuperI/O chip. The PMC contains the hardware watchdog timer.
 */

#include <sys/types.h>
#include <sys/time.h>
#include <sys/cmn_err.h>
#include <sys/param.h>
#include <sys/modctl.h>
#include <sys/conf.h>
#include <sys/stat.h>
#include <sys/clock.h>
#include <sys/reboot.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/file.h>
#include <sys/note.h>

#ifdef	DEBUG
int pmc_debug_flag = 0;
#define	DPRINTF(ARGLIST) if (pmc_debug_flag) printf ARGLIST;
#else
#define	DPRINTF(ARGLIST)
#endif /* DEBUG */

/* Driver soft state structure */
typedef struct pmc {
	dev_info_t		*dip;
	ddi_acc_handle_t	pmc_handle;
} pmc_t;

static void *pmc_soft_state;
static int instance = -1;

/* dev_ops and cb_ops entry point function declarations */
static int pmc_attach(dev_info_t *, ddi_attach_cmd_t);
static int pmc_detach(dev_info_t *, ddi_detach_cmd_t);
static int pmc_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);

/* hardware watchdog parameters */
static uint_t pmc_set_watchdog_timer(uint_t);
static uint_t pmc_clear_watchdog_timer(void);

extern volatile uint8_t	*v_pmc_addr_reg;
extern volatile uint8_t	*v_pmc_data_reg;
extern int		watchdog_enable;
extern int		watchdog_available;
extern int		watchdog_activated;
extern int		boothowto;
extern uint_t		watchdog_timeout_seconds;

/*
 * Power Management Registers and values
 */
#define	PMC_WDTO	0x05	/* Watchdog Time Out */
#define	PMC_CLEAR_WDTO	0x00

struct cb_ops pmc_cb_ops = {
	nodev,
	nodev,
	nodev,
	nodev,
	nodev,			/* dump */
	nodev,
	nodev,
	nodev,
	nodev,			/* devmap */
	nodev,
	nodev,
	nochpoll,
	ddi_prop_op,
	NULL,			/* for STREAMS drivers */
	D_NEW | D_MP,		/* driver compatibility flag */
	CB_REV,
	nodev,
	nodev
};

static struct dev_ops pmc_dev_ops = {
	DEVO_REV,			/* driver build version */
	0,				/* device reference count */
	pmc_getinfo,
	nulldev,
	nulldev,			/* probe */
	pmc_attach,
	pmc_detach,
	nulldev,			/* reset */
	&pmc_cb_ops,
	(struct bus_ops *)NULL,
	nulldev				/* power */
};

/* module configuration stuff */
extern struct mod_ops mod_driverops;
static struct modldrv modldrv = {
	&mod_driverops,
	"pmc driver",
	&pmc_dev_ops
};
static struct modlinkage modlinkage = {
	MODREV_1,
	&modldrv,
	0
};


int
_init(void)
{
	int e;

	e = ddi_soft_state_init(&pmc_soft_state, sizeof (pmc_t), 1);
	if (e != 0) {
		DPRINTF(("_init: ddi_soft_state_init failed\n"));
		return (e);
	}

	e = mod_install(&modlinkage);
	if (e != 0) {
		DPRINTF(("_init: mod_install failed\n"));
		ddi_soft_state_fini(&pmc_soft_state);
		return (e);
	}

	if (v_pmc_addr_reg != NULL) {
		tod_ops.tod_set_watchdog_timer = pmc_set_watchdog_timer;
		tod_ops.tod_clear_watchdog_timer = pmc_clear_watchdog_timer;

		/*
		 * See if the user has enabled the watchdog timer, and if
		 * it's available.
		 */
		if (watchdog_enable) {
			if (!watchdog_available) {
				cmn_err(CE_WARN, "pmc: Hardware watchdog "
					"unavailable");
			} else if (boothowto & RB_DEBUG) {
				watchdog_available = 0;
				cmn_err(CE_WARN, "pmc: kernel debugger "
					"detected: hardware watchdog disabled");
			}
		}
	}
	return (e);
}

int
_fini(void)
{
	int e;

	if (v_pmc_addr_reg != NULL)
		return (DDI_FAILURE);
	else {
		e = mod_remove(&modlinkage);
		if (e != 0)
			return (e);

		ddi_soft_state_fini(&pmc_soft_state);
		return (DDI_SUCCESS);
	}
}


int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}

static int
pmc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
{
	_NOTE(ARGUNUSED(dip))

	pmc_t	*pmcp;
	int	instance;

	switch (cmd) {
	case DDI_INFO_DEVT2DEVINFO:
		instance = getminor((dev_t)arg);
		pmcp = (pmc_t *)ddi_get_soft_state(pmc_soft_state, instance);
		if (pmcp == NULL) {
			*result = (void *)NULL;
			return (DDI_FAILURE);
		}
		*result = (void *)pmcp->dip;
		return (DDI_SUCCESS);

	case DDI_INFO_DEVT2INSTANCE:
		*result = (void *)(uintptr_t)getminor((dev_t)arg);
		return (DDI_SUCCESS);

	default:
		return (DDI_FAILURE);
	}
}


static int
pmc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
	pmc_t	*pmcp;
	uint_t	wd_timout;

	switch (cmd) {
	case DDI_ATTACH:
		break;
	case DDI_RESUME:
		if (v_pmc_addr_reg != NULL && watchdog_enable) {
			int ret = 0;
			wd_timout = watchdog_timeout_seconds;
			mutex_enter(&tod_lock);
			ret = tod_ops.tod_set_watchdog_timer(wd_timout);
			mutex_exit(&tod_lock);
			if (ret == 0)
				return (DDI_FAILURE);
		}
		return (DDI_SUCCESS);
	default:
		return (DDI_FAILURE);
	}

	if (instance != -1) {
		DPRINTF(("pmc_attach: Another instance is already attached."));
		return (DDI_FAILURE);
	}

	instance = ddi_get_instance(dip);

	if (ddi_soft_state_zalloc(pmc_soft_state, instance) != DDI_SUCCESS) {
		DPRINTF(("pmc_attach: Failed to allocate soft state."));
		return (DDI_FAILURE);
	}

	pmcp = (pmc_t *)ddi_get_soft_state(pmc_soft_state, instance);
	pmcp->dip = dip;

	return (DDI_SUCCESS);
}

static int
pmc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
	_NOTE(ARGUNUSED(dip))

	pmc_t	*pmcp;

	switch (cmd) {
	case DDI_DETACH:
		/* allow detach if no hardware watchdog */
		if (v_pmc_addr_reg == NULL || !watchdog_activated) {
			pmcp = (pmc_t *)ddi_get_soft_state(pmc_soft_state,
				instance);
			if (pmcp == NULL)
				return (ENXIO);
			ddi_soft_state_free(pmc_soft_state, instance);
			return (DDI_SUCCESS);
		} else
			return (DDI_FAILURE);
	case DDI_SUSPEND:
		if (v_pmc_addr_reg != NULL && watchdog_activated) {
			mutex_enter(&tod_lock);
			(void) tod_ops.tod_clear_watchdog_timer();
			mutex_exit(&tod_lock);
		}
		return (DDI_SUCCESS);
	default:
		return (DDI_FAILURE);
	}

}

/*
 * Set the hardware watchdog timer; returning what we set it to.
 */
static uint_t
pmc_set_watchdog_timer(uint_t timeoutval)
{
	uint_t timeoutval_minutes;
	ASSERT(MUTEX_HELD(&tod_lock));

	/* sanity checks */
	if (watchdog_enable == 0 || watchdog_available == 0 ||
	    timeoutval == 0)
		return (0);

	/*
	 * Historically the timer has been counted out in seconds.
	 * The PC87317 counts the timeout in minutes. The default
	 * timeout is 10 seconds; the least we can do is one minute.
	 */
	timeoutval_minutes = (timeoutval + 59) / 60;
	if (timeoutval_minutes > UINT8_MAX)
		return (0);

	*v_pmc_addr_reg = (uint8_t)PMC_WDTO;
	*v_pmc_data_reg = (uint8_t)timeoutval_minutes;
	watchdog_activated = 1;

	/* we'll still return seconds */
	return (timeoutval_minutes * 60);
}

/*
 * Clear the hardware watchdog timer; returning what it was set to.
 */
static uint_t
pmc_clear_watchdog_timer(void)
{
	uint_t	wd_timeout;

	ASSERT(MUTEX_HELD(&tod_lock));
	if (watchdog_activated == 0)
		return (0);

	*v_pmc_addr_reg = (uint8_t)PMC_WDTO;
	wd_timeout = (uint_t)*v_pmc_data_reg;
	*v_pmc_data_reg = (uint8_t)PMC_CLEAR_WDTO;
	watchdog_activated = 0;

	/* return seconds */
	return (wd_timeout * 60);
}