Merge tag 'u-boot-imx-20191009' of https://gitlab.denx.de/u-boot/custodians/u-boot-imx
[oweals/u-boot.git] / drivers / pci / pci_auto_common.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * PCI auto-configuration library
4  *
5  * Author: Matt Porter <mporter@mvista.com>
6  *
7  * Copyright 2000 MontaVista Software Inc.
8  *
9  * Modifications for driver model:
10  * Copyright 2015 Google, Inc
11  * Written by Simon Glass <sjg@chromium.org>
12  */
13
14 #include <common.h>
15 #include <dm.h>
16 #include <errno.h>
17 #include <pci.h>
18
19 void pciauto_region_init(struct pci_region *res)
20 {
21         /*
22          * Avoid allocating PCI resources from address 0 -- this is illegal
23          * according to PCI 2.1 and moreover, this is known to cause Linux IDE
24          * drivers to fail. Use a reasonable starting value of 0x1000 instead
25          * if the bus start address is below 0x1000.
26          */
27         res->bus_lower = res->bus_start < 0x1000 ? 0x1000 : res->bus_start;
28 }
29
30 void pciauto_region_align(struct pci_region *res, pci_size_t size)
31 {
32         res->bus_lower = ((res->bus_lower - 1) | (size - 1)) + 1;
33 }
34
35 int pciauto_region_allocate(struct pci_region *res, pci_size_t size,
36         pci_addr_t *bar, bool supports_64bit)
37 {
38         pci_addr_t addr;
39
40         if (!res) {
41                 debug("No resource\n");
42                 goto error;
43         }
44
45         addr = ((res->bus_lower - 1) | (size - 1)) + 1;
46
47         if (addr - res->bus_start + size > res->size) {
48                 debug("No room in resource, avail start=%llx / size=%llx, "
49                       "need=%llx\n", (unsigned long long)res->bus_lower,
50                       (unsigned long long)res->size, (unsigned long long)size);
51                 goto error;
52         }
53
54         if (upper_32_bits(addr) && !supports_64bit) {
55                 debug("Cannot assign 64-bit address to 32-bit-only resource\n");
56                 goto error;
57         }
58
59         res->bus_lower = addr + size;
60
61         debug("address=0x%llx bus_lower=0x%llx\n", (unsigned long long)addr,
62               (unsigned long long)res->bus_lower);
63
64         *bar = addr;
65         return 0;
66
67  error:
68         *bar = (pci_addr_t)-1;
69         return -1;
70 }
71
72 static void pciauto_show_region(const char *name, struct pci_region *region)
73 {
74         pciauto_region_init(region);
75         debug("PCI Autoconfig: Bus %s region: [%llx-%llx],\n"
76               "\t\tPhysical Memory [%llx-%llxx]\n", name,
77               (unsigned long long)region->bus_start,
78               (unsigned long long)(region->bus_start + region->size - 1),
79               (unsigned long long)region->phys_start,
80               (unsigned long long)(region->phys_start + region->size - 1));
81 }
82
83 void pciauto_config_init(struct pci_controller *hose)
84 {
85         int i;
86
87         hose->pci_io = NULL;
88         hose->pci_mem = NULL;
89         hose->pci_prefetch = NULL;
90
91         for (i = 0; i < hose->region_count; i++) {
92                 switch (hose->regions[i].flags) {
93                 case PCI_REGION_IO:
94                         if (!hose->pci_io ||
95                             hose->pci_io->size < hose->regions[i].size)
96                                 hose->pci_io = hose->regions + i;
97                         break;
98                 case PCI_REGION_MEM:
99                         if (!hose->pci_mem ||
100                             hose->pci_mem->size < hose->regions[i].size)
101                                 hose->pci_mem = hose->regions + i;
102                         break;
103                 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
104                         if (!hose->pci_prefetch ||
105                             hose->pci_prefetch->size < hose->regions[i].size)
106                                 hose->pci_prefetch = hose->regions + i;
107                         break;
108                 }
109         }
110
111
112         if (hose->pci_mem)
113                 pciauto_show_region("Memory", hose->pci_mem);
114         if (hose->pci_prefetch)
115                 pciauto_show_region("Prefetchable Mem", hose->pci_prefetch);
116         if (hose->pci_io)
117                 pciauto_show_region("I/O", hose->pci_io);
118 }