wl1251: fix ELP_CTRL register reads
[pandora-wifi.git] / drivers / net / wireless / wl12xx / wl1271_spi.c
1 /*
2  * This file is part of wl1271
3  *
4  * Copyright (C) 2008-2009 Nokia Corporation
5  *
6  * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/crc7.h>
27 #include <linux/spi/spi.h>
28
29 #include "wl1271.h"
30 #include "wl12xx_80211.h"
31 #include "wl1271_spi.h"
32
33
34 void wl1271_spi_reset(struct wl1271 *wl)
35 {
36         u8 *cmd;
37         struct spi_transfer t;
38         struct spi_message m;
39
40         cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
41         if (!cmd) {
42                 wl1271_error("could not allocate cmd for spi reset");
43                 return;
44         }
45
46         memset(&t, 0, sizeof(t));
47         spi_message_init(&m);
48
49         memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
50
51         t.tx_buf = cmd;
52         t.len = WSPI_INIT_CMD_LEN;
53         spi_message_add_tail(&t, &m);
54
55         spi_sync(wl->spi, &m);
56
57         wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
58 }
59
60 void wl1271_spi_init(struct wl1271 *wl)
61 {
62         u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
63         struct spi_transfer t;
64         struct spi_message m;
65
66         cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
67         if (!cmd) {
68                 wl1271_error("could not allocate cmd for spi init");
69                 return;
70         }
71
72         memset(crc, 0, sizeof(crc));
73         memset(&t, 0, sizeof(t));
74         spi_message_init(&m);
75
76         /*
77          * Set WSPI_INIT_COMMAND
78          * the data is being send from the MSB to LSB
79          */
80         cmd[2] = 0xff;
81         cmd[3] = 0xff;
82         cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
83         cmd[0] = 0;
84         cmd[7] = 0;
85         cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
86         cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
87
88         if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
89                 cmd[5] |=  WSPI_INIT_CMD_DIS_FIXEDBUSY;
90         else
91                 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
92
93         cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
94                 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
95
96         crc[0] = cmd[1];
97         crc[1] = cmd[0];
98         crc[2] = cmd[7];
99         crc[3] = cmd[6];
100         crc[4] = cmd[5];
101
102         cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
103         cmd[4] |= WSPI_INIT_CMD_END;
104
105         t.tx_buf = cmd;
106         t.len = WSPI_INIT_CMD_LEN;
107         spi_message_add_tail(&t, &m);
108
109         spi_sync(wl->spi, &m);
110
111         wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
112 }
113
114 #define WL1271_BUSY_WORD_TIMEOUT 1000
115
116 /* FIXME: Check busy words, removed due to SPI bug */
117 #if 0
118 static void wl1271_spi_read_busy(struct wl1271 *wl, void *buf, size_t len)
119 {
120         struct spi_transfer t[1];
121         struct spi_message m;
122         u32 *busy_buf;
123         int num_busy_bytes = 0;
124
125         wl1271_info("spi read BUSY!");
126
127         /*
128          * Look for the non-busy word in the read buffer, and if found,
129          * read in the remaining data into the buffer.
130          */
131         busy_buf = (u32 *)buf;
132         for (; (u32)busy_buf < (u32)buf + len; busy_buf++) {
133                 num_busy_bytes += sizeof(u32);
134                 if (*busy_buf & 0x1) {
135                         spi_message_init(&m);
136                         memset(t, 0, sizeof(t));
137                         memmove(buf, busy_buf, len - num_busy_bytes);
138                         t[0].rx_buf = buf + (len - num_busy_bytes);
139                         t[0].len = num_busy_bytes;
140                         spi_message_add_tail(&t[0], &m);
141                         spi_sync(wl->spi, &m);
142                         return;
143                 }
144         }
145
146         /*
147          * Read further busy words from SPI until a non-busy word is
148          * encountered, then read the data itself into the buffer.
149          */
150         wl1271_info("spi read BUSY-polling needed!");
151
152         num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
153         busy_buf = wl->buffer_busyword;
154         while (num_busy_bytes) {
155                 num_busy_bytes--;
156                 spi_message_init(&m);
157                 memset(t, 0, sizeof(t));
158                 t[0].rx_buf = busy_buf;
159                 t[0].len = sizeof(u32);
160                 spi_message_add_tail(&t[0], &m);
161                 spi_sync(wl->spi, &m);
162
163                 if (*busy_buf & 0x1) {
164                         spi_message_init(&m);
165                         memset(t, 0, sizeof(t));
166                         t[0].rx_buf = buf;
167                         t[0].len = len;
168                         spi_message_add_tail(&t[0], &m);
169                         spi_sync(wl->spi, &m);
170                         return;
171                 }
172         }
173
174         /* The SPI bus is unresponsive, the read failed. */
175         memset(buf, 0, len);
176         wl1271_error("SPI read busy-word timeout!\n");
177 }
178 #endif
179
180 void wl1271_spi_raw_read(struct wl1271 *wl, int addr, void *buf,
181                          size_t len, bool fixed)
182 {
183         struct spi_transfer t[3];
184         struct spi_message m;
185         u32 *busy_buf;
186         u32 *cmd;
187
188         cmd = &wl->buffer_cmd;
189         busy_buf = wl->buffer_busyword;
190
191         *cmd = 0;
192         *cmd |= WSPI_CMD_READ;
193         *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
194         *cmd |= addr & WSPI_CMD_BYTE_ADDR;
195
196         if (fixed)
197                 *cmd |= WSPI_CMD_FIXED;
198
199         spi_message_init(&m);
200         memset(t, 0, sizeof(t));
201
202         t[0].tx_buf = cmd;
203         t[0].len = 4;
204         spi_message_add_tail(&t[0], &m);
205
206         /* Busy and non busy words read */
207         t[1].rx_buf = busy_buf;
208         t[1].len = WL1271_BUSY_WORD_LEN;
209         spi_message_add_tail(&t[1], &m);
210
211         t[2].rx_buf = buf;
212         t[2].len = len;
213         spi_message_add_tail(&t[2], &m);
214
215         spi_sync(wl->spi, &m);
216
217         /* FIXME: Check busy words, removed due to SPI bug */
218         /* if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1))
219            wl1271_spi_read_busy(wl, buf, len); */
220
221         wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
222         wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
223 }
224
225 void wl1271_spi_raw_write(struct wl1271 *wl, int addr, void *buf,
226                           size_t len, bool fixed)
227 {
228         struct spi_transfer t[2];
229         struct spi_message m;
230         u32 *cmd;
231
232         cmd = &wl->buffer_cmd;
233
234         *cmd = 0;
235         *cmd |= WSPI_CMD_WRITE;
236         *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
237         *cmd |= addr & WSPI_CMD_BYTE_ADDR;
238
239         if (fixed)
240                 *cmd |= WSPI_CMD_FIXED;
241
242         spi_message_init(&m);
243         memset(t, 0, sizeof(t));
244
245         t[0].tx_buf = cmd;
246         t[0].len = sizeof(*cmd);
247         spi_message_add_tail(&t[0], &m);
248
249         t[1].tx_buf = buf;
250         t[1].len = len;
251         spi_message_add_tail(&t[1], &m);
252
253         spi_sync(wl->spi, &m);
254
255         wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
256         wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
257 }