blob: cbe0c724fb6e412eb898053b2e5f494c27b26b90 [file] [log] [blame]
Angel Ponsc74dae92020-04-02 23:48:16 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Yinghai Lu304f24c2005-07-08 02:56:47 +00002
3#include <console/console.h>
4#include <device/device.h>
5#include <device/pci.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02006#include <device/pci_ops.h>
Yinghai Lu304f24c2005-07-08 02:56:47 +00007#include <device/cardbus.h>
8
Uwe Hermannd453dd02010-10-18 00:00:57 +00009/*
10 * I don't think this code is quite correct but it is close.
Yinghai Lu304f24c2005-07-08 02:56:47 +000011 * Anyone with a cardbus bridge and a little time should be able
12 * to make it usable quickly. -- Eric Biederman 24 March 2005
13 */
14
15/*
Uwe Hermannd453dd02010-10-18 00:00:57 +000016 * IO should be max 256 bytes. However, since we may have a P2P bridge below
17 * a cardbus bridge, we need 4K.
Yinghai Lu304f24c2005-07-08 02:56:47 +000018 */
Uwe Hermannd453dd02010-10-18 00:00:57 +000019#define CARDBUS_IO_SIZE 4096
20#define CARDBUS_MEM_SIZE (32 * 1024 * 1024)
Yinghai Lu304f24c2005-07-08 02:56:47 +000021
Elyes HAOUASe18cbea2018-05-02 21:20:59 +020022static void cardbus_record_bridge_resource(struct device *dev, resource_t moving,
Uwe Hermannd453dd02010-10-18 00:00:57 +000023 resource_t min_size, unsigned int index, unsigned long type)
Yinghai Lu304f24c2005-07-08 02:56:47 +000024{
Yinghai Lu304f24c2005-07-08 02:56:47 +000025 struct resource *resource;
Uwe Hermanne4870472010-11-04 23:23:47 +000026 unsigned long gran;
27 resource_t step;
Uwe Hermannd453dd02010-10-18 00:00:57 +000028
29 /* Initialize the constraints on the current bus. */
Myles Watson03adcfd2010-06-07 16:51:11 +000030 resource = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +000031 if (!moving)
32 return;
Uwe Hermannd453dd02010-10-18 00:00:57 +000033
Uwe Hermanne4870472010-11-04 23:23:47 +000034 resource = new_resource(dev, index);
35 resource->size = 0;
36 gran = 0;
37 step = 1;
38 while ((moving & step) == 0) {
39 gran += 1;
40 step <<= 1;
Yinghai Lu304f24c2005-07-08 02:56:47 +000041 }
Uwe Hermanne4870472010-11-04 23:23:47 +000042 resource->gran = gran;
43 resource->align = gran;
44 resource->limit = moving | (step - 1);
45 resource->flags = type;
46
47 /* Don't let the minimum size exceed what we can put in the resource. */
48 if ((min_size - 1) > resource->limit)
49 min_size = resource->limit + 1;
50
51 resource->size = min_size;
Yinghai Lu304f24c2005-07-08 02:56:47 +000052}
53
Elyes HAOUASe18cbea2018-05-02 21:20:59 +020054static void cardbus_size_bridge_resource(struct device *dev, unsigned int index)
Yinghai Lu304f24c2005-07-08 02:56:47 +000055{
56 struct resource *resource;
57 resource_t min_size;
Uwe Hermannd453dd02010-10-18 00:00:57 +000058
Yinghai Lu304f24c2005-07-08 02:56:47 +000059 resource = find_resource(dev, index);
60 if (resource) {
61 min_size = resource->size;
Uwe Hermannd453dd02010-10-18 00:00:57 +000062 /*
Elyes HAOUAS394ec022018-08-07 12:23:43 +020063 * Always allocate at least the minimum size to a
Yinghai Lu304f24c2005-07-08 02:56:47 +000064 * cardbus bridge in case a new card is plugged in.
65 */
Uwe Hermannd453dd02010-10-18 00:00:57 +000066 if (resource->size < min_size)
Yinghai Lu304f24c2005-07-08 02:56:47 +000067 resource->size = min_size;
Yinghai Lu304f24c2005-07-08 02:56:47 +000068 }
69}
70
Elyes HAOUASe18cbea2018-05-02 21:20:59 +020071void cardbus_read_resources(struct device *dev)
Yinghai Lu304f24c2005-07-08 02:56:47 +000072{
73 resource_t moving_base, moving_limit, moving;
74 unsigned long type;
Uwe Hermannd453dd02010-10-18 00:00:57 +000075 u16 ctl;
Ronald G. Minnich43225bc2005-11-22 00:07:02 +000076
Uwe Hermannd453dd02010-10-18 00:00:57 +000077 /* See if needs a card control registers base address. */
Ronald G. Minnich43225bc2005-11-22 00:07:02 +000078
79 pci_get_resource(dev, PCI_BASE_ADDRESS_0);
80
81 compact_resources(dev);
82
Uwe Hermannd453dd02010-10-18 00:00:57 +000083 /* See which bridge I/O resources are implemented. */
84 moving_base = pci_moving_config32(dev, PCI_CB_IO_BASE_0);
Yinghai Lu304f24c2005-07-08 02:56:47 +000085 moving_limit = pci_moving_config32(dev, PCI_CB_IO_LIMIT_0);
86 moving = moving_base & moving_limit;
87
Uwe Hermannd453dd02010-10-18 00:00:57 +000088 /* Initialize the I/O space constraints on the current bus. */
Yinghai Lu304f24c2005-07-08 02:56:47 +000089 cardbus_record_bridge_resource(dev, moving, CARDBUS_IO_SIZE,
Uwe Hermannd453dd02010-10-18 00:00:57 +000090 PCI_CB_IO_BASE_0, IORESOURCE_IO);
Yinghai Lu304f24c2005-07-08 02:56:47 +000091 cardbus_size_bridge_resource(dev, PCI_CB_IO_BASE_0);
92
Uwe Hermannd453dd02010-10-18 00:00:57 +000093 /* See which bridge I/O resources are implemented. */
94 moving_base = pci_moving_config32(dev, PCI_CB_IO_BASE_1);
Yinghai Lu304f24c2005-07-08 02:56:47 +000095 moving_limit = pci_moving_config32(dev, PCI_CB_IO_LIMIT_1);
96 moving = moving_base & moving_limit;
97
Uwe Hermannd453dd02010-10-18 00:00:57 +000098 /* Initialize the I/O space constraints on the current bus. */
Yinghai Lu304f24c2005-07-08 02:56:47 +000099 cardbus_record_bridge_resource(dev, moving, CARDBUS_IO_SIZE,
Uwe Hermannd453dd02010-10-18 00:00:57 +0000100 PCI_CB_IO_BASE_1, IORESOURCE_IO);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000101
Uwe Hermannd453dd02010-10-18 00:00:57 +0000102 /* If I can, enable prefetch for mem0. */
Yinghai Lu304f24c2005-07-08 02:56:47 +0000103 ctl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL);
104 ctl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
105 ctl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
106 ctl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
107 pci_write_config16(dev, PCI_CB_BRIDGE_CONTROL, ctl);
108 ctl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL);
109
Uwe Hermannd453dd02010-10-18 00:00:57 +0000110 /* See which bridge memory resources are implemented. */
111 moving_base = pci_moving_config32(dev, PCI_CB_MEMORY_BASE_0);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000112 moving_limit = pci_moving_config32(dev, PCI_CB_MEMORY_LIMIT_0);
113 moving = moving_base & moving_limit;
114
Uwe Hermannd453dd02010-10-18 00:00:57 +0000115 /* Initialize the memory space constraints on the current bus. */
Yinghai Lu304f24c2005-07-08 02:56:47 +0000116 type = IORESOURCE_MEM;
Uwe Hermannd453dd02010-10-18 00:00:57 +0000117 if (ctl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)
Yinghai Lu304f24c2005-07-08 02:56:47 +0000118 type |= IORESOURCE_PREFETCH;
Yinghai Lu304f24c2005-07-08 02:56:47 +0000119 cardbus_record_bridge_resource(dev, moving, CARDBUS_MEM_SIZE,
Uwe Hermannd453dd02010-10-18 00:00:57 +0000120 PCI_CB_MEMORY_BASE_0, type);
121 if (type & IORESOURCE_PREFETCH)
Yinghai Lu304f24c2005-07-08 02:56:47 +0000122 cardbus_size_bridge_resource(dev, PCI_CB_MEMORY_BASE_0);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000123
Uwe Hermannd453dd02010-10-18 00:00:57 +0000124 /* See which bridge memory resources are implemented. */
125 moving_base = pci_moving_config32(dev, PCI_CB_MEMORY_BASE_1);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000126 moving_limit = pci_moving_config32(dev, PCI_CB_MEMORY_LIMIT_1);
127 moving = moving_base & moving_limit;
128
Uwe Hermannd453dd02010-10-18 00:00:57 +0000129 /* Initialize the memory space constraints on the current bus. */
Yinghai Lu304f24c2005-07-08 02:56:47 +0000130 cardbus_record_bridge_resource(dev, moving, CARDBUS_MEM_SIZE,
Uwe Hermannd453dd02010-10-18 00:00:57 +0000131 PCI_CB_MEMORY_BASE_1, IORESOURCE_MEM);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000132 cardbus_size_bridge_resource(dev, PCI_CB_MEMORY_BASE_1);
133
134 compact_resources(dev);
135}
136
Elyes HAOUASe18cbea2018-05-02 21:20:59 +0200137void cardbus_enable_resources(struct device *dev)
Yinghai Lu304f24c2005-07-08 02:56:47 +0000138{
Uwe Hermannd453dd02010-10-18 00:00:57 +0000139 u16 ctrl;
140
Yinghai Lu304f24c2005-07-08 02:56:47 +0000141 ctrl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL);
Myles Watson894a3472010-06-09 22:41:35 +0000142 ctrl |= (dev->link_list->bridge_ctrl & (
Yinghai Lu304f24c2005-07-08 02:56:47 +0000143 PCI_BRIDGE_CTL_NO_ISA |
144 PCI_BRIDGE_CTL_VGA |
145 PCI_BRIDGE_CTL_MASTER_ABORT |
146 PCI_BRIDGE_CTL_BUS_RESET));
Uwe Hermannd453dd02010-10-18 00:00:57 +0000147 /* Error check */
Kyösti Mälkki382e2162019-09-21 16:19:32 +0300148 ctrl |= (PCI_CB_BRIDGE_CTL_PARITY | PCI_CB_BRIDGE_CTL_SERR);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000149 printk(BIOS_DEBUG, "%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
Kyösti Mälkki0bca0502019-09-21 16:21:47 +0300150 pci_write_config16(dev, PCI_CB_BRIDGE_CONTROL, ctrl);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000151
152 pci_dev_enable_resources(dev);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000153}
154
Yinghai Lu304f24c2005-07-08 02:56:47 +0000155struct device_operations default_cardbus_ops_bus = {
156 .read_resources = cardbus_read_resources,
157 .set_resources = pci_dev_set_resources,
158 .enable_resources = cardbus_enable_resources,
Uwe Hermannd453dd02010-10-18 00:00:57 +0000159 .scan_bus = pci_scan_bridge,
Yinghai Lu304f24c2005-07-08 02:56:47 +0000160 .reset_bus = pci_bus_reset,
161};