amdsmn.c revision 1.1 1 1.1 christos /* $NetBSD: amdsmn.c,v 1.1 2018/01/25 01:22:21 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2017 Conrad Meyer <cem (at) FreeBSD.org>
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * NetBSD port by Ian Clark <mrrooster (at) gmail.com>
8 1.1 christos *
9 1.1 christos * Redistribution and use in source and binary forms, with or without
10 1.1 christos * modification, are permitted provided that the following conditions
11 1.1 christos * are met:
12 1.1 christos * 1. Redistributions of source code must retain the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer.
14 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 christos * notice, this list of conditions and the following disclaimer in the
16 1.1 christos * documentation and/or other materials provided with the distribution.
17 1.1 christos *
18 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 1.1 christos * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 1.1 christos * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 1.1 christos * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 1.1 christos * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 1.1 christos * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 1.1 christos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 1.1 christos * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
29 1.1 christos */
30 1.1 christos
31 1.1 christos #include <sys/cdefs.h>
32 1.1 christos __KERNEL_RCSID(0, "$NetBSD: amdsmn.c,v 1.1 2018/01/25 01:22:21 christos Exp $ ");
33 1.1 christos
34 1.1 christos /*
35 1.1 christos * Driver for the AMD Family 17h CPU System Management Network.
36 1.1 christos */
37 1.1 christos
38 1.1 christos #include <sys/param.h>
39 1.1 christos #include <sys/device.h>
40 1.1 christos #include <sys/errno.h>
41 1.1 christos #include <sys/mutex.h>
42 1.1 christos #include <sys/systm.h>
43 1.1 christos #include <sys/cpu.h>
44 1.1 christos
45 1.1 christos #include <machine/specialreg.h>
46 1.1 christos
47 1.1 christos #include <dev/pci/pcireg.h>
48 1.1 christos #include <dev/pci/pcivar.h>
49 1.1 christos #include <dev/pci/pcidevs.h>
50 1.1 christos
51 1.1 christos #include "amdsmn.h"
52 1.1 christos
53 1.1 christos #define SMN_ADDR_REG 0x60
54 1.1 christos #define SMN_DATA_REG 0x64
55 1.1 christos #define AMD_17H_MANAGEMENT_NETWORK_PCI_ID 0x14501022
56 1.1 christos
57 1.1 christos struct amdsmn_softc {
58 1.1 christos kmutex_t smn_lock;
59 1.1 christos struct pci_attach_args pa;
60 1.1 christos pci_chipset_tag_t pc;
61 1.1 christos pcitag_t pcitag;
62 1.1 christos };
63 1.1 christos
64 1.1 christos static int amdsmn_match(device_t, cfdata_t, void *);
65 1.1 christos static void amdsmn_attach(device_t, device_t, void *);
66 1.1 christos static int amdsmn_detach(device_t, int);
67 1.1 christos static int amdsmn_misc_search(device_t, cfdata_t, const int *, void *);
68 1.1 christos
69 1.1 christos CFATTACH_DECL_NEW(amdsmn, sizeof(struct amdsmn_softc), amdsmn_match,
70 1.1 christos amdsmn_attach, amdsmn_detach, NULL);
71 1.1 christos
72 1.1 christos static int
73 1.1 christos amdsmn_match(device_t parent, cfdata_t match, void *aux)
74 1.1 christos {
75 1.1 christos struct pci_attach_args *pa = aux;
76 1.1 christos
77 1.1 christos return pa->pa_id == AMD_17H_MANAGEMENT_NETWORK_PCI_ID ? 2 : 0;
78 1.1 christos }
79 1.1 christos
80 1.1 christos static int
81 1.1 christos amdsmn_misc_search(device_t parent, cfdata_t cf, const int *locs, void *aux)
82 1.1 christos {
83 1.1 christos if (config_match(parent, cf, aux))
84 1.1 christos config_attach_loc(parent, cf, locs, aux, NULL);
85 1.1 christos
86 1.1 christos return 0;
87 1.1 christos }
88 1.1 christos
89 1.1 christos static void
90 1.1 christos amdsmn_attach(device_t parent, device_t self, void *aux)
91 1.1 christos {
92 1.1 christos struct amdsmn_softc *sc = device_private(self);
93 1.1 christos struct pci_attach_args *pa = aux;
94 1.1 christos
95 1.1 christos mutex_init(&sc->smn_lock, MUTEX_DEFAULT, IPL_NONE);
96 1.1 christos sc->pa = *pa;
97 1.1 christos sc->pc = pa->pa_pc;
98 1.1 christos sc->pcitag = pa->pa_tag;
99 1.1 christos aprint_normal(": AMD Family 17h System Management Network\n");
100 1.1 christos config_search_loc(amdsmn_misc_search, self, "amdsmn", NULL, &sc->pa);
101 1.1 christos }
102 1.1 christos
103 1.1 christos static int
104 1.1 christos amdsmn_detach(device_t self, int flags)
105 1.1 christos {
106 1.1 christos struct amdsmn_softc *sc = device_private(self);
107 1.1 christos
108 1.1 christos mutex_destroy(&sc->smn_lock);
109 1.1 christos aprint_normal_dev(self,"detach!\n");
110 1.1 christos
111 1.1 christos return 0;
112 1.1 christos }
113 1.1 christos
114 1.1 christos int
115 1.1 christos amdsmn_read(device_t dev, uint32_t addr, uint32_t *value)
116 1.1 christos {
117 1.1 christos struct amdsmn_softc *sc = device_private(dev);
118 1.1 christos
119 1.1 christos mutex_enter(&sc->smn_lock);
120 1.1 christos pci_conf_write(sc->pc, sc->pcitag, SMN_ADDR_REG, addr);
121 1.1 christos *value = pci_conf_read(sc->pc, sc->pcitag, SMN_DATA_REG);
122 1.1 christos mutex_exit(&sc->smn_lock);
123 1.1 christos
124 1.1 christos return 0;
125 1.1 christos }
126 1.1 christos
127 1.1 christos int
128 1.1 christos amdsmn_write(device_t dev, uint32_t addr, uint32_t value)
129 1.1 christos {
130 1.1 christos struct amdsmn_softc *sc = device_private(dev);
131 1.1 christos
132 1.1 christos mutex_enter(&sc->smn_lock);
133 1.1 christos pci_conf_write(sc->pc, sc->pcitag, SMN_ADDR_REG, addr);
134 1.1 christos pci_conf_write(sc->pc, sc->pcitag, SMN_DATA_REG, value);
135 1.1 christos mutex_exit(&sc->smn_lock);
136 1.1 christos
137 1.1 christos return 0;
138 1.1 christos }
139