blob: adfc50358174500e738689057821a6a1754944b1 [file] [log] [blame]
Uwe Hermannb80dbf02007-04-22 19:08:13 +00001/*
Stefan Reinauer7e61e452008-01-18 10:35:56 +00002 * This file is part of the coreboot project.
Uwe Hermannb80dbf02007-04-22 19:08:13 +00003 *
4 * Copyright (C) 2005 Linux Networx
5 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
6 * Copyright (C) 2005 Ronald G. Minnich <rminnich@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Uwe Hermannb80dbf02007-04-22 19:08:13 +000016 */
Yinghai Lu304f24c2005-07-08 02:56:47 +000017
18#include <console/console.h>
19#include <device/device.h>
20#include <device/pci.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020021#include <device/pci_ops.h>
Yinghai Lu304f24c2005-07-08 02:56:47 +000022#include <device/pci_ids.h>
23#include <device/cardbus.h>
24
Uwe Hermannd453dd02010-10-18 00:00:57 +000025/*
26 * I don't think this code is quite correct but it is close.
Yinghai Lu304f24c2005-07-08 02:56:47 +000027 * Anyone with a cardbus bridge and a little time should be able
28 * to make it usable quickly. -- Eric Biederman 24 March 2005
29 */
30
31/*
Uwe Hermannd453dd02010-10-18 00:00:57 +000032 * IO should be max 256 bytes. However, since we may have a P2P bridge below
33 * a cardbus bridge, we need 4K.
Yinghai Lu304f24c2005-07-08 02:56:47 +000034 */
Uwe Hermannd453dd02010-10-18 00:00:57 +000035#define CARDBUS_IO_SIZE 4096
36#define CARDBUS_MEM_SIZE (32 * 1024 * 1024)
Yinghai Lu304f24c2005-07-08 02:56:47 +000037
Elyes HAOUASe18cbea2018-05-02 21:20:59 +020038static void cardbus_record_bridge_resource(struct device *dev, resource_t moving,
Uwe Hermannd453dd02010-10-18 00:00:57 +000039 resource_t min_size, unsigned int index, unsigned long type)
Yinghai Lu304f24c2005-07-08 02:56:47 +000040{
Yinghai Lu304f24c2005-07-08 02:56:47 +000041 struct resource *resource;
Uwe Hermanne4870472010-11-04 23:23:47 +000042 unsigned long gran;
43 resource_t step;
Uwe Hermannd453dd02010-10-18 00:00:57 +000044
45 /* Initialize the constraints on the current bus. */
Myles Watson03adcfd2010-06-07 16:51:11 +000046 resource = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +000047 if (!moving)
48 return;
Uwe Hermannd453dd02010-10-18 00:00:57 +000049
Uwe Hermanne4870472010-11-04 23:23:47 +000050 resource = new_resource(dev, index);
51 resource->size = 0;
52 gran = 0;
53 step = 1;
54 while ((moving & step) == 0) {
55 gran += 1;
56 step <<= 1;
Yinghai Lu304f24c2005-07-08 02:56:47 +000057 }
Uwe Hermanne4870472010-11-04 23:23:47 +000058 resource->gran = gran;
59 resource->align = gran;
60 resource->limit = moving | (step - 1);
61 resource->flags = type;
62
63 /* Don't let the minimum size exceed what we can put in the resource. */
64 if ((min_size - 1) > resource->limit)
65 min_size = resource->limit + 1;
66
67 resource->size = min_size;
Yinghai Lu304f24c2005-07-08 02:56:47 +000068}
69
Elyes HAOUASe18cbea2018-05-02 21:20:59 +020070static void cardbus_size_bridge_resource(struct device *dev, unsigned int index)
Yinghai Lu304f24c2005-07-08 02:56:47 +000071{
72 struct resource *resource;
73 resource_t min_size;
Uwe Hermannd453dd02010-10-18 00:00:57 +000074
Yinghai Lu304f24c2005-07-08 02:56:47 +000075 resource = find_resource(dev, index);
76 if (resource) {
77 min_size = resource->size;
Uwe Hermannd453dd02010-10-18 00:00:57 +000078 /*
Elyes HAOUAS394ec022018-08-07 12:23:43 +020079 * Always allocate at least the minimum size to a
Yinghai Lu304f24c2005-07-08 02:56:47 +000080 * cardbus bridge in case a new card is plugged in.
81 */
Uwe Hermannd453dd02010-10-18 00:00:57 +000082 if (resource->size < min_size)
Yinghai Lu304f24c2005-07-08 02:56:47 +000083 resource->size = min_size;
Yinghai Lu304f24c2005-07-08 02:56:47 +000084 }
85}
86
Elyes HAOUASe18cbea2018-05-02 21:20:59 +020087void cardbus_read_resources(struct device *dev)
Yinghai Lu304f24c2005-07-08 02:56:47 +000088{
89 resource_t moving_base, moving_limit, moving;
90 unsigned long type;
Uwe Hermannd453dd02010-10-18 00:00:57 +000091 u16 ctl;
Ronald G. Minnich43225bc2005-11-22 00:07:02 +000092
Uwe Hermannd453dd02010-10-18 00:00:57 +000093 /* See if needs a card control registers base address. */
Ronald G. Minnich43225bc2005-11-22 00:07:02 +000094
95 pci_get_resource(dev, PCI_BASE_ADDRESS_0);
96
97 compact_resources(dev);
98
Uwe Hermannd453dd02010-10-18 00:00:57 +000099 /* See which bridge I/O resources are implemented. */
100 moving_base = pci_moving_config32(dev, PCI_CB_IO_BASE_0);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000101 moving_limit = pci_moving_config32(dev, PCI_CB_IO_LIMIT_0);
102 moving = moving_base & moving_limit;
103
Uwe Hermannd453dd02010-10-18 00:00:57 +0000104 /* Initialize the I/O space constraints on the current bus. */
Yinghai Lu304f24c2005-07-08 02:56:47 +0000105 cardbus_record_bridge_resource(dev, moving, CARDBUS_IO_SIZE,
Uwe Hermannd453dd02010-10-18 00:00:57 +0000106 PCI_CB_IO_BASE_0, IORESOURCE_IO);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000107 cardbus_size_bridge_resource(dev, PCI_CB_IO_BASE_0);
108
Uwe Hermannd453dd02010-10-18 00:00:57 +0000109 /* See which bridge I/O resources are implemented. */
110 moving_base = pci_moving_config32(dev, PCI_CB_IO_BASE_1);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000111 moving_limit = pci_moving_config32(dev, PCI_CB_IO_LIMIT_1);
112 moving = moving_base & moving_limit;
113
Uwe Hermannd453dd02010-10-18 00:00:57 +0000114 /* Initialize the I/O space constraints on the current bus. */
Yinghai Lu304f24c2005-07-08 02:56:47 +0000115 cardbus_record_bridge_resource(dev, moving, CARDBUS_IO_SIZE,
Uwe Hermannd453dd02010-10-18 00:00:57 +0000116 PCI_CB_IO_BASE_1, IORESOURCE_IO);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000117
Uwe Hermannd453dd02010-10-18 00:00:57 +0000118 /* If I can, enable prefetch for mem0. */
Yinghai Lu304f24c2005-07-08 02:56:47 +0000119 ctl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL);
120 ctl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
121 ctl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
122 ctl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
123 pci_write_config16(dev, PCI_CB_BRIDGE_CONTROL, ctl);
124 ctl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL);
125
Uwe Hermannd453dd02010-10-18 00:00:57 +0000126 /* See which bridge memory resources are implemented. */
127 moving_base = pci_moving_config32(dev, PCI_CB_MEMORY_BASE_0);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000128 moving_limit = pci_moving_config32(dev, PCI_CB_MEMORY_LIMIT_0);
129 moving = moving_base & moving_limit;
130
Uwe Hermannd453dd02010-10-18 00:00:57 +0000131 /* Initialize the memory space constraints on the current bus. */
Yinghai Lu304f24c2005-07-08 02:56:47 +0000132 type = IORESOURCE_MEM;
Uwe Hermannd453dd02010-10-18 00:00:57 +0000133 if (ctl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)
Yinghai Lu304f24c2005-07-08 02:56:47 +0000134 type |= IORESOURCE_PREFETCH;
Yinghai Lu304f24c2005-07-08 02:56:47 +0000135 cardbus_record_bridge_resource(dev, moving, CARDBUS_MEM_SIZE,
Uwe Hermannd453dd02010-10-18 00:00:57 +0000136 PCI_CB_MEMORY_BASE_0, type);
137 if (type & IORESOURCE_PREFETCH)
Yinghai Lu304f24c2005-07-08 02:56:47 +0000138 cardbus_size_bridge_resource(dev, PCI_CB_MEMORY_BASE_0);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000139
Uwe Hermannd453dd02010-10-18 00:00:57 +0000140 /* See which bridge memory resources are implemented. */
141 moving_base = pci_moving_config32(dev, PCI_CB_MEMORY_BASE_1);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000142 moving_limit = pci_moving_config32(dev, PCI_CB_MEMORY_LIMIT_1);
143 moving = moving_base & moving_limit;
144
Uwe Hermannd453dd02010-10-18 00:00:57 +0000145 /* Initialize the memory space constraints on the current bus. */
Yinghai Lu304f24c2005-07-08 02:56:47 +0000146 cardbus_record_bridge_resource(dev, moving, CARDBUS_MEM_SIZE,
Uwe Hermannd453dd02010-10-18 00:00:57 +0000147 PCI_CB_MEMORY_BASE_1, IORESOURCE_MEM);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000148 cardbus_size_bridge_resource(dev, PCI_CB_MEMORY_BASE_1);
149
150 compact_resources(dev);
151}
152
Elyes HAOUASe18cbea2018-05-02 21:20:59 +0200153void cardbus_enable_resources(struct device *dev)
Yinghai Lu304f24c2005-07-08 02:56:47 +0000154{
Uwe Hermannd453dd02010-10-18 00:00:57 +0000155 u16 ctrl;
156
Yinghai Lu304f24c2005-07-08 02:56:47 +0000157 ctrl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL);
Myles Watson894a3472010-06-09 22:41:35 +0000158 ctrl |= (dev->link_list->bridge_ctrl & (
Stefan Reinauer14e22772010-04-27 06:56:47 +0000159 PCI_BRIDGE_CTL_PARITY |
160 PCI_BRIDGE_CTL_SERR |
Yinghai Lu304f24c2005-07-08 02:56:47 +0000161 PCI_BRIDGE_CTL_NO_ISA |
162 PCI_BRIDGE_CTL_VGA |
163 PCI_BRIDGE_CTL_MASTER_ABORT |
164 PCI_BRIDGE_CTL_BUS_RESET));
Uwe Hermannd453dd02010-10-18 00:00:57 +0000165 /* Error check */
166 ctrl |= (PCI_CB_BRIDGE_CTL_PARITY + PCI_CB_BRIDGE_CTL_SERR);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000167 printk(BIOS_DEBUG, "%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000168 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
169
170 pci_dev_enable_resources(dev);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000171}
172
Yinghai Lu304f24c2005-07-08 02:56:47 +0000173struct device_operations default_cardbus_ops_bus = {
174 .read_resources = cardbus_read_resources,
175 .set_resources = pci_dev_set_resources,
176 .enable_resources = cardbus_enable_resources,
Uwe Hermannd453dd02010-10-18 00:00:57 +0000177 .init = 0,
178 .scan_bus = pci_scan_bridge,
Yinghai Lu304f24c2005-07-08 02:56:47 +0000179 .enable = 0,
180 .reset_bus = pci_bus_reset,
181};