blob: c3462b75bd080d4f7e9d72dd979f86bd5c894319 [file] [log] [blame]
Arend van Spriel5b435de2011-10-05 13:19:03 +02001/*
2 * Copyright (c) 2010 Broadcom Corporation
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
Joe Perches02f77192012-01-15 00:38:44 -080016
Arend van Spriel5b435de2011-10-05 13:19:03 +020017#include <linux/types.h>
18#include <linux/netdevice.h>
19#include <linux/mmc/sdio.h>
20#include <linux/mmc/core.h>
21#include <linux/mmc/sdio_func.h>
22#include <linux/mmc/sdio_ids.h>
23#include <linux/mmc/card.h>
24#include <linux/suspend.h>
25#include <linux/errno.h>
26#include <linux/sched.h> /* request_irq() */
Stephen Rothwellb7a57e72011-10-12 21:35:07 +020027#include <linux/module.h>
Franky Linba89bf12012-04-27 18:56:59 -070028#include <linux/platform_device.h>
Hante Meuleman668761a2013-04-12 10:55:55 +020029#include <linux/platform_data/brcmfmac-sdio.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020030#include <net/cfg80211.h>
31
32#include <defs.h>
33#include <brcm_hw_ids.h>
34#include <brcmu_utils.h>
35#include <brcmu_wifi.h>
36#include "sdio_host.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020037#include "dhd_dbg.h"
Franky Lina8a363a2011-12-16 18:37:13 -080038#include "dhd_bus.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020039
40#define SDIO_VENDOR_ID_BROADCOM 0x02d0
41
42#define DMA_ALIGN_MASK 0x03
43
Hante Meuleman369508c2013-04-11 13:28:54 +020044#define SDIO_DEVICE_ID_BROADCOM_43143 43143
Franky Lin4a1c02c2012-08-30 19:42:59 +020045#define SDIO_DEVICE_ID_BROADCOM_43241 0x4324
Arend van Spriel5b435de2011-10-05 13:19:03 +020046#define SDIO_DEVICE_ID_BROADCOM_4329 0x4329
Franky Lince2d7d72011-12-08 15:06:39 -080047#define SDIO_DEVICE_ID_BROADCOM_4330 0x4330
Franky Lin85a4a1c2012-06-26 21:26:39 +020048#define SDIO_DEVICE_ID_BROADCOM_4334 0x4334
Franky Lin6a1c7482013-04-11 13:28:53 +020049#define SDIO_DEVICE_ID_BROADCOM_4335 0x4335
Arend van Spriel5b435de2011-10-05 13:19:03 +020050
51#define SDIO_FUNC1_BLOCKSIZE 64
52#define SDIO_FUNC2_BLOCKSIZE 512
53
54/* devices we support, null terminated */
55static const struct sdio_device_id brcmf_sdmmc_ids[] = {
Hante Meuleman369508c2013-04-11 13:28:54 +020056 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_43143)},
Franky Lin4a1c02c2012-08-30 19:42:59 +020057 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_43241)},
Arend van Spriel5b435de2011-10-05 13:19:03 +020058 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4329)},
Franky Lince2d7d72011-12-08 15:06:39 -080059 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4330)},
Franky Lin85a4a1c2012-06-26 21:26:39 +020060 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4334)},
Franky Lin6a1c7482013-04-11 13:28:53 +020061 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4335)},
Arend van Spriel5b435de2011-10-05 13:19:03 +020062 { /* end: all zeroes */ },
63};
64MODULE_DEVICE_TABLE(sdio, brcmf_sdmmc_ids);
65
Hante Meuleman668761a2013-04-12 10:55:55 +020066static struct brcmfmac_sdio_platform_data *brcmfmac_sdio_pdata;
67
Franky Linba89bf12012-04-27 18:56:59 -070068
Franky Lin78b3f1c2013-06-18 13:29:28 +020069bool
Arend van Spriel5b435de2011-10-05 13:19:03 +020070brcmf_pm_resume_error(struct brcmf_sdio_dev *sdiodev)
71{
72 bool is_err = false;
73#ifdef CONFIG_PM_SLEEP
74 is_err = atomic_read(&sdiodev->suspend);
75#endif
76 return is_err;
77}
78
Franky Lin78b3f1c2013-06-18 13:29:28 +020079void
Arend van Spriel5b435de2011-10-05 13:19:03 +020080brcmf_pm_resume_wait(struct brcmf_sdio_dev *sdiodev, wait_queue_head_t *wq)
81{
82#ifdef CONFIG_PM_SLEEP
83 int retry = 0;
84 while (atomic_read(&sdiodev->suspend) && retry++ != 30)
85 wait_event_timeout(*wq, false, HZ/100);
86#endif
87}
88
89static inline int brcmf_sdioh_f0_write_byte(struct brcmf_sdio_dev *sdiodev,
90 uint regaddr, u8 *byte)
91{
92 struct sdio_func *sdfunc = sdiodev->func[0];
93 int err_ret;
94
95 /*
96 * Can only directly write to some F0 registers.
97 * Handle F2 enable/disable and Abort command
98 * as a special case.
99 */
100 if (regaddr == SDIO_CCCR_IOEx) {
101 sdfunc = sdiodev->func[2];
102 if (sdfunc) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200103 if (*byte & SDIO_FUNC_ENABLE_2) {
104 /* Enable Function 2 */
105 err_ret = sdio_enable_func(sdfunc);
106 if (err_ret)
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100107 brcmf_err("enable F2 failed:%d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200108 err_ret);
109 } else {
110 /* Disable Function 2 */
111 err_ret = sdio_disable_func(sdfunc);
112 if (err_ret)
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100113 brcmf_err("Disable F2 failed:%d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200114 err_ret);
115 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200116 }
Franky Linba89bf12012-04-27 18:56:59 -0700117 } else if ((regaddr == SDIO_CCCR_ABORT) ||
118 (regaddr == SDIO_CCCR_IENx)) {
Franky Lin1cc26992012-04-23 14:24:52 -0700119 sdfunc = kmemdup(sdiodev->func[0], sizeof(struct sdio_func),
120 GFP_KERNEL);
121 if (!sdfunc)
122 return -ENOMEM;
123 sdfunc->num = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200124 sdio_writeb(sdfunc, *byte, regaddr, &err_ret);
Franky Lin1cc26992012-04-23 14:24:52 -0700125 kfree(sdfunc);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200126 } else if (regaddr < 0xF0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100127 brcmf_err("F0 Wr:0x%02x: write disallowed\n", regaddr);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200128 err_ret = -EPERM;
129 } else {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200130 sdio_f0_writeb(sdfunc, *byte, regaddr, &err_ret);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200131 }
132
133 return err_ret;
134}
135
136int brcmf_sdioh_request_byte(struct brcmf_sdio_dev *sdiodev, uint rw, uint func,
137 uint regaddr, u8 *byte)
138{
139 int err_ret;
140
Arend van Sprielc3203372013-04-03 12:40:44 +0200141 brcmf_dbg(SDIO, "rw=%d, func=%d, addr=0x%05x\n", rw, func, regaddr);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200142
143 brcmf_pm_resume_wait(sdiodev, &sdiodev->request_byte_wait);
144 if (brcmf_pm_resume_error(sdiodev))
145 return -EIO;
146
147 if (rw && func == 0) {
148 /* handle F0 separately */
149 err_ret = brcmf_sdioh_f0_write_byte(sdiodev, regaddr, byte);
150 } else {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200151 if (rw) /* CMD52 Write */
152 sdio_writeb(sdiodev->func[func], *byte, regaddr,
153 &err_ret);
154 else if (func == 0) {
155 *byte = sdio_f0_readb(sdiodev->func[func], regaddr,
156 &err_ret);
157 } else {
158 *byte = sdio_readb(sdiodev->func[func], regaddr,
159 &err_ret);
160 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200161 }
162
163 if (err_ret)
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100164 brcmf_err("Failed to %s byte F%d:@0x%05x=%02x, Err: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200165 rw ? "write" : "read", func, regaddr, *byte, err_ret);
166
167 return err_ret;
168}
169
170int brcmf_sdioh_request_word(struct brcmf_sdio_dev *sdiodev,
171 uint rw, uint func, uint addr, u32 *word,
172 uint nbytes)
173{
174 int err_ret = -EIO;
175
176 if (func == 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100177 brcmf_err("Only CMD52 allowed to F0\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200178 return -EINVAL;
179 }
180
Arend van Sprielc3203372013-04-03 12:40:44 +0200181 brcmf_dbg(SDIO, "rw=%d, func=%d, addr=0x%05x, nbytes=%d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200182 rw, func, addr, nbytes);
183
184 brcmf_pm_resume_wait(sdiodev, &sdiodev->request_word_wait);
185 if (brcmf_pm_resume_error(sdiodev))
186 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200187
188 if (rw) { /* CMD52 Write */
189 if (nbytes == 4)
190 sdio_writel(sdiodev->func[func], *word, addr,
191 &err_ret);
192 else if (nbytes == 2)
193 sdio_writew(sdiodev->func[func], (*word & 0xFFFF),
194 addr, &err_ret);
195 else
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100196 brcmf_err("Invalid nbytes: %d\n", nbytes);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200197 } else { /* CMD52 Read */
198 if (nbytes == 4)
199 *word = sdio_readl(sdiodev->func[func], addr, &err_ret);
200 else if (nbytes == 2)
201 *word = sdio_readw(sdiodev->func[func], addr,
202 &err_ret) & 0xFFFF;
203 else
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100204 brcmf_err("Invalid nbytes: %d\n", nbytes);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200205 }
206
Arend van Spriel5b435de2011-10-05 13:19:03 +0200207 if (err_ret)
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100208 brcmf_err("Failed to %s word, Err: 0x%08x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200209 rw ? "write" : "read", err_ret);
210
211 return err_ret;
212}
213
Arend van Spriel5b435de2011-10-05 13:19:03 +0200214static int brcmf_sdioh_get_cisaddr(struct brcmf_sdio_dev *sdiodev, u32 regaddr)
215{
216 /* read 24 bits and return valid 17 bit addr */
Franky Lind8b3fc52012-05-04 18:27:38 -0700217 int i, ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200218 u32 scratch, regdata;
219 __le32 scratch_le;
220 u8 *ptr = (u8 *)&scratch_le;
221
222 for (i = 0; i < 3; i++) {
Franky Lind8b3fc52012-05-04 18:27:38 -0700223 regdata = brcmf_sdio_regrl(sdiodev, regaddr, &ret);
224 if (ret != 0)
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100225 brcmf_err("Can't read!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200226
227 *ptr++ = (u8) regdata;
228 regaddr++;
229 }
230
231 /* Only the lower 17-bits are valid */
232 scratch = le32_to_cpu(scratch_le);
233 scratch &= 0x0001FFFF;
234 return scratch;
235}
236
237static int brcmf_sdioh_enablefuncs(struct brcmf_sdio_dev *sdiodev)
238{
239 int err_ret;
240 u32 fbraddr;
241 u8 func;
242
Arend van Sprielc3203372013-04-03 12:40:44 +0200243 brcmf_dbg(SDIO, "\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200244
245 /* Get the Card's common CIS address */
246 sdiodev->func_cis_ptr[0] = brcmf_sdioh_get_cisaddr(sdiodev,
247 SDIO_CCCR_CIS);
Arend van Sprielc3203372013-04-03 12:40:44 +0200248 brcmf_dbg(SDIO, "Card's Common CIS Ptr = 0x%x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200249 sdiodev->func_cis_ptr[0]);
250
251 /* Get the Card's function CIS (for each function) */
252 for (fbraddr = SDIO_FBR_BASE(1), func = 1;
253 func <= sdiodev->num_funcs; func++, fbraddr += SDIOD_FBR_SIZE) {
254 sdiodev->func_cis_ptr[func] =
255 brcmf_sdioh_get_cisaddr(sdiodev, SDIO_FBR_CIS + fbraddr);
Arend van Sprielc3203372013-04-03 12:40:44 +0200256 brcmf_dbg(SDIO, "Function %d CIS Ptr = 0x%x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200257 func, sdiodev->func_cis_ptr[func]);
258 }
259
260 /* Enable Function 1 */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200261 err_ret = sdio_enable_func(sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200262 if (err_ret)
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100263 brcmf_err("Failed to enable F1 Err: 0x%08x\n", err_ret);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200264
265 return false;
266}
267
268/*
269 * Public entry points & extern's
270 */
271int brcmf_sdioh_attach(struct brcmf_sdio_dev *sdiodev)
272{
273 int err_ret = 0;
274
Arend van Sprielc3203372013-04-03 12:40:44 +0200275 brcmf_dbg(SDIO, "\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200276
277 sdiodev->num_funcs = 2;
278
279 sdio_claim_host(sdiodev->func[1]);
Franky Lin38b0b0d2012-11-05 16:22:24 -0800280
Arend van Spriel5b435de2011-10-05 13:19:03 +0200281 err_ret = sdio_set_block_size(sdiodev->func[1], SDIO_FUNC1_BLOCKSIZE);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200282 if (err_ret) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100283 brcmf_err("Failed to set F1 blocksize\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200284 goto out;
285 }
286
Arend van Spriel5b435de2011-10-05 13:19:03 +0200287 err_ret = sdio_set_block_size(sdiodev->func[2], SDIO_FUNC2_BLOCKSIZE);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200288 if (err_ret) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100289 brcmf_err("Failed to set F2 blocksize\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200290 goto out;
291 }
292
293 brcmf_sdioh_enablefuncs(sdiodev);
294
295out:
Franky Lin38b0b0d2012-11-05 16:22:24 -0800296 sdio_release_host(sdiodev->func[1]);
Arend van Sprielc3203372013-04-03 12:40:44 +0200297 brcmf_dbg(SDIO, "Done\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200298 return err_ret;
299}
300
301void brcmf_sdioh_detach(struct brcmf_sdio_dev *sdiodev)
302{
Arend van Sprielc3203372013-04-03 12:40:44 +0200303 brcmf_dbg(SDIO, "\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200304
305 /* Disable Function 2 */
306 sdio_claim_host(sdiodev->func[2]);
307 sdio_disable_func(sdiodev->func[2]);
308 sdio_release_host(sdiodev->func[2]);
309
310 /* Disable Function 1 */
311 sdio_claim_host(sdiodev->func[1]);
312 sdio_disable_func(sdiodev->func[1]);
313 sdio_release_host(sdiodev->func[1]);
314
315}
316
317static int brcmf_ops_sdio_probe(struct sdio_func *func,
Hante Meuleman5b3c1832012-11-14 18:46:20 -0800318 const struct sdio_device_id *id)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200319{
Hante Meuleman5b3c1832012-11-14 18:46:20 -0800320 int err;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200321 struct brcmf_sdio_dev *sdiodev;
Franky Lin655713b2011-11-22 17:21:51 -0800322 struct brcmf_bus *bus_if;
Franky Linba89bf12012-04-27 18:56:59 -0700323
Arend van Sprielc3203372013-04-03 12:40:44 +0200324 brcmf_dbg(SDIO, "Enter\n");
325 brcmf_dbg(SDIO, "Class=%x\n", func->class);
326 brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor);
327 brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device);
328 brcmf_dbg(SDIO, "Function#: %d\n", func->num);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200329
Hante Meuleman5b3c1832012-11-14 18:46:20 -0800330 /* Consume func num 1 but dont do anything with it. */
331 if (func->num == 1)
332 return 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200333
Hante Meuleman5b3c1832012-11-14 18:46:20 -0800334 /* Ignore anything but func 2 */
335 if (func->num != 2)
336 return -ENODEV;
337
338 bus_if = kzalloc(sizeof(struct brcmf_bus), GFP_KERNEL);
339 if (!bus_if)
340 return -ENOMEM;
341 sdiodev = kzalloc(sizeof(struct brcmf_sdio_dev), GFP_KERNEL);
342 if (!sdiodev) {
343 kfree(bus_if);
344 return -ENOMEM;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200345 }
346
Hante Meuleman5b3c1832012-11-14 18:46:20 -0800347 sdiodev->func[0] = func->card->sdio_func[0];
348 sdiodev->func[1] = func->card->sdio_func[0];
349 sdiodev->func[2] = func;
Franky Linba89bf12012-04-27 18:56:59 -0700350
Hante Meuleman5b3c1832012-11-14 18:46:20 -0800351 sdiodev->bus_if = bus_if;
352 bus_if->bus_priv.sdio = sdiodev;
Hante Meuleman5b3c1832012-11-14 18:46:20 -0800353 dev_set_drvdata(&func->dev, bus_if);
354 dev_set_drvdata(&sdiodev->func[1]->dev, bus_if);
355 sdiodev->dev = &sdiodev->func[1]->dev;
Hante Meuleman668761a2013-04-12 10:55:55 +0200356 sdiodev->pdata = brcmfmac_sdio_pdata;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200357
Hante Meuleman5b3c1832012-11-14 18:46:20 -0800358 atomic_set(&sdiodev->suspend, false);
359 init_waitqueue_head(&sdiodev->request_byte_wait);
360 init_waitqueue_head(&sdiodev->request_word_wait);
Hante Meuleman5b3c1832012-11-14 18:46:20 -0800361 init_waitqueue_head(&sdiodev->request_buffer_wait);
Franky Lind76d1c82011-12-08 15:06:37 -0800362
Arend van Sprielc3203372013-04-03 12:40:44 +0200363 brcmf_dbg(SDIO, "F2 found, calling brcmf_sdio_probe...\n");
Hante Meuleman5b3c1832012-11-14 18:46:20 -0800364 err = brcmf_sdio_probe(sdiodev);
365 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100366 brcmf_err("F2 error, probe failed %d...\n", err);
Hante Meuleman5b3c1832012-11-14 18:46:20 -0800367 goto fail;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200368 }
Arend van Sprielc3203372013-04-03 12:40:44 +0200369 brcmf_dbg(SDIO, "F2 init completed...\n");
Hante Meuleman5b3c1832012-11-14 18:46:20 -0800370 return 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200371
Hante Meuleman5b3c1832012-11-14 18:46:20 -0800372fail:
373 dev_set_drvdata(&func->dev, NULL);
374 dev_set_drvdata(&sdiodev->func[1]->dev, NULL);
375 kfree(sdiodev);
376 kfree(bus_if);
377 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200378}
379
380static void brcmf_ops_sdio_remove(struct sdio_func *func)
381{
Franky Lin655713b2011-11-22 17:21:51 -0800382 struct brcmf_bus *bus_if;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200383 struct brcmf_sdio_dev *sdiodev;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200384
Arend van Sprielc3203372013-04-03 12:40:44 +0200385 brcmf_dbg(SDIO, "Enter\n");
386 brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor);
387 brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device);
388 brcmf_dbg(SDIO, "Function: %d\n", func->num);
Hante Meuleman5b3c1832012-11-14 18:46:20 -0800389
390 if (func->num != 1 && func->num != 2)
391 return;
392
393 bus_if = dev_get_drvdata(&func->dev);
394 if (bus_if) {
Arend van Spriel0a332e42012-02-09 21:09:02 +0100395 sdiodev = bus_if->bus_priv.sdio;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200396 brcmf_sdio_remove(sdiodev);
Hante Meuleman5b3c1832012-11-14 18:46:20 -0800397
398 dev_set_drvdata(&sdiodev->func[1]->dev, NULL);
399 dev_set_drvdata(&sdiodev->func[2]->dev, NULL);
400
Franky Lin655713b2011-11-22 17:21:51 -0800401 kfree(bus_if);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200402 kfree(sdiodev);
403 }
Hante Meuleman5b3c1832012-11-14 18:46:20 -0800404
Arend van Sprielc3203372013-04-03 12:40:44 +0200405 brcmf_dbg(SDIO, "Exit\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200406}
407
408#ifdef CONFIG_PM_SLEEP
409static int brcmf_sdio_suspend(struct device *dev)
410{
411 mmc_pm_flag_t sdio_flags;
Hante Meuleman5b3c1832012-11-14 18:46:20 -0800412 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
413 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200414 int ret = 0;
415
Arend van Sprielc3203372013-04-03 12:40:44 +0200416 brcmf_dbg(SDIO, "\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200417
Arend van Spriel5b435de2011-10-05 13:19:03 +0200418 atomic_set(&sdiodev->suspend, true);
419
420 sdio_flags = sdio_get_host_pm_caps(sdiodev->func[1]);
421 if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100422 brcmf_err("Host can't keep power while suspended\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200423 return -EINVAL;
424 }
425
426 ret = sdio_set_host_pm_flags(sdiodev->func[1], MMC_PM_KEEP_POWER);
427 if (ret) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100428 brcmf_err("Failed to set pm_flags\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200429 return ret;
430 }
431
432 brcmf_sdio_wdtmr_enable(sdiodev, false);
433
434 return ret;
435}
436
437static int brcmf_sdio_resume(struct device *dev)
438{
Hante Meuleman5b3c1832012-11-14 18:46:20 -0800439 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
440 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200441
Arend van Spriel5b435de2011-10-05 13:19:03 +0200442 brcmf_sdio_wdtmr_enable(sdiodev, true);
443 atomic_set(&sdiodev->suspend, false);
444 return 0;
445}
446
447static const struct dev_pm_ops brcmf_sdio_pm_ops = {
448 .suspend = brcmf_sdio_suspend,
449 .resume = brcmf_sdio_resume,
450};
451#endif /* CONFIG_PM_SLEEP */
452
453static struct sdio_driver brcmf_sdmmc_driver = {
454 .probe = brcmf_ops_sdio_probe,
455 .remove = brcmf_ops_sdio_remove,
Hante Meuleman668761a2013-04-12 10:55:55 +0200456 .name = BRCMFMAC_SDIO_PDATA_NAME,
Arend van Spriel5b435de2011-10-05 13:19:03 +0200457 .id_table = brcmf_sdmmc_ids,
458#ifdef CONFIG_PM_SLEEP
459 .drv = {
460 .pm = &brcmf_sdio_pm_ops,
461 },
462#endif /* CONFIG_PM_SLEEP */
463};
464
Franky Linba89bf12012-04-27 18:56:59 -0700465static int brcmf_sdio_pd_probe(struct platform_device *pdev)
466{
Hante Meuleman668761a2013-04-12 10:55:55 +0200467 brcmf_dbg(SDIO, "Enter\n");
Franky Linba89bf12012-04-27 18:56:59 -0700468
Hante Meuleman668761a2013-04-12 10:55:55 +0200469 brcmfmac_sdio_pdata = pdev->dev.platform_data;
Franky Linba89bf12012-04-27 18:56:59 -0700470
Hante Meuleman668761a2013-04-12 10:55:55 +0200471 if (brcmfmac_sdio_pdata->power_on)
472 brcmfmac_sdio_pdata->power_on();
Franky Linba89bf12012-04-27 18:56:59 -0700473
Arend van Sprieldb4efbb2013-09-25 12:11:01 +0200474 return 0;
Franky Linba89bf12012-04-27 18:56:59 -0700475}
476
Hante Meuleman668761a2013-04-12 10:55:55 +0200477static int brcmf_sdio_pd_remove(struct platform_device *pdev)
478{
479 brcmf_dbg(SDIO, "Enter\n");
480
481 if (brcmfmac_sdio_pdata->power_off)
482 brcmfmac_sdio_pdata->power_off();
483
484 sdio_unregister_driver(&brcmf_sdmmc_driver);
485
486 return 0;
487}
488
Franky Linba89bf12012-04-27 18:56:59 -0700489static struct platform_driver brcmf_sdio_pd = {
Hante Meuleman668761a2013-04-12 10:55:55 +0200490 .remove = brcmf_sdio_pd_remove,
Franky Linba89bf12012-04-27 18:56:59 -0700491 .driver = {
Libo Chenf91f5f02013-05-21 10:42:40 +0800492 .name = BRCMFMAC_SDIO_PDATA_NAME,
493 .owner = THIS_MODULE,
Franky Linba89bf12012-04-27 18:56:59 -0700494 }
495};
496
Arend van Sprieldb4efbb2013-09-25 12:11:01 +0200497void brcmf_sdio_register(void)
498{
499 int ret;
500
501 ret = sdio_register_driver(&brcmf_sdmmc_driver);
502 if (ret)
503 brcmf_err("sdio_register_driver failed: %d\n", ret);
504}
505
Franky Linba89bf12012-04-27 18:56:59 -0700506void brcmf_sdio_exit(void)
507{
Arend van Sprielc3203372013-04-03 12:40:44 +0200508 brcmf_dbg(SDIO, "Enter\n");
Franky Linba89bf12012-04-27 18:56:59 -0700509
Hante Meuleman668761a2013-04-12 10:55:55 +0200510 if (brcmfmac_sdio_pdata)
511 platform_driver_unregister(&brcmf_sdio_pd);
512 else
513 sdio_unregister_driver(&brcmf_sdmmc_driver);
Franky Linba89bf12012-04-27 18:56:59 -0700514}
515
Arend van Sprieldb4efbb2013-09-25 12:11:01 +0200516void __init brcmf_sdio_init(void)
Franky Linba89bf12012-04-27 18:56:59 -0700517{
518 int ret;
519
Arend van Sprielc3203372013-04-03 12:40:44 +0200520 brcmf_dbg(SDIO, "Enter\n");
Franky Linba89bf12012-04-27 18:56:59 -0700521
Hante Meuleman668761a2013-04-12 10:55:55 +0200522 ret = platform_driver_probe(&brcmf_sdio_pd, brcmf_sdio_pd_probe);
Arend van Sprieldb4efbb2013-09-25 12:11:01 +0200523 if (ret == -ENODEV)
524 brcmf_dbg(SDIO, "No platform data available.\n");
Franky Linba89bf12012-04-27 18:56:59 -0700525}