xref: /freebsd/sys/dev/rtwn/pci/rtwn_pci_reg.c (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1 /*      $OpenBSD: if_rtwn.c,v 1.6 2015/08/28 00:03:53 deraadt Exp $     */
2 
3 /*-
4  * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
5  * Copyright (c) 2015 Stefan Sperling <stsp@openbsd.org>
6  * Copyright (c) 2016 Andriy Voskoboinyk <avos@FreeBSD.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23 
24 #include <sys/param.h>
25 #include <sys/lock.h>
26 #include <sys/mutex.h>
27 #include <sys/mbuf.h>
28 #include <sys/kernel.h>
29 #include <sys/socket.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/queue.h>
33 #include <sys/taskqueue.h>
34 #include <sys/bus.h>
35 #include <sys/endian.h>
36 
37 #include <machine/bus.h>
38 #include <machine/resource.h>
39 #include <sys/rman.h>
40 
41 #include <net/if.h>
42 #include <net/ethernet.h>
43 #include <net/if_media.h>
44 
45 #include <net80211/ieee80211_var.h>
46 #include <net80211/ieee80211_radiotap.h>
47 
48 #include <dev/rtwn/if_rtwnvar.h>
49 
50 #include <dev/rtwn/pci/rtwn_pci_var.h>
51 #include <dev/rtwn/pci/rtwn_pci_reg.h>
52 
53 int
54 rtwn_pci_write_1(struct rtwn_softc *sc, uint16_t addr, uint8_t val)
55 {
56 	struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
57 
58 	bus_space_write_1(pc->pc_st, pc->pc_sh, addr, val);
59 
60 	return (0);
61 }
62 
63 int
64 rtwn_pci_write_2(struct rtwn_softc *sc, uint16_t addr, uint16_t val)
65 {
66 	struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
67 
68 	val = htole16(val);
69 	bus_space_write_2(pc->pc_st, pc->pc_sh, addr, val);
70 
71 	return (0);
72 }
73 
74 int
75 rtwn_pci_write_4(struct rtwn_softc *sc, uint16_t addr, uint32_t val)
76 {
77 	struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
78 
79 	val = htole32(val);
80 	bus_space_write_4(pc->pc_st, pc->pc_sh, addr, val);
81 
82 	return (0);
83 }
84 
85 uint8_t
86 rtwn_pci_read_1(struct rtwn_softc *sc, uint16_t addr)
87 {
88 	struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
89 
90 	return (bus_space_read_1(pc->pc_st, pc->pc_sh, addr));
91 }
92 
93 uint16_t
94 rtwn_pci_read_2(struct rtwn_softc *sc, uint16_t addr)
95 {
96 	struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
97 	uint16_t val;
98 
99 	val = bus_space_read_2(pc->pc_st, pc->pc_sh, addr);
100 	return le16toh(val);
101 }
102 
103 uint32_t
104 rtwn_pci_read_4(struct rtwn_softc *sc, uint16_t addr)
105 {
106 	struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
107 	uint32_t val;
108 
109 	val = bus_space_read_4(pc->pc_st, pc->pc_sh, addr);
110 	return le32toh(val);
111 }
112 
113 void
114 rtwn_pci_delay(struct rtwn_softc *sc, int usec)
115 {
116 	if (usec < 1000)
117 		DELAY(usec);
118 	else {
119 		(void) mtx_sleep(sc, &sc->sc_mtx, 0, "rtwn_pci",
120 		    msecs_to_ticks(usec / 1000));
121 	}
122 }
123