blob: 63c8701dedcf589e8ce0a931868dc8f41b1ae802 [file] [log] [blame]
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001/* ////////////////////////////////////////////////////////////////////////// */
2/* */
3/* Copyright (c) Atmel Corporation. All rights reserved. */
4/* */
5/* Module Name: wilc_sdio.c */
6/* */
7/* */
8/* //////////////////////////////////////////////////////////////////////////// */
9
Chaehyun Lim0c9fc332015-09-30 17:52:14 +090010#include <linux/string.h>
Johnny Kimc5c77ba2015-05-11 14:30:56 +090011#include "wilc_wlan_if.h"
12#include "wilc_wlan.h"
Glen Lee9c800322015-11-06 18:40:22 +090013#include "wilc_wfi_netdevice.h"
Glen Lee35a45692015-12-21 14:18:29 +090014#include <linux/mmc/sdio_func.h>
15#include <linux/mmc/card.h>
16#include <linux/mmc/sdio_ids.h>
17#include <linux/mmc/sdio.h>
18#include <linux/mmc/host.h>
19#include <linux/of_gpio.h>
20
21#define SDIO_MODALIAS "wilc1000_sdio"
22
23#define SDIO_VENDOR_ID_WILC 0x0296
24#define SDIO_DEVICE_ID_WILC 0x5347
25
26static const struct sdio_device_id wilc_sdio_ids[] = {
27 { SDIO_DEVICE(SDIO_VENDOR_ID_WILC, SDIO_DEVICE_ID_WILC) },
28 { },
29};
Johnny Kimc5c77ba2015-05-11 14:30:56 +090030
Tony Cho229d7402015-07-28 17:47:26 +090031#define WILC_SDIO_BLOCK_SIZE 512
Johnny Kimc5c77ba2015-05-11 14:30:56 +090032
Bhumika Goyal6a707a92016-02-21 03:12:11 +053033struct wilc_sdio {
Arnd Bergmannc4d139c2015-11-16 15:05:04 +010034 bool irq_gpio;
Chaehyun Limfbc2fe12015-09-15 14:06:16 +090035 u32 block_size;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090036 int nint;
37#define MAX_NUN_INT_THRPT_ENH2 (5) /* Max num interrupts allowed in registers 0xf7, 0xf8 */
38 int has_thrpt_enh3;
Bhumika Goyal6a707a92016-02-21 03:12:11 +053039};
Johnny Kimc5c77ba2015-05-11 14:30:56 +090040
Bhumika Goyal6a707a92016-02-21 03:12:11 +053041static struct wilc_sdio g_sdio;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090042
Glen Lee49dcd0d2015-11-18 15:11:26 +090043static int sdio_write_reg(struct wilc *wilc, u32 addr, u32 data);
44static int sdio_read_reg(struct wilc *wilc, u32 addr, u32 *data);
Glen Lee5397cbc2016-01-25 16:35:09 +090045static int sdio_init(struct wilc *wilc, bool resume);
Johnny Kimc5c77ba2015-05-11 14:30:56 +090046
Glen Lee35a45692015-12-21 14:18:29 +090047static void wilc_sdio_interrupt(struct sdio_func *func)
48{
49 sdio_release_host(func);
50 wilc_handle_isr(sdio_get_drvdata(func));
51 sdio_claim_host(func);
52}
53
Chaehyun Lima91632f2016-03-03 14:28:20 +090054static int wilc_sdio_cmd52(struct wilc *wilc, struct sdio_cmd52 *cmd)
Glen Lee35a45692015-12-21 14:18:29 +090055{
56 struct sdio_func *func = container_of(wilc->dev, struct sdio_func, dev);
57 int ret;
58 u8 data;
59
60 sdio_claim_host(func);
61
62 func->num = cmd->function;
63 if (cmd->read_write) { /* write */
64 if (cmd->raw) {
65 sdio_writeb(func, cmd->data, cmd->address, &ret);
66 data = sdio_readb(func, cmd->address, &ret);
67 cmd->data = data;
68 } else {
69 sdio_writeb(func, cmd->data, cmd->address, &ret);
70 }
71 } else { /* read */
72 data = sdio_readb(func, cmd->address, &ret);
73 cmd->data = data;
74 }
75
76 sdio_release_host(func);
77
78 if (ret)
79 dev_err(&func->dev, "wilc_sdio_cmd52..failed, err(%d)\n", ret);
80 return ret;
81}
82
83
Chaehyun Limfc17eaa2016-03-03 14:28:22 +090084static int wilc_sdio_cmd53(struct wilc *wilc, struct sdio_cmd53 *cmd)
Glen Lee35a45692015-12-21 14:18:29 +090085{
86 struct sdio_func *func = container_of(wilc->dev, struct sdio_func, dev);
87 int size, ret;
88
89 sdio_claim_host(func);
90
91 func->num = cmd->function;
92 func->cur_blksize = cmd->block_size;
93 if (cmd->block_mode)
94 size = cmd->count * cmd->block_size;
95 else
96 size = cmd->count;
97
98 if (cmd->read_write) { /* write */
99 ret = sdio_memcpy_toio(func, cmd->address,
100 (void *)cmd->buffer, size);
101 } else { /* read */
102 ret = sdio_memcpy_fromio(func, (void *)cmd->buffer,
103 cmd->address, size);
104 }
105
106 sdio_release_host(func);
107
108 if (ret)
109 dev_err(&func->dev, "wilc_sdio_cmd53..failed, err(%d)\n", ret);
110
111 return ret;
112}
113
114static int linux_sdio_probe(struct sdio_func *func,
115 const struct sdio_device_id *id)
116{
117 struct wilc *wilc;
118 int gpio, ret;
119
120 gpio = -1;
121 if (IS_ENABLED(CONFIG_WILC1000_HW_OOB_INTR)) {
122 gpio = of_get_gpio(func->dev.of_node, 0);
123 if (gpio < 0)
124 gpio = GPIO_NUM;
125 }
126
127 dev_dbg(&func->dev, "Initializing netdev\n");
128 ret = wilc_netdev_init(&wilc, &func->dev, HIF_SDIO, gpio,
129 &wilc_hif_sdio);
130 if (ret) {
131 dev_err(&func->dev, "Couldn't initialize netdev\n");
132 return ret;
133 }
134 sdio_set_drvdata(func, wilc);
135 wilc->dev = &func->dev;
136
137 dev_info(&func->dev, "Driver Initializing success\n");
138 return 0;
139}
140
141static void linux_sdio_remove(struct sdio_func *func)
142{
143 wilc_netdev_cleanup(sdio_get_drvdata(func));
144}
145
Glen Lee76855ba2016-01-25 16:35:08 +0900146static int sdio_reset(struct wilc *wilc)
147{
Chaehyun Lima91632f2016-03-03 14:28:20 +0900148 struct sdio_cmd52 cmd;
Glen Lee76855ba2016-01-25 16:35:08 +0900149 int ret;
150 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
151
152 cmd.read_write = 1;
153 cmd.function = 0;
154 cmd.raw = 0;
155 cmd.address = 0x6;
156 cmd.data = 0x8;
157 ret = wilc_sdio_cmd52(wilc, &cmd);
158 if (ret) {
159 dev_err(&func->dev, "Fail cmd 52, reset cmd ...\n");
160 return ret;
161 }
162 return 0;
163}
164
165static int wilc_sdio_suspend(struct device *dev)
166{
167 struct sdio_func *func = dev_to_sdio_func(dev);
168 struct wilc *wilc = sdio_get_drvdata(func);
169 int ret;
170
171 dev_info(dev, "sdio suspend\n");
172 chip_wakeup(wilc);
173
174 if (!wilc->suspend_event) {
175 wilc_chip_sleep_manually(wilc);
176 } else {
177 host_sleep_notify(wilc);
178 chip_allow_sleep(wilc);
179 }
180
181 ret = sdio_reset(wilc);
182 if (ret) {
183 dev_err(&func->dev, "Fail reset sdio\n");
184 return ret;
185 }
186 sdio_claim_host(func);
187
188 return 0;
189}
190
191static int wilc_sdio_resume(struct device *dev)
192{
193 struct sdio_func *func = dev_to_sdio_func(dev);
194 struct wilc *wilc = sdio_get_drvdata(func);
195
196 dev_info(dev, "sdio resume\n");
197 sdio_release_host(func);
198 chip_wakeup(wilc);
Glen Lee5397cbc2016-01-25 16:35:09 +0900199 sdio_init(wilc, true);
Glen Lee76855ba2016-01-25 16:35:08 +0900200
201 if (wilc->suspend_event)
202 host_wakeup_notify(wilc);
203
204 chip_allow_sleep(wilc);
205
206 return 0;
207}
208
209static const struct dev_pm_ops wilc_sdio_pm_ops = {
210 .suspend = wilc_sdio_suspend,
211 .resume = wilc_sdio_resume,
212};
213
Glen Lee35a45692015-12-21 14:18:29 +0900214static struct sdio_driver wilc1000_sdio_driver = {
215 .name = SDIO_MODALIAS,
216 .id_table = wilc_sdio_ids,
217 .probe = linux_sdio_probe,
218 .remove = linux_sdio_remove,
Glen Lee76855ba2016-01-25 16:35:08 +0900219 .drv = {
220 .pm = &wilc_sdio_pm_ops,
221 }
Glen Lee35a45692015-12-21 14:18:29 +0900222};
223module_driver(wilc1000_sdio_driver,
224 sdio_register_driver,
225 sdio_unregister_driver);
226MODULE_LICENSE("GPL");
227
228static int wilc_sdio_enable_interrupt(struct wilc *dev)
229{
230 struct sdio_func *func = container_of(dev->dev, struct sdio_func, dev);
231 int ret = 0;
232
233 sdio_claim_host(func);
234 ret = sdio_claim_irq(func, wilc_sdio_interrupt);
235 sdio_release_host(func);
236
237 if (ret < 0) {
238 dev_err(&func->dev, "can't claim sdio_irq, err(%d)\n", ret);
239 ret = -EIO;
240 }
241 return ret;
242}
243
244static void wilc_sdio_disable_interrupt(struct wilc *dev)
245{
246 struct sdio_func *func = container_of(dev->dev, struct sdio_func, dev);
247 int ret;
248
249 dev_dbg(&func->dev, "wilc_sdio_disable_interrupt IN\n");
250
251 sdio_claim_host(func);
252 ret = sdio_release_irq(func);
253 if (ret < 0)
254 dev_err(&func->dev, "can't release sdio_irq, err(%d)\n", ret);
255 sdio_release_host(func);
256
257 dev_info(&func->dev, "wilc_sdio_disable_interrupt OUT\n");
258}
259
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900260/********************************************
261 *
262 * Function 0
263 *
264 ********************************************/
265
Glen Lee49dcd0d2015-11-18 15:11:26 +0900266static int sdio_set_func0_csa_address(struct wilc *wilc, u32 adr)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900267{
Glen Leeac1da162015-12-21 14:18:13 +0900268 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
Chaehyun Lima91632f2016-03-03 14:28:20 +0900269 struct sdio_cmd52 cmd;
Glen Lee28e9ad22015-12-21 14:18:19 +0900270 int ret;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900271
272 /**
273 * Review: BIG ENDIAN
274 **/
275 cmd.read_write = 1;
276 cmd.function = 0;
277 cmd.raw = 0;
278 cmd.address = 0x10c;
Chaehyun Lim51e825f2015-09-15 14:06:14 +0900279 cmd.data = (u8)adr;
Glen Lee28e9ad22015-12-21 14:18:19 +0900280 ret = wilc_sdio_cmd52(wilc, &cmd);
281 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900282 dev_err(&func->dev, "Failed cmd52, set 0x10c data...\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900283 goto _fail_;
284 }
285
286 cmd.address = 0x10d;
Chaehyun Lim51e825f2015-09-15 14:06:14 +0900287 cmd.data = (u8)(adr >> 8);
Glen Lee28e9ad22015-12-21 14:18:19 +0900288 ret = wilc_sdio_cmd52(wilc, &cmd);
289 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900290 dev_err(&func->dev, "Failed cmd52, set 0x10d data...\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900291 goto _fail_;
292 }
293
294 cmd.address = 0x10e;
Chaehyun Lim51e825f2015-09-15 14:06:14 +0900295 cmd.data = (u8)(adr >> 16);
Glen Lee28e9ad22015-12-21 14:18:19 +0900296 ret = wilc_sdio_cmd52(wilc, &cmd);
297 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900298 dev_err(&func->dev, "Failed cmd52, set 0x10e data...\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900299 goto _fail_;
300 }
301
302 return 1;
303_fail_:
304 return 0;
305}
306
Glen Lee49dcd0d2015-11-18 15:11:26 +0900307static int sdio_set_func0_block_size(struct wilc *wilc, u32 block_size)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900308{
Glen Leeac1da162015-12-21 14:18:13 +0900309 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
Chaehyun Lima91632f2016-03-03 14:28:20 +0900310 struct sdio_cmd52 cmd;
Glen Lee28e9ad22015-12-21 14:18:19 +0900311 int ret;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900312
313 cmd.read_write = 1;
314 cmd.function = 0;
315 cmd.raw = 0;
316 cmd.address = 0x10;
Chaehyun Lim51e825f2015-09-15 14:06:14 +0900317 cmd.data = (u8)block_size;
Glen Lee28e9ad22015-12-21 14:18:19 +0900318 ret = wilc_sdio_cmd52(wilc, &cmd);
319 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900320 dev_err(&func->dev, "Failed cmd52, set 0x10 data...\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900321 goto _fail_;
322 }
323
324 cmd.address = 0x11;
Chaehyun Lim51e825f2015-09-15 14:06:14 +0900325 cmd.data = (u8)(block_size >> 8);
Glen Lee28e9ad22015-12-21 14:18:19 +0900326 ret = wilc_sdio_cmd52(wilc, &cmd);
327 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900328 dev_err(&func->dev, "Failed cmd52, set 0x11 data...\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900329 goto _fail_;
330 }
331
332 return 1;
333_fail_:
334 return 0;
335}
336
337/********************************************
338 *
339 * Function 1
340 *
341 ********************************************/
342
Glen Lee49dcd0d2015-11-18 15:11:26 +0900343static int sdio_set_func1_block_size(struct wilc *wilc, u32 block_size)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900344{
Glen Leeac1da162015-12-21 14:18:13 +0900345 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
Chaehyun Lima91632f2016-03-03 14:28:20 +0900346 struct sdio_cmd52 cmd;
Glen Lee28e9ad22015-12-21 14:18:19 +0900347 int ret;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900348
349 cmd.read_write = 1;
350 cmd.function = 0;
351 cmd.raw = 0;
352 cmd.address = 0x110;
Chaehyun Lim51e825f2015-09-15 14:06:14 +0900353 cmd.data = (u8)block_size;
Glen Lee28e9ad22015-12-21 14:18:19 +0900354 ret = wilc_sdio_cmd52(wilc, &cmd);
355 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900356 dev_err(&func->dev, "Failed cmd52, set 0x110 data...\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900357 goto _fail_;
358 }
359 cmd.address = 0x111;
Chaehyun Lim51e825f2015-09-15 14:06:14 +0900360 cmd.data = (u8)(block_size >> 8);
Glen Lee28e9ad22015-12-21 14:18:19 +0900361 ret = wilc_sdio_cmd52(wilc, &cmd);
362 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900363 dev_err(&func->dev, "Failed cmd52, set 0x111 data...\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900364 goto _fail_;
365 }
366
367 return 1;
368_fail_:
369 return 0;
370}
371
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900372/********************************************
373 *
374 * Sdio interfaces
375 *
376 ********************************************/
Glen Lee49dcd0d2015-11-18 15:11:26 +0900377static int sdio_write_reg(struct wilc *wilc, u32 addr, u32 data)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900378{
Glen Leeac1da162015-12-21 14:18:13 +0900379 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
Glen Lee6f8bded2015-12-21 14:18:18 +0900380 int ret;
Glen Leeac1da162015-12-21 14:18:13 +0900381
Glen Lee9e6627a2015-12-21 14:18:08 +0900382 data = cpu_to_le32(data);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900383
384 if ((addr >= 0xf0) && (addr <= 0xff)) {
Chaehyun Lima91632f2016-03-03 14:28:20 +0900385 struct sdio_cmd52 cmd;
Tony Cho9c844692015-07-28 17:47:24 +0900386
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900387 cmd.read_write = 1;
388 cmd.function = 0;
389 cmd.raw = 0;
390 cmd.address = addr;
391 cmd.data = data;
Glen Lee28e9ad22015-12-21 14:18:19 +0900392 ret = wilc_sdio_cmd52(wilc, &cmd);
393 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900394 dev_err(&func->dev,
395 "Failed cmd 52, read reg (%08x) ...\n", addr);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900396 goto _fail_;
397 }
398 } else {
Chaehyun Limfc17eaa2016-03-03 14:28:22 +0900399 struct sdio_cmd53 cmd;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900400
401 /**
402 * set the AHB address
403 **/
Glen Lee49dcd0d2015-11-18 15:11:26 +0900404 if (!sdio_set_func0_csa_address(wilc, addr))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900405 goto _fail_;
406
407 cmd.read_write = 1;
408 cmd.function = 0;
409 cmd.address = 0x10f;
410 cmd.block_mode = 0;
411 cmd.increment = 1;
412 cmd.count = 4;
Chaehyun Lim51e825f2015-09-15 14:06:14 +0900413 cmd.buffer = (u8 *)&data;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900414 cmd.block_size = g_sdio.block_size; /* johnny : prevent it from setting unexpected value */
Glen Lee6f8bded2015-12-21 14:18:18 +0900415 ret = wilc_sdio_cmd53(wilc, &cmd);
416 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900417 dev_err(&func->dev,
418 "Failed cmd53, write reg (%08x)...\n", addr);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900419 goto _fail_;
420 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900421 }
422
423 return 1;
424
425_fail_:
426
427 return 0;
428}
429
Glen Lee49dcd0d2015-11-18 15:11:26 +0900430static int sdio_write(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900431{
Glen Leeac1da162015-12-21 14:18:13 +0900432 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900433 u32 block_size = g_sdio.block_size;
Chaehyun Limfc17eaa2016-03-03 14:28:22 +0900434 struct sdio_cmd53 cmd;
Glen Lee6f8bded2015-12-21 14:18:18 +0900435 int nblk, nleft, ret;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900436
437 cmd.read_write = 1;
438 if (addr > 0) {
439 /**
440 * has to be word aligned...
441 **/
442 if (size & 0x3) {
443 size += 4;
444 size &= ~0x3;
445 }
446
447 /**
448 * func 0 access
449 **/
450 cmd.function = 0;
451 cmd.address = 0x10f;
452 } else {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900453 /**
454 * has to be word aligned...
455 **/
456 if (size & 0x3) {
457 size += 4;
458 size &= ~0x3;
459 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900460
461 /**
462 * func 1 access
463 **/
464 cmd.function = 1;
465 cmd.address = 0;
466 }
467
468 nblk = size / block_size;
469 nleft = size % block_size;
470
471 if (nblk > 0) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900472 cmd.block_mode = 1;
473 cmd.increment = 1;
474 cmd.count = nblk;
475 cmd.buffer = buf;
476 cmd.block_size = block_size;
477 if (addr > 0) {
Glen Lee49dcd0d2015-11-18 15:11:26 +0900478 if (!sdio_set_func0_csa_address(wilc, addr))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900479 goto _fail_;
480 }
Glen Lee6f8bded2015-12-21 14:18:18 +0900481 ret = wilc_sdio_cmd53(wilc, &cmd);
482 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900483 dev_err(&func->dev,
484 "Failed cmd53 [%x], block send...\n", addr);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900485 goto _fail_;
486 }
487 if (addr > 0)
488 addr += nblk * block_size;
489 buf += nblk * block_size;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900490 }
491
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900492 if (nleft > 0) {
493 cmd.block_mode = 0;
494 cmd.increment = 1;
495 cmd.count = nleft;
496 cmd.buffer = buf;
497
498 cmd.block_size = block_size; /* johnny : prevent it from setting unexpected value */
499
500 if (addr > 0) {
Glen Lee49dcd0d2015-11-18 15:11:26 +0900501 if (!sdio_set_func0_csa_address(wilc, addr))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900502 goto _fail_;
503 }
Glen Lee6f8bded2015-12-21 14:18:18 +0900504 ret = wilc_sdio_cmd53(wilc, &cmd);
505 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900506 dev_err(&func->dev,
507 "Failed cmd53 [%x], bytes send...\n", addr);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900508 goto _fail_;
509 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900510 }
511
512 return 1;
513
514_fail_:
515
516 return 0;
517}
518
Glen Lee49dcd0d2015-11-18 15:11:26 +0900519static int sdio_read_reg(struct wilc *wilc, u32 addr, u32 *data)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900520{
Glen Leeac1da162015-12-21 14:18:13 +0900521 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
Glen Lee6f8bded2015-12-21 14:18:18 +0900522 int ret;
Glen Leeac1da162015-12-21 14:18:13 +0900523
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900524 if ((addr >= 0xf0) && (addr <= 0xff)) {
Chaehyun Lima91632f2016-03-03 14:28:20 +0900525 struct sdio_cmd52 cmd;
Tony Cho9c844692015-07-28 17:47:24 +0900526
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900527 cmd.read_write = 0;
528 cmd.function = 0;
529 cmd.raw = 0;
530 cmd.address = addr;
Glen Lee28e9ad22015-12-21 14:18:19 +0900531 ret = wilc_sdio_cmd52(wilc, &cmd);
532 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900533 dev_err(&func->dev,
534 "Failed cmd 52, read reg (%08x) ...\n", addr);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900535 goto _fail_;
536 }
537 *data = cmd.data;
538 } else {
Chaehyun Limfc17eaa2016-03-03 14:28:22 +0900539 struct sdio_cmd53 cmd;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900540
Glen Lee49dcd0d2015-11-18 15:11:26 +0900541 if (!sdio_set_func0_csa_address(wilc, addr))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900542 goto _fail_;
543
544 cmd.read_write = 0;
545 cmd.function = 0;
546 cmd.address = 0x10f;
547 cmd.block_mode = 0;
548 cmd.increment = 1;
549 cmd.count = 4;
Chaehyun Lim51e825f2015-09-15 14:06:14 +0900550 cmd.buffer = (u8 *)data;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900551
552 cmd.block_size = g_sdio.block_size; /* johnny : prevent it from setting unexpected value */
Glen Lee6f8bded2015-12-21 14:18:18 +0900553 ret = wilc_sdio_cmd53(wilc, &cmd);
554 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900555 dev_err(&func->dev,
556 "Failed cmd53, read reg (%08x)...\n", addr);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900557 goto _fail_;
558 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900559 }
560
Glen Lee9e6627a2015-12-21 14:18:08 +0900561 *data = cpu_to_le32(*data);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900562
563 return 1;
564
565_fail_:
566
567 return 0;
568}
569
Glen Lee49dcd0d2015-11-18 15:11:26 +0900570static int sdio_read(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900571{
Glen Leeac1da162015-12-21 14:18:13 +0900572 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900573 u32 block_size = g_sdio.block_size;
Chaehyun Limfc17eaa2016-03-03 14:28:22 +0900574 struct sdio_cmd53 cmd;
Glen Lee6f8bded2015-12-21 14:18:18 +0900575 int nblk, nleft, ret;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900576
577 cmd.read_write = 0;
578 if (addr > 0) {
579 /**
580 * has to be word aligned...
581 **/
582 if (size & 0x3) {
583 size += 4;
584 size &= ~0x3;
585 }
586
587 /**
588 * func 0 access
589 **/
590 cmd.function = 0;
591 cmd.address = 0x10f;
592 } else {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900593 /**
594 * has to be word aligned...
595 **/
596 if (size & 0x3) {
597 size += 4;
598 size &= ~0x3;
599 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900600
601 /**
602 * func 1 access
603 **/
604 cmd.function = 1;
605 cmd.address = 0;
606 }
607
608 nblk = size / block_size;
609 nleft = size % block_size;
610
611 if (nblk > 0) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900612 cmd.block_mode = 1;
613 cmd.increment = 1;
614 cmd.count = nblk;
615 cmd.buffer = buf;
616 cmd.block_size = block_size;
617 if (addr > 0) {
Glen Lee49dcd0d2015-11-18 15:11:26 +0900618 if (!sdio_set_func0_csa_address(wilc, addr))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900619 goto _fail_;
620 }
Glen Lee6f8bded2015-12-21 14:18:18 +0900621 ret = wilc_sdio_cmd53(wilc, &cmd);
622 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900623 dev_err(&func->dev,
624 "Failed cmd53 [%x], block read...\n", addr);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900625 goto _fail_;
626 }
627 if (addr > 0)
628 addr += nblk * block_size;
629 buf += nblk * block_size;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900630 } /* if (nblk > 0) */
631
632 if (nleft > 0) {
633 cmd.block_mode = 0;
634 cmd.increment = 1;
635 cmd.count = nleft;
636 cmd.buffer = buf;
637
638 cmd.block_size = block_size; /* johnny : prevent it from setting unexpected value */
639
640 if (addr > 0) {
Glen Lee49dcd0d2015-11-18 15:11:26 +0900641 if (!sdio_set_func0_csa_address(wilc, addr))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900642 goto _fail_;
643 }
Glen Lee6f8bded2015-12-21 14:18:18 +0900644 ret = wilc_sdio_cmd53(wilc, &cmd);
645 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900646 dev_err(&func->dev,
647 "Failed cmd53 [%x], bytes read...\n", addr);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900648 goto _fail_;
649 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900650 }
651
652 return 1;
653
654_fail_:
655
656 return 0;
657}
658
659/********************************************
660 *
661 * Bus interfaces
662 *
663 ********************************************/
664
Glen Lee49dcd0d2015-11-18 15:11:26 +0900665static int sdio_deinit(struct wilc *wilc)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900666{
667 return 1;
668}
669
Glen Lee5397cbc2016-01-25 16:35:09 +0900670static int sdio_init(struct wilc *wilc, bool resume)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900671{
Glen Leeac1da162015-12-21 14:18:13 +0900672 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
Chaehyun Lima91632f2016-03-03 14:28:20 +0900673 struct sdio_cmd52 cmd;
Glen Lee28e9ad22015-12-21 14:18:19 +0900674 int loop, ret;
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900675 u32 chipid;
Tony Cho9c844692015-07-28 17:47:24 +0900676
Glen Lee5397cbc2016-01-25 16:35:09 +0900677 if (!resume) {
Bhumika Goyal6a707a92016-02-21 03:12:11 +0530678 memset(&g_sdio, 0, sizeof(struct wilc_sdio));
Janani Ravichandran5343c7b2016-02-11 16:41:21 -0500679 g_sdio.irq_gpio = wilc->dev_irq_num;
Glen Lee5397cbc2016-01-25 16:35:09 +0900680 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900681
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900682 /**
683 * function 0 csa enable
684 **/
685 cmd.read_write = 1;
686 cmd.function = 0;
687 cmd.raw = 1;
688 cmd.address = 0x100;
689 cmd.data = 0x80;
Glen Lee28e9ad22015-12-21 14:18:19 +0900690 ret = wilc_sdio_cmd52(wilc, &cmd);
691 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900692 dev_err(&func->dev, "Fail cmd 52, enable csa...\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900693 goto _fail_;
694 }
695
696 /**
697 * function 0 block size
698 **/
Glen Lee49dcd0d2015-11-18 15:11:26 +0900699 if (!sdio_set_func0_block_size(wilc, WILC_SDIO_BLOCK_SIZE)) {
Glen Leeac1da162015-12-21 14:18:13 +0900700 dev_err(&func->dev, "Fail cmd 52, set func 0 block size...\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900701 goto _fail_;
702 }
703 g_sdio.block_size = WILC_SDIO_BLOCK_SIZE;
704
705 /**
706 * enable func1 IO
707 **/
708 cmd.read_write = 1;
709 cmd.function = 0;
710 cmd.raw = 1;
711 cmd.address = 0x2;
712 cmd.data = 0x2;
Glen Lee28e9ad22015-12-21 14:18:19 +0900713 ret = wilc_sdio_cmd52(wilc, &cmd);
714 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900715 dev_err(&func->dev,
716 "Fail cmd 52, set IOE register...\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900717 goto _fail_;
718 }
719
720 /**
721 * make sure func 1 is up
722 **/
723 cmd.read_write = 0;
724 cmd.function = 0;
725 cmd.raw = 0;
726 cmd.address = 0x3;
727 loop = 3;
728 do {
729 cmd.data = 0;
Glen Lee28e9ad22015-12-21 14:18:19 +0900730 ret = wilc_sdio_cmd52(wilc, &cmd);
731 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900732 dev_err(&func->dev,
733 "Fail cmd 52, get IOR register...\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900734 goto _fail_;
735 }
736 if (cmd.data == 0x2)
737 break;
738 } while (loop--);
739
740 if (loop <= 0) {
Glen Leeac1da162015-12-21 14:18:13 +0900741 dev_err(&func->dev, "Fail func 1 is not ready...\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900742 goto _fail_;
743 }
744
745 /**
746 * func 1 is ready, set func 1 block size
747 **/
Glen Lee49dcd0d2015-11-18 15:11:26 +0900748 if (!sdio_set_func1_block_size(wilc, WILC_SDIO_BLOCK_SIZE)) {
Glen Leeac1da162015-12-21 14:18:13 +0900749 dev_err(&func->dev, "Fail set func 1 block size...\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900750 goto _fail_;
751 }
752
753 /**
754 * func 1 interrupt enable
755 **/
756 cmd.read_write = 1;
757 cmd.function = 0;
758 cmd.raw = 1;
759 cmd.address = 0x4;
760 cmd.data = 0x3;
Glen Lee28e9ad22015-12-21 14:18:19 +0900761 ret = wilc_sdio_cmd52(wilc, &cmd);
762 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900763 dev_err(&func->dev, "Fail cmd 52, set IEN register...\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900764 goto _fail_;
765 }
766
767 /**
768 * make sure can read back chip id correctly
769 **/
Glen Lee5397cbc2016-01-25 16:35:09 +0900770 if (!resume) {
771 if (!sdio_read_reg(wilc, 0x1000, &chipid)) {
772 dev_err(&func->dev, "Fail cmd read chip id...\n");
773 goto _fail_;
774 }
775 dev_err(&func->dev, "chipid (%08x)\n", chipid);
776 if ((chipid & 0xfff) > 0x2a0)
777 g_sdio.has_thrpt_enh3 = 1;
778 else
779 g_sdio.has_thrpt_enh3 = 0;
780 dev_info(&func->dev, "has_thrpt_enh3 = %d...\n",
781 g_sdio.has_thrpt_enh3);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900782 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900783
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900784 return 1;
785
786_fail_:
787
788 return 0;
789}
790
Glen Lee49dcd0d2015-11-18 15:11:26 +0900791static int sdio_read_size(struct wilc *wilc, u32 *size)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900792{
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900793 u32 tmp;
Chaehyun Lima91632f2016-03-03 14:28:20 +0900794 struct sdio_cmd52 cmd;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900795
796 /**
797 * Read DMA count in words
798 **/
Tony Cho202f66a2015-07-28 17:47:35 +0900799 cmd.read_write = 0;
800 cmd.function = 0;
801 cmd.raw = 0;
802 cmd.address = 0xf2;
803 cmd.data = 0;
Glen Leeea5779b2015-11-18 15:11:27 +0900804 wilc_sdio_cmd52(wilc, &cmd);
Tony Cho202f66a2015-07-28 17:47:35 +0900805 tmp = cmd.data;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900806
Tony Cho202f66a2015-07-28 17:47:35 +0900807 /* cmd.read_write = 0; */
808 /* cmd.function = 0; */
809 /* cmd.raw = 0; */
810 cmd.address = 0xf3;
811 cmd.data = 0;
Glen Leeea5779b2015-11-18 15:11:27 +0900812 wilc_sdio_cmd52(wilc, &cmd);
Tony Cho202f66a2015-07-28 17:47:35 +0900813 tmp |= (cmd.data << 8);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900814
815 *size = tmp;
816 return 1;
817}
818
Glen Lee49dcd0d2015-11-18 15:11:26 +0900819static int sdio_read_int(struct wilc *wilc, u32 *int_status)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900820{
Glen Leeac1da162015-12-21 14:18:13 +0900821 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900822 u32 tmp;
Chaehyun Lima91632f2016-03-03 14:28:20 +0900823 struct sdio_cmd52 cmd;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900824
Glen Lee49dcd0d2015-11-18 15:11:26 +0900825 sdio_read_size(wilc, &tmp);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900826
827 /**
828 * Read IRQ flags
829 **/
Arnd Bergmannc4d139c2015-11-16 15:05:04 +0100830 if (!g_sdio.irq_gpio) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900831 int i;
Tony Cho9c844692015-07-28 17:47:24 +0900832
Colin Ian King6e183782018-12-19 16:30:07 +0000833 cmd.read_write = 0;
Arnd Bergmannc4d139c2015-11-16 15:05:04 +0100834 cmd.function = 1;
835 cmd.address = 0x04;
836 cmd.data = 0;
Glen Leeea5779b2015-11-18 15:11:27 +0900837 wilc_sdio_cmd52(wilc, &cmd);
Arnd Bergmannc4d139c2015-11-16 15:05:04 +0100838
839 if (cmd.data & BIT(0))
840 tmp |= INT_0;
841 if (cmd.data & BIT(2))
842 tmp |= INT_1;
843 if (cmd.data & BIT(3))
844 tmp |= INT_2;
845 if (cmd.data & BIT(4))
846 tmp |= INT_3;
847 if (cmd.data & BIT(5))
848 tmp |= INT_4;
849 if (cmd.data & BIT(6))
850 tmp |= INT_5;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900851 for (i = g_sdio.nint; i < MAX_NUM_INT; i++) {
852 if ((tmp >> (IRG_FLAGS_OFFSET + i)) & 0x1) {
Glen Leeac1da162015-12-21 14:18:13 +0900853 dev_err(&func->dev,
854 "Unexpected interrupt (1) : tmp=%x, data=%x\n",
855 tmp, cmd.data);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900856 break;
857 }
858 }
Arnd Bergmannc4d139c2015-11-16 15:05:04 +0100859 } else {
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900860 u32 irq_flags;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900861
862 cmd.read_write = 0;
863 cmd.function = 0;
864 cmd.raw = 0;
865 cmd.address = 0xf7;
866 cmd.data = 0;
Glen Leeea5779b2015-11-18 15:11:27 +0900867 wilc_sdio_cmd52(wilc, &cmd);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900868 irq_flags = cmd.data & 0x1f;
869 tmp |= ((irq_flags >> 0) << IRG_FLAGS_OFFSET);
870 }
871
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900872 *int_status = tmp;
873
874 return 1;
875}
876
Glen Lee49dcd0d2015-11-18 15:11:26 +0900877static int sdio_clear_int_ext(struct wilc *wilc, u32 val)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900878{
Glen Leeac1da162015-12-21 14:18:13 +0900879 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900880 int ret;
881
882 if (g_sdio.has_thrpt_enh3) {
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900883 u32 reg;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900884
Arnd Bergmannc4d139c2015-11-16 15:05:04 +0100885 if (g_sdio.irq_gpio) {
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900886 u32 flags;
Tony Cho9c844692015-07-28 17:47:24 +0900887
Anish Bhattffda2032015-09-29 12:15:49 -0700888 flags = val & (BIT(MAX_NUN_INT_THRPT_ENH2) - 1);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900889 reg = flags;
Arnd Bergmannc4d139c2015-11-16 15:05:04 +0100890 } else {
891 reg = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900892 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900893 /* select VMM table 0 */
894 if ((val & SEL_VMM_TBL0) == SEL_VMM_TBL0)
Anish Bhattffda2032015-09-29 12:15:49 -0700895 reg |= BIT(5);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900896 /* select VMM table 1 */
897 if ((val & SEL_VMM_TBL1) == SEL_VMM_TBL1)
Anish Bhattffda2032015-09-29 12:15:49 -0700898 reg |= BIT(6);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900899 /* enable VMM */
900 if ((val & EN_VMM) == EN_VMM)
Anish Bhattffda2032015-09-29 12:15:49 -0700901 reg |= BIT(7);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900902 if (reg) {
Chaehyun Lima91632f2016-03-03 14:28:20 +0900903 struct sdio_cmd52 cmd;
Tony Cho9c844692015-07-28 17:47:24 +0900904
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900905 cmd.read_write = 1;
906 cmd.function = 0;
907 cmd.raw = 0;
908 cmd.address = 0xf8;
909 cmd.data = reg;
910
Glen Leeea5779b2015-11-18 15:11:27 +0900911 ret = wilc_sdio_cmd52(wilc, &cmd);
Glen Lee28e9ad22015-12-21 14:18:19 +0900912 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900913 dev_err(&func->dev,
914 "Failed cmd52, set 0xf8 data (%d) ...\n",
915 __LINE__);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900916 goto _fail_;
917 }
918
919 }
920 } else {
Arnd Bergmannc4d139c2015-11-16 15:05:04 +0100921 if (g_sdio.irq_gpio) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900922 /* see below. has_thrpt_enh2 uses register 0xf8 to clear interrupts. */
923 /* Cannot clear multiple interrupts. Must clear each interrupt individually */
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900924 u32 flags;
Tony Cho9c844692015-07-28 17:47:24 +0900925
Anish Bhattffda2032015-09-29 12:15:49 -0700926 flags = val & (BIT(MAX_NUM_INT) - 1);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900927 if (flags) {
928 int i;
929
930 ret = 1;
931 for (i = 0; i < g_sdio.nint; i++) {
932 if (flags & 1) {
Chaehyun Lima91632f2016-03-03 14:28:20 +0900933 struct sdio_cmd52 cmd;
Tony Cho9c844692015-07-28 17:47:24 +0900934
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900935 cmd.read_write = 1;
936 cmd.function = 0;
937 cmd.raw = 0;
938 cmd.address = 0xf8;
Anish Bhattffda2032015-09-29 12:15:49 -0700939 cmd.data = BIT(i);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900940
Glen Leeea5779b2015-11-18 15:11:27 +0900941 ret = wilc_sdio_cmd52(wilc, &cmd);
Glen Lee28e9ad22015-12-21 14:18:19 +0900942 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900943 dev_err(&func->dev,
944 "Failed cmd52, set 0xf8 data (%d) ...\n",
945 __LINE__);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900946 goto _fail_;
947 }
948
949 }
950 if (!ret)
951 break;
952 flags >>= 1;
953 }
Tony Cho4aa857722015-07-28 17:47:25 +0900954 if (!ret)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900955 goto _fail_;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900956 for (i = g_sdio.nint; i < MAX_NUM_INT; i++) {
957 if (flags & 1)
Glen Leeac1da162015-12-21 14:18:13 +0900958 dev_err(&func->dev,
959 "Unexpected interrupt cleared %d...\n",
960 i);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900961 flags >>= 1;
962 }
963 }
964 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900965
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900966 {
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900967 u32 vmm_ctl;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900968
969 vmm_ctl = 0;
970 /* select VMM table 0 */
971 if ((val & SEL_VMM_TBL0) == SEL_VMM_TBL0)
Anish Bhattffda2032015-09-29 12:15:49 -0700972 vmm_ctl |= BIT(0);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900973 /* select VMM table 1 */
974 if ((val & SEL_VMM_TBL1) == SEL_VMM_TBL1)
Anish Bhattffda2032015-09-29 12:15:49 -0700975 vmm_ctl |= BIT(1);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900976 /* enable VMM */
977 if ((val & EN_VMM) == EN_VMM)
Anish Bhattffda2032015-09-29 12:15:49 -0700978 vmm_ctl |= BIT(2);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900979
980 if (vmm_ctl) {
Chaehyun Lima91632f2016-03-03 14:28:20 +0900981 struct sdio_cmd52 cmd;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900982
983 cmd.read_write = 1;
984 cmd.function = 0;
985 cmd.raw = 0;
986 cmd.address = 0xf6;
987 cmd.data = vmm_ctl;
Glen Leeea5779b2015-11-18 15:11:27 +0900988 ret = wilc_sdio_cmd52(wilc, &cmd);
Glen Lee28e9ad22015-12-21 14:18:19 +0900989 if (ret) {
Glen Leeac1da162015-12-21 14:18:13 +0900990 dev_err(&func->dev,
991 "Failed cmd52, set 0xf6 data (%d) ...\n",
992 __LINE__);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900993 goto _fail_;
994 }
995 }
996 }
997 }
998
999 return 1;
1000_fail_:
1001 return 0;
1002}
1003
Glen Lee49dcd0d2015-11-18 15:11:26 +09001004static int sdio_sync_ext(struct wilc *wilc, int nint)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001005{
Glen Leeac1da162015-12-21 14:18:13 +09001006 struct sdio_func *func = dev_to_sdio_func(wilc->dev);
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001007 u32 reg;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001008
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001009 if (nint > MAX_NUM_INT) {
Colin Ian King95b8cb82016-06-23 14:14:07 +01001010 dev_err(&func->dev, "Too many interrupts (%d)...\n", nint);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001011 return 0;
1012 }
1013 if (nint > MAX_NUN_INT_THRPT_ENH2) {
Glen Leeac1da162015-12-21 14:18:13 +09001014 dev_err(&func->dev,
1015 "Cannot support more than 5 interrupts when has_thrpt_enh2=1.\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001016 return 0;
1017 }
1018
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001019 g_sdio.nint = nint;
1020
1021 /**
1022 * Disable power sequencer
1023 **/
Glen Lee49dcd0d2015-11-18 15:11:26 +09001024 if (!sdio_read_reg(wilc, WILC_MISC, &reg)) {
Glen Leeac1da162015-12-21 14:18:13 +09001025 dev_err(&func->dev, "Failed read misc reg...\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001026 return 0;
1027 }
1028
Anish Bhattffda2032015-09-29 12:15:49 -07001029 reg &= ~BIT(8);
Glen Lee49dcd0d2015-11-18 15:11:26 +09001030 if (!sdio_write_reg(wilc, WILC_MISC, reg)) {
Glen Leeac1da162015-12-21 14:18:13 +09001031 dev_err(&func->dev, "Failed write misc reg...\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001032 return 0;
1033 }
1034
Arnd Bergmannc4d139c2015-11-16 15:05:04 +01001035 if (g_sdio.irq_gpio) {
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001036 u32 reg;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001037 int ret, i;
1038
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001039 /**
1040 * interrupt pin mux select
1041 **/
Glen Lee49dcd0d2015-11-18 15:11:26 +09001042 ret = sdio_read_reg(wilc, WILC_PIN_MUX_0, &reg);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001043 if (!ret) {
Glen Leeac1da162015-12-21 14:18:13 +09001044 dev_err(&func->dev, "Failed read reg (%08x)...\n",
1045 WILC_PIN_MUX_0);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001046 return 0;
1047 }
Anish Bhattffda2032015-09-29 12:15:49 -07001048 reg |= BIT(8);
Glen Lee49dcd0d2015-11-18 15:11:26 +09001049 ret = sdio_write_reg(wilc, WILC_PIN_MUX_0, reg);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001050 if (!ret) {
Glen Leeac1da162015-12-21 14:18:13 +09001051 dev_err(&func->dev, "Failed write reg (%08x)...\n",
1052 WILC_PIN_MUX_0);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001053 return 0;
1054 }
1055
1056 /**
1057 * interrupt enable
1058 **/
Glen Lee49dcd0d2015-11-18 15:11:26 +09001059 ret = sdio_read_reg(wilc, WILC_INTR_ENABLE, &reg);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001060 if (!ret) {
Glen Leeac1da162015-12-21 14:18:13 +09001061 dev_err(&func->dev, "Failed read reg (%08x)...\n",
1062 WILC_INTR_ENABLE);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001063 return 0;
1064 }
1065
Tony Cho4aa857722015-07-28 17:47:25 +09001066 for (i = 0; (i < 5) && (nint > 0); i++, nint--)
Anish Bhattffda2032015-09-29 12:15:49 -07001067 reg |= BIT((27 + i));
Glen Lee49dcd0d2015-11-18 15:11:26 +09001068 ret = sdio_write_reg(wilc, WILC_INTR_ENABLE, reg);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001069 if (!ret) {
Glen Leeac1da162015-12-21 14:18:13 +09001070 dev_err(&func->dev, "Failed write reg (%08x)...\n",
1071 WILC_INTR_ENABLE);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001072 return 0;
1073 }
1074 if (nint) {
Glen Lee49dcd0d2015-11-18 15:11:26 +09001075 ret = sdio_read_reg(wilc, WILC_INTR2_ENABLE, &reg);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001076 if (!ret) {
Glen Leeac1da162015-12-21 14:18:13 +09001077 dev_err(&func->dev,
1078 "Failed read reg (%08x)...\n",
1079 WILC_INTR2_ENABLE);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001080 return 0;
1081 }
1082
Tony Cho4aa857722015-07-28 17:47:25 +09001083 for (i = 0; (i < 3) && (nint > 0); i++, nint--)
Anish Bhattffda2032015-09-29 12:15:49 -07001084 reg |= BIT(i);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001085
Glen Lee49dcd0d2015-11-18 15:11:26 +09001086 ret = sdio_read_reg(wilc, WILC_INTR2_ENABLE, &reg);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001087 if (!ret) {
Glen Leeac1da162015-12-21 14:18:13 +09001088 dev_err(&func->dev,
1089 "Failed write reg (%08x)...\n",
1090 WILC_INTR2_ENABLE);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001091 return 0;
1092 }
1093 }
1094 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001095 return 1;
1096}
1097
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001098/********************************************
1099 *
1100 * Global sdio HIF function table
1101 *
1102 ********************************************/
1103
Arnd Bergmann7d37a4a2015-11-16 15:05:05 +01001104const struct wilc_hif_func wilc_hif_sdio = {
1105 .hif_init = sdio_init,
1106 .hif_deinit = sdio_deinit,
1107 .hif_read_reg = sdio_read_reg,
1108 .hif_write_reg = sdio_write_reg,
1109 .hif_block_rx = sdio_read,
1110 .hif_block_tx = sdio_write,
Arnd Bergmann7d37a4a2015-11-16 15:05:05 +01001111 .hif_read_int = sdio_read_int,
1112 .hif_clear_int_ext = sdio_clear_int_ext,
1113 .hif_read_size = sdio_read_size,
1114 .hif_block_tx_ext = sdio_write,
1115 .hif_block_rx_ext = sdio_read,
1116 .hif_sync_ext = sdio_sync_ext,
Arnd Bergmann5547c1f2015-11-16 15:05:06 +01001117 .enable_interrupt = wilc_sdio_enable_interrupt,
1118 .disable_interrupt = wilc_sdio_disable_interrupt,
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001119};
1120