source: rtems/bsps/i386/shared/pci/pci_io.c

Last change on this file was c1b1c0fa, checked in by Joel Sherrill <joel@…>, on 07/08/22 at 13:46:14

bsps/i386/shared/pci/pci_io.c:Change license to BSD-2

Updates #3053.

  • Property mode set to 100644
File size: 5.9 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/**
4 * @file
5 *
6 * PCI Support when Configuration Space is in I/O
7 */
8
9/*
10 *  COPYRIGHT (c) 2016.
11 *  On-Line Applications Research Corporation (OAR).
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#include <rtems.h>
36#include <bsp.h>
37#include <bsp/bspimpl.h>
38
39static int pci_io_initialized = 0;
40
41/*
42 * Forward reference. Initialized at bottom.
43 */
44static const pci_config_access_functions pci_io_indirect_functions;
45
46/*
47 * Detects presense of PCI Configuration is in I/O space. If so, return
48 * pointer to accessor methods.
49 *
50 * NOTE: TBD to determine if (a) PCI Bus exists and (b) this is the
51 *       access method.
52 */
53const pci_config_access_functions *pci_io_initialize(void)
54{
55  pci_io_initialized = 1;
56
57  printk( "PCI I/O Support Initialized\n" );
58
59  return &pci_io_indirect_functions;
60}
61
62/*
63 * Build PCI Address
64 */
65static inline uint32_t pci_io_build_address(
66  uint16_t  bus,
67  uint16_t  slot,
68  uint16_t  function,
69  uint16_t  offset
70)
71{
72  uint32_t bus_u32      = (uint32_t)bus;
73  uint32_t slot_u32     = (uint32_t)slot;
74  uint32_t function_u32 = (uint32_t)function;
75  uint32_t address;
76
77  /*
78   * create configuration address as per figure at
79   *   http://wiki.osdev.org/PCI#Configuration_Space_Access_Mechanism_.231
80   */
81  address  = (uint32_t) 0x80000000;  /* Bit  31    - Enable Bit */
82                                     /* Bits 30-24 - Reserved */
83  address |= bus_u32 << 16;          /* Bits 23-16 - Bus Number */
84  address |= slot_u32 << 11;         /* Bits 15-11 - Device/Slot Number */
85  address |= function_u32 << 8;      /* Bits 10-8  - Function Number */
86  address |= offset & 0xfc;          /* Bits 7-2   - Offset/Register Number */
87                                     /* Bits 1-0   - Reserved 0 */
88  return address;
89}
90
91static int BSP_pci_read_config_byte(
92  unsigned char bus,
93  unsigned char slot,
94  unsigned char function,
95  unsigned char offset,
96  unsigned char *value
97)
98{
99  uint32_t address;
100  uint32_t tmp;
101
102  address = pci_io_build_address( bus, slot, function, offset );
103
104  /* write out the address */
105  outport_long(0xCF8, address);
106
107  /* read in the data */
108  inport_long(0xCFC, tmp);
109
110  /* (offset & 3) * 8) = 0 will choose the first byte of the 32 bits register */
111  *value = (uint16_t)(tmp >> ((offset & 3) * 8)) & 0xff;
112  return PCIBIOS_SUCCESSFUL;
113}
114
115static int BSP_pci_read_config_word(
116  unsigned char bus,
117  unsigned char slot,
118  unsigned char function,
119  unsigned char offset,
120  unsigned short *value
121)
122{
123  uint32_t address;
124  uint32_t tmp;
125
126  address = pci_io_build_address( bus, slot, function, offset );
127
128  /* write out the address */
129  outport_long(0xCF8, address);
130
131  /* read in the data */
132  inport_long(0xCFC, tmp);
133
134  /* (offset & 2) * 8) = 0 will choose the first word of the 32 bits register */
135  *value = (uint16_t)(tmp >> ((offset & 2) * 8)) & 0xffff;
136  return PCIBIOS_SUCCESSFUL;
137}
138
139static int BSP_pci_read_config_dword(
140  unsigned char bus,
141  unsigned char slot,
142  unsigned char function,
143  unsigned char offset,
144  uint32_t     *value
145)
146{
147  uint32_t address;
148  uint32_t tmp;
149
150  address = pci_io_build_address( bus, slot, function, offset );
151
152  /* write out the address */
153  outport_long(0xCF8, address);
154
155  /* read in the data */
156  inport_long(0xCFC, tmp);
157
158  *value = tmp;
159  return PCIBIOS_SUCCESSFUL;
160}
161
162static int BSP_pci_write_config_byte(
163  unsigned char bus,
164  unsigned char slot,
165  unsigned char function,
166  unsigned char offset,
167  unsigned char value
168)
169{
170  uint32_t address;
171
172  address = pci_io_build_address( bus, slot, function, offset );
173
174  /* write out the address */
175  outport_long(0xCF8, address);
176
177  /* read in the data */
178  outport_byte(0xCFC, value);
179
180  return PCIBIOS_SUCCESSFUL;
181}
182
183static int BSP_pci_write_config_word(
184  unsigned char bus,
185  unsigned char slot,
186  unsigned char function,
187  unsigned char offset,
188  unsigned short value
189)
190{
191  uint32_t address;
192
193  address = pci_io_build_address( bus, slot, function, offset );
194
195  /* write out the address */
196  outport_long(0xCF8, address);
197
198  /* read in the data */
199  outport_word(0xCFC, value);
200
201  return PCIBIOS_SUCCESSFUL;
202}
203
204static int BSP_pci_write_config_dword(
205  unsigned char bus,
206  unsigned char slot,
207  unsigned char function,
208  unsigned char offset,
209  uint32_t      value
210)
211{
212  uint32_t address;
213
214  address = pci_io_build_address( bus, slot, function, offset );
215
216  /* write out the address */
217  outport_long(0xCF8, address);
218
219  /* read in the data */
220  outport_long(0xCFC, value);
221
222  return PCIBIOS_SUCCESSFUL;
223}
224
225static const pci_config_access_functions pci_io_indirect_functions = {
226  BSP_pci_read_config_byte,
227  BSP_pci_read_config_word,
228  BSP_pci_read_config_dword,
229  BSP_pci_write_config_byte,
230  BSP_pci_write_config_word,
231  BSP_pci_write_config_dword
232};
Note: See TracBrowser for help on using the repository browser.