Merge from vendor branch INTEL_ACPICA:
[dragonfly.git] / sys / dev / netif / acx / acxcmd.c
1 /*
2  * Copyright (c) 2006 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Sepherosa Ziehau <sepherosa@gmail.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/dev/netif/acx/acxcmd.c,v 1.5 2007/01/01 03:31:52 sephe Exp $
35  */
36
37 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/endian.h>
40 #include <sys/kernel.h>
41 #include <sys/rman.h>
42 #include <sys/socket.h>
43 #include <sys/sysctl.h>
44
45 #include <net/if.h>
46 #include <net/if_arp.h>
47 #include <net/if_media.h>
48
49 #include <netproto/802_11/ieee80211_var.h>
50
51 #define ACX_DEBUG
52
53 #include "if_acxreg.h"
54 #include "if_acxvar.h"
55 #include "acxcmd.h"
56
57 #define CMDPRM_WRITE_REGION_1(sc, r, rlen)              \
58         bus_space_write_region_1((sc)->sc_mem2_bt,      \
59                                  (sc)->sc_mem2_bh,      \
60                                  (sc)->sc_cmd_param,    \
61                                  (const uint8_t *)(r), (rlen))
62
63 #define CMDPRM_READ_REGION_1(sc, r, rlen)                               \
64         bus_space_read_region_1((sc)->sc_mem2_bt, (sc)->sc_mem2_bh,     \
65                                 (sc)->sc_cmd_param, (uint8_t *)(r), (rlen))
66
67 /*
68  * This will clear previous command's
69  * execution status too
70  */
71 #define CMD_WRITE_4(sc, val)                                    \
72         bus_space_write_4((sc)->sc_mem2_bt, (sc)->sc_mem2_bh,   \
73                           (sc)->sc_cmd, (val))
74 #define CMD_READ_4(sc)          \
75         bus_space_read_4((sc)->sc_mem2_bt, (sc)->sc_mem2_bh, (sc)->sc_cmd)
76
77 /*
78  * acx command register layerout:
79  * upper 16bits are command execution status
80  * lower 16bits are command to be executed
81  */
82 #define ACX_CMD_STATUS_SHIFT    16
83 #define ACX_CMD_STATUS_OK       1
84
85 struct radio_init {
86         uint32_t        radio_ofs;      /* radio firmware offset */
87         uint32_t        radio_len;      /* radio firmware length */
88 } __packed;
89
90 struct bss_join_hdr {
91         uint8_t         bssid[IEEE80211_ADDR_LEN];
92         uint16_t        beacon_intvl;
93         uint8_t         chip_spec[3];
94         uint8_t         ndata_txrate;   /* see ACX_NDATA_TXRATE_ */
95         uint8_t         ndata_txopt;    /* see ACX_NDATA_TXOPT_ */
96         uint8_t         mode;           /* see ACX_MODE_ */
97         uint8_t         channel;
98         uint8_t         esslen;
99         char            essid[1];
100 } __packed;
101
102 /*
103  * non-data frame tx rate
104  */
105 #define ACX_NDATA_TXRATE_2              20      /* 2Mbits/s */
106
107 /*
108  * non-data frame tx options
109  */
110 #define ACX_NDATA_TXOPT_PBCC            0x40
111 #define ACX_NDATA_TXOPT_OFDM            0x20
112 #define ACX_NDATA_TXOPT_SHORT_PREAMBLE  0x10
113
114 #define BSS_JOIN_BUFLEN         \
115         (sizeof(struct bss_join_hdr) + IEEE80211_NWID_LEN - 1)
116 #define BSS_JOIN_PARAM_SIZE(bj) \
117         (sizeof(struct bss_join_hdr) + (bj)->esslen)
118
119 void
120 acx_init_cmd_reg(struct acx_softc *sc)
121 {
122         sc->sc_cmd = CSR_READ_4(sc, ACXREG_CMD_REG_OFFSET);
123         sc->sc_cmd_param = sc->sc_cmd + ACX_CMD_REG_SIZE;
124
125         /* Clear command & status */
126         CMD_WRITE_4(sc, 0);
127 }
128
129 int
130 acx_join_bss(struct acx_softc *sc, uint8_t mode, struct ieee80211_node *node)
131 {
132         uint8_t bj_buf[BSS_JOIN_BUFLEN];
133         struct bss_join_hdr *bj;
134         int i, dtim_intvl;
135
136         bzero(bj_buf, sizeof(bj_buf));
137         bj = (struct bss_join_hdr *)bj_buf;
138
139         for (i = 0; i < IEEE80211_ADDR_LEN; ++i)
140                 bj->bssid[i] = node->ni_bssid[IEEE80211_ADDR_LEN - i - 1];
141
142         bj->beacon_intvl = htole16(node->ni_intval);
143
144         dtim_intvl = sc->sc_ic.ic_dtim_period;
145         sc->chip_set_bss_join_param(sc, bj->chip_spec, dtim_intvl);
146
147         bj->ndata_txrate = ACX_NDATA_TXRATE_2;
148         bj->ndata_txopt = 0;
149         bj->mode = mode;
150         bj->channel = ieee80211_chan2ieee(&sc->sc_ic, node->ni_chan);
151         bj->esslen = node->ni_esslen;
152         bcopy(node->ni_essid, bj->essid, node->ni_esslen);
153
154         DPRINTF((&sc->sc_ic.ic_if, "join BSS/IBSS on channel %d\n", bj->channel));
155         return acx_exec_command(sc, ACXCMD_JOIN_BSS,
156                                 bj, BSS_JOIN_PARAM_SIZE(bj), NULL, 0);
157 }
158
159 int
160 acx_enable_txchan(struct acx_softc *sc, uint8_t chan)
161 {
162         return acx_exec_command(sc, ACXCMD_ENABLE_TXCHAN, &chan, sizeof(chan),
163                                 NULL, 0);
164 }
165
166 int
167 acx_enable_rxchan(struct acx_softc *sc, uint8_t chan)
168 {
169         return acx_exec_command(sc, ACXCMD_ENABLE_RXCHAN, &chan, sizeof(chan),
170                                 NULL, 0);
171 }
172
173 int
174 acx_get_conf(struct acx_softc *sc, uint16_t conf_id, void *conf,
175              uint16_t conf_len)
176 {
177         struct acx_conf *confcom;
178
179         if (conf_len < sizeof(*confcom)) {
180                 if_printf(&sc->sc_ic.ic_if, "%s configure data is too short\n",
181                           __func__);
182                 return 1;
183         }
184
185         confcom = conf;
186         confcom->conf_id = htole16(conf_id);
187         confcom->conf_data_len = htole16(conf_len - sizeof(*confcom));
188
189         return acx_exec_command(sc, ACXCMD_GET_CONF, confcom, sizeof(*confcom),
190                                 conf, conf_len);
191 }
192
193 int
194 acx_set_conf(struct acx_softc *sc, uint16_t conf_id, void *conf,
195              uint16_t conf_len)
196 {
197         struct acx_conf *confcom;
198
199         if (conf_len < sizeof(*confcom)) {
200                 if_printf(&sc->sc_ic.ic_if, "%s configure data is too short\n",
201                           __func__);
202                 return 1;
203         }
204
205         confcom = conf;
206         confcom->conf_id = htole16(conf_id);
207         confcom->conf_data_len = htole16(conf_len - sizeof(*confcom));
208
209         return acx_exec_command(sc, ACXCMD_SET_CONF, conf, conf_len, NULL, 0);
210 }
211
212 int
213 acx_set_tmplt(struct acx_softc *sc, uint16_t cmd, void *tmplt,
214               uint16_t tmplt_len)
215 {
216         uint16_t *size;
217
218         if (tmplt_len < sizeof(*size)) {
219                 if_printf(&sc->sc_ic.ic_if, "%s template is too short\n",
220                           __func__);
221                 return 1;
222         }
223
224         size = tmplt;
225         *size = htole16(tmplt_len - sizeof(*size));
226
227         return acx_exec_command(sc, cmd, tmplt, tmplt_len, NULL, 0);
228 }
229
230 int
231 acx_init_radio(struct acx_softc *sc, uint32_t radio_ofs, uint32_t radio_len)
232 {
233         struct radio_init r;
234
235         r.radio_ofs = htole32(radio_ofs);
236         r.radio_len = htole32(radio_len);
237         return acx_exec_command(sc, ACXCMD_INIT_RADIO, &r, sizeof(r), NULL, 0);
238 }
239
240 int
241 acx_exec_command(struct acx_softc *sc, uint16_t cmd, void *param,
242                  uint16_t param_len, void *result, uint16_t result_len)
243 {
244         uint16_t status;
245         int i, ret;
246
247         ASSERT_SERIALIZED(sc->sc_ic.ic_if.if_serializer);
248
249         if ((sc->sc_flags & ACX_FLAG_FW_LOADED) == 0) {
250                 if_printf(&sc->sc_ic.ic_if, "cmd 0x%04x failed (base firmware "
251                           "not loaded)", cmd);
252                 return 1;
253         }
254
255         ret = 0;
256
257         if (param != NULL && param_len != 0) {
258                 /* Set command param */
259                 CMDPRM_WRITE_REGION_1(sc, param, param_len);
260         }
261
262         /* Set command */
263         CMD_WRITE_4(sc, cmd);
264
265         /* Exec command */
266         CSR_WRITE_2(sc, ACXREG_INTR_TRIG, ACXRV_TRIG_CMD_FINI);
267         DELAY(50);      /* XXX maybe 100 */
268
269         /* Wait for command to complete */
270         if (cmd == ACXCMD_INIT_RADIO) {
271                 /* XXX radio initialization is extremely long */
272                 tsleep(&cmd, 0, "rdinit", (150 * hz) / 1000);   /* 150ms */
273         }
274
275 #define CMDWAIT_RETRY_MAX       1000
276         for (i = 0; i < CMDWAIT_RETRY_MAX; ++i) {
277                 uint16_t reg;
278
279                 reg = CSR_READ_2(sc, ACXREG_INTR_STATUS);
280                 if (reg & ACXRV_INTR_CMD_FINI) {
281                         CSR_WRITE_2(sc, ACXREG_INTR_ACK, ACXRV_INTR_CMD_FINI);
282                         break;
283                 }
284                 DELAY(50);
285         }
286         if (i == CMDWAIT_RETRY_MAX) {
287                 if_printf(&sc->sc_ic.ic_if, "cmd %04x failed (timeout)\n", cmd);
288                 ret = 1;
289                 goto back;
290         }
291 #undef CMDWAIT_RETRY_MAX
292
293         /* Get command exec status */
294         status = (CMD_READ_4(sc) >> ACX_CMD_STATUS_SHIFT);
295         if (status != ACX_CMD_STATUS_OK) {
296                 if_printf(&sc->sc_ic.ic_if, "cmd %04x failed\n", cmd);
297                 ret = 1;
298                 goto back;
299         }
300
301         if (result != NULL && result_len != 0) {
302                 /* Get command result */
303                 CMDPRM_READ_REGION_1(sc, result, result_len);
304         }
305
306 back:
307         CMD_WRITE_4(sc, 0);
308         return ret;
309 }