diff mbox

[U-Boot,02/23] pci: Add a function to find a device by class

Message ID 1422321801-6743-3-git-send-email-sjg@chromium.org
State Superseded
Delegated to: Simon Glass
Headers show

Commit Message

Simon Glass Jan. 27, 2015, 1:23 a.m. UTC
There is an existing function prototype in the header file but it is not
implemented. Implement something similar.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 drivers/pci/pci.c | 38 ++++++++++++++++++++++++++++++++++++++
 include/pci.h     |  3 +--
 2 files changed, 39 insertions(+), 2 deletions(-)

Comments

Bin Meng Jan. 27, 2015, 9:53 a.m. UTC | #1
Hi Simon,

On Tue, Jan 27, 2015 at 9:23 AM, Simon Glass <sjg@chromium.org> wrote:
> There is an existing function prototype in the header file but it is not
> implemented. Implement something similar.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
>  drivers/pci/pci.c | 38 ++++++++++++++++++++++++++++++++++++++
>  include/pci.h     |  3 +--
>  2 files changed, 39 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
> index 950a247..215a092 100644
> --- a/drivers/pci/pci.c
> +++ b/drivers/pci/pci.c
> @@ -15,6 +15,7 @@
>  #include <common.h>
>
>  #include <command.h>
> +#include <errno.h>
>  #include <asm/processor.h>
>  #include <asm/io.h>
>  #include <pci.h>
> @@ -236,6 +237,43 @@ pci_dev_t pci_find_devices(struct pci_device_id *ids, int index)
>         return -1;
>  }
>
> +pci_dev_t pci_find_class(uint find_class, int index)
> +{
> +       int bus;
> +       int devnum;
> +       pci_dev_t bdf;
> +       uint32_t class;
> +
> +       for (bus = 0; bus <= pci_last_busno(); bus++) {
> +               for (devnum = 0; devnum < PCI_MAX_PCI_DEVICES-1; devnum++) {

space after PCI_MAX_PCI_DEVICES and before 1?

> +                       pci_read_config_dword(PCI_BDF(bus, devnum, 0),
> +                                             PCI_CLASS_REVISION, &class);
> +                       if (class >> 16 == 0xffff)
> +                               continue;
> +
> +                       for (bdf = PCI_BDF(bus, devnum, 0);
> +                                       bdf <= PCI_BDF(bus, devnum,
> +                                               PCI_MAX_PCI_FUNCTIONS - 1);
> +                                       bdf += PCI_BDF(0, 0, 1)) {
> +                               pci_read_config_dword(bdf, PCI_CLASS_REVISION,
> +                                                     &class);
> +                               class >>= 8;
> +
> +                               if (class != find_class)
> +                                       continue;
> +                               if (index) {
> +                                       index--;
> +                                       continue;

It is worth to have a comment block to explain the 'index' logic here.

> +                               }
> +                               /* Return index'th controller. */
> +                               return bdf;
> +                       }
> +               }
> +       }
> +
> +       return -ENODEV;
> +}
> +
>  pci_dev_t pci_find_device(unsigned int vendor, unsigned int device, int index)
>  {
>         struct pci_device_id ids[2] = { {}, {0, 0} };
> diff --git a/include/pci.h b/include/pci.h
> index 4fbb8f6..004a048 100644
> --- a/include/pci.h
> +++ b/include/pci.h
> @@ -644,8 +644,7 @@ extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev);
>
>  extern pci_dev_t pci_find_device (unsigned int vendor, unsigned int device, int index);
>  extern pci_dev_t pci_find_devices (struct pci_device_id *ids, int index);
> -extern pci_dev_t pci_find_class(int wanted_class, int wanted_sub_code,
> -                               int wanted_prog_if, int index);
> +pci_dev_t pci_find_class(unsigned int find_class, int index);
>
>  extern int pci_hose_config_device(struct pci_controller *hose,
>                                   pci_dev_t dev,
> --

Regards,
Bin
diff mbox

Patch

diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 950a247..215a092 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -15,6 +15,7 @@ 
 #include <common.h>
 
 #include <command.h>
+#include <errno.h>
 #include <asm/processor.h>
 #include <asm/io.h>
 #include <pci.h>
@@ -236,6 +237,43 @@  pci_dev_t pci_find_devices(struct pci_device_id *ids, int index)
 	return -1;
 }
 
+pci_dev_t pci_find_class(uint find_class, int index)
+{
+	int bus;
+	int devnum;
+	pci_dev_t bdf;
+	uint32_t class;
+
+	for (bus = 0; bus <= pci_last_busno(); bus++) {
+		for (devnum = 0; devnum < PCI_MAX_PCI_DEVICES-1; devnum++) {
+			pci_read_config_dword(PCI_BDF(bus, devnum, 0),
+					      PCI_CLASS_REVISION, &class);
+			if (class >> 16 == 0xffff)
+				continue;
+
+			for (bdf = PCI_BDF(bus, devnum, 0);
+					bdf <= PCI_BDF(bus, devnum,
+						PCI_MAX_PCI_FUNCTIONS - 1);
+					bdf += PCI_BDF(0, 0, 1)) {
+				pci_read_config_dword(bdf, PCI_CLASS_REVISION,
+						      &class);
+				class >>= 8;
+
+				if (class != find_class)
+					continue;
+				if (index) {
+					index--;
+					continue;
+				}
+				/* Return index'th controller. */
+				return bdf;
+			}
+		}
+	}
+
+	return -ENODEV;
+}
+
 pci_dev_t pci_find_device(unsigned int vendor, unsigned int device, int index)
 {
 	struct pci_device_id ids[2] = { {}, {0, 0} };
diff --git a/include/pci.h b/include/pci.h
index 4fbb8f6..004a048 100644
--- a/include/pci.h
+++ b/include/pci.h
@@ -644,8 +644,7 @@  extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev);
 
 extern pci_dev_t pci_find_device (unsigned int vendor, unsigned int device, int index);
 extern pci_dev_t pci_find_devices (struct pci_device_id *ids, int index);
-extern pci_dev_t pci_find_class(int wanted_class, int wanted_sub_code,
-				int wanted_prog_if, int index);
+pci_dev_t pci_find_class(unsigned int find_class, int index);
 
 extern int pci_hose_config_device(struct pci_controller *hose,
 				  pci_dev_t dev,