1 /* $NetBSD: becc.c,v 1.16 2012/10/14 14:20:57 msaitoh Exp $ */ 2 3 /* 4 * Copyright (c) 2002, 2003 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 /* 39 * Autoconfiguration support for the ADI Engineering Big Endian 40 * Companion Chip. 41 */ 42 43 #include <sys/cdefs.h> 44 __KERNEL_RCSID(0, "$NetBSD: becc.c,v 1.16 2012/10/14 14:20:57 msaitoh Exp $"); 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/device.h> 49 50 #define _ARM32_BUS_DMA_PRIVATE 51 #include <sys/bus.h> 52 53 #include <arm/xscale/i80200reg.h> 54 #include <arm/xscale/beccreg.h> 55 #include <arm/xscale/beccvar.h> 56 57 /* 58 * Virtual address at which the BECC is mapped. This is filled in 59 * by machine-dependent code. 60 */ 61 vaddr_t becc_vaddr; 62 63 /* 64 * BECC revision number. This is initialized by early bootstrap code. 65 */ 66 int becc_rev; 67 const char *becc_revisions[] = { 68 "<= 7", 69 "8", 70 ">= 9", 71 }; 72 73 /* 74 * There can be only one BECC, so we keep a global pointer to 75 * the softc, so board-specific code can use features of the 76 * BECC without having to have a handle on the softc itself. 77 */ 78 struct becc_softc *becc_softc; 79 80 static int becc_search(device_t, cfdata_t, const int *, void *); 81 static int becc_print(void *, const char *); 82 83 static void becc_pci_dma_init(struct becc_softc *); 84 static void becc_local_dma_init(struct becc_softc *); 85 86 /* 87 * becc_attach: 88 * 89 * Board-independent attach routine for the BECC. 90 */ 91 void 92 becc_attach(struct becc_softc *sc) 93 { 94 struct pcibus_attach_args pba; 95 uint32_t reg; 96 97 becc_softc = sc; 98 99 /* 100 * Set the AF bit in the BCUMOD since the BECC will honor it. 101 * This allows the BECC to return the requested 4-byte word 102 * first when filling a cache line. 103 */ 104 __asm volatile("mrc p13, 0, %0, c1, c1, 0" : "=r" (reg)); 105 __asm volatile("mcr p13, 0, %0, c1, c1, 0" : : "r" (reg | BCUMOD_AF)); 106 107 /* 108 * Program the address windows of the PCI core. Note 109 * that PCI master and target cycles must be disabled 110 * while we configure the windows. 111 */ 112 reg = becc_pcicore_read(sc, PCI_COMMAND_STATUS_REG); 113 reg &= ~(PCI_COMMAND_MEM_ENABLE|PCI_COMMAND_MASTER_ENABLE); 114 becc_pcicore_write(sc, PCI_COMMAND_STATUS_REG, reg); 115 116 /* 117 * Program the two inbound PCI memory windows. 118 */ 119 becc_pcicore_write(sc, PCI_MAPREG_START + 0, 120 sc->sc_iwin[0].iwin_base | PCI_MAPREG_MEM_TYPE_32BIT | 121 PCI_MAPREG_MEM_PREFETCHABLE_MASK); 122 reg = becc_pcicore_read(sc, PCI_MAPREG_START + 0); 123 BECC_CSR_WRITE(BECC_PSTR0, sc->sc_iwin[0].iwin_xlate & PSTRx_ADDRMASK); 124 125 becc_pcicore_write(sc, PCI_MAPREG_START + 4, 126 sc->sc_iwin[1].iwin_base | PCI_MAPREG_MEM_TYPE_32BIT | 127 PCI_MAPREG_MEM_PREFETCHABLE_MASK); 128 reg = becc_pcicore_read(sc, PCI_MAPREG_START + 4); 129 BECC_CSR_WRITE(BECC_PSTR1, sc->sc_iwin[1].iwin_xlate & PSTRx_ADDRMASK); 130 131 /* 132 * ...and the third on v8 and later. 133 */ 134 if (becc_rev >= BECC_REV_V8) { 135 becc_pcicore_write(sc, PCI_MAPREG_START + 8, 136 sc->sc_iwin[2].iwin_base | PCI_MAPREG_MEM_TYPE_32BIT | 137 PCI_MAPREG_MEM_PREFETCHABLE_MASK); 138 reg = becc_pcicore_read(sc, PCI_MAPREG_START + 8); 139 BECC_CSR_WRITE(BECC_PSTR2, 140 sc->sc_iwin[2].iwin_xlate & PSTR2_ADDRMASK); 141 } 142 143 /* 144 * Program the two outbound PCI memory windows. 145 * NOTE: WE DO NOT BYTE-SWAP OUTBOUND WINDOWS IN BIG-ENDIAN 146 * MODE. I know this seems counter-intuitive, but that's 147 * how it is. 148 * 149 * There's a third window on v9 and later, but we don't 150 * use it for anything; program it anyway, just to be 151 * safe. 152 */ 153 BECC_CSR_WRITE(BECC_POMR1, sc->sc_owin_xlate[0] /* | POMRx_F32 */); 154 BECC_CSR_WRITE(BECC_POMR2, sc->sc_owin_xlate[1] /* | POMRx_F32 */); 155 156 if (becc_rev >= BECC_REV_V9) 157 BECC_CSR_WRITE(BECC_POMR3, 158 sc->sc_owin_xlate[2] /* | POMRx_F32 */); 159 160 /* 161 * Program the PCI I/O window. See note above about byte-swapping. 162 * 163 * XXX What about STREAM transfers? 164 */ 165 BECC_CSR_WRITE(BECC_POIR, sc->sc_ioout_xlate); 166 167 /* 168 * Configure PCI configuration cycle access. 169 */ 170 BECC_CSR_WRITE(BECC_POCR, 0); 171 172 /* 173 * ...and now reenable PCI access. 174 */ 175 reg = becc_pcicore_read(sc, PCI_COMMAND_STATUS_REG); 176 reg |= PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE | 177 PCI_COMMAND_PARITY_ENABLE | PCI_COMMAND_SERR_ENABLE; 178 becc_pcicore_write(sc, PCI_COMMAND_STATUS_REG, reg); 179 180 /* Initialize the bus space tags. */ 181 becc_io_bs_init(&sc->sc_pci_iot, sc); 182 becc_mem_bs_init(&sc->sc_pci_memt, sc); 183 184 /* Initialize the PCI chipset tag. */ 185 becc_pci_init(&sc->sc_pci_chipset, sc); 186 187 /* Initialize the DMA tags. */ 188 becc_pci_dma_init(sc); 189 becc_local_dma_init(sc); 190 191 /* 192 * Attach any on-chip peripherals. We used indirect config, since 193 * the BECC is a soft-core with a variety of peripherals, depending 194 * on configuration. 195 */ 196 config_search_ia(becc_search, sc->sc_dev, "becc", NULL); 197 198 /* 199 * Attach the PCI bus. 200 */ 201 pba.pba_iot = &sc->sc_pci_iot; 202 pba.pba_memt = &sc->sc_pci_memt; 203 pba.pba_dmat = &sc->sc_pci_dmat; 204 pba.pba_dmat64 = NULL; 205 pba.pba_pc = &sc->sc_pci_chipset; 206 pba.pba_bus = 0; 207 pba.pba_bridgetag = NULL; 208 pba.pba_intrswiz = 0; 209 pba.pba_intrtag = 0; 210 pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY | 211 PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | PCI_FLAGS_MWI_OKAY; 212 (void) config_found_ia(sc->sc_dev, "pcibus", &pba, pcibusprint); 213 } 214 215 /* 216 * becc_search: 217 * 218 * Indirect autoconfiguration glue for BECC. 219 */ 220 static int 221 becc_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux) 222 { 223 struct becc_softc *sc = device_private(parent); 224 struct becc_attach_args ba; 225 226 ba.ba_dmat = &sc->sc_local_dmat; 227 228 if (config_match(parent, cf, &ba) > 0) 229 config_attach(parent, cf, &ba, becc_print); 230 231 return (0); 232 } 233 234 /* 235 * becc_print: 236 * 237 * Autoconfiguration cfprint routine when attaching 238 * to the BECC. 239 */ 240 static int 241 becc_print(void *aux, const char *pnp) 242 { 243 244 return (UNCONF); 245 } 246 247 /* 248 * becc_pci_dma_init: 249 * 250 * Initialize the PCI DMA tag. 251 */ 252 static void 253 becc_pci_dma_init(struct becc_softc *sc) 254 { 255 bus_dma_tag_t dmat = &sc->sc_pci_dmat; 256 struct arm32_dma_range *dr = sc->sc_pci_dma_range; 257 int i = 0; 258 259 /* 260 * If we have the 128MB window, put it first, since it 261 * will always cover the entire memory range. 262 */ 263 if (becc_rev >= BECC_REV_V8) { 264 dr[i].dr_sysbase = sc->sc_iwin[2].iwin_xlate; 265 dr[i].dr_busbase = sc->sc_iwin[2].iwin_base; 266 dr[i].dr_len = (128U * 1024 * 1024); 267 i++; 268 } 269 270 dr[i].dr_sysbase = sc->sc_iwin[0].iwin_xlate; 271 dr[i].dr_busbase = sc->sc_iwin[0].iwin_base; 272 dr[i].dr_len = (32U * 1024 * 1024); 273 i++; 274 275 dr[i].dr_sysbase = sc->sc_iwin[1].iwin_xlate; 276 dr[i].dr_busbase = sc->sc_iwin[1].iwin_base; 277 dr[i].dr_len = (32U * 1024 * 1024); 278 i++; 279 280 dmat->_ranges = dr; 281 dmat->_nranges = i; 282 283 dmat->_dmamap_create = _bus_dmamap_create; 284 dmat->_dmamap_destroy = _bus_dmamap_destroy; 285 dmat->_dmamap_load = _bus_dmamap_load; 286 dmat->_dmamap_load_mbuf = _bus_dmamap_load_mbuf; 287 dmat->_dmamap_load_uio = _bus_dmamap_load_uio; 288 dmat->_dmamap_load_raw = _bus_dmamap_load_raw; 289 dmat->_dmamap_unload = _bus_dmamap_unload; 290 dmat->_dmamap_sync_pre = _bus_dmamap_sync; 291 dmat->_dmamap_sync_post = NULL; 292 293 dmat->_dmamem_alloc = _bus_dmamem_alloc; 294 dmat->_dmamem_free = _bus_dmamem_free; 295 dmat->_dmamem_map = _bus_dmamem_map; 296 dmat->_dmamem_unmap = _bus_dmamem_unmap; 297 dmat->_dmamem_mmap = _bus_dmamem_mmap; 298 299 dmat->_dmatag_subregion = _bus_dmatag_subregion; 300 dmat->_dmatag_destroy = _bus_dmatag_destroy; 301 } 302 303 /* 304 * becc_local_dma_init: 305 * 306 * Initialize the local DMA tag. 307 */ 308 static void 309 becc_local_dma_init(struct becc_softc *sc) 310 { 311 bus_dma_tag_t dmat = &sc->sc_local_dmat; 312 313 dmat->_ranges = NULL; 314 dmat->_nranges = 0; 315 316 dmat->_dmamap_create = _bus_dmamap_create; 317 dmat->_dmamap_destroy = _bus_dmamap_destroy; 318 dmat->_dmamap_load = _bus_dmamap_load; 319 dmat->_dmamap_load_mbuf = _bus_dmamap_load_mbuf; 320 dmat->_dmamap_load_uio = _bus_dmamap_load_uio; 321 dmat->_dmamap_load_raw = _bus_dmamap_load_raw; 322 dmat->_dmamap_unload = _bus_dmamap_unload; 323 dmat->_dmamap_sync_pre = _bus_dmamap_sync; 324 dmat->_dmamap_sync_post = NULL; 325 326 dmat->_dmamem_alloc = _bus_dmamem_alloc; 327 dmat->_dmamem_free = _bus_dmamem_free; 328 dmat->_dmamem_map = _bus_dmamem_map; 329 dmat->_dmamem_unmap = _bus_dmamem_unmap; 330 dmat->_dmamem_mmap = _bus_dmamem_mmap; 331 } 332 333 uint32_t 334 becc_pcicore_read(struct becc_softc *sc, bus_addr_t reg) 335 { 336 vaddr_t va = sc->sc_pci_cfg_base | (1U << BECC_IDSEL_BIT) | reg; 337 338 return (*(volatile uint32_t *) va); 339 } 340 341 void 342 becc_pcicore_write(struct becc_softc *sc, bus_addr_t reg, uint32_t val) 343 { 344 vaddr_t va = sc->sc_pci_cfg_base | (1U << BECC_IDSEL_BIT) | reg; 345 346 *(volatile uint32_t *) va = val; 347 } 348