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