blob: f8b4f18472e9e399df1bca0e4d9159ff027ff23e [file] [log] [blame]
Aaron Durbine18d68f2013-10-24 00:05:31 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008-2009 coresystems GmbH
5 * Copyright (C) 2013 Google Inc.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <stdint.h>
22#include <arch/io.h>
Aaron Durbind7bc23a2013-10-29 16:37:10 -050023#include <console/console.h>
Aaron Durbine18d68f2013-10-24 00:05:31 -050024#include <device/device.h>
25#include <device/pci.h>
26#include <device/pci_ids.h>
27#include <romstage_handoff.h>
28
29#include <baytrail/iomap.h>
30#include <baytrail/lpc.h>
31#include <baytrail/nvs.h>
32#include <baytrail/pci_devs.h>
Aaron Durbind7bc23a2013-10-29 16:37:10 -050033#include <baytrail/pmc.h>
Aaron Durbine18d68f2013-10-24 00:05:31 -050034#include <baytrail/ramstage.h>
35
36static inline void
37add_mmio_resource(device_t dev, int i, unsigned long addr, unsigned long size)
38{
39 mmio_resource(dev, i, addr >> 10, size >> 10);
40}
41
42static void sc_add_mmio_resources(device_t dev)
43{
Duncan Laurie7fbe20b2013-11-04 17:00:22 -080044 add_mmio_resource(dev, 0xfeb, ABORT_BASE_ADDRESS, ABORT_BASE_SIZE);
45 add_mmio_resource(dev, PBASE, PMC_BASE_ADDRESS, PMC_BASE_SIZE);
46 add_mmio_resource(dev, IOBASE, IO_BASE_ADDRESS, IO_BASE_SIZE);
47 add_mmio_resource(dev, IBASE, ILB_BASE_ADDRESS, ILB_BASE_SIZE);
48 add_mmio_resource(dev, SBASE, SPI_BASE_ADDRESS, SPI_BASE_SIZE);
49 add_mmio_resource(dev, MPBASE, MPHY_BASE_ADDRESS, MPHY_BASE_SIZE);
50 add_mmio_resource(dev, PUBASE, PUNIT_BASE_ADDRESS, PUNIT_BASE_SIZE);
51 add_mmio_resource(dev, RCBA, RCBA_BASE_ADDRESS, RCBA_BASE_SIZE);
Aaron Durbine18d68f2013-10-24 00:05:31 -050052}
53
54/* Default IO range claimed by the LPC device. The upper bound is exclusive. */
55#define LPC_DEFAULT_IO_RANGE_LOWER 0
56#define LPC_DEFAULT_IO_RANGE_UPPER 0x1000
57
58static inline int io_range_in_default(int base, int size)
59{
60 /* Does it start above the range? */
61 if (base >= LPC_DEFAULT_IO_RANGE_UPPER)
62 return 0;
63
64 /* Is it entirely contained? */
65 if (base >= LPC_DEFAULT_IO_RANGE_LOWER &&
66 (base + size) < LPC_DEFAULT_IO_RANGE_UPPER)
67 return 1;
68
69 /* This will return not in range for partial overlaps. */
70 return 0;
71}
72
73/*
74 * Note: this function assumes there is no overlap with the default LPC device's
75 * claimed range: LPC_DEFAULT_IO_RANGE_LOWER -> LPC_DEFAULT_IO_RANGE_UPPER.
76 */
77static void sc_add_io_resource(device_t dev, int base, int size, int index)
78{
79 struct resource *res;
80
81 if (io_range_in_default(base, size))
82 return;
83
84 res = new_resource(dev, index);
85 res->base = base;
86 res->size = size;
87 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
88}
89
90static void sc_add_io_resources(device_t dev)
91{
92 struct resource *res;
93
94 /* Add the default claimed IO range for the LPC device. */
95 res = new_resource(dev, 0);
96 res->base = LPC_DEFAULT_IO_RANGE_LOWER;
97 res->size = LPC_DEFAULT_IO_RANGE_UPPER - LPC_DEFAULT_IO_RANGE_LOWER;
98 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
99
100 /* GPIO */
101 sc_add_io_resource(dev, GPIO_BASE_ADDRESS, 256, GBASE);
102
103 /* ACPI */
104 sc_add_io_resource(dev, ACPI_BASE_ADDRESS, 128, ABASE);
105}
106
107static void sc_read_resources(device_t dev)
108{
109 /* Get the normal PCI resources of this device. */
110 pci_dev_read_resources(dev);
111
112 /* Add non-standard MMIO resources. */
113 sc_add_mmio_resources(dev);
114
115 /* Add IO resources. */
116 sc_add_io_resources(dev);
117}
118
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500119/*
120 * Common code for the south cluster devices.
121 */
122
123/* Set bit in function disble register to hide this device. */
124static void sc_disable_devfn(device_t dev)
125{
126 const unsigned long func_dis = PMC_BASE_ADDRESS + FUNC_DIS;
127 const unsigned long func_dis2 = PMC_BASE_ADDRESS + FUNC_DIS2;
128 uint32_t mask = 0;
129 uint32_t mask2 = 0;
130
131 switch (dev->path.pci.devfn) {
132 case PCI_DEVFN(SDIO_DEV, SDIO_FUNC):
133 mask |= SDIO_DIS;
134 break;
135 case PCI_DEVFN(SD_DEV, SD_FUNC):
136 mask |= SD_DIS;
137 break;
138 case PCI_DEVFN(SATA_DEV, SATA_FUNC):
139 mask |= SATA_DIS;
140 break;
141 case PCI_DEVFN(XHCI_DEV, XHCI_FUNC):
142 mask |= XHCI_DIS;
143 /* Disable super speed PHY when XHCI is not available. */
144 mask2 |= USH_SS_PHY_DIS;
145 break;
146 case PCI_DEVFN(LPE_DEV, LPE_FUNC):
147 mask |= LPE_DIS;
148 break;
149 case PCI_DEVFN(MMC_DEV, MMC_FUNC):
150 mask |= MMC_DIS;
151 break;
152 case PCI_DEVFN(SIO_DMA1_DEV, SIO_DMA1_FUNC):
153 mask |= SIO_DMA1_DIS;
154 break;
155 case PCI_DEVFN(I2C1_DEV, I2C1_FUNC):
156 mask |= I2C1_DIS;
157 break;
158 case PCI_DEVFN(I2C2_DEV, I2C2_FUNC):
159 mask |= I2C1_DIS;
160 break;
161 case PCI_DEVFN(I2C3_DEV, I2C3_FUNC):
162 mask |= I2C3_DIS;
163 break;
164 case PCI_DEVFN(I2C4_DEV, I2C4_FUNC):
165 mask |= I2C4_DIS;
166 break;
167 case PCI_DEVFN(I2C5_DEV, I2C5_FUNC):
168 mask |= I2C5_DIS;
169 break;
170 case PCI_DEVFN(I2C6_DEV, I2C6_FUNC):
171 mask |= I2C6_DIS;
172 break;
173 case PCI_DEVFN(I2C7_DEV, I2C7_FUNC):
174 mask |= I2C7_DIS;
175 break;
176 case PCI_DEVFN(TXE_DEV, TXE_FUNC):
177 mask |= TXE_DIS;
178 break;
179 case PCI_DEVFN(HDA_DEV, HDA_FUNC):
180 mask |= HDA_DIS;
181 break;
182 case PCI_DEVFN(PCIE_PORT1_DEV, PCIE_PORT1_FUNC):
183 mask |= PCIE_PORT1_DIS;
184 break;
185 case PCI_DEVFN(PCIE_PORT2_DEV, PCIE_PORT2_FUNC):
186 mask |= PCIE_PORT2_DIS;
187 break;
188 case PCI_DEVFN(PCIE_PORT3_DEV, PCIE_PORT3_FUNC):
189 mask |= PCIE_PORT3_DIS;
190 break;
191 case PCI_DEVFN(PCIE_PORT4_DEV, PCIE_PORT4_FUNC):
192 mask |= PCIE_PORT4_DIS;
193 break;
194 case PCI_DEVFN(EHCI_DEV, EHCI_FUNC):
195 mask |= EHCI_DIS;
196 break;
197 case PCI_DEVFN(SIO_DMA2_DEV, SIO_DMA2_FUNC):
198 mask |= SIO_DMA2_DIS;
199 break;
200 case PCI_DEVFN(PWM1_DEV, PWM1_FUNC):
201 mask |= PWM1_DIS;
202 break;
203 case PCI_DEVFN(PWM2_DEV, PWM2_FUNC):
204 mask |= PWM2_DIS;
205 break;
206 case PCI_DEVFN(HSUART1_DEV, HSUART1_FUNC):
207 mask |= HSUART1_DIS;
208 break;
209 case PCI_DEVFN(HSUART2_DEV, HSUART2_FUNC):
210 mask |= HSUART2_DIS;
211 break;
212 case PCI_DEVFN(SPI_DEV, SPI_FUNC):
213 mask |= SPI_DIS;
214 break;
215 case PCI_DEVFN(SMBUS_DEV, SMBUS_FUNC):
216 mask2 |= SMBUS_DIS;
217 break;
218 }
219
220 if (mask != 0) {
221 write32(func_dis, read32(func_dis) | mask);
222 /* Ensure posted write hits. */
223 read32(func_dis);
224 }
225
226 if (mask2 != 0) {
227 write32(func_dis2, read32(func_dis2) | mask2);
228 /* Ensure posted write hits. */
229 read32(func_dis2);
230 }
231}
232
233static inline void set_d3hot_bits(device_t dev, int offset)
234{
235 uint32_t reg8;
236 printk(BIOS_DEBUG, "Power management CAP offset 0x%x.\n", offset);
237 reg8 = pci_read_config8(dev, offset + 4);
238 reg8 |= 0x3;
239 pci_write_config8(dev, offset + 4, reg8);
240}
241
Aaron Durbin46ab8cd2013-10-30 17:07:46 -0500242/* Parts of the audio subsystem are powered by the HDA device. Therefore, one
243 * cannot put HDA into D3Hot. Instead perform this workaround to make some of
244 * the audio paths work for LPE audio. */
245static void hda_work_around(device_t dev)
246{
247 unsigned long gctl = TEMP_BASE_ADDRESS + 0x8;
248
249 /* Need to set magic register 0x43 to 0xd7 in config space. */
250 pci_write_config8(dev, 0x43, 0xd7);
251
252 /* Need to set bit 0 of GCTL to take the device out of reset. However,
253 * that requires setting up the 64-bit BAR. */
254 pci_write_config32(dev, PCI_BASE_ADDRESS_0, TEMP_BASE_ADDRESS);
255 pci_write_config32(dev, PCI_BASE_ADDRESS_1, 0);
256 pci_write_config8(dev, PCI_COMMAND, PCI_COMMAND_MEMORY);
257 write32(gctl, read32(gctl) | 0x1);
258 pci_write_config8(dev, PCI_COMMAND, 0);
259 pci_write_config32(dev, PCI_BASE_ADDRESS_0, 0);
260}
261
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500262static int place_device_in_d3hot(device_t dev)
263{
264 unsigned offset;
265
Aaron Durbin46ab8cd2013-10-30 17:07:46 -0500266 /* Parts of the HDA block are used for LPE audio as well.
267 * Therefore assume the HDA will never be put into D3Hot. */
268 if (dev->path.pci.devfn == PCI_DEVFN(HDA_DEV, HDA_FUNC)) {
269 hda_work_around(dev);
270 return 0;
271 }
272
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500273 offset = pci_find_capability(dev, PCI_CAP_ID_PM);
274
275 if (offset != 0) {
276 set_d3hot_bits(dev, offset);
277 return 0;
278 }
279
280 /* For some reason some of the devices don't have the capability
281 * pointer set correctly. Work around this by hard coding the offset. */
282 switch (dev->path.pci.devfn) {
283 case PCI_DEVFN(SDIO_DEV, SDIO_FUNC):
284 offset = 0x80;
285 break;
286 case PCI_DEVFN(SD_DEV, SD_FUNC):
287 offset = 0x80;
288 break;
289 case PCI_DEVFN(MMC_DEV, MMC_FUNC):
290 offset = 0x80;
291 break;
292 case PCI_DEVFN(LPE_DEV, LPE_FUNC):
293 offset = 0x80;
294 break;
295 case PCI_DEVFN(SIO_DMA1_DEV, SIO_DMA1_FUNC):
296 offset = 0x80;
297 break;
298 case PCI_DEVFN(I2C1_DEV, I2C1_FUNC):
299 offset = 0x80;
300 break;
301 case PCI_DEVFN(I2C2_DEV, I2C2_FUNC):
302 offset = 0x80;
303 break;
304 case PCI_DEVFN(I2C3_DEV, I2C3_FUNC):
305 offset = 0x80;
306 break;
307 case PCI_DEVFN(I2C4_DEV, I2C4_FUNC):
308 offset = 0x80;
309 break;
310 case PCI_DEVFN(I2C5_DEV, I2C5_FUNC):
311 offset = 0x80;
312 break;
313 case PCI_DEVFN(I2C6_DEV, I2C6_FUNC):
314 offset = 0x80;
315 break;
316 case PCI_DEVFN(I2C7_DEV, I2C7_FUNC):
317 offset = 0x80;
318 break;
319 case PCI_DEVFN(SIO_DMA2_DEV, SIO_DMA2_FUNC):
320 offset = 0x80;
321 break;
322 case PCI_DEVFN(PWM1_DEV, PWM1_FUNC):
323 offset = 0x80;
324 break;
325 case PCI_DEVFN(PWM2_DEV, PWM2_FUNC):
326 offset = 0x80;
327 break;
328 case PCI_DEVFN(HSUART1_DEV, HSUART1_FUNC):
329 offset = 0x80;
330 break;
331 case PCI_DEVFN(HSUART2_DEV, HSUART2_FUNC):
332 offset = 0x80;
333 break;
334 case PCI_DEVFN(SPI_DEV, SPI_FUNC):
335 offset = 0x80;
336 break;
337 case PCI_DEVFN(SATA_DEV, SATA_FUNC):
338 offset = 0x70;
339 break;
340 case PCI_DEVFN(XHCI_DEV, XHCI_FUNC):
341 offset = 0x70;
342 break;
343 case PCI_DEVFN(EHCI_DEV, EHCI_FUNC):
344 offset = 0x70;
345 break;
346 case PCI_DEVFN(HDA_DEV, HDA_FUNC):
347 offset = 0x50;
348 break;
349 case PCI_DEVFN(SMBUS_DEV, SMBUS_FUNC):
350 offset = 0x50;
351 break;
352 case PCI_DEVFN(TXE_DEV, TXE_FUNC):
Aaron Durbin1eae3ee2013-10-30 17:08:59 -0500353 /* TXE cannot be placed in D3Hot. */
354 return 0;
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500355 case PCI_DEVFN(PCIE_PORT1_DEV, PCIE_PORT1_FUNC):
356 offset = 0xa0;
357 break;
358 case PCI_DEVFN(PCIE_PORT2_DEV, PCIE_PORT2_FUNC):
359 offset = 0xa0;
360 break;
361 case PCI_DEVFN(PCIE_PORT3_DEV, PCIE_PORT3_FUNC):
362 offset = 0xa0;
363 break;
364 case PCI_DEVFN(PCIE_PORT4_DEV, PCIE_PORT4_FUNC):
365 offset = 0xa0;
366 break;
367 }
368
369 if (offset != 0) {
370 set_d3hot_bits(dev, offset);
371 return 0;
372 }
373
374 return -1;
375}
376
377/* Common PCI device function disable. */
378void southcluster_enable_dev(device_t dev)
379{
380 uint32_t reg32;
381
382 if (!dev->enabled) {
383 int slot = PCI_SLOT(dev->path.pci.devfn);
384 int func = PCI_FUNC(dev->path.pci.devfn);
385 printk(BIOS_DEBUG, "%s: Disabling device: %02x.%01x\n",
386 dev_path(dev), slot, func);
387
388 /* Ensure memory, io, and bus master are all disabled */
389 reg32 = pci_read_config32(dev, PCI_COMMAND);
390 reg32 &= ~(PCI_COMMAND_MASTER |
391 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
392 pci_write_config32(dev, PCI_COMMAND, reg32);
393
394 /* Place device in D3Hot */
395 if (place_device_in_d3hot(dev) < 0) {
396 printk(BIOS_WARNING,
397 "Could not place %02x.%01x into D3Hot. "
398 "Keeping device visible.\n", slot, func);
399 return;
400 }
401 /* Disable this device if possible */
402 sc_disable_devfn(dev);
403 } else {
404 /* Enable SERR */
405 reg32 = pci_read_config32(dev, PCI_COMMAND);
406 reg32 |= PCI_COMMAND_SERR;
407 pci_write_config32(dev, PCI_COMMAND, reg32);
408 }
409}
410
Aaron Durbine18d68f2013-10-24 00:05:31 -0500411static struct device_operations device_ops = {
412 .read_resources = sc_read_resources,
413 .set_resources = pci_dev_set_resources,
414 .enable_resources = NULL,
415 .init = NULL,
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500416 .enable = southcluster_enable_dev,
Duncan Laurie5d535542013-10-31 10:10:20 -0700417 .scan_bus = scan_static_bus,
Aaron Durbine18d68f2013-10-24 00:05:31 -0500418 .ops_pci = &soc_pci_ops,
419};
420
421static const struct pci_driver southcluster __pci_driver = {
422 .ops = &device_ops,
423 .vendor = PCI_VENDOR_ID_INTEL,
424 .device = LPC_DEVID,
425};