mirror of
https://github.com/apache/nuttx.git
synced 2026-05-20 12:33:27 +08:00
Changed the pci bus for arm architecture
Signed-off-by: yangshuyong <yangshuyong@xiaomi.com> Signed-off-by: lipengfei28 <lipengfei28@xiaomi.com>
This commit is contained in:
+11
-1
@@ -8,4 +8,14 @@ menuconfig PCI
|
||||
default n
|
||||
---help---
|
||||
Enables support for the PCI bus.
|
||||
Backend bust be provided by per-arch or per-board implementation.
|
||||
Backend must be provided by per-arch or per-board implementation.
|
||||
|
||||
if PCI
|
||||
|
||||
config PCI_ASSIGN_ALL_BUSES
|
||||
bool "Assign resource to all buses"
|
||||
default !ARCH_X86 && !ARCH_X86_64
|
||||
---help---
|
||||
Assign resources to all buses. This is required for some
|
||||
platforms that have multiple PCI buses.
|
||||
endif
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
|
||||
ifeq ($(CONFIG_PCI),y)
|
||||
|
||||
CSRCS += pci.c
|
||||
CSRCS += pci.c pci_ecam.c
|
||||
|
||||
# Include PCI device driver build support
|
||||
|
||||
|
||||
+936
-595
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,323 @@
|
||||
/****************************************************************************
|
||||
* drivers/pci/pci_ecam.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include <nuttx/kmalloc.h>
|
||||
#include <nuttx/pci/pci.h>
|
||||
#include <nuttx/pci/pci_ecam.h>
|
||||
#include <nuttx/nuttx.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define readb(a) (*(FAR volatile uint8_t *)(a))
|
||||
#define writeb(v,a) (*(FAR volatile uint8_t *)(a) = (v))
|
||||
#define readw(a) (*(FAR volatile uint16_t *)(a))
|
||||
#define writew(v,a) (*(FAR volatile uint16_t *)(a) = (v))
|
||||
#define readl(a) (*(FAR volatile uint32_t *)(a))
|
||||
#define writel(v,a) (*(FAR volatile uint32_t *)(a) = (v))
|
||||
|
||||
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
|
||||
#define IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
static int pci_ecam_read_config(FAR struct pci_bus_s *bus, uint32_t devfn,
|
||||
int where, int size, FAR uint32_t *val);
|
||||
|
||||
static int pci_ecam_write_config(FAR struct pci_bus_s *bus, uint32_t devfn,
|
||||
int where, int size, uint32_t val);
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
****************************************************************************/
|
||||
|
||||
struct pci_ecam_s
|
||||
{
|
||||
struct pci_controller_s ctrl;
|
||||
struct pci_resource_s cfg;
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
static const struct pci_ops_s g_pci_ecam_ops =
|
||||
{
|
||||
.read = pci_ecam_read_config,
|
||||
.write = pci_ecam_write_config,
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: pci_ecam_from_controller
|
||||
*
|
||||
* Description:
|
||||
* To get the ecam type from ctrl type.
|
||||
*
|
||||
* Input Parameters:
|
||||
* ctrl - The pci controller
|
||||
*
|
||||
* Returned Value:
|
||||
* Return the struct pci_ecam_pcie s address
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static inline FAR struct pci_ecam_s *
|
||||
pci_ecam_from_controller(FAR struct pci_controller_s *ctrl)
|
||||
{
|
||||
return container_of(ctrl, struct pci_ecam_s, ctrl);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: pci_ecam_conf_address
|
||||
*
|
||||
* Description:
|
||||
* This function is used to get the specify reg address of a function
|
||||
* configuration space.
|
||||
*
|
||||
* Input Parameters:
|
||||
* bus - PCI bus type private data
|
||||
* devfn - Specify a BDF
|
||||
* where - Which ID in configuration space
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static FAR void *pci_ecam_conf_address(FAR const struct pci_bus_s *bus,
|
||||
uint32_t devfn, int where)
|
||||
{
|
||||
FAR struct pci_ecam_s *ecam = pci_ecam_from_controller(bus->ctrl);
|
||||
FAR void *addr;
|
||||
|
||||
addr = (FAR void *)ecam->cfg.start;
|
||||
addr += bus->number << 20;
|
||||
addr += PCI_SLOT(devfn) << 15;
|
||||
addr += PCI_FUNC(devfn) << 12;
|
||||
addr += where;
|
||||
|
||||
return addr;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: pci_ecam_addr_valid
|
||||
*
|
||||
* Description:
|
||||
* To check the bus number whether or not valid.
|
||||
*
|
||||
* Input Parameters:
|
||||
* bus - The bus private data
|
||||
* devfn - Get a specify dev by devfn
|
||||
*
|
||||
* Returned Value:
|
||||
* True if success, false if failed
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static bool pci_ecam_addr_valid(FAR const struct pci_bus_s *bus,
|
||||
uint32_t devfn)
|
||||
{
|
||||
FAR struct pci_ecam_s *ecam = pci_ecam_from_controller(bus->ctrl);
|
||||
int num_buses = DIV_ROUND_UP(pci_resource_size(&ecam->cfg), 1 << 20);
|
||||
|
||||
return bus->number < num_buses;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: pci_ecam_read_config
|
||||
*
|
||||
* Description:
|
||||
* Read data from the speicfy register.
|
||||
*
|
||||
* Input Parameters:
|
||||
* bus - The bus on this to read reg data
|
||||
* devfn - BDF
|
||||
* where - Which cfg space ID
|
||||
* size - Data size
|
||||
* val - Return value to this var
|
||||
*
|
||||
* Returned Value:
|
||||
* Return the specify enum result of operation
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static int pci_ecam_read_config(FAR struct pci_bus_s *bus, uint32_t devfn,
|
||||
int where, int size, FAR uint32_t *val)
|
||||
{
|
||||
FAR void *addr;
|
||||
|
||||
if (!pci_ecam_addr_valid(bus, devfn))
|
||||
{
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
addr = pci_ecam_conf_address(bus, devfn, where);
|
||||
|
||||
if (!IS_ALIGNED((uintptr_t)addr, size))
|
||||
{
|
||||
*val = 0;
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (size == 4)
|
||||
{
|
||||
*val = readl(addr);
|
||||
}
|
||||
else if (size == 2)
|
||||
{
|
||||
*val = readw(addr);
|
||||
}
|
||||
else if (size == 1)
|
||||
{
|
||||
*val = readb(addr);
|
||||
}
|
||||
else
|
||||
{
|
||||
*val = 0;
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: pci_ecam_write_config
|
||||
*
|
||||
* Description:
|
||||
* Write data into speicfy register.
|
||||
*
|
||||
* Input Parameters:
|
||||
* bus - The specify bus private data
|
||||
* devfn - BDF
|
||||
* where - Which ID in configuration space
|
||||
* size - Data Size
|
||||
* val - The value to be written
|
||||
*
|
||||
* Returned Value:
|
||||
* Return the specify enum result of operation
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static int pci_ecam_write_config(FAR struct pci_bus_s *bus, uint32_t devfn,
|
||||
int where, int size, uint32_t val)
|
||||
{
|
||||
FAR void *addr;
|
||||
|
||||
if (!pci_ecam_addr_valid(bus, devfn))
|
||||
{
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
addr = pci_ecam_conf_address(bus, devfn, where);
|
||||
|
||||
if (!IS_ALIGNED((uintptr_t)addr, size))
|
||||
{
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (size == 4)
|
||||
{
|
||||
writel(val, addr);
|
||||
}
|
||||
else if (size == 2)
|
||||
{
|
||||
writew(val, addr);
|
||||
}
|
||||
else if (size == 1)
|
||||
{
|
||||
writeb(val, addr);
|
||||
}
|
||||
else
|
||||
{
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: pci_ecam_register
|
||||
*
|
||||
* Description:
|
||||
* This function is used to register an ecam driver for pci.
|
||||
*
|
||||
* Input Parameters:
|
||||
* cfg - Configuration space data
|
||||
* io - I/O space data
|
||||
* mem - No-prefetchable space data
|
||||
* mem_pref - Prefetchable space data
|
||||
*
|
||||
* Returned Value:
|
||||
* Return 0 if success, nageative if failed
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int pci_ecam_register(FAR const struct pci_resource_s *cfg,
|
||||
FAR const struct pci_resource_s *io,
|
||||
FAR const struct pci_resource_s *mem,
|
||||
FAR const struct pci_resource_s *mem_pref)
|
||||
{
|
||||
FAR struct pci_ecam_s *ecam;
|
||||
|
||||
if (cfg == NULL || (io == NULL && mem == NULL && mem_pref == NULL))
|
||||
{
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
ecam = kmm_zalloc(sizeof(*ecam));
|
||||
if (ecam == NULL)
|
||||
{
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
memcpy(&ecam->cfg, cfg, sizeof(*cfg));
|
||||
|
||||
if (io != NULL)
|
||||
{
|
||||
memcpy(&ecam->ctrl.io, io, sizeof(*io));
|
||||
}
|
||||
|
||||
if (mem != NULL)
|
||||
{
|
||||
memcpy(&ecam->ctrl.mem, mem, sizeof(*mem));
|
||||
}
|
||||
|
||||
if (mem_pref != NULL)
|
||||
{
|
||||
memcpy(&ecam->ctrl.mem_pref, mem_pref, sizeof(*mem_pref));
|
||||
}
|
||||
|
||||
ecam->ctrl.ops = &g_pci_ecam_ops;
|
||||
return pci_register_controller(&ecam->ctrl);
|
||||
}
|
||||
+433
-290
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,56 @@
|
||||
/****************************************************************************
|
||||
* include/nuttx/pci/pci_ecam.h
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __INCLUDE_NUTTX_PCI_PCI_ECAM_H
|
||||
#define __INCLUDE_NUTTX_PCI_PCI_ECAM_H
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/pci/pci.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: pci_ecam_register
|
||||
*
|
||||
* Description:
|
||||
* This function is used to register an ecam driver for pci.
|
||||
*
|
||||
* Input Parameters:
|
||||
* cfg - Configuration space data
|
||||
* io - I/O space data
|
||||
* mem - No-prefetchable space data
|
||||
* mem_pref - Prefetchable space data
|
||||
*
|
||||
* Returned Value:
|
||||
* Return 0 if success, nageative if failed
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int pci_ecam_register(FAR const struct pci_resource_s *cfg,
|
||||
FAR const struct pci_resource_s *io,
|
||||
FAR const struct pci_resource_s *mem,
|
||||
FAR const struct pci_resource_s *mem_pref);
|
||||
|
||||
#endif /* __INCLUDE_NUTTX_PCI_PCI_ECAM_H */
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user