Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:20

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*  PCI Access Library
0004  *
0005  *  COPYRIGHT (c) 2010 Cobham Gaisler AB.
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, this list of conditions and the following disclaimer.
0012  * 2. Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0026  * POSSIBILITY OF SUCH DAMAGE.
0027  */
0028 
0029 #include <pci.h>
0030 
0031 /* Get PCI I/O or Configuration space access function */
0032 static int pci_ioc_func(int wr, int size, void **func, void **ops)
0033 {
0034     int ofs;
0035 
0036     ofs = 0;
0037     if (wr)
0038         ofs += 3;
0039     if (size == 4)
0040         size = 3;
0041     ofs += (size & 0x3) - 1;
0042     if (ops[ofs] == NULL)
0043         return -1;
0044     if (func)
0045         *func = ops[ofs];
0046     return 0;
0047 }
0048 
0049 /* Get Registers-over-Memory Space access function */
0050 static int pci_memreg_func(int wr, int size, void **func, int endian)
0051 {
0052     void **ops;
0053     int ofs = 0;
0054 
0055     ops = (void **)pci_access_ops.memreg;
0056     if (!ops)
0057         return -1;
0058 
0059     if (size == 2)
0060         ofs += 2;
0061     else if (size == 4)
0062         ofs += 6;
0063 
0064     if (size != 1 && endian == PCI_BIG_ENDIAN)
0065         ofs += 2;
0066 
0067     if (wr)
0068         ofs += 1;
0069 
0070     if (ops[ofs] == NULL)
0071         return -1;
0072     if (func)
0073         *func = ops[ofs];
0074     return 0;
0075 }
0076 
0077 /* Get function pointer from Host/BSP driver definitions */
0078 int pci_access_func(int wr, int size, void **func, int endian, int type)
0079 {
0080     switch (type) {
0081     default:
0082     case 2: /* Memory Space - not implemented */
0083         return -1;
0084     case 1: /* I/O space */
0085         return pci_ioc_func(wr, size, func, (void**)&pci_access_ops.cfg);
0086     case 3: /* Registers over Memory space */
0087         return pci_memreg_func(wr, size, func, endian);
0088     case 4: /* Configuration space */
0089         return pci_ioc_func(wr, size, func, (void**)&pci_access_ops.io);
0090     }
0091 }