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/interrupt.h> 25 #include <linux/irq.h> 26 #include <linux/module.h> 27 #include <linux/slab.h> 28 #include <linux/swab.h> 29 #include <linux/crc7.h> 30 #include <linux/spi/spi.h> 31 #include <linux/wl12xx.h> 32 #include <linux/platform_device.h> 33 34 #include "wlcore.h" 35 #include "wl12xx_80211.h" 36 #include "io.h" 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 67 /* HW limitation: maximum possible chunk size is 4095 bytes */ 68 #define WSPI_MAX_CHUNK_SIZE 4092 69 70 /* 71 * only support SPI for 12xx - this code should be reworked when 18xx 72 * support is introduced 73 */ 74 #define SPI_AGGR_BUFFER_SIZE (4 * PAGE_SIZE) 75 76 #define WSPI_MAX_NUM_OF_CHUNKS (SPI_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE) 77 78 struct wl12xx_spi_glue { 79 struct device *dev; 80 struct platform_device *core; 81 }; 82 83 static void wl12xx_spi_reset(struct device *child) 84 { 85 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent); 86 u8 *cmd; 87 struct spi_transfer t; 88 struct spi_message m; 89 90 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); 91 if (!cmd) { 92 dev_err(child->parent, 93 "could not allocate cmd for spi reset\n"); 94 return; 95 } 96 97 memset(&t, 0, sizeof(t)); 98 spi_message_init(&m); 99 100 memset(cmd, 0xff, WSPI_INIT_CMD_LEN); 101 102 t.tx_buf = cmd; 103 t.len = WSPI_INIT_CMD_LEN; 104 spi_message_add_tail(&t, &m); 105 106 spi_sync(to_spi_device(glue->dev), &m); 107 108 kfree(cmd); 109 } 110 111 static void wl12xx_spi_init(struct device *child) 112 { 113 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent); 114 struct spi_transfer t; 115 struct spi_message m; 116 u8 *cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); 117 118 if (!cmd) { 119 dev_err(child->parent, 120 "could not allocate cmd for spi init\n"); 121 return; 122 } 123 124 memset(&t, 0, sizeof(t)); 125 spi_message_init(&m); 126 127 /* 128 * Set WSPI_INIT_COMMAND 129 * the data is being send from the MSB to LSB 130 */ 131 cmd[0] = 0xff; 132 cmd[1] = 0xff; 133 cmd[2] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX; 134 cmd[3] = 0; 135 cmd[4] = 0; 136 cmd[5] = HW_ACCESS_WSPI_INIT_CMD_MASK << 3; 137 cmd[5] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN; 138 139 cmd[6] = WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS 140 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS; 141 142 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0) 143 cmd[6] |= WSPI_INIT_CMD_DIS_FIXEDBUSY; 144 else 145 cmd[6] |= WSPI_INIT_CMD_EN_FIXEDBUSY; 146 147 cmd[7] = crc7_be(0, cmd+2, WSPI_INIT_CMD_CRC_LEN) | WSPI_INIT_CMD_END; 148 /* 149 * The above is the logical order; it must actually be stored 150 * in the buffer byte-swapped. 151 */ 152 __swab32s((u32 *)cmd); 153 __swab32s((u32 *)cmd+1); 154 155 t.tx_buf = cmd; 156 t.len = WSPI_INIT_CMD_LEN; 157 spi_message_add_tail(&t, &m); 158 159 spi_sync(to_spi_device(glue->dev), &m); 160 kfree(cmd); 161 } 162 163 #define WL1271_BUSY_WORD_TIMEOUT 1000 164 165 static int wl12xx_spi_read_busy(struct device *child) 166 { 167 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent); 168 struct wl1271 *wl = dev_get_drvdata(child); 169 struct spi_transfer t[1]; 170 struct spi_message m; 171 u32 *busy_buf; 172 int num_busy_bytes = 0; 173 174 /* 175 * Read further busy words from SPI until a non-busy word is 176 * encountered, then read the data itself into the buffer. 177 */ 178 179 num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT; 180 busy_buf = wl->buffer_busyword; 181 while (num_busy_bytes) { 182 num_busy_bytes--; 183 spi_message_init(&m); 184 memset(t, 0, sizeof(t)); 185 t[0].rx_buf = busy_buf; 186 t[0].len = sizeof(u32); 187 t[0].cs_change = true; 188 spi_message_add_tail(&t[0], &m); 189 spi_sync(to_spi_device(glue->dev), &m); 190 191 if (*busy_buf & 0x1) 192 return 0; 193 } 194 195 /* The SPI bus is unresponsive, the read failed. */ 196 dev_err(child->parent, "SPI read busy-word timeout!\n"); 197 return -ETIMEDOUT; 198 } 199 200 static int __must_check wl12xx_spi_raw_read(struct device *child, int addr, 201 void *buf, size_t len, bool fixed) 202 { 203 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent); 204 struct wl1271 *wl = dev_get_drvdata(child); 205 struct spi_transfer t[2]; 206 struct spi_message m; 207 u32 *busy_buf; 208 u32 *cmd; 209 u32 chunk_len; 210 211 while (len > 0) { 212 chunk_len = min_t(size_t, WSPI_MAX_CHUNK_SIZE, len); 213 214 cmd = &wl->buffer_cmd; 215 busy_buf = wl->buffer_busyword; 216 217 *cmd = 0; 218 *cmd |= WSPI_CMD_READ; 219 *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) & 220 WSPI_CMD_BYTE_LENGTH; 221 *cmd |= addr & WSPI_CMD_BYTE_ADDR; 222 223 if (fixed) 224 *cmd |= WSPI_CMD_FIXED; 225 226 spi_message_init(&m); 227 memset(t, 0, sizeof(t)); 228 229 t[0].tx_buf = cmd; 230 t[0].len = 4; 231 t[0].cs_change = true; 232 spi_message_add_tail(&t[0], &m); 233 234 /* Busy and non busy words read */ 235 t[1].rx_buf = busy_buf; 236 t[1].len = WL1271_BUSY_WORD_LEN; 237 t[1].cs_change = true; 238 spi_message_add_tail(&t[1], &m); 239 240 spi_sync(to_spi_device(glue->dev), &m); 241 242 if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) && 243 wl12xx_spi_read_busy(child)) { 244 memset(buf, 0, chunk_len); 245 return 0; 246 } 247 248 spi_message_init(&m); 249 memset(t, 0, sizeof(t)); 250 251 t[0].rx_buf = buf; 252 t[0].len = chunk_len; 253 t[0].cs_change = true; 254 spi_message_add_tail(&t[0], &m); 255 256 spi_sync(to_spi_device(glue->dev), &m); 257 258 if (!fixed) 259 addr += chunk_len; 260 buf += chunk_len; 261 len -= chunk_len; 262 } 263 264 return 0; 265 } 266 267 static int __must_check wl12xx_spi_raw_write(struct device *child, int addr, 268 void *buf, size_t len, bool fixed) 269 { 270 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent); 271 struct spi_transfer t[2 * (WSPI_MAX_NUM_OF_CHUNKS + 1)]; 272 struct spi_message m; 273 u32 commands[WSPI_MAX_NUM_OF_CHUNKS]; 274 u32 *cmd; 275 u32 chunk_len; 276 int i; 277 278 WARN_ON(len > SPI_AGGR_BUFFER_SIZE); 279 280 spi_message_init(&m); 281 memset(t, 0, sizeof(t)); 282 283 cmd = &commands[0]; 284 i = 0; 285 while (len > 0) { 286 chunk_len = min_t(size_t, WSPI_MAX_CHUNK_SIZE, len); 287 288 *cmd = 0; 289 *cmd |= WSPI_CMD_WRITE; 290 *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) & 291 WSPI_CMD_BYTE_LENGTH; 292 *cmd |= addr & WSPI_CMD_BYTE_ADDR; 293 294 if (fixed) 295 *cmd |= WSPI_CMD_FIXED; 296 297 t[i].tx_buf = cmd; 298 t[i].len = sizeof(*cmd); 299 spi_message_add_tail(&t[i++], &m); 300 301 t[i].tx_buf = buf; 302 t[i].len = chunk_len; 303 spi_message_add_tail(&t[i++], &m); 304 305 if (!fixed) 306 addr += chunk_len; 307 buf += chunk_len; 308 len -= chunk_len; 309 cmd++; 310 } 311 312 spi_sync(to_spi_device(glue->dev), &m); 313 314 return 0; 315 } 316 317 static struct wl1271_if_operations spi_ops = { 318 .read = wl12xx_spi_raw_read, 319 .write = wl12xx_spi_raw_write, 320 .reset = wl12xx_spi_reset, 321 .init = wl12xx_spi_init, 322 .set_block_size = NULL, 323 }; 324 325 static int wl1271_probe(struct spi_device *spi) 326 { 327 struct wl12xx_spi_glue *glue; 328 struct wlcore_platdev_data pdev_data; 329 struct resource res[1]; 330 int ret; 331 332 memset(&pdev_data, 0x00, sizeof(pdev_data)); 333 334 /* TODO: add DT parsing when needed */ 335 336 pdev_data.if_ops = &spi_ops; 337 338 glue = devm_kzalloc(&spi->dev, sizeof(*glue), GFP_KERNEL); 339 if (!glue) { 340 dev_err(&spi->dev, "can't allocate glue\n"); 341 return -ENOMEM; 342 } 343 344 glue->dev = &spi->dev; 345 346 spi_set_drvdata(spi, glue); 347 348 /* This is the only SPI value that we need to set here, the rest 349 * comes from the board-peripherals file */ 350 spi->bits_per_word = 32; 351 352 ret = spi_setup(spi); 353 if (ret < 0) { 354 dev_err(glue->dev, "spi_setup failed\n"); 355 return ret; 356 } 357 358 glue->core = platform_device_alloc("wl12xx", PLATFORM_DEVID_AUTO); 359 if (!glue->core) { 360 dev_err(glue->dev, "can't allocate platform_device\n"); 361 return -ENOMEM; 362 } 363 364 glue->core->dev.parent = &spi->dev; 365 366 memset(res, 0x00, sizeof(res)); 367 368 res[0].start = spi->irq; 369 res[0].flags = IORESOURCE_IRQ; 370 res[0].name = "irq"; 371 372 ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res)); 373 if (ret) { 374 dev_err(glue->dev, "can't add resources\n"); 375 goto out_dev_put; 376 } 377 378 ret = platform_device_add_data(glue->core, &pdev_data, 379 sizeof(pdev_data)); 380 if (ret) { 381 dev_err(glue->dev, "can't add platform data\n"); 382 goto out_dev_put; 383 } 384 385 ret = platform_device_add(glue->core); 386 if (ret) { 387 dev_err(glue->dev, "can't register platform device\n"); 388 goto out_dev_put; 389 } 390 391 return 0; 392 393 out_dev_put: 394 platform_device_put(glue->core); 395 return ret; 396 } 397 398 static int wl1271_remove(struct spi_device *spi) 399 { 400 struct wl12xx_spi_glue *glue = spi_get_drvdata(spi); 401 402 platform_device_unregister(glue->core); 403 404 return 0; 405 } 406 407 408 static struct spi_driver wl1271_spi_driver = { 409 .driver = { 410 .name = "wl1271_spi", 411 .owner = THIS_MODULE, 412 }, 413 414 .probe = wl1271_probe, 415 .remove = wl1271_remove, 416 }; 417 418 module_spi_driver(wl1271_spi_driver); 419 MODULE_LICENSE("GPL"); 420 MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>"); 421 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>"); 422 MODULE_ALIAS("spi:wl1271"); 423