summaryrefslogtreecommitdiff
path: root/usr/src/uts/sun4u/opl/io/oplpanel/oplpanel.c
blob: d42cb990c86e551b27141df74e1da2d36e27e941 (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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 * 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
 */

/*
 * All Rights Reserved, Copyright (c) FUJITSU LIMITED 2006
 */

/*
 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
 */


#include <sys/types.h>
#include <sys/time.h>
#include <sys/errno.h>
#include <sys/cmn_err.h>
#include <sys/param.h>
#include <sys/modctl.h>
#include <sys/conf.h>
#include <sys/open.h>
#include <sys/stat.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/file.h>
#include <sys/intr.h>
#include <sys/machsystm.h>

#define	PNLIE_MASK	0x010	/* interrupt enable/disable */
#define	PNLINT_MASK	0x001	/* interrupted flag */

#ifdef DEBUG
int panel_debug = 0;
static void panel_ddi_put8(ddi_acc_handle_t, uint8_t *, uint8_t);
#define	DCMN_ERR(x)	if (panel_debug) cmn_err x

#else

#define	DCMN_ERR(x)
#define	panel_ddi_put8(x, y, z)	ddi_put8(x, y, z)

#endif

static int	panel_getinfo(dev_info_t *, ddi_info_cmd_t, void *,  void **);
static int	panel_attach(dev_info_t *, ddi_attach_cmd_t);
static int	panel_detach(dev_info_t *, ddi_detach_cmd_t);
static uint_t	panel_intr(caddr_t);
static int	panel_open(dev_t *, int, int, cred_t *);
static int	panel_close(dev_t, int, int, cred_t *);

static char	*panel_name = "oplpanel";
int		panel_enable = 1;	/* enable or disable */

extern uint64_t	cpc_level15_inum;	/* in cpc_subr.c */

struct panel_state {
	dev_info_t		*dip;
	ddi_iblock_cookie_t	iblock_cookie;
	ddi_acc_handle_t	panel_regs_handle;
	uint8_t			*panelregs;		/* mapping address */
	uint8_t			panelregs_state;	/* keeping regs. */
};

struct cb_ops panel_cb_ops = {
	nodev,		/* open */
	nodev,		/* close */
	nodev,		/* strategy */
	nodev,		/* print */
	nodev,		/* dump */
	nodev,		/* read */
	nodev,		/* write */
	nodev,		/* ioctl */
	nodev,		/* devmap */
	nodev,		/* mmap */
	nodev,		/* segmap */
	nochpoll,	/* poll */
	nodev,		/* prop_op */
	NULL,		/* streamtab */
	D_NEW | D_MP | D_HOTPLUG,	/* flag */
	CB_REV,		/* cb_rev */
	nodev,		/* async I/O read entry point */
	nodev		/* async I/O write entry point */
};

static struct dev_ops panel_dev_ops = {
	DEVO_REV,		/* driver build version */
	0,			/* device reference count */
	panel_getinfo,		/* getinfo */
	nulldev,		/* identify */
	nulldev,		/* probe */
	panel_attach,		/* attach */
	panel_detach,		/* detach */
	nulldev,		/* reset */
	&panel_cb_ops,		/* cb_ops */
	NULL,			/* bus_ops */
	nulldev,		/* power */
	ddi_quiesce_not_supported,	/* devo_quiesce */
};

/* module configuration stuff */
static void		*panelstates;
extern struct mod_ops	mod_driverops;

static struct modldrv modldrv = {
	&mod_driverops,
	"OPL panel driver",
	&panel_dev_ops
};

static struct modlinkage modlinkage = {
	MODREV_1,
	&modldrv,
	0
};


int
_init(void)
{
	int	status;

	DCMN_ERR((CE_CONT, "%s: _init\n", panel_name));

	status = ddi_soft_state_init(&panelstates,
	    sizeof (struct panel_state), 0);
	if (status != 0) {
		cmn_err(CE_WARN, "%s: ddi_soft_state_init failed.",
		    panel_name);
		return (status);
	}

	status = mod_install(&modlinkage);
	if (status != 0) {
		ddi_soft_state_fini(&panelstates);
	}

	return (status);
}

int
_fini(void)
{
	/*
	 * Can't unload to make sure the panel switch always works.
	 */
	return (EBUSY);
}

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

static int
panel_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{

	int instance;
	struct panel_state *statep = NULL;

	ddi_device_acc_attr_t access_attr = {
		DDI_DEVICE_ATTR_V0,
		DDI_STRUCTURE_BE_ACC,
		DDI_STRICTORDER_ACC
	};

	instance = ddi_get_instance(dip);

	DCMN_ERR((CE_CONT, "%s%d: attach\n", panel_name, instance));

	switch (cmd) {
	case DDI_ATTACH:
		DCMN_ERR((CE_CONT, "%s%d: DDI_ATTACH\n",
		    panel_name, instance));
		break;

	case DDI_RESUME:
		DCMN_ERR((CE_CONT, "%s%d: DDI_RESUME\n",
		    panel_name, instance));

		if ((statep = (struct panel_state *)
		    ddi_get_soft_state(panelstates, instance)) == NULL) {
			cmn_err(CE_WARN, "%s%d: ddi_get_soft_state failed.",
			    panel_name, instance);
			return (DDI_FAILURE);
		}

		/* enable the interrupt just in case */
		panel_ddi_put8(statep->panel_regs_handle, statep->panelregs,
		    statep->panelregs_state);
		return (DDI_SUCCESS);

	default:
		return (DDI_FAILURE);
	}

	/*
	 * Attach routine
	 */

	/* alloc and get soft state */
	if (ddi_soft_state_zalloc(panelstates, instance) != DDI_SUCCESS) {
		cmn_err(CE_WARN, "%s%d: ddi_soft_state_zalloc failed.",
		    panel_name, instance);
		goto attach_failed2;
	}
	if ((statep = (struct panel_state *)
	    ddi_get_soft_state(panelstates, instance)) == NULL) {
		cmn_err(CE_WARN, "%s%d: ddi_get_soft_state failed.",
		    panel_name, instance);
		goto attach_failed1;
	}

	/* set the dip in the soft state */
	statep->dip = dip;

	/* mapping register */
	if (ddi_regs_map_setup(dip, 0, (caddr_t *)&statep->panelregs,
	    0, 0, /* the entire space is mapped */
	    &access_attr, &statep->panel_regs_handle) != DDI_SUCCESS) {
		cmn_err(CE_WARN, "%s%d: ddi_regs_map_setup failed.",
		    panel_name, instance);
		goto attach_failed1;
	}

	/* setup the interrupt handler */
	(void) ddi_get_iblock_cookie(dip, 0, &statep->iblock_cookie);
	if (ddi_add_intr(dip, 0, &statep->iblock_cookie, 0, &panel_intr,
	    (caddr_t)statep) != DDI_SUCCESS) {
		cmn_err(CE_WARN, "%s%d: cannot add interrupt handler.",
		    panel_name, instance);
		goto attach_failed0;
	}

	/* ATTACH SUCCESS */

	/* announce the device */
	ddi_report_dev(dip);

	/* turn on interrupt */
	statep->panelregs_state = 0 | PNLIE_MASK;
	panel_ddi_put8(statep->panel_regs_handle, statep->panelregs,
	    statep->panelregs_state);

	return (DDI_SUCCESS);

attach_failed0:
	ddi_regs_map_free(&statep->panel_regs_handle);
attach_failed1:
	ddi_soft_state_free(panelstates, instance);
attach_failed2:
	DCMN_ERR((CE_NOTE, "%s%d: attach failed", panel_name, instance));
	return (DDI_FAILURE);
}

static int
panel_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
	int instance;
	struct panel_state *statep;

	instance = ddi_get_instance(dip);

	DCMN_ERR((CE_CONT, "%s%d: detach\n", panel_name, instance));

	if ((statep = (struct panel_state *)
	    ddi_get_soft_state(panelstates, instance)) == NULL) {
		cmn_err(CE_WARN, "%s%d: ddi_get_soft_state failed.",
		    panel_name, instance);
		return (DDI_FAILURE);
	}

	switch (cmd) {
	case DDI_DETACH:
		DCMN_ERR((CE_CONT, "%s%d: DDI_DETACH\n",
		    panel_name, instance));

		/* turn off interrupt */
		statep->panelregs_state &= ~PNLIE_MASK;
		panel_ddi_put8(statep->panel_regs_handle, statep->panelregs,
		    statep->panelregs_state);

		/* free all resources for the dip */
		ddi_remove_intr(dip, 0, statep->iblock_cookie);

		/* need not free iblock_cookie */
		ddi_regs_map_free(&statep->panel_regs_handle);
		ddi_soft_state_free(panelstates, instance);

		return (DDI_SUCCESS);

	case DDI_SUSPEND:
		DCMN_ERR((CE_CONT, "%s%d: DDI_SUSPEND\n",
		    panel_name, instance));
		return (DDI_SUCCESS);

	default:
		return (DDI_FAILURE);

	}
	/* Not reached */
}

/*ARGSUSED*/
static int
panel_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg,  void **resultp)
{
	struct panel_state *statep;
	int	instance;
	dev_t	dev = (dev_t)arg;

	instance = getminor(dev);

	DCMN_ERR((CE_CONT, "%s%d: getinfo\n", panel_name, instance));

	switch (cmd) {
	case DDI_INFO_DEVT2DEVINFO:
		if ((statep = (struct panel_state *)
		    ddi_get_soft_state(panelstates, instance)) == NULL) {
			cmn_err(CE_WARN, "%s%d: ddi_get_soft_state failed.",
			    panel_name, instance);
			*resultp = NULL;
			return (DDI_FAILURE);
		}
		*resultp = statep->dip;
		break;
	case DDI_INFO_DEVT2INSTANCE:
		*resultp = (void *)(uintptr_t)instance;
		break;
	default:
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

static  uint_t
panel_intr(caddr_t arg)
{
	struct panel_state *statep = (struct panel_state *)arg;

	/* to confirm the validity of the interrupt */
	if (!(ddi_get8(statep->panel_regs_handle, statep->panelregs) &
	    PNLINT_MASK)) {
		return (DDI_INTR_UNCLAIMED);
	}

	/*
	 * Clear the PNLINT bit
	 * HW reported that there might be a delay in the PNLINT bit
	 * clearing. We force synchronization by attempting to read
	 * back the reg after clearing the bit.
	 */
	panel_ddi_put8(statep->panel_regs_handle, statep->panelregs,
	    statep->panelregs_state | PNLINT_MASK);
	(void) ddi_get8(statep->panel_regs_handle, statep->panelregs);

	if (panel_enable) {
		/* avoid double panic */
		panel_enable 	= 0;

		cmn_err(CE_PANIC,
		    "System Panel Driver: Emergency panic request "
		    "detected!");
		/* Not reached */
	}

	return (DDI_INTR_CLAIMED);
}

#ifdef DEBUG
static void
panel_ddi_put8(ddi_acc_handle_t handle, uint8_t *dev_addr, uint8_t value)
{
	if (panel_debug) {
		cmn_err(CE_CONT, "%s: old value = 0x%x\n",
		    panel_name, ddi_get8(handle, dev_addr));
		cmn_err(CE_CONT, "%s: writing value = 0x%x\n",
		    panel_name, value);
		ddi_put8(handle, dev_addr, value);
		cmn_err(CE_CONT, "%s: new value = 0x%x\n",
		    panel_name, ddi_get8(handle, dev_addr));
	} else {
		ddi_put8(handle, dev_addr, value);
	}
}
#endif