summaryrefslogtreecommitdiffstats
path: root/cfe/cfe/usb/usbmain.c
blob: c39a7f6d69c26196188669571478ed315bf56ff5 (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
359
360
361
362
363
364
365
366
367
/*  *********************************************************************
    *  Broadcom Common Firmware Environment (CFE)
    *  
    *  Main Module				File: usbmain.c
    *  
    *  Main module that invokes the top of the USB stack from CFE.
    *  
    *  Author:  Mitch Lichtenberg (mpl@broadcom.com)
    *  
    *********************************************************************  
    *
    *  Copyright 2000,2001,2002,2003
    *  Broadcom Corporation. All rights reserved.
    *  
    *  This software is furnished under license and may be used and 
    *  copied only in accordance with the following terms and 
    *  conditions.  Subject to these conditions, you may download, 
    *  copy, install, use, modify and distribute modified or unmodified 
    *  copies of this software in source and/or binary form.  No title 
    *  or ownership is transferred hereby.
    *  
    *  1) Any source code used, modified or distributed must reproduce 
    *     and retain this copyright notice and list of conditions 
    *     as they appear in the source file.
    *  
    *  2) No right is granted to use any trade name, trademark, or 
    *     logo of Broadcom Corporation.  The "Broadcom Corporation" 
    *     name may not be used to endorse or promote products derived 
    *     from this software without the prior written permission of 
    *     Broadcom Corporation.
    *  
    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT 
    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN 
    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF 
    *     THE POSSIBILITY OF SUCH DAMAGE.
    ********************************************************************* */


#include "lib_types.h"
#include "lib_malloc.h"
#include "lib_string.h"
#include "lib_printf.h"
#include "lib_queue.h"
#include "lib_physio.h"

#include "cfe_timer.h"
#include "ui_command.h"

#if CFG_PCI
#include "pcireg.h"
#include "pcivar.h"
#endif

#include "usbchap9.h"
#include "usbd.h"

#include "bsp_config.h"


/*  *********************************************************************
    *  Externs
    ********************************************************************* */

extern usb_hcdrv_t ohci_driver;			/* OHCI Driver dispatch */

extern int ohcidebug;				/* OHCI debug control */
extern int usb_noisy;				/* USBD debug control */

int usb_init(void);				/* forward */
int ui_init_usbcmds(void);			/* forward */

/*  *********************************************************************
    *  Globals
    ********************************************************************* */

/*
 * We keep track of the pointers to USB buses in globals.
 * One entry in this array per USB bus (the Opti controller
 * on the SWARM has two functions, so it's two buses)
 */

#define USB_MAX_BUS 4
int usb_buscnt = 0;
usbbus_t *usb_buses[USB_MAX_BUS];


/*  *********************************************************************
    *  usb_cfe_timer(arg)
    *  
    *  This routine is called periodically by CFE's timer routines
    *  to give the USB subsystem some time.  Basically we scan
    *  for work to do to manage configuration updates, and handle
    *  interrupts from the USB controllers.
    *  
    *  Input parameters: 
    *  	   arg - value we passed when the timer was initialized
    *  	          (not used)
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

static void usb_cfe_timer(void *arg)
{
    int idx;
    static int in_poll = 0;

    /*
     * We sometimes call the timer routines in here, which calls
     * the polling loop.  This code is not reentrant, so
     * prevent us from running the interrupt routine or
     * bus daemon while we are already in there.
     */

    if (in_poll) return;

    /*
     * Do not allow nested "interrupts."
     */

    in_poll = 1;

    for (idx = 0; idx < usb_buscnt; idx++) {
	if (usb_buses[idx]) {
	    usb_poll(usb_buses[idx]);
	    usb_daemon(usb_buses[idx]);
	    }
	}

    /*
     * Okay to call polling again.
     */

    in_poll = 0;
}


/*  *********************************************************************
    *  usb_init_one_ohci(addr)
    *  
    *  Initialize one USB controller.
    *  
    *  Input parameters: 
    *  	   addr - physical address of OHCI registers
    *  	   
    *  Return value:
    *  	   0 if ok
    *  	   else error
    ********************************************************************* */
static int usb_init_one_ohci(uint32_t addr)
{
    usbbus_t *bus;
    int res;

    bus = UBCREATE(&ohci_driver, addr);

    if (bus == NULL) {
	printf("USB: Could not create OHCI driver structure for controller at 0x%08X\n",addr);
	return -1;
	}

    bus->ub_num = usb_buscnt;

    res = UBSTART(bus);

    if (res != 0) {
	printf("USB: Could not init OHCI controller at 0x%08X\n",addr);
	UBSTOP(bus);
	return -1;
	}
    else {
	usb_buses[usb_buscnt++] = bus;
	usb_initroot(bus);
	}

    return 0;
}

