source: mainline/uspace/drv/infrastructure/rootamdm37x/rootamdm37x.c@ 708ec60

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 708ec60 was 708ec60, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

rootamdm37x: Replace magic numbers with proper register struct and defines.

  • Property mode set to 100644
File size: 7.5 KB
Line 
1/*
2 * Copyright (c) 2012 Jan Vesely
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/**
30 * @defgroup root_amdm37x TI AM/DM37x platform driver.
31 * @brief HelenOS TI AM/DM37x platform driver.
32 * @{
33 */
34
35/** @file
36 */
37
38#include <ddf/driver.h>
39#include <ddf/log.h>
40#include <errno.h>
41#include <ops/hw_res.h>
42#include <stdio.h>
43#include <ddi.h>
44
45#include "uhh.h"
46#include "usbtll.h"
47#include "core_cm.h"
48#include "usbhost_cm.h"
49
50#define NAME "rootamdm37x"
51
52/** Obtain function soft-state from DDF function node */
53#define ROOTARM_FUN(fnode) \
54 ((rootamdm37x_fun_t *) (fnode)->driver_data)
55
56typedef struct {
57 hw_resource_list_t hw_resources;
58} rootamdm37x_fun_t;
59
60#define OHCI_BASE_ADDRESS 0x48064400
61#define OHCI_SIZE 1024
62#define EHCI_BASE_ADDRESS 0x48064800
63#define EHCI_SIZE 1024
64
65static hw_resource_t ohci_res[] = {
66 {
67 .type = MEM_RANGE,
68 /* See amdm37x TRM page. 3316 for these values */
69 .res.io_range = {
70 .address = OHCI_BASE_ADDRESS,
71 .size = OHCI_SIZE,
72 .endianness = LITTLE_ENDIAN
73 },
74 },
75 {
76 .type = INTERRUPT,
77 .res.interrupt = { .irq = 76 },
78 },
79};
80
81static const rootamdm37x_fun_t ohci = {
82 .hw_resources = {
83 .resources = ohci_res,
84 .count = sizeof(ohci_res)/sizeof(ohci_res[0]),
85 }
86};
87
88static hw_resource_t ehci_res[] = {
89 {
90 .type = MEM_RANGE,
91 /* See amdm37x TRM page. 3316 for these values */
92 .res.io_range = {
93 .address = EHCI_BASE_ADDRESS,
94 .size = EHCI_SIZE,
95 .endianness = LITTLE_ENDIAN
96 },
97 },
98 {
99 .type = INTERRUPT,
100 .res.interrupt = { .irq = 77 },
101 },
102};
103
104static const rootamdm37x_fun_t ehci = {
105 .hw_resources = {
106 .resources = ehci_res,
107 .count = sizeof(ehci_res)/sizeof(ehci_res[0]),
108 }
109};
110
111static hw_resource_list_t *rootamdm37x_get_resources(ddf_fun_t *fnode);
112static bool rootamdm37x_enable_interrupt(ddf_fun_t *fun);
113
114static hw_res_ops_t fun_hw_res_ops = {
115 .get_resource_list = &rootamdm37x_get_resources,
116 .enable_interrupt = &rootamdm37x_enable_interrupt,
117};
118
119static ddf_dev_ops_t rootamdm37x_fun_ops =
120{
121 .interfaces[HW_RES_DEV_IFACE] = &fun_hw_res_ops
122};
123
124static int usb_clocks(bool on)
125{
126 usbhost_cm_regs_t *usb_host_cm = NULL;
127 core_cm_regs_t *l4_core_cm = NULL;
128
129 int ret = pio_enable((void*)USBHOST_CM_BASE_ADDRESS, USBHOST_CM_SIZE,
130 (void**)&usb_host_cm);
131 if (ret != EOK)
132 return ret;
133
134 ret = pio_enable((void*)CORE_CM_BASE_ADDRESS, CORE_CM_SIZE,
135 (void**)&l4_core_cm);
136 if (ret != EOK)
137 return ret;
138
139 assert(l4_core_cm);
140 assert(usb_host_cm);
141 if (on) {
142 l4_core_cm->iclken3 |= CORE_CM_ICLKEN3_EN_USBTLL_FLAG;
143 l4_core_cm->fclken3 |= CORE_CM_FCLKEN3_EN_USBTLL_FLAG;
144 usb_host_cm->iclken |= USBHOST_CM_ICLKEN_EN_USBHOST;
145 usb_host_cm->fclken |= USBHOST_CM_FCLKEN_EN_USBHOST1_FLAG;
146 usb_host_cm->fclken |= USBHOST_CM_FCLKEN_EN_USBHOST2_FLAG;
147 } else {
148 usb_host_cm->fclken &= ~USBHOST_CM_FCLKEN_EN_USBHOST2_FLAG;
149 usb_host_cm->fclken &= ~USBHOST_CM_FCLKEN_EN_USBHOST1_FLAG;
150 usb_host_cm->iclken &= ~USBHOST_CM_ICLKEN_EN_USBHOST;
151 l4_core_cm->fclken3 &= ~CORE_CM_FCLKEN3_EN_USBTLL_FLAG;
152 l4_core_cm->iclken3 &= ~CORE_CM_ICLKEN3_EN_USBTLL_FLAG;
153 }
154
155 //TODO Unmap those registers.
156
157 return ret;
158}
159
160static int usb_tll_init()
161{
162 tll_regs_t *usb_tll = NULL;
163 uhh_regs_t *uhh_conf = NULL;
164
165 int ret = pio_enable((void*)AMDM37x_USBTLL_BASE_ADDRESS,
166 AMDM37x_USBTLL_SIZE, (void**)&usb_tll);
167 if (ret != EOK)
168 return ret;
169
170 ret = pio_enable((void*)AMDM37x_UHH_BASE_ADDRESS,
171 AMDM37x_UHH_SIZE, (void**)&uhh_conf);
172 if (ret != EOK)
173 return ret;
174
175 /* Reset USB tll */
176 usb_tll->sysconfig |= TLL_SYSCONFIG_SOFTRESET_FLAG;
177 ddf_msg(LVL_DEBUG2, "Waiting for USB TLL reset");
178 while (!(usb_tll->sysstatus & TLL_SYSSTATUS_RESET_DONE_FLAG));
179 ddf_msg(LVL_DEBUG, "USB TLL Reset done.");
180
181 return EOK;
182}
183
184static bool rootamdm37x_add_fun(ddf_dev_t *dev, const char *name,
185 const char *str_match_id, const rootamdm37x_fun_t *fun)
186{
187 ddf_msg(LVL_DEBUG, "Adding new function '%s'.", name);
188
189 ddf_fun_t *fnode = NULL;
190 match_id_t *match_id = NULL;
191
192 /* Create new device. */
193 fnode = ddf_fun_create(dev, fun_inner, name);
194 if (fnode == NULL)
195 goto failure;
196
197 fnode->driver_data = (void*)fun;
198
199 /* Initialize match id list */
200 match_id = create_match_id();
201 if (match_id == NULL)
202 goto failure;
203
204 match_id->id = str_match_id;
205 match_id->score = 100;
206 add_match_id(&fnode->match_ids, match_id);
207
208 /* Set provided operations to the device. */
209 fnode->ops = &rootamdm37x_fun_ops;
210
211 /* Register function. */
212 if (ddf_fun_bind(fnode) != EOK) {
213 ddf_msg(LVL_ERROR, "Failed binding function %s.", name);
214 goto failure;
215 }
216
217 return true;
218
219failure:
220 if (match_id != NULL)
221 match_id->id = NULL;
222
223 if (fnode != NULL) {
224 fnode->driver_data = NULL;
225 ddf_fun_destroy(fnode);
226 }
227
228 ddf_msg(LVL_ERROR, "Failed adding function '%s'.", name);
229
230 return false;
231}
232
233/** Add the root device.
234 *
235 * @param dev Device which is root of the whole device tree
236 * (both of HW and pseudo devices).
237 *
238 * @return Zero on success, negative error number otherwise.
239 *
240 */
241static int rootamdm37x_dev_add(ddf_dev_t *dev)
242{
243 int ret = usb_clocks(true);
244 if (ret != EOK) {
245 ddf_msg(LVL_FATAL, "Failed to enable USB HC clocks!.\n");
246 return ret;
247 }
248
249 ret = usb_tll_init();
250 if (ret != EOK) {
251 ddf_msg(LVL_FATAL, "Failed to init USB TLL!.\n");
252 usb_clocks(false);
253 return ret;
254 }
255
256 /* Register functions */
257 if (!rootamdm37x_add_fun(dev, "ohci", "usb/host=ohci", &ohci))
258 ddf_msg(LVL_ERROR, "Failed to add OHCI function for "
259 "BeagleBoard-xM platform.");
260 if (!rootamdm37x_add_fun(dev, "ehci", "usb/host=ehci", &ehci))
261 ddf_msg(LVL_ERROR, "Failed to add EHCI function for "
262 "BeagleBoard-xM platform.");
263
264 return EOK;
265}
266
267/** The root device driver's standard operations. */
268static driver_ops_t rootamdm37x_ops = {
269 .dev_add = &rootamdm37x_dev_add
270};
271
272/** The root device driver structure. */
273static driver_t rootamdm37x_driver = {
274 .name = NAME,
275 .driver_ops = &rootamdm37x_ops
276};
277
278static hw_resource_list_t *rootamdm37x_get_resources(ddf_fun_t *fnode)
279{
280 rootamdm37x_fun_t *fun = ROOTARM_FUN(fnode);
281 assert(fun != NULL);
282 return &fun->hw_resources;
283}
284
285static bool rootamdm37x_enable_interrupt(ddf_fun_t *fun)
286{
287 /* TODO */
288 return false;
289}
290
291int main(int argc, char *argv[])
292{
293 printf("%s: HelenOS AM/DM37x(OMAP37x) platform driver\n", NAME);
294 ddf_log_init(NAME, LVL_ERROR);
295 return ddf_driver_main(&rootamdm37x_driver);
296}
297
298/**
299 * @}
300 */
Note: See TracBrowser for help on using the repository browser.