Angel Pons | c74dae9 | 2020-04-02 23:48:16 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 2 | |
| 3 | #include <console/console.h> |
| 4 | #include <device/device.h> |
| 5 | #include <device/pci.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 6 | #include <device/pci_ops.h> |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 7 | #include <device/pcix.h> |
Elyes Haouas | 04c3b5a | 2022-10-07 10:08:05 +0200 | [diff] [blame] | 8 | #include <stdint.h> |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 9 | |
Elyes HAOUAS | b9e82f0 | 2018-05-02 21:29:55 +0200 | [diff] [blame] | 10 | static void pcix_tune_dev(struct device *dev) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 11 | { |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 12 | u32 status; |
| 13 | u16 orig_cmd, cmd; |
| 14 | unsigned int cap, max_read, max_tran; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 15 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 16 | if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 17 | return; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 18 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 19 | cap = pci_find_capability(dev, PCI_CAP_ID_PCIX); |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 20 | if (!cap) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 21 | return; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 22 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 23 | printk(BIOS_DEBUG, "%s PCI-X tuning\n", dev_path(dev)); |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 24 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 25 | status = pci_read_config32(dev, cap + PCI_X_STATUS); |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 26 | orig_cmd = cmd = pci_read_config16(dev, cap + PCI_X_CMD); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 27 | |
| 28 | max_read = (status & PCI_X_STATUS_MAX_READ) >> 21; |
| 29 | max_tran = (status & PCI_X_STATUS_MAX_SPLIT) >> 23; |
| 30 | if (max_read != ((cmd & PCI_X_CMD_MAX_READ) >> 2)) { |
| 31 | cmd &= ~PCI_X_CMD_MAX_READ; |
| 32 | cmd |= max_read << 2; |
| 33 | } |
| 34 | if (max_tran != ((cmd & PCI_X_CMD_MAX_SPLIT) >> 4)) { |
| 35 | cmd &= ~PCI_X_CMD_MAX_SPLIT; |
| 36 | cmd |= max_tran << 4; |
| 37 | } |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 38 | |
| 39 | /* Don't attempt to handle PCI-X errors. */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 40 | cmd &= ~PCI_X_CMD_DPERR_E; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 41 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 42 | /* Enable relaxed ordering. */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 43 | cmd |= PCI_X_CMD_ERO; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 44 | |
| 45 | if (orig_cmd != cmd) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 46 | pci_write_config16(dev, cap + PCI_X_CMD, cmd); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 49 | static void pcix_tune_bus(struct bus *bus) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 50 | { |
Elyes HAOUAS | b9e82f0 | 2018-05-02 21:29:55 +0200 | [diff] [blame] | 51 | struct device *child; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 52 | |
| 53 | for (child = bus->children; child; child = child->sibling) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 54 | pcix_tune_dev(child); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 57 | const char *pcix_speed(u16 sstatus) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 58 | { |
| 59 | static const char conventional[] = "Conventional PCI"; |
| 60 | static const char pcix_66mhz[] = "66MHz PCI-X"; |
| 61 | static const char pcix_100mhz[] = "100MHz PCI-X"; |
| 62 | static const char pcix_133mhz[] = "133MHz PCI-X"; |
| 63 | static const char pcix_266mhz[] = "266MHz PCI-X"; |
| 64 | static const char pcix_533mhz[] = "533MHZ PCI-X"; |
| 65 | static const char unknown[] = "Unknown"; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 66 | const char *result; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 67 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 68 | result = unknown; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 69 | |
| 70 | switch (PCI_X_SSTATUS_MFREQ(sstatus)) { |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 71 | case PCI_X_SSTATUS_CONVENTIONAL_PCI: |
| 72 | result = conventional; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 73 | break; |
| 74 | case PCI_X_SSTATUS_MODE1_66MHZ: |
| 75 | result = pcix_66mhz; |
| 76 | break; |
| 77 | case PCI_X_SSTATUS_MODE1_100MHZ: |
| 78 | result = pcix_100mhz; |
| 79 | break; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 80 | case PCI_X_SSTATUS_MODE1_133MHZ: |
| 81 | result = pcix_133mhz; |
| 82 | break; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 83 | case PCI_X_SSTATUS_MODE2_266MHZ_REF_66MHZ: |
| 84 | case PCI_X_SSTATUS_MODE2_266MHZ_REF_100MHZ: |
| 85 | case PCI_X_SSTATUS_MODE2_266MHZ_REF_133MHZ: |
| 86 | result = pcix_266mhz; |
| 87 | break; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 88 | case PCI_X_SSTATUS_MODE2_533MHZ_REF_66MHZ: |
| 89 | case PCI_X_SSTATUS_MODE2_533MHZ_REF_100MHZ: |
| 90 | case PCI_X_SSTATUS_MODE2_533MHZ_REF_133MHZ: |
| 91 | result = pcix_533mhz; |
| 92 | break; |
| 93 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 94 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 95 | return result; |
| 96 | } |
| 97 | |
Elyes HAOUAS | b9e82f0 | 2018-05-02 21:29:55 +0200 | [diff] [blame] | 98 | void pcix_scan_bridge(struct device *dev) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 99 | { |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 100 | unsigned int pos; |
| 101 | u16 sstatus; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 102 | |
Kyösti Mälkki | 580e722 | 2015-03-19 21:04:23 +0200 | [diff] [blame] | 103 | do_pci_scan_bridge(dev, pci_scan_bus); |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 104 | |
| 105 | /* Find the PCI-X capability. */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 106 | pos = pci_find_capability(dev, PCI_CAP_ID_PCIX); |
| 107 | sstatus = pci_read_config16(dev, pos + PCI_X_SEC_STATUS); |
| 108 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 109 | if (PCI_X_SSTATUS_MFREQ(sstatus) != PCI_X_SSTATUS_CONVENTIONAL_PCI) |
Arthur Heymans | 7fcd4d5 | 2023-08-24 15:12:19 +0200 | [diff] [blame] | 110 | pcix_tune_bus(dev->downstream); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 111 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 112 | /* Print the PCI-X bus speed. */ |
Arthur Heymans | 7fcd4d5 | 2023-08-24 15:12:19 +0200 | [diff] [blame] | 113 | printk(BIOS_DEBUG, "PCI: %02x:%02x: %s\n", dev->downstream->segment_group, |
| 114 | dev->downstream->secondary, pcix_speed(sstatus)); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 117 | /** Default device operations for PCI-X bridges */ |
| 118 | static struct pci_operations pcix_bus_ops_pci = { |
| 119 | .set_subsystem = 0, |
| 120 | }; |
| 121 | |
| 122 | struct device_operations default_pcix_ops_bus = { |
| 123 | .read_resources = pci_bus_read_resources, |
| 124 | .set_resources = pci_dev_set_resources, |
| 125 | .enable_resources = pci_bus_enable_resources, |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 126 | .scan_bus = pcix_scan_bridge, |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 127 | .reset_bus = pci_bus_reset, |
| 128 | .ops_pci = &pcix_bus_ops_pci, |
| 129 | }; |