blob: 19e9900216feecbac2bc256bc60fe44bbf6f8da1 [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>
21#include <device/pci_ids.h>
22#include <device/cardbus.h>
23
Uwe Hermannd453dd02010-10-18 00:00:57 +000024/*
25 * I don't think this code is quite correct but it is close.
Yinghai Lu304f24c2005-07-08 02:56:47 +000026 * Anyone with a cardbus bridge and a little time should be able
27 * to make it usable quickly. -- Eric Biederman 24 March 2005
28 */
29
30/*
Uwe Hermannd453dd02010-10-18 00:00:57 +000031 * IO should be max 256 bytes. However, since we may have a P2P bridge below
32 * a cardbus bridge, we need 4K.
Yinghai Lu304f24c2005-07-08 02:56:47 +000033 */
Uwe Hermannd453dd02010-10-18 00:00:57 +000034#define CARDBUS_IO_SIZE 4096
35#define CARDBUS_MEM_SIZE (32 * 1024 * 1024)
Yinghai Lu304f24c2005-07-08 02:56:47 +000036
Uwe Hermannd453dd02010-10-18 00:00:57 +000037static void cardbus_record_bridge_resource(device_t dev, resource_t moving,
38 resource_t min_size, unsigned int index, unsigned long type)
Yinghai Lu304f24c2005-07-08 02:56:47 +000039{
Yinghai Lu304f24c2005-07-08 02:56:47 +000040 struct resource *resource;
Uwe Hermanne4870472010-11-04 23:23:47 +000041 unsigned long gran;
42 resource_t step;
Uwe Hermannd453dd02010-10-18 00:00:57 +000043
44 /* Initialize the constraints on the current bus. */
Myles Watson03adcfd2010-06-07 16:51:11 +000045 resource = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +000046 if (!moving)
47 return;
Uwe Hermannd453dd02010-10-18 00:00:57 +000048
Uwe Hermanne4870472010-11-04 23:23:47 +000049 resource = new_resource(dev, index);
50 resource->size = 0;
51 gran = 0;
52 step = 1;
53 while ((moving & step) == 0) {
54 gran += 1;
55 step <<= 1;
Yinghai Lu304f24c2005-07-08 02:56:47 +000056 }
Uwe Hermanne4870472010-11-04 23:23:47 +000057 resource->gran = gran;
58 resource->align = gran;
59 resource->limit = moving | (step - 1);
60 resource->flags = type;
61
62 /* Don't let the minimum size exceed what we can put in the resource. */
63 if ((min_size - 1) > resource->limit)
64 min_size = resource->limit + 1;
65
66 resource->size = min_size;
Yinghai Lu304f24c2005-07-08 02:56:47 +000067}
68
Uwe Hermannd453dd02010-10-18 00:00:57 +000069static void cardbus_size_bridge_resource(device_t dev, unsigned int index)
Yinghai Lu304f24c2005-07-08 02:56:47 +000070{
71 struct resource *resource;
72 resource_t min_size;
Uwe Hermannd453dd02010-10-18 00:00:57 +000073
Yinghai Lu304f24c2005-07-08 02:56:47 +000074 resource = find_resource(dev, index);
75 if (resource) {
76 min_size = resource->size;
Uwe Hermannd453dd02010-10-18 00:00:57 +000077 /*
78 * Always allocate at least the miniumum size to a
Yinghai Lu304f24c2005-07-08 02:56:47 +000079 * cardbus bridge in case a new card is plugged in.
80 */
Uwe Hermannd453dd02010-10-18 00:00:57 +000081 if (resource->size < min_size)
Yinghai Lu304f24c2005-07-08 02:56:47 +000082 resource->size = min_size;
Yinghai Lu304f24c2005-07-08 02:56:47 +000083 }
84}
85
86void cardbus_read_resources(device_t dev)
87{
88 resource_t moving_base, moving_limit, moving;
89 unsigned long type;
Uwe Hermannd453dd02010-10-18 00:00:57 +000090 u16 ctl;
Ronald G. Minnich43225bc2005-11-22 00:07:02 +000091
Uwe Hermannd453dd02010-10-18 00:00:57 +000092 /* See if needs a card control registers base address. */
Ronald G. Minnich43225bc2005-11-22 00:07:02 +000093
94 pci_get_resource(dev, PCI_BASE_ADDRESS_0);
95
96 compact_resources(dev);
97
Uwe Hermannd453dd02010-10-18 00:00:57 +000098 /* See which bridge I/O resources are implemented. */
99 moving_base = pci_moving_config32(dev, PCI_CB_IO_BASE_0);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000100 moving_limit = pci_moving_config32(dev, PCI_CB_IO_LIMIT_0);
101 moving = moving_base & moving_limit;
102
Uwe Hermannd453dd02010-10-18 00:00:57 +0000103 /* Initialize the I/O space constraints on the current bus. */
Yinghai Lu304f24c2005-07-08 02:56:47 +0000104 cardbus_record_bridge_resource(dev, moving, CARDBUS_IO_SIZE,
Uwe Hermannd453dd02010-10-18 00:00:57 +0000105 PCI_CB_IO_BASE_0, IORESOURCE_IO);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000106 cardbus_size_bridge_resource(dev, PCI_CB_IO_BASE_0);
107
Uwe Hermannd453dd02010-10-18 00:00:57 +0000108 /* See which bridge I/O resources are implemented. */
109 moving_base = pci_moving_config32(dev, PCI_CB_IO_BASE_1);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000110 moving_limit = pci_moving_config32(dev, PCI_CB_IO_LIMIT_1);
111 moving = moving_base & moving_limit;
112
Uwe Hermannd453dd02010-10-18 00:00:57 +0000113 /* Initialize the I/O space constraints on the current bus. */
Yinghai Lu304f24c2005-07-08 02:56:47 +0000114 cardbus_record_bridge_resource(dev, moving, CARDBUS_IO_SIZE,
Uwe Hermannd453dd02010-10-18 00:00:57 +0000115 PCI_CB_IO_BASE_1, IORESOURCE_IO);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000116
Uwe Hermannd453dd02010-10-18 00:00:57 +0000117 /* If I can, enable prefetch for mem0. */
Yinghai Lu304f24c2005-07-08 02:56:47 +0000118 ctl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL);
119 ctl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
120 ctl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
121 ctl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
122 pci_write_config16(dev, PCI_CB_BRIDGE_CONTROL, ctl);
123 ctl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL);
124
Uwe Hermannd453dd02010-10-18 00:00:57 +0000125 /* See which bridge memory resources are implemented. */
126 moving_base = pci_moving_config32(dev, PCI_CB_MEMORY_BASE_0);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000127 moving_limit = pci_moving_config32(dev, PCI_CB_MEMORY_LIMIT_0);
128 moving = moving_base & moving_limit;
129
Uwe Hermannd453dd02010-10-18 00:00:57 +0000130 /* Initialize the memory space constraints on the current bus. */
Yinghai Lu304f24c2005-07-08 02:56:47 +0000131 type = IORESOURCE_MEM;
Uwe Hermannd453dd02010-10-18 00:00:57 +0000132 if (ctl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)
Yinghai Lu304f24c2005-07-08 02:56:47 +0000133 type |= IORESOURCE_PREFETCH;
Yinghai Lu304f24c2005-07-08 02:56:47 +0000134 cardbus_record_bridge_resource(dev, moving, CARDBUS_MEM_SIZE,
Uwe Hermannd453dd02010-10-18 00:00:57 +0000135 PCI_CB_MEMORY_BASE_0, type);
136 if (type & IORESOURCE_PREFETCH)
Yinghai Lu304f24c2005-07-08 02:56:47 +0000137 cardbus_size_bridge_resource(dev, PCI_CB_MEMORY_BASE_0);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000138
Uwe Hermannd453dd02010-10-18 00:00:57 +0000139 /* See which bridge memory resources are implemented. */
140 moving_base = pci_moving_config32(dev, PCI_CB_MEMORY_BASE_1);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000141 moving_limit = pci_moving_config32(dev, PCI_CB_MEMORY_LIMIT_1);
142 moving = moving_base & moving_limit;
143
Uwe Hermannd453dd02010-10-18 00:00:57 +0000144 /* Initialize the memory space constraints on the current bus. */
Yinghai Lu304f24c2005-07-08 02:56:47 +0000145 cardbus_record_bridge_resource(dev, moving, CARDBUS_MEM_SIZE,
Uwe Hermannd453dd02010-10-18 00:00:57 +0000146 PCI_CB_MEMORY_BASE_1, IORESOURCE_MEM);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000147 cardbus_size_bridge_resource(dev, PCI_CB_MEMORY_BASE_1);
148
149 compact_resources(dev);
150}
151
152void cardbus_enable_resources(device_t dev)
153{
Uwe Hermannd453dd02010-10-18 00:00:57 +0000154 u16 ctrl;
155
Yinghai Lu304f24c2005-07-08 02:56:47 +0000156 ctrl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL);
Myles Watson894a3472010-06-09 22:41:35 +0000157 ctrl |= (dev->link_list->bridge_ctrl & (
Stefan Reinauer14e22772010-04-27 06:56:47 +0000158 PCI_BRIDGE_CTL_PARITY |
159 PCI_BRIDGE_CTL_SERR |
Yinghai Lu304f24c2005-07-08 02:56:47 +0000160 PCI_BRIDGE_CTL_NO_ISA |
161 PCI_BRIDGE_CTL_VGA |
162 PCI_BRIDGE_CTL_MASTER_ABORT |
163 PCI_BRIDGE_CTL_BUS_RESET));
Uwe Hermannd453dd02010-10-18 00:00:57 +0000164 /* Error check */
165 ctrl |= (PCI_CB_BRIDGE_CTL_PARITY + PCI_CB_BRIDGE_CTL_SERR);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000166 printk(BIOS_DEBUG, "%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000167 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
168
169 pci_dev_enable_resources(dev);
Yinghai Lu304f24c2005-07-08 02:56:47 +0000170}
171
Yinghai Lu304f24c2005-07-08 02:56:47 +0000172struct device_operations default_cardbus_ops_bus = {
173 .read_resources = cardbus_read_resources,
174 .set_resources = pci_dev_set_resources,
175 .enable_resources = cardbus_enable_resources,
Uwe Hermannd453dd02010-10-18 00:00:57 +0000176 .init = 0,
177 .scan_bus = pci_scan_bridge,
Yinghai Lu304f24c2005-07-08 02:56:47 +0000178 .enable = 0,
179 .reset_bus = pci_bus_reset,
180};