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 | * |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <stddef.h> |
| 15 | #include <console/console.h> |
| 16 | #include <device/pci_ehci.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 17 | #include <device/mmio.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 18 | #include <device/pci_ops.h> |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 19 | #include <device/pci.h> |
| 20 | #include <device/pci_def.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include "ehci_debug.h" |
| 24 | #include "ehci.h" |
| 25 | |
Kyösti Mälkki | d07f377 | 2017-09-07 19:16:27 +0300 | [diff] [blame] | 26 | #if ENV_RAMSTAGE |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 27 | static struct device_operations *ehci_drv_ops; |
| 28 | static struct device_operations ehci_dbg_ops; |
| 29 | #endif |
| 30 | |
Kyösti Mälkki | 6f6a249 | 2014-02-09 19:21:30 +0200 | [diff] [blame] | 31 | 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] | 32 | { |
Kyösti Mälkki | f2cc3dd | 2019-09-27 18:09:52 +0300 | [diff] [blame] | 33 | pci_devfn_t dev = pci_ehci_dbg_dev(CONFIG_USBDEBUG_HCD_INDEX); |
Kyösti Mälkki | 6683e40 | 2017-07-30 13:23:32 +0300 | [diff] [blame] | 34 | |
Kyösti Mälkki | 5bd9264 | 2019-01-06 16:13:07 +0200 | [diff] [blame] | 35 | /* We only support controllers on bus 0. */ |
Kyösti Mälkki | f2cc3dd | 2019-09-27 18:09:52 +0300 | [diff] [blame] | 36 | if (PCI_DEV2SEGBUS(dev) != 0) |
Kyösti Mälkki | 5bd9264 | 2019-01-06 16:13:07 +0200 | [diff] [blame] | 37 | return -1; |
| 38 | |
Kyösti Mälkki | f2cc3dd | 2019-09-27 18:09:52 +0300 | [diff] [blame] | 39 | u32 class = pci_s_read_config32(dev, PCI_CLASS_REVISION) >> 8; |
Kyösti Mälkki | 6683e40 | 2017-07-30 13:23:32 +0300 | [diff] [blame] | 40 | if (class != PCI_EHCI_CLASSCODE) |
| 41 | return -1; |
| 42 | |
Kyösti Mälkki | f2cc3dd | 2019-09-27 18:09:52 +0300 | [diff] [blame] | 43 | u8 pm_cap = pci_s_find_capability(dev, PCI_CAP_ID_PM); |
Kyösti Mälkki | 6aea6f7 | 2017-07-30 09:01:24 +0300 | [diff] [blame] | 44 | if (pm_cap) { |
Kyösti Mälkki | f2cc3dd | 2019-09-27 18:09:52 +0300 | [diff] [blame] | 45 | u16 pm_ctrl = pci_s_read_config16(dev, pm_cap + PCI_PM_CTRL); |
Kyösti Mälkki | 6aea6f7 | 2017-07-30 09:01:24 +0300 | [diff] [blame] | 46 | /* Set to D0 and disable PM events. */ |
| 47 | pm_ctrl &= ~PCI_PM_CTRL_PME_ENABLE; |
| 48 | pm_ctrl &= ~PCI_PM_CTRL_STATE_MASK; |
Kyösti Mälkki | f2cc3dd | 2019-09-27 18:09:52 +0300 | [diff] [blame] | 49 | pci_s_write_config16(dev, pm_cap + PCI_PM_CTRL, pm_ctrl); |
Kyösti Mälkki | 6aea6f7 | 2017-07-30 09:01:24 +0300 | [diff] [blame] | 50 | } |
| 51 | |
Kyösti Mälkki | f2cc3dd | 2019-09-27 18:09:52 +0300 | [diff] [blame] | 52 | u8 pos = pci_s_find_capability(dev, PCI_CAP_ID_EHCI_DEBUG); |
Kyösti Mälkki | 6f6a249 | 2014-02-09 19:21:30 +0200 | [diff] [blame] | 53 | if (!pos) |
| 54 | return -1; |
| 55 | |
Kyösti Mälkki | f2cc3dd | 2019-09-27 18:09:52 +0300 | [diff] [blame] | 56 | u32 cap = pci_s_read_config32(dev, pos); |
Kyösti Mälkki | 6f6a249 | 2014-02-09 19:21:30 +0200 | [diff] [blame] | 57 | |
| 58 | /* FIXME: We should remove static EHCI_BAR_INDEX. */ |
Kyösti Mälkki | d1a0c57 | 2017-07-30 11:37:14 +0300 | [diff] [blame] | 59 | u8 ehci_bar = 0x10 + 4 * ((cap >> 29) - 1); |
| 60 | if (ehci_bar != EHCI_BAR_INDEX) |
Kyösti Mälkki | 6f6a249 | 2014-02-09 19:21:30 +0200 | [diff] [blame] | 61 | return -1; |
| 62 | |
Kyösti Mälkki | f2cc3dd | 2019-09-27 18:09:52 +0300 | [diff] [blame] | 63 | pci_s_write_config32(dev, ehci_bar, CONFIG_EHCI_BAR); |
Kyösti Mälkki | d1a0c57 | 2017-07-30 11:37:14 +0300 | [diff] [blame] | 64 | |
Kyösti Mälkki | f2cc3dd | 2019-09-27 18:09:52 +0300 | [diff] [blame] | 65 | pci_s_write_config8(dev, PCI_COMMAND, PCI_COMMAND_MEMORY | |
Kyösti Mälkki | d1a0c57 | 2017-07-30 11:37:14 +0300 | [diff] [blame] | 66 | PCI_COMMAND_MASTER); |
| 67 | |
Kyösti Mälkki | 6f6a249 | 2014-02-09 19:21:30 +0200 | [diff] [blame] | 68 | *base = CONFIG_EHCI_BAR; |
| 69 | *dbg_offset = (cap>>16) & 0x1ffc; |
Kyösti Mälkki | d1a0c57 | 2017-07-30 11:37:14 +0300 | [diff] [blame] | 70 | |
Kyösti Mälkki | 6f6a249 | 2014-02-09 19:21:30 +0200 | [diff] [blame] | 71 | return 0; |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void ehci_debug_select_port(unsigned int port) |
| 75 | { |
| 76 | pci_devfn_t dbg_dev = pci_ehci_dbg_dev(CONFIG_USBDEBUG_HCD_INDEX); |
| 77 | pci_ehci_dbg_set_port(dbg_dev, port); |
| 78 | } |
| 79 | |
Kyösti Mälkki | d07f377 | 2017-09-07 19:16:27 +0300 | [diff] [blame] | 80 | #if ENV_RAMSTAGE |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 81 | static void pci_ehci_set_resources(struct device *dev) |
| 82 | { |
| 83 | struct resource *res; |
| 84 | |
| 85 | printk(BIOS_DEBUG, "%s EHCI Debug Port hook triggered\n", dev_path(dev)); |
| 86 | usbdebug_disable(); |
| 87 | |
| 88 | if (ehci_drv_ops->set_resources) |
| 89 | ehci_drv_ops->set_resources(dev); |
| 90 | res = find_resource(dev, EHCI_BAR_INDEX); |
| 91 | if (!res) |
| 92 | return; |
| 93 | |
| 94 | usbdebug_re_enable((u32)res->base); |
| 95 | report_resource_stored(dev, res, ""); |
| 96 | printk(BIOS_DEBUG, "%s EHCI Debug Port relocated\n", dev_path(dev)); |
| 97 | } |
| 98 | |
| 99 | void pci_ehci_read_resources(struct device *dev) |
| 100 | { |
| 101 | pci_devfn_t dbg_dev = pci_ehci_dbg_dev(CONFIG_USBDEBUG_HCD_INDEX); |
| 102 | |
| 103 | if (!ehci_drv_ops && pci_match_simple_dev(dev, dbg_dev)) { |
| 104 | memcpy(&ehci_dbg_ops, dev->ops, sizeof(ehci_dbg_ops)); |
| 105 | ehci_drv_ops = dev->ops; |
| 106 | ehci_dbg_ops.set_resources = pci_ehci_set_resources; |
| 107 | dev->ops = &ehci_dbg_ops; |
| 108 | printk(BIOS_DEBUG, "%s EHCI BAR hook registered\n", dev_path(dev)); |
| 109 | } else { |
| 110 | printk(BIOS_DEBUG, "More than one caller of %s from %s\n", __func__, dev_path(dev)); |
| 111 | } |
| 112 | |
| 113 | pci_dev_read_resources(dev); |
| 114 | } |
| 115 | #endif |
| 116 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 117 | u8 *pci_ehci_base_regs(pci_devfn_t sdev) |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 118 | { |
Kyösti Mälkki | f2cc3dd | 2019-09-27 18:09:52 +0300 | [diff] [blame] | 119 | u8 *base = (u8 *)(pci_s_read_config32(sdev, EHCI_BAR_INDEX) & ~0x0f); |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 120 | return base + HC_LENGTH(read32(base)); |
| 121 | } |