blob: 2eb070e7e1ea8018c92929adc7d2bff69413339e [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>
Elyes Haouas04c3b5a2022-10-07 10:08:05 +02008#include <types.h>
Yinghai Lu304f24c2005-07-08 02:56:47 +00009
Uwe Hermannd453dd02010-10-18 00:00:57 +000010/*
11 * I don't think this code is quite correct but it is close.
Yinghai Lu304f24c2005-07-08 02:56:47 +000012 * Anyone with a cardbus bridge and a little time should be able
13 * to make it usable quickly. -- Eric Biederman 24 March 2005
14 */
15
16/*
Uwe Hermannd453dd02010-10-18 00:00:57 +000017 * IO should be max 256 bytes. However, since we may have a P2P bridge below
18 * a cardbus bridge, we need 4K.
Yinghai Lu304f24c2005-07-08 02:56:47 +000019 */
Uwe Hermannd453dd02010-10-18 00:00:57 +000020#define CARDBUS_IO_SIZE 4096
21#define CARDBUS_MEM_SIZE (32 * 1024 * 1024)
Yinghai Lu304f24c2005-07-08 02:56:47 +000022
Elyes HAOUASe18cbea2018-05-02 21:20:59 +020023static void cardbus_record_bridge_resource(struct device *dev, resource_t moving,
Uwe Hermannd453dd02010-10-18 00:00:57 +000024 resource_t min_size, unsigned int index, unsigned long type)
Yinghai Lu304f24c2005-07-08 02:56:47 +000025{
Yinghai Lu304f24c2005-07-08 02:56:47 +000026 struct resource *resource;
Uwe Hermanne4870472010-11-04 23:23:47 +000027 unsigned long gran;
28 resource_t step;
Uwe Hermannd453dd02010-10-18 00:00:57 +000029
30 /* Initialize the constraints on the current bus. */
Myles Watson03adcfd2010-06-07 16:51:11 +000031 resource = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +000032 if (!moving)
33 return;
Uwe Hermannd453dd02010-10-18 00:00:57 +000034
Uwe Hermanne4870472010-11-04 23:23:47 +000035 resource = new_resource(dev, index);
36 resource->size = 0;
37 gran = 0;
38 step = 1;
39 while ((moving & step) == 0) {
40 gran += 1;
41 step <<= 1;
Yinghai Lu304f24c2005-07-08 02:56:47 +000042 }
Uwe Hermanne4870472010-11-04 23:23:47 +000043 resource->gran = gran;
44 resource->align = gran;
45 resource->limit = moving | (step - 1);
46 resource->flags = type;
47
48 /* Don't let the minimum size exceed what we can put in the resource. */
49 if ((min_size - 1) > resource->limit)
50 min_size = resource->limit + 1;
51
52 resource->size = min_size;
Yinghai Lu304f24c2005-07-08 02:56:47 +000053}
54
Elyes HAOUASe18cbea2018-05-02 21:20:59 +020055void cardbus_read_resources(struct device *dev)
Yinghai Lu304f24c2005-07-08 02:56:47 +000056{
57 resource_t moving_base, moving_limit, moving;
58 unsigned long type;
Uwe Hermannd453dd02010-10-18 00:00:57 +000059 u16 ctl;
Ronald G. Minnich43225bc2005-11-22 00:07:02 +000060
Uwe Hermannd453dd02010-10-18 00:00:57 +000061 /* See if needs a card control registers base address. */
Ronald G. Minnich43225bc2005-11-22 00:07:02 +000062
63 pci_get_resource(dev, PCI_BASE_ADDRESS_0);
64
65 compact_resources(dev);
66
Uwe Hermannd453dd02010-10-18 00:00:57 +000067 /* See which bridge I/O resources are implemented. */
68 moving_base = pci_moving_config32(dev, PCI_CB_IO_BASE_0);
Yinghai Lu304f24c2005-07-08 02:56:47 +000069 moving_limit = pci_moving_config32(dev, PCI_CB_IO_LIMIT_0);
70 moving = moving_base & moving_limit;
71
Uwe Hermannd453dd02010-10-18 00:00:57 +000072 /* Initialize the I/O space constraints on the current bus. */
Yinghai Lu304f24c2005-07-08 02:56:47 +000073 cardbus_record_bridge_resource(dev, moving, CARDBUS_IO_SIZE,
Uwe Hermannd453dd02010-10-18 00:00:57 +000074 PCI_CB_IO_BASE_0, IORESOURCE_IO);
Yinghai Lu304f24c2005-07-08 02:56:47 +000075
Uwe Hermannd453dd02010-10-18 00:00:57 +000076 /* See which bridge I/O resources are implemented. */
77 moving_base = pci_moving_config32(dev, PCI_CB_IO_BASE_1);
Yinghai Lu304f24c2005-07-08 02:56:47 +000078 moving_limit = pci_moving_config32(dev, PCI_CB_IO_LIMIT_1);
79 moving = moving_base & moving_limit;
80
Uwe Hermannd453dd02010-10-18 00:00:57 +000081 /* Initialize the I/O space constraints on the current bus. */
Yinghai Lu304f24c2005-07-08 02:56:47 +000082 cardbus_record_bridge_resource(dev, moving, CARDBUS_IO_SIZE,
Uwe Hermannd453dd02010-10-18 00:00:57 +000083 PCI_CB_IO_BASE_1, IORESOURCE_IO);
Yinghai Lu304f24c2005-07-08 02:56:47 +000084
Uwe Hermannd453dd02010-10-18 00:00:57 +000085 /* If I can, enable prefetch for mem0. */
Yinghai Lu304f24c2005-07-08 02:56:47 +000086 ctl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL);
87 ctl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
88 ctl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
89 ctl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
90 pci_write_config16(dev, PCI_CB_BRIDGE_CONTROL, ctl);
91 ctl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL);
92
Uwe Hermannd453dd02010-10-18 00:00:57 +000093 /* See which bridge memory resources are implemented. */
94 moving_base = pci_moving_config32(dev, PCI_CB_MEMORY_BASE_0);
Yinghai Lu304f24c2005-07-08 02:56:47 +000095 moving_limit = pci_moving_config32(dev, PCI_CB_MEMORY_LIMIT_0);
96 moving = moving_base & moving_limit;
97
Uwe Hermannd453dd02010-10-18 00:00:57 +000098 /* Initialize the memory space constraints on the current bus. */
Yinghai Lu304f24c2005-07-08 02:56:47 +000099 type = IORESOURCE_MEM;
Uwe Hermannd453dd02010-10-18 00:00:57 +0000100 if (ctl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)
Yinghai Lu304f24c2005-07-08 02:56:47 +0000101 type |= IORESOURCE_PREFETCH;
Yinghai Lu304f24c2005-07-08 02:56:47 +0000102 cardbus_record_bridge_resource(dev, moving, CARDBUS_MEM_SIZE,
Uwe Hermannd453dd02010-10-18 00:00:57 +0000103 PCI_CB_MEMORY_BASE_0, type);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000104
Uwe Hermannd453dd02010-10-18 00:00:57 +0000105 /* See which bridge memory resources are implemented. */
106 moving_base = pci_moving_config32(dev, PCI_CB_MEMORY_BASE_1);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000107 moving_limit = pci_moving_config32(dev, PCI_CB_MEMORY_LIMIT_1);
108 moving = moving_base & moving_limit;
109
Uwe Hermannd453dd02010-10-18 00:00:57 +0000110 /* Initialize the memory space constraints on the current bus. */
Yinghai Lu304f24c2005-07-08 02:56:47 +0000111 cardbus_record_bridge_resource(dev, moving, CARDBUS_MEM_SIZE,
Uwe Hermannd453dd02010-10-18 00:00:57 +0000112 PCI_CB_MEMORY_BASE_1, IORESOURCE_MEM);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000113
114 compact_resources(dev);
115}
116
Elyes HAOUASe18cbea2018-05-02 21:20:59 +0200117void cardbus_enable_resources(struct device *dev)
Yinghai Lu304f24c2005-07-08 02:56:47 +0000118{
Uwe Hermannd453dd02010-10-18 00:00:57 +0000119 u16 ctrl;
120
Yinghai Lu304f24c2005-07-08 02:56:47 +0000121 ctrl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL);
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200122 ctrl |= (dev->downstream->bridge_ctrl & (
Yinghai Lu304f24c2005-07-08 02:56:47 +0000123 PCI_BRIDGE_CTL_VGA |
124 PCI_BRIDGE_CTL_MASTER_ABORT |
125 PCI_BRIDGE_CTL_BUS_RESET));
Uwe Hermannd453dd02010-10-18 00:00:57 +0000126 /* Error check */
Kyösti Mälkki382e2162019-09-21 16:19:32 +0300127 ctrl |= (PCI_CB_BRIDGE_CTL_PARITY | PCI_CB_BRIDGE_CTL_SERR);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000128 printk(BIOS_DEBUG, "%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
Kyösti Mälkki0bca0502019-09-21 16:21:47 +0300129 pci_write_config16(dev, PCI_CB_BRIDGE_CONTROL, ctrl);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000130
131 pci_dev_enable_resources(dev);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000132}
133
Yinghai Lu304f24c2005-07-08 02:56:47 +0000134struct device_operations default_cardbus_ops_bus = {
135 .read_resources = cardbus_read_resources,
136 .set_resources = pci_dev_set_resources,
137 .enable_resources = cardbus_enable_resources,
Uwe Hermannd453dd02010-10-18 00:00:57 +0000138 .scan_bus = pci_scan_bridge,
Yinghai Lu304f24c2005-07-08 02:56:47 +0000139 .reset_bus = pci_bus_reset,
140};