1*0f9891f1Sjsg /* $OpenBSD: iosf_pci.c,v 1.2 2024/05/24 06:02:57 jsg Exp $ */
2a72a7a26Sdlg
3a72a7a26Sdlg /*
4a72a7a26Sdlg * Copyright (c) 2023 David Gwynne <dlg@openbsd.org>
5a72a7a26Sdlg *
6a72a7a26Sdlg * Permission to use, copy, modify, and distribute this software for any
7a72a7a26Sdlg * purpose with or without fee is hereby granted, provided that the above
8a72a7a26Sdlg * copyright notice and this permission notice appear in all copies.
9a72a7a26Sdlg *
10a72a7a26Sdlg * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11a72a7a26Sdlg * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12a72a7a26Sdlg * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13a72a7a26Sdlg * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14a72a7a26Sdlg * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15a72a7a26Sdlg * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16a72a7a26Sdlg * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17a72a7a26Sdlg */
18a72a7a26Sdlg
19a72a7a26Sdlg #include <sys/param.h>
20a72a7a26Sdlg #include <sys/systm.h>
21a72a7a26Sdlg #include <sys/device.h>
22a72a7a26Sdlg
23a72a7a26Sdlg #include <machine/bus.h>
24a72a7a26Sdlg
25a72a7a26Sdlg #include <dev/pci/pcivar.h>
26a72a7a26Sdlg #include <dev/pci/pcidevs.h>
27a72a7a26Sdlg
28a72a7a26Sdlg #include <dev/ic/iosfvar.h>
29a72a7a26Sdlg
30a72a7a26Sdlg /*
31a72a7a26Sdlg * Intel OnChip System Fabric driver
32a72a7a26Sdlg */
33a72a7a26Sdlg
34a72a7a26Sdlg struct iosf_pci_softc {
35a72a7a26Sdlg struct device sc_dev;
36a72a7a26Sdlg
37a72a7a26Sdlg pci_chipset_tag_t sc_pc;
38a72a7a26Sdlg pcitag_t sc_pcitag;
39a72a7a26Sdlg
40a72a7a26Sdlg int sc_semaddr;
41a72a7a26Sdlg
42a72a7a26Sdlg struct iosf_mbi sc_mbi;
43a72a7a26Sdlg };
44a72a7a26Sdlg
45a72a7a26Sdlg static int iosf_pci_match(struct device *, void *, void *);
46a72a7a26Sdlg static void iosf_pci_attach(struct device *, struct device *, void *);
47a72a7a26Sdlg
48a72a7a26Sdlg static uint32_t iosf_pci_mbi_mdr_rd(struct iosf_mbi *, uint32_t, uint32_t);
49a72a7a26Sdlg static void iosf_pci_mbi_mdr_wr(struct iosf_mbi *, uint32_t, uint32_t,
50a72a7a26Sdlg uint32_t);
51a72a7a26Sdlg
52a72a7a26Sdlg const struct cfattach iosf_pci_ca = {
53a72a7a26Sdlg sizeof(struct iosf_pci_softc), iosf_pci_match, iosf_pci_attach
54a72a7a26Sdlg };
55a72a7a26Sdlg
56a72a7a26Sdlg struct iosf_pci_device {
57a72a7a26Sdlg struct pci_matchid id_pm;
58a72a7a26Sdlg int id_semaddr;
59a72a7a26Sdlg };
60a72a7a26Sdlg
61a72a7a26Sdlg static const struct iosf_pci_device iosf_pci_devices[] = {
62a72a7a26Sdlg { { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_BAYTRAIL_HB }, 0x7 },
63a72a7a26Sdlg { { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_BSW_HB }, 0x10e },
64a72a7a26Sdlg /* Quark X1000, -1 */
65a72a7a26Sdlg /* Tangier, -1 */
66a72a7a26Sdlg };
67a72a7a26Sdlg
68a72a7a26Sdlg static const struct iosf_pci_device *
iosf_pci_device_match(struct pci_attach_args * pa)69a72a7a26Sdlg iosf_pci_device_match(struct pci_attach_args *pa)
70a72a7a26Sdlg {
71a72a7a26Sdlg pci_vendor_id_t vid = PCI_VENDOR(pa->pa_id);
72a72a7a26Sdlg pci_product_id_t pid = PCI_PRODUCT(pa->pa_id);
73a72a7a26Sdlg const struct iosf_pci_device *id;
74a72a7a26Sdlg size_t i;
75a72a7a26Sdlg
76a72a7a26Sdlg for (i = 0; i < nitems(iosf_pci_devices); i++) {
77a72a7a26Sdlg id = &iosf_pci_devices[i];
78a72a7a26Sdlg if (id->id_pm.pm_vid == vid && id->id_pm.pm_pid == pid)
79a72a7a26Sdlg return (id);
80a72a7a26Sdlg }
81a72a7a26Sdlg
82a72a7a26Sdlg return (NULL);
83a72a7a26Sdlg }
84a72a7a26Sdlg
85a72a7a26Sdlg static int
iosf_pci_match(struct device * parent,void * match,void * aux)86a72a7a26Sdlg iosf_pci_match(struct device *parent, void *match, void *aux)
87a72a7a26Sdlg {
88a72a7a26Sdlg struct pci_attach_args *pa = aux;
89a72a7a26Sdlg
90a72a7a26Sdlg if (iosf_pci_device_match(pa) != NULL) {
91a72a7a26Sdlg /* match higher than pchb(4) */
92a72a7a26Sdlg return (2);
93a72a7a26Sdlg }
94a72a7a26Sdlg
95a72a7a26Sdlg return (0);
96a72a7a26Sdlg }
97a72a7a26Sdlg
98a72a7a26Sdlg static void
iosf_pci_attach(struct device * parent,struct device * self,void * aux)99a72a7a26Sdlg iosf_pci_attach(struct device *parent, struct device *self, void *aux)
100a72a7a26Sdlg {
101a72a7a26Sdlg struct iosf_pci_softc *sc = (struct iosf_pci_softc *)self;
102a72a7a26Sdlg struct pci_attach_args *pa = aux;
103a72a7a26Sdlg const struct iosf_pci_device *id = iosf_pci_device_match(pa);
104a72a7a26Sdlg
105a72a7a26Sdlg sc->sc_pc = pa->pa_pc;
106a72a7a26Sdlg sc->sc_pcitag = pa->pa_tag;
107a72a7a26Sdlg
108a72a7a26Sdlg printf(": mbi\n");
109a72a7a26Sdlg
110a72a7a26Sdlg sc->sc_mbi.mbi_dev = self;
111a72a7a26Sdlg sc->sc_mbi.mbi_prio = 2; /* prefer pci over acpi ops */
112a72a7a26Sdlg sc->sc_mbi.mbi_semaddr = id->id_semaddr;
113a72a7a26Sdlg sc->sc_mbi.mbi_mdr_rd = iosf_pci_mbi_mdr_rd;
114a72a7a26Sdlg sc->sc_mbi.mbi_mdr_wr = iosf_pci_mbi_mdr_wr;
115a72a7a26Sdlg
116a72a7a26Sdlg iosf_mbi_attach(&sc->sc_mbi);
117a72a7a26Sdlg }
118a72a7a26Sdlg
119a72a7a26Sdlg /*
120a72a7a26Sdlg * mbi mdr pciconf operations
121a72a7a26Sdlg */
122a72a7a26Sdlg
123a72a7a26Sdlg #define IOSF_PCI_MBI_MCR 0xd0
124a72a7a26Sdlg #define IOSF_PCI_MBI_MDR 0xd4
125a72a7a26Sdlg #define IOSF_PCI_MBI_MCRX 0xd8
126a72a7a26Sdlg
127a72a7a26Sdlg static uint32_t
iosf_pci_mbi_mdr_rd(struct iosf_mbi * mbi,uint32_t mcr,uint32_t mcrx)128a72a7a26Sdlg iosf_pci_mbi_mdr_rd(struct iosf_mbi *mbi, uint32_t mcr, uint32_t mcrx)
129a72a7a26Sdlg {
130a72a7a26Sdlg struct iosf_pci_softc *sc = (struct iosf_pci_softc *)mbi->mbi_dev;
131a72a7a26Sdlg
132a72a7a26Sdlg if (mcrx != 0) {
133a72a7a26Sdlg pci_conf_write(sc->sc_pc, sc->sc_pcitag,
134a72a7a26Sdlg IOSF_PCI_MBI_MCRX, mcrx);
135a72a7a26Sdlg }
136a72a7a26Sdlg pci_conf_write(sc->sc_pc, sc->sc_pcitag, IOSF_PCI_MBI_MCR, mcr);
137a72a7a26Sdlg
138a72a7a26Sdlg return (pci_conf_read(sc->sc_pc, sc->sc_pcitag, IOSF_PCI_MBI_MDR));
139a72a7a26Sdlg }
140a72a7a26Sdlg
141a72a7a26Sdlg static void
iosf_pci_mbi_mdr_wr(struct iosf_mbi * mbi,uint32_t mcr,uint32_t mcrx,uint32_t mdr)142a72a7a26Sdlg iosf_pci_mbi_mdr_wr(struct iosf_mbi *mbi, uint32_t mcr, uint32_t mcrx,
143a72a7a26Sdlg uint32_t mdr)
144a72a7a26Sdlg {
145a72a7a26Sdlg struct iosf_pci_softc *sc = (struct iosf_pci_softc *)mbi->mbi_dev;
146a72a7a26Sdlg
147a72a7a26Sdlg pci_conf_write(sc->sc_pc, sc->sc_pcitag, IOSF_PCI_MBI_MDR, mdr);
148a72a7a26Sdlg
149a72a7a26Sdlg if (mcrx != 0) {
150a72a7a26Sdlg pci_conf_write(sc->sc_pc, sc->sc_pcitag,
151a72a7a26Sdlg IOSF_PCI_MBI_MCRX, mcrx);
152a72a7a26Sdlg }
153a72a7a26Sdlg
154a72a7a26Sdlg pci_conf_write(sc->sc_pc, sc->sc_pcitag, IOSF_PCI_MBI_MCR, mcr);
155a72a7a26Sdlg }
156