Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 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 | |
Alexey Dobriyan | a6b7a40 | 2011-06-06 10:43:46 +0000 | [diff] [blame] | 24 | #include <linux/interrupt.h> |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 25 | #include <linux/irq.h> |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 26 | #include <linux/module.h> |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 27 | #include <linux/crc7.h> |
| 28 | #include <linux/spi/spi.h> |
Ohad Ben-Cohen | c1f9a09 | 2010-09-16 13:16:02 +0200 | [diff] [blame] | 29 | #include <linux/wl12xx.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 31 | |
Shahar Levi | 00d2010 | 2010-11-08 11:20:10 +0000 | [diff] [blame] | 32 | #include "wl12xx.h" |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 33 | #include "wl12xx_80211.h" |
Shahar Levi | 00d2010 | 2010-11-08 11:20:10 +0000 | [diff] [blame] | 34 | #include "io.h" |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 35 | |
Shahar Levi | 00d2010 | 2010-11-08 11:20:10 +0000 | [diff] [blame] | 36 | #include "reg.h" |
Teemu Paasikivi | 760d969 | 2010-02-22 08:38:25 +0200 | [diff] [blame] | 37 | |
| 38 | #define WSPI_CMD_READ 0x40000000 |
| 39 | #define WSPI_CMD_WRITE 0x00000000 |
| 40 | #define WSPI_CMD_FIXED 0x20000000 |
| 41 | #define WSPI_CMD_BYTE_LENGTH 0x1FFE0000 |
| 42 | #define WSPI_CMD_BYTE_LENGTH_OFFSET 17 |
| 43 | #define WSPI_CMD_BYTE_ADDR 0x0001FFFF |
| 44 | |
| 45 | #define WSPI_INIT_CMD_CRC_LEN 5 |
| 46 | |
| 47 | #define WSPI_INIT_CMD_START 0x00 |
| 48 | #define WSPI_INIT_CMD_TX 0x40 |
| 49 | /* the extra bypass bit is sampled by the TNET as '1' */ |
| 50 | #define WSPI_INIT_CMD_BYPASS_BIT 0x80 |
| 51 | #define WSPI_INIT_CMD_FIXEDBUSY_LEN 0x07 |
| 52 | #define WSPI_INIT_CMD_EN_FIXEDBUSY 0x80 |
| 53 | #define WSPI_INIT_CMD_DIS_FIXEDBUSY 0x00 |
| 54 | #define WSPI_INIT_CMD_IOD 0x40 |
| 55 | #define WSPI_INIT_CMD_IP 0x20 |
| 56 | #define WSPI_INIT_CMD_CS 0x10 |
| 57 | #define WSPI_INIT_CMD_WS 0x08 |
| 58 | #define WSPI_INIT_CMD_WSPI 0x01 |
| 59 | #define WSPI_INIT_CMD_END 0x01 |
| 60 | |
| 61 | #define WSPI_INIT_CMD_LEN 8 |
| 62 | |
| 63 | #define HW_ACCESS_WSPI_FIXED_BUSY_LEN \ |
| 64 | ((WL1271_BUSY_WORD_LEN - 4) / sizeof(u32)) |
| 65 | #define HW_ACCESS_WSPI_INIT_CMD_MASK 0 |
| 66 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 67 | /* HW limitation: maximum possible chunk size is 4095 bytes */ |
| 68 | #define WSPI_MAX_CHUNK_SIZE 4092 |
| 69 | |
| 70 | #define WSPI_MAX_NUM_OF_CHUNKS (WL1271_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE) |
| 71 | |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 72 | struct wl12xx_spi_glue { |
| 73 | struct device *dev; |
| 74 | struct wl1271 *wl; |
| 75 | }; |
| 76 | |
| 77 | static inline struct wl12xx_spi_glue *wl_to_glue(struct wl1271 *wl) |
Teemu Paasikivi | 8197b71 | 2010-02-22 08:38:23 +0200 | [diff] [blame] | 78 | { |
| 79 | return wl->if_priv; |
| 80 | } |
| 81 | |
| 82 | static struct device *wl1271_spi_wl_to_dev(struct wl1271 *wl) |
| 83 | { |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 84 | return wl_to_glue(wl)->dev; |
Teemu Paasikivi | 8197b71 | 2010-02-22 08:38:23 +0200 | [diff] [blame] | 85 | } |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 86 | |
Teemu Paasikivi | 760d969 | 2010-02-22 08:38:25 +0200 | [diff] [blame] | 87 | static void wl1271_spi_disable_interrupts(struct wl1271 *wl) |
Teemu Paasikivi | 54f7e50 | 2010-02-22 08:38:22 +0200 | [diff] [blame] | 88 | { |
| 89 | disable_irq(wl->irq); |
| 90 | } |
| 91 | |
Teemu Paasikivi | 760d969 | 2010-02-22 08:38:25 +0200 | [diff] [blame] | 92 | static void wl1271_spi_enable_interrupts(struct wl1271 *wl) |
Teemu Paasikivi | 54f7e50 | 2010-02-22 08:38:22 +0200 | [diff] [blame] | 93 | { |
| 94 | enable_irq(wl->irq); |
| 95 | } |
| 96 | |
Teemu Paasikivi | 760d969 | 2010-02-22 08:38:25 +0200 | [diff] [blame] | 97 | static void wl1271_spi_reset(struct wl1271 *wl) |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 98 | { |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 99 | struct wl12xx_spi_glue *glue = wl_to_glue(wl); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 100 | u8 *cmd; |
| 101 | struct spi_transfer t; |
| 102 | struct spi_message m; |
| 103 | |
| 104 | cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); |
| 105 | if (!cmd) { |
| 106 | wl1271_error("could not allocate cmd for spi reset"); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | memset(&t, 0, sizeof(t)); |
| 111 | spi_message_init(&m); |
| 112 | |
| 113 | memset(cmd, 0xff, WSPI_INIT_CMD_LEN); |
| 114 | |
| 115 | t.tx_buf = cmd; |
| 116 | t.len = WSPI_INIT_CMD_LEN; |
| 117 | spi_message_add_tail(&t, &m); |
| 118 | |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 119 | spi_sync(to_spi_device(glue->dev), &m); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 120 | |
| 121 | wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN); |
Dan Carpenter | 0dd3866 | 2010-12-21 07:00:13 +0300 | [diff] [blame] | 122 | kfree(cmd); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 123 | } |
| 124 | |
Teemu Paasikivi | 760d969 | 2010-02-22 08:38:25 +0200 | [diff] [blame] | 125 | static void wl1271_spi_init(struct wl1271 *wl) |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 126 | { |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 127 | struct wl12xx_spi_glue *glue = wl_to_glue(wl); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 128 | u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd; |
| 129 | struct spi_transfer t; |
| 130 | struct spi_message m; |
| 131 | |
| 132 | cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); |
| 133 | if (!cmd) { |
| 134 | wl1271_error("could not allocate cmd for spi init"); |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | memset(crc, 0, sizeof(crc)); |
| 139 | memset(&t, 0, sizeof(t)); |
| 140 | spi_message_init(&m); |
| 141 | |
| 142 | /* |
| 143 | * Set WSPI_INIT_COMMAND |
| 144 | * the data is being send from the MSB to LSB |
| 145 | */ |
| 146 | cmd[2] = 0xff; |
| 147 | cmd[3] = 0xff; |
| 148 | cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX; |
| 149 | cmd[0] = 0; |
| 150 | cmd[7] = 0; |
| 151 | cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3; |
| 152 | cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN; |
| 153 | |
| 154 | if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0) |
| 155 | cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY; |
| 156 | else |
| 157 | cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY; |
| 158 | |
| 159 | cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS |
| 160 | | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS; |
| 161 | |
| 162 | crc[0] = cmd[1]; |
| 163 | crc[1] = cmd[0]; |
| 164 | crc[2] = cmd[7]; |
| 165 | crc[3] = cmd[6]; |
| 166 | crc[4] = cmd[5]; |
| 167 | |
| 168 | cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1; |
| 169 | cmd[4] |= WSPI_INIT_CMD_END; |
| 170 | |
| 171 | t.tx_buf = cmd; |
| 172 | t.len = WSPI_INIT_CMD_LEN; |
| 173 | spi_message_add_tail(&t, &m); |
| 174 | |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 175 | spi_sync(to_spi_device(glue->dev), &m); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 176 | wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN); |
Kulikov Vasiliy | bb12361 | 2010-07-31 20:33:59 +0400 | [diff] [blame] | 177 | kfree(cmd); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 178 | } |
| 179 | |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 180 | #define WL1271_BUSY_WORD_TIMEOUT 1000 |
| 181 | |
Juuso Oikarinen | 259da43 | 2010-03-26 12:53:18 +0200 | [diff] [blame] | 182 | static int wl1271_spi_read_busy(struct wl1271 *wl) |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 183 | { |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 184 | struct wl12xx_spi_glue *glue = wl_to_glue(wl); |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 185 | struct spi_transfer t[1]; |
| 186 | struct spi_message m; |
| 187 | u32 *busy_buf; |
| 188 | int num_busy_bytes = 0; |
| 189 | |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 190 | /* |
| 191 | * Read further busy words from SPI until a non-busy word is |
| 192 | * encountered, then read the data itself into the buffer. |
| 193 | */ |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 194 | |
| 195 | num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT; |
| 196 | busy_buf = wl->buffer_busyword; |
| 197 | while (num_busy_bytes) { |
| 198 | num_busy_bytes--; |
| 199 | spi_message_init(&m); |
| 200 | memset(t, 0, sizeof(t)); |
| 201 | t[0].rx_buf = busy_buf; |
| 202 | t[0].len = sizeof(u32); |
Juuso Oikarinen | 259da43 | 2010-03-26 12:53:18 +0200 | [diff] [blame] | 203 | t[0].cs_change = true; |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 204 | spi_message_add_tail(&t[0], &m); |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 205 | spi_sync(to_spi_device(glue->dev), &m); |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 206 | |
Juuso Oikarinen | 259da43 | 2010-03-26 12:53:18 +0200 | [diff] [blame] | 207 | if (*busy_buf & 0x1) |
| 208 | return 0; |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /* The SPI bus is unresponsive, the read failed. */ |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 212 | wl1271_error("SPI read busy-word timeout!\n"); |
Juuso Oikarinen | 259da43 | 2010-03-26 12:53:18 +0200 | [diff] [blame] | 213 | return -ETIMEDOUT; |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 214 | } |
| 215 | |
Teemu Paasikivi | 760d969 | 2010-02-22 08:38:25 +0200 | [diff] [blame] | 216 | static void wl1271_spi_raw_read(struct wl1271 *wl, int addr, void *buf, |
Juuso Oikarinen | 259da43 | 2010-03-26 12:53:18 +0200 | [diff] [blame] | 217 | size_t len, bool fixed) |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 218 | { |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 219 | struct wl12xx_spi_glue *glue = wl_to_glue(wl); |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 220 | struct spi_transfer t[2]; |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 221 | struct spi_message m; |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 222 | u32 *busy_buf; |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 223 | u32 *cmd; |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 224 | u32 chunk_len; |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 225 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 226 | while (len > 0) { |
| 227 | chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 228 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 229 | cmd = &wl->buffer_cmd; |
| 230 | busy_buf = wl->buffer_busyword; |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 231 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 232 | *cmd = 0; |
| 233 | *cmd |= WSPI_CMD_READ; |
| 234 | *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) & |
| 235 | WSPI_CMD_BYTE_LENGTH; |
| 236 | *cmd |= addr & WSPI_CMD_BYTE_ADDR; |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 237 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 238 | if (fixed) |
| 239 | *cmd |= WSPI_CMD_FIXED; |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 240 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 241 | spi_message_init(&m); |
| 242 | memset(t, 0, sizeof(t)); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 243 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 244 | t[0].tx_buf = cmd; |
| 245 | t[0].len = 4; |
| 246 | t[0].cs_change = true; |
| 247 | spi_message_add_tail(&t[0], &m); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 248 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 249 | /* Busy and non busy words read */ |
| 250 | t[1].rx_buf = busy_buf; |
| 251 | t[1].len = WL1271_BUSY_WORD_LEN; |
| 252 | t[1].cs_change = true; |
| 253 | spi_message_add_tail(&t[1], &m); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 254 | |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 255 | spi_sync(to_spi_device(glue->dev), &m); |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 256 | |
| 257 | if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) && |
| 258 | wl1271_spi_read_busy(wl)) { |
| 259 | memset(buf, 0, chunk_len); |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | spi_message_init(&m); |
| 264 | memset(t, 0, sizeof(t)); |
| 265 | |
| 266 | t[0].rx_buf = buf; |
| 267 | t[0].len = chunk_len; |
| 268 | t[0].cs_change = true; |
| 269 | spi_message_add_tail(&t[0], &m); |
| 270 | |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 271 | spi_sync(to_spi_device(glue->dev), &m); |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 272 | |
| 273 | wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd)); |
| 274 | wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, chunk_len); |
| 275 | |
| 276 | if (!fixed) |
| 277 | addr += chunk_len; |
| 278 | buf += chunk_len; |
| 279 | len -= chunk_len; |
Juuso Oikarinen | 259da43 | 2010-03-26 12:53:18 +0200 | [diff] [blame] | 280 | } |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 281 | } |
| 282 | |
Teemu Paasikivi | 760d969 | 2010-02-22 08:38:25 +0200 | [diff] [blame] | 283 | static void wl1271_spi_raw_write(struct wl1271 *wl, int addr, void *buf, |
Juuso Oikarinen | 7462141 | 2009-10-12 15:08:54 +0300 | [diff] [blame] | 284 | size_t len, bool fixed) |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 285 | { |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 286 | struct wl12xx_spi_glue *glue = wl_to_glue(wl); |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 287 | struct spi_transfer t[2 * WSPI_MAX_NUM_OF_CHUNKS]; |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 288 | struct spi_message m; |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 289 | u32 commands[WSPI_MAX_NUM_OF_CHUNKS]; |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 290 | u32 *cmd; |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 291 | u32 chunk_len; |
| 292 | int i; |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 293 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 294 | WARN_ON(len > WL1271_AGGR_BUFFER_SIZE); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 295 | |
| 296 | spi_message_init(&m); |
| 297 | memset(t, 0, sizeof(t)); |
| 298 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 299 | cmd = &commands[0]; |
| 300 | i = 0; |
| 301 | while (len > 0) { |
| 302 | chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 303 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 304 | *cmd = 0; |
| 305 | *cmd |= WSPI_CMD_WRITE; |
| 306 | *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) & |
| 307 | WSPI_CMD_BYTE_LENGTH; |
| 308 | *cmd |= addr & WSPI_CMD_BYTE_ADDR; |
| 309 | |
| 310 | if (fixed) |
| 311 | *cmd |= WSPI_CMD_FIXED; |
| 312 | |
| 313 | t[i].tx_buf = cmd; |
| 314 | t[i].len = sizeof(*cmd); |
| 315 | spi_message_add_tail(&t[i++], &m); |
| 316 | |
| 317 | t[i].tx_buf = buf; |
| 318 | t[i].len = chunk_len; |
| 319 | spi_message_add_tail(&t[i++], &m); |
| 320 | |
| 321 | wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd)); |
| 322 | wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, chunk_len); |
| 323 | |
| 324 | if (!fixed) |
| 325 | addr += chunk_len; |
| 326 | buf += chunk_len; |
| 327 | len -= chunk_len; |
| 328 | cmd++; |
| 329 | } |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 330 | |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 331 | spi_sync(to_spi_device(glue->dev), &m); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 332 | } |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 333 | |
Ido Yariv | a620865 | 2011-03-01 15:14:41 +0200 | [diff] [blame] | 334 | static irqreturn_t wl1271_hardirq(int irq, void *cookie) |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 335 | { |
Ido Yariv | a620865 | 2011-03-01 15:14:41 +0200 | [diff] [blame] | 336 | struct wl1271 *wl = cookie; |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 337 | unsigned long flags; |
| 338 | |
| 339 | wl1271_debug(DEBUG_IRQ, "IRQ"); |
| 340 | |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 341 | /* complete the ELP completion */ |
| 342 | spin_lock_irqsave(&wl->wl_lock, flags); |
Ido Yariv | a620865 | 2011-03-01 15:14:41 +0200 | [diff] [blame] | 343 | set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags); |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 344 | if (wl->elp_compl) { |
| 345 | complete(wl->elp_compl); |
| 346 | wl->elp_compl = NULL; |
| 347 | } |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 348 | spin_unlock_irqrestore(&wl->wl_lock, flags); |
| 349 | |
Ido Yariv | a620865 | 2011-03-01 15:14:41 +0200 | [diff] [blame] | 350 | return IRQ_WAKE_THREAD; |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Ohad Ben-Cohen | 2cc78ff | 2010-09-16 01:22:04 +0200 | [diff] [blame] | 353 | static int wl1271_spi_set_power(struct wl1271 *wl, bool enable) |
Teemu Paasikivi | becd551 | 2010-03-18 12:26:27 +0200 | [diff] [blame] | 354 | { |
| 355 | if (wl->set_power) |
| 356 | wl->set_power(enable); |
Ohad Ben-Cohen | 2cc78ff | 2010-09-16 01:22:04 +0200 | [diff] [blame] | 357 | |
| 358 | return 0; |
Teemu Paasikivi | becd551 | 2010-03-18 12:26:27 +0200 | [diff] [blame] | 359 | } |
| 360 | |
Teemu Paasikivi | 8197b71 | 2010-02-22 08:38:23 +0200 | [diff] [blame] | 361 | static struct wl1271_if_operations spi_ops = { |
| 362 | .read = wl1271_spi_raw_read, |
| 363 | .write = wl1271_spi_raw_write, |
| 364 | .reset = wl1271_spi_reset, |
| 365 | .init = wl1271_spi_init, |
Teemu Paasikivi | becd551 | 2010-03-18 12:26:27 +0200 | [diff] [blame] | 366 | .power = wl1271_spi_set_power, |
Teemu Paasikivi | 8197b71 | 2010-02-22 08:38:23 +0200 | [diff] [blame] | 367 | .dev = wl1271_spi_wl_to_dev, |
| 368 | .enable_irq = wl1271_spi_enable_interrupts, |
Luciano Coelho | a81159e | 2011-03-14 14:05:13 +0200 | [diff] [blame] | 369 | .disable_irq = wl1271_spi_disable_interrupts, |
| 370 | .set_block_size = NULL, |
Teemu Paasikivi | 8197b71 | 2010-02-22 08:38:23 +0200 | [diff] [blame] | 371 | }; |
| 372 | |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 373 | static int __devinit wl1271_probe(struct spi_device *spi) |
| 374 | { |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 375 | struct wl12xx_spi_glue *glue; |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 376 | struct wl12xx_platform_data *pdata; |
| 377 | struct ieee80211_hw *hw; |
| 378 | struct wl1271 *wl; |
Ido Yariv | 341b7cd | 2011-03-31 10:07:01 +0200 | [diff] [blame] | 379 | unsigned long irqflags; |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 380 | int ret = -ENOMEM; |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 381 | |
| 382 | pdata = spi->dev.platform_data; |
| 383 | if (!pdata) { |
| 384 | wl1271_error("no platform data"); |
| 385 | return -ENODEV; |
| 386 | } |
| 387 | |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 388 | glue = kzalloc(sizeof(*glue), GFP_KERNEL); |
| 389 | if (!glue) { |
| 390 | wl1271_error("can't allocate glue"); |
| 391 | goto out; |
| 392 | } |
| 393 | |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 394 | hw = wl1271_alloc_hw(); |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 395 | if (IS_ERR(hw)) { |
| 396 | ret = PTR_ERR(hw); |
| 397 | goto out_free_glue; |
| 398 | } |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 399 | |
| 400 | wl = hw->priv; |
| 401 | |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 402 | glue->dev = &spi->dev; |
| 403 | glue->wl = wl; |
| 404 | |
| 405 | spi_set_drvdata(spi, glue); |
| 406 | wl->if_priv = glue; |
Teemu Paasikivi | 8197b71 | 2010-02-22 08:38:23 +0200 | [diff] [blame] | 407 | |
| 408 | wl->if_ops = &spi_ops; |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 409 | |
| 410 | /* This is the only SPI value that we need to set here, the rest |
| 411 | * comes from the board-peripherals file */ |
| 412 | spi->bits_per_word = 32; |
| 413 | |
| 414 | ret = spi_setup(spi); |
| 415 | if (ret < 0) { |
| 416 | wl1271_error("spi_setup failed"); |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 417 | goto out_free_hw; |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | wl->set_power = pdata->set_power; |
| 421 | if (!wl->set_power) { |
| 422 | wl1271_error("set power function missing in platform data"); |
| 423 | ret = -ENODEV; |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 424 | goto out_free_hw; |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 425 | } |
| 426 | |
Ohad Ben-Cohen | 15cea99 | 2010-09-16 01:31:51 +0200 | [diff] [blame] | 427 | wl->ref_clock = pdata->board_ref_clock; |
Shahar Levi | 5ea417a | 2011-03-06 16:32:11 +0200 | [diff] [blame] | 428 | wl->tcxo_clock = pdata->board_tcxo_clock; |
Ido Yariv | 341b7cd | 2011-03-31 10:07:01 +0200 | [diff] [blame] | 429 | wl->platform_quirks = pdata->platform_quirks; |
| 430 | |
| 431 | if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ) |
| 432 | irqflags = IRQF_TRIGGER_RISING; |
| 433 | else |
| 434 | irqflags = IRQF_TRIGGER_HIGH | IRQF_ONESHOT; |
Ohad Ben-Cohen | 15cea99 | 2010-09-16 01:31:51 +0200 | [diff] [blame] | 435 | |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 436 | wl->irq = spi->irq; |
| 437 | if (wl->irq < 0) { |
| 438 | wl1271_error("irq missing in platform data"); |
| 439 | ret = -ENODEV; |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 440 | goto out_free_hw; |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Ido Yariv | a620865 | 2011-03-01 15:14:41 +0200 | [diff] [blame] | 443 | ret = request_threaded_irq(wl->irq, wl1271_hardirq, wl1271_irq, |
Ido Yariv | 341b7cd | 2011-03-31 10:07:01 +0200 | [diff] [blame] | 444 | irqflags, |
Ido Yariv | a620865 | 2011-03-01 15:14:41 +0200 | [diff] [blame] | 445 | DRIVER_NAME, wl); |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 446 | if (ret < 0) { |
| 447 | wl1271_error("request_irq() failed: %d", ret); |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 448 | goto out_free_hw; |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 449 | } |
| 450 | |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 451 | disable_irq(wl->irq); |
| 452 | |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 453 | ret = wl1271_init_ieee80211(wl); |
| 454 | if (ret) |
Juuso Oikarinen | a1dd818 | 2010-03-18 12:26:31 +0200 | [diff] [blame] | 455 | goto out_irq; |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 456 | |
| 457 | ret = wl1271_register_hw(wl); |
| 458 | if (ret) |
Juuso Oikarinen | a1dd818 | 2010-03-18 12:26:31 +0200 | [diff] [blame] | 459 | goto out_irq; |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 460 | |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 461 | return 0; |
| 462 | |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 463 | out_irq: |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 464 | free_irq(wl->irq, wl); |
| 465 | |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 466 | out_free_hw: |
Teemu Paasikivi | 3b56dd6 | 2010-03-18 12:26:46 +0200 | [diff] [blame] | 467 | wl1271_free_hw(wl); |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 468 | |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 469 | out_free_glue: |
| 470 | kfree(glue); |
| 471 | out: |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 472 | return ret; |
| 473 | } |
| 474 | |
| 475 | static int __devexit wl1271_remove(struct spi_device *spi) |
| 476 | { |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 477 | struct wl12xx_spi_glue *glue = spi_get_drvdata(spi); |
| 478 | struct wl1271 *wl = glue->wl; |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 479 | |
Teemu Paasikivi | 3b56dd6 | 2010-03-18 12:26:46 +0200 | [diff] [blame] | 480 | wl1271_unregister_hw(wl); |
Juuso Oikarinen | 4e23b11 | 2010-08-13 04:46:48 +0200 | [diff] [blame] | 481 | free_irq(wl->irq, wl); |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 482 | wl1271_free_hw(wl); |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame^] | 483 | kfree(glue); |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | |
| 489 | static struct spi_driver wl1271_spi_driver = { |
| 490 | .driver = { |
Luciano Coelho | 7fdd50d | 2010-03-26 12:53:10 +0200 | [diff] [blame] | 491 | .name = "wl1271_spi", |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 492 | .bus = &spi_bus_type, |
| 493 | .owner = THIS_MODULE, |
| 494 | }, |
| 495 | |
| 496 | .probe = wl1271_probe, |
| 497 | .remove = __devexit_p(wl1271_remove), |
| 498 | }; |
| 499 | |
| 500 | static int __init wl1271_init(void) |
| 501 | { |
Felipe Balbi | ba2274c | 2011-05-14 00:26:23 +0300 | [diff] [blame] | 502 | return spi_register_driver(&wl1271_spi_driver); |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | static void __exit wl1271_exit(void) |
| 506 | { |
| 507 | spi_unregister_driver(&wl1271_spi_driver); |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | module_init(wl1271_init); |
| 511 | module_exit(wl1271_exit); |
| 512 | |
| 513 | MODULE_LICENSE("GPL"); |
Luciano Coelho | 5245e3a | 2011-03-30 21:31:39 +0300 | [diff] [blame] | 514 | MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>"); |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 515 | MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>"); |
Arik Nemtsov | c302b2c | 2011-08-17 10:45:48 +0300 | [diff] [blame] | 516 | MODULE_FIRMWARE(WL127X_FW_NAME); |
Shahar Levi | 5aa4234 | 2011-03-06 16:32:07 +0200 | [diff] [blame] | 517 | MODULE_FIRMWARE(WL128X_FW_NAME); |
Ameya Palande | f148cfd | 2010-07-05 17:12:38 +0300 | [diff] [blame] | 518 | MODULE_ALIAS("spi:wl1271"); |