source: mainline/uspace/drv/platform/pc/pc.c

Last change on this file was b25970f, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix ISA-only PC support.

  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2 * Copyright (c) 2018 Jiri Svoboda
3 * Copyright (c) 2010 Lenka Trochtova
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/**
31 * @addtogroup pc pc
32 * @{
33 */
34
35/** @file
36 */
37
38#include <assert.h>
39#include <stdio.h>
40#include <errno.h>
41#include <stdbool.h>
42#include <fibril_synch.h>
43#include <stdlib.h>
44#include <str.h>
45#include <ctype.h>
46#include <macros.h>
47
48#include <ddf/driver.h>
49#include <ddf/log.h>
50#include <ipc/dev_iface.h>
51#include <ops/hw_res.h>
52#include <ops/pio_window.h>
53
54#define NAME "pc"
55
56typedef struct pc_fun {
57 hw_resource_list_t hw_resources;
58 pio_window_t pio_window;
59} pc_fun_t;
60
61static errno_t pc_dev_add(ddf_dev_t *dev);
62static void pc_init(void);
63
64/** The root device driver's standard operations. */
65static driver_ops_t pc_ops = {
66 .dev_add = &pc_dev_add
67};
68
69/** The root device driver structure. */
70static driver_t pc_driver = {
71 .name = NAME,
72 .driver_ops = &pc_ops
73};
74
75static hw_resource_t pci_conf_regs[] = {
76 {
77 .type = IO_RANGE,
78 .res.io_range = {
79 .address = 0xCF8,
80 .size = 4,
81 .relative = false,
82 .endianness = LITTLE_ENDIAN
83 }
84 },
85 {
86 .type = IO_RANGE,
87 .res.io_range = {
88 .address = 0xCFC,
89 .size = 4,
90 .relative = false,
91 .endianness = LITTLE_ENDIAN
92 }
93 }
94};
95
96static pc_fun_t sys_data = {
97 .hw_resources = {
98 sizeof(pci_conf_regs) / sizeof(pci_conf_regs[0]),
99 pci_conf_regs
100 },
101 .pio_window = {
102 .mem = {
103 .base = UINT32_C(0),
104 .size = UINT32_C(0xffffffff) /* practical maximum */
105 },
106 .io = {
107 .base = UINT32_C(0),
108 .size = UINT32_C(0x10000)
109 }
110 }
111};
112
113/** Obtain function soft-state from DDF function node */
114static pc_fun_t *pc_fun(ddf_fun_t *fnode)
115{
116 return ddf_fun_data_get(fnode);
117}
118
119static hw_resource_list_t *pc_get_resources(ddf_fun_t *fnode)
120{
121 pc_fun_t *fun = pc_fun(fnode);
122
123 assert(fun != NULL);
124 return &fun->hw_resources;
125}
126
127static errno_t pc_enable_interrupt(ddf_fun_t *fun, int irq)
128{
129 /* TODO */
130
131 return false;
132}
133
134static pio_window_t *pc_get_pio_window(ddf_fun_t *fnode)
135{
136 pc_fun_t *fun = pc_fun(fnode);
137
138 assert(fun != NULL);
139 return &fun->pio_window;
140}
141
142static hw_res_ops_t fun_hw_res_ops = {
143 .get_resource_list = &pc_get_resources,
144 .enable_interrupt = &pc_enable_interrupt,
145};
146
147static pio_window_ops_t fun_pio_window_ops = {
148 .get_pio_window = &pc_get_pio_window
149};
150
151/* Initialized in pc_init() function. */
152static ddf_dev_ops_t pc_fun_ops;
153
154static errno_t pc_add_sysbus(ddf_dev_t *dev)
155{
156 ddf_fun_t *fnode = NULL;
157 errno_t rc;
158
159 ddf_msg(LVL_DEBUG, "Adding system bus.");
160
161 /* Create new device. */
162 fnode = ddf_fun_create(dev, fun_inner, "sys");
163 if (fnode == NULL) {
164 rc = ENOMEM;
165 goto error;
166 }
167
168 pc_fun_t *fun = ddf_fun_data_alloc(fnode, sizeof(pc_fun_t));
169 *fun = sys_data;
170
171 /* Add match IDs */
172 rc = ddf_fun_add_match_id(fnode, "intel_pci", 100);
173 if (rc != EOK)
174 goto error;
175
176 rc = ddf_fun_add_match_id(fnode, "isa", 10);
177 if (rc != EOK)
178 goto error;
179
180 /* Set provided operations to the device. */
181 ddf_fun_set_ops(fnode, &pc_fun_ops);
182
183 /* Register function. */
184 rc = ddf_fun_bind(fnode);
185 if (rc != EOK) {
186 ddf_msg(LVL_ERROR, "Failed binding system bus function.");
187 goto error;
188 }
189
190 return EOK;
191
192error:
193 if (fnode != NULL)
194 ddf_fun_destroy(fnode);
195
196 ddf_msg(LVL_ERROR, "Failed adding system bus.");
197 return rc;
198}
199
200static errno_t pc_add_functions(ddf_dev_t *dev)
201{
202 errno_t rc;
203
204 rc = pc_add_sysbus(dev);
205 if (rc != EOK)
206 return rc;
207
208 return EOK;
209}
210
211/** Get the root device.
212 *
213 * @param dev The device which is root of the whole device tree (both
214 * of HW and pseudo devices).
215 * @return Zero on success, error number otherwise.
216 */
217static errno_t pc_dev_add(ddf_dev_t *dev)
218{
219 errno_t rc;
220
221 ddf_msg(LVL_DEBUG, "pc_dev_add, device handle = %d",
222 (int)ddf_dev_get_handle(dev));
223
224 /* Register functions. */
225 rc = pc_add_functions(dev);
226 if (rc != EOK) {
227 ddf_msg(LVL_ERROR, "Failed to add functions for PC platform.");
228 return rc;
229 }
230
231 return EOK;
232}
233
234static void pc_init(void)
235{
236 ddf_log_init(NAME);
237 pc_fun_ops.interfaces[HW_RES_DEV_IFACE] = &fun_hw_res_ops;
238 pc_fun_ops.interfaces[PIO_WINDOW_DEV_IFACE] = &fun_pio_window_ops;
239}
240
241int main(int argc, char *argv[])
242{
243 printf(NAME ": HelenOS PC platform driver\n");
244 pc_init();
245 return ddf_driver_main(&pc_driver);
246}
247
248/**
249 * @}
250 */
Note: See TracBrowser for help on using the repository browser.