| 1 | /*
|
|---|
| 2 | * Copyright (c) 2006 Jakub Jermar
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup sparc64
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * @brief PCI driver.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <arch/drivers/pci.h>
|
|---|
| 38 | #include <genarch/ofw/ofw_tree.h>
|
|---|
| 39 | #include <genarch/ofw/upa.h>
|
|---|
| 40 | #include <arch/trap/interrupt.h>
|
|---|
| 41 | #include <mm/page.h>
|
|---|
| 42 | #include <mm/slab.h>
|
|---|
| 43 | #include <typedefs.h>
|
|---|
| 44 | #include <debug.h>
|
|---|
| 45 | #include <print.h>
|
|---|
| 46 | #include <str.h>
|
|---|
| 47 | #include <arch/asm.h>
|
|---|
| 48 | #include <sysinfo/sysinfo.h>
|
|---|
| 49 |
|
|---|
| 50 | #define SABRE_INTERNAL_REG 0
|
|---|
| 51 | #define PSYCHO_INTERNAL_REG 2
|
|---|
| 52 |
|
|---|
| 53 | #define OBIO_IMR_BASE 0x200
|
|---|
| 54 | #define OBIO_IMR(ino) (OBIO_IMR_BASE + ((ino) & INO_MASK))
|
|---|
| 55 |
|
|---|
| 56 | #define OBIO_CIR_BASE 0x300
|
|---|
| 57 | #define OBIO_CIR(ino) (OBIO_CIR_BASE + ((ino) & INO_MASK))
|
|---|
| 58 |
|
|---|
| 59 | static void obio_enable_interrupt(pci_t *, int);
|
|---|
| 60 | static void obio_clear_interrupt(pci_t *, int);
|
|---|
| 61 |
|
|---|
| 62 | static pci_t *pci_sabre_init(ofw_tree_node_t *);
|
|---|
| 63 | static pci_t *pci_psycho_init(ofw_tree_node_t *);
|
|---|
| 64 |
|
|---|
| 65 | /** PCI operations for Sabre model. */
|
|---|
| 66 | static pci_operations_t pci_sabre_ops = {
|
|---|
| 67 | .enable_interrupt = obio_enable_interrupt,
|
|---|
| 68 | .clear_interrupt = obio_clear_interrupt
|
|---|
| 69 | };
|
|---|
| 70 | /** PCI operations for Psycho model. */
|
|---|
| 71 | static pci_operations_t pci_psycho_ops = {
|
|---|
| 72 | .enable_interrupt = obio_enable_interrupt,
|
|---|
| 73 | .clear_interrupt = obio_clear_interrupt
|
|---|
| 74 | };
|
|---|
| 75 |
|
|---|
| 76 | /** Initialize PCI controller (model Sabre).
|
|---|
| 77 | *
|
|---|
| 78 | * @param node OpenFirmware device tree node of the Sabre.
|
|---|
| 79 | *
|
|---|
| 80 | * @return Address of the initialized PCI structure.
|
|---|
| 81 | */
|
|---|
| 82 | pci_t *pci_sabre_init(ofw_tree_node_t *node)
|
|---|
| 83 | {
|
|---|
| 84 | pci_t *pci;
|
|---|
| 85 | ofw_tree_property_t *prop;
|
|---|
| 86 |
|
|---|
| 87 | /*
|
|---|
| 88 | * Get registers.
|
|---|
| 89 | */
|
|---|
| 90 | prop = ofw_tree_getprop(node, "reg");
|
|---|
| 91 | if (!prop || !prop->value)
|
|---|
| 92 | return NULL;
|
|---|
| 93 |
|
|---|
| 94 | ofw_upa_reg_t *reg = prop->value;
|
|---|
| 95 | size_t regs = prop->size / sizeof(ofw_upa_reg_t);
|
|---|
| 96 |
|
|---|
| 97 | if (regs < SABRE_INTERNAL_REG + 1)
|
|---|
| 98 | return NULL;
|
|---|
| 99 |
|
|---|
| 100 | uintptr_t paddr;
|
|---|
| 101 | if (!ofw_upa_apply_ranges(node->parent, ®[SABRE_INTERNAL_REG],
|
|---|
| 102 | &paddr))
|
|---|
| 103 | return NULL;
|
|---|
| 104 |
|
|---|
| 105 | pci = (pci_t *) malloc(sizeof(pci_t), FRAME_ATOMIC);
|
|---|
| 106 | if (!pci)
|
|---|
| 107 | return NULL;
|
|---|
| 108 |
|
|---|
| 109 | pci->model = PCI_SABRE;
|
|---|
| 110 | pci->op = &pci_sabre_ops;
|
|---|
| 111 | pci->reg = (uint64_t *) hw_map(paddr, reg[SABRE_INTERNAL_REG].size);
|
|---|
| 112 |
|
|---|
| 113 | /*
|
|---|
| 114 | * Set sysinfo data needed by the uspace OBIO driver.
|
|---|
| 115 | */
|
|---|
| 116 | sysinfo_set_item_val("obio.base.physical", NULL, paddr);
|
|---|
| 117 | sysinfo_set_item_val("kbd.cir.obio", NULL, 1);
|
|---|
| 118 |
|
|---|
| 119 | return pci;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 | /** Initialize the Psycho PCI controller.
|
|---|
| 124 | *
|
|---|
| 125 | * @param node OpenFirmware device tree node of the Psycho.
|
|---|
| 126 | *
|
|---|
| 127 | * @return Address of the initialized PCI structure.
|
|---|
| 128 | */
|
|---|
| 129 | pci_t *pci_psycho_init(ofw_tree_node_t *node)
|
|---|
| 130 | {
|
|---|
| 131 | pci_t *pci;
|
|---|
| 132 | ofw_tree_property_t *prop;
|
|---|
| 133 |
|
|---|
| 134 | /*
|
|---|
| 135 | * Get registers.
|
|---|
| 136 | */
|
|---|
| 137 | prop = ofw_tree_getprop(node, "reg");
|
|---|
| 138 | if (!prop || !prop->value)
|
|---|
| 139 | return NULL;
|
|---|
| 140 |
|
|---|
| 141 | ofw_upa_reg_t *reg = prop->value;
|
|---|
| 142 | size_t regs = prop->size / sizeof(ofw_upa_reg_t);
|
|---|
| 143 |
|
|---|
| 144 | if (regs < PSYCHO_INTERNAL_REG + 1)
|
|---|
| 145 | return NULL;
|
|---|
| 146 |
|
|---|
| 147 | uintptr_t paddr;
|
|---|
| 148 | if (!ofw_upa_apply_ranges(node->parent, ®[PSYCHO_INTERNAL_REG],
|
|---|
| 149 | &paddr))
|
|---|
| 150 | return NULL;
|
|---|
| 151 |
|
|---|
| 152 | pci = (pci_t *) malloc(sizeof(pci_t), FRAME_ATOMIC);
|
|---|
| 153 | if (!pci)
|
|---|
| 154 | return NULL;
|
|---|
| 155 |
|
|---|
| 156 | pci->model = PCI_PSYCHO;
|
|---|
| 157 | pci->op = &pci_psycho_ops;
|
|---|
| 158 | pci->reg = (uint64_t *) hw_map(paddr, reg[PSYCHO_INTERNAL_REG].size);
|
|---|
| 159 |
|
|---|
| 160 | /*
|
|---|
| 161 | * Set sysinfo data needed by the uspace OBIO driver.
|
|---|
| 162 | */
|
|---|
| 163 | sysinfo_set_item_val("obio.base.physical", NULL, paddr);
|
|---|
| 164 | sysinfo_set_item_val("kbd.cir.obio", NULL, 1);
|
|---|
| 165 |
|
|---|
| 166 | return pci;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | void obio_enable_interrupt(pci_t *pci, int inr)
|
|---|
| 170 | {
|
|---|
| 171 | pci->reg[OBIO_IMR(inr & INO_MASK)] |= IMAP_V_MASK;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | void obio_clear_interrupt(pci_t *pci, int inr)
|
|---|
| 175 | {
|
|---|
| 176 | pci->reg[OBIO_CIR(inr & INO_MASK)] = 0; /* set IDLE */
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /** Initialize PCI controller. */
|
|---|
| 180 | pci_t *pci_init(ofw_tree_node_t *node)
|
|---|
| 181 | {
|
|---|
| 182 | ofw_tree_property_t *prop;
|
|---|
| 183 |
|
|---|
| 184 | /*
|
|---|
| 185 | * First, verify this is a PCI node.
|
|---|
| 186 | */
|
|---|
| 187 | ASSERT(str_cmp(ofw_tree_node_name(node), "pci") == 0);
|
|---|
| 188 |
|
|---|
| 189 | /*
|
|---|
| 190 | * Determine PCI controller model.
|
|---|
| 191 | */
|
|---|
| 192 | prop = ofw_tree_getprop(node, "model");
|
|---|
| 193 | if (!prop || !prop->value)
|
|---|
| 194 | return NULL;
|
|---|
| 195 |
|
|---|
| 196 | if (str_cmp(prop->value, "SUNW,sabre") == 0) {
|
|---|
| 197 | /*
|
|---|
| 198 | * PCI controller Sabre.
|
|---|
| 199 | * This model is found on UltraSPARC IIi based machines.
|
|---|
| 200 | */
|
|---|
| 201 | return pci_sabre_init(node);
|
|---|
| 202 | } else if (str_cmp(prop->value, "SUNW,psycho") == 0) {
|
|---|
| 203 | /*
|
|---|
| 204 | * PCI controller Psycho.
|
|---|
| 205 | * Used on UltraSPARC II based processors, for instance,
|
|---|
| 206 | * on Ultra 60.
|
|---|
| 207 | */
|
|---|
| 208 | return pci_psycho_init(node);
|
|---|
| 209 | } else {
|
|---|
| 210 | /*
|
|---|
| 211 | * Unsupported model.
|
|---|
| 212 | */
|
|---|
| 213 | printf("Unsupported PCI controller model (%s).\n",
|
|---|
| 214 | (char *) prop->value);
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | return NULL;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | void pci_enable_interrupt(pci_t *pci, int inr)
|
|---|
| 221 | {
|
|---|
| 222 | ASSERT(pci->op && pci->op->enable_interrupt);
|
|---|
| 223 | pci->op->enable_interrupt(pci, inr);
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | void pci_clear_interrupt(void *pcip, int inr)
|
|---|
| 227 | {
|
|---|
| 228 | pci_t *pci = (pci_t *)pcip;
|
|---|
| 229 |
|
|---|
| 230 | ASSERT(pci->op && pci->op->clear_interrupt);
|
|---|
| 231 | pci->op->clear_interrupt(pci, inr);
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | /** @}
|
|---|
| 235 | */
|
|---|