b43: LP-PHY: Initialize TX power control
[pandora-kernel.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 static int wl1271_translate_reg_addr(struct wl1271 *wl, int addr)
34 {
35         return addr - wl->physical_reg_addr + wl->virtual_reg_addr;
36 }
37
38 static int wl1271_translate_mem_addr(struct wl1271 *wl, int addr)
39 {
40         return addr - wl->physical_mem_addr + wl->virtual_mem_addr;
41 }
42
43
44 void wl1271_spi_reset(struct wl1271 *wl)
45 {
46         u8 *cmd;
47         struct spi_transfer t;
48         struct spi_message m;
49
50         cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
51         if (!cmd) {
52                 wl1271_error("could not allocate cmd for spi reset");
53                 return;
54         }
55
56         memset(&t, 0, sizeof(t));
57         spi_message_init(&m);
58
59         memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
60
61         t.tx_buf = cmd;
62         t.len = WSPI_INIT_CMD_LEN;
63         spi_message_add_tail(&t, &m);
64
65         spi_sync(wl->spi, &m);
66
67         wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
68 }
69
70 void wl1271_spi_init(struct wl1271 *wl)
71 {
72         u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
73         struct spi_transfer t;
74         struct spi_message m;
75
76         cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
77         if (!cmd) {
78                 wl1271_error("could not allocate cmd for spi init");
79                 return;
80         }
81
82         memset(crc, 0, sizeof(crc));
83         memset(&t, 0, sizeof(t));
84         spi_message_init(&m);
85
86         /*
87          * Set WSPI_INIT_COMMAND
88          * the data is being send from the MSB to LSB
89          */
90         cmd[2] = 0xff;
91         cmd[3] = 0xff;
92         cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
93         cmd[0] = 0;
94         cmd[7] = 0;
95         cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
96         cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
97
98         if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
99                 cmd[5] |=  WSPI_INIT_CMD_DIS_FIXEDBUSY;
100         else
101                 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
102
103         cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
104                 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
105
106         crc[0] = cmd[1];
107         crc[1] = cmd[0];
108         crc[2] = cmd[7];
109         crc[3] = cmd[6];
110         crc[4] = cmd[5];
111
112         cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
113         cmd[4] |= WSPI_INIT_CMD_END;
114
115         t.tx_buf = cmd;
116         t.len = WSPI_INIT_CMD_LEN;
117         spi_message_add_tail(&t, &m);
118
119         spi_sync(wl->spi, &m);
120
121         wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
122 }
123
124 /* Set the SPI partitions to access the chip addresses
125  *
126  * There are two VIRTUAL (SPI) partitions (the memory partition and the
127  * registers partition), which are mapped to two different areas of the
128  * PHYSICAL (hardware) memory.  This function also makes other checks to
129  * ensure that the partitions are not overlapping.  In the diagram below, the
130  * memory partition comes before the register partition, but the opposite is
131  * also supported.
132  *
133  *                               PHYSICAL address
134  *                                     space
135  *
136  *                                    |    |
137  *                                 ...+----+--> mem_start
138  *          VIRTUAL address     ...   |    |
139  *               space       ...      |    | [PART_0]
140  *                        ...         |    |
141  * 0x00000000 <--+----+...         ...+----+--> mem_start + mem_size
142  *               |    |         ...   |    |
143  *               |MEM |      ...      |    |
144  *               |    |   ...         |    |
145  *  part_size <--+----+...            |    | {unused area)
146  *               |    |   ...         |    |
147  *               |REG |      ...      |    |
148  *  part_size    |    |         ...   |    |
149  *      +     <--+----+...         ...+----+--> reg_start
150  *  reg_size              ...         |    |
151  *                           ...      |    | [PART_1]
152  *                              ...   |    |
153  *                                 ...+----+--> reg_start + reg_size
154  *                                    |    |
155  *
156  */
157 int wl1271_set_partition(struct wl1271 *wl,
158                           u32 mem_start, u32 mem_size,
159                           u32 reg_start, u32 reg_size)
160 {
161         struct wl1271_partition *partition;
162         struct spi_transfer t;
163         struct spi_message m;
164         size_t len, cmd_len;
165         u32 *cmd;
166         int addr;
167
168         cmd_len = sizeof(u32) + 2 * sizeof(struct wl1271_partition);
169         cmd = kzalloc(cmd_len, GFP_KERNEL);
170         if (!cmd)
171                 return -ENOMEM;
172
173         spi_message_init(&m);
174         memset(&t, 0, sizeof(t));
175
176         partition = (struct wl1271_partition *) (cmd + 1);
177         addr = HW_ACCESS_PART0_SIZE_ADDR;
178         len = 2 * sizeof(struct wl1271_partition);
179
180         *cmd |= WSPI_CMD_WRITE;
181         *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
182         *cmd |= addr & WSPI_CMD_BYTE_ADDR;
183
184         wl1271_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
185                      mem_start, mem_size);
186         wl1271_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
187                      reg_start, reg_size);
188
189         /* Make sure that the two partitions together don't exceed the
190          * address range */
191         if ((mem_size + reg_size) > HW_ACCESS_MEMORY_MAX_RANGE) {
192                 wl1271_debug(DEBUG_SPI, "Total size exceeds maximum virtual"
193                              " address range.  Truncating partition[0].");
194                 mem_size = HW_ACCESS_MEMORY_MAX_RANGE - reg_size;
195                 wl1271_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
196                              mem_start, mem_size);
197                 wl1271_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
198                              reg_start, reg_size);
199         }
200
201         if ((mem_start < reg_start) &&
202             ((mem_start + mem_size) > reg_start)) {
203                 /* Guarantee that the memory partition doesn't overlap the
204                  * registers partition */
205                 wl1271_debug(DEBUG_SPI, "End of partition[0] is "
206                              "overlapping partition[1].  Adjusted.");
207                 mem_size = reg_start - mem_start;
208                 wl1271_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
209                              mem_start, mem_size);
210                 wl1271_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
211                              reg_start, reg_size);
212         } else if ((reg_start < mem_start) &&
213                    ((reg_start + reg_size) > mem_start)) {
214                 /* Guarantee that the register partition doesn't overlap the
215                  * memory partition */
216                 wl1271_debug(DEBUG_SPI, "End of partition[1] is"
217                              " overlapping partition[0].  Adjusted.");
218                 reg_size = mem_start - reg_start;
219                 wl1271_debug(DEBUG_SPI, "mem_start %08X mem_size %08X",
220                              mem_start, mem_size);
221                 wl1271_debug(DEBUG_SPI, "reg_start %08X reg_size %08X",
222                              reg_start, reg_size);
223         }
224
225         partition[0].start = mem_start;
226         partition[0].size  = mem_size;
227         partition[1].start = reg_start;
228         partition[1].size  = reg_size;
229
230         wl->physical_mem_addr = mem_start;
231         wl->physical_reg_addr = reg_start;
232
233         wl->virtual_mem_addr = 0;
234         wl->virtual_reg_addr = mem_size;
235
236         t.tx_buf = cmd;
237         t.len = cmd_len;
238         spi_message_add_tail(&t, &m);
239
240         spi_sync(wl->spi, &m);
241
242         kfree(cmd);
243
244         return 0;
245 }
246
247 void wl1271_spi_read(struct wl1271 *wl, int addr, void *buf,
248                      size_t len, bool fixed)
249 {
250         struct spi_transfer t[3];
251         struct spi_message m;
252         u8 *busy_buf;
253         u32 *cmd;
254
255         cmd = &wl->buffer_cmd;
256         busy_buf = wl->buffer_busyword;
257
258         *cmd = 0;
259         *cmd |= WSPI_CMD_READ;
260         *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
261         *cmd |= addr & WSPI_CMD_BYTE_ADDR;
262
263         if (fixed)
264                 *cmd |= WSPI_CMD_FIXED;
265
266         spi_message_init(&m);
267         memset(t, 0, sizeof(t));
268
269         t[0].tx_buf = cmd;
270         t[0].len = 4;
271         spi_message_add_tail(&t[0], &m);
272
273         /* Busy and non busy words read */
274         t[1].rx_buf = busy_buf;
275         t[1].len = WL1271_BUSY_WORD_LEN;
276         spi_message_add_tail(&t[1], &m);
277
278         t[2].rx_buf = buf;
279         t[2].len = len;
280         spi_message_add_tail(&t[2], &m);
281
282         spi_sync(wl->spi, &m);
283
284         /* FIXME: check busy words */
285
286         wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
287         wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
288 }
289
290 void wl1271_spi_write(struct wl1271 *wl, int addr, void *buf,
291                       size_t len, bool fixed)
292 {
293         struct spi_transfer t[2];
294         struct spi_message m;
295         u32 *cmd;
296
297         cmd = &wl->buffer_cmd;
298
299         *cmd = 0;
300         *cmd |= WSPI_CMD_WRITE;
301         *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
302         *cmd |= addr & WSPI_CMD_BYTE_ADDR;
303
304         if (fixed)
305                 *cmd |= WSPI_CMD_FIXED;
306
307         spi_message_init(&m);
308         memset(t, 0, sizeof(t));
309
310         t[0].tx_buf = cmd;
311         t[0].len = sizeof(*cmd);
312         spi_message_add_tail(&t[0], &m);
313
314         t[1].tx_buf = buf;
315         t[1].len = len;
316         spi_message_add_tail(&t[1], &m);
317
318         spi_sync(wl->spi, &m);
319
320         wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
321         wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
322 }
323
324 void wl1271_spi_mem_read(struct wl1271 *wl, int addr, void *buf,
325                          size_t len)
326 {
327         int physical;
328
329         physical = wl1271_translate_mem_addr(wl, addr);
330
331         wl1271_spi_read(wl, physical, buf, len, false);
332 }
333
334 void wl1271_spi_mem_write(struct wl1271 *wl, int addr, void *buf,
335                           size_t len)
336 {
337         int physical;
338
339         physical = wl1271_translate_mem_addr(wl, addr);
340
341         wl1271_spi_write(wl, physical, buf, len, false);
342 }
343
344 void wl1271_spi_reg_read(struct wl1271 *wl, int addr, void *buf, size_t len,
345                          bool fixed)
346 {
347         int physical;
348
349         physical = wl1271_translate_reg_addr(wl, addr);
350
351         wl1271_spi_read(wl, physical, buf, len, fixed);
352 }
353
354 void wl1271_spi_reg_write(struct wl1271 *wl, int addr, void *buf, size_t len,
355                           bool fixed)
356 {
357         int physical;
358
359         physical = wl1271_translate_reg_addr(wl, addr);
360
361         wl1271_spi_write(wl, physical, buf, len, fixed);
362 }
363
364 u32 wl1271_mem_read32(struct wl1271 *wl, int addr)
365 {
366         return wl1271_read32(wl, wl1271_translate_mem_addr(wl, addr));
367 }
368
369 void wl1271_mem_write32(struct wl1271 *wl, int addr, u32 val)
370 {
371         wl1271_write32(wl, wl1271_translate_mem_addr(wl, addr), val);
372 }
373
374 u32 wl1271_reg_read32(struct wl1271 *wl, int addr)
375 {
376         return wl1271_read32(wl, wl1271_translate_reg_addr(wl, addr));
377 }
378
379 void wl1271_reg_write32(struct wl1271 *wl, int addr, u32 val)
380 {
381         wl1271_write32(wl, wl1271_translate_reg_addr(wl, addr), val);
382 }