summaryrefslogtreecommitdiff
path: root/sys/dev/pci/iosf_pci.c
blob: 886a39c60714662c5a844dfcbf78f5a145c52adc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*	$OpenBSD: iosf_pci.c,v 1.1 2023/04/23 00:20:26 dlg Exp $ */

/*
 * Copyright (c) 2023 David Gwynne <dlg@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/mutex.h>
#include <sys/rwlock.h>

#include <machine/bus.h>

#include <dev/pci/pcivar.h>
#include <dev/pci/pcidevs.h>

#include <dev/ic/iosfvar.h>

/*
 * Intel OnChip System Fabric driver
 */

struct iosf_pci_softc {
	struct device		sc_dev;

	pci_chipset_tag_t	sc_pc;
	pcitag_t		sc_pcitag;

	int			sc_semaddr;

	struct iosf_mbi		sc_mbi;
};

static int	iosf_pci_match(struct device *, void *, void *);
static void	iosf_pci_attach(struct device *, struct device *, void *);

static uint32_t	iosf_pci_mbi_mdr_rd(struct iosf_mbi *, uint32_t, uint32_t);
static void	iosf_pci_mbi_mdr_wr(struct iosf_mbi *, uint32_t, uint32_t,
		    uint32_t);

const struct cfattach iosf_pci_ca = {
	sizeof(struct iosf_pci_softc), iosf_pci_match, iosf_pci_attach
};

struct iosf_pci_device {
	struct pci_matchid		id_pm;
	int				id_semaddr;
};

static const struct iosf_pci_device iosf_pci_devices[] = {
	{ { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_BAYTRAIL_HB },	0x7 },
	{ { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_BSW_HB },	0x10e },
	/* Quark X1000, -1 */
	/* Tangier, -1 */
};

static const struct iosf_pci_device *
iosf_pci_device_match(struct pci_attach_args *pa)
{
	pci_vendor_id_t vid = PCI_VENDOR(pa->pa_id);
	pci_product_id_t pid = PCI_PRODUCT(pa->pa_id);
	const struct iosf_pci_device *id;
	size_t i;

	for (i = 0; i < nitems(iosf_pci_devices); i++) {
		id = &iosf_pci_devices[i];
		if (id->id_pm.pm_vid == vid && id->id_pm.pm_pid == pid)
			return (id);
	}

	return (NULL);
}

static int
iosf_pci_match(struct device *parent, void *match, void *aux)
{
	struct pci_attach_args *pa = aux;

	if (iosf_pci_device_match(pa) != NULL) {
		/* match higher than pchb(4) */
		return (2);
	}

	return (0);
}

static void
iosf_pci_attach(struct device *parent, struct device *self, void *aux)
{
	struct iosf_pci_softc *sc = (struct iosf_pci_softc *)self;
	struct pci_attach_args *pa = aux;
	const struct iosf_pci_device *id = iosf_pci_device_match(pa);

	sc->sc_pc = pa->pa_pc;
	sc->sc_pcitag = pa->pa_tag;

	printf(": mbi\n");

	sc->sc_mbi.mbi_dev = self;
	sc->sc_mbi.mbi_prio = 2; /* prefer pci over acpi ops */
	sc->sc_mbi.mbi_semaddr = id->id_semaddr;
	sc->sc_mbi.mbi_mdr_rd = iosf_pci_mbi_mdr_rd;
	sc->sc_mbi.mbi_mdr_wr = iosf_pci_mbi_mdr_wr;

	iosf_mbi_attach(&sc->sc_mbi);
}

/*
 * mbi mdr pciconf operations
 */

#define IOSF_PCI_MBI_MCR		0xd0
#define IOSF_PCI_MBI_MDR		0xd4
#define IOSF_PCI_MBI_MCRX		0xd8

static uint32_t
iosf_pci_mbi_mdr_rd(struct iosf_mbi *mbi, uint32_t mcr, uint32_t mcrx)
{
	struct iosf_pci_softc *sc = (struct iosf_pci_softc *)mbi->mbi_dev;

	if (mcrx != 0) {
		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
		    IOSF_PCI_MBI_MCRX, mcrx);
	}
	pci_conf_write(sc->sc_pc, sc->sc_pcitag, IOSF_PCI_MBI_MCR, mcr);

	return (pci_conf_read(sc->sc_pc, sc->sc_pcitag, IOSF_PCI_MBI_MDR));
}

static void
iosf_pci_mbi_mdr_wr(struct iosf_mbi *mbi, uint32_t mcr, uint32_t mcrx,
    uint32_t mdr)
{
	struct iosf_pci_softc *sc = (struct iosf_pci_softc *)mbi->mbi_dev;

	pci_conf_write(sc->sc_pc, sc->sc_pcitag, IOSF_PCI_MBI_MDR, mdr);

	if (mcrx != 0) {
		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
		    IOSF_PCI_MBI_MCRX, mcrx);
	}

	pci_conf_write(sc->sc_pc, sc->sc_pcitag, IOSF_PCI_MBI_MCR, mcr);
}