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> |
Felipe Balbi | 0969d67 | 2011-10-05 09:29:13 +0300 | [diff] [blame] | 30 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/slab.h> |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 32 | |
Shahar Levi | 00d2010 | 2010-11-08 11:20:10 +0000 | [diff] [blame] | 33 | #include "wl12xx.h" |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 34 | #include "wl12xx_80211.h" |
Shahar Levi | 00d2010 | 2010-11-08 11:20:10 +0000 | [diff] [blame] | 35 | #include "io.h" |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 36 | |
Shahar Levi | 00d2010 | 2010-11-08 11:20:10 +0000 | [diff] [blame] | 37 | #include "reg.h" |
Teemu Paasikivi | 760d969 | 2010-02-22 08:38:25 +0200 | [diff] [blame] | 38 | |
| 39 | #define WSPI_CMD_READ 0x40000000 |
| 40 | #define WSPI_CMD_WRITE 0x00000000 |
| 41 | #define WSPI_CMD_FIXED 0x20000000 |
| 42 | #define WSPI_CMD_BYTE_LENGTH 0x1FFE0000 |
| 43 | #define WSPI_CMD_BYTE_LENGTH_OFFSET 17 |
| 44 | #define WSPI_CMD_BYTE_ADDR 0x0001FFFF |
| 45 | |
| 46 | #define WSPI_INIT_CMD_CRC_LEN 5 |
| 47 | |
| 48 | #define WSPI_INIT_CMD_START 0x00 |
| 49 | #define WSPI_INIT_CMD_TX 0x40 |
| 50 | /* the extra bypass bit is sampled by the TNET as '1' */ |
| 51 | #define WSPI_INIT_CMD_BYPASS_BIT 0x80 |
| 52 | #define WSPI_INIT_CMD_FIXEDBUSY_LEN 0x07 |
| 53 | #define WSPI_INIT_CMD_EN_FIXEDBUSY 0x80 |
| 54 | #define WSPI_INIT_CMD_DIS_FIXEDBUSY 0x00 |
| 55 | #define WSPI_INIT_CMD_IOD 0x40 |
| 56 | #define WSPI_INIT_CMD_IP 0x20 |
| 57 | #define WSPI_INIT_CMD_CS 0x10 |
| 58 | #define WSPI_INIT_CMD_WS 0x08 |
| 59 | #define WSPI_INIT_CMD_WSPI 0x01 |
| 60 | #define WSPI_INIT_CMD_END 0x01 |
| 61 | |
| 62 | #define WSPI_INIT_CMD_LEN 8 |
| 63 | |
| 64 | #define HW_ACCESS_WSPI_FIXED_BUSY_LEN \ |
| 65 | ((WL1271_BUSY_WORD_LEN - 4) / sizeof(u32)) |
| 66 | #define HW_ACCESS_WSPI_INIT_CMD_MASK 0 |
| 67 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 68 | /* HW limitation: maximum possible chunk size is 4095 bytes */ |
| 69 | #define WSPI_MAX_CHUNK_SIZE 4092 |
| 70 | |
| 71 | #define WSPI_MAX_NUM_OF_CHUNKS (WL1271_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE) |
| 72 | |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame] | 73 | struct wl12xx_spi_glue { |
| 74 | struct device *dev; |
Felipe Balbi | 0969d67 | 2011-10-05 09:29:13 +0300 | [diff] [blame] | 75 | struct platform_device *core; |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame] | 76 | }; |
| 77 | |
Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame^] | 78 | static void wl12xx_spi_reset(struct device *child) |
Teemu Paasikivi | 8197b71 | 2010-02-22 08:38:23 +0200 | [diff] [blame] | 79 | { |
Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame^] | 80 | struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 81 | u8 *cmd; |
| 82 | struct spi_transfer t; |
| 83 | struct spi_message m; |
| 84 | |
| 85 | cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); |
| 86 | if (!cmd) { |
| 87 | wl1271_error("could not allocate cmd for spi reset"); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | memset(&t, 0, sizeof(t)); |
| 92 | spi_message_init(&m); |
| 93 | |
| 94 | memset(cmd, 0xff, WSPI_INIT_CMD_LEN); |
| 95 | |
| 96 | t.tx_buf = cmd; |
| 97 | t.len = WSPI_INIT_CMD_LEN; |
| 98 | spi_message_add_tail(&t, &m); |
| 99 | |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame] | 100 | spi_sync(to_spi_device(glue->dev), &m); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 101 | |
| 102 | wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN); |
Dan Carpenter | 0dd3866 | 2010-12-21 07:00:13 +0300 | [diff] [blame] | 103 | kfree(cmd); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 104 | } |
| 105 | |
Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame^] | 106 | static void wl12xx_spi_init(struct device *child) |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 107 | { |
Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame^] | 108 | struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 109 | u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd; |
| 110 | struct spi_transfer t; |
| 111 | struct spi_message m; |
| 112 | |
| 113 | cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); |
| 114 | if (!cmd) { |
| 115 | wl1271_error("could not allocate cmd for spi init"); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | memset(crc, 0, sizeof(crc)); |
| 120 | memset(&t, 0, sizeof(t)); |
| 121 | spi_message_init(&m); |
| 122 | |
| 123 | /* |
| 124 | * Set WSPI_INIT_COMMAND |
| 125 | * the data is being send from the MSB to LSB |
| 126 | */ |
| 127 | cmd[2] = 0xff; |
| 128 | cmd[3] = 0xff; |
| 129 | cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX; |
| 130 | cmd[0] = 0; |
| 131 | cmd[7] = 0; |
| 132 | cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3; |
| 133 | cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN; |
| 134 | |
| 135 | if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0) |
| 136 | cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY; |
| 137 | else |
| 138 | cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY; |
| 139 | |
| 140 | cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS |
| 141 | | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS; |
| 142 | |
| 143 | crc[0] = cmd[1]; |
| 144 | crc[1] = cmd[0]; |
| 145 | crc[2] = cmd[7]; |
| 146 | crc[3] = cmd[6]; |
| 147 | crc[4] = cmd[5]; |
| 148 | |
| 149 | cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1; |
| 150 | cmd[4] |= WSPI_INIT_CMD_END; |
| 151 | |
| 152 | t.tx_buf = cmd; |
| 153 | t.len = WSPI_INIT_CMD_LEN; |
| 154 | spi_message_add_tail(&t, &m); |
| 155 | |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame] | 156 | spi_sync(to_spi_device(glue->dev), &m); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 157 | wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN); |
Kulikov Vasiliy | bb12361 | 2010-07-31 20:33:59 +0400 | [diff] [blame] | 158 | kfree(cmd); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 159 | } |
| 160 | |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 161 | #define WL1271_BUSY_WORD_TIMEOUT 1000 |
| 162 | |
Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame^] | 163 | static int wl12xx_spi_read_busy(struct device *child) |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 164 | { |
Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame^] | 165 | struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent); |
| 166 | struct wl1271 *wl = dev_get_drvdata(child); |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 167 | struct spi_transfer t[1]; |
| 168 | struct spi_message m; |
| 169 | u32 *busy_buf; |
| 170 | int num_busy_bytes = 0; |
| 171 | |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 172 | /* |
| 173 | * Read further busy words from SPI until a non-busy word is |
| 174 | * encountered, then read the data itself into the buffer. |
| 175 | */ |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 176 | |
| 177 | num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT; |
| 178 | busy_buf = wl->buffer_busyword; |
| 179 | while (num_busy_bytes) { |
| 180 | num_busy_bytes--; |
| 181 | spi_message_init(&m); |
| 182 | memset(t, 0, sizeof(t)); |
| 183 | t[0].rx_buf = busy_buf; |
| 184 | t[0].len = sizeof(u32); |
Juuso Oikarinen | 259da43 | 2010-03-26 12:53:18 +0200 | [diff] [blame] | 185 | t[0].cs_change = true; |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 186 | spi_message_add_tail(&t[0], &m); |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame] | 187 | spi_sync(to_spi_device(glue->dev), &m); |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 188 | |
Juuso Oikarinen | 259da43 | 2010-03-26 12:53:18 +0200 | [diff] [blame] | 189 | if (*busy_buf & 0x1) |
| 190 | return 0; |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* The SPI bus is unresponsive, the read failed. */ |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 194 | wl1271_error("SPI read busy-word timeout!\n"); |
Juuso Oikarinen | 259da43 | 2010-03-26 12:53:18 +0200 | [diff] [blame] | 195 | return -ETIMEDOUT; |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 196 | } |
| 197 | |
Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame^] | 198 | static void wl12xx_spi_raw_read(struct device *child, int addr, void *buf, |
Juuso Oikarinen | 259da43 | 2010-03-26 12:53:18 +0200 | [diff] [blame] | 199 | size_t len, bool fixed) |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 200 | { |
Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame^] | 201 | struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent); |
| 202 | struct wl1271 *wl = dev_get_drvdata(child); |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 203 | struct spi_transfer t[2]; |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 204 | struct spi_message m; |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 205 | u32 *busy_buf; |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 206 | u32 *cmd; |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 207 | u32 chunk_len; |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 208 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 209 | while (len > 0) { |
| 210 | chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 211 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 212 | cmd = &wl->buffer_cmd; |
| 213 | busy_buf = wl->buffer_busyword; |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 214 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 215 | *cmd = 0; |
| 216 | *cmd |= WSPI_CMD_READ; |
| 217 | *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) & |
| 218 | WSPI_CMD_BYTE_LENGTH; |
| 219 | *cmd |= addr & WSPI_CMD_BYTE_ADDR; |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 220 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 221 | if (fixed) |
| 222 | *cmd |= WSPI_CMD_FIXED; |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 223 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 224 | spi_message_init(&m); |
| 225 | memset(t, 0, sizeof(t)); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 226 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 227 | t[0].tx_buf = cmd; |
| 228 | t[0].len = 4; |
| 229 | t[0].cs_change = true; |
| 230 | spi_message_add_tail(&t[0], &m); |
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 | /* Busy and non busy words read */ |
| 233 | t[1].rx_buf = busy_buf; |
| 234 | t[1].len = WL1271_BUSY_WORD_LEN; |
| 235 | t[1].cs_change = true; |
| 236 | spi_message_add_tail(&t[1], &m); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 237 | |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame] | 238 | spi_sync(to_spi_device(glue->dev), &m); |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 239 | |
| 240 | if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) && |
Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame^] | 241 | wl12xx_spi_read_busy(child)) { |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 242 | memset(buf, 0, chunk_len); |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | spi_message_init(&m); |
| 247 | memset(t, 0, sizeof(t)); |
| 248 | |
| 249 | t[0].rx_buf = buf; |
| 250 | t[0].len = chunk_len; |
| 251 | t[0].cs_change = true; |
| 252 | spi_message_add_tail(&t[0], &m); |
| 253 | |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame] | 254 | spi_sync(to_spi_device(glue->dev), &m); |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 255 | |
| 256 | wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd)); |
| 257 | wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, chunk_len); |
| 258 | |
| 259 | if (!fixed) |
| 260 | addr += chunk_len; |
| 261 | buf += chunk_len; |
| 262 | len -= chunk_len; |
Juuso Oikarinen | 259da43 | 2010-03-26 12:53:18 +0200 | [diff] [blame] | 263 | } |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 264 | } |
| 265 | |
Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame^] | 266 | static void wl12xx_spi_raw_write(struct device *child, int addr, void *buf, |
| 267 | size_t len, bool fixed) |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 268 | { |
Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame^] | 269 | struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent); |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 270 | struct spi_transfer t[2 * WSPI_MAX_NUM_OF_CHUNKS]; |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 271 | struct spi_message m; |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 272 | u32 commands[WSPI_MAX_NUM_OF_CHUNKS]; |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 273 | u32 *cmd; |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 274 | u32 chunk_len; |
| 275 | int i; |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 276 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 277 | WARN_ON(len > WL1271_AGGR_BUFFER_SIZE); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 278 | |
| 279 | spi_message_init(&m); |
| 280 | memset(t, 0, sizeof(t)); |
| 281 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 282 | cmd = &commands[0]; |
| 283 | i = 0; |
| 284 | while (len > 0) { |
| 285 | chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 286 | |
Ido Yariv | 5c57a90 | 2010-09-30 13:28:26 +0200 | [diff] [blame] | 287 | *cmd = 0; |
| 288 | *cmd |= WSPI_CMD_WRITE; |
| 289 | *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) & |
| 290 | WSPI_CMD_BYTE_LENGTH; |
| 291 | *cmd |= addr & WSPI_CMD_BYTE_ADDR; |
| 292 | |
| 293 | if (fixed) |
| 294 | *cmd |= WSPI_CMD_FIXED; |
| 295 | |
| 296 | t[i].tx_buf = cmd; |
| 297 | t[i].len = sizeof(*cmd); |
| 298 | spi_message_add_tail(&t[i++], &m); |
| 299 | |
| 300 | t[i].tx_buf = buf; |
| 301 | t[i].len = chunk_len; |
| 302 | spi_message_add_tail(&t[i++], &m); |
| 303 | |
| 304 | wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd)); |
| 305 | wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, chunk_len); |
| 306 | |
| 307 | if (!fixed) |
| 308 | addr += chunk_len; |
| 309 | buf += chunk_len; |
| 310 | len -= chunk_len; |
| 311 | cmd++; |
| 312 | } |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 313 | |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame] | 314 | spi_sync(to_spi_device(glue->dev), &m); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 315 | } |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 316 | |
Teemu Paasikivi | 8197b71 | 2010-02-22 08:38:23 +0200 | [diff] [blame] | 317 | static struct wl1271_if_operations spi_ops = { |
Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame^] | 318 | .read = wl12xx_spi_raw_read, |
| 319 | .write = wl12xx_spi_raw_write, |
| 320 | .reset = wl12xx_spi_reset, |
| 321 | .init = wl12xx_spi_init, |
Luciano Coelho | a81159e | 2011-03-14 14:05:13 +0200 | [diff] [blame] | 322 | .set_block_size = NULL, |
Teemu Paasikivi | 8197b71 | 2010-02-22 08:38:23 +0200 | [diff] [blame] | 323 | }; |
| 324 | |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 325 | static int __devinit wl1271_probe(struct spi_device *spi) |
| 326 | { |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame] | 327 | struct wl12xx_spi_glue *glue; |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 328 | struct wl12xx_platform_data *pdata; |
Felipe Balbi | 0969d67 | 2011-10-05 09:29:13 +0300 | [diff] [blame] | 329 | struct resource res[1]; |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame] | 330 | int ret = -ENOMEM; |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 331 | |
| 332 | pdata = spi->dev.platform_data; |
| 333 | if (!pdata) { |
| 334 | wl1271_error("no platform data"); |
| 335 | return -ENODEV; |
| 336 | } |
| 337 | |
Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame^] | 338 | pdata->ops = &spi_ops; |
| 339 | |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame] | 340 | glue = kzalloc(sizeof(*glue), GFP_KERNEL); |
| 341 | if (!glue) { |
| 342 | wl1271_error("can't allocate glue"); |
| 343 | goto out; |
| 344 | } |
| 345 | |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame] | 346 | glue->dev = &spi->dev; |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame] | 347 | |
| 348 | spi_set_drvdata(spi, glue); |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 349 | |
| 350 | /* This is the only SPI value that we need to set here, the rest |
| 351 | * comes from the board-peripherals file */ |
| 352 | spi->bits_per_word = 32; |
| 353 | |
| 354 | ret = spi_setup(spi); |
| 355 | if (ret < 0) { |
| 356 | wl1271_error("spi_setup failed"); |
Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame^] | 357 | goto out_free_glue; |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 358 | } |
| 359 | |
Felipe Balbi | 0969d67 | 2011-10-05 09:29:13 +0300 | [diff] [blame] | 360 | glue->core = platform_device_alloc("wl12xx-spi", -1); |
| 361 | if (!glue->core) { |
| 362 | wl1271_error("can't allocate platform_device"); |
| 363 | ret = -ENOMEM; |
Felipe Balbi | a390e85 | 2011-10-06 10:07:44 +0300 | [diff] [blame^] | 364 | goto out_free_glue; |
Felipe Balbi | 0969d67 | 2011-10-05 09:29:13 +0300 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | glue->core->dev.parent = &spi->dev; |
| 368 | |
| 369 | memset(res, 0x00, sizeof(res)); |
| 370 | |
| 371 | res[0].start = spi->irq; |
| 372 | res[0].flags = IORESOURCE_IRQ; |
| 373 | res[0].name = "irq"; |
| 374 | |
| 375 | ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res)); |
| 376 | if (ret) { |
| 377 | wl1271_error("can't add resources"); |
| 378 | goto out_dev_put; |
| 379 | } |
| 380 | |
| 381 | ret = platform_device_add_data(glue->core, pdata, sizeof(*pdata)); |
| 382 | if (ret) { |
| 383 | wl1271_error("can't add platform data"); |
| 384 | goto out_dev_put; |
| 385 | } |
| 386 | |
| 387 | ret = platform_device_add(glue->core); |
| 388 | if (ret) { |
| 389 | wl1271_error("can't register platform device"); |
| 390 | goto out_dev_put; |
| 391 | } |
| 392 | |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 393 | return 0; |
| 394 | |
Felipe Balbi | 0969d67 | 2011-10-05 09:29:13 +0300 | [diff] [blame] | 395 | out_dev_put: |
| 396 | platform_device_put(glue->core); |
| 397 | |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame] | 398 | out_free_glue: |
| 399 | kfree(glue); |
| 400 | out: |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 401 | return ret; |
| 402 | } |
| 403 | |
| 404 | static int __devexit wl1271_remove(struct spi_device *spi) |
| 405 | { |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame] | 406 | struct wl12xx_spi_glue *glue = spi_get_drvdata(spi); |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 407 | |
Felipe Balbi | 0969d67 | 2011-10-05 09:29:13 +0300 | [diff] [blame] | 408 | platform_device_del(glue->core); |
| 409 | platform_device_put(glue->core); |
Felipe Balbi | b65019f | 2011-10-04 23:36:47 +0300 | [diff] [blame] | 410 | kfree(glue); |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | |
| 416 | static struct spi_driver wl1271_spi_driver = { |
| 417 | .driver = { |
Luciano Coelho | 7fdd50d | 2010-03-26 12:53:10 +0200 | [diff] [blame] | 418 | .name = "wl1271_spi", |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 419 | .bus = &spi_bus_type, |
| 420 | .owner = THIS_MODULE, |
| 421 | }, |
| 422 | |
| 423 | .probe = wl1271_probe, |
| 424 | .remove = __devexit_p(wl1271_remove), |
| 425 | }; |
| 426 | |
| 427 | static int __init wl1271_init(void) |
| 428 | { |
Felipe Balbi | ba2274c | 2011-05-14 00:26:23 +0300 | [diff] [blame] | 429 | return spi_register_driver(&wl1271_spi_driver); |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static void __exit wl1271_exit(void) |
| 433 | { |
| 434 | spi_unregister_driver(&wl1271_spi_driver); |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | module_init(wl1271_init); |
| 438 | module_exit(wl1271_exit); |
| 439 | |
| 440 | MODULE_LICENSE("GPL"); |
Luciano Coelho | 5245e3a | 2011-03-30 21:31:39 +0300 | [diff] [blame] | 441 | MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>"); |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame] | 442 | MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>"); |
Arik Nemtsov | c302b2c | 2011-08-17 10:45:48 +0300 | [diff] [blame] | 443 | MODULE_FIRMWARE(WL127X_FW_NAME); |
Shahar Levi | 5aa4234 | 2011-03-06 16:32:07 +0200 | [diff] [blame] | 444 | MODULE_FIRMWARE(WL128X_FW_NAME); |
Ameya Palande | f148cfd | 2010-07-05 17:12:38 +0300 | [diff] [blame] | 445 | MODULE_ALIAS("spi:wl1271"); |