1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /* PCI Access Library |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 2010 Cobham Gaisler AB. |
---|
6 | * |
---|
7 | * Redistribution and use in source and binary forms, with or without |
---|
8 | * modification, are permitted provided that the following conditions |
---|
9 | * are met: |
---|
10 | * 1. Redistributions of source code must retain the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer. |
---|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer in the |
---|
14 | * documentation and/or other materials provided with the distribution. |
---|
15 | * |
---|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
26 | * POSSIBILITY OF SUCH DAMAGE. |
---|
27 | */ |
---|
28 | |
---|
29 | #include <pci.h> |
---|
30 | |
---|
31 | /* Get PCI I/O or Configuration space access function */ |
---|
32 | static int pci_ioc_func(int wr, int size, void **func, void **ops) |
---|
33 | { |
---|
34 | int ofs; |
---|
35 | |
---|
36 | ofs = 0; |
---|
37 | if (wr) |
---|
38 | ofs += 3; |
---|
39 | if (size == 4) |
---|
40 | size = 3; |
---|
41 | ofs += (size & 0x3) - 1; |
---|
42 | if (ops[ofs] == NULL) |
---|
43 | return -1; |
---|
44 | if (func) |
---|
45 | *func = ops[ofs]; |
---|
46 | return 0; |
---|
47 | } |
---|
48 | |
---|
49 | /* Get Registers-over-Memory Space access function */ |
---|
50 | static int pci_memreg_func(int wr, int size, void **func, int endian) |
---|
51 | { |
---|
52 | void **ops; |
---|
53 | int ofs = 0; |
---|
54 | |
---|
55 | ops = (void **)pci_access_ops.memreg; |
---|
56 | if (!ops) |
---|
57 | return -1; |
---|
58 | |
---|
59 | if (size == 2) |
---|
60 | ofs += 2; |
---|
61 | else if (size == 4) |
---|
62 | ofs += 6; |
---|
63 | |
---|
64 | if (size != 1 && endian == PCI_BIG_ENDIAN) |
---|
65 | ofs += 2; |
---|
66 | |
---|
67 | if (wr) |
---|
68 | ofs += 1; |
---|
69 | |
---|
70 | if (ops[ofs] == NULL) |
---|
71 | return -1; |
---|
72 | if (func) |
---|
73 | *func = ops[ofs]; |
---|
74 | return 0; |
---|
75 | } |
---|
76 | |
---|
77 | /* Get function pointer from Host/BSP driver definitions */ |
---|
78 | int pci_access_func(int wr, int size, void **func, int endian, int type) |
---|
79 | { |
---|
80 | switch (type) { |
---|
81 | default: |
---|
82 | case 2: /* Memory Space - not implemented */ |
---|
83 | return -1; |
---|
84 | case 1: /* I/O space */ |
---|
85 | return pci_ioc_func(wr, size, func, (void**)&pci_access_ops.cfg); |
---|
86 | case 3: /* Registers over Memory space */ |
---|
87 | return pci_memreg_func(wr, size, func, endian); |
---|
88 | case 4: /* Configuration space */ |
---|
89 | return pci_ioc_func(wr, size, func, (void**)&pci_access_ops.io); |
---|
90 | } |
---|
91 | } |
---|