1/* $NetBSD: pciconf_ofmethod.c,v 1.7 2021/01/06 08:17:46 rin Exp $ */
2
3/*-
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jochen Kunz and Tim Rightnour
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * Generic PowerPC functions for talking to a PCI Bridge via the RTAS or
34 * OF methods of the device.
35 */
36
37#define _POWERPC_BUS_DMA_PRIVATE
38
39#include <sys/cdefs.h>
40__KERNEL_RCSID(0, "$NetBSD: pciconf_ofmethod.c,v 1.7 2021/01/06 08:17:46 rin Exp $");
41
42#include <sys/types.h>
43#include <sys/param.h>
44#include <sys/time.h>
45#include <sys/systm.h>
46#include <sys/errno.h>
47#include <sys/device.h>
48#include <sys/bus.h>
49#include <sys/intr.h>
50
51#include <uvm/uvm_extern.h>
52
53#include <machine/pio.h>
54
55#include <dev/ofw/openfirm.h>
56#include <dev/ofw/ofw_pci.h>
57
58#include <dev/pci/pcivar.h>
59#include <dev/pci/pcireg.h>
60#include <dev/pci/pcidevs.h>
61
62void
63genppc_pci_ofmethod_attach_hook(device_t parent, device_t self,
64    struct pcibus_attach_args *pba)
65{
66
67	if (pba->pba_bus != 0)
68		return;
69
70	aprint_normal(": OFW method configuration space access");
71}
72
73pcitag_t
74genppc_pci_ofmethod_make_tag(void *v, int bus, int dev, int func)
75{
76	return ((bus << OFW_PCI_PHYS_HI_BUSSHIFT) & OFW_PCI_PHYS_HI_BUSMASK) |
77	    ((dev << OFW_PCI_PHYS_HI_DEVICESHIFT) & OFW_PCI_PHYS_HI_DEVICEMASK)|
78	    ((func << OFW_PCI_PHYS_HI_FUNCTIONSHIFT) &
79	    OFW_PCI_PHYS_HI_FUNCTIONMASK);
80}
81
82void
83genppc_pci_ofmethod_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp,
84    int *fp)
85{
86
87	if (bp != NULL)
88		*bp = OFW_PCI_PHYS_HI_BUS(tag);
89	if (dp != NULL)
90		*dp = OFW_PCI_PHYS_HI_DEVICE(tag);
91	if (fp != NULL)
92		*fp = OFW_PCI_PHYS_HI_FUNCTION(tag);
93	return;
94}
95
96pcireg_t
97genppc_pci_ofmethod_conf_read(void *v, pcitag_t tag, int reg)
98{
99	pci_chipset_tag_t pc = (pci_chipset_tag_t)v;
100	pcireg_t data;
101
102	if ((unsigned int)reg >= PCI_CONF_SIZE)
103		return (pcireg_t) -1;
104
105	tag &= OFW_PCI_PHYS_HI_BUSMASK | OFW_PCI_PHYS_HI_DEVICEMASK |
106	    OFW_PCI_PHYS_HI_FUNCTIONMASK;
107	tag |= reg & OFW_PCI_PHYS_HI_REGISTERMASK;
108	if (OF_call_method("config-l@", pc->pc_ihandle, 1, 1, tag, &data) < 0)
109		return (pcireg_t) -1;
110	/*printf("data=0x%x\n", data); */
111	return data;
112}
113
114void
115genppc_pci_ofmethod_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data)
116{
117	pci_chipset_tag_t pc = (pci_chipset_tag_t)v;
118
119	if ((unsigned int)reg >= PCI_CONF_SIZE)
120		return;
121
122	tag &= OFW_PCI_PHYS_HI_BUSMASK | OFW_PCI_PHYS_HI_DEVICEMASK |
123	    OFW_PCI_PHYS_HI_FUNCTIONMASK;
124	tag |= reg & OFW_PCI_PHYS_HI_REGISTERMASK;
125	OF_call_method("config-l!", pc->pc_ihandle, 2, 0, data, tag);
126}
127