Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) Freescale Semiconductor, Inc. 2007 |
| 4 | * |
| 5 | * Author: Scott Wood <scottwood@freescale.com>, |
| 6 | * with some bits from older board-specific PCI initialization. |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 10 | #include <init.h> |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 11 | #include <pci.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 12 | #include <asm/bitops.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
Kim Phillips | 3c6f3b7 | 2007-07-25 19:25:28 -0500 | [diff] [blame] | 15 | |
| 16 | #if defined(CONFIG_OF_LIBFDT) |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 17 | #include <linux/libfdt.h> |
Kim Phillips | fd47a74 | 2007-12-20 14:09:22 -0600 | [diff] [blame] | 18 | #include <fdt_support.h> |
Kim Phillips | 3c6f3b7 | 2007-07-25 19:25:28 -0500 | [diff] [blame] | 19 | #endif |
| 20 | |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 21 | #include <asm/mpc8349_pci.h> |
| 22 | |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 23 | #define MAX_BUSES 2 |
| 24 | |
| 25 | DECLARE_GLOBAL_DATA_PTR; |
| 26 | |
| 27 | static struct pci_controller pci_hose[MAX_BUSES]; |
| 28 | static int pci_num_buses; |
| 29 | |
Sinan Akman | 8d09f91 | 2021-05-11 14:18:02 -0400 | [diff] [blame] | 30 | #if !defined(CONFIG_DM_PCI) |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 31 | static void pci_init_bus(int bus, struct pci_region *reg) |
| 32 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 33 | volatile immap_t *immr = (volatile immap_t *)CONFIG_SYS_IMMR; |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 34 | volatile pot83xx_t *pot = immr->ios.pot; |
| 35 | volatile pcictrl83xx_t *pci_ctrl = &immr->pci_ctrl[bus]; |
| 36 | struct pci_controller *hose = &pci_hose[bus]; |
| 37 | u32 dev; |
| 38 | u16 reg16; |
| 39 | int i; |
| 40 | |
| 41 | if (bus == 1) |
| 42 | pot += 3; |
| 43 | |
| 44 | /* Setup outbound translation windows */ |
| 45 | for (i = 0; i < 3; i++, reg++, pot++) { |
| 46 | if (reg->size == 0) |
| 47 | break; |
| 48 | |
| 49 | hose->regions[i] = *reg; |
| 50 | hose->region_count++; |
| 51 | |
| 52 | pot->potar = reg->bus_start >> 12; |
| 53 | pot->pobar = reg->phys_start >> 12; |
| 54 | pot->pocmr = ~(reg->size - 1) >> 12; |
| 55 | |
| 56 | if (reg->flags & PCI_REGION_IO) |
| 57 | pot->pocmr |= POCMR_IO; |
| 58 | #ifdef CONFIG_83XX_PCI_STREAMING |
| 59 | else if (reg->flags & PCI_REGION_PREFETCH) |
| 60 | pot->pocmr |= POCMR_SE; |
| 61 | #endif |
| 62 | |
| 63 | if (bus == 1) |
| 64 | pot->pocmr |= POCMR_DST; |
| 65 | |
| 66 | pot->pocmr |= POCMR_EN; |
| 67 | } |
| 68 | |
| 69 | /* Point inbound translation at RAM */ |
| 70 | pci_ctrl->pitar1 = 0; |
| 71 | pci_ctrl->pibar1 = 0; |
| 72 | pci_ctrl->piebar1 = 0; |
| 73 | pci_ctrl->piwar1 = PIWAR_EN | PIWAR_PF | PIWAR_RTT_SNOOP | |
Wolfgang Denk | ec7fbf5 | 2013-10-04 17:43:24 +0200 | [diff] [blame] | 74 | PIWAR_WTT_SNOOP | (__ilog2(gd->ram_size - 1)); |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 75 | |
| 76 | i = hose->region_count++; |
| 77 | hose->regions[i].bus_start = 0; |
| 78 | hose->regions[i].phys_start = 0; |
| 79 | hose->regions[i].size = gd->ram_size; |
Kumar Gala | efa1f1d | 2009-02-06 09:49:31 -0600 | [diff] [blame] | 80 | hose->regions[i].flags = PCI_REGION_MEM | PCI_REGION_SYS_MEMORY; |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 81 | |
Anton Vorontsov | 41d6cf8 | 2009-02-19 18:20:50 +0300 | [diff] [blame] | 82 | hose->first_busno = pci_last_busno() + 1; |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 83 | hose->last_busno = 0xff; |
| 84 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 85 | pci_setup_indirect(hose, CONFIG_SYS_IMMR + 0x8300 + bus * 0x80, |
Wolfgang Denk | ec7fbf5 | 2013-10-04 17:43:24 +0200 | [diff] [blame] | 86 | CONFIG_SYS_IMMR + 0x8304 + bus * 0x80); |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 87 | |
| 88 | pci_register_hose(hose); |
| 89 | |
| 90 | /* |
| 91 | * Write to Command register |
| 92 | */ |
| 93 | reg16 = 0xff; |
| 94 | dev = PCI_BDF(hose->first_busno, 0, 0); |
| 95 | pci_hose_read_config_word(hose, dev, PCI_COMMAND, ®16); |
| 96 | reg16 |= PCI_COMMAND_SERR | PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY; |
| 97 | pci_hose_write_config_word(hose, dev, PCI_COMMAND, reg16); |
| 98 | |
| 99 | /* |
| 100 | * Clear non-reserved bits in status register. |
| 101 | */ |
| 102 | pci_hose_write_config_word(hose, dev, PCI_STATUS, 0xffff); |
| 103 | pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80); |
| 104 | pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08); |
| 105 | |
| 106 | #ifdef CONFIG_PCI_SCAN_SHOW |
| 107 | printf("PCI: Bus Dev VenId DevId Class Int\n"); |
| 108 | #endif |
Ira Snyder | e2e29ec | 2009-01-12 13:32:26 -0800 | [diff] [blame] | 109 | #ifndef CONFIG_PCISLAVE |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 110 | /* |
| 111 | * Hose scan. |
| 112 | */ |
| 113 | hose->last_busno = pci_hose_scan(hose); |
Ira Snyder | e2e29ec | 2009-01-12 13:32:26 -0800 | [diff] [blame] | 114 | #endif |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* |
| 118 | * The caller must have already set OCCR, and the PCI_LAW BARs |
| 119 | * must have been set to cover all of the requested regions. |
| 120 | * |
| 121 | * If fewer than three regions are requested, then the region |
| 122 | * list is terminated with a region of size 0. |
| 123 | */ |
Peter Tyser | e228332 | 2010-09-14 19:13:50 -0500 | [diff] [blame] | 124 | void mpc83xx_pci_init(int num_buses, struct pci_region **reg) |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 125 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 126 | volatile immap_t *immr = (volatile immap_t *)CONFIG_SYS_IMMR; |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 127 | int i; |
| 128 | |
| 129 | if (num_buses > MAX_BUSES) { |
Robert P. J. Day | cbd618f | 2015-12-16 12:25:42 -0500 | [diff] [blame] | 130 | printf("%d PCI buses requested, %d supported\n", |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 131 | num_buses, MAX_BUSES); |
| 132 | |
| 133 | num_buses = MAX_BUSES; |
| 134 | } |
| 135 | |
| 136 | pci_num_buses = num_buses; |
| 137 | |
| 138 | /* |
| 139 | * Release PCI RST Output signal. |
| 140 | * Power on to RST high must be at least 100 ms as per PCI spec. |
Peter Tyser | e228332 | 2010-09-14 19:13:50 -0500 | [diff] [blame] | 141 | * On warm boots only 1 ms is required, but we play it safe. |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 142 | */ |
Peter Tyser | e228332 | 2010-09-14 19:13:50 -0500 | [diff] [blame] | 143 | udelay(100000); |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 144 | |
| 145 | for (i = 0; i < num_buses; i++) |
| 146 | immr->pci_ctrl[i].gcr = 1; |
| 147 | |
| 148 | /* |
| 149 | * RST high to first config access must be at least 2^25 cycles |
| 150 | * as per PCI spec. This could be cut in half if we know we're |
| 151 | * running at 66MHz. This could be insufficiently long if we're |
| 152 | * running the PCI bus at significantly less than 33MHz. |
| 153 | */ |
| 154 | udelay(1020000); |
| 155 | |
| 156 | for (i = 0; i < num_buses; i++) |
| 157 | pci_init_bus(i, reg[i]); |
| 158 | } |
| 159 | |
Ira W. Snyder | 51dc5e3 | 2008-08-22 11:00:14 -0700 | [diff] [blame] | 160 | #ifdef CONFIG_PCISLAVE |
| 161 | |
| 162 | #define PCI_FUNCTION_CONFIG 0x44 |
| 163 | #define PCI_FUNCTION_CFG_LOCK 0x20 |
| 164 | |
| 165 | /* |
| 166 | * Unlock the configuration bit so that the host system can begin booting |
| 167 | * |
| 168 | * This should be used after you have: |
| 169 | * 1) Called mpc83xx_pci_init() |
| 170 | * 2) Set up your inbound translation windows to the appropriate size |
| 171 | */ |
| 172 | void mpc83xx_pcislave_unlock(int bus) |
| 173 | { |
| 174 | struct pci_controller *hose = &pci_hose[bus]; |
| 175 | u32 dev; |
| 176 | u16 reg16; |
| 177 | |
| 178 | /* Unlock configuration lock in PCI function configuration register */ |
| 179 | dev = PCI_BDF(hose->first_busno, 0, 0); |
| 180 | pci_hose_read_config_word (hose, dev, PCI_FUNCTION_CONFIG, ®16); |
| 181 | reg16 &= ~(PCI_FUNCTION_CFG_LOCK); |
| 182 | pci_hose_write_config_word (hose, dev, PCI_FUNCTION_CONFIG, reg16); |
Ira Snyder | e2e29ec | 2009-01-12 13:32:26 -0800 | [diff] [blame] | 183 | |
| 184 | /* The configuration bit is now unlocked, so we can scan the bus */ |
| 185 | hose->last_busno = pci_hose_scan(hose); |
Ira W. Snyder | 51dc5e3 | 2008-08-22 11:00:14 -0700 | [diff] [blame] | 186 | } |
| 187 | #endif |
Sinan Akman | 8d09f91 | 2021-05-11 14:18:02 -0400 | [diff] [blame] | 188 | #endif /* CONFIG_DM_PCI */ |
Ira W. Snyder | 51dc5e3 | 2008-08-22 11:00:14 -0700 | [diff] [blame] | 189 | |
Kim Phillips | 3c6f3b7 | 2007-07-25 19:25:28 -0500 | [diff] [blame] | 190 | #if defined(CONFIG_OF_LIBFDT) |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 191 | void ft_pci_setup(void *blob, struct bd_info *bd) |
Kim Phillips | 3c6f3b7 | 2007-07-25 19:25:28 -0500 | [diff] [blame] | 192 | { |
| 193 | int nodeoffset; |
Kim Phillips | 3c6f3b7 | 2007-07-25 19:25:28 -0500 | [diff] [blame] | 194 | int tmp[2]; |
Kim Phillips | fd47a74 | 2007-12-20 14:09:22 -0600 | [diff] [blame] | 195 | const char *path; |
Kim Phillips | 3c6f3b7 | 2007-07-25 19:25:28 -0500 | [diff] [blame] | 196 | |
| 197 | if (pci_num_buses < 1) |
| 198 | return; |
| 199 | |
Kim Phillips | fd47a74 | 2007-12-20 14:09:22 -0600 | [diff] [blame] | 200 | nodeoffset = fdt_path_offset(blob, "/aliases"); |
Kim Phillips | 3c6f3b7 | 2007-07-25 19:25:28 -0500 | [diff] [blame] | 201 | if (nodeoffset >= 0) { |
Kim Phillips | fd47a74 | 2007-12-20 14:09:22 -0600 | [diff] [blame] | 202 | path = fdt_getprop(blob, nodeoffset, "pci0", NULL); |
| 203 | if (path) { |
| 204 | tmp[0] = cpu_to_be32(pci_hose[0].first_busno); |
| 205 | tmp[1] = cpu_to_be32(pci_hose[0].last_busno); |
| 206 | do_fixup_by_path(blob, path, "bus-range", |
| 207 | &tmp, sizeof(tmp), 1); |
Kim Phillips | 2141681 | 2007-08-15 22:30:33 -0500 | [diff] [blame] | 208 | |
Kim Phillips | fd47a74 | 2007-12-20 14:09:22 -0600 | [diff] [blame] | 209 | tmp[0] = cpu_to_be32(gd->pci_clk); |
| 210 | do_fixup_by_path(blob, path, "clock-frequency", |
| 211 | &tmp, sizeof(tmp[0]), 1); |
| 212 | } |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 213 | |
Kim Phillips | fd47a74 | 2007-12-20 14:09:22 -0600 | [diff] [blame] | 214 | if (pci_num_buses < 2) |
| 215 | return; |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 216 | |
Kim Phillips | fd47a74 | 2007-12-20 14:09:22 -0600 | [diff] [blame] | 217 | path = fdt_getprop(blob, nodeoffset, "pci1", NULL); |
| 218 | if (path) { |
Anton Vorontsov | 7c78547 | 2009-02-19 18:20:46 +0300 | [diff] [blame] | 219 | tmp[0] = cpu_to_be32(pci_hose[1].first_busno); |
| 220 | tmp[1] = cpu_to_be32(pci_hose[1].last_busno); |
Kim Phillips | fd47a74 | 2007-12-20 14:09:22 -0600 | [diff] [blame] | 221 | do_fixup_by_path(blob, path, "bus-range", |
| 222 | &tmp, sizeof(tmp), 1); |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 223 | |
Kim Phillips | fd47a74 | 2007-12-20 14:09:22 -0600 | [diff] [blame] | 224 | tmp[0] = cpu_to_be32(gd->pci_clk); |
| 225 | do_fixup_by_path(blob, path, "clock-frequency", |
| 226 | &tmp, sizeof(tmp[0]), 1); |
| 227 | } |
Scott Wood | 2fa1391 | 2007-04-16 14:34:21 -0500 | [diff] [blame] | 228 | } |
| 229 | } |
Kim Phillips | fd47a74 | 2007-12-20 14:09:22 -0600 | [diff] [blame] | 230 | #endif /* CONFIG_OF_LIBFDT */ |