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