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/param.h>
22 #include <sys/lock.h>
23 #include <sys/mutex.h>
24 #include <sys/mbuf.h>
25 #include <sys/kernel.h>
26 #include <sys/socket.h>
27 #include <sys/systm.h>
28 #include <sys/malloc.h>
29 #include <sys/queue.h>
30 #include <sys/taskqueue.h>
31 #include <sys/bus.h>
32 #include <sys/endian.h>
33
34 #include <machine/bus.h>
35 #include <machine/resource.h>
36 #include <sys/rman.h>
37
38 #include <net/if.h>
39 #include <net/ethernet.h>
40 #include <net/if_media.h>
41
42 #include <net80211/ieee80211_var.h>
43 #include <net80211/ieee80211_radiotap.h>
44
45 #include <dev/rtwn/if_rtwnvar.h>
46
47 #include <dev/rtwn/pci/rtwn_pci_var.h>
48 #include <dev/rtwn/pci/rtwn_pci_reg.h>
49
50 int
rtwn_pci_write_1(struct rtwn_softc * sc,uint16_t addr,uint8_t val)51 rtwn_pci_write_1(struct rtwn_softc *sc, uint16_t addr, uint8_t val)
52 {
53 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
54
55 bus_space_write_1(pc->pc_st, pc->pc_sh, addr, val);
56
57 return (0);
58 }
59
60 int
rtwn_pci_write_2(struct rtwn_softc * sc,uint16_t addr,uint16_t val)61 rtwn_pci_write_2(struct rtwn_softc *sc, uint16_t addr, uint16_t val)
62 {
63 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
64
65 val = htole16(val);
66 bus_space_write_2(pc->pc_st, pc->pc_sh, addr, val);
67
68 return (0);
69 }
70
71 int
rtwn_pci_write_4(struct rtwn_softc * sc,uint16_t addr,uint32_t val)72 rtwn_pci_write_4(struct rtwn_softc *sc, uint16_t addr, uint32_t val)
73 {
74 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
75
76 val = htole32(val);
77 bus_space_write_4(pc->pc_st, pc->pc_sh, addr, val);
78
79 return (0);
80 }
81
82 uint8_t
rtwn_pci_read_1(struct rtwn_softc * sc,uint16_t addr)83 rtwn_pci_read_1(struct rtwn_softc *sc, uint16_t addr)
84 {
85 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
86
87 return (bus_space_read_1(pc->pc_st, pc->pc_sh, addr));
88 }
89
90 uint16_t
rtwn_pci_read_2(struct rtwn_softc * sc,uint16_t addr)91 rtwn_pci_read_2(struct rtwn_softc *sc, uint16_t addr)
92 {
93 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
94 uint16_t val;
95
96 val = bus_space_read_2(pc->pc_st, pc->pc_sh, addr);
97 return le16toh(val);
98 }
99
100 uint32_t
rtwn_pci_read_4(struct rtwn_softc * sc,uint16_t addr)101 rtwn_pci_read_4(struct rtwn_softc *sc, uint16_t addr)
102 {
103 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
104 uint32_t val;
105
106 val = bus_space_read_4(pc->pc_st, pc->pc_sh, addr);
107 return le32toh(val);
108 }
109
110 void
rtwn_pci_delay(struct rtwn_softc * sc,int usec)111 rtwn_pci_delay(struct rtwn_softc *sc, int usec)
112 {
113 if (usec < 1000)
114 DELAY(usec);
115 else {
116 (void) mtx_sleep(sc, &sc->sc_mtx, 0, "rtwn_pci",
117 msecs_to_ticks(usec / 1000));
118 }
119 }
120