blob: c05129163ebca30e73b659a70aa84ff60281b369 [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>
20#include <arch/io.h>
21#include <device/pci.h>
22#include <device/pci_def.h>
23#include <string.h>
24
25#include "ehci_debug.h"
26#include "ehci.h"
27
Kyösti Mälkkid07f3772017-09-07 19:16:27 +030028#if ENV_RAMSTAGE
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +020029static struct device_operations *ehci_drv_ops;
30static struct device_operations ehci_dbg_ops;
31#endif
32
Kyösti Mälkki6f6a2492014-02-09 19:21:30 +020033int ehci_debug_hw_enable(unsigned int *base, unsigned int *dbg_offset)
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +020034{
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +020035 pci_devfn_t dbg_dev = pci_ehci_dbg_dev(CONFIG_USBDEBUG_HCD_INDEX);
Kyösti Mälkki6683e402017-07-30 13:23:32 +030036
Kyösti Mälkki5bd92642019-01-06 16:13:07 +020037 /* We only support controllers on bus 0. */
38 if (PCI_DEV2SEGBUS(dbg_dev) != 0)
39 return -1;
40
Kyösti Mälkki6f6a2492014-02-09 19:21:30 +020041#ifdef __SIMPLE_DEVICE__
42 pci_devfn_t dev = dbg_dev;
43#else
Kyösti Mälkkie7377552018-06-21 16:20:55 +030044 struct device *dev = pcidev_path_on_root(PCI_DEV2DEVFN(dbg_dev));
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +020045#endif
Kyösti Mälkki6f6a2492014-02-09 19:21:30 +020046
Kyösti Mälkki6683e402017-07-30 13:23:32 +030047 u32 class = pci_read_config32(dev, PCI_CLASS_REVISION) >> 8;
48 if (class != PCI_EHCI_CLASSCODE)
49 return -1;
50
Kyösti Mälkki6aea6f72017-07-30 09:01:24 +030051 u8 pm_cap = pci_find_capability(dev, PCI_CAP_ID_PM);
52 if (pm_cap) {
53 u16 pm_ctrl = pci_read_config16(dev, pm_cap + PCI_PM_CTRL);
54 /* Set to D0 and disable PM events. */
55 pm_ctrl &= ~PCI_PM_CTRL_PME_ENABLE;
56 pm_ctrl &= ~PCI_PM_CTRL_STATE_MASK;
57 pci_write_config16(dev, pm_cap + PCI_PM_CTRL, pm_ctrl);
58 }
59
Kyösti Mälkki6f6a2492014-02-09 19:21:30 +020060 u8 pos = pci_find_capability(dev, PCI_CAP_ID_EHCI_DEBUG);
61 if (!pos)
62 return -1;
63
64 u32 cap = pci_read_config32(dev, pos);
65
66 /* FIXME: We should remove static EHCI_BAR_INDEX. */
Kyösti Mälkkid1a0c572017-07-30 11:37:14 +030067 u8 ehci_bar = 0x10 + 4 * ((cap >> 29) - 1);
68 if (ehci_bar != EHCI_BAR_INDEX)
Kyösti Mälkki6f6a2492014-02-09 19:21:30 +020069 return -1;
70
Kyösti Mälkkid1a0c572017-07-30 11:37:14 +030071 pci_write_config32(dev, ehci_bar, CONFIG_EHCI_BAR);
72
73 pci_write_config8(dev, PCI_COMMAND, PCI_COMMAND_MEMORY |
74 PCI_COMMAND_MASTER);
75
Kyösti Mälkki6f6a2492014-02-09 19:21:30 +020076 *base = CONFIG_EHCI_BAR;
77 *dbg_offset = (cap>>16) & 0x1ffc;
Kyösti Mälkkid1a0c572017-07-30 11:37:14 +030078
Kyösti Mälkki6f6a2492014-02-09 19:21:30 +020079 return 0;
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +020080}
81
82void ehci_debug_select_port(unsigned int port)
83{
84 pci_devfn_t dbg_dev = pci_ehci_dbg_dev(CONFIG_USBDEBUG_HCD_INDEX);
85 pci_ehci_dbg_set_port(dbg_dev, port);
86}
87
Kyösti Mälkkid07f3772017-09-07 19:16:27 +030088#if ENV_RAMSTAGE
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +020089static void pci_ehci_set_resources(struct device *dev)
90{
91 struct resource *res;
92
93 printk(BIOS_DEBUG, "%s EHCI Debug Port hook triggered\n", dev_path(dev));
94 usbdebug_disable();
95
96 if (ehci_drv_ops->set_resources)
97 ehci_drv_ops->set_resources(dev);
98 res = find_resource(dev, EHCI_BAR_INDEX);
99 if (!res)
100 return;
101
102 usbdebug_re_enable((u32)res->base);
103 report_resource_stored(dev, res, "");
104 printk(BIOS_DEBUG, "%s EHCI Debug Port relocated\n", dev_path(dev));
105}
106
107void pci_ehci_read_resources(struct device *dev)
108{
109 pci_devfn_t dbg_dev = pci_ehci_dbg_dev(CONFIG_USBDEBUG_HCD_INDEX);
110
111 if (!ehci_drv_ops && pci_match_simple_dev(dev, dbg_dev)) {
112 memcpy(&ehci_dbg_ops, dev->ops, sizeof(ehci_dbg_ops));
113 ehci_drv_ops = dev->ops;
114 ehci_dbg_ops.set_resources = pci_ehci_set_resources;
115 dev->ops = &ehci_dbg_ops;
116 printk(BIOS_DEBUG, "%s EHCI BAR hook registered\n", dev_path(dev));
117 } else {
118 printk(BIOS_DEBUG, "More than one caller of %s from %s\n", __func__, dev_path(dev));
119 }
120
121 pci_dev_read_resources(dev);
122}
123#endif
124
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800125u8 *pci_ehci_base_regs(pci_devfn_t sdev)
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +0200126{
127#ifdef __SIMPLE_DEVICE__
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800128 u8 *base = (u8 *)(pci_read_config32(sdev, EHCI_BAR_INDEX) & ~0x0f);
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +0200129#else
Kyösti Mälkkie7377552018-06-21 16:20:55 +0300130 struct device *dev = pcidev_path_on_root(PCI_DEV2DEVFN(sdev));
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800131 u8 *base = (u8 *)(pci_read_config32(dev, EHCI_BAR_INDEX) & ~0x0f);
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +0200132#endif
133 return base + HC_LENGTH(read32(base));
134}