1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** USBX Component                                                        */
17 /**                                                                       */
18 /**   OHCI Controller Driver                                              */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /* Include necessary system files.  */
25 
26 #define UX_SOURCE_CODE
27 
28 #include "ux_api.h"
29 #include "ux_hcd_ohci.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_hcd_ohci_periodic_tree_create                   PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*     This function creates the periodic static tree for the interrupt   */
46 /*     and isochronous eds.                                               */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    hcd_ohci                              Pointer to OHCI controller    */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    Completion Status                                                   */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_hcd_ohci_ed_obtain                Obtain an ED                  */
59 /*    _ux_utility_physical_address          Get physical address          */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    OHCI Controller Driver                                              */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
70 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            resulting in version 6.1    */
72 /*                                                                        */
73 /**************************************************************************/
_ux_hcd_ohci_periodic_tree_create(UX_HCD_OHCI * hcd_ohci)74 UINT  _ux_hcd_ohci_periodic_tree_create(UX_HCD_OHCI *hcd_ohci)
75 {
76 
77 UX_HCD_OHCI_HCCA        *ohci_hcca;
78 UX_OHCI_ED              *ed;
79 UINT                    list_index;
80 UINT                    list_entries;
81 UINT                    current_list_entry;
82 UX_OHCI_ED              *ed_list[32];
83 UX_OHCI_ED              *ed_start_list[32];
84 
85 
86     /* We need the pointer to the HCCA. It contains the pointer to the first
87        32 periodic entries.  */
88     ohci_hcca =  hcd_ohci -> ux_hcd_ohci_hcca;
89 
90     /* Start with the 1st list - it has 32 entries.  */
91     list_entries =  32;
92 
93     /* Create each list one by one starting from the 32ms list.  */
94     for (list_index = 0; list_index < 6; list_index++)
95     {
96 
97         for (current_list_entry = 0; current_list_entry < list_entries;current_list_entry++)
98         {
99 
100             /* In each list, insert an static ED as the anchor. There should not
101                be any errors when obtaining a new ED, still we do a sanity check.  */
102             ed =  _ux_hcd_ohci_ed_obtain(hcd_ohci);
103             if (ed == UX_NULL)
104                 return(UX_NO_ED_AVAILABLE);
105 
106             /* Mark this anchor ED as static by putting it as SKIPPED, the OHCI
107                controller will not look into its tail and head list and will simply
108                jump to the next ED.  */
109             ed -> ux_ohci_ed_dw0 =  UX_OHCI_ED_SKIP;
110 
111             /* Either we hook this new ED to the start list for further processing
112                or we hook it to the 2 successive entries in the previous list.  */
113             if (list_index == 0)
114             {
115 
116                 ed_start_list[current_list_entry] =  ed;
117             }
118             else
119             {
120 
121                 ed_list[current_list_entry * 2] -> ux_ohci_ed_next_ed =        _ux_utility_physical_address(ed);
122                 ed_list[(current_list_entry * 2) + 1] -> ux_ohci_ed_next_ed =  _ux_utility_physical_address(ed);
123             }
124 
125             /* Memorize this ED in the local list. We do this operation now, otherwise
126                we would erase the previous list eds.  */
127             ed_list[current_list_entry] =  ed;
128         }
129 
130         /*  Shift the number of entries in the next list by 1 (i.e. divide by 2).  */
131         list_entries =  list_entries >> 1;
132     }
133 
134     /* The tree has been completed but the entries in the HCCA are in the wrong order.
135        We need to swap each entry according to the OHCI specified entry order list
136        so that we have a fair interval frequency for each periodic ED. The primary eds
137        are fetched from the start list, translated into physical addresses and stored
138        into the HCCA.  */
139     for (current_list_entry = 0; current_list_entry < 32; current_list_entry++)
140     {
141 
142         ed =  ed_start_list[_ux_system_host_hcd_periodic_tree_entries[current_list_entry]];
143         ohci_hcca -> ux_hcd_ohci_hcca_ed[current_list_entry] =  _ux_utility_physical_address(ed);
144     }
145 
146     /* Return successful completion.  */
147     return(UX_SUCCESS);
148 }
149 
150