Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 1 | /* |
| 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älkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <stddef.h> |
| 18 | #include <console/console.h> |
| 19 | #include <device/pci_ehci.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 20 | #include <device/mmio.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 21 | #include <device/pci_ops.h> |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 22 | #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älkki | d07f377 | 2017-09-07 19:16:27 +0300 | [diff] [blame] | 29 | #if ENV_RAMSTAGE |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 30 | static struct device_operations *ehci_drv_ops; |
| 31 | static struct device_operations ehci_dbg_ops; |
| 32 | #endif |
| 33 | |
Kyösti Mälkki | 6f6a249 | 2014-02-09 19:21:30 +0200 | [diff] [blame] | 34 | int ehci_debug_hw_enable(unsigned int *base, unsigned int *dbg_offset) |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 35 | { |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 36 | pci_devfn_t dbg_dev = pci_ehci_dbg_dev(CONFIG_USBDEBUG_HCD_INDEX); |
Kyösti Mälkki | 6683e40 | 2017-07-30 13:23:32 +0300 | [diff] [blame] | 37 | |
Kyösti Mälkki | 5bd9264 | 2019-01-06 16:13:07 +0200 | [diff] [blame] | 38 | /* We only support controllers on bus 0. */ |
| 39 | if (PCI_DEV2SEGBUS(dbg_dev) != 0) |
| 40 | return -1; |
| 41 | |
Kyösti Mälkki | 6f6a249 | 2014-02-09 19:21:30 +0200 | [diff] [blame] | 42 | #ifdef __SIMPLE_DEVICE__ |
| 43 | pci_devfn_t dev = dbg_dev; |
| 44 | #else |
Kyösti Mälkki | e737755 | 2018-06-21 16:20:55 +0300 | [diff] [blame] | 45 | struct device *dev = pcidev_path_on_root(PCI_DEV2DEVFN(dbg_dev)); |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 46 | #endif |
Kyösti Mälkki | 6f6a249 | 2014-02-09 19:21:30 +0200 | [diff] [blame] | 47 | |
Kyösti Mälkki | 6683e40 | 2017-07-30 13:23:32 +0300 | [diff] [blame] | 48 | u32 class = pci_read_config32(dev, PCI_CLASS_REVISION) >> 8; |
| 49 | if (class != PCI_EHCI_CLASSCODE) |
| 50 | return -1; |
| 51 | |
Kyösti Mälkki | 9c0e14e | 2019-01-23 16:46:35 +0200 | [diff] [blame] | 52 | u8 pm_cap = pci_s_find_capability(dbg_dev, PCI_CAP_ID_PM); |
Kyösti Mälkki | 6aea6f7 | 2017-07-30 09:01:24 +0300 | [diff] [blame] | 53 | if (pm_cap) { |
| 54 | u16 pm_ctrl = pci_read_config16(dev, pm_cap + PCI_PM_CTRL); |
| 55 | /* Set to D0 and disable PM events. */ |
| 56 | pm_ctrl &= ~PCI_PM_CTRL_PME_ENABLE; |
| 57 | pm_ctrl &= ~PCI_PM_CTRL_STATE_MASK; |
| 58 | pci_write_config16(dev, pm_cap + PCI_PM_CTRL, pm_ctrl); |
| 59 | } |
| 60 | |
Kyösti Mälkki | 9c0e14e | 2019-01-23 16:46:35 +0200 | [diff] [blame] | 61 | u8 pos = pci_s_find_capability(dbg_dev, PCI_CAP_ID_EHCI_DEBUG); |
Kyösti Mälkki | 6f6a249 | 2014-02-09 19:21:30 +0200 | [diff] [blame] | 62 | if (!pos) |
| 63 | return -1; |
| 64 | |
| 65 | u32 cap = pci_read_config32(dev, pos); |
| 66 | |
| 67 | /* FIXME: We should remove static EHCI_BAR_INDEX. */ |
Kyösti Mälkki | d1a0c57 | 2017-07-30 11:37:14 +0300 | [diff] [blame] | 68 | u8 ehci_bar = 0x10 + 4 * ((cap >> 29) - 1); |
| 69 | if (ehci_bar != EHCI_BAR_INDEX) |
Kyösti Mälkki | 6f6a249 | 2014-02-09 19:21:30 +0200 | [diff] [blame] | 70 | return -1; |
| 71 | |
Kyösti Mälkki | d1a0c57 | 2017-07-30 11:37:14 +0300 | [diff] [blame] | 72 | pci_write_config32(dev, ehci_bar, CONFIG_EHCI_BAR); |
| 73 | |
| 74 | pci_write_config8(dev, PCI_COMMAND, PCI_COMMAND_MEMORY | |
| 75 | PCI_COMMAND_MASTER); |
| 76 | |
Kyösti Mälkki | 6f6a249 | 2014-02-09 19:21:30 +0200 | [diff] [blame] | 77 | *base = CONFIG_EHCI_BAR; |
| 78 | *dbg_offset = (cap>>16) & 0x1ffc; |
Kyösti Mälkki | d1a0c57 | 2017-07-30 11:37:14 +0300 | [diff] [blame] | 79 | |
Kyösti Mälkki | 6f6a249 | 2014-02-09 19:21:30 +0200 | [diff] [blame] | 80 | return 0; |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void ehci_debug_select_port(unsigned int port) |
| 84 | { |
| 85 | pci_devfn_t dbg_dev = pci_ehci_dbg_dev(CONFIG_USBDEBUG_HCD_INDEX); |
| 86 | pci_ehci_dbg_set_port(dbg_dev, port); |
| 87 | } |
| 88 | |
Kyösti Mälkki | d07f377 | 2017-09-07 19:16:27 +0300 | [diff] [blame] | 89 | #if ENV_RAMSTAGE |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 90 | static void pci_ehci_set_resources(struct device *dev) |
| 91 | { |
| 92 | struct resource *res; |
| 93 | |
| 94 | printk(BIOS_DEBUG, "%s EHCI Debug Port hook triggered\n", dev_path(dev)); |
| 95 | usbdebug_disable(); |
| 96 | |
| 97 | if (ehci_drv_ops->set_resources) |
| 98 | ehci_drv_ops->set_resources(dev); |
| 99 | res = find_resource(dev, EHCI_BAR_INDEX); |
| 100 | if (!res) |
| 101 | return; |
| 102 | |
| 103 | usbdebug_re_enable((u32)res->base); |
| 104 | report_resource_stored(dev, res, ""); |
| 105 | printk(BIOS_DEBUG, "%s EHCI Debug Port relocated\n", dev_path(dev)); |
| 106 | } |
| 107 | |
| 108 | void pci_ehci_read_resources(struct device *dev) |
| 109 | { |
| 110 | pci_devfn_t dbg_dev = pci_ehci_dbg_dev(CONFIG_USBDEBUG_HCD_INDEX); |
| 111 | |
| 112 | if (!ehci_drv_ops && pci_match_simple_dev(dev, dbg_dev)) { |
| 113 | memcpy(&ehci_dbg_ops, dev->ops, sizeof(ehci_dbg_ops)); |
| 114 | ehci_drv_ops = dev->ops; |
| 115 | ehci_dbg_ops.set_resources = pci_ehci_set_resources; |
| 116 | dev->ops = &ehci_dbg_ops; |
| 117 | printk(BIOS_DEBUG, "%s EHCI BAR hook registered\n", dev_path(dev)); |
| 118 | } else { |
| 119 | printk(BIOS_DEBUG, "More than one caller of %s from %s\n", __func__, dev_path(dev)); |
| 120 | } |
| 121 | |
| 122 | pci_dev_read_resources(dev); |
| 123 | } |
| 124 | #endif |
| 125 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 126 | u8 *pci_ehci_base_regs(pci_devfn_t sdev) |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 127 | { |
| 128 | #ifdef __SIMPLE_DEVICE__ |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 129 | u8 *base = (u8 *)(pci_read_config32(sdev, EHCI_BAR_INDEX) & ~0x0f); |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 130 | #else |
Kyösti Mälkki | e737755 | 2018-06-21 16:20:55 +0300 | [diff] [blame] | 131 | struct device *dev = pcidev_path_on_root(PCI_DEV2DEVFN(sdev)); |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 132 | u8 *base = (u8 *)(pci_read_config32(dev, EHCI_BAR_INDEX) & ~0x0f); |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 133 | #endif |
| 134 | return base + HC_LENGTH(read32(base)); |
| 135 | } |