#if CFG_PCI
/*  *********************************************************************
    *  usb_init_pci_ohci()
    *  
    *  Initialize all PCI-based OHCI controllers
    *  
    *  Input parameters: 
    *  	   nothing
    *  	   
    *  Return value:
    *  	   0 if ok
    *  	   else error
    ********************************************************************* */
static int usb_init_pci_ohci(void)
{
    int res;
    pcitag_t tag;
    uint32_t pciclass;
    physaddr_t bar;
    int idx;

    idx = 0;

    while (pci_find_class(PCI_CLASS_SERIALBUS,idx,&tag) == 0) {
	pciclass = pci_conf_read(tag,PCI_CLASS_REG);
	if ((PCI_SUBCLASS(pciclass) == PCI_SUBCLASS_SERIALBUS_USB) && 
	    (PCI_INTERFACE(pciclass) == 0x10)) {
	    bar = (physaddr_t) pci_conf_read(tag,PCI_MAPREG_START);
	    pci_tagprintf(tag,"OHCI USB controller found at %08X\n",(uint32_t) bar);

	    /* On the BCM1250, this sets the address to "match bits" mode, 
	       which eliminates the need for byte swaps of data to/from the registers. */
	    bar |= 0x20000000;

	    res = usb_init_one_ohci(bar);
	    if (res < 0) break;
	    }
	idx++;
	}

    return 0;
}
#endif



/*  *********************************************************************
    *  usb_init()
    *  
    *  Initialize the USB subsystem
    *  
    *  Input parameters: 
    *  	   nothing
    *  	   
    *  Return value:
    *  	   0 if ok
    *  	   else error code
    ********************************************************************* */

int usb_init(void)
{
    static int initdone = 0;

    if (initdone) {
	printf("USB has already been initialized.\n");
	return -1;
	}

    printf("Initializing USB.\n");

    initdone = 1;

    usb_buscnt = 0;

#if CFG_PCI
    usb_init_pci_ohci();
#endif

#if CFG_USB_OHCI_BASE
    usb_init_one_ohci(CFG_USB_OHCI_BASE);
#endif
	    
    cfe_bg_add(usb_cfe_timer,NULL);

    return 0;
}



static int ui_cmd_usb_start(ui_cmdline_t *cmd,int argc,char *argv[])
{
    int res = 0;

    if (cmd_sw_isset(cmd,"-o")) ohcidebug++;
    if (cmd_sw_isset(cmd,"-oo")) ohcidebug+=2;
    if (cmd_sw_isset(cmd,"-u")) usb_noisy++;
    if (cmd_sw_isset(cmd,"-uu")) usb_noisy+=2;

    if (usb_buscnt == 0) {
	res = usb_init();
	}
    
    return res;
}


static int ui_cmd_usb_show(ui_cmdline_t *cmd,int argc,char *argv[])
{
    int busnum;
    int devnum;
    char *x;
    int idx;
    uint32_t arg;

    x = cmd_getarg(cmd,1);
    if (!x) devnum = 0;
    else devnum = atoi(x);

    x = cmd_getarg(cmd,0);
    if (!x) x = "*";
    busnum = atoi(x);

    if (busnum >= usb_buscnt) {
	printf("Invalid bus number,  %d USB Buses currently configured.\n",usb_buscnt);
	return -1;
	}

    arg = cmd_sw_isset(cmd,"-v") ? 0x100 : 0;
    arg |= (devnum & 0xFF);

    if (x[0] == '*') {
	for (idx = 0; idx < usb_buscnt; idx++) {
	    usbhub_dumpbus(usb_buses[idx],arg);
	    }
	}
    else {
	usbhub_dumpbus(usb_buses[busnum],arg);
	}
    
    return 0;

}

/*  *********************************************************************
    *  ui_init_usbcmds(void)
    *  
    *  Initialize the USB commands
    *  
    *  Input parameters: 
    *  	   nothing
    *  	   
    *  Return value:
    *  	   0
    ********************************************************************* */

int ui_init_usbcmds(void)
{
    cmd_addcmd("usb init",
	       ui_cmd_usb_start,
	       NULL,
	       "Initialize the USB controller.",
	       "usb init",
	       "-o;OHCI debug messages|"
	       "-oo;more OHCI debug messages|"
	       "-u;USBD debug messages|"
	       "-uu;more USBD debug messages");


    cmd_addcmd("show usb",
	       ui_cmd_usb_show,
	       NULL,
	       "Display devices connected to USB bus.",
	       "usb show [bus [device]]\n\n"
	       "Displays the configuration descriptors for devices connected to the USB\n"
	       "If you specify a bus, the entire bus is displayed.  If you specify the\n"
	       "device number as well, only the specified device is displayed\n",
	       "-v;Display descriptors from the devices");

    return 0;
}