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