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 /**   Utility                                                             */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /* Include necessary system files.  */
25 
26 #define UX_SOURCE_CODE
27 
28 #include "ux_api.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_utility_pci_read                                PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function reads a 32/16/8 bit value from a specific PCI bus     */
44 /*    at a certain offset.                                                */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    bus_number                            PCI bus number                */
49 /*    device_number                         Device number                 */
50 /*    function_number                       Function number               */
51 /*    offset                                Offset                        */
52 /*    read_size                             Size of read                  */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    32-bit value                                                        */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    inpl                                  PCI input long                */
61 /*    inpw                                  PCI input word                */
62 /*    inpb                                  PCI input byte                */
63 /*    outpl                                 PCI output function           */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    USBX Components                                                     */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
74 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            resulting in version 6.1    */
76 /*                                                                        */
77 /**************************************************************************/
_ux_utility_pci_read(ULONG bus_number,ULONG device_number,ULONG function_number,ULONG offset,UINT read_size)78 ULONG  _ux_utility_pci_read(ULONG bus_number, ULONG device_number, ULONG function_number,
79                                         ULONG offset, UINT read_size)
80 {
81 
82 ULONG   destination_address;
83 ULONG   cfg_ctrl;
84 
85 
86     /* Calculate the destination address.  */
87     destination_address =  (((bus_number << 16) & 0x00ff0000) | ((device_number << 11) & 0x0000f800) |
88                                 ((function_number << 8) & 0x00000700));
89 
90     /* Calculate the configure control value.  */
91     cfg_ctrl = destination_address | offset | 0x80000000;
92 
93     /* Read based on the size requested.  */
94     switch(read_size)
95     {
96 
97     case 32:
98 
99         /* Write the address we need to read from.  */
100         outpl(UX_PCI_CFG_CTRL_ADDRESS, cfg_ctrl);
101 
102         /* Return the 32 bit content of this address.  */
103         return(inpl(UX_PCI_CFG_DATA_ADDRESS));
104 
105     case 16:
106 
107         /* Write the address we need to read from.  */
108         outpl(UX_PCI_CFG_CTRL_ADDRESS, cfg_ctrl);
109 
110         /* Return the 16 bit content of this address.  */
111         return((USHORT)(inpw(UX_PCI_CFG_DATA_ADDRESS)));
112 
113     case 8:
114 
115         /* Write the address we need to read from.  */
116         outpl(UX_PCI_CFG_CTRL_ADDRESS, cfg_ctrl);
117 
118         /* Return the 8 bit content of this address */
119         return((ULONG)(inpb(UX_PCI_CFG_DATA_ADDRESS)));
120 
121     default:
122 
123         return(0);
124     }
125 }
126