Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/net/wireless/libertas/if_sdio.c |
| 3 | * |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 4 | * Copyright 2007-2008 Pierre Ossman |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 5 | * |
| 6 | * Inspired by if_cs.c, Copyright 2007 Holger Schurig |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or (at |
| 11 | * your option) any later version. |
| 12 | * |
| 13 | * This hardware has more or less no CMD53 support, so all registers |
| 14 | * must be accessed using sdio_readb()/sdio_writeb(). |
| 15 | * |
| 16 | * Transfers must be in one transaction or the firmware goes bonkers. |
| 17 | * This means that the transfer must either be small enough to do a |
| 18 | * byte based transfer or it must be padded to a multiple of the |
| 19 | * current block size. |
| 20 | * |
| 21 | * As SDIO is still new to the kernel, it is unfortunately common with |
David Woodhouse | 7e22627 | 2007-12-14 22:53:41 -0500 | [diff] [blame] | 22 | * bugs in the host controllers related to that. One such bug is that |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 23 | * controllers cannot do transfers that aren't a multiple of 4 bytes. |
| 24 | * If you don't have time to fix the host controller driver, you can |
| 25 | * work around the problem by modifying if_sdio_host_to_card() and |
| 26 | * if_sdio_card_to_host() to pad the data. |
| 27 | */ |
| 28 | |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame] | 29 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 30 | |
Ilpo Järvinen | 2382792 | 2008-12-13 21:33:13 +0200 | [diff] [blame] | 31 | #include <linux/kernel.h> |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 32 | #include <linux/moduleparam.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 34 | #include <linux/firmware.h> |
| 35 | #include <linux/netdevice.h> |
| 36 | #include <linux/delay.h> |
| 37 | #include <linux/mmc/card.h> |
| 38 | #include <linux/mmc/sdio_func.h> |
| 39 | #include <linux/mmc/sdio_ids.h> |
Daniel Mack | 8a64c0f | 2010-04-06 10:52:44 +0200 | [diff] [blame] | 40 | #include <linux/mmc/sdio.h> |
| 41 | #include <linux/mmc/host.h> |
Daniel Drake | 7e1f79a | 2011-08-01 16:43:27 +0100 | [diff] [blame^] | 42 | #include <linux/pm_runtime.h> |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 43 | |
| 44 | #include "host.h" |
| 45 | #include "decl.h" |
| 46 | #include "defs.h" |
| 47 | #include "dev.h" |
Bing Zhao | 6bc61f4 | 2009-06-01 18:04:36 -0700 | [diff] [blame] | 48 | #include "cmd.h" |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 49 | #include "if_sdio.h" |
| 50 | |
Daniel Drake | 7e1f79a | 2011-08-01 16:43:27 +0100 | [diff] [blame^] | 51 | static void if_sdio_interrupt(struct sdio_func *func); |
| 52 | |
Bing Zhao | 6bc61f4 | 2009-06-01 18:04:36 -0700 | [diff] [blame] | 53 | /* The if_sdio_remove() callback function is called when |
| 54 | * user removes this module from kernel space or ejects |
| 55 | * the card from the slot. The driver handles these 2 cases |
| 56 | * differently for SD8688 combo chip. |
| 57 | * If the user is removing the module, the FUNC_SHUTDOWN |
| 58 | * command for SD8688 is sent to the firmware. |
| 59 | * If the card is removed, there is no need to send this command. |
| 60 | * |
| 61 | * The variable 'user_rmmod' is used to distinguish these two |
| 62 | * scenarios. This flag is initialized as FALSE in case the card |
| 63 | * is removed, and will be set to TRUE for module removal when |
| 64 | * module_exit function is called. |
| 65 | */ |
| 66 | static u8 user_rmmod; |
| 67 | |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 68 | static char *lbs_helper_name = NULL; |
| 69 | module_param_named(helper_name, lbs_helper_name, charp, 0644); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 70 | |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 71 | static char *lbs_fw_name = NULL; |
| 72 | module_param_named(fw_name, lbs_fw_name, charp, 0644); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 73 | |
| 74 | static const struct sdio_device_id if_sdio_ids[] = { |
Bing Zhao | e45d8e5 | 2009-04-06 15:50:56 -0700 | [diff] [blame] | 75 | { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, |
| 76 | SDIO_DEVICE_ID_MARVELL_LIBERTAS) }, |
| 77 | { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, |
| 78 | SDIO_DEVICE_ID_MARVELL_8688WLAN) }, |
| 79 | { /* end: all zeroes */ }, |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | MODULE_DEVICE_TABLE(sdio, if_sdio_ids); |
| 83 | |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 84 | #define MODEL_8385 0x04 |
| 85 | #define MODEL_8686 0x0b |
| 86 | #define MODEL_8688 0x10 |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 87 | |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 88 | static const struct lbs_fw_table fw_table[] = { |
| 89 | { MODEL_8385, "libertas/sd8385_helper.bin", "libertas/sd8385.bin" }, |
| 90 | { MODEL_8385, "sd8385_helper.bin", "sd8385.bin" }, |
| 91 | { MODEL_8686, "libertas/sd8686_v9_helper.bin", "libertas/sd8686_v9.bin" }, |
| 92 | { MODEL_8686, "libertas/sd8686_v8_helper.bin", "libertas/sd8686_v8.bin" }, |
| 93 | { MODEL_8686, "sd8686_helper.bin", "sd8686.bin" }, |
| 94 | { MODEL_8688, "libertas/sd8688_helper.bin", "libertas/sd8688.bin" }, |
| 95 | { MODEL_8688, "sd8688_helper.bin", "sd8688.bin" }, |
| 96 | { 0, NULL, NULL } |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 97 | }; |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 98 | MODULE_FIRMWARE("libertas/sd8385_helper.bin"); |
| 99 | MODULE_FIRMWARE("libertas/sd8385.bin"); |
Ben Hutchings | a974a4b | 2009-11-07 22:00:03 +0000 | [diff] [blame] | 100 | MODULE_FIRMWARE("sd8385_helper.bin"); |
| 101 | MODULE_FIRMWARE("sd8385.bin"); |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 102 | MODULE_FIRMWARE("libertas/sd8686_v9_helper.bin"); |
| 103 | MODULE_FIRMWARE("libertas/sd8686_v9.bin"); |
| 104 | MODULE_FIRMWARE("libertas/sd8686_v8_helper.bin"); |
| 105 | MODULE_FIRMWARE("libertas/sd8686_v8.bin"); |
Ben Hutchings | a974a4b | 2009-11-07 22:00:03 +0000 | [diff] [blame] | 106 | MODULE_FIRMWARE("sd8686_helper.bin"); |
| 107 | MODULE_FIRMWARE("sd8686.bin"); |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 108 | MODULE_FIRMWARE("libertas/sd8688_helper.bin"); |
| 109 | MODULE_FIRMWARE("libertas/sd8688.bin"); |
Ben Hutchings | a974a4b | 2009-11-07 22:00:03 +0000 | [diff] [blame] | 110 | MODULE_FIRMWARE("sd8688_helper.bin"); |
| 111 | MODULE_FIRMWARE("sd8688.bin"); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 112 | |
| 113 | struct if_sdio_packet { |
| 114 | struct if_sdio_packet *next; |
| 115 | u16 nb; |
| 116 | u8 buffer[0] __attribute__((aligned(4))); |
| 117 | }; |
| 118 | |
| 119 | struct if_sdio_card { |
| 120 | struct sdio_func *func; |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 121 | struct lbs_private *priv; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 122 | |
| 123 | int model; |
| 124 | unsigned long ioport; |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame] | 125 | unsigned int scratch_reg; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 126 | |
| 127 | const char *helper; |
| 128 | const char *firmware; |
Rusty Russell | 886275c | 2010-08-11 23:04:33 -0600 | [diff] [blame] | 129 | bool helper_allocated; |
| 130 | bool firmware_allocated; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 131 | |
Mike Rapoport | 557de5e | 2010-08-22 14:22:40 +0300 | [diff] [blame] | 132 | u8 buffer[65536] __attribute__((aligned(4))); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 133 | |
| 134 | spinlock_t lock; |
| 135 | struct if_sdio_packet *packets; |
Dan Williams | 9b02f41 | 2009-02-20 12:27:38 -0500 | [diff] [blame] | 136 | |
| 137 | struct workqueue_struct *workqueue; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 138 | struct work_struct packet_worker; |
Bing Zhao | b136a14 | 2009-05-19 19:48:19 -0700 | [diff] [blame] | 139 | |
| 140 | u8 rx_unit; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | /********************************************************************/ |
| 144 | /* I/O */ |
| 145 | /********************************************************************/ |
| 146 | |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame] | 147 | /* |
| 148 | * For SD8385/SD8686, this function reads firmware status after |
| 149 | * the image is downloaded, or reads RX packet length when |
| 150 | * interrupt (with IF_SDIO_H_INT_UPLD bit set) is received. |
| 151 | * For SD8688, this function reads firmware status only. |
| 152 | */ |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 153 | static u16 if_sdio_read_scratch(struct if_sdio_card *card, int *err) |
| 154 | { |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame] | 155 | int ret; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 156 | u16 scratch; |
| 157 | |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame] | 158 | scratch = sdio_readb(card->func, card->scratch_reg, &ret); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 159 | if (!ret) |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame] | 160 | scratch |= sdio_readb(card->func, card->scratch_reg + 1, |
| 161 | &ret) << 8; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 162 | |
| 163 | if (err) |
| 164 | *err = ret; |
| 165 | |
| 166 | if (ret) |
| 167 | return 0xffff; |
| 168 | |
| 169 | return scratch; |
| 170 | } |
| 171 | |
Bing Zhao | b136a14 | 2009-05-19 19:48:19 -0700 | [diff] [blame] | 172 | static u8 if_sdio_read_rx_unit(struct if_sdio_card *card) |
| 173 | { |
| 174 | int ret; |
| 175 | u8 rx_unit; |
| 176 | |
| 177 | rx_unit = sdio_readb(card->func, IF_SDIO_RX_UNIT, &ret); |
| 178 | |
| 179 | if (ret) |
| 180 | rx_unit = 0; |
| 181 | |
| 182 | return rx_unit; |
| 183 | } |
| 184 | |
| 185 | static u16 if_sdio_read_rx_len(struct if_sdio_card *card, int *err) |
| 186 | { |
| 187 | int ret; |
| 188 | u16 rx_len; |
| 189 | |
| 190 | switch (card->model) { |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 191 | case MODEL_8385: |
| 192 | case MODEL_8686: |
Bing Zhao | b136a14 | 2009-05-19 19:48:19 -0700 | [diff] [blame] | 193 | rx_len = if_sdio_read_scratch(card, &ret); |
| 194 | break; |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 195 | case MODEL_8688: |
Bing Zhao | b136a14 | 2009-05-19 19:48:19 -0700 | [diff] [blame] | 196 | default: /* for newer chipsets */ |
| 197 | rx_len = sdio_readb(card->func, IF_SDIO_RX_LEN, &ret); |
| 198 | if (!ret) |
| 199 | rx_len <<= card->rx_unit; |
| 200 | else |
| 201 | rx_len = 0xffff; /* invalid length */ |
| 202 | |
| 203 | break; |
| 204 | } |
| 205 | |
| 206 | if (err) |
| 207 | *err = ret; |
| 208 | |
| 209 | return rx_len; |
| 210 | } |
| 211 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 212 | static int if_sdio_handle_cmd(struct if_sdio_card *card, |
| 213 | u8 *buffer, unsigned size) |
| 214 | { |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 215 | struct lbs_private *priv = card->priv; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 216 | int ret; |
| 217 | unsigned long flags; |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 218 | u8 i; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 219 | |
| 220 | lbs_deb_enter(LBS_DEB_SDIO); |
| 221 | |
Dan Williams | ddac452 | 2007-12-11 13:49:39 -0500 | [diff] [blame] | 222 | if (size > LBS_CMD_BUFFER_SIZE) { |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 223 | lbs_deb_sdio("response packet too large (%d bytes)\n", |
| 224 | (int)size); |
| 225 | ret = -E2BIG; |
| 226 | goto out; |
| 227 | } |
| 228 | |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 229 | spin_lock_irqsave(&priv->driver_lock, flags); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 230 | |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 231 | i = (priv->resp_idx == 0) ? 1 : 0; |
| 232 | BUG_ON(priv->resp_len[i]); |
| 233 | priv->resp_len[i] = size; |
| 234 | memcpy(priv->resp_buf[i], buffer, size); |
| 235 | lbs_notify_command_response(priv, i); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 236 | |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 237 | spin_unlock_irqrestore(&card->priv->driver_lock, flags); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 238 | |
| 239 | ret = 0; |
| 240 | |
| 241 | out: |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 242 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | static int if_sdio_handle_data(struct if_sdio_card *card, |
| 247 | u8 *buffer, unsigned size) |
| 248 | { |
| 249 | int ret; |
| 250 | struct sk_buff *skb; |
| 251 | char *data; |
| 252 | |
| 253 | lbs_deb_enter(LBS_DEB_SDIO); |
| 254 | |
| 255 | if (size > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) { |
| 256 | lbs_deb_sdio("response packet too large (%d bytes)\n", |
| 257 | (int)size); |
| 258 | ret = -E2BIG; |
| 259 | goto out; |
| 260 | } |
| 261 | |
Pierre Ossman | 51e6b71 | 2007-10-22 19:05:32 +0200 | [diff] [blame] | 262 | skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + NET_IP_ALIGN); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 263 | if (!skb) { |
| 264 | ret = -ENOMEM; |
| 265 | goto out; |
| 266 | } |
| 267 | |
Pierre Ossman | 51e6b71 | 2007-10-22 19:05:32 +0200 | [diff] [blame] | 268 | skb_reserve(skb, NET_IP_ALIGN); |
| 269 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 270 | data = skb_put(skb, size); |
| 271 | |
| 272 | memcpy(data, buffer, size); |
| 273 | |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 274 | lbs_process_rxed_packet(card->priv, skb); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 275 | |
| 276 | ret = 0; |
| 277 | |
| 278 | out: |
| 279 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 280 | |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | static int if_sdio_handle_event(struct if_sdio_card *card, |
| 285 | u8 *buffer, unsigned size) |
| 286 | { |
| 287 | int ret; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 288 | u32 event; |
| 289 | |
| 290 | lbs_deb_enter(LBS_DEB_SDIO); |
| 291 | |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 292 | if (card->model == MODEL_8385) { |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 293 | event = sdio_readb(card->func, IF_SDIO_EVENT, &ret); |
| 294 | if (ret) |
| 295 | goto out; |
Bing Zhao | f5ac2b9 | 2009-02-04 22:22:39 -0800 | [diff] [blame] | 296 | |
| 297 | /* right shift 3 bits to get the event id */ |
| 298 | event >>= 3; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 299 | } else { |
| 300 | if (size < 4) { |
| 301 | lbs_deb_sdio("event packet too small (%d bytes)\n", |
| 302 | (int)size); |
| 303 | ret = -EINVAL; |
| 304 | goto out; |
| 305 | } |
| 306 | event = buffer[3] << 24; |
| 307 | event |= buffer[2] << 16; |
| 308 | event |= buffer[1] << 8; |
| 309 | event |= buffer[0] << 0; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 310 | } |
| 311 | |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 312 | lbs_queue_event(card->priv, event & 0xFF); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 313 | ret = 0; |
| 314 | |
| 315 | out: |
| 316 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 317 | |
| 318 | return ret; |
| 319 | } |
| 320 | |
Dan Williams | 96021f0 | 2010-04-15 13:27:44 -0700 | [diff] [blame] | 321 | static int if_sdio_wait_status(struct if_sdio_card *card, const u8 condition) |
| 322 | { |
| 323 | u8 status; |
| 324 | unsigned long timeout; |
| 325 | int ret = 0; |
| 326 | |
| 327 | timeout = jiffies + HZ; |
| 328 | while (1) { |
| 329 | status = sdio_readb(card->func, IF_SDIO_STATUS, &ret); |
Dan Williams | ca962557 | 2010-04-28 12:37:46 -0700 | [diff] [blame] | 330 | if (ret) |
| 331 | return ret; |
| 332 | if ((status & condition) == condition) |
Dan Williams | 96021f0 | 2010-04-15 13:27:44 -0700 | [diff] [blame] | 333 | break; |
| 334 | if (time_after(jiffies, timeout)) |
| 335 | return -ETIMEDOUT; |
| 336 | mdelay(1); |
| 337 | } |
| 338 | return ret; |
| 339 | } |
| 340 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 341 | static int if_sdio_card_to_host(struct if_sdio_card *card) |
| 342 | { |
| 343 | int ret; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 344 | u16 size, type, chunk; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 345 | |
| 346 | lbs_deb_enter(LBS_DEB_SDIO); |
| 347 | |
Bing Zhao | b136a14 | 2009-05-19 19:48:19 -0700 | [diff] [blame] | 348 | size = if_sdio_read_rx_len(card, &ret); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 349 | if (ret) |
| 350 | goto out; |
| 351 | |
| 352 | if (size < 4) { |
| 353 | lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n", |
| 354 | (int)size); |
| 355 | ret = -EINVAL; |
| 356 | goto out; |
| 357 | } |
| 358 | |
Dan Williams | 96021f0 | 2010-04-15 13:27:44 -0700 | [diff] [blame] | 359 | ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY); |
| 360 | if (ret) |
| 361 | goto out; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 362 | |
| 363 | /* |
| 364 | * The transfer must be in one transaction or the firmware |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 365 | * goes suicidal. There's no way to guarantee that for all |
| 366 | * controllers, but we can at least try. |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 367 | */ |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 368 | chunk = sdio_align_size(card->func, size); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 369 | |
| 370 | ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk); |
| 371 | if (ret) |
| 372 | goto out; |
| 373 | |
| 374 | chunk = card->buffer[0] | (card->buffer[1] << 8); |
| 375 | type = card->buffer[2] | (card->buffer[3] << 8); |
| 376 | |
| 377 | lbs_deb_sdio("packet of type %d and size %d bytes\n", |
| 378 | (int)type, (int)chunk); |
| 379 | |
| 380 | if (chunk > size) { |
| 381 | lbs_deb_sdio("packet fragment (%d > %d)\n", |
| 382 | (int)chunk, (int)size); |
| 383 | ret = -EINVAL; |
| 384 | goto out; |
| 385 | } |
| 386 | |
| 387 | if (chunk < size) { |
| 388 | lbs_deb_sdio("packet fragment (%d < %d)\n", |
| 389 | (int)chunk, (int)size); |
| 390 | } |
| 391 | |
| 392 | switch (type) { |
| 393 | case MVMS_CMD: |
| 394 | ret = if_sdio_handle_cmd(card, card->buffer + 4, chunk - 4); |
| 395 | if (ret) |
| 396 | goto out; |
| 397 | break; |
| 398 | case MVMS_DAT: |
| 399 | ret = if_sdio_handle_data(card, card->buffer + 4, chunk - 4); |
| 400 | if (ret) |
| 401 | goto out; |
| 402 | break; |
| 403 | case MVMS_EVENT: |
| 404 | ret = if_sdio_handle_event(card, card->buffer + 4, chunk - 4); |
| 405 | if (ret) |
| 406 | goto out; |
| 407 | break; |
| 408 | default: |
| 409 | lbs_deb_sdio("invalid type (%d) from firmware\n", |
| 410 | (int)type); |
| 411 | ret = -EINVAL; |
| 412 | goto out; |
| 413 | } |
| 414 | |
| 415 | out: |
| 416 | if (ret) |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame] | 417 | pr_err("problem fetching packet from firmware\n"); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 418 | |
| 419 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 420 | |
| 421 | return ret; |
| 422 | } |
| 423 | |
| 424 | static void if_sdio_host_to_card_worker(struct work_struct *work) |
| 425 | { |
| 426 | struct if_sdio_card *card; |
| 427 | struct if_sdio_packet *packet; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 428 | int ret; |
| 429 | unsigned long flags; |
| 430 | |
| 431 | lbs_deb_enter(LBS_DEB_SDIO); |
| 432 | |
| 433 | card = container_of(work, struct if_sdio_card, packet_worker); |
| 434 | |
| 435 | while (1) { |
| 436 | spin_lock_irqsave(&card->lock, flags); |
| 437 | packet = card->packets; |
| 438 | if (packet) |
| 439 | card->packets = packet->next; |
| 440 | spin_unlock_irqrestore(&card->lock, flags); |
| 441 | |
| 442 | if (!packet) |
| 443 | break; |
| 444 | |
| 445 | sdio_claim_host(card->func); |
| 446 | |
Dan Williams | 96021f0 | 2010-04-15 13:27:44 -0700 | [diff] [blame] | 447 | ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY); |
| 448 | if (ret == 0) { |
| 449 | ret = sdio_writesb(card->func, card->ioport, |
| 450 | packet->buffer, packet->nb); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 451 | } |
| 452 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 453 | if (ret) |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame] | 454 | pr_err("error %d sending packet to firmware\n", ret); |
Dan Williams | 96021f0 | 2010-04-15 13:27:44 -0700 | [diff] [blame] | 455 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 456 | sdio_release_host(card->func); |
| 457 | |
| 458 | kfree(packet); |
| 459 | } |
| 460 | |
| 461 | lbs_deb_leave(LBS_DEB_SDIO); |
| 462 | } |
| 463 | |
| 464 | /********************************************************************/ |
| 465 | /* Firmware */ |
| 466 | /********************************************************************/ |
| 467 | |
Dan Williams | 96021f0 | 2010-04-15 13:27:44 -0700 | [diff] [blame] | 468 | #define FW_DL_READY_STATUS (IF_SDIO_IO_RDY | IF_SDIO_DL_RDY) |
| 469 | |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 470 | static int if_sdio_prog_helper(struct if_sdio_card *card, |
| 471 | const struct firmware *fw) |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 472 | { |
| 473 | int ret; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 474 | unsigned long timeout; |
| 475 | u8 *chunk_buffer; |
| 476 | u32 chunk_size; |
David Woodhouse | 6dfff89 | 2008-05-23 18:37:51 +0100 | [diff] [blame] | 477 | const u8 *firmware; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 478 | size_t size; |
| 479 | |
| 480 | lbs_deb_enter(LBS_DEB_SDIO); |
| 481 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 482 | chunk_buffer = kzalloc(64, GFP_KERNEL); |
| 483 | if (!chunk_buffer) { |
| 484 | ret = -ENOMEM; |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 485 | goto out; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | sdio_claim_host(card->func); |
| 489 | |
| 490 | ret = sdio_set_block_size(card->func, 32); |
| 491 | if (ret) |
| 492 | goto release; |
| 493 | |
| 494 | firmware = fw->data; |
| 495 | size = fw->size; |
| 496 | |
| 497 | while (size) { |
Dan Williams | 96021f0 | 2010-04-15 13:27:44 -0700 | [diff] [blame] | 498 | ret = if_sdio_wait_status(card, FW_DL_READY_STATUS); |
| 499 | if (ret) |
| 500 | goto release; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 501 | |
Dan Williams | ac1a474 | 2010-04-15 13:32:58 -0700 | [diff] [blame] | 502 | /* On some platforms (like Davinci) the chip needs more time |
| 503 | * between helper blocks. |
| 504 | */ |
| 505 | mdelay(2); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 506 | |
| 507 | chunk_size = min(size, (size_t)60); |
| 508 | |
Holger Schurig | c2df2ef | 2007-12-07 15:30:44 +0000 | [diff] [blame] | 509 | *((__le32*)chunk_buffer) = cpu_to_le32(chunk_size); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 510 | memcpy(chunk_buffer + 4, firmware, chunk_size); |
| 511 | /* |
| 512 | lbs_deb_sdio("sending %d bytes chunk\n", chunk_size); |
| 513 | */ |
| 514 | ret = sdio_writesb(card->func, card->ioport, |
| 515 | chunk_buffer, 64); |
| 516 | if (ret) |
| 517 | goto release; |
| 518 | |
| 519 | firmware += chunk_size; |
| 520 | size -= chunk_size; |
| 521 | } |
| 522 | |
| 523 | /* an empty block marks the end of the transfer */ |
| 524 | memset(chunk_buffer, 0, 4); |
| 525 | ret = sdio_writesb(card->func, card->ioport, chunk_buffer, 64); |
| 526 | if (ret) |
| 527 | goto release; |
| 528 | |
| 529 | lbs_deb_sdio("waiting for helper to boot...\n"); |
| 530 | |
| 531 | /* wait for the helper to boot by looking at the size register */ |
| 532 | timeout = jiffies + HZ; |
| 533 | while (1) { |
| 534 | u16 req_size; |
| 535 | |
| 536 | req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret); |
| 537 | if (ret) |
| 538 | goto release; |
| 539 | |
| 540 | req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8; |
| 541 | if (ret) |
| 542 | goto release; |
| 543 | |
| 544 | if (req_size != 0) |
| 545 | break; |
| 546 | |
| 547 | if (time_after(jiffies, timeout)) { |
| 548 | ret = -ETIMEDOUT; |
| 549 | goto release; |
| 550 | } |
| 551 | |
| 552 | msleep(10); |
| 553 | } |
| 554 | |
| 555 | ret = 0; |
| 556 | |
| 557 | release: |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 558 | sdio_release_host(card->func); |
| 559 | kfree(chunk_buffer); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 560 | |
| 561 | out: |
| 562 | if (ret) |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame] | 563 | pr_err("failed to load helper firmware\n"); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 564 | |
| 565 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 566 | return ret; |
| 567 | } |
| 568 | |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 569 | static int if_sdio_prog_real(struct if_sdio_card *card, |
| 570 | const struct firmware *fw) |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 571 | { |
| 572 | int ret; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 573 | unsigned long timeout; |
| 574 | u8 *chunk_buffer; |
| 575 | u32 chunk_size; |
David Woodhouse | 6dfff89 | 2008-05-23 18:37:51 +0100 | [diff] [blame] | 576 | const u8 *firmware; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 577 | size_t size, req_size; |
| 578 | |
| 579 | lbs_deb_enter(LBS_DEB_SDIO); |
| 580 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 581 | chunk_buffer = kzalloc(512, GFP_KERNEL); |
| 582 | if (!chunk_buffer) { |
| 583 | ret = -ENOMEM; |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 584 | goto out; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | sdio_claim_host(card->func); |
| 588 | |
| 589 | ret = sdio_set_block_size(card->func, 32); |
| 590 | if (ret) |
| 591 | goto release; |
| 592 | |
| 593 | firmware = fw->data; |
| 594 | size = fw->size; |
| 595 | |
| 596 | while (size) { |
Dan Williams | 96021f0 | 2010-04-15 13:27:44 -0700 | [diff] [blame] | 597 | ret = if_sdio_wait_status(card, FW_DL_READY_STATUS); |
| 598 | if (ret) |
| 599 | goto release; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 600 | |
| 601 | req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret); |
| 602 | if (ret) |
| 603 | goto release; |
| 604 | |
| 605 | req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8; |
| 606 | if (ret) |
| 607 | goto release; |
| 608 | /* |
| 609 | lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size); |
| 610 | */ |
| 611 | if (req_size == 0) { |
| 612 | lbs_deb_sdio("firmware helper gave up early\n"); |
| 613 | ret = -EIO; |
| 614 | goto release; |
| 615 | } |
| 616 | |
| 617 | if (req_size & 0x01) { |
| 618 | lbs_deb_sdio("firmware helper signalled error\n"); |
| 619 | ret = -EIO; |
| 620 | goto release; |
| 621 | } |
| 622 | |
| 623 | if (req_size > size) |
| 624 | req_size = size; |
| 625 | |
| 626 | while (req_size) { |
| 627 | chunk_size = min(req_size, (size_t)512); |
| 628 | |
| 629 | memcpy(chunk_buffer, firmware, chunk_size); |
| 630 | /* |
| 631 | lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n", |
| 632 | chunk_size, (chunk_size + 31) / 32 * 32); |
| 633 | */ |
| 634 | ret = sdio_writesb(card->func, card->ioport, |
Ilpo Järvinen | 2382792 | 2008-12-13 21:33:13 +0200 | [diff] [blame] | 635 | chunk_buffer, roundup(chunk_size, 32)); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 636 | if (ret) |
| 637 | goto release; |
| 638 | |
| 639 | firmware += chunk_size; |
| 640 | size -= chunk_size; |
| 641 | req_size -= chunk_size; |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | ret = 0; |
| 646 | |
| 647 | lbs_deb_sdio("waiting for firmware to boot...\n"); |
| 648 | |
| 649 | /* wait for the firmware to boot */ |
| 650 | timeout = jiffies + HZ; |
| 651 | while (1) { |
| 652 | u16 scratch; |
| 653 | |
| 654 | scratch = if_sdio_read_scratch(card, &ret); |
| 655 | if (ret) |
| 656 | goto release; |
| 657 | |
| 658 | if (scratch == IF_SDIO_FIRMWARE_OK) |
| 659 | break; |
| 660 | |
| 661 | if (time_after(jiffies, timeout)) { |
| 662 | ret = -ETIMEDOUT; |
| 663 | goto release; |
| 664 | } |
| 665 | |
| 666 | msleep(10); |
| 667 | } |
| 668 | |
| 669 | ret = 0; |
| 670 | |
| 671 | release: |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 672 | sdio_release_host(card->func); |
| 673 | kfree(chunk_buffer); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 674 | |
| 675 | out: |
| 676 | if (ret) |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame] | 677 | pr_err("failed to load firmware\n"); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 678 | |
| 679 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 680 | return ret; |
| 681 | } |
| 682 | |
| 683 | static int if_sdio_prog_firmware(struct if_sdio_card *card) |
| 684 | { |
| 685 | int ret; |
| 686 | u16 scratch; |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 687 | const struct firmware *helper = NULL; |
| 688 | const struct firmware *mainfw = NULL; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 689 | |
| 690 | lbs_deb_enter(LBS_DEB_SDIO); |
| 691 | |
Paul Fox | 731b203 | 2010-10-29 14:57:28 +0100 | [diff] [blame] | 692 | /* |
| 693 | * Disable interrupts |
| 694 | */ |
| 695 | sdio_claim_host(card->func); |
| 696 | sdio_writeb(card->func, 0x00, IF_SDIO_H_INT_MASK, &ret); |
| 697 | sdio_release_host(card->func); |
| 698 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 699 | sdio_claim_host(card->func); |
| 700 | scratch = if_sdio_read_scratch(card, &ret); |
| 701 | sdio_release_host(card->func); |
| 702 | |
Paul Fox | 731b203 | 2010-10-29 14:57:28 +0100 | [diff] [blame] | 703 | lbs_deb_sdio("firmware status = %#x\n", scratch); |
| 704 | lbs_deb_sdio("scratch ret = %d\n", ret); |
| 705 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 706 | if (ret) |
| 707 | goto out; |
| 708 | |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame] | 709 | |
Paul Fox | 731b203 | 2010-10-29 14:57:28 +0100 | [diff] [blame] | 710 | /* |
| 711 | * The manual clearly describes that FEDC is the right code to use |
| 712 | * to detect firmware presence, but for SD8686 it is not that simple. |
| 713 | * Scratch is also used to store the RX packet length, so we lose |
| 714 | * the FEDC value early on. So we use a non-zero check in order |
| 715 | * to validate firmware presence. |
| 716 | * Additionally, the SD8686 in the Gumstix always has the high scratch |
| 717 | * bit set, even when the firmware is not loaded. So we have to |
| 718 | * exclude that from the test. |
| 719 | */ |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 720 | if (scratch == IF_SDIO_FIRMWARE_OK) { |
| 721 | lbs_deb_sdio("firmware already loaded\n"); |
| 722 | goto success; |
Paul Fox | 731b203 | 2010-10-29 14:57:28 +0100 | [diff] [blame] | 723 | } else if ((card->model == MODEL_8686) && (scratch & 0x7fff)) { |
| 724 | lbs_deb_sdio("firmware may be running\n"); |
| 725 | goto success; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 726 | } |
| 727 | |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 728 | ret = lbs_get_firmware(&card->func->dev, lbs_helper_name, lbs_fw_name, |
| 729 | card->model, &fw_table[0], &helper, &mainfw); |
| 730 | if (ret) { |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame] | 731 | pr_err("failed to find firmware (%d)\n", ret); |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 732 | goto out; |
| 733 | } |
| 734 | |
| 735 | ret = if_sdio_prog_helper(card, helper); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 736 | if (ret) |
| 737 | goto out; |
| 738 | |
Paul Fox | 731b203 | 2010-10-29 14:57:28 +0100 | [diff] [blame] | 739 | lbs_deb_sdio("Helper firmware loaded\n"); |
| 740 | |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 741 | ret = if_sdio_prog_real(card, mainfw); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 742 | if (ret) |
| 743 | goto out; |
| 744 | |
Paul Fox | 731b203 | 2010-10-29 14:57:28 +0100 | [diff] [blame] | 745 | lbs_deb_sdio("Firmware loaded\n"); |
| 746 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 747 | success: |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 748 | sdio_claim_host(card->func); |
| 749 | sdio_set_block_size(card->func, IF_SDIO_BLOCK_SIZE); |
| 750 | sdio_release_host(card->func); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 751 | ret = 0; |
| 752 | |
| 753 | out: |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 754 | if (helper) |
| 755 | release_firmware(helper); |
| 756 | if (mainfw) |
| 757 | release_firmware(mainfw); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 758 | |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 759 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 760 | return ret; |
| 761 | } |
| 762 | |
Daniel Drake | 7e1f79a | 2011-08-01 16:43:27 +0100 | [diff] [blame^] | 763 | /********************************************************************/ |
| 764 | /* Power management */ |
| 765 | /********************************************************************/ |
| 766 | |
| 767 | static int if_sdio_power_on(struct if_sdio_card *card) |
| 768 | { |
| 769 | struct sdio_func *func = card->func; |
| 770 | struct lbs_private *priv = card->priv; |
| 771 | struct mmc_host *host = func->card->host; |
| 772 | int ret; |
| 773 | |
| 774 | sdio_claim_host(func); |
| 775 | |
| 776 | ret = sdio_enable_func(func); |
| 777 | if (ret) |
| 778 | goto release; |
| 779 | |
| 780 | /* For 1-bit transfers to the 8686 model, we need to enable the |
| 781 | * interrupt flag in the CCCR register. Set the MMC_QUIRK_LENIENT_FN0 |
| 782 | * bit to allow access to non-vendor registers. */ |
| 783 | if ((card->model == MODEL_8686) && |
| 784 | (host->caps & MMC_CAP_SDIO_IRQ) && |
| 785 | (host->ios.bus_width == MMC_BUS_WIDTH_1)) { |
| 786 | u8 reg; |
| 787 | |
| 788 | func->card->quirks |= MMC_QUIRK_LENIENT_FN0; |
| 789 | reg = sdio_f0_readb(func, SDIO_CCCR_IF, &ret); |
| 790 | if (ret) |
| 791 | goto disable; |
| 792 | |
| 793 | reg |= SDIO_BUS_ECSI; |
| 794 | sdio_f0_writeb(func, reg, SDIO_CCCR_IF, &ret); |
| 795 | if (ret) |
| 796 | goto disable; |
| 797 | } |
| 798 | |
| 799 | card->ioport = sdio_readb(func, IF_SDIO_IOPORT, &ret); |
| 800 | if (ret) |
| 801 | goto disable; |
| 802 | |
| 803 | card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 1, &ret) << 8; |
| 804 | if (ret) |
| 805 | goto disable; |
| 806 | |
| 807 | card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 2, &ret) << 16; |
| 808 | if (ret) |
| 809 | goto disable; |
| 810 | |
| 811 | sdio_release_host(func); |
| 812 | ret = if_sdio_prog_firmware(card); |
| 813 | sdio_claim_host(func); |
| 814 | if (ret) |
| 815 | goto disable; |
| 816 | |
| 817 | /* |
| 818 | * Get rx_unit if the chip is SD8688 or newer. |
| 819 | * SD8385 & SD8686 do not have rx_unit. |
| 820 | */ |
| 821 | if ((card->model != MODEL_8385) |
| 822 | && (card->model != MODEL_8686)) |
| 823 | card->rx_unit = if_sdio_read_rx_unit(card); |
| 824 | else |
| 825 | card->rx_unit = 0; |
| 826 | |
| 827 | /* |
| 828 | * Set up the interrupt handler late. |
| 829 | * |
| 830 | * If we set it up earlier, the (buggy) hardware generates a spurious |
| 831 | * interrupt, even before the interrupt has been enabled, with |
| 832 | * CCCR_INTx = 0. |
| 833 | * |
| 834 | * We register the interrupt handler late so that we can handle any |
| 835 | * spurious interrupts, and also to avoid generation of that known |
| 836 | * spurious interrupt in the first place. |
| 837 | */ |
| 838 | ret = sdio_claim_irq(func, if_sdio_interrupt); |
| 839 | if (ret) |
| 840 | goto disable; |
| 841 | |
| 842 | /* |
| 843 | * Enable interrupts now that everything is set up |
| 844 | */ |
| 845 | sdio_writeb(func, 0x0f, IF_SDIO_H_INT_MASK, &ret); |
| 846 | if (ret) |
| 847 | goto release_irq; |
| 848 | |
| 849 | sdio_release_host(func); |
| 850 | |
| 851 | /* |
| 852 | * FUNC_INIT is required for SD8688 WLAN/BT multiple functions |
| 853 | */ |
| 854 | if (card->model == MODEL_8688) { |
| 855 | struct cmd_header cmd; |
| 856 | |
| 857 | memset(&cmd, 0, sizeof(cmd)); |
| 858 | |
| 859 | lbs_deb_sdio("send function INIT command\n"); |
| 860 | if (__lbs_cmd(priv, CMD_FUNC_INIT, &cmd, sizeof(cmd), |
| 861 | lbs_cmd_copyback, (unsigned long) &cmd)) |
| 862 | netdev_alert(priv->dev, "CMD_FUNC_INIT cmd failed\n"); |
| 863 | } |
| 864 | |
| 865 | priv->fw_ready = 1; |
| 866 | |
| 867 | return 0; |
| 868 | |
| 869 | release_irq: |
| 870 | sdio_release_irq(func); |
| 871 | disable: |
| 872 | sdio_disable_func(func); |
| 873 | release: |
| 874 | sdio_release_host(func); |
| 875 | return ret; |
| 876 | } |
| 877 | |
| 878 | static int if_sdio_power_off(struct if_sdio_card *card) |
| 879 | { |
| 880 | struct sdio_func *func = card->func; |
| 881 | struct lbs_private *priv = card->priv; |
| 882 | |
| 883 | priv->fw_ready = 0; |
| 884 | |
| 885 | sdio_claim_host(func); |
| 886 | sdio_release_irq(func); |
| 887 | sdio_disable_func(func); |
| 888 | sdio_release_host(func); |
| 889 | return 0; |
| 890 | } |
| 891 | |
| 892 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 893 | /*******************************************************************/ |
| 894 | /* Libertas callbacks */ |
| 895 | /*******************************************************************/ |
| 896 | |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 897 | static int if_sdio_host_to_card(struct lbs_private *priv, |
| 898 | u8 type, u8 *buf, u16 nb) |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 899 | { |
| 900 | int ret; |
| 901 | struct if_sdio_card *card; |
| 902 | struct if_sdio_packet *packet, *cur; |
| 903 | u16 size; |
| 904 | unsigned long flags; |
| 905 | |
| 906 | lbs_deb_enter_args(LBS_DEB_SDIO, "type %d, bytes %d", type, nb); |
| 907 | |
| 908 | card = priv->card; |
| 909 | |
| 910 | if (nb > (65536 - sizeof(struct if_sdio_packet) - 4)) { |
| 911 | ret = -EINVAL; |
| 912 | goto out; |
| 913 | } |
| 914 | |
| 915 | /* |
| 916 | * The transfer must be in one transaction or the firmware |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 917 | * goes suicidal. There's no way to guarantee that for all |
| 918 | * controllers, but we can at least try. |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 919 | */ |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 920 | size = sdio_align_size(card->func, nb + 4); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 921 | |
| 922 | packet = kzalloc(sizeof(struct if_sdio_packet) + size, |
| 923 | GFP_ATOMIC); |
| 924 | if (!packet) { |
| 925 | ret = -ENOMEM; |
| 926 | goto out; |
| 927 | } |
| 928 | |
| 929 | packet->next = NULL; |
| 930 | packet->nb = size; |
| 931 | |
| 932 | /* |
| 933 | * SDIO specific header. |
| 934 | */ |
| 935 | packet->buffer[0] = (nb + 4) & 0xff; |
| 936 | packet->buffer[1] = ((nb + 4) >> 8) & 0xff; |
| 937 | packet->buffer[2] = type; |
| 938 | packet->buffer[3] = 0; |
| 939 | |
| 940 | memcpy(packet->buffer + 4, buf, nb); |
| 941 | |
| 942 | spin_lock_irqsave(&card->lock, flags); |
| 943 | |
| 944 | if (!card->packets) |
| 945 | card->packets = packet; |
| 946 | else { |
| 947 | cur = card->packets; |
| 948 | while (cur->next) |
| 949 | cur = cur->next; |
| 950 | cur->next = packet; |
| 951 | } |
| 952 | |
| 953 | switch (type) { |
| 954 | case MVMS_CMD: |
| 955 | priv->dnld_sent = DNLD_CMD_SENT; |
| 956 | break; |
| 957 | case MVMS_DAT: |
| 958 | priv->dnld_sent = DNLD_DATA_SENT; |
| 959 | break; |
| 960 | default: |
| 961 | lbs_deb_sdio("unknown packet type %d\n", (int)type); |
| 962 | } |
| 963 | |
| 964 | spin_unlock_irqrestore(&card->lock, flags); |
| 965 | |
Dan Williams | 9b02f41 | 2009-02-20 12:27:38 -0500 | [diff] [blame] | 966 | queue_work(card->workqueue, &card->packet_worker); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 967 | |
| 968 | ret = 0; |
| 969 | |
| 970 | out: |
| 971 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 972 | |
| 973 | return ret; |
| 974 | } |
| 975 | |
Amitkumar Karwar | 4912545 | 2009-09-30 20:04:38 -0700 | [diff] [blame] | 976 | static int if_sdio_enter_deep_sleep(struct lbs_private *priv) |
| 977 | { |
| 978 | int ret = -1; |
| 979 | struct cmd_header cmd; |
| 980 | |
| 981 | memset(&cmd, 0, sizeof(cmd)); |
| 982 | |
| 983 | lbs_deb_sdio("send DEEP_SLEEP command\n"); |
| 984 | ret = __lbs_cmd(priv, CMD_802_11_DEEP_SLEEP, &cmd, sizeof(cmd), |
| 985 | lbs_cmd_copyback, (unsigned long) &cmd); |
| 986 | if (ret) |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 987 | netdev_err(priv->dev, "DEEP_SLEEP cmd failed\n"); |
Amitkumar Karwar | 4912545 | 2009-09-30 20:04:38 -0700 | [diff] [blame] | 988 | |
| 989 | mdelay(200); |
| 990 | return ret; |
| 991 | } |
| 992 | |
| 993 | static int if_sdio_exit_deep_sleep(struct lbs_private *priv) |
| 994 | { |
| 995 | struct if_sdio_card *card = priv->card; |
| 996 | int ret = -1; |
| 997 | |
| 998 | lbs_deb_enter(LBS_DEB_SDIO); |
| 999 | sdio_claim_host(card->func); |
| 1000 | |
| 1001 | sdio_writeb(card->func, HOST_POWER_UP, CONFIGURATION_REG, &ret); |
| 1002 | if (ret) |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 1003 | netdev_err(priv->dev, "sdio_writeb failed!\n"); |
Amitkumar Karwar | 4912545 | 2009-09-30 20:04:38 -0700 | [diff] [blame] | 1004 | |
| 1005 | sdio_release_host(card->func); |
| 1006 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 1007 | return ret; |
| 1008 | } |
| 1009 | |
| 1010 | static int if_sdio_reset_deep_sleep_wakeup(struct lbs_private *priv) |
| 1011 | { |
| 1012 | struct if_sdio_card *card = priv->card; |
| 1013 | int ret = -1; |
| 1014 | |
| 1015 | lbs_deb_enter(LBS_DEB_SDIO); |
| 1016 | sdio_claim_host(card->func); |
| 1017 | |
| 1018 | sdio_writeb(card->func, 0, CONFIGURATION_REG, &ret); |
| 1019 | if (ret) |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 1020 | netdev_err(priv->dev, "sdio_writeb failed!\n"); |
Amitkumar Karwar | 4912545 | 2009-09-30 20:04:38 -0700 | [diff] [blame] | 1021 | |
| 1022 | sdio_release_host(card->func); |
| 1023 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 1024 | return ret; |
| 1025 | |
| 1026 | } |
| 1027 | |
Daniel Drake | 9a821f5 | 2011-06-07 18:15:57 +0100 | [diff] [blame] | 1028 | static struct mmc_host *reset_host; |
| 1029 | |
| 1030 | static void if_sdio_reset_card_worker(struct work_struct *work) |
| 1031 | { |
| 1032 | /* |
| 1033 | * The actual reset operation must be run outside of lbs_thread. This |
| 1034 | * is because mmc_remove_host() will cause the device to be instantly |
| 1035 | * destroyed, and the libertas driver then needs to end lbs_thread, |
| 1036 | * leading to a deadlock. |
| 1037 | * |
| 1038 | * We run it in a workqueue totally independent from the if_sdio_card |
| 1039 | * instance for that reason. |
| 1040 | */ |
| 1041 | |
| 1042 | pr_info("Resetting card..."); |
| 1043 | mmc_remove_host(reset_host); |
| 1044 | mmc_add_host(reset_host); |
| 1045 | } |
| 1046 | static DECLARE_WORK(card_reset_work, if_sdio_reset_card_worker); |
| 1047 | |
| 1048 | static void if_sdio_reset_card(struct lbs_private *priv) |
| 1049 | { |
| 1050 | struct if_sdio_card *card = priv->card; |
| 1051 | |
| 1052 | if (work_pending(&card_reset_work)) |
| 1053 | return; |
| 1054 | |
| 1055 | reset_host = card->func->card->host; |
| 1056 | schedule_work(&card_reset_work); |
| 1057 | } |
| 1058 | |
Daniel Drake | 7e1f79a | 2011-08-01 16:43:27 +0100 | [diff] [blame^] | 1059 | static int if_sdio_power_save(struct lbs_private *priv) |
| 1060 | { |
| 1061 | struct if_sdio_card *card = priv->card; |
| 1062 | int ret; |
| 1063 | |
| 1064 | flush_workqueue(card->workqueue); |
| 1065 | |
| 1066 | ret = if_sdio_power_off(card); |
| 1067 | |
| 1068 | /* Let runtime PM know the card is powered off */ |
| 1069 | pm_runtime_put_sync(&card->func->dev); |
| 1070 | |
| 1071 | return ret; |
| 1072 | } |
| 1073 | |
| 1074 | static int if_sdio_power_restore(struct lbs_private *priv) |
| 1075 | { |
| 1076 | struct if_sdio_card *card = priv->card; |
| 1077 | |
| 1078 | /* Make sure the card will not be powered off by runtime PM */ |
| 1079 | pm_runtime_get_sync(&card->func->dev); |
| 1080 | |
| 1081 | return if_sdio_power_on(card); |
| 1082 | } |
| 1083 | |
| 1084 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1085 | /*******************************************************************/ |
| 1086 | /* SDIO callbacks */ |
| 1087 | /*******************************************************************/ |
| 1088 | |
| 1089 | static void if_sdio_interrupt(struct sdio_func *func) |
| 1090 | { |
| 1091 | int ret; |
| 1092 | struct if_sdio_card *card; |
| 1093 | u8 cause; |
| 1094 | |
| 1095 | lbs_deb_enter(LBS_DEB_SDIO); |
| 1096 | |
| 1097 | card = sdio_get_drvdata(func); |
| 1098 | |
| 1099 | cause = sdio_readb(card->func, IF_SDIO_H_INT_STATUS, &ret); |
Daniel Drake | d2ac49f | 2011-06-03 00:13:26 +0100 | [diff] [blame] | 1100 | if (ret || !cause) |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1101 | goto out; |
| 1102 | |
| 1103 | lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause); |
| 1104 | |
| 1105 | sdio_writeb(card->func, ~cause, IF_SDIO_H_INT_STATUS, &ret); |
| 1106 | if (ret) |
| 1107 | goto out; |
| 1108 | |
| 1109 | /* |
| 1110 | * Ignore the define name, this really means the card has |
| 1111 | * successfully received the command. |
| 1112 | */ |
Amitkumar Karwar | 4912545 | 2009-09-30 20:04:38 -0700 | [diff] [blame] | 1113 | card->priv->is_activity_detected = 1; |
David Woodhouse | e775ed7 | 2007-12-06 14:36:11 +0000 | [diff] [blame] | 1114 | if (cause & IF_SDIO_H_INT_DNLD) |
| 1115 | lbs_host_to_card_done(card->priv); |
| 1116 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1117 | |
| 1118 | if (cause & IF_SDIO_H_INT_UPLD) { |
| 1119 | ret = if_sdio_card_to_host(card); |
| 1120 | if (ret) |
| 1121 | goto out; |
| 1122 | } |
| 1123 | |
| 1124 | ret = 0; |
| 1125 | |
| 1126 | out: |
| 1127 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 1128 | } |
| 1129 | |
| 1130 | static int if_sdio_probe(struct sdio_func *func, |
| 1131 | const struct sdio_device_id *id) |
| 1132 | { |
| 1133 | struct if_sdio_card *card; |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 1134 | struct lbs_private *priv; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1135 | int ret, i; |
| 1136 | unsigned int model; |
| 1137 | struct if_sdio_packet *packet; |
| 1138 | |
| 1139 | lbs_deb_enter(LBS_DEB_SDIO); |
| 1140 | |
| 1141 | for (i = 0;i < func->card->num_info;i++) { |
| 1142 | if (sscanf(func->card->info[i], |
| 1143 | "802.11 SDIO ID: %x", &model) == 1) |
| 1144 | break; |
| 1145 | if (sscanf(func->card->info[i], |
| 1146 | "ID: %x", &model) == 1) |
| 1147 | break; |
Bing Zhao | e70a5ac | 2009-05-19 19:48:18 -0700 | [diff] [blame] | 1148 | if (!strcmp(func->card->info[i], "IBIS Wireless SDIO Card")) { |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 1149 | model = MODEL_8385; |
Bing Zhao | e70a5ac | 2009-05-19 19:48:18 -0700 | [diff] [blame] | 1150 | break; |
| 1151 | } |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | if (i == func->card->num_info) { |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame] | 1155 | pr_err("unable to identify card model\n"); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1156 | return -ENODEV; |
| 1157 | } |
| 1158 | |
| 1159 | card = kzalloc(sizeof(struct if_sdio_card), GFP_KERNEL); |
| 1160 | if (!card) |
| 1161 | return -ENOMEM; |
| 1162 | |
| 1163 | card->func = func; |
| 1164 | card->model = model; |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame] | 1165 | |
| 1166 | switch (card->model) { |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 1167 | case MODEL_8385: |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame] | 1168 | card->scratch_reg = IF_SDIO_SCRATCH_OLD; |
| 1169 | break; |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 1170 | case MODEL_8686: |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame] | 1171 | card->scratch_reg = IF_SDIO_SCRATCH; |
| 1172 | break; |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 1173 | case MODEL_8688: |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame] | 1174 | default: /* for newer chipsets */ |
| 1175 | card->scratch_reg = IF_SDIO_FW_STATUS; |
| 1176 | break; |
| 1177 | } |
| 1178 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1179 | spin_lock_init(&card->lock); |
Dan Williams | 9b02f41 | 2009-02-20 12:27:38 -0500 | [diff] [blame] | 1180 | card->workqueue = create_workqueue("libertas_sdio"); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1181 | INIT_WORK(&card->packet_worker, if_sdio_host_to_card_worker); |
| 1182 | |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 1183 | /* Check if we support this card */ |
| 1184 | for (i = 0; i < ARRAY_SIZE(fw_table); i++) { |
| 1185 | if (card->model == fw_table[i].model) |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1186 | break; |
| 1187 | } |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 1188 | if (i == ARRAY_SIZE(fw_table)) { |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame] | 1189 | pr_err("unknown card model 0x%x\n", card->model); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1190 | ret = -ENODEV; |
| 1191 | goto free; |
| 1192 | } |
| 1193 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1194 | sdio_set_drvdata(func, card); |
| 1195 | |
| 1196 | lbs_deb_sdio("class = 0x%X, vendor = 0x%X, " |
| 1197 | "device = 0x%X, model = 0x%X, ioport = 0x%X\n", |
| 1198 | func->class, func->vendor, func->device, |
| 1199 | model, (unsigned)card->ioport); |
| 1200 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1201 | |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 1202 | priv = lbs_add_card(card, &func->dev); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1203 | if (!priv) { |
| 1204 | ret = -ENOMEM; |
Daniel Drake | 7e1f79a | 2011-08-01 16:43:27 +0100 | [diff] [blame^] | 1205 | goto free; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | card->priv = priv; |
| 1209 | |
| 1210 | priv->card = card; |
| 1211 | priv->hw_host_to_card = if_sdio_host_to_card; |
Amitkumar Karwar | 4912545 | 2009-09-30 20:04:38 -0700 | [diff] [blame] | 1212 | priv->enter_deep_sleep = if_sdio_enter_deep_sleep; |
| 1213 | priv->exit_deep_sleep = if_sdio_exit_deep_sleep; |
| 1214 | priv->reset_deep_sleep_wakeup = if_sdio_reset_deep_sleep_wakeup; |
Daniel Drake | 9a821f5 | 2011-06-07 18:15:57 +0100 | [diff] [blame] | 1215 | priv->reset_card = if_sdio_reset_card; |
Daniel Drake | 7e1f79a | 2011-08-01 16:43:27 +0100 | [diff] [blame^] | 1216 | priv->power_save = if_sdio_power_save; |
| 1217 | priv->power_restore = if_sdio_power_restore; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1218 | |
Daniel Drake | 7e1f79a | 2011-08-01 16:43:27 +0100 | [diff] [blame^] | 1219 | ret = if_sdio_power_on(card); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1220 | if (ret) |
| 1221 | goto err_activate_card; |
| 1222 | |
Daniel Drake | 7e1f79a | 2011-08-01 16:43:27 +0100 | [diff] [blame^] | 1223 | ret = lbs_start_card(priv); |
| 1224 | if_sdio_power_off(card); |
| 1225 | if (ret) |
| 1226 | goto err_activate_card; |
| 1227 | |
| 1228 | /* Tell PM core that we don't need the card to be powered now */ |
| 1229 | pm_runtime_put_noidle(&func->dev); |
| 1230 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1231 | out: |
| 1232 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 1233 | |
| 1234 | return ret; |
| 1235 | |
| 1236 | err_activate_card: |
Dan Williams | 9b02f41 | 2009-02-20 12:27:38 -0500 | [diff] [blame] | 1237 | flush_workqueue(card->workqueue); |
| 1238 | lbs_remove_card(priv); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1239 | free: |
Dan Williams | 9b02f41 | 2009-02-20 12:27:38 -0500 | [diff] [blame] | 1240 | destroy_workqueue(card->workqueue); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1241 | while (card->packets) { |
| 1242 | packet = card->packets; |
| 1243 | card->packets = card->packets->next; |
| 1244 | kfree(packet); |
| 1245 | } |
| 1246 | |
Rusty Russell | 886275c | 2010-08-11 23:04:33 -0600 | [diff] [blame] | 1247 | if (card->helper_allocated) |
| 1248 | kfree(card->helper); |
| 1249 | if (card->firmware_allocated) |
| 1250 | kfree(card->firmware); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1251 | kfree(card); |
| 1252 | |
| 1253 | goto out; |
| 1254 | } |
| 1255 | |
| 1256 | static void if_sdio_remove(struct sdio_func *func) |
| 1257 | { |
| 1258 | struct if_sdio_card *card; |
| 1259 | struct if_sdio_packet *packet; |
| 1260 | |
| 1261 | lbs_deb_enter(LBS_DEB_SDIO); |
| 1262 | |
| 1263 | card = sdio_get_drvdata(func); |
| 1264 | |
Daniel Drake | 7e1f79a | 2011-08-01 16:43:27 +0100 | [diff] [blame^] | 1265 | /* Undo decrement done above in if_sdio_probe */ |
| 1266 | pm_runtime_get_noresume(&func->dev); |
| 1267 | |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 1268 | if (user_rmmod && (card->model == MODEL_8688)) { |
Bing Zhao | 6bc61f4 | 2009-06-01 18:04:36 -0700 | [diff] [blame] | 1269 | /* |
| 1270 | * FUNC_SHUTDOWN is required for SD8688 WLAN/BT |
| 1271 | * multiple functions |
| 1272 | */ |
| 1273 | struct cmd_header cmd; |
| 1274 | |
| 1275 | memset(&cmd, 0, sizeof(cmd)); |
| 1276 | |
| 1277 | lbs_deb_sdio("send function SHUTDOWN command\n"); |
| 1278 | if (__lbs_cmd(card->priv, CMD_FUNC_SHUTDOWN, |
| 1279 | &cmd, sizeof(cmd), lbs_cmd_copyback, |
| 1280 | (unsigned long) &cmd)) |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame] | 1281 | pr_alert("CMD_FUNC_SHUTDOWN cmd failed\n"); |
Bing Zhao | 6bc61f4 | 2009-06-01 18:04:36 -0700 | [diff] [blame] | 1282 | } |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 1283 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1284 | |
| 1285 | lbs_deb_sdio("call remove card\n"); |
Bing Zhao | 6bc61f4 | 2009-06-01 18:04:36 -0700 | [diff] [blame] | 1286 | lbs_stop_card(card->priv); |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 1287 | lbs_remove_card(card->priv); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1288 | |
Dan Williams | 9b02f41 | 2009-02-20 12:27:38 -0500 | [diff] [blame] | 1289 | flush_workqueue(card->workqueue); |
| 1290 | destroy_workqueue(card->workqueue); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1291 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1292 | while (card->packets) { |
| 1293 | packet = card->packets; |
| 1294 | card->packets = card->packets->next; |
| 1295 | kfree(packet); |
| 1296 | } |
| 1297 | |
Rusty Russell | 886275c | 2010-08-11 23:04:33 -0600 | [diff] [blame] | 1298 | if (card->helper_allocated) |
| 1299 | kfree(card->helper); |
| 1300 | if (card->firmware_allocated) |
| 1301 | kfree(card->firmware); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1302 | kfree(card); |
| 1303 | |
| 1304 | lbs_deb_leave(LBS_DEB_SDIO); |
| 1305 | } |
| 1306 | |
Amitkumar Karwar | 66fceb6 | 2010-05-19 03:24:38 -0700 | [diff] [blame] | 1307 | static int if_sdio_suspend(struct device *dev) |
| 1308 | { |
| 1309 | struct sdio_func *func = dev_to_sdio_func(dev); |
| 1310 | int ret; |
| 1311 | struct if_sdio_card *card = sdio_get_drvdata(func); |
| 1312 | |
| 1313 | mmc_pm_flag_t flags = sdio_get_host_pm_caps(func); |
| 1314 | |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 1315 | dev_info(dev, "%s: suspend: PM flags = 0x%x\n", |
| 1316 | sdio_func_id(func), flags); |
Amitkumar Karwar | 66fceb6 | 2010-05-19 03:24:38 -0700 | [diff] [blame] | 1317 | |
| 1318 | /* If we aren't being asked to wake on anything, we should bail out |
| 1319 | * and let the SD stack power down the card. |
| 1320 | */ |
| 1321 | if (card->priv->wol_criteria == EHS_REMOVE_WAKEUP) { |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 1322 | dev_info(dev, "Suspend without wake params -- powering down card\n"); |
Amitkumar Karwar | 66fceb6 | 2010-05-19 03:24:38 -0700 | [diff] [blame] | 1323 | return -ENOSYS; |
| 1324 | } |
| 1325 | |
| 1326 | if (!(flags & MMC_PM_KEEP_POWER)) { |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 1327 | dev_err(dev, "%s: cannot remain alive while host is suspended\n", |
| 1328 | sdio_func_id(func)); |
Amitkumar Karwar | 66fceb6 | 2010-05-19 03:24:38 -0700 | [diff] [blame] | 1329 | return -ENOSYS; |
| 1330 | } |
| 1331 | |
| 1332 | ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER); |
| 1333 | if (ret) |
| 1334 | return ret; |
| 1335 | |
| 1336 | ret = lbs_suspend(card->priv); |
| 1337 | if (ret) |
| 1338 | return ret; |
| 1339 | |
| 1340 | return sdio_set_host_pm_flags(func, MMC_PM_WAKE_SDIO_IRQ); |
| 1341 | } |
| 1342 | |
| 1343 | static int if_sdio_resume(struct device *dev) |
| 1344 | { |
| 1345 | struct sdio_func *func = dev_to_sdio_func(dev); |
| 1346 | struct if_sdio_card *card = sdio_get_drvdata(func); |
| 1347 | int ret; |
| 1348 | |
Joe Perches | f3a57fd | 2011-05-02 16:49:15 -0700 | [diff] [blame] | 1349 | dev_info(dev, "%s: resume: we're back\n", sdio_func_id(func)); |
Amitkumar Karwar | 66fceb6 | 2010-05-19 03:24:38 -0700 | [diff] [blame] | 1350 | |
| 1351 | ret = lbs_resume(card->priv); |
| 1352 | |
| 1353 | return ret; |
| 1354 | } |
| 1355 | |
| 1356 | static const struct dev_pm_ops if_sdio_pm_ops = { |
| 1357 | .suspend = if_sdio_suspend, |
| 1358 | .resume = if_sdio_resume, |
| 1359 | }; |
| 1360 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1361 | static struct sdio_driver if_sdio_driver = { |
| 1362 | .name = "libertas_sdio", |
| 1363 | .id_table = if_sdio_ids, |
| 1364 | .probe = if_sdio_probe, |
| 1365 | .remove = if_sdio_remove, |
Amitkumar Karwar | 66fceb6 | 2010-05-19 03:24:38 -0700 | [diff] [blame] | 1366 | .drv = { |
| 1367 | .pm = &if_sdio_pm_ops, |
| 1368 | }, |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1369 | }; |
| 1370 | |
| 1371 | /*******************************************************************/ |
| 1372 | /* Module functions */ |
| 1373 | /*******************************************************************/ |
| 1374 | |
Andres Salomon | 4fb910f | 2007-11-20 17:43:45 -0500 | [diff] [blame] | 1375 | static int __init if_sdio_init_module(void) |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1376 | { |
| 1377 | int ret = 0; |
| 1378 | |
| 1379 | lbs_deb_enter(LBS_DEB_SDIO); |
| 1380 | |
| 1381 | printk(KERN_INFO "libertas_sdio: Libertas SDIO driver\n"); |
| 1382 | printk(KERN_INFO "libertas_sdio: Copyright Pierre Ossman\n"); |
| 1383 | |
| 1384 | ret = sdio_register_driver(&if_sdio_driver); |
| 1385 | |
Bing Zhao | 6bc61f4 | 2009-06-01 18:04:36 -0700 | [diff] [blame] | 1386 | /* Clear the flag in case user removes the card. */ |
| 1387 | user_rmmod = 0; |
| 1388 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1389 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 1390 | |
| 1391 | return ret; |
| 1392 | } |
| 1393 | |
Andres Salomon | 4fb910f | 2007-11-20 17:43:45 -0500 | [diff] [blame] | 1394 | static void __exit if_sdio_exit_module(void) |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1395 | { |
| 1396 | lbs_deb_enter(LBS_DEB_SDIO); |
| 1397 | |
Bing Zhao | 6bc61f4 | 2009-06-01 18:04:36 -0700 | [diff] [blame] | 1398 | /* Set the flag as user is removing this module. */ |
| 1399 | user_rmmod = 1; |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 1400 | |
Daniel Drake | 9a821f5 | 2011-06-07 18:15:57 +0100 | [diff] [blame] | 1401 | cancel_work_sync(&card_reset_work); |
| 1402 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1403 | sdio_unregister_driver(&if_sdio_driver); |
| 1404 | |
| 1405 | lbs_deb_leave(LBS_DEB_SDIO); |
| 1406 | } |
| 1407 | |
| 1408 | module_init(if_sdio_init_module); |
| 1409 | module_exit(if_sdio_exit_module); |
| 1410 | |
| 1411 | MODULE_DESCRIPTION("Libertas SDIO WLAN Driver"); |
| 1412 | MODULE_AUTHOR("Pierre Ossman"); |
| 1413 | MODULE_LICENSE("GPL"); |