Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 1 | /* |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 2 | * This file is part of wl1251 |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 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 | |
Bob Copeland | 8e639c0 | 2009-08-07 13:33:26 +0300 | [diff] [blame] | 24 | #include <linux/irq.h> |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 25 | #include <linux/module.h> |
| 26 | #include <linux/crc7.h> |
| 27 | #include <linux/spi/spi.h> |
Bob Copeland | 8e639c0 | 2009-08-07 13:33:26 +0300 | [diff] [blame] | 28 | #include <linux/spi/wl12xx.h> |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 29 | |
Kalle Valo | 1367411 | 2009-06-12 14:17:25 +0300 | [diff] [blame] | 30 | #include "wl1251.h" |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 31 | #include "reg.h" |
Kalle Valo | ef2f8d4 | 2009-06-12 14:17:19 +0300 | [diff] [blame] | 32 | #include "wl1251_spi.h" |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 33 | |
Bob Copeland | af8c78e | 2009-08-07 13:33:34 +0300 | [diff] [blame^] | 34 | static struct spi_device *wl_to_spi(struct wl1251 *wl) |
| 35 | { |
| 36 | return wl->if_priv; |
| 37 | } |
| 38 | |
Bob Copeland | 08d9f572 | 2009-08-07 13:33:11 +0300 | [diff] [blame] | 39 | static void wl1251_spi_reset(struct wl1251 *wl) |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 40 | { |
| 41 | u8 *cmd; |
| 42 | struct spi_transfer t; |
| 43 | struct spi_message m; |
| 44 | |
| 45 | cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); |
| 46 | if (!cmd) { |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 47 | wl1251_error("could not allocate cmd for spi reset"); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 48 | return; |
| 49 | } |
| 50 | |
| 51 | memset(&t, 0, sizeof(t)); |
| 52 | spi_message_init(&m); |
| 53 | |
| 54 | memset(cmd, 0xff, WSPI_INIT_CMD_LEN); |
| 55 | |
| 56 | t.tx_buf = cmd; |
| 57 | t.len = WSPI_INIT_CMD_LEN; |
| 58 | spi_message_add_tail(&t, &m); |
| 59 | |
Bob Copeland | af8c78e | 2009-08-07 13:33:34 +0300 | [diff] [blame^] | 60 | spi_sync(wl_to_spi(wl), &m); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 61 | |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 62 | wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 63 | } |
| 64 | |
Bob Copeland | 8e639c0 | 2009-08-07 13:33:26 +0300 | [diff] [blame] | 65 | static void wl1251_spi_wake(struct wl1251 *wl) |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 66 | { |
| 67 | u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd; |
| 68 | struct spi_transfer t; |
| 69 | struct spi_message m; |
| 70 | |
| 71 | cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); |
| 72 | if (!cmd) { |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 73 | wl1251_error("could not allocate cmd for spi init"); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 74 | return; |
| 75 | } |
| 76 | |
| 77 | memset(crc, 0, sizeof(crc)); |
| 78 | memset(&t, 0, sizeof(t)); |
| 79 | spi_message_init(&m); |
| 80 | |
| 81 | /* |
| 82 | * Set WSPI_INIT_COMMAND |
| 83 | * the data is being send from the MSB to LSB |
| 84 | */ |
| 85 | cmd[2] = 0xff; |
| 86 | cmd[3] = 0xff; |
| 87 | cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX; |
| 88 | cmd[0] = 0; |
| 89 | cmd[7] = 0; |
| 90 | cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3; |
| 91 | cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN; |
| 92 | |
| 93 | if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0) |
| 94 | cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY; |
| 95 | else |
| 96 | cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY; |
| 97 | |
| 98 | cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS |
| 99 | | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS; |
| 100 | |
| 101 | crc[0] = cmd[1]; |
| 102 | crc[1] = cmd[0]; |
| 103 | crc[2] = cmd[7]; |
| 104 | crc[3] = cmd[6]; |
| 105 | crc[4] = cmd[5]; |
| 106 | |
| 107 | cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1; |
| 108 | cmd[4] |= WSPI_INIT_CMD_END; |
| 109 | |
| 110 | t.tx_buf = cmd; |
| 111 | t.len = WSPI_INIT_CMD_LEN; |
| 112 | spi_message_add_tail(&t, &m); |
| 113 | |
Bob Copeland | af8c78e | 2009-08-07 13:33:34 +0300 | [diff] [blame^] | 114 | spi_sync(wl_to_spi(wl), &m); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 115 | |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 116 | wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 117 | } |
| 118 | |
Bob Copeland | 08d9f572 | 2009-08-07 13:33:11 +0300 | [diff] [blame] | 119 | static void wl1251_spi_reset_wake(struct wl1251 *wl) |
| 120 | { |
| 121 | wl1251_spi_reset(wl); |
Bob Copeland | 8e639c0 | 2009-08-07 13:33:26 +0300 | [diff] [blame] | 122 | wl1251_spi_wake(wl); |
Bob Copeland | 08d9f572 | 2009-08-07 13:33:11 +0300 | [diff] [blame] | 123 | } |
| 124 | |
Bob Copeland | 08d9f572 | 2009-08-07 13:33:11 +0300 | [diff] [blame] | 125 | static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf, |
| 126 | size_t len) |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 127 | { |
| 128 | struct spi_transfer t[3]; |
| 129 | struct spi_message m; |
Kalle Valo | 5262c12 | 2009-06-12 14:14:55 +0300 | [diff] [blame] | 130 | u8 *busy_buf; |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 131 | u32 *cmd; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 132 | |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 133 | cmd = &wl->buffer_cmd; |
Kalle Valo | 5262c12 | 2009-06-12 14:14:55 +0300 | [diff] [blame] | 134 | busy_buf = wl->buffer_busyword; |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 135 | |
| 136 | *cmd = 0; |
| 137 | *cmd |= WSPI_CMD_READ; |
| 138 | *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH; |
| 139 | *cmd |= addr & WSPI_CMD_BYTE_ADDR; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 140 | |
| 141 | spi_message_init(&m); |
| 142 | memset(t, 0, sizeof(t)); |
| 143 | |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 144 | t[0].tx_buf = cmd; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 145 | t[0].len = 4; |
| 146 | spi_message_add_tail(&t[0], &m); |
| 147 | |
| 148 | /* Busy and non busy words read */ |
| 149 | t[1].rx_buf = busy_buf; |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 150 | t[1].len = WL1251_BUSY_WORD_LEN; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 151 | spi_message_add_tail(&t[1], &m); |
| 152 | |
| 153 | t[2].rx_buf = buf; |
| 154 | t[2].len = len; |
| 155 | spi_message_add_tail(&t[2], &m); |
| 156 | |
Bob Copeland | af8c78e | 2009-08-07 13:33:34 +0300 | [diff] [blame^] | 157 | spi_sync(wl_to_spi(wl), &m); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 158 | |
| 159 | /* FIXME: check busy words */ |
| 160 | |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 161 | wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd)); |
| 162 | wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 163 | } |
| 164 | |
Bob Copeland | 08d9f572 | 2009-08-07 13:33:11 +0300 | [diff] [blame] | 165 | static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf, |
| 166 | size_t len) |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 167 | { |
| 168 | struct spi_transfer t[2]; |
| 169 | struct spi_message m; |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 170 | u32 *cmd; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 171 | |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 172 | cmd = &wl->buffer_cmd; |
| 173 | |
| 174 | *cmd = 0; |
| 175 | *cmd |= WSPI_CMD_WRITE; |
| 176 | *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH; |
| 177 | *cmd |= addr & WSPI_CMD_BYTE_ADDR; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 178 | |
| 179 | spi_message_init(&m); |
| 180 | memset(t, 0, sizeof(t)); |
| 181 | |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 182 | t[0].tx_buf = cmd; |
| 183 | t[0].len = sizeof(*cmd); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 184 | spi_message_add_tail(&t[0], &m); |
| 185 | |
| 186 | t[1].tx_buf = buf; |
| 187 | t[1].len = len; |
| 188 | spi_message_add_tail(&t[1], &m); |
| 189 | |
Bob Copeland | af8c78e | 2009-08-07 13:33:34 +0300 | [diff] [blame^] | 190 | spi_sync(wl_to_spi(wl), &m); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 191 | |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 192 | wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd)); |
| 193 | wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 194 | } |
Bob Copeland | 08d9f572 | 2009-08-07 13:33:11 +0300 | [diff] [blame] | 195 | |
Bob Copeland | af8c78e | 2009-08-07 13:33:34 +0300 | [diff] [blame^] | 196 | static const struct wl1251_if_operations wl1251_spi_ops = { |
Bob Copeland | 08d9f572 | 2009-08-07 13:33:11 +0300 | [diff] [blame] | 197 | .read = wl1251_spi_read, |
| 198 | .write = wl1251_spi_write, |
| 199 | .reset = wl1251_spi_reset_wake, |
| 200 | }; |
Bob Copeland | 8e639c0 | 2009-08-07 13:33:26 +0300 | [diff] [blame] | 201 | |
| 202 | static int __devinit wl1251_spi_probe(struct spi_device *spi) |
| 203 | { |
| 204 | struct wl12xx_platform_data *pdata; |
| 205 | struct ieee80211_hw *hw; |
| 206 | struct wl1251 *wl; |
| 207 | int ret; |
| 208 | |
| 209 | pdata = spi->dev.platform_data; |
| 210 | if (!pdata) { |
| 211 | wl1251_error("no platform data"); |
| 212 | return -ENODEV; |
| 213 | } |
| 214 | |
| 215 | hw = wl1251_alloc_hw(); |
| 216 | if (IS_ERR(hw)) |
| 217 | return PTR_ERR(hw); |
| 218 | |
| 219 | wl = hw->priv; |
| 220 | |
| 221 | SET_IEEE80211_DEV(hw, &spi->dev); |
| 222 | dev_set_drvdata(&spi->dev, wl); |
Bob Copeland | af8c78e | 2009-08-07 13:33:34 +0300 | [diff] [blame^] | 223 | wl->if_priv = spi; |
Bob Copeland | 8e639c0 | 2009-08-07 13:33:26 +0300 | [diff] [blame] | 224 | wl->if_ops = &wl1251_spi_ops; |
| 225 | |
| 226 | /* This is the only SPI value that we need to set here, the rest |
| 227 | * comes from the board-peripherals file */ |
| 228 | spi->bits_per_word = 32; |
| 229 | |
| 230 | ret = spi_setup(spi); |
| 231 | if (ret < 0) { |
| 232 | wl1251_error("spi_setup failed"); |
| 233 | goto out_free; |
| 234 | } |
| 235 | |
| 236 | wl->set_power = pdata->set_power; |
| 237 | if (!wl->set_power) { |
| 238 | wl1251_error("set power function missing in platform data"); |
| 239 | return -ENODEV; |
| 240 | } |
| 241 | |
| 242 | wl->irq = spi->irq; |
| 243 | if (wl->irq < 0) { |
| 244 | wl1251_error("irq missing in platform data"); |
| 245 | return -ENODEV; |
| 246 | } |
| 247 | |
| 248 | ret = request_irq(wl->irq, wl1251_irq, 0, DRIVER_NAME, wl); |
| 249 | if (ret < 0) { |
| 250 | wl1251_error("request_irq() failed: %d", ret); |
| 251 | goto out_free; |
| 252 | } |
| 253 | |
| 254 | set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING); |
| 255 | |
| 256 | disable_irq(wl->irq); |
| 257 | |
| 258 | ret = wl1251_init_ieee80211(wl); |
| 259 | if (ret) |
| 260 | goto out_irq; |
| 261 | |
| 262 | return 0; |
| 263 | |
| 264 | out_irq: |
| 265 | free_irq(wl->irq, wl); |
| 266 | |
| 267 | out_free: |
| 268 | ieee80211_free_hw(hw); |
| 269 | |
| 270 | return ret; |
| 271 | } |
| 272 | |
| 273 | static int __devexit wl1251_spi_remove(struct spi_device *spi) |
| 274 | { |
| 275 | struct wl1251 *wl = dev_get_drvdata(&spi->dev); |
| 276 | |
| 277 | wl1251_free_hw(wl); |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static struct spi_driver wl1251_spi_driver = { |
| 283 | .driver = { |
| 284 | .name = "wl12xx", |
| 285 | .bus = &spi_bus_type, |
| 286 | .owner = THIS_MODULE, |
| 287 | }, |
| 288 | |
| 289 | .probe = wl1251_spi_probe, |
| 290 | .remove = __devexit_p(wl1251_spi_remove), |
| 291 | }; |
| 292 | |
| 293 | static int __init wl1251_spi_init(void) |
| 294 | { |
| 295 | int ret; |
| 296 | |
| 297 | ret = spi_register_driver(&wl1251_spi_driver); |
| 298 | if (ret < 0) { |
| 299 | wl1251_error("failed to register spi driver: %d", ret); |
| 300 | goto out; |
| 301 | } |
| 302 | |
| 303 | out: |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | static void __exit wl1251_spi_exit(void) |
| 308 | { |
| 309 | spi_unregister_driver(&wl1251_spi_driver); |
| 310 | |
| 311 | wl1251_notice("unloaded"); |
| 312 | } |
| 313 | |
| 314 | module_init(wl1251_spi_init); |
| 315 | module_exit(wl1251_spi_exit); |
| 316 | |
| 317 | MODULE_LICENSE("GPL"); |
| 318 | MODULE_AUTHOR("Kalle Valo <Kalle.Valo@nokia.com>"); |
| 319 | MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>"); |