Home | History | Annotate | Line # | Download | only in macppc
      1 /*	$NetBSD: rbus_machdep.c,v 1.21 2025/03/17 08:23:13 macallan Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1999
      5  *     TSUBAI Masanari.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: rbus_machdep.c,v 1.21 2025/03/17 08:23:13 macallan Exp $");
     32 
     33 #include <sys/param.h>
     34 #include <sys/device.h>
     35 #include <sys/systm.h>
     36 
     37 #include <powerpc/oea/bat.h>
     38 #include <sys/bus.h>
     39 
     40 #include <dev/pci/pcivar.h>
     41 #include <dev/pci/pcidevs.h>
     42 #include <dev/cardbus/rbus.h>
     43 #include <dev/ofw/openfirm.h>
     44 
     45 static void macppc_cardbus_init(pci_chipset_tag_t, pcitag_t);
     46 
     47 #ifdef DEBUG_ALLOC
     48 # define DPRINTF printf
     49 #else
     50 # define DPRINTF while (0) printf
     51 #endif
     52 
     53 int
     54 md_space_map(bus_space_tag_t t, bus_addr_t bpa, bus_size_t size,
     55     int flags, bus_space_handle_t *bshp)
     56 {
     57 	DPRINTF("md_space_map: %p, 0x%x, 0x%x\n", t, bpa, size);
     58 
     59 	return bus_space_map(t, bpa, size, flags, bshp);
     60 }
     61 
     62 void
     63 md_space_unmap(bus_space_tag_t t, bus_space_handle_t bsh, bus_size_t size,
     64     bus_addr_t *adrp)
     65 {
     66 	DPRINTF("md_space_unmap: %p 0x%x\n", t, bsh);
     67 
     68 	if (adrp)
     69 		*adrp = bsh - t->pbs_offset;
     70 
     71 	bus_space_unmap(t, bsh, size);
     72 }
     73 
     74 rbus_tag_t
     75 rbus_pccbb_parent_mem(struct pci_attach_args *pa)
     76 {
     77 	bus_addr_t start = 0;
     78 	bus_size_t size = 0;
     79 	int node, reg[5];
     80 
     81 	macppc_cardbus_init(pa->pa_pc, pa->pa_tag);
     82 
     83 	node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
     84 
     85 	/*
     86 	 * the Powerbook 3400c has the cardbus controller's memory space in
     87 	 * 'assigned-addresses', at 0xa0000000. Core99 machines do not, they
     88 	 * have 'reserved-segment' instead, which points at 0x90000000 on
     89 	 * my Pismo, TiBook and AluBook.
     90 	 * This leaves the older G3 Powerbooks with pre-3.0 OF, I suspect they
     91 	 * behave like the 3400c.
     92 	 */
     93 
     94 	/* first we look for 'reserved-segment' */
     95 	if (OF_getprop(node, "reserved-segment", &start, 4) == 4) {
     96 		size = 0x10000000;
     97 		printf("found reserved-segment %08x\n", start);
     98 	} else {
     99 		/* if that fails look at assigned-addresses */
    100 		OF_getprop(node, "assigned-addresses", reg, sizeof(reg));
    101 		/* see if the range is big enough */
    102 		if (reg[4] < 0x10000) {
    103 			/* nope, use the default */
    104 			start = 0x90000000;
    105 			size = 0x10000000;
    106 		} else {
    107 			start = reg[2];
    108 			size = reg[4];
    109 			printf("found assigned-addresses %08x\n", start);
    110 		}
    111 	}
    112 
    113 	return rbus_new_root_delegate(pa->pa_memt, start, size, 0);
    114 }
    115 
    116 rbus_tag_t
    117 rbus_pccbb_parent_io(struct pci_attach_args *pa)
    118 {
    119 	bus_addr_t start = 0x2000;
    120 	bus_size_t size  = 0x0800;
    121 
    122 	start += pa->pa_function * size;
    123 	return rbus_new_root_delegate(pa->pa_iot, start, size, 0);
    124 
    125 	/* XXX we should use ``offset''? */
    126 }
    127 
    128 void
    129 macppc_cardbus_init(pci_chipset_tag_t pc, pcitag_t tag)
    130 {
    131 	u_int x;
    132 	static int initted = 0;
    133 
    134 	if (initted)
    135 		return;
    136 	initted = 1;
    137 
    138 	/* XXX What about other bridges? */
    139 
    140 	x = pci_conf_read(pc, tag, PCI_ID_REG);
    141 	if (PCI_VENDOR(x) == PCI_VENDOR_TI &&
    142 	    PCI_PRODUCT(x) == PCI_PRODUCT_TI_PCI1211) {
    143 		/* For CardBus card. */
    144 		pci_conf_write(pc, tag, 0x18, 0x10010100);
    145 
    146 		/* Route INTA to MFUNC0 */
    147 		x = pci_conf_read(pc, tag, 0x8c);
    148 		x |= 0x02;
    149 		pci_conf_write(pc, tag, 0x8c, x);
    150 
    151 		tag = pci_make_tag(pc, 0, 0, 0);
    152 		x = pci_conf_read(pc, tag, PCI_ID_REG);
    153 		if (PCI_VENDOR(x) == PCI_VENDOR_MOT &&
    154 		    PCI_PRODUCT(x) == PCI_PRODUCT_MOT_MPC106) {
    155 			/* Set subordinate bus number to 1. */
    156 			x = pci_conf_read(pc, tag, 0x40);
    157 			x |= 1 << 8;
    158 			pci_conf_write(pc, tag, 0x40, x);
    159 		}
    160 	}
    161 }
    162