Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / obp / tools / promif / common / prom_ioctl.c
CommitLineData
920dae64
AT
1/*
2* ========== Copyright Header Begin ==========================================
3*
4* Hypervisor Software File: prom_ioctl.c
5*
6* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
7*
8* - Do no alter or remove copyright notices
9*
10* - Redistribution and use of this software in source and binary forms, with
11* or without modification, are permitted provided that the following
12* conditions are met:
13*
14* - Redistribution of source code must retain the above copyright notice,
15* this list of conditions and the following disclaimer.
16*
17* - Redistribution in binary form must reproduce the above copyright notice,
18* this list of conditions and the following disclaimer in the
19* documentation and/or other materials provided with the distribution.
20*
21* Neither the name of Sun Microsystems, Inc. or the names of contributors
22* may be used to endorse or promote products derived from this software
23* without specific prior written permission.
24*
25* This software is provided "AS IS," without a warranty of any kind.
26* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
27* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
28* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
29* MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
30* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
31* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
32* OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
33* FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
34* DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
35* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
36* SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
37*
38* You acknowledge that this software is not designed, licensed or
39* intended for use in the design, construction, operation or maintenance of
40* any nuclear facility.
41*
42* ========== Copyright Header End ============================================
43*/
44/*
45 * Copyright (c) 2001-2003 Sun Microsystems, Inc.
46 * All rights reserved.
47 * Use is subject to license terms.
48 */
49
50#pragma ident "@(#)prom_ioctl.c 1.1 01/04/19 SMI"
51
52#include <sys/promif.h>
53#include <sys/promimpl.h>
54#include <sys/openpromio.h>
55#include <sys/mman.h>
56
57static phandle_t oprom_dev_phandle;
58static phandle_t flash_dev_phandle;
59typedef struct openpromio openpromio_t;
60
61int32_t
62ioctl(int32_t fildes, int32_t request, void *arg)
63{
64 phandle_t flash_phandle;
65 phandle_t opt_phandle;
66
67 /*
68 * Currently only supports /dev/openprom and /dev/flashprom
69 */
70 if (fildes == NULL) {
71 prom_printf("Null file descriptor\n");
72 return (-1);
73 } else if (fildes == OPENPROM_FD) {
74
75 openpromio_t *op_struct = (openpromio_t *)arg;
76
77 switch (request) {
78 case OPROMGETOPT:
79 opt_phandle = prom_finddevice("/options");
80 op_struct->oprom_size = prom_getprop(opt_phandle,
81 op_struct->opio_u.b, op_struct->opio_u.b);
82 break;
83
84 case OPROMGETPROP:
85 op_struct->oprom_size = prom_getprop(oprom_dev_phandle,
86 op_struct->opio_u.b, op_struct->opio_u.b);
87 break;
88
89 case OPROMNXTOPT:
90 opt_phandle = prom_finddevice("/options");
91 op_struct->oprom_size = prom_nextprop(opt_phandle,
92 op_struct->opio_u.b, op_struct->opio_u.b);
93 break;
94
95 case OPROMNXTPROP:
96 op_struct->oprom_size = prom_nextprop(oprom_dev_phandle,
97 op_struct->opio_u.b, op_struct->opio_u.b);
98 break;
99
100 case OPROMGETPROPLEN:
101 op_struct->opio_u.i = prom_getproplen(oprom_dev_phandle,
102 op_struct->opio_u.b);
103 break;
104
105 case OPROMCHILD:
106 op_struct->opio_u.i = prom_childnode(op_struct->opio_u.i);
107 oprom_dev_phandle = op_struct->opio_u.i;
108 break;
109
110 case OPROMNEXT:
111 op_struct->opio_u.i = prom_nextnode(op_struct->opio_u.i);
112 oprom_dev_phandle = op_struct->opio_u.i;
113 break;
114
115 case OPROMGETVERSION:
116 flash_phandle = prom_finddevice("/flashprom");
117 op_struct->oprom_size = prom_getprop(flash_phandle, "version",
118 op_struct->opio_u.b);
119 break;
120
121 default:
122 printf("Unrecognized stream request %u\n", request);
123 return (-1);
124
125 } } else if (fildes == FLASHPROM_FD) { /* OPENPROM_FD and switch */
126
127 int32_t protection;
128 uchar_t *gpio_va;
129
130 switch (request) {
131 case _PGI:
132 protection = (PROT_READ | PROT_WRITE);
133 gpio_va = (uchar_t *)mmap((caddr_t)0, 1,
134 protection, MAP_SHARED, fildes, (1<< 28));
135 if (gpio_va == MAP_FAILED) {
136 printf("ERROR: Unable to map in flashprom gpio\n");
137 return (-1);
138 }
139 *gpio_va = *(uchar_t *)arg;
140 break;
141
142 case _PGO:
143 protection = PROT_READ;
144 gpio_va = (uchar_t *)mmap((caddr_t)0, 1,
145 protection, MAP_SHARED, fildes, (1<< 28));
146 if (gpio_va == MAP_FAILED) {
147 printf("ERROR: Unable to map in flashprom gpio\n");
148 return (-1);
149 }
150 *(uchar_t *)arg = *gpio_va;
151 break;
152
153 default:
154 printf("Unrecognized stream request %u\n", request);
155 return (-1);
156
157 } } else { /* FLASHPROM_FD and switch */
158 prom_printf("Unknown file stream type\n");
159 return (-1);
160 }
161
162 return (0);
163
164}