Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Simon Glass | 40ffb68 | 2015-01-27 22:13:31 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015, Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | * All rights reserved. |
Simon Glass | 40ffb68 | 2015-01-27 22:13:31 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Stefan Roese | b0ff2c1 | 2016-07-18 12:51:39 +0200 | [diff] [blame] | 9 | #include <dm.h> |
Samuel Holland | 386d128 | 2021-07-05 13:29:02 +0100 | [diff] [blame] | 10 | #include <dm/device_compat.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 11 | #include <init.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 40ffb68 | 2015-01-27 22:13:31 -0700 | [diff] [blame] | 13 | #include <pci.h> |
Samuel Holland | 386d128 | 2021-07-05 13:29:02 +0100 | [diff] [blame] | 14 | #include <reset.h> |
Simon Glass | 40ffb68 | 2015-01-27 22:13:31 -0700 | [diff] [blame] | 15 | #include <usb.h> |
Jean-Jacques Hiblot | ad4142b | 2019-09-11 11:33:46 +0200 | [diff] [blame] | 16 | #include <usb/xhci.h> |
Simon Glass | 40ffb68 | 2015-01-27 22:13:31 -0700 | [diff] [blame] | 17 | |
Samuel Holland | 386d128 | 2021-07-05 13:29:02 +0100 | [diff] [blame] | 18 | struct xhci_pci_plat { |
| 19 | struct reset_ctl reset; |
| 20 | }; |
| 21 | |
Pali Rohár | de1627b | 2021-01-18 12:30:04 +0100 | [diff] [blame] | 22 | static int xhci_pci_init(struct udevice *dev, struct xhci_hccr **ret_hccr, |
| 23 | struct xhci_hcor **ret_hcor) |
Stefan Roese | b0ff2c1 | 2016-07-18 12:51:39 +0200 | [diff] [blame] | 24 | { |
| 25 | struct xhci_hccr *hccr; |
| 26 | struct xhci_hcor *hcor; |
| 27 | u32 cmd; |
| 28 | |
| 29 | hccr = (struct xhci_hccr *)dm_pci_map_bar(dev, |
Andrew Scull | 6520c82 | 2022-04-21 16:11:13 +0000 | [diff] [blame] | 30 | PCI_BASE_ADDRESS_0, 0, 0, PCI_REGION_TYPE, |
| 31 | PCI_REGION_MEM); |
Pali Rohár | de1627b | 2021-01-18 12:30:04 +0100 | [diff] [blame] | 32 | if (!hccr) { |
| 33 | printf("xhci-pci init cannot map PCI mem bar\n"); |
| 34 | return -EIO; |
| 35 | } |
| 36 | |
Stefan Roese | b0ff2c1 | 2016-07-18 12:51:39 +0200 | [diff] [blame] | 37 | hcor = (struct xhci_hcor *)((uintptr_t) hccr + |
| 38 | HC_LENGTH(xhci_readl(&hccr->cr_capbase))); |
| 39 | |
Bin Meng | 7380f7a | 2018-06-03 19:04:14 -0700 | [diff] [blame] | 40 | debug("XHCI-PCI init hccr %p and hcor %p hc_length %d\n", |
| 41 | hccr, hcor, (u32)HC_LENGTH(xhci_readl(&hccr->cr_capbase))); |
Stefan Roese | b0ff2c1 | 2016-07-18 12:51:39 +0200 | [diff] [blame] | 42 | |
| 43 | *ret_hccr = hccr; |
| 44 | *ret_hcor = hcor; |
| 45 | |
| 46 | /* enable busmaster */ |
| 47 | dm_pci_read_config32(dev, PCI_COMMAND, &cmd); |
| 48 | cmd |= PCI_COMMAND_MASTER; |
| 49 | dm_pci_write_config32(dev, PCI_COMMAND, cmd); |
Pali Rohár | de1627b | 2021-01-18 12:30:04 +0100 | [diff] [blame] | 50 | return 0; |
Stefan Roese | b0ff2c1 | 2016-07-18 12:51:39 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static int xhci_pci_probe(struct udevice *dev) |
| 54 | { |
Samuel Holland | 386d128 | 2021-07-05 13:29:02 +0100 | [diff] [blame] | 55 | struct xhci_pci_plat *plat = dev_get_plat(dev); |
Stefan Roese | b0ff2c1 | 2016-07-18 12:51:39 +0200 | [diff] [blame] | 56 | struct xhci_hccr *hccr; |
| 57 | struct xhci_hcor *hcor; |
Pali Rohár | de1627b | 2021-01-18 12:30:04 +0100 | [diff] [blame] | 58 | int ret; |
Stefan Roese | b0ff2c1 | 2016-07-18 12:51:39 +0200 | [diff] [blame] | 59 | |
Samuel Holland | 386d128 | 2021-07-05 13:29:02 +0100 | [diff] [blame] | 60 | ret = reset_get_by_index(dev, 0, &plat->reset); |
| 61 | if (ret && ret != -ENOENT && ret != -ENOTSUPP) { |
| 62 | dev_err(dev, "failed to get reset\n"); |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | if (reset_valid(&plat->reset)) { |
| 67 | ret = reset_assert(&plat->reset); |
| 68 | if (ret) |
| 69 | goto err_reset; |
| 70 | |
| 71 | ret = reset_deassert(&plat->reset); |
| 72 | if (ret) |
| 73 | goto err_reset; |
| 74 | } |
| 75 | |
Pali Rohár | de1627b | 2021-01-18 12:30:04 +0100 | [diff] [blame] | 76 | ret = xhci_pci_init(dev, &hccr, &hcor); |
| 77 | if (ret) |
Samuel Holland | 386d128 | 2021-07-05 13:29:02 +0100 | [diff] [blame] | 78 | goto err_reset; |
| 79 | |
| 80 | ret = xhci_register(dev, hccr, hcor); |
| 81 | if (ret) |
| 82 | goto err_reset; |
| 83 | |
| 84 | return 0; |
| 85 | |
| 86 | err_reset: |
| 87 | if (reset_valid(&plat->reset)) |
| 88 | reset_free(&plat->reset); |
| 89 | |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | static int xhci_pci_remove(struct udevice *dev) |
| 94 | { |
| 95 | struct xhci_pci_plat *plat = dev_get_plat(dev); |
Stefan Roese | b0ff2c1 | 2016-07-18 12:51:39 +0200 | [diff] [blame] | 96 | |
Samuel Holland | 386d128 | 2021-07-05 13:29:02 +0100 | [diff] [blame] | 97 | xhci_deregister(dev); |
| 98 | if (reset_valid(&plat->reset)) |
| 99 | reset_free(&plat->reset); |
| 100 | |
| 101 | return 0; |
Stefan Roese | b0ff2c1 | 2016-07-18 12:51:39 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Stefan Roese | b0ff2c1 | 2016-07-18 12:51:39 +0200 | [diff] [blame] | 104 | static const struct udevice_id xhci_pci_ids[] = { |
| 105 | { .compatible = "xhci-pci" }, |
| 106 | { } |
| 107 | }; |
| 108 | |
| 109 | U_BOOT_DRIVER(xhci_pci) = { |
| 110 | .name = "xhci_pci", |
| 111 | .id = UCLASS_USB, |
| 112 | .probe = xhci_pci_probe, |
Samuel Holland | 386d128 | 2021-07-05 13:29:02 +0100 | [diff] [blame] | 113 | .remove = xhci_pci_remove, |
Stefan Roese | b0ff2c1 | 2016-07-18 12:51:39 +0200 | [diff] [blame] | 114 | .of_match = xhci_pci_ids, |
| 115 | .ops = &xhci_usb_ops, |
Samuel Holland | 386d128 | 2021-07-05 13:29:02 +0100 | [diff] [blame] | 116 | .plat_auto = sizeof(struct xhci_pci_plat), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 117 | .priv_auto = sizeof(struct xhci_ctrl), |
Nicolas Saenz Julienne | 9631625 | 2021-01-14 16:49:00 +0100 | [diff] [blame] | 118 | .flags = DM_FLAG_OS_PREPARE | DM_FLAG_ALLOC_PRIV_DMA, |
Stefan Roese | b0ff2c1 | 2016-07-18 12:51:39 +0200 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | static struct pci_device_id xhci_pci_supported[] = { |
| 122 | { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_XHCI, ~0) }, |
| 123 | {}, |
| 124 | }; |
| 125 | |
| 126 | U_BOOT_PCI_DEVICE(xhci_pci, xhci_pci_supported); |