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