Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/net/wireless/libertas/if_spi.c |
| 3 | * |
| 4 | * Driver for Marvell SPI WLAN cards. |
| 5 | * |
| 6 | * Copyright 2008 Analog Devices Inc. |
| 7 | * |
| 8 | * Authors: |
| 9 | * Andrey Yurovsky <andrey@cozybit.com> |
| 10 | * Colin McCabe <colin@cozybit.com> |
| 11 | * |
| 12 | * Inspired by if_sdio.c, Copyright 2007-2008 Pierre Ossman |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | */ |
| 19 | |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame] | 20 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 21 | |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 22 | #include <linux/moduleparam.h> |
| 23 | #include <linux/firmware.h> |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 24 | #include <linux/jiffies.h> |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 25 | #include <linux/list.h> |
| 26 | #include <linux/netdevice.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 28 | #include <linux/spi/libertas_spi.h> |
| 29 | #include <linux/spi/spi.h> |
| 30 | |
| 31 | #include "host.h" |
| 32 | #include "decl.h" |
| 33 | #include "defs.h" |
| 34 | #include "dev.h" |
| 35 | #include "if_spi.h" |
| 36 | |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 37 | struct if_spi_packet { |
| 38 | struct list_head list; |
| 39 | u16 blen; |
| 40 | u8 buffer[0] __attribute__((aligned(4))); |
| 41 | }; |
| 42 | |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 43 | struct if_spi_card { |
| 44 | struct spi_device *spi; |
| 45 | struct lbs_private *priv; |
Mike Rapoport | 0c2bec9 | 2009-02-03 09:04:20 +0200 | [diff] [blame] | 46 | struct libertas_spi_platform_data *pdata; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 47 | |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 48 | /* The card ID and card revision, as reported by the hardware. */ |
| 49 | u16 card_id; |
| 50 | u8 card_rev; |
| 51 | |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 52 | /* The last time that we initiated an SPU operation */ |
| 53 | unsigned long prev_xfer_time; |
| 54 | |
| 55 | int use_dummy_writes; |
| 56 | unsigned long spu_port_delay; |
| 57 | unsigned long spu_reg_delay; |
| 58 | |
| 59 | /* Handles all SPI communication (except for FW load) */ |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 60 | struct workqueue_struct *workqueue; |
| 61 | struct work_struct packet_work; |
Vasily Khoruzhick | f39de99 | 2011-03-16 16:41:46 +0200 | [diff] [blame] | 62 | struct work_struct resume_work; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 63 | |
| 64 | u8 cmd_buffer[IF_SPI_CMD_BUF_SIZE]; |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 65 | |
| 66 | /* A buffer of incoming packets from libertas core. |
| 67 | * Since we can't sleep in hw_host_to_card, we have to buffer |
| 68 | * them. */ |
| 69 | struct list_head cmd_packet_list; |
| 70 | struct list_head data_packet_list; |
| 71 | |
| 72 | /* Protects cmd_packet_list and data_packet_list */ |
| 73 | spinlock_t buffer_lock; |
Vasily Khoruzhick | f39de99 | 2011-03-16 16:41:46 +0200 | [diff] [blame] | 74 | |
| 75 | /* True is card suspended */ |
| 76 | u8 suspended; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | static void free_if_spi_card(struct if_spi_card *card) |
| 80 | { |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 81 | struct list_head *cursor, *next; |
| 82 | struct if_spi_packet *packet; |
| 83 | |
| 84 | list_for_each_safe(cursor, next, &card->cmd_packet_list) { |
| 85 | packet = container_of(cursor, struct if_spi_packet, list); |
| 86 | list_del(&packet->list); |
| 87 | kfree(packet); |
| 88 | } |
| 89 | list_for_each_safe(cursor, next, &card->data_packet_list) { |
| 90 | packet = container_of(cursor, struct if_spi_packet, list); |
| 91 | list_del(&packet->list); |
| 92 | kfree(packet); |
| 93 | } |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 94 | spi_set_drvdata(card->spi, NULL); |
| 95 | kfree(card); |
| 96 | } |
| 97 | |
Dan Williams | 915a824 | 2010-08-07 21:16:30 -0500 | [diff] [blame] | 98 | #define MODEL_8385 0x04 |
| 99 | #define MODEL_8686 0x0b |
| 100 | #define MODEL_8688 0x10 |
| 101 | |
| 102 | static const struct lbs_fw_table fw_table[] = { |
| 103 | { MODEL_8385, "libertas/gspi8385_helper.bin", "libertas/gspi8385.bin" }, |
| 104 | { MODEL_8385, "libertas/gspi8385_hlp.bin", "libertas/gspi8385.bin" }, |
| 105 | { MODEL_8686, "libertas/gspi8686_v9_helper.bin", "libertas/gspi8686_v9.bin" }, |
| 106 | { MODEL_8686, "libertas/gspi8686_hlp.bin", "libertas/gspi8686.bin" }, |
| 107 | { MODEL_8688, "libertas/gspi8688_helper.bin", "libertas/gspi8688.bin" }, |
| 108 | { 0, NULL, NULL } |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 109 | }; |
Dan Williams | 915a824 | 2010-08-07 21:16:30 -0500 | [diff] [blame] | 110 | MODULE_FIRMWARE("libertas/gspi8385_helper.bin"); |
| 111 | MODULE_FIRMWARE("libertas/gspi8385_hlp.bin"); |
| 112 | MODULE_FIRMWARE("libertas/gspi8385.bin"); |
| 113 | MODULE_FIRMWARE("libertas/gspi8686_v9_helper.bin"); |
| 114 | MODULE_FIRMWARE("libertas/gspi8686_v9.bin"); |
| 115 | MODULE_FIRMWARE("libertas/gspi8686_hlp.bin"); |
| 116 | MODULE_FIRMWARE("libertas/gspi8686.bin"); |
| 117 | MODULE_FIRMWARE("libertas/gspi8688_helper.bin"); |
| 118 | MODULE_FIRMWARE("libertas/gspi8688.bin"); |
| 119 | |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * SPI Interface Unit Routines |
| 123 | * |
| 124 | * The SPU sits between the host and the WLAN module. |
| 125 | * All communication with the firmware is through SPU transactions. |
| 126 | * |
| 127 | * First we have to put a SPU register name on the bus. Then we can |
| 128 | * either read from or write to that register. |
| 129 | * |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 130 | */ |
| 131 | |
| 132 | static void spu_transaction_init(struct if_spi_card *card) |
| 133 | { |
| 134 | if (!time_after(jiffies, card->prev_xfer_time + 1)) { |
| 135 | /* Unfortunately, the SPU requires a delay between successive |
| 136 | * transactions. If our last transaction was more than a jiffy |
| 137 | * ago, we have obviously already delayed enough. |
| 138 | * If not, we have to busy-wait to be on the safe side. */ |
| 139 | ndelay(400); |
| 140 | } |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static void spu_transaction_finish(struct if_spi_card *card) |
| 144 | { |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 145 | card->prev_xfer_time = jiffies; |
| 146 | } |
| 147 | |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 148 | /* |
| 149 | * Write out a byte buffer to an SPI register, |
| 150 | * using a series of 16-bit transfers. |
| 151 | */ |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 152 | static int spu_write(struct if_spi_card *card, u16 reg, const u8 *buf, int len) |
| 153 | { |
| 154 | int err = 0; |
Holger Schurig | d18ba45 | 2009-10-22 15:30:44 +0200 | [diff] [blame] | 155 | __le16 reg_out = cpu_to_le16(reg | IF_SPI_WRITE_OPERATION_MASK); |
Sebastian Andrzej Siewior | 4d1d498 | 2009-06-04 21:57:03 +0200 | [diff] [blame] | 156 | struct spi_message m; |
| 157 | struct spi_transfer reg_trans; |
| 158 | struct spi_transfer data_trans; |
| 159 | |
| 160 | spi_message_init(&m); |
| 161 | memset(®_trans, 0, sizeof(reg_trans)); |
| 162 | memset(&data_trans, 0, sizeof(data_trans)); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 163 | |
| 164 | /* You must give an even number of bytes to the SPU, even if it |
| 165 | * doesn't care about the last one. */ |
| 166 | BUG_ON(len & 0x1); |
| 167 | |
| 168 | spu_transaction_init(card); |
| 169 | |
| 170 | /* write SPU register index */ |
Sebastian Andrzej Siewior | 4d1d498 | 2009-06-04 21:57:03 +0200 | [diff] [blame] | 171 | reg_trans.tx_buf = ®_out; |
| 172 | reg_trans.len = sizeof(reg_out); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 173 | |
Sebastian Andrzej Siewior | 4d1d498 | 2009-06-04 21:57:03 +0200 | [diff] [blame] | 174 | data_trans.tx_buf = buf; |
| 175 | data_trans.len = len; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 176 | |
Sebastian Andrzej Siewior | 4d1d498 | 2009-06-04 21:57:03 +0200 | [diff] [blame] | 177 | spi_message_add_tail(®_trans, &m); |
| 178 | spi_message_add_tail(&data_trans, &m); |
| 179 | |
| 180 | err = spi_sync(card->spi, &m); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 181 | spu_transaction_finish(card); |
| 182 | return err; |
| 183 | } |
| 184 | |
| 185 | static inline int spu_write_u16(struct if_spi_card *card, u16 reg, u16 val) |
| 186 | { |
Holger Schurig | d18ba45 | 2009-10-22 15:30:44 +0200 | [diff] [blame] | 187 | __le16 buff; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 188 | |
Sebastian Andrzej Siewior | f488b72 | 2009-05-22 21:19:40 +0200 | [diff] [blame] | 189 | buff = cpu_to_le16(val); |
| 190 | return spu_write(card, reg, (u8 *)&buff, sizeof(u16)); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static inline int spu_reg_is_port_reg(u16 reg) |
| 194 | { |
| 195 | switch (reg) { |
| 196 | case IF_SPI_IO_RDWRPORT_REG: |
| 197 | case IF_SPI_CMD_RDWRPORT_REG: |
| 198 | case IF_SPI_DATA_RDWRPORT_REG: |
| 199 | return 1; |
| 200 | default: |
| 201 | return 0; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | static int spu_read(struct if_spi_card *card, u16 reg, u8 *buf, int len) |
| 206 | { |
Sebastian Andrzej Siewior | 4d1d498 | 2009-06-04 21:57:03 +0200 | [diff] [blame] | 207 | unsigned int delay; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 208 | int err = 0; |
Holger Schurig | d18ba45 | 2009-10-22 15:30:44 +0200 | [diff] [blame] | 209 | __le16 reg_out = cpu_to_le16(reg | IF_SPI_READ_OPERATION_MASK); |
Sebastian Andrzej Siewior | 4d1d498 | 2009-06-04 21:57:03 +0200 | [diff] [blame] | 210 | struct spi_message m; |
| 211 | struct spi_transfer reg_trans; |
| 212 | struct spi_transfer dummy_trans; |
| 213 | struct spi_transfer data_trans; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 214 | |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 215 | /* |
| 216 | * You must take an even number of bytes from the SPU, even if you |
| 217 | * don't care about the last one. |
| 218 | */ |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 219 | BUG_ON(len & 0x1); |
| 220 | |
| 221 | spu_transaction_init(card); |
| 222 | |
Sebastian Andrzej Siewior | 4d1d498 | 2009-06-04 21:57:03 +0200 | [diff] [blame] | 223 | spi_message_init(&m); |
| 224 | memset(®_trans, 0, sizeof(reg_trans)); |
| 225 | memset(&dummy_trans, 0, sizeof(dummy_trans)); |
| 226 | memset(&data_trans, 0, sizeof(data_trans)); |
| 227 | |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 228 | /* write SPU register index */ |
Sebastian Andrzej Siewior | 4d1d498 | 2009-06-04 21:57:03 +0200 | [diff] [blame] | 229 | reg_trans.tx_buf = ®_out; |
| 230 | reg_trans.len = sizeof(reg_out); |
| 231 | spi_message_add_tail(®_trans, &m); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 232 | |
| 233 | delay = spu_reg_is_port_reg(reg) ? card->spu_port_delay : |
| 234 | card->spu_reg_delay; |
| 235 | if (card->use_dummy_writes) { |
| 236 | /* Clock in dummy cycles while the SPU fills the FIFO */ |
Sebastian Andrzej Siewior | 4d1d498 | 2009-06-04 21:57:03 +0200 | [diff] [blame] | 237 | dummy_trans.len = delay / 8; |
| 238 | spi_message_add_tail(&dummy_trans, &m); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 239 | } else { |
| 240 | /* Busy-wait while the SPU fills the FIFO */ |
Sebastian Andrzej Siewior | 4d1d498 | 2009-06-04 21:57:03 +0200 | [diff] [blame] | 241 | reg_trans.delay_usecs = |
| 242 | DIV_ROUND_UP((100 + (delay * 10)), 1000); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /* read in data */ |
Sebastian Andrzej Siewior | 4d1d498 | 2009-06-04 21:57:03 +0200 | [diff] [blame] | 246 | data_trans.rx_buf = buf; |
| 247 | data_trans.len = len; |
| 248 | spi_message_add_tail(&data_trans, &m); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 249 | |
Sebastian Andrzej Siewior | 4d1d498 | 2009-06-04 21:57:03 +0200 | [diff] [blame] | 250 | err = spi_sync(card->spi, &m); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 251 | spu_transaction_finish(card); |
| 252 | return err; |
| 253 | } |
| 254 | |
| 255 | /* Read 16 bits from an SPI register */ |
| 256 | static inline int spu_read_u16(struct if_spi_card *card, u16 reg, u16 *val) |
| 257 | { |
Holger Schurig | d18ba45 | 2009-10-22 15:30:44 +0200 | [diff] [blame] | 258 | __le16 buf; |
Sebastian Andrzej Siewior | f488b72 | 2009-05-22 21:19:40 +0200 | [diff] [blame] | 259 | int ret; |
| 260 | |
| 261 | ret = spu_read(card, reg, (u8 *)&buf, sizeof(buf)); |
| 262 | if (ret == 0) |
| 263 | *val = le16_to_cpup(&buf); |
| 264 | return ret; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 267 | /* |
| 268 | * Read 32 bits from an SPI register. |
| 269 | * The low 16 bits are read first. |
| 270 | */ |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 271 | static int spu_read_u32(struct if_spi_card *card, u16 reg, u32 *val) |
| 272 | { |
Holger Schurig | d18ba45 | 2009-10-22 15:30:44 +0200 | [diff] [blame] | 273 | __le32 buf; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 274 | int err; |
Sebastian Andrzej Siewior | f488b72 | 2009-05-22 21:19:40 +0200 | [diff] [blame] | 275 | |
| 276 | err = spu_read(card, reg, (u8 *)&buf, sizeof(buf)); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 277 | if (!err) |
Sebastian Andrzej Siewior | f488b72 | 2009-05-22 21:19:40 +0200 | [diff] [blame] | 278 | *val = le32_to_cpup(&buf); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 279 | return err; |
| 280 | } |
| 281 | |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 282 | /* |
| 283 | * Keep reading 16 bits from an SPI register until you get the correct result. |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 284 | * |
| 285 | * If mask = 0, the correct result is any non-zero number. |
| 286 | * If mask != 0, the correct result is any number where |
| 287 | * number & target_mask == target |
| 288 | * |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 289 | * Returns -ETIMEDOUT if a second passes without the correct result. |
| 290 | */ |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 291 | static int spu_wait_for_u16(struct if_spi_card *card, u16 reg, |
| 292 | u16 target_mask, u16 target) |
| 293 | { |
| 294 | int err; |
| 295 | unsigned long timeout = jiffies + 5*HZ; |
| 296 | while (1) { |
| 297 | u16 val; |
| 298 | err = spu_read_u16(card, reg, &val); |
| 299 | if (err) |
| 300 | return err; |
| 301 | if (target_mask) { |
| 302 | if ((val & target_mask) == target) |
| 303 | return 0; |
| 304 | } else { |
| 305 | if (val) |
| 306 | return 0; |
| 307 | } |
| 308 | udelay(100); |
| 309 | if (time_after(jiffies, timeout)) { |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame] | 310 | pr_err("%s: timeout with val=%02x, target_mask=%02x, target=%02x\n", |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 311 | __func__, val, target_mask, target); |
| 312 | return -ETIMEDOUT; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 317 | /* |
| 318 | * Read 16 bits from an SPI register until you receive a specific value. |
| 319 | * Returns -ETIMEDOUT if a 4 tries pass without success. |
| 320 | */ |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 321 | static int spu_wait_for_u32(struct if_spi_card *card, u32 reg, u32 target) |
| 322 | { |
| 323 | int err, try; |
| 324 | for (try = 0; try < 4; ++try) { |
| 325 | u32 val = 0; |
| 326 | err = spu_read_u32(card, reg, &val); |
| 327 | if (err) |
| 328 | return err; |
| 329 | if (val == target) |
| 330 | return 0; |
| 331 | mdelay(100); |
| 332 | } |
| 333 | return -ETIMEDOUT; |
| 334 | } |
| 335 | |
| 336 | static int spu_set_interrupt_mode(struct if_spi_card *card, |
| 337 | int suppress_host_int, |
| 338 | int auto_int) |
| 339 | { |
| 340 | int err = 0; |
| 341 | |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 342 | /* |
| 343 | * We can suppress a host interrupt by clearing the appropriate |
| 344 | * bit in the "host interrupt status mask" register |
| 345 | */ |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 346 | if (suppress_host_int) { |
| 347 | err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0); |
| 348 | if (err) |
| 349 | return err; |
| 350 | } else { |
| 351 | err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, |
| 352 | IF_SPI_HISM_TX_DOWNLOAD_RDY | |
| 353 | IF_SPI_HISM_RX_UPLOAD_RDY | |
| 354 | IF_SPI_HISM_CMD_DOWNLOAD_RDY | |
| 355 | IF_SPI_HISM_CARDEVENT | |
| 356 | IF_SPI_HISM_CMD_UPLOAD_RDY); |
| 357 | if (err) |
| 358 | return err; |
| 359 | } |
| 360 | |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 361 | /* |
| 362 | * If auto-interrupts are on, the completion of certain transactions |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 363 | * will trigger an interrupt automatically. If auto-interrupts |
| 364 | * are off, we need to set the "Card Interrupt Cause" register to |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 365 | * trigger a card interrupt. |
| 366 | */ |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 367 | if (auto_int) { |
| 368 | err = spu_write_u16(card, IF_SPI_HOST_INT_CTRL_REG, |
| 369 | IF_SPI_HICT_TX_DOWNLOAD_OVER_AUTO | |
| 370 | IF_SPI_HICT_RX_UPLOAD_OVER_AUTO | |
| 371 | IF_SPI_HICT_CMD_DOWNLOAD_OVER_AUTO | |
| 372 | IF_SPI_HICT_CMD_UPLOAD_OVER_AUTO); |
| 373 | if (err) |
| 374 | return err; |
| 375 | } else { |
| 376 | err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0); |
| 377 | if (err) |
| 378 | return err; |
| 379 | } |
| 380 | return err; |
| 381 | } |
| 382 | |
| 383 | static int spu_get_chip_revision(struct if_spi_card *card, |
| 384 | u16 *card_id, u8 *card_rev) |
| 385 | { |
| 386 | int err = 0; |
| 387 | u32 dev_ctrl; |
| 388 | err = spu_read_u32(card, IF_SPI_DEVICEID_CTRL_REG, &dev_ctrl); |
| 389 | if (err) |
| 390 | return err; |
| 391 | *card_id = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_ID(dev_ctrl); |
| 392 | *card_rev = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_REV(dev_ctrl); |
| 393 | return err; |
| 394 | } |
| 395 | |
| 396 | static int spu_set_bus_mode(struct if_spi_card *card, u16 mode) |
| 397 | { |
| 398 | int err = 0; |
| 399 | u16 rval; |
| 400 | /* set bus mode */ |
| 401 | err = spu_write_u16(card, IF_SPI_SPU_BUS_MODE_REG, mode); |
| 402 | if (err) |
| 403 | return err; |
| 404 | /* Check that we were able to read back what we just wrote. */ |
| 405 | err = spu_read_u16(card, IF_SPI_SPU_BUS_MODE_REG, &rval); |
| 406 | if (err) |
| 407 | return err; |
Andrey Yurovsky | 88d8952 | 2009-07-31 11:35:19 -0700 | [diff] [blame] | 408 | if ((rval & 0xF) != mode) { |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame] | 409 | pr_err("Can't read bus mode register\n"); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 410 | return -EIO; |
| 411 | } |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | static int spu_init(struct if_spi_card *card, int use_dummy_writes) |
| 416 | { |
| 417 | int err = 0; |
| 418 | u32 delay; |
| 419 | |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 420 | /* |
| 421 | * We have to start up in timed delay mode so that we can safely |
| 422 | * read the Delay Read Register. |
| 423 | */ |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 424 | card->use_dummy_writes = 0; |
| 425 | err = spu_set_bus_mode(card, |
| 426 | IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING | |
| 427 | IF_SPI_BUS_MODE_DELAY_METHOD_TIMED | |
| 428 | IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA); |
| 429 | if (err) |
| 430 | return err; |
| 431 | card->spu_port_delay = 1000; |
| 432 | card->spu_reg_delay = 1000; |
| 433 | err = spu_read_u32(card, IF_SPI_DELAY_READ_REG, &delay); |
| 434 | if (err) |
| 435 | return err; |
| 436 | card->spu_port_delay = delay & 0x0000ffff; |
| 437 | card->spu_reg_delay = (delay & 0xffff0000) >> 16; |
| 438 | |
| 439 | /* If dummy clock delay mode has been requested, switch to it now */ |
| 440 | if (use_dummy_writes) { |
| 441 | card->use_dummy_writes = 1; |
| 442 | err = spu_set_bus_mode(card, |
| 443 | IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING | |
| 444 | IF_SPI_BUS_MODE_DELAY_METHOD_DUMMY_CLOCK | |
| 445 | IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA); |
| 446 | if (err) |
| 447 | return err; |
| 448 | } |
| 449 | |
| 450 | lbs_deb_spi("Initialized SPU unit. " |
| 451 | "spu_port_delay=0x%04lx, spu_reg_delay=0x%04lx\n", |
| 452 | card->spu_port_delay, card->spu_reg_delay); |
| 453 | return err; |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * Firmware Loading |
| 458 | */ |
| 459 | |
Dan Williams | 915a824 | 2010-08-07 21:16:30 -0500 | [diff] [blame] | 460 | static int if_spi_prog_helper_firmware(struct if_spi_card *card, |
| 461 | const struct firmware *firmware) |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 462 | { |
| 463 | int err = 0; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 464 | int bytes_remaining; |
| 465 | const u8 *fw; |
| 466 | u8 temp[HELPER_FW_LOAD_CHUNK_SZ]; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 467 | |
| 468 | lbs_deb_enter(LBS_DEB_SPI); |
| 469 | |
| 470 | err = spu_set_interrupt_mode(card, 1, 0); |
| 471 | if (err) |
| 472 | goto out; |
Dan Williams | 915a824 | 2010-08-07 21:16:30 -0500 | [diff] [blame] | 473 | |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 474 | bytes_remaining = firmware->size; |
| 475 | fw = firmware->data; |
| 476 | |
| 477 | /* Load helper firmware image */ |
| 478 | while (bytes_remaining > 0) { |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 479 | /* |
| 480 | * Scratch pad 1 should contain the number of bytes we |
| 481 | * want to download to the firmware |
| 482 | */ |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 483 | err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG, |
| 484 | HELPER_FW_LOAD_CHUNK_SZ); |
| 485 | if (err) |
Dan Williams | 915a824 | 2010-08-07 21:16:30 -0500 | [diff] [blame] | 486 | goto out; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 487 | |
| 488 | err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG, |
| 489 | IF_SPI_HIST_CMD_DOWNLOAD_RDY, |
| 490 | IF_SPI_HIST_CMD_DOWNLOAD_RDY); |
| 491 | if (err) |
Dan Williams | 915a824 | 2010-08-07 21:16:30 -0500 | [diff] [blame] | 492 | goto out; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 493 | |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 494 | /* |
| 495 | * Feed the data into the command read/write port reg |
| 496 | * in chunks of 64 bytes |
| 497 | */ |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 498 | memset(temp, 0, sizeof(temp)); |
| 499 | memcpy(temp, fw, |
| 500 | min(bytes_remaining, HELPER_FW_LOAD_CHUNK_SZ)); |
| 501 | mdelay(10); |
| 502 | err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG, |
| 503 | temp, HELPER_FW_LOAD_CHUNK_SZ); |
| 504 | if (err) |
Dan Williams | 915a824 | 2010-08-07 21:16:30 -0500 | [diff] [blame] | 505 | goto out; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 506 | |
| 507 | /* Interrupt the boot code */ |
| 508 | err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0); |
| 509 | if (err) |
Dan Williams | 915a824 | 2010-08-07 21:16:30 -0500 | [diff] [blame] | 510 | goto out; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 511 | err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, |
| 512 | IF_SPI_CIC_CMD_DOWNLOAD_OVER); |
| 513 | if (err) |
Dan Williams | 915a824 | 2010-08-07 21:16:30 -0500 | [diff] [blame] | 514 | goto out; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 515 | bytes_remaining -= HELPER_FW_LOAD_CHUNK_SZ; |
| 516 | fw += HELPER_FW_LOAD_CHUNK_SZ; |
| 517 | } |
| 518 | |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 519 | /* |
| 520 | * Once the helper / single stage firmware download is complete, |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 521 | * write 0 to scratch pad 1 and interrupt the |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 522 | * bootloader. This completes the helper download. |
| 523 | */ |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 524 | err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG, FIRMWARE_DNLD_OK); |
| 525 | if (err) |
Dan Williams | 915a824 | 2010-08-07 21:16:30 -0500 | [diff] [blame] | 526 | goto out; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 527 | err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0); |
| 528 | if (err) |
Dan Williams | 915a824 | 2010-08-07 21:16:30 -0500 | [diff] [blame] | 529 | goto out; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 530 | err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, |
| 531 | IF_SPI_CIC_CMD_DOWNLOAD_OVER); |
Dan Williams | 915a824 | 2010-08-07 21:16:30 -0500 | [diff] [blame] | 532 | goto out; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 533 | |
| 534 | lbs_deb_spi("waiting for helper to boot...\n"); |
| 535 | |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 536 | out: |
| 537 | if (err) |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame] | 538 | pr_err("failed to load helper firmware (err=%d)\n", err); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 539 | lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err); |
| 540 | return err; |
| 541 | } |
| 542 | |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 543 | /* |
| 544 | * Returns the length of the next packet the firmware expects us to send. |
| 545 | * Sets crc_err if the previous transfer had a CRC error. |
| 546 | */ |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 547 | static int if_spi_prog_main_firmware_check_len(struct if_spi_card *card, |
| 548 | int *crc_err) |
| 549 | { |
| 550 | u16 len; |
| 551 | int err = 0; |
| 552 | |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 553 | /* |
| 554 | * wait until the host interrupt status register indicates |
| 555 | * that we are ready to download |
| 556 | */ |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 557 | err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG, |
| 558 | IF_SPI_HIST_CMD_DOWNLOAD_RDY, |
| 559 | IF_SPI_HIST_CMD_DOWNLOAD_RDY); |
| 560 | if (err) { |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame] | 561 | pr_err("timed out waiting for host_int_status\n"); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 562 | return err; |
| 563 | } |
| 564 | |
| 565 | /* Ask the device how many bytes of firmware it wants. */ |
| 566 | err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len); |
| 567 | if (err) |
| 568 | return err; |
| 569 | |
| 570 | if (len > IF_SPI_CMD_BUF_SIZE) { |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame] | 571 | pr_err("firmware load device requested a larger transfer than we are prepared to handle (len = %d)\n", |
| 572 | len); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 573 | return -EIO; |
| 574 | } |
| 575 | if (len & 0x1) { |
| 576 | lbs_deb_spi("%s: crc error\n", __func__); |
| 577 | len &= ~0x1; |
| 578 | *crc_err = 1; |
| 579 | } else |
| 580 | *crc_err = 0; |
| 581 | |
| 582 | return len; |
| 583 | } |
| 584 | |
Dan Williams | 915a824 | 2010-08-07 21:16:30 -0500 | [diff] [blame] | 585 | static int if_spi_prog_main_firmware(struct if_spi_card *card, |
| 586 | const struct firmware *firmware) |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 587 | { |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 588 | struct lbs_private *priv = card->priv; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 589 | int len, prev_len; |
| 590 | int bytes, crc_err = 0, err = 0; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 591 | const u8 *fw; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 592 | u16 num_crc_errs; |
| 593 | |
| 594 | lbs_deb_enter(LBS_DEB_SPI); |
| 595 | |
| 596 | err = spu_set_interrupt_mode(card, 1, 0); |
| 597 | if (err) |
| 598 | goto out; |
| 599 | |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 600 | err = spu_wait_for_u16(card, IF_SPI_SCRATCH_1_REG, 0, 0); |
| 601 | if (err) { |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 602 | netdev_err(priv->dev, |
| 603 | "%s: timed out waiting for initial scratch reg = 0\n", |
| 604 | __func__); |
Dan Williams | 915a824 | 2010-08-07 21:16:30 -0500 | [diff] [blame] | 605 | goto out; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | num_crc_errs = 0; |
| 609 | prev_len = 0; |
| 610 | bytes = firmware->size; |
| 611 | fw = firmware->data; |
| 612 | while ((len = if_spi_prog_main_firmware_check_len(card, &crc_err))) { |
| 613 | if (len < 0) { |
| 614 | err = len; |
Dan Williams | 915a824 | 2010-08-07 21:16:30 -0500 | [diff] [blame] | 615 | goto out; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 616 | } |
| 617 | if (bytes < 0) { |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 618 | /* |
| 619 | * If there are no more bytes left, we would normally |
| 620 | * expect to have terminated with len = 0 |
| 621 | */ |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 622 | netdev_err(priv->dev, |
| 623 | "Firmware load wants more bytes than we have to offer.\n"); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 624 | break; |
| 625 | } |
| 626 | if (crc_err) { |
| 627 | /* Previous transfer failed. */ |
| 628 | if (++num_crc_errs > MAX_MAIN_FW_LOAD_CRC_ERR) { |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame] | 629 | pr_err("Too many CRC errors encountered in firmware load.\n"); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 630 | err = -EIO; |
Dan Williams | 915a824 | 2010-08-07 21:16:30 -0500 | [diff] [blame] | 631 | goto out; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 632 | } |
| 633 | } else { |
| 634 | /* Previous transfer succeeded. Advance counters. */ |
| 635 | bytes -= prev_len; |
| 636 | fw += prev_len; |
| 637 | } |
| 638 | if (bytes < len) { |
| 639 | memset(card->cmd_buffer, 0, len); |
| 640 | memcpy(card->cmd_buffer, fw, bytes); |
| 641 | } else |
| 642 | memcpy(card->cmd_buffer, fw, len); |
| 643 | |
| 644 | err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0); |
| 645 | if (err) |
Dan Williams | 915a824 | 2010-08-07 21:16:30 -0500 | [diff] [blame] | 646 | goto out; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 647 | err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG, |
| 648 | card->cmd_buffer, len); |
| 649 | if (err) |
Dan Williams | 915a824 | 2010-08-07 21:16:30 -0500 | [diff] [blame] | 650 | goto out; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 651 | err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG , |
| 652 | IF_SPI_CIC_CMD_DOWNLOAD_OVER); |
| 653 | if (err) |
Dan Williams | 915a824 | 2010-08-07 21:16:30 -0500 | [diff] [blame] | 654 | goto out; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 655 | prev_len = len; |
| 656 | } |
| 657 | if (bytes > prev_len) { |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame] | 658 | pr_err("firmware load wants fewer bytes than we have to offer\n"); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /* Confirm firmware download */ |
| 662 | err = spu_wait_for_u32(card, IF_SPI_SCRATCH_4_REG, |
| 663 | SUCCESSFUL_FW_DOWNLOAD_MAGIC); |
| 664 | if (err) { |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame] | 665 | pr_err("failed to confirm the firmware download\n"); |
Dan Williams | 915a824 | 2010-08-07 21:16:30 -0500 | [diff] [blame] | 666 | goto out; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 667 | } |
| 668 | |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 669 | out: |
| 670 | if (err) |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame] | 671 | pr_err("failed to load firmware (err=%d)\n", err); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 672 | lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err); |
| 673 | return err; |
| 674 | } |
| 675 | |
| 676 | /* |
| 677 | * SPI Transfer Thread |
| 678 | * |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 679 | * The SPI worker handles all SPI transfers, so there is no need for a lock. |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 680 | */ |
| 681 | |
| 682 | /* Move a command from the card to the host */ |
| 683 | static int if_spi_c2h_cmd(struct if_spi_card *card) |
| 684 | { |
| 685 | struct lbs_private *priv = card->priv; |
| 686 | unsigned long flags; |
| 687 | int err = 0; |
| 688 | u16 len; |
| 689 | u8 i; |
| 690 | |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 691 | /* |
| 692 | * We need a buffer big enough to handle whatever people send to |
| 693 | * hw_host_to_card |
| 694 | */ |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 695 | BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_CMD_BUFFER_SIZE); |
| 696 | BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_UPLD_SIZE); |
| 697 | |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 698 | /* |
| 699 | * It's just annoying if the buffer size isn't a multiple of 4, because |
| 700 | * then we might have len < IF_SPI_CMD_BUF_SIZE but |
| 701 | * ALIGN(len, 4) > IF_SPI_CMD_BUF_SIZE |
| 702 | */ |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 703 | BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE % 4 != 0); |
| 704 | |
| 705 | lbs_deb_enter(LBS_DEB_SPI); |
| 706 | |
| 707 | /* How many bytes are there to read? */ |
| 708 | err = spu_read_u16(card, IF_SPI_SCRATCH_2_REG, &len); |
| 709 | if (err) |
| 710 | goto out; |
| 711 | if (!len) { |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 712 | netdev_err(priv->dev, "%s: error: card has no data for host\n", |
| 713 | __func__); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 714 | err = -EINVAL; |
| 715 | goto out; |
| 716 | } else if (len > IF_SPI_CMD_BUF_SIZE) { |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 717 | netdev_err(priv->dev, |
| 718 | "%s: error: response packet too large: %d bytes, but maximum is %d\n", |
| 719 | __func__, len, IF_SPI_CMD_BUF_SIZE); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 720 | err = -EINVAL; |
| 721 | goto out; |
| 722 | } |
| 723 | |
| 724 | /* Read the data from the WLAN module into our command buffer */ |
| 725 | err = spu_read(card, IF_SPI_CMD_RDWRPORT_REG, |
| 726 | card->cmd_buffer, ALIGN(len, 4)); |
| 727 | if (err) |
| 728 | goto out; |
| 729 | |
| 730 | spin_lock_irqsave(&priv->driver_lock, flags); |
| 731 | i = (priv->resp_idx == 0) ? 1 : 0; |
| 732 | BUG_ON(priv->resp_len[i]); |
| 733 | priv->resp_len[i] = len; |
| 734 | memcpy(priv->resp_buf[i], card->cmd_buffer, len); |
| 735 | lbs_notify_command_response(priv, i); |
| 736 | spin_unlock_irqrestore(&priv->driver_lock, flags); |
| 737 | |
| 738 | out: |
| 739 | if (err) |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 740 | netdev_err(priv->dev, "%s: err=%d\n", __func__, err); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 741 | lbs_deb_leave(LBS_DEB_SPI); |
| 742 | return err; |
| 743 | } |
| 744 | |
| 745 | /* Move data from the card to the host */ |
| 746 | static int if_spi_c2h_data(struct if_spi_card *card) |
| 747 | { |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 748 | struct lbs_private *priv = card->priv; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 749 | struct sk_buff *skb; |
| 750 | char *data; |
| 751 | u16 len; |
| 752 | int err = 0; |
| 753 | |
| 754 | lbs_deb_enter(LBS_DEB_SPI); |
| 755 | |
| 756 | /* How many bytes are there to read? */ |
| 757 | err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len); |
| 758 | if (err) |
| 759 | goto out; |
| 760 | if (!len) { |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 761 | netdev_err(priv->dev, "%s: error: card has no data for host\n", |
| 762 | __func__); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 763 | err = -EINVAL; |
| 764 | goto out; |
| 765 | } else if (len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) { |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 766 | netdev_err(priv->dev, |
| 767 | "%s: error: card has %d bytes of data, but our maximum skb size is %zu\n", |
| 768 | __func__, len, MRVDRV_ETH_RX_PACKET_BUFFER_SIZE); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 769 | err = -EINVAL; |
| 770 | goto out; |
| 771 | } |
| 772 | |
| 773 | /* TODO: should we allocate a smaller skb if we have less data? */ |
| 774 | skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE); |
| 775 | if (!skb) { |
| 776 | err = -ENOBUFS; |
| 777 | goto out; |
| 778 | } |
| 779 | skb_reserve(skb, IPFIELD_ALIGN_OFFSET); |
| 780 | data = skb_put(skb, len); |
| 781 | |
| 782 | /* Read the data from the WLAN module into our skb... */ |
| 783 | err = spu_read(card, IF_SPI_DATA_RDWRPORT_REG, data, ALIGN(len, 4)); |
| 784 | if (err) |
| 785 | goto free_skb; |
| 786 | |
| 787 | /* pass the SKB to libertas */ |
| 788 | err = lbs_process_rxed_packet(card->priv, skb); |
| 789 | if (err) |
| 790 | goto free_skb; |
| 791 | |
| 792 | /* success */ |
| 793 | goto out; |
| 794 | |
| 795 | free_skb: |
| 796 | dev_kfree_skb(skb); |
| 797 | out: |
| 798 | if (err) |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 799 | netdev_err(priv->dev, "%s: err=%d\n", __func__, err); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 800 | lbs_deb_leave(LBS_DEB_SPI); |
| 801 | return err; |
| 802 | } |
| 803 | |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 804 | /* Move data or a command from the host to the card. */ |
| 805 | static void if_spi_h2c(struct if_spi_card *card, |
| 806 | struct if_spi_packet *packet, int type) |
| 807 | { |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 808 | struct lbs_private *priv = card->priv; |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 809 | int err = 0; |
| 810 | u16 int_type, port_reg; |
| 811 | |
| 812 | switch (type) { |
| 813 | case MVMS_DAT: |
| 814 | int_type = IF_SPI_CIC_TX_DOWNLOAD_OVER; |
| 815 | port_reg = IF_SPI_DATA_RDWRPORT_REG; |
| 816 | break; |
| 817 | case MVMS_CMD: |
| 818 | int_type = IF_SPI_CIC_CMD_DOWNLOAD_OVER; |
| 819 | port_reg = IF_SPI_CMD_RDWRPORT_REG; |
| 820 | break; |
| 821 | default: |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 822 | netdev_err(priv->dev, "can't transfer buffer of type %d\n", |
| 823 | type); |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 824 | err = -EINVAL; |
| 825 | goto out; |
| 826 | } |
| 827 | |
| 828 | /* Write the data to the card */ |
| 829 | err = spu_write(card, port_reg, packet->buffer, packet->blen); |
| 830 | if (err) |
| 831 | goto out; |
| 832 | |
| 833 | out: |
| 834 | kfree(packet); |
| 835 | |
| 836 | if (err) |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 837 | netdev_err(priv->dev, "%s: error %d\n", __func__, err); |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 838 | } |
| 839 | |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 840 | /* Inform the host about a card event */ |
| 841 | static void if_spi_e2h(struct if_spi_card *card) |
| 842 | { |
| 843 | int err = 0; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 844 | u32 cause; |
| 845 | struct lbs_private *priv = card->priv; |
| 846 | |
| 847 | err = spu_read_u32(card, IF_SPI_SCRATCH_3_REG, &cause); |
| 848 | if (err) |
| 849 | goto out; |
| 850 | |
andrey@cozybit.com | ea2d063 | 2009-05-19 17:20:13 -0700 | [diff] [blame] | 851 | /* re-enable the card event interrupt */ |
| 852 | spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, |
| 853 | ~IF_SPI_HICU_CARD_EVENT); |
| 854 | |
| 855 | /* generate a card interrupt */ |
| 856 | spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, IF_SPI_CIC_HOST_EVENT); |
| 857 | |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 858 | lbs_queue_event(priv, cause & 0xff); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 859 | out: |
| 860 | if (err) |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 861 | netdev_err(priv->dev, "%s: error %d\n", __func__, err); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 862 | } |
| 863 | |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 864 | static void if_spi_host_to_card_worker(struct work_struct *work) |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 865 | { |
| 866 | int err; |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 867 | struct if_spi_card *card; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 868 | u16 hiStatus; |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 869 | unsigned long flags; |
| 870 | struct if_spi_packet *packet; |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 871 | struct lbs_private *priv; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 872 | |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 873 | card = container_of(work, struct if_spi_card, packet_work); |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 874 | priv = card->priv; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 875 | |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 876 | lbs_deb_enter(LBS_DEB_SPI); |
| 877 | |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 878 | /* |
| 879 | * Read the host interrupt status register to see what we |
| 880 | * can do. |
| 881 | */ |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 882 | err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG, |
| 883 | &hiStatus); |
| 884 | if (err) { |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 885 | netdev_err(priv->dev, "I/O error\n"); |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 886 | goto err; |
| 887 | } |
| 888 | |
| 889 | if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY) { |
| 890 | err = if_spi_c2h_cmd(card); |
| 891 | if (err) |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 892 | goto err; |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 893 | } |
| 894 | if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY) { |
| 895 | err = if_spi_c2h_data(card); |
| 896 | if (err) |
| 897 | goto err; |
| 898 | } |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 899 | |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 900 | /* |
| 901 | * workaround: in PS mode, the card does not set the Command |
| 902 | * Download Ready bit, but it sets TX Download Ready. |
| 903 | */ |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 904 | if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY || |
| 905 | (card->priv->psstate != PS_STATE_FULL_POWER && |
| 906 | (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY))) { |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 907 | /* |
| 908 | * This means two things. First of all, |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 909 | * if there was a previous command sent, the card has |
| 910 | * successfully received it. |
| 911 | * Secondly, it is now ready to download another |
| 912 | * command. |
| 913 | */ |
| 914 | lbs_host_to_card_done(card->priv); |
Andrey Yurovsky | b3781c7 | 2009-06-12 12:45:36 -0700 | [diff] [blame] | 915 | |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 916 | /* Do we have any command packets from the host to send? */ |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 917 | packet = NULL; |
| 918 | spin_lock_irqsave(&card->buffer_lock, flags); |
| 919 | if (!list_empty(&card->cmd_packet_list)) { |
| 920 | packet = (struct if_spi_packet *)(card-> |
| 921 | cmd_packet_list.next); |
| 922 | list_del(&packet->list); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 923 | } |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 924 | spin_unlock_irqrestore(&card->buffer_lock, flags); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 925 | |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 926 | if (packet) |
| 927 | if_spi_h2c(card, packet, MVMS_CMD); |
| 928 | } |
| 929 | if (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY) { |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 930 | /* Do we have any data packets from the host to send? */ |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 931 | packet = NULL; |
| 932 | spin_lock_irqsave(&card->buffer_lock, flags); |
| 933 | if (!list_empty(&card->data_packet_list)) { |
| 934 | packet = (struct if_spi_packet *)(card-> |
| 935 | data_packet_list.next); |
| 936 | list_del(&packet->list); |
| 937 | } |
| 938 | spin_unlock_irqrestore(&card->buffer_lock, flags); |
| 939 | |
| 940 | if (packet) |
| 941 | if_spi_h2c(card, packet, MVMS_DAT); |
| 942 | } |
| 943 | if (hiStatus & IF_SPI_HIST_CARD_EVENT) |
| 944 | if_spi_e2h(card); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 945 | |
| 946 | err: |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 947 | if (err) |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 948 | netdev_err(priv->dev, "%s: got error %d\n", __func__, err); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 949 | |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 950 | lbs_deb_leave(LBS_DEB_SPI); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | /* |
| 954 | * Host to Card |
| 955 | * |
| 956 | * Called from Libertas to transfer some data to the WLAN device |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 957 | * We can't sleep here. |
| 958 | */ |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 959 | static int if_spi_host_to_card(struct lbs_private *priv, |
| 960 | u8 type, u8 *buf, u16 nb) |
| 961 | { |
| 962 | int err = 0; |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 963 | unsigned long flags; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 964 | struct if_spi_card *card = priv->card; |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 965 | struct if_spi_packet *packet; |
| 966 | u16 blen; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 967 | |
| 968 | lbs_deb_enter_args(LBS_DEB_SPI, "type %d, bytes %d", type, nb); |
| 969 | |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 970 | if (nb == 0) { |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 971 | netdev_err(priv->dev, "%s: invalid size requested: %d\n", |
| 972 | __func__, nb); |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 973 | err = -EINVAL; |
| 974 | goto out; |
| 975 | } |
| 976 | blen = ALIGN(nb, 4); |
| 977 | packet = kzalloc(sizeof(struct if_spi_packet) + blen, GFP_ATOMIC); |
| 978 | if (!packet) { |
| 979 | err = -ENOMEM; |
| 980 | goto out; |
| 981 | } |
| 982 | packet->blen = blen; |
| 983 | memcpy(packet->buffer, buf, nb); |
| 984 | memset(packet->buffer + nb, 0, blen - nb); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 985 | |
| 986 | switch (type) { |
| 987 | case MVMS_CMD: |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 988 | priv->dnld_sent = DNLD_CMD_SENT; |
| 989 | spin_lock_irqsave(&card->buffer_lock, flags); |
| 990 | list_add_tail(&packet->list, &card->cmd_packet_list); |
| 991 | spin_unlock_irqrestore(&card->buffer_lock, flags); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 992 | break; |
| 993 | case MVMS_DAT: |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 994 | priv->dnld_sent = DNLD_DATA_SENT; |
| 995 | spin_lock_irqsave(&card->buffer_lock, flags); |
| 996 | list_add_tail(&packet->list, &card->data_packet_list); |
| 997 | spin_unlock_irqrestore(&card->buffer_lock, flags); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 998 | break; |
| 999 | default: |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 1000 | netdev_err(priv->dev, "can't transfer buffer of type %d\n", |
| 1001 | type); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1002 | err = -EINVAL; |
| 1003 | break; |
| 1004 | } |
| 1005 | |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 1006 | /* Queue spi xfer work */ |
| 1007 | queue_work(card->workqueue, &card->packet_work); |
| 1008 | out: |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1009 | lbs_deb_leave_args(LBS_DEB_SPI, "err=%d", err); |
| 1010 | return err; |
| 1011 | } |
| 1012 | |
| 1013 | /* |
| 1014 | * Host Interrupts |
| 1015 | * |
| 1016 | * Service incoming interrupts from the WLAN device. We can't sleep here, so |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 1017 | * don't try to talk on the SPI bus, just queue the SPI xfer work. |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1018 | */ |
| 1019 | static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id) |
| 1020 | { |
| 1021 | struct if_spi_card *card = dev_id; |
| 1022 | |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 1023 | queue_work(card->workqueue, &card->packet_work); |
| 1024 | |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1025 | return IRQ_HANDLED; |
| 1026 | } |
| 1027 | |
| 1028 | /* |
| 1029 | * SPI callbacks |
| 1030 | */ |
| 1031 | |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 1032 | static int if_spi_init_card(struct if_spi_card *card) |
| 1033 | { |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 1034 | struct lbs_private *priv = card->priv; |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 1035 | struct spi_device *spi = card->spi; |
| 1036 | int err, i; |
| 1037 | u32 scratch; |
| 1038 | const struct firmware *helper = NULL; |
| 1039 | const struct firmware *mainfw = NULL; |
| 1040 | |
| 1041 | lbs_deb_enter(LBS_DEB_SPI); |
| 1042 | |
| 1043 | err = spu_init(card, card->pdata->use_dummy_writes); |
| 1044 | if (err) |
| 1045 | goto out; |
| 1046 | err = spu_get_chip_revision(card, &card->card_id, &card->card_rev); |
| 1047 | if (err) |
| 1048 | goto out; |
| 1049 | |
| 1050 | err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch); |
| 1051 | if (err) |
| 1052 | goto out; |
| 1053 | if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC) |
| 1054 | lbs_deb_spi("Firmware is already loaded for " |
| 1055 | "Marvell WLAN 802.11 adapter\n"); |
| 1056 | else { |
| 1057 | /* Check if we support this card */ |
| 1058 | for (i = 0; i < ARRAY_SIZE(fw_table); i++) { |
| 1059 | if (card->card_id == fw_table[i].model) |
| 1060 | break; |
| 1061 | } |
| 1062 | if (i == ARRAY_SIZE(fw_table)) { |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 1063 | netdev_err(priv->dev, "Unsupported chip_id: 0x%02x\n", |
| 1064 | card->card_id); |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 1065 | err = -ENODEV; |
| 1066 | goto out; |
| 1067 | } |
| 1068 | |
| 1069 | err = lbs_get_firmware(&card->spi->dev, NULL, NULL, |
| 1070 | card->card_id, &fw_table[0], &helper, |
| 1071 | &mainfw); |
| 1072 | if (err) { |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 1073 | netdev_err(priv->dev, "failed to find firmware (%d)\n", |
| 1074 | err); |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 1075 | goto out; |
| 1076 | } |
| 1077 | |
| 1078 | lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter " |
| 1079 | "(chip_id = 0x%04x, chip_rev = 0x%02x) " |
| 1080 | "attached to SPI bus_num %d, chip_select %d. " |
| 1081 | "spi->max_speed_hz=%d\n", |
| 1082 | card->card_id, card->card_rev, |
| 1083 | spi->master->bus_num, spi->chip_select, |
| 1084 | spi->max_speed_hz); |
| 1085 | err = if_spi_prog_helper_firmware(card, helper); |
| 1086 | if (err) |
| 1087 | goto out; |
| 1088 | err = if_spi_prog_main_firmware(card, mainfw); |
| 1089 | if (err) |
| 1090 | goto out; |
| 1091 | lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n"); |
| 1092 | } |
| 1093 | |
| 1094 | err = spu_set_interrupt_mode(card, 0, 1); |
| 1095 | if (err) |
| 1096 | goto out; |
| 1097 | |
| 1098 | out: |
| 1099 | if (helper) |
| 1100 | release_firmware(helper); |
| 1101 | if (mainfw) |
| 1102 | release_firmware(mainfw); |
| 1103 | |
| 1104 | lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err); |
| 1105 | |
| 1106 | return err; |
| 1107 | } |
| 1108 | |
Vasily Khoruzhick | f39de99 | 2011-03-16 16:41:46 +0200 | [diff] [blame] | 1109 | static void if_spi_resume_worker(struct work_struct *work) |
| 1110 | { |
| 1111 | struct if_spi_card *card; |
| 1112 | |
| 1113 | card = container_of(work, struct if_spi_card, resume_work); |
| 1114 | |
| 1115 | if (card->suspended) { |
| 1116 | if (card->pdata->setup) |
| 1117 | card->pdata->setup(card->spi); |
| 1118 | |
| 1119 | /* Init card ... */ |
| 1120 | if_spi_init_card(card); |
| 1121 | |
| 1122 | enable_irq(card->spi->irq); |
| 1123 | |
| 1124 | /* And resume it ... */ |
| 1125 | lbs_resume(card->priv); |
| 1126 | |
| 1127 | card->suspended = 0; |
| 1128 | } |
| 1129 | } |
| 1130 | |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1131 | static int __devinit if_spi_probe(struct spi_device *spi) |
| 1132 | { |
| 1133 | struct if_spi_card *card; |
| 1134 | struct lbs_private *priv = NULL; |
| 1135 | struct libertas_spi_platform_data *pdata = spi->dev.platform_data; |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 1136 | int err = 0; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1137 | |
| 1138 | lbs_deb_enter(LBS_DEB_SPI); |
| 1139 | |
Mike Rapoport | 0c2bec9 | 2009-02-03 09:04:20 +0200 | [diff] [blame] | 1140 | if (!pdata) { |
| 1141 | err = -EINVAL; |
| 1142 | goto out; |
| 1143 | } |
| 1144 | |
| 1145 | if (pdata->setup) { |
| 1146 | err = pdata->setup(spi); |
| 1147 | if (err) |
| 1148 | goto out; |
| 1149 | } |
| 1150 | |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1151 | /* Allocate card structure to represent this specific device */ |
| 1152 | card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL); |
| 1153 | if (!card) { |
| 1154 | err = -ENOMEM; |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 1155 | goto teardown; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1156 | } |
| 1157 | spi_set_drvdata(spi, card); |
Mike Rapoport | 0c2bec9 | 2009-02-03 09:04:20 +0200 | [diff] [blame] | 1158 | card->pdata = pdata; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1159 | card->spi = spi; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1160 | card->prev_xfer_time = jiffies; |
| 1161 | |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 1162 | INIT_LIST_HEAD(&card->cmd_packet_list); |
| 1163 | INIT_LIST_HEAD(&card->data_packet_list); |
| 1164 | spin_lock_init(&card->buffer_lock); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1165 | |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1166 | /* Initialize the SPI Interface Unit */ |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1167 | |
| 1168 | /* Firmware load */ |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 1169 | err = if_spi_init_card(card); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1170 | if (err) |
Sebastian Andrzej Siewior | 4d1d498 | 2009-06-04 21:57:03 +0200 | [diff] [blame] | 1171 | goto free_card; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1172 | |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 1173 | /* |
| 1174 | * Register our card with libertas. |
| 1175 | * This will call alloc_etherdev. |
| 1176 | */ |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1177 | priv = lbs_add_card(card, &spi->dev); |
| 1178 | if (!priv) { |
| 1179 | err = -ENOMEM; |
Sebastian Andrzej Siewior | 4d1d498 | 2009-06-04 21:57:03 +0200 | [diff] [blame] | 1180 | goto free_card; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1181 | } |
| 1182 | card->priv = priv; |
Vasily Khoruzhick | f39de99 | 2011-03-16 16:41:46 +0200 | [diff] [blame] | 1183 | priv->setup_fw_on_resume = 1; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1184 | priv->card = card; |
| 1185 | priv->hw_host_to_card = if_spi_host_to_card; |
Amitkumar Karwar | 4912545 | 2009-09-30 20:04:38 -0700 | [diff] [blame] | 1186 | priv->enter_deep_sleep = NULL; |
| 1187 | priv->exit_deep_sleep = NULL; |
| 1188 | priv->reset_deep_sleep_wakeup = NULL; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1189 | priv->fw_ready = 1; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1190 | |
| 1191 | /* Initialize interrupt handling stuff. */ |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 1192 | card->workqueue = create_workqueue("libertas_spi"); |
| 1193 | INIT_WORK(&card->packet_work, if_spi_host_to_card_worker); |
Vasily Khoruzhick | f39de99 | 2011-03-16 16:41:46 +0200 | [diff] [blame] | 1194 | INIT_WORK(&card->resume_work, if_spi_resume_worker); |
Anna Neal | b26ed97 | 2009-04-02 14:44:09 -0700 | [diff] [blame] | 1195 | |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1196 | err = request_irq(spi->irq, if_spi_host_interrupt, |
| 1197 | IRQF_TRIGGER_FALLING, "libertas_spi", card); |
| 1198 | if (err) { |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame] | 1199 | pr_err("can't get host irq line-- request_irq failed\n"); |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 1200 | goto terminate_workqueue; |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1201 | } |
| 1202 | |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 1203 | /* |
| 1204 | * Start the card. |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1205 | * This will call register_netdev, and we'll start |
Randy Dunlap | 8973a6e | 2011-04-26 15:25:29 -0700 | [diff] [blame] | 1206 | * getting interrupts... |
| 1207 | */ |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1208 | err = lbs_start_card(priv); |
| 1209 | if (err) |
| 1210 | goto release_irq; |
| 1211 | |
| 1212 | lbs_deb_spi("Finished initializing WLAN module.\n"); |
| 1213 | |
| 1214 | /* successful exit */ |
| 1215 | goto out; |
| 1216 | |
| 1217 | release_irq: |
| 1218 | free_irq(spi->irq, card); |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 1219 | terminate_workqueue: |
| 1220 | flush_workqueue(card->workqueue); |
| 1221 | destroy_workqueue(card->workqueue); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1222 | lbs_remove_card(priv); /* will call free_netdev */ |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1223 | free_card: |
| 1224 | free_if_spi_card(card); |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 1225 | teardown: |
| 1226 | if (pdata->teardown) |
| 1227 | pdata->teardown(spi); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1228 | out: |
| 1229 | lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err); |
| 1230 | return err; |
| 1231 | } |
| 1232 | |
| 1233 | static int __devexit libertas_spi_remove(struct spi_device *spi) |
| 1234 | { |
| 1235 | struct if_spi_card *card = spi_get_drvdata(spi); |
| 1236 | struct lbs_private *priv = card->priv; |
| 1237 | |
| 1238 | lbs_deb_spi("libertas_spi_remove\n"); |
| 1239 | lbs_deb_enter(LBS_DEB_SPI); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1240 | |
Vasily Khoruzhick | f39de99 | 2011-03-16 16:41:46 +0200 | [diff] [blame] | 1241 | cancel_work_sync(&card->resume_work); |
| 1242 | |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1243 | lbs_stop_card(priv); |
Andrey Yurovsky | efcfd1f | 2009-06-18 09:51:57 -0700 | [diff] [blame] | 1244 | lbs_remove_card(priv); /* will call free_netdev */ |
| 1245 | |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1246 | free_irq(spi->irq, card); |
Vasily Khoruzhick | 16f775b | 2011-01-21 22:44:48 +0200 | [diff] [blame] | 1247 | flush_workqueue(card->workqueue); |
| 1248 | destroy_workqueue(card->workqueue); |
Mike Rapoport | 0c2bec9 | 2009-02-03 09:04:20 +0200 | [diff] [blame] | 1249 | if (card->pdata->teardown) |
| 1250 | card->pdata->teardown(spi); |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1251 | free_if_spi_card(card); |
| 1252 | lbs_deb_leave(LBS_DEB_SPI); |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
Vasily Khoruzhick | f39de99 | 2011-03-16 16:41:46 +0200 | [diff] [blame] | 1256 | static int if_spi_suspend(struct device *dev) |
| 1257 | { |
| 1258 | struct spi_device *spi = to_spi_device(dev); |
| 1259 | struct if_spi_card *card = spi_get_drvdata(spi); |
| 1260 | |
| 1261 | if (!card->suspended) { |
| 1262 | lbs_suspend(card->priv); |
| 1263 | flush_workqueue(card->workqueue); |
| 1264 | disable_irq(spi->irq); |
| 1265 | |
| 1266 | if (card->pdata->teardown) |
| 1267 | card->pdata->teardown(spi); |
| 1268 | card->suspended = 1; |
| 1269 | } |
| 1270 | |
| 1271 | return 0; |
| 1272 | } |
| 1273 | |
| 1274 | static int if_spi_resume(struct device *dev) |
| 1275 | { |
| 1276 | struct spi_device *spi = to_spi_device(dev); |
| 1277 | struct if_spi_card *card = spi_get_drvdata(spi); |
| 1278 | |
| 1279 | /* Schedule delayed work */ |
| 1280 | schedule_work(&card->resume_work); |
| 1281 | |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | static const struct dev_pm_ops if_spi_pm_ops = { |
| 1286 | .suspend = if_spi_suspend, |
| 1287 | .resume = if_spi_resume, |
| 1288 | }; |
| 1289 | |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1290 | static struct spi_driver libertas_spi_driver = { |
| 1291 | .probe = if_spi_probe, |
| 1292 | .remove = __devexit_p(libertas_spi_remove), |
| 1293 | .driver = { |
| 1294 | .name = "libertas_spi", |
| 1295 | .bus = &spi_bus_type, |
| 1296 | .owner = THIS_MODULE, |
Vasily Khoruzhick | f39de99 | 2011-03-16 16:41:46 +0200 | [diff] [blame] | 1297 | .pm = &if_spi_pm_ops, |
Colin McCabe | d2b21f1 | 2009-01-09 14:58:09 -0800 | [diff] [blame] | 1298 | }, |
| 1299 | }; |
| 1300 | |
| 1301 | /* |
| 1302 | * Module functions |
| 1303 | */ |
| 1304 | |
| 1305 | static int __init if_spi_init_module(void) |
| 1306 | { |
| 1307 | int ret = 0; |
| 1308 | lbs_deb_enter(LBS_DEB_SPI); |
| 1309 | printk(KERN_INFO "libertas_spi: Libertas SPI driver\n"); |
| 1310 | ret = spi_register_driver(&libertas_spi_driver); |
| 1311 | lbs_deb_leave(LBS_DEB_SPI); |
| 1312 | return ret; |
| 1313 | } |
| 1314 | |
| 1315 | static void __exit if_spi_exit_module(void) |
| 1316 | { |
| 1317 | lbs_deb_enter(LBS_DEB_SPI); |
| 1318 | spi_unregister_driver(&libertas_spi_driver); |
| 1319 | lbs_deb_leave(LBS_DEB_SPI); |
| 1320 | } |
| 1321 | |
| 1322 | module_init(if_spi_init_module); |
| 1323 | module_exit(if_spi_exit_module); |
| 1324 | |
| 1325 | MODULE_DESCRIPTION("Libertas SPI WLAN Driver"); |
| 1326 | MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, " |
| 1327 | "Colin McCabe <colin@cozybit.com>"); |
| 1328 | MODULE_LICENSE("GPL"); |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 1329 | MODULE_ALIAS("spi:libertas_spi"); |