Nico Huber | b0f8326 | 2014-01-01 20:47:55 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2014 Nico Huber <nico.h@gmx.de> |
| 3 | * |
| 4 | * Code borrowed from pci_early.c: |
| 5 | * Copyright (C) 2011 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 <device/pci_ehci.h> |
| 22 | |
| 23 | static unsigned pci_find_next_capability(pci_devfn_t dev, unsigned cap, unsigned last) |
| 24 | { |
| 25 | unsigned pos = 0; |
| 26 | u16 status; |
| 27 | unsigned reps = 48; |
| 28 | |
| 29 | status = pci_read_config16(dev, PCI_STATUS); |
| 30 | if (!(status & PCI_STATUS_CAP_LIST)) |
| 31 | return 0; |
| 32 | |
| 33 | u8 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE); |
| 34 | switch (hdr_type & 0x7f) { |
| 35 | case PCI_HEADER_TYPE_NORMAL: |
| 36 | case PCI_HEADER_TYPE_BRIDGE: |
| 37 | pos = PCI_CAPABILITY_LIST; |
| 38 | break; |
| 39 | case PCI_HEADER_TYPE_CARDBUS: |
| 40 | pos = PCI_CB_CAPABILITY_LIST; |
| 41 | break; |
| 42 | default: |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | pos = pci_read_config8(dev, pos); |
| 47 | while (reps-- && (pos >= 0x40)) { /* Loop through the linked list. */ |
| 48 | unsigned this_cap; |
| 49 | |
| 50 | pos &= ~3; |
| 51 | this_cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID); |
| 52 | if (this_cap == 0xff) |
| 53 | break; |
| 54 | |
| 55 | if (!last && (this_cap == cap)) |
| 56 | return pos; |
| 57 | |
| 58 | if (last == pos) |
| 59 | last = 0; |
| 60 | |
| 61 | pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT); |
| 62 | } |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static unsigned pci_find_capability(pci_devfn_t dev, unsigned cap) |
| 67 | { |
| 68 | return pci_find_next_capability(dev, cap, 0); |
| 69 | } |
| 70 | |
| 71 | extern void *ehci_bar; |
| 72 | int ehci_debug_hw_enable(unsigned int *base, unsigned int *dbg_offset) |
| 73 | { |
| 74 | pci_devfn_t dbg_dev = pci_ehci_dbg_dev(CONFIG_USBDEBUG_HCD_INDEX); |
| 75 | pci_ehci_dbg_enable(dbg_dev, CONFIG_EHCI_BAR); |
| 76 | pci_devfn_t dev = dbg_dev; |
| 77 | |
| 78 | u8 pos = pci_find_capability(dev, PCI_CAP_ID_EHCI_DEBUG); |
| 79 | if (!pos) |
| 80 | return -1; |
| 81 | |
| 82 | u32 cap = pci_read_config32(dev, pos); |
| 83 | |
| 84 | /* FIXME: We should remove static EHCI_BAR_INDEX. */ |
| 85 | u8 dbg_bar = 0x10 + 4 * ((cap >> 29) - 1); |
| 86 | if (dbg_bar != EHCI_BAR_INDEX) |
| 87 | return -1; |
| 88 | |
| 89 | *base = (u32)ehci_bar; |
| 90 | *dbg_offset = (cap>>16) & 0x1ffc; |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | void ehci_debug_select_port(unsigned int port) |
| 95 | { |
| 96 | pci_devfn_t dbg_dev = pci_ehci_dbg_dev(CONFIG_USBDEBUG_HCD_INDEX); |
| 97 | pci_ehci_dbg_set_port(dbg_dev, port); |
| 98 | } |