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 | |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame^] | 24 | #include <linux/irq.h> |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 25 | #include <linux/module.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/crc7.h> |
| 28 | #include <linux/spi/spi.h> |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame^] | 29 | #include <linux/spi/wl12xx.h> |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 30 | |
| 31 | #include "wl1271.h" |
| 32 | #include "wl12xx_80211.h" |
| 33 | #include "wl1271_spi.h" |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame^] | 34 | #include "wl1271_io.h" |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 35 | |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 36 | |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 37 | void wl1271_spi_reset(struct wl1271 *wl) |
| 38 | { |
| 39 | u8 *cmd; |
| 40 | struct spi_transfer t; |
| 41 | struct spi_message m; |
| 42 | |
| 43 | cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); |
| 44 | if (!cmd) { |
| 45 | wl1271_error("could not allocate cmd for spi reset"); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | memset(&t, 0, sizeof(t)); |
| 50 | spi_message_init(&m); |
| 51 | |
| 52 | memset(cmd, 0xff, WSPI_INIT_CMD_LEN); |
| 53 | |
| 54 | t.tx_buf = cmd; |
| 55 | t.len = WSPI_INIT_CMD_LEN; |
| 56 | spi_message_add_tail(&t, &m); |
| 57 | |
| 58 | spi_sync(wl->spi, &m); |
| 59 | |
| 60 | wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN); |
| 61 | } |
| 62 | |
| 63 | void wl1271_spi_init(struct wl1271 *wl) |
| 64 | { |
| 65 | u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd; |
| 66 | struct spi_transfer t; |
| 67 | struct spi_message m; |
| 68 | |
| 69 | cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); |
| 70 | if (!cmd) { |
| 71 | wl1271_error("could not allocate cmd for spi init"); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | memset(crc, 0, sizeof(crc)); |
| 76 | memset(&t, 0, sizeof(t)); |
| 77 | spi_message_init(&m); |
| 78 | |
| 79 | /* |
| 80 | * Set WSPI_INIT_COMMAND |
| 81 | * the data is being send from the MSB to LSB |
| 82 | */ |
| 83 | cmd[2] = 0xff; |
| 84 | cmd[3] = 0xff; |
| 85 | cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX; |
| 86 | cmd[0] = 0; |
| 87 | cmd[7] = 0; |
| 88 | cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3; |
| 89 | cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN; |
| 90 | |
| 91 | if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0) |
| 92 | cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY; |
| 93 | else |
| 94 | cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY; |
| 95 | |
| 96 | cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS |
| 97 | | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS; |
| 98 | |
| 99 | crc[0] = cmd[1]; |
| 100 | crc[1] = cmd[0]; |
| 101 | crc[2] = cmd[7]; |
| 102 | crc[3] = cmd[6]; |
| 103 | crc[4] = cmd[5]; |
| 104 | |
| 105 | cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1; |
| 106 | cmd[4] |= WSPI_INIT_CMD_END; |
| 107 | |
| 108 | t.tx_buf = cmd; |
| 109 | t.len = WSPI_INIT_CMD_LEN; |
| 110 | spi_message_add_tail(&t, &m); |
| 111 | |
| 112 | spi_sync(wl->spi, &m); |
| 113 | |
| 114 | wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN); |
| 115 | } |
| 116 | |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 117 | #define WL1271_BUSY_WORD_TIMEOUT 1000 |
| 118 | |
Luciano Coelho | 938e30c | 2009-10-15 10:33:27 +0300 | [diff] [blame] | 119 | /* FIXME: Check busy words, removed due to SPI bug */ |
| 120 | #if 0 |
| 121 | static void wl1271_spi_read_busy(struct wl1271 *wl, void *buf, size_t len) |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 122 | { |
| 123 | struct spi_transfer t[1]; |
| 124 | struct spi_message m; |
| 125 | u32 *busy_buf; |
| 126 | int num_busy_bytes = 0; |
| 127 | |
| 128 | wl1271_info("spi read BUSY!"); |
| 129 | |
| 130 | /* |
| 131 | * Look for the non-busy word in the read buffer, and if found, |
| 132 | * read in the remaining data into the buffer. |
| 133 | */ |
| 134 | busy_buf = (u32 *)buf; |
| 135 | for (; (u32)busy_buf < (u32)buf + len; busy_buf++) { |
| 136 | num_busy_bytes += sizeof(u32); |
| 137 | if (*busy_buf & 0x1) { |
| 138 | spi_message_init(&m); |
| 139 | memset(t, 0, sizeof(t)); |
| 140 | memmove(buf, busy_buf, len - num_busy_bytes); |
| 141 | t[0].rx_buf = buf + (len - num_busy_bytes); |
| 142 | t[0].len = num_busy_bytes; |
| 143 | spi_message_add_tail(&t[0], &m); |
| 144 | spi_sync(wl->spi, &m); |
| 145 | return; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Read further busy words from SPI until a non-busy word is |
| 151 | * encountered, then read the data itself into the buffer. |
| 152 | */ |
| 153 | wl1271_info("spi read BUSY-polling needed!"); |
| 154 | |
| 155 | num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT; |
| 156 | busy_buf = wl->buffer_busyword; |
| 157 | while (num_busy_bytes) { |
| 158 | num_busy_bytes--; |
| 159 | spi_message_init(&m); |
| 160 | memset(t, 0, sizeof(t)); |
| 161 | t[0].rx_buf = busy_buf; |
| 162 | t[0].len = sizeof(u32); |
| 163 | spi_message_add_tail(&t[0], &m); |
| 164 | spi_sync(wl->spi, &m); |
| 165 | |
| 166 | if (*busy_buf & 0x1) { |
| 167 | spi_message_init(&m); |
| 168 | memset(t, 0, sizeof(t)); |
| 169 | t[0].rx_buf = buf; |
| 170 | t[0].len = len; |
| 171 | spi_message_add_tail(&t[0], &m); |
| 172 | spi_sync(wl->spi, &m); |
| 173 | return; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /* The SPI bus is unresponsive, the read failed. */ |
| 178 | memset(buf, 0, len); |
| 179 | wl1271_error("SPI read busy-word timeout!\n"); |
| 180 | } |
Luciano Coelho | 938e30c | 2009-10-15 10:33:27 +0300 | [diff] [blame] | 181 | #endif |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 182 | |
Juuso Oikarinen | 7462141 | 2009-10-12 15:08:54 +0300 | [diff] [blame] | 183 | void wl1271_spi_raw_read(struct wl1271 *wl, int addr, void *buf, |
| 184 | size_t len, bool fixed) |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 185 | { |
| 186 | struct spi_transfer t[3]; |
| 187 | struct spi_message m; |
Juuso Oikarinen | 545f1da | 2009-10-08 21:56:23 +0300 | [diff] [blame] | 188 | u32 *busy_buf; |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 189 | u32 *cmd; |
| 190 | |
| 191 | cmd = &wl->buffer_cmd; |
| 192 | busy_buf = wl->buffer_busyword; |
| 193 | |
| 194 | *cmd = 0; |
| 195 | *cmd |= WSPI_CMD_READ; |
| 196 | *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH; |
| 197 | *cmd |= addr & WSPI_CMD_BYTE_ADDR; |
| 198 | |
| 199 | if (fixed) |
| 200 | *cmd |= WSPI_CMD_FIXED; |
| 201 | |
| 202 | spi_message_init(&m); |
| 203 | memset(t, 0, sizeof(t)); |
| 204 | |
| 205 | t[0].tx_buf = cmd; |
| 206 | t[0].len = 4; |
| 207 | spi_message_add_tail(&t[0], &m); |
| 208 | |
| 209 | /* Busy and non busy words read */ |
| 210 | t[1].rx_buf = busy_buf; |
| 211 | t[1].len = WL1271_BUSY_WORD_LEN; |
| 212 | spi_message_add_tail(&t[1], &m); |
| 213 | |
| 214 | t[2].rx_buf = buf; |
| 215 | t[2].len = len; |
| 216 | spi_message_add_tail(&t[2], &m); |
| 217 | |
| 218 | spi_sync(wl->spi, &m); |
| 219 | |
Juuso Oikarinen | c6d5d06 | 2009-10-13 12:47:47 +0300 | [diff] [blame] | 220 | /* FIXME: Check busy words, removed due to SPI bug */ |
| 221 | /* if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1)) |
| 222 | wl1271_spi_read_busy(wl, buf, len); */ |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 223 | |
| 224 | wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd)); |
| 225 | wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, len); |
| 226 | } |
| 227 | |
Juuso Oikarinen | 7462141 | 2009-10-12 15:08:54 +0300 | [diff] [blame] | 228 | void wl1271_spi_raw_write(struct wl1271 *wl, int addr, void *buf, |
| 229 | size_t len, bool fixed) |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 230 | { |
| 231 | struct spi_transfer t[2]; |
| 232 | struct spi_message m; |
| 233 | u32 *cmd; |
| 234 | |
| 235 | cmd = &wl->buffer_cmd; |
| 236 | |
| 237 | *cmd = 0; |
| 238 | *cmd |= WSPI_CMD_WRITE; |
| 239 | *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH; |
| 240 | *cmd |= addr & WSPI_CMD_BYTE_ADDR; |
| 241 | |
| 242 | if (fixed) |
| 243 | *cmd |= WSPI_CMD_FIXED; |
| 244 | |
| 245 | spi_message_init(&m); |
| 246 | memset(t, 0, sizeof(t)); |
| 247 | |
| 248 | t[0].tx_buf = cmd; |
| 249 | t[0].len = sizeof(*cmd); |
| 250 | spi_message_add_tail(&t[0], &m); |
| 251 | |
| 252 | t[1].tx_buf = buf; |
| 253 | t[1].len = len; |
| 254 | spi_message_add_tail(&t[1], &m); |
| 255 | |
| 256 | spi_sync(wl->spi, &m); |
| 257 | |
| 258 | wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd)); |
| 259 | wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, len); |
| 260 | } |
Teemu Paasikivi | 2d5e82b | 2010-02-22 08:38:21 +0200 | [diff] [blame^] | 261 | |
| 262 | static irqreturn_t wl1271_irq(int irq, void *cookie) |
| 263 | { |
| 264 | struct wl1271 *wl; |
| 265 | unsigned long flags; |
| 266 | |
| 267 | wl1271_debug(DEBUG_IRQ, "IRQ"); |
| 268 | |
| 269 | wl = cookie; |
| 270 | |
| 271 | /* complete the ELP completion */ |
| 272 | spin_lock_irqsave(&wl->wl_lock, flags); |
| 273 | if (wl->elp_compl) { |
| 274 | complete(wl->elp_compl); |
| 275 | wl->elp_compl = NULL; |
| 276 | } |
| 277 | |
| 278 | ieee80211_queue_work(wl->hw, &wl->irq_work); |
| 279 | spin_unlock_irqrestore(&wl->wl_lock, flags); |
| 280 | |
| 281 | return IRQ_HANDLED; |
| 282 | } |
| 283 | |
| 284 | static void wl1271_device_release(struct device *dev) |
| 285 | { |
| 286 | |
| 287 | } |
| 288 | |
| 289 | static struct platform_device wl1271_device = { |
| 290 | .name = "wl1271", |
| 291 | .id = -1, |
| 292 | |
| 293 | /* device model insists to have a release function */ |
| 294 | .dev = { |
| 295 | .release = wl1271_device_release, |
| 296 | }, |
| 297 | }; |
| 298 | |
| 299 | static int __devinit wl1271_probe(struct spi_device *spi) |
| 300 | { |
| 301 | struct wl12xx_platform_data *pdata; |
| 302 | struct ieee80211_hw *hw; |
| 303 | struct wl1271 *wl; |
| 304 | int ret; |
| 305 | |
| 306 | pdata = spi->dev.platform_data; |
| 307 | if (!pdata) { |
| 308 | wl1271_error("no platform data"); |
| 309 | return -ENODEV; |
| 310 | } |
| 311 | |
| 312 | hw = wl1271_alloc_hw(); |
| 313 | if (IS_ERR(hw)) |
| 314 | return PTR_ERR(hw); |
| 315 | |
| 316 | wl = hw->priv; |
| 317 | |
| 318 | dev_set_drvdata(&spi->dev, wl); |
| 319 | wl->spi = spi; |
| 320 | |
| 321 | /* This is the only SPI value that we need to set here, the rest |
| 322 | * comes from the board-peripherals file */ |
| 323 | spi->bits_per_word = 32; |
| 324 | |
| 325 | ret = spi_setup(spi); |
| 326 | if (ret < 0) { |
| 327 | wl1271_error("spi_setup failed"); |
| 328 | goto out_free; |
| 329 | } |
| 330 | |
| 331 | wl->set_power = pdata->set_power; |
| 332 | if (!wl->set_power) { |
| 333 | wl1271_error("set power function missing in platform data"); |
| 334 | ret = -ENODEV; |
| 335 | goto out_free; |
| 336 | } |
| 337 | |
| 338 | wl->irq = spi->irq; |
| 339 | if (wl->irq < 0) { |
| 340 | wl1271_error("irq missing in platform data"); |
| 341 | ret = -ENODEV; |
| 342 | goto out_free; |
| 343 | } |
| 344 | |
| 345 | ret = request_irq(wl->irq, wl1271_irq, 0, DRIVER_NAME, wl); |
| 346 | if (ret < 0) { |
| 347 | wl1271_error("request_irq() failed: %d", ret); |
| 348 | goto out_free; |
| 349 | } |
| 350 | |
| 351 | set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING); |
| 352 | |
| 353 | disable_irq(wl->irq); |
| 354 | |
| 355 | ret = platform_device_register(&wl1271_device); |
| 356 | if (ret) { |
| 357 | wl1271_error("couldn't register platform device"); |
| 358 | goto out_irq; |
| 359 | } |
| 360 | dev_set_drvdata(&wl1271_device.dev, wl); |
| 361 | |
| 362 | ret = wl1271_init_ieee80211(wl); |
| 363 | if (ret) |
| 364 | goto out_platform; |
| 365 | |
| 366 | ret = wl1271_register_hw(wl); |
| 367 | if (ret) |
| 368 | goto out_platform; |
| 369 | |
| 370 | wl1271_notice("initialized"); |
| 371 | |
| 372 | return 0; |
| 373 | |
| 374 | out_platform: |
| 375 | platform_device_unregister(&wl1271_device); |
| 376 | |
| 377 | out_irq: |
| 378 | free_irq(wl->irq, wl); |
| 379 | |
| 380 | out_free: |
| 381 | ieee80211_free_hw(hw); |
| 382 | |
| 383 | return ret; |
| 384 | } |
| 385 | |
| 386 | static int __devexit wl1271_remove(struct spi_device *spi) |
| 387 | { |
| 388 | struct wl1271 *wl = dev_get_drvdata(&spi->dev); |
| 389 | |
| 390 | platform_device_unregister(&wl1271_device); |
| 391 | free_irq(wl->irq, wl); |
| 392 | |
| 393 | wl1271_free_hw(wl); |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | |
| 399 | static struct spi_driver wl1271_spi_driver = { |
| 400 | .driver = { |
| 401 | .name = "wl1271", |
| 402 | .bus = &spi_bus_type, |
| 403 | .owner = THIS_MODULE, |
| 404 | }, |
| 405 | |
| 406 | .probe = wl1271_probe, |
| 407 | .remove = __devexit_p(wl1271_remove), |
| 408 | }; |
| 409 | |
| 410 | static int __init wl1271_init(void) |
| 411 | { |
| 412 | int ret; |
| 413 | |
| 414 | ret = spi_register_driver(&wl1271_spi_driver); |
| 415 | if (ret < 0) { |
| 416 | wl1271_error("failed to register spi driver: %d", ret); |
| 417 | goto out; |
| 418 | } |
| 419 | |
| 420 | out: |
| 421 | return ret; |
| 422 | } |
| 423 | |
| 424 | static void __exit wl1271_exit(void) |
| 425 | { |
| 426 | spi_unregister_driver(&wl1271_spi_driver); |
| 427 | |
| 428 | wl1271_notice("unloaded"); |
| 429 | } |
| 430 | |
| 431 | module_init(wl1271_init); |
| 432 | module_exit(wl1271_exit); |
| 433 | |
| 434 | MODULE_LICENSE("GPL"); |
| 435 | MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>"); |
| 436 | MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>"); |
| 437 | MODULE_FIRMWARE(WL1271_FW_NAME); |