blob: bcce792659a763e405dd28cc1f017eea8773b2e1 [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>
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080023#include <cbmem.h>
Aaron Durbind7bc23a2013-10-29 16:37:10 -050024#include <console/console.h>
Aaron Durbine18d68f2013-10-24 00:05:31 -050025#include <device/device.h>
26#include <device/pci.h>
27#include <device/pci_ids.h>
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080028#include <pc80/mc146818rtc.h>
Aaron Durbine18d68f2013-10-24 00:05:31 -050029#include <romstage_handoff.h>
30
31#include <baytrail/iomap.h>
Aaron Durbin3bde3d72013-11-04 21:45:52 -060032#include <baytrail/irq.h>
Aaron Durbine18d68f2013-10-24 00:05:31 -050033#include <baytrail/lpc.h>
34#include <baytrail/nvs.h>
35#include <baytrail/pci_devs.h>
Aaron Durbind7bc23a2013-10-29 16:37:10 -050036#include <baytrail/pmc.h>
Aaron Durbine18d68f2013-10-24 00:05:31 -050037#include <baytrail/ramstage.h>
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080038#include "chip.h"
Aaron Durbine18d68f2013-10-24 00:05:31 -050039
40static inline void
41add_mmio_resource(device_t dev, int i, unsigned long addr, unsigned long size)
42{
43 mmio_resource(dev, i, addr >> 10, size >> 10);
44}
45
46static void sc_add_mmio_resources(device_t dev)
47{
Duncan Laurie7fbe20b2013-11-04 17:00:22 -080048 add_mmio_resource(dev, 0xfeb, ABORT_BASE_ADDRESS, ABORT_BASE_SIZE);
49 add_mmio_resource(dev, PBASE, PMC_BASE_ADDRESS, PMC_BASE_SIZE);
50 add_mmio_resource(dev, IOBASE, IO_BASE_ADDRESS, IO_BASE_SIZE);
51 add_mmio_resource(dev, IBASE, ILB_BASE_ADDRESS, ILB_BASE_SIZE);
52 add_mmio_resource(dev, SBASE, SPI_BASE_ADDRESS, SPI_BASE_SIZE);
53 add_mmio_resource(dev, MPBASE, MPHY_BASE_ADDRESS, MPHY_BASE_SIZE);
54 add_mmio_resource(dev, PUBASE, PUNIT_BASE_ADDRESS, PUNIT_BASE_SIZE);
55 add_mmio_resource(dev, RCBA, RCBA_BASE_ADDRESS, RCBA_BASE_SIZE);
Aaron Durbine18d68f2013-10-24 00:05:31 -050056}
57
58/* Default IO range claimed by the LPC device. The upper bound is exclusive. */
59#define LPC_DEFAULT_IO_RANGE_LOWER 0
60#define LPC_DEFAULT_IO_RANGE_UPPER 0x1000
61
62static inline int io_range_in_default(int base, int size)
63{
64 /* Does it start above the range? */
65 if (base >= LPC_DEFAULT_IO_RANGE_UPPER)
66 return 0;
67
68 /* Is it entirely contained? */
69 if (base >= LPC_DEFAULT_IO_RANGE_LOWER &&
70 (base + size) < LPC_DEFAULT_IO_RANGE_UPPER)
71 return 1;
72
73 /* This will return not in range for partial overlaps. */
74 return 0;
75}
76
77/*
78 * Note: this function assumes there is no overlap with the default LPC device's
79 * claimed range: LPC_DEFAULT_IO_RANGE_LOWER -> LPC_DEFAULT_IO_RANGE_UPPER.
80 */
81static void sc_add_io_resource(device_t dev, int base, int size, int index)
82{
83 struct resource *res;
84
85 if (io_range_in_default(base, size))
86 return;
87
88 res = new_resource(dev, index);
89 res->base = base;
90 res->size = size;
91 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
92}
93
94static void sc_add_io_resources(device_t dev)
95{
96 struct resource *res;
97
98 /* Add the default claimed IO range for the LPC device. */
99 res = new_resource(dev, 0);
100 res->base = LPC_DEFAULT_IO_RANGE_LOWER;
101 res->size = LPC_DEFAULT_IO_RANGE_UPPER - LPC_DEFAULT_IO_RANGE_LOWER;
102 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
103
104 /* GPIO */
105 sc_add_io_resource(dev, GPIO_BASE_ADDRESS, 256, GBASE);
106
107 /* ACPI */
108 sc_add_io_resource(dev, ACPI_BASE_ADDRESS, 128, ABASE);
109}
110
111static void sc_read_resources(device_t dev)
112{
113 /* Get the normal PCI resources of this device. */
114 pci_dev_read_resources(dev);
115
116 /* Add non-standard MMIO resources. */
117 sc_add_mmio_resources(dev);
118
119 /* Add IO resources. */
120 sc_add_io_resources(dev);
121}
122
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800123static void sc_rtc_init(void)
124{
125 uint32_t gen_pmcon1;
126 int rtc_fail;
127 struct chipset_power_state *ps = cbmem_find(CBMEM_ID_POWER_STATE);
128
129 if (ps != NULL) {
130 gen_pmcon1 = ps->gen_pmcon1;
131 } else {
132 gen_pmcon1 = read32(PMC_BASE_ADDRESS + GEN_PMCON1);
133 }
134
135 rtc_fail = !!(gen_pmcon1 & RPS);
136
137 if (rtc_fail) {
138 printk(BIOS_DEBUG, "RTC failure.\n");
139 }
140
141 rtc_init(rtc_fail);
142}
143
Aaron Durbin3bde3d72013-11-04 21:45:52 -0600144static void sc_init(device_t dev)
145{
146 int i;
147 const unsigned long pr_base = ILB_BASE_ADDRESS + 0x08;
148 const unsigned long ir_base = ILB_BASE_ADDRESS + 0x20;
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800149 const unsigned long gen_pmcon1 = PMC_BASE_ADDRESS + GEN_PMCON1;
Aaron Durbin1af36632013-11-07 10:42:16 -0600150 const unsigned long actl = ILB_BASE_ADDRESS + ACTL;
Aaron Durbin3bde3d72013-11-04 21:45:52 -0600151 const struct baytrail_irq_route *ir = &global_baytrail_irq_route;
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800152 struct soc_intel_baytrail_config *config = dev->chip_info;
Aaron Durbin3bde3d72013-11-04 21:45:52 -0600153
154 /* Set up the PIRQ PIC routing based on static config. */
155 for (i = 0; i < NUM_PIRQS; i++) {
156 write8(pr_base + i*sizeof(ir->pic[i]), ir->pic[i]);
157 }
158 /* Set up the per device PIRQ routing base on static config. */
159 for (i = 0; i < NUM_IR_DEVS; i++) {
160 write16(ir_base + i*sizeof(ir->pcidev[i]), ir->pcidev[i]);
161 }
Aaron Durbin1af36632013-11-07 10:42:16 -0600162
163 /* Route SCI to IRQ9 */
164 write32(actl, (read32(actl) & ~SCIS_MASK) | SCIS_IRQ9);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800165
166 sc_rtc_init();
167
168 if (config->disable_slp_x_stretch_sus_fail) {
169 printk(BIOS_DEBUG, "Disabling slp_x stretching.\n");
170 write32(gen_pmcon1,
171 read32(gen_pmcon1) | DIS_SLP_X_STRCH_SUS_UP);
172 } else {
173 write32(gen_pmcon1,
174 read32(gen_pmcon1) & ~DIS_SLP_X_STRCH_SUS_UP);
175 }
Aaron Durbin3bde3d72013-11-04 21:45:52 -0600176}
177
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500178/*
179 * Common code for the south cluster devices.
180 */
181
182/* Set bit in function disble register to hide this device. */
183static void sc_disable_devfn(device_t dev)
184{
185 const unsigned long func_dis = PMC_BASE_ADDRESS + FUNC_DIS;
186 const unsigned long func_dis2 = PMC_BASE_ADDRESS + FUNC_DIS2;
187 uint32_t mask = 0;
188 uint32_t mask2 = 0;
189
190 switch (dev->path.pci.devfn) {
191 case PCI_DEVFN(SDIO_DEV, SDIO_FUNC):
192 mask |= SDIO_DIS;
193 break;
194 case PCI_DEVFN(SD_DEV, SD_FUNC):
195 mask |= SD_DIS;
196 break;
197 case PCI_DEVFN(SATA_DEV, SATA_FUNC):
198 mask |= SATA_DIS;
199 break;
200 case PCI_DEVFN(XHCI_DEV, XHCI_FUNC):
201 mask |= XHCI_DIS;
202 /* Disable super speed PHY when XHCI is not available. */
203 mask2 |= USH_SS_PHY_DIS;
204 break;
205 case PCI_DEVFN(LPE_DEV, LPE_FUNC):
206 mask |= LPE_DIS;
207 break;
208 case PCI_DEVFN(MMC_DEV, MMC_FUNC):
209 mask |= MMC_DIS;
210 break;
211 case PCI_DEVFN(SIO_DMA1_DEV, SIO_DMA1_FUNC):
212 mask |= SIO_DMA1_DIS;
213 break;
214 case PCI_DEVFN(I2C1_DEV, I2C1_FUNC):
215 mask |= I2C1_DIS;
216 break;
217 case PCI_DEVFN(I2C2_DEV, I2C2_FUNC):
218 mask |= I2C1_DIS;
219 break;
220 case PCI_DEVFN(I2C3_DEV, I2C3_FUNC):
221 mask |= I2C3_DIS;
222 break;
223 case PCI_DEVFN(I2C4_DEV, I2C4_FUNC):
224 mask |= I2C4_DIS;
225 break;
226 case PCI_DEVFN(I2C5_DEV, I2C5_FUNC):
227 mask |= I2C5_DIS;
228 break;
229 case PCI_DEVFN(I2C6_DEV, I2C6_FUNC):
230 mask |= I2C6_DIS;
231 break;
232 case PCI_DEVFN(I2C7_DEV, I2C7_FUNC):
233 mask |= I2C7_DIS;
234 break;
235 case PCI_DEVFN(TXE_DEV, TXE_FUNC):
236 mask |= TXE_DIS;
237 break;
238 case PCI_DEVFN(HDA_DEV, HDA_FUNC):
239 mask |= HDA_DIS;
240 break;
241 case PCI_DEVFN(PCIE_PORT1_DEV, PCIE_PORT1_FUNC):
242 mask |= PCIE_PORT1_DIS;
243 break;
244 case PCI_DEVFN(PCIE_PORT2_DEV, PCIE_PORT2_FUNC):
245 mask |= PCIE_PORT2_DIS;
246 break;
247 case PCI_DEVFN(PCIE_PORT3_DEV, PCIE_PORT3_FUNC):
248 mask |= PCIE_PORT3_DIS;
249 break;
250 case PCI_DEVFN(PCIE_PORT4_DEV, PCIE_PORT4_FUNC):
251 mask |= PCIE_PORT4_DIS;
252 break;
253 case PCI_DEVFN(EHCI_DEV, EHCI_FUNC):
254 mask |= EHCI_DIS;
255 break;
256 case PCI_DEVFN(SIO_DMA2_DEV, SIO_DMA2_FUNC):
257 mask |= SIO_DMA2_DIS;
258 break;
259 case PCI_DEVFN(PWM1_DEV, PWM1_FUNC):
260 mask |= PWM1_DIS;
261 break;
262 case PCI_DEVFN(PWM2_DEV, PWM2_FUNC):
263 mask |= PWM2_DIS;
264 break;
265 case PCI_DEVFN(HSUART1_DEV, HSUART1_FUNC):
266 mask |= HSUART1_DIS;
267 break;
268 case PCI_DEVFN(HSUART2_DEV, HSUART2_FUNC):
269 mask |= HSUART2_DIS;
270 break;
271 case PCI_DEVFN(SPI_DEV, SPI_FUNC):
272 mask |= SPI_DIS;
273 break;
274 case PCI_DEVFN(SMBUS_DEV, SMBUS_FUNC):
275 mask2 |= SMBUS_DIS;
276 break;
277 }
278
279 if (mask != 0) {
280 write32(func_dis, read32(func_dis) | mask);
281 /* Ensure posted write hits. */
282 read32(func_dis);
283 }
284
285 if (mask2 != 0) {
286 write32(func_dis2, read32(func_dis2) | mask2);
287 /* Ensure posted write hits. */
288 read32(func_dis2);
289 }
290}
291
292static inline void set_d3hot_bits(device_t dev, int offset)
293{
294 uint32_t reg8;
295 printk(BIOS_DEBUG, "Power management CAP offset 0x%x.\n", offset);
296 reg8 = pci_read_config8(dev, offset + 4);
297 reg8 |= 0x3;
298 pci_write_config8(dev, offset + 4, reg8);
299}
300
Aaron Durbin46ab8cd2013-10-30 17:07:46 -0500301/* Parts of the audio subsystem are powered by the HDA device. Therefore, one
302 * cannot put HDA into D3Hot. Instead perform this workaround to make some of
303 * the audio paths work for LPE audio. */
304static void hda_work_around(device_t dev)
305{
306 unsigned long gctl = TEMP_BASE_ADDRESS + 0x8;
307
308 /* Need to set magic register 0x43 to 0xd7 in config space. */
309 pci_write_config8(dev, 0x43, 0xd7);
310
311 /* Need to set bit 0 of GCTL to take the device out of reset. However,
312 * that requires setting up the 64-bit BAR. */
313 pci_write_config32(dev, PCI_BASE_ADDRESS_0, TEMP_BASE_ADDRESS);
314 pci_write_config32(dev, PCI_BASE_ADDRESS_1, 0);
315 pci_write_config8(dev, PCI_COMMAND, PCI_COMMAND_MEMORY);
316 write32(gctl, read32(gctl) | 0x1);
317 pci_write_config8(dev, PCI_COMMAND, 0);
318 pci_write_config32(dev, PCI_BASE_ADDRESS_0, 0);
319}
320
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500321static int place_device_in_d3hot(device_t dev)
322{
323 unsigned offset;
324
Aaron Durbin46ab8cd2013-10-30 17:07:46 -0500325 /* Parts of the HDA block are used for LPE audio as well.
326 * Therefore assume the HDA will never be put into D3Hot. */
327 if (dev->path.pci.devfn == PCI_DEVFN(HDA_DEV, HDA_FUNC)) {
328 hda_work_around(dev);
329 return 0;
330 }
331
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500332 offset = pci_find_capability(dev, PCI_CAP_ID_PM);
333
334 if (offset != 0) {
335 set_d3hot_bits(dev, offset);
336 return 0;
337 }
338
339 /* For some reason some of the devices don't have the capability
340 * pointer set correctly. Work around this by hard coding the offset. */
341 switch (dev->path.pci.devfn) {
342 case PCI_DEVFN(SDIO_DEV, SDIO_FUNC):
343 offset = 0x80;
344 break;
345 case PCI_DEVFN(SD_DEV, SD_FUNC):
346 offset = 0x80;
347 break;
348 case PCI_DEVFN(MMC_DEV, MMC_FUNC):
349 offset = 0x80;
350 break;
351 case PCI_DEVFN(LPE_DEV, LPE_FUNC):
352 offset = 0x80;
353 break;
354 case PCI_DEVFN(SIO_DMA1_DEV, SIO_DMA1_FUNC):
355 offset = 0x80;
356 break;
357 case PCI_DEVFN(I2C1_DEV, I2C1_FUNC):
358 offset = 0x80;
359 break;
360 case PCI_DEVFN(I2C2_DEV, I2C2_FUNC):
361 offset = 0x80;
362 break;
363 case PCI_DEVFN(I2C3_DEV, I2C3_FUNC):
364 offset = 0x80;
365 break;
366 case PCI_DEVFN(I2C4_DEV, I2C4_FUNC):
367 offset = 0x80;
368 break;
369 case PCI_DEVFN(I2C5_DEV, I2C5_FUNC):
370 offset = 0x80;
371 break;
372 case PCI_DEVFN(I2C6_DEV, I2C6_FUNC):
373 offset = 0x80;
374 break;
375 case PCI_DEVFN(I2C7_DEV, I2C7_FUNC):
376 offset = 0x80;
377 break;
378 case PCI_DEVFN(SIO_DMA2_DEV, SIO_DMA2_FUNC):
379 offset = 0x80;
380 break;
381 case PCI_DEVFN(PWM1_DEV, PWM1_FUNC):
382 offset = 0x80;
383 break;
384 case PCI_DEVFN(PWM2_DEV, PWM2_FUNC):
385 offset = 0x80;
386 break;
387 case PCI_DEVFN(HSUART1_DEV, HSUART1_FUNC):
388 offset = 0x80;
389 break;
390 case PCI_DEVFN(HSUART2_DEV, HSUART2_FUNC):
391 offset = 0x80;
392 break;
393 case PCI_DEVFN(SPI_DEV, SPI_FUNC):
394 offset = 0x80;
395 break;
396 case PCI_DEVFN(SATA_DEV, SATA_FUNC):
397 offset = 0x70;
398 break;
399 case PCI_DEVFN(XHCI_DEV, XHCI_FUNC):
400 offset = 0x70;
401 break;
402 case PCI_DEVFN(EHCI_DEV, EHCI_FUNC):
403 offset = 0x70;
404 break;
405 case PCI_DEVFN(HDA_DEV, HDA_FUNC):
406 offset = 0x50;
407 break;
408 case PCI_DEVFN(SMBUS_DEV, SMBUS_FUNC):
409 offset = 0x50;
410 break;
411 case PCI_DEVFN(TXE_DEV, TXE_FUNC):
Aaron Durbin1eae3ee2013-10-30 17:08:59 -0500412 /* TXE cannot be placed in D3Hot. */
413 return 0;
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500414 case PCI_DEVFN(PCIE_PORT1_DEV, PCIE_PORT1_FUNC):
415 offset = 0xa0;
416 break;
417 case PCI_DEVFN(PCIE_PORT2_DEV, PCIE_PORT2_FUNC):
418 offset = 0xa0;
419 break;
420 case PCI_DEVFN(PCIE_PORT3_DEV, PCIE_PORT3_FUNC):
421 offset = 0xa0;
422 break;
423 case PCI_DEVFN(PCIE_PORT4_DEV, PCIE_PORT4_FUNC):
424 offset = 0xa0;
425 break;
426 }
427
428 if (offset != 0) {
429 set_d3hot_bits(dev, offset);
430 return 0;
431 }
432
433 return -1;
434}
435
436/* Common PCI device function disable. */
437void southcluster_enable_dev(device_t dev)
438{
439 uint32_t reg32;
440
441 if (!dev->enabled) {
442 int slot = PCI_SLOT(dev->path.pci.devfn);
443 int func = PCI_FUNC(dev->path.pci.devfn);
444 printk(BIOS_DEBUG, "%s: Disabling device: %02x.%01x\n",
445 dev_path(dev), slot, func);
446
447 /* Ensure memory, io, and bus master are all disabled */
448 reg32 = pci_read_config32(dev, PCI_COMMAND);
449 reg32 &= ~(PCI_COMMAND_MASTER |
450 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
451 pci_write_config32(dev, PCI_COMMAND, reg32);
452
453 /* Place device in D3Hot */
454 if (place_device_in_d3hot(dev) < 0) {
455 printk(BIOS_WARNING,
456 "Could not place %02x.%01x into D3Hot. "
457 "Keeping device visible.\n", slot, func);
458 return;
459 }
460 /* Disable this device if possible */
461 sc_disable_devfn(dev);
462 } else {
463 /* Enable SERR */
464 reg32 = pci_read_config32(dev, PCI_COMMAND);
465 reg32 |= PCI_COMMAND_SERR;
466 pci_write_config32(dev, PCI_COMMAND, reg32);
467 }
468}
469
Aaron Durbine18d68f2013-10-24 00:05:31 -0500470static struct device_operations device_ops = {
471 .read_resources = sc_read_resources,
472 .set_resources = pci_dev_set_resources,
473 .enable_resources = NULL,
Aaron Durbin3bde3d72013-11-04 21:45:52 -0600474 .init = sc_init,
Aaron Durbind7bc23a2013-10-29 16:37:10 -0500475 .enable = southcluster_enable_dev,
Duncan Laurie5d535542013-10-31 10:10:20 -0700476 .scan_bus = scan_static_bus,
Aaron Durbine18d68f2013-10-24 00:05:31 -0500477 .ops_pci = &soc_pci_ops,
478};
479
480static const struct pci_driver southcluster __pci_driver = {
481 .ops = &device_ops,
482 .vendor = PCI_VENDOR_ID_INTEL,
483 .device = LPC_DEVID,
484};