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