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