1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
6 * Copyright (c) 2010, ST-Ericsson
8 #include <linux/module.h>
9 #include <linux/mmc/sdio.h>
10 #include <linux/mmc/sdio_func.h>
11 #include <linux/mmc/card.h>
12 #include <linux/interrupt.h>
13 #include <linux/of_irq.h>
14 #include <linux/irq.h>
22 static const struct wfx_platform_data wfx_sdio_pdata = {
23 .file_fw = "wfm_wf200",
24 .file_pds = "wf200.pds",
27 struct wfx_sdio_priv {
28 struct sdio_func *func;
35 static int wfx_sdio_copy_from_io(void *priv, unsigned int reg_id,
36 void *dst, size_t count)
38 struct wfx_sdio_priv *bus = priv;
39 unsigned int sdio_addr = reg_id << 2;
42 WARN(reg_id > 7, "chip only has 7 registers");
43 WARN(((uintptr_t)dst) & 3, "unaligned buffer size");
44 WARN(count & 3, "unaligned buffer address");
46 /* Use queue mode buffers */
47 if (reg_id == WFX_REG_IN_OUT_QUEUE)
48 sdio_addr |= (bus->buf_id_rx + 1) << 7;
49 ret = sdio_memcpy_fromio(bus->func, dst, sdio_addr, count);
50 if (!ret && reg_id == WFX_REG_IN_OUT_QUEUE)
51 bus->buf_id_rx = (bus->buf_id_rx + 1) % 4;
56 static int wfx_sdio_copy_to_io(void *priv, unsigned int reg_id,
57 const void *src, size_t count)
59 struct wfx_sdio_priv *bus = priv;
60 unsigned int sdio_addr = reg_id << 2;
63 WARN(reg_id > 7, "chip only has 7 registers");
64 WARN(((uintptr_t)src) & 3, "unaligned buffer size");
65 WARN(count & 3, "unaligned buffer address");
67 /* Use queue mode buffers */
68 if (reg_id == WFX_REG_IN_OUT_QUEUE)
69 sdio_addr |= bus->buf_id_tx << 7;
70 // FIXME: discards 'const' qualifier for src
71 ret = sdio_memcpy_toio(bus->func, sdio_addr, (void *)src, count);
72 if (!ret && reg_id == WFX_REG_IN_OUT_QUEUE)
73 bus->buf_id_tx = (bus->buf_id_tx + 1) % 32;
78 static void wfx_sdio_lock(void *priv)
80 struct wfx_sdio_priv *bus = priv;
82 sdio_claim_host(bus->func);
85 static void wfx_sdio_unlock(void *priv)
87 struct wfx_sdio_priv *bus = priv;
89 sdio_release_host(bus->func);
92 static void wfx_sdio_irq_handler(struct sdio_func *func)
94 struct wfx_sdio_priv *bus = sdio_get_drvdata(func);
96 wfx_bh_request_rx(bus->core);
99 static irqreturn_t wfx_sdio_irq_handler_ext(int irq, void *priv)
101 struct wfx_sdio_priv *bus = priv;
103 sdio_claim_host(bus->func);
104 wfx_bh_request_rx(bus->core);
105 sdio_release_host(bus->func);
109 static int wfx_sdio_irq_subscribe(void *priv)
111 struct wfx_sdio_priv *bus = priv;
117 sdio_claim_host(bus->func);
118 ret = sdio_claim_irq(bus->func, wfx_sdio_irq_handler);
119 sdio_release_host(bus->func);
123 flags = irq_get_trigger_type(bus->of_irq);
125 flags = IRQF_TRIGGER_HIGH;
126 flags |= IRQF_ONESHOT;
127 ret = devm_request_threaded_irq(&bus->func->dev, bus->of_irq, NULL,
128 wfx_sdio_irq_handler_ext, flags,
132 sdio_claim_host(bus->func);
133 cccr = sdio_f0_readb(bus->func, SDIO_CCCR_IENx, NULL);
135 cccr |= BIT(bus->func->num);
136 sdio_f0_writeb(bus->func, cccr, SDIO_CCCR_IENx, NULL);
137 sdio_release_host(bus->func);
141 static int wfx_sdio_irq_unsubscribe(void *priv)
143 struct wfx_sdio_priv *bus = priv;
147 devm_free_irq(&bus->func->dev, bus->of_irq, bus);
148 sdio_claim_host(bus->func);
149 ret = sdio_release_irq(bus->func);
150 sdio_release_host(bus->func);
154 static size_t wfx_sdio_align_size(void *priv, size_t size)
156 struct wfx_sdio_priv *bus = priv;
158 return sdio_align_size(bus->func, size);
161 static const struct hwbus_ops wfx_sdio_hwbus_ops = {
162 .copy_from_io = wfx_sdio_copy_from_io,
163 .copy_to_io = wfx_sdio_copy_to_io,
164 .irq_subscribe = wfx_sdio_irq_subscribe,
165 .irq_unsubscribe = wfx_sdio_irq_unsubscribe,
166 .lock = wfx_sdio_lock,
167 .unlock = wfx_sdio_unlock,
168 .align_size = wfx_sdio_align_size,
171 static const struct of_device_id wfx_sdio_of_match[] = {
172 { .compatible = "silabs,wfx-sdio" },
173 { .compatible = "silabs,wf200" },
176 MODULE_DEVICE_TABLE(of, wfx_sdio_of_match);
178 static int wfx_sdio_probe(struct sdio_func *func,
179 const struct sdio_device_id *id)
181 struct device_node *np = func->dev.of_node;
182 struct wfx_sdio_priv *bus;
185 if (func->num != 1) {
186 dev_err(&func->dev, "SDIO function number is %d while it should always be 1 (unsupported chip?)\n",
191 bus = devm_kzalloc(&func->dev, sizeof(*bus), GFP_KERNEL);
196 if (!of_match_node(wfx_sdio_of_match, np)) {
197 dev_warn(&func->dev, "no compatible device found in DT\n");
200 bus->of_irq = irq_of_parse_and_map(np, 0);
203 "device is not declared in DT, features will be limited\n");
204 // FIXME: ignore VID/PID and only rely on device tree
209 sdio_set_drvdata(func, bus);
210 func->card->quirks |= MMC_QUIRK_LENIENT_FN0 |
211 MMC_QUIRK_BLKSZ_FOR_BYTE_MODE |
212 MMC_QUIRK_BROKEN_BYTE_MODE_512;
214 sdio_claim_host(func);
215 ret = sdio_enable_func(func);
216 // Block of 64 bytes is more efficient than 512B for frame sizes < 4k
217 sdio_set_block_size(func, 64);
218 sdio_release_host(func);
222 bus->core = wfx_init_common(&func->dev, &wfx_sdio_pdata,
223 &wfx_sdio_hwbus_ops, bus);
229 ret = wfx_probe(bus->core);
236 sdio_claim_host(func);
237 sdio_disable_func(func);
238 sdio_release_host(func);
243 static void wfx_sdio_remove(struct sdio_func *func)
245 struct wfx_sdio_priv *bus = sdio_get_drvdata(func);
247 wfx_release(bus->core);
248 sdio_claim_host(func);
249 sdio_disable_func(func);
250 sdio_release_host(func);
253 #define SDIO_VENDOR_ID_SILABS 0x0000
254 #define SDIO_DEVICE_ID_SILABS_WF200 0x1000
255 static const struct sdio_device_id wfx_sdio_ids[] = {
256 { SDIO_DEVICE(SDIO_VENDOR_ID_SILABS, SDIO_DEVICE_ID_SILABS_WF200) },
257 // FIXME: ignore VID/PID and only rely on device tree
258 // { SDIO_DEVICE(SDIO_ANY_ID, SDIO_ANY_ID) },
261 MODULE_DEVICE_TABLE(sdio, wfx_sdio_ids);
263 struct sdio_driver wfx_sdio_driver = {
265 .id_table = wfx_sdio_ids,
266 .probe = wfx_sdio_probe,
267 .remove = wfx_sdio_remove,
269 .owner = THIS_MODULE,
270 .of_match_table = wfx_sdio_of_match,