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