summaryrefslogtreecommitdiff
path: root/usr/src/uts/sun4u/sunfire/io/sram.c
blob: 426c8b8cd1a809555e2a4cb339f5dd4ecb643327 (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
/*
 * 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 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
 */


#include <sys/types.h>
#include <sys/conf.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/ddi_impldefs.h>
#include <sys/obpdefs.h>
#include <sys/cmn_err.h>
#include <sys/errno.h>
#include <sys/kmem.h>
#include <sys/debug.h>
#include <sys/sysmacros.h>
#include <sys/autoconf.h>
#include <sys/modctl.h>

#include <sys/fhc.h>
#include <sys/sram.h>
#include <sys/promif.h>

/* Useful debugging Stuff */
#include <sys/nexusdebug.h>

/*
 * Function protoypes
 */

static int sram_attach(dev_info_t *, ddi_attach_cmd_t);

static int sram_detach(dev_info_t *, ddi_detach_cmd_t);

static void sram_add_kstats(struct sram_soft_state *);

/*
 * Configuration data structures
 */
static struct cb_ops sram_cb_ops = {
	nulldev,			/* open */
	nulldev,			/* close */
	nulldev,			/* strategy */
	nulldev,			/* print */
	nodev,				/* dump */
	nulldev,			/* read */
	nulldev,			/* write */
	nulldev,			/* ioctl */
	nodev,				/* devmap */
	nodev,				/* mmap */
	nodev,				/* segmap */
	nochpoll,			/* poll */
	ddi_prop_op,			/* cb_prop_op */
	0,				/* streamtab */
	D_MP | D_NEW | D_HOTPLUG,	/* Driver compatibility flag */
	CB_REV,				/* rev */
	nodev,				/* cb_aread */
	nodev				/* cb_awrite */
};

static struct dev_ops sram_ops = {
	DEVO_REV,			/* rev */
	0,				/* refcnt  */
	ddi_no_info,			/* getinfo */
	nulldev,			/* identify */
	nulldev,			/* probe */
	sram_attach,			/* attach */
	sram_detach,			/* detach */
	nulldev,			/* reset */
	&sram_cb_ops,			/* cb_ops */
	(struct bus_ops *)0,		/* bus_ops */
	nulldev,			/* power */
	ddi_quiesce_not_needed,			/* quiesce */
};


/*
 * Driver globals
 */
void *sramp;			/* sram soft state hook */
static struct kstat *resetinfo_ksp = NULL;
static int reset_info_created = 0;

extern struct mod_ops mod_driverops;

static struct modldrv modldrv = {
	&mod_driverops,		/* Type of module.  This one is a driver */
	"Sram Leaf",		/* name of module */
	&sram_ops,		/* driver ops */
};

static struct modlinkage modlinkage = {
	MODREV_1,
	(void *)&modldrv,
	NULL
};

/*
 * These are the module initialization routines.
 */

int
_init(void)
{
	int error;

	if ((error = ddi_soft_state_init(&sramp,
	    sizeof (struct sram_soft_state), 1)) == 0 &&
	    (error = mod_install(&modlinkage)) != 0)
		ddi_soft_state_fini(&sramp);
	return (error);
}

int
_fini(void)
{
	int error;

	if ((error = mod_remove(&modlinkage)) == 0)
		ddi_soft_state_fini(&sramp);
	return (error);
}

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

static int
sram_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
{
	int instance;
	struct sram_soft_state *softsp;

	switch (cmd) {
	case DDI_ATTACH:
		break;

	case DDI_RESUME:
		return (DDI_SUCCESS);

	default:
		return (DDI_FAILURE);
	}

	instance = ddi_get_instance(devi);

	if (ddi_soft_state_zalloc(sramp, instance) != DDI_SUCCESS)
		return (DDI_FAILURE);

	softsp = ddi_get_soft_state(sramp, instance);

	/* Set the dip in the soft state */
	softsp->dip = devi;

	/* get the board number from this devices parent. */
	softsp->pdip = ddi_get_parent(softsp->dip);
	if ((softsp->board = (int)ddi_getprop(DDI_DEV_T_ANY, softsp->pdip,
	    DDI_PROP_DONTPASS, OBP_BOARDNUM, -1)) == -1) {
		cmn_err(CE_WARN, "sram%d: unable to retrieve %s property",
		    instance, OBP_BOARDNUM);
		goto bad;
	}

	DPRINTF(SRAM_ATTACH_DEBUG, ("sram%d: devi= 0x%p\n, "
	    " softsp=0x%p\n", instance, (void *)devi, (void *)softsp));

	/* map in the registers for this device. */
	if (ddi_map_regs(softsp->dip, 0,
	    (caddr_t *)&softsp->sram_base, 0, 0)) {
		cmn_err(CE_WARN, "sram%d: unable to map registers",
		    instance);
		goto bad;
	}

	/* nothing to suspend/resume here */
	(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
	    "pm-hardware-state", "no-suspend-resume");

	/* create the kstats for this device. */
	sram_add_kstats(softsp);

	ddi_report_dev(devi);

	return (DDI_SUCCESS);

bad:
	ddi_soft_state_free(sramp, instance);
	return (DDI_FAILURE);
}

/* ARGSUSED */
static int
sram_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
{
	int instance;
	struct sram_soft_state *softsp;

	/* get the instance of this devi */
	instance = ddi_get_instance(devi);

	/* get the soft state pointer for this device node */
	softsp = ddi_get_soft_state(sramp, instance);

	switch (cmd) {
	case DDI_SUSPEND:
		return (DDI_SUCCESS);

	case DDI_DETACH:
		(void) fhc_bdlist_lock(softsp->board);
		if (fhc_bd_detachable(softsp->board))
			break;
		else
			fhc_bdlist_unlock();
		/* FALLTHROUGH */

	default:
		return (DDI_FAILURE);
	}

	fhc_bdlist_unlock();

	/*
	 * We do not remove the kstat here. There is only one instance for
	 * the whole machine, and it must remain in existence while the
	 * system is running.
	 */


	/* unmap the registers */
	ddi_unmap_regs(softsp->dip, 0,
	    (caddr_t *)&softsp->sram_base, 0, 0);

	/* free the soft state structure */
	ddi_soft_state_free(sramp, instance);

	ddi_prop_remove_all(devi);

	return (DDI_SUCCESS);
}

/*
 * The Reset-info structure passed up by POST has it's own kstat.
 * It only needs to get created once. So the first sram instance
 * that gets created will check for the OBP property 'reset-info'
 * in the root node of the OBP device tree. If this property exists,
 * then the reset-info kstat will get created. Otherwise it will
 * not get created. This will inform users whether or not a fatal
 * hardware reset has recently occurred.
 */
static void
sram_add_kstats(struct sram_soft_state *softsp)
{
	int reset_size;		/* size of data collected by POST */
	char *ksptr;		/* memory pointer for byte copy */
	char *srptr;		/* pointer to sram for byte copy */
	int i;
	union  {
		char size[4];	/* copy in word byte-by-byte */
		uint_t len;
	} rst_size;

	/*
	 * only one reset_info kstat per system, so don't create it if
	 * it exists already.
	 */
	if (reset_info_created) {
		return;
	}

	/* mark that this code has been run. */
	reset_info_created = 1;

	/* does the root node have a 'fatal-reset-info' property? */
	if (prom_getprop(prom_rootnode(), "fatal-reset-info",
	    (caddr_t)&softsp->offset) == -1) {
		return;
	}

	/* XXX - workaround for OBP bug */
	softsp->reset_info = softsp->sram_base + softsp->offset;

	/*
	 * First read size. In case FW has not word aligned structure,
	 * copy the unsigned int into a 4 byte union, then read it out as
	 * an inteeger.
	 */
	for (i = 0, srptr = softsp->reset_info; i < 4; i++) {
		rst_size.size[i] = *srptr++;
	}
	reset_size = rst_size.len;

	/*
	 * If the reset size is zero, then POST did not
	 * record any info.
	 */
	if ((uint_t)reset_size == 0) {
		return;
	}

	/* Check for illegal size values. */
	if ((uint_t)reset_size > MX_RSTINFO_SZ) {
		cmn_err(CE_NOTE, "sram%d: illegal "
		    "reset_size: 0x%x",
		    ddi_get_instance(softsp->dip),
		    reset_size);
		return;
	}

	/* create the reset-info kstat */
	resetinfo_ksp = kstat_create("unix", 0,
	    RESETINFO_KSTAT_NAME, "misc", KSTAT_TYPE_RAW,
	    reset_size, KSTAT_FLAG_PERSISTENT);

	if (resetinfo_ksp == NULL) {
		cmn_err(CE_WARN, "sram%d: kstat_create failed",
		    ddi_get_instance(softsp->dip));
		return;
	}

	/*
	 * now copy the data into kstat. Don't use block
	 * copy, the local space sram does not support this.
	 */
	srptr = softsp->reset_info;

	ksptr = (char *)resetinfo_ksp->ks_data;

	for (i = 0; i < reset_size; i++) {
		*ksptr++ = *srptr++;
	}

	kstat_install(resetinfo_ksp);
}