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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <sys/conf.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/modctl.h>
#include <sys/ddi_impldefs.h>
#include <sys/kmem.h>
#include <sys/devops.h>
/*
* Time periods, in nanoseconds
*/
#define PMUGPIO_TWO_SEC 2000000000LL
static dev_info_t *pmugpio_dip;
typedef struct pmugpio_state {
uint8_t *pmugpio_reset_reg;
ddi_acc_handle_t pmugpio_reset_reg_handle;
uint8_t *pmugpio_watchdog_reg;
ddi_acc_handle_t pmugpio_watchdog_reg_handle;
hrtime_t hw_last_pat;
} pmugpio_state_t;
static void *pmugpio_statep;
static int pmugpio_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
static int pmugpio_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
static int pmugpio_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
void **result);
static int pmugpio_map_regs(dev_info_t *, pmugpio_state_t *);
struct cb_ops pmugpio_cb_ops = {
nulldev, /* open */
nulldev, /* close */
nulldev, /* strategy */
nulldev, /* print */
nulldev, /* dump */
nulldev, /* read */
nulldev, /* write */
nulldev, /* ioctl */
nulldev, /* devmap */
nulldev, /* mmap */
nulldev, /* segmap */
nochpoll, /* poll */
ddi_prop_op, /* cb_prop_op */
NULL, /* streamtab */
D_MP | D_NEW
};
static struct dev_ops pmugpio_ops = {
DEVO_REV, /* Devo_rev */
0, /* Refcnt */
pmugpio_info, /* Info */
nulldev, /* Identify */
nulldev, /* Probe */
pmugpio_attach, /* Attach */
pmugpio_detach, /* Detach */
nodev, /* Reset */
&pmugpio_cb_ops, /* Driver operations */
0, /* Bus operations */
NULL /* Power */
};
static struct modldrv modldrv = {
&mod_driverops, /* This one is a driver */
"Pmugpio Driver %I%", /* Name of the module. */
&pmugpio_ops, /* Driver ops */
};
static struct modlinkage modlinkage = {
MODREV_1, (void *)&modldrv, NULL
};
int
_init(void)
{
int error;
/* Initialize the soft state structures */
if ((error = ddi_soft_state_init(&pmugpio_statep,
sizeof (pmugpio_state_t), 1)) != 0) {
return (error);
}
/* Install the loadable module */
if ((error = mod_install(&modlinkage)) != 0) {
ddi_soft_state_fini(&pmugpio_statep);
}
return (error);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
int
_fini(void)
{
int error;
error = mod_remove(&modlinkage);
if (error == 0) {
/* Release per module resources */
ddi_soft_state_fini(&pmugpio_statep);
}
return (error);
}
static int
pmugpio_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
int instance;
pmugpio_state_t *pmugpio_ptr = NULL;
switch (cmd) {
case DDI_ATTACH:
break;
case DDI_RESUME:
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
/* Get the instance and create soft state */
instance = ddi_get_instance(dip);
if (ddi_soft_state_zalloc(pmugpio_statep, instance) != 0) {
return (DDI_FAILURE);
}
pmugpio_ptr = ddi_get_soft_state(pmugpio_statep, instance);
if (pmugpio_ptr == NULL) {
return (DDI_FAILURE);
}
if (pmugpio_map_regs(dip, pmugpio_ptr) != DDI_SUCCESS) {
ddi_soft_state_free(pmugpio_statep, instance);
return (DDI_FAILURE);
}
/* Display information in the banner */
ddi_report_dev(dip);
/* Save the dip */
pmugpio_dip = dip;
return (DDI_SUCCESS);
}
/* ARGSUSED */
static int
pmugpio_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
/* Pointer to soft state */
switch (cmd) {
case DDI_SUSPEND:
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
/* ARGSUSED */
static int
pmugpio_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
void *arg, void **result)
{
dev_t dev;
int instance, error;
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
*result = (void *)pmugpio_dip;
error = DDI_SUCCESS;
break;
case DDI_INFO_DEVT2INSTANCE:
dev = (dev_t)arg;
instance = getminor(dev);
*result = (void *)(uintptr_t)instance;
error = DDI_SUCCESS;
break;
default:
error = DDI_FAILURE;
}
return (error);
}
void
pmugpio_watchdog_pat(void)
{
dev_info_t *dip = pmugpio_dip;
int instance;
pmugpio_state_t *pmugpio_ptr;
hrtime_t now;
uint8_t value;
if (dip == NULL) {
return;
}
instance = ddi_get_instance(dip);
pmugpio_ptr = ddi_get_soft_state(pmugpio_statep, instance);
if (pmugpio_ptr == NULL) {
return;
}
/*
* The RMC can read interrupts either high to low OR low to high. As
* a result all that needs to happen is that when we hit the time to
* send an signal we simply need to change the state.
*/
now = gethrtime();
if ((now - pmugpio_ptr->hw_last_pat) >= PMUGPIO_TWO_SEC) {
/*
* fetch current reg value and invert it
*/
value = (uint8_t)(0xff ^
ddi_get8(pmugpio_ptr->pmugpio_watchdog_reg_handle,
pmugpio_ptr->pmugpio_watchdog_reg));
ddi_put8(pmugpio_ptr->pmugpio_watchdog_reg_handle,
pmugpio_ptr->pmugpio_watchdog_reg, value);
pmugpio_ptr->hw_last_pat = now;
}
}
void
pmugpio_reset(void)
{
dev_info_t *dip = pmugpio_dip;
int instance;
pmugpio_state_t *pmugpio_ptr;
if (dip == NULL) {
return;
}
instance = ddi_get_instance(dip);
pmugpio_ptr = ddi_get_soft_state(pmugpio_statep, instance);
if (pmugpio_ptr == NULL) {
return;
}
/*
* turn all bits on then off again - pmubus nexus will ensure
* that only unmasked bit is affected
*/
ddi_put8(pmugpio_ptr->pmugpio_reset_reg_handle,
pmugpio_ptr->pmugpio_reset_reg, ~0);
ddi_put8(pmugpio_ptr->pmugpio_reset_reg_handle,
pmugpio_ptr->pmugpio_reset_reg, 0);
}
static int
pmugpio_map_regs(dev_info_t *dip, pmugpio_state_t *pmugpio_ptr)
{
ddi_device_acc_attr_t attr;
/* The host controller will be little endian */
attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
if (ddi_regs_map_setup(dip, 1,
(caddr_t *)&pmugpio_ptr->pmugpio_watchdog_reg, 0, 1, &attr,
&pmugpio_ptr->pmugpio_watchdog_reg_handle) != DDI_SUCCESS) {
return (DDI_FAILURE);
}
if (ddi_regs_map_setup(dip, 0,
(caddr_t *)&pmugpio_ptr->pmugpio_reset_reg, 0, 1, &attr,
&pmugpio_ptr->pmugpio_reset_reg_handle) != DDI_SUCCESS) {
ddi_regs_map_free(&pmugpio_ptr->pmugpio_watchdog_reg_handle);
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
|