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