blob: 1e755249b1c9a382558d3c6e63867fbd4fae3e57 [file] [log] [blame]
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2006 Eric Biederman (ebiederm@xmission.com)
5 * Copyright (C) 2007 AMD
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.
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +020015 */
16
17#include <stddef.h>
18#include <console/console.h>
19#include <device/pci_ehci.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020020#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020021#include <device/pci_ops.h>
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +020022#include <device/pci.h>
23#include <device/pci_def.h>
24#include <string.h>
25
26#include "ehci_debug.h"
27#include "ehci.h"
28
Kyösti Mälkkid07f3772017-09-07 19:16:27 +030029#if ENV_RAMSTAGE
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +020030static struct device_operations *ehci_drv_ops;
31static struct device_operations ehci_dbg_ops;
32#endif
33
Kyösti Mälkki6f6a2492014-02-09 19:21:30 +020034int ehci_debug_hw_enable(unsigned int *base, unsigned int *dbg_offset)
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +020035{
Kyösti Mälkkif2cc3dd2019-09-27 18:09:52 +030036 pci_devfn_t dev = pci_ehci_dbg_dev(CONFIG_USBDEBUG_HCD_INDEX);
Kyösti Mälkki6683e402017-07-30 13:23:32 +030037
Kyösti Mälkki5bd92642019-01-06 16:13:07 +020038 /* We only support controllers on bus 0. */
Kyösti Mälkkif2cc3dd2019-09-27 18:09:52 +030039 if (PCI_DEV2SEGBUS(dev) != 0)
Kyösti Mälkki5bd92642019-01-06 16:13:07 +020040 return -1;
41
Kyösti Mälkkif2cc3dd2019-09-27 18:09:52 +030042 u32 class = pci_s_read_config32(dev, PCI_CLASS_REVISION) >> 8;
Kyösti Mälkki6683e402017-07-30 13:23:32 +030043 if (class != PCI_EHCI_CLASSCODE)
44 return -1;
45
Kyösti Mälkkif2cc3dd2019-09-27 18:09:52 +030046 u8 pm_cap = pci_s_find_capability(dev, PCI_CAP_ID_PM);
Kyösti Mälkki6aea6f72017-07-30 09:01:24 +030047 if (pm_cap) {
Kyösti Mälkkif2cc3dd2019-09-27 18:09:52 +030048 u16 pm_ctrl = pci_s_read_config16(dev, pm_cap + PCI_PM_CTRL);
Kyösti Mälkki6aea6f72017-07-30 09:01:24 +030049 /* Set to D0 and disable PM events. */
50 pm_ctrl &= ~PCI_PM_CTRL_PME_ENABLE;
51 pm_ctrl &= ~PCI_PM_CTRL_STATE_MASK;
Kyösti Mälkkif2cc3dd2019-09-27 18:09:52 +030052 pci_s_write_config16(dev, pm_cap + PCI_PM_CTRL, pm_ctrl);
Kyösti Mälkki6aea6f72017-07-30 09:01:24 +030053 }
54
Kyösti Mälkkif2cc3dd2019-09-27 18:09:52 +030055 u8 pos = pci_s_find_capability(dev, PCI_CAP_ID_EHCI_DEBUG);
Kyösti Mälkki6f6a2492014-02-09 19:21:30 +020056 if (!pos)
57 return -1;
58
Kyösti Mälkkif2cc3dd2019-09-27 18:09:52 +030059 u32 cap = pci_s_read_config32(dev, pos);
Kyösti Mälkki6f6a2492014-02-09 19:21:30 +020060
61 /* FIXME: We should remove static EHCI_BAR_INDEX. */
Kyösti Mälkkid1a0c572017-07-30 11:37:14 +030062 u8 ehci_bar = 0x10 + 4 * ((cap >> 29) - 1);
63 if (ehci_bar != EHCI_BAR_INDEX)
Kyösti Mälkki6f6a2492014-02-09 19:21:30 +020064 return -1;
65
Kyösti Mälkkif2cc3dd2019-09-27 18:09:52 +030066 pci_s_write_config32(dev, ehci_bar, CONFIG_EHCI_BAR);
Kyösti Mälkkid1a0c572017-07-30 11:37:14 +030067
Kyösti Mälkkif2cc3dd2019-09-27 18:09:52 +030068 pci_s_write_config8(dev, PCI_COMMAND, PCI_COMMAND_MEMORY |
Kyösti Mälkkid1a0c572017-07-30 11:37:14 +030069 PCI_COMMAND_MASTER);
70
Kyösti Mälkki6f6a2492014-02-09 19:21:30 +020071 *base = CONFIG_EHCI_BAR;
72 *dbg_offset = (cap>>16) & 0x1ffc;
Kyösti Mälkkid1a0c572017-07-30 11:37:14 +030073
Kyösti Mälkki6f6a2492014-02-09 19:21:30 +020074 return 0;
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +020075}
76
77void ehci_debug_select_port(unsigned int port)
78{
79 pci_devfn_t dbg_dev = pci_ehci_dbg_dev(CONFIG_USBDEBUG_HCD_INDEX);
80 pci_ehci_dbg_set_port(dbg_dev, port);
81}
82
Kyösti Mälkkid07f3772017-09-07 19:16:27 +030083#if ENV_RAMSTAGE
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +020084static void pci_ehci_set_resources(struct device *dev)
85{
86 struct resource *res;
87
88 printk(BIOS_DEBUG, "%s EHCI Debug Port hook triggered\n", dev_path(dev));
89 usbdebug_disable();
90
91 if (ehci_drv_ops->set_resources)
92 ehci_drv_ops->set_resources(dev);
93 res = find_resource(dev, EHCI_BAR_INDEX);
94 if (!res)
95 return;
96
97 usbdebug_re_enable((u32)res->base);
98 report_resource_stored(dev, res, "");
99 printk(BIOS_DEBUG, "%s EHCI Debug Port relocated\n", dev_path(dev));
100}
101
102void pci_ehci_read_resources(struct device *dev)
103{
104 pci_devfn_t dbg_dev = pci_ehci_dbg_dev(CONFIG_USBDEBUG_HCD_INDEX);
105
106 if (!ehci_drv_ops && pci_match_simple_dev(dev, dbg_dev)) {
107 memcpy(&ehci_dbg_ops, dev->ops, sizeof(ehci_dbg_ops));
108 ehci_drv_ops = dev->ops;
109 ehci_dbg_ops.set_resources = pci_ehci_set_resources;
110 dev->ops = &ehci_dbg_ops;
111 printk(BIOS_DEBUG, "%s EHCI BAR hook registered\n", dev_path(dev));
112 } else {
113 printk(BIOS_DEBUG, "More than one caller of %s from %s\n", __func__, dev_path(dev));
114 }
115
116 pci_dev_read_resources(dev);
117}
118#endif
119
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800120u8 *pci_ehci_base_regs(pci_devfn_t sdev)
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +0200121{
Kyösti Mälkkif2cc3dd2019-09-27 18:09:52 +0300122 u8 *base = (u8 *)(pci_s_read_config32(sdev, EHCI_BAR_INDEX) & ~0x0f);
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +0200123 return base + HC_LENGTH(read32(base));
124}