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/mmc/sdio.h>
9 #include <linux/mmc/sdio_func.h>
10 #include <linux/mmc/card.h>
11 #include <linux/of_irq.h>
16 static const struct wfx_platform_data wfx_sdio_pdata = {
17 .file_fw = "wfm_wf200",
18 .file_pds = "wf200.pds",
21 struct wfx_sdio_priv {
22 struct sdio_func *func;
29 static int wfx_sdio_copy_from_io(void *priv, unsigned int reg_id,
30 void *dst, size_t count)
32 struct wfx_sdio_priv *bus = priv;
33 unsigned int sdio_addr = reg_id << 2;
36 WARN(reg_id > 7, "chip only has 7 registers");
37 WARN(((uintptr_t)dst) & 3, "unaligned buffer size");
38 WARN(count & 3, "unaligned buffer address");
40 /* Use queue mode buffers */
41 if (reg_id == WFX_REG_IN_OUT_QUEUE)
42 sdio_addr |= (bus->buf_id_rx + 1) << 7;
43 ret = sdio_memcpy_fromio(bus->func, dst, sdio_addr, count);
44 if (!ret && reg_id == WFX_REG_IN_OUT_QUEUE)
45 bus->buf_id_rx = (bus->buf_id_rx + 1) % 4;
50 static int wfx_sdio_copy_to_io(void *priv, unsigned int reg_id,
51 const void *src, size_t count)
53 struct wfx_sdio_priv *bus = priv;
54 unsigned int sdio_addr = reg_id << 2;
57 WARN(reg_id > 7, "chip only has 7 registers");
58 WARN(((uintptr_t)src) & 3, "unaligned buffer size");
59 WARN(count & 3, "unaligned buffer address");
61 /* Use queue mode buffers */
62 if (reg_id == WFX_REG_IN_OUT_QUEUE)
63 sdio_addr |= bus->buf_id_tx << 7;
64 // FIXME: discards 'const' qualifier for src
65 ret = sdio_memcpy_toio(bus->func, sdio_addr, (void *)src, count);
66 if (!ret && reg_id == WFX_REG_IN_OUT_QUEUE)
67 bus->buf_id_tx = (bus->buf_id_tx + 1) % 32;
72 static void wfx_sdio_lock(void *priv)
74 struct wfx_sdio_priv *bus = priv;
76 sdio_claim_host(bus->func);
79 static void wfx_sdio_unlock(void *priv)
81 struct wfx_sdio_priv *bus = priv;
83 sdio_release_host(bus->func);
86 static void wfx_sdio_irq_handler(struct sdio_func *func)
88 struct wfx_sdio_priv *bus = sdio_get_drvdata(func);
90 wfx_bh_request_rx(bus->core);
93 static irqreturn_t wfx_sdio_irq_handler_ext(int irq, void *priv)
95 struct wfx_sdio_priv *bus = priv;
97 sdio_claim_host(bus->func);
98 wfx_bh_request_rx(bus->core);
99 sdio_release_host(bus->func);
103 static int wfx_sdio_irq_subscribe(void *priv)
105 struct wfx_sdio_priv *bus = priv;
111 sdio_claim_host(bus->func);
112 ret = sdio_claim_irq(bus->func, wfx_sdio_irq_handler);
113 sdio_release_host(bus->func);
117 sdio_claim_host(bus->func);
118 cccr = sdio_f0_readb(bus->func, SDIO_CCCR_IENx, NULL);
120 cccr |= BIT(bus->func->num);
121 sdio_f0_writeb(bus->func, cccr, SDIO_CCCR_IENx, NULL);
122 sdio_release_host(bus->func);
123 flags = irq_get_trigger_type(bus->of_irq);
125 flags = IRQF_TRIGGER_HIGH;
126 flags |= IRQF_ONESHOT;
127 return devm_request_threaded_irq(&bus->func->dev, bus->of_irq, NULL,
128 wfx_sdio_irq_handler_ext, flags,
132 static int wfx_sdio_irq_unsubscribe(void *priv)
134 struct wfx_sdio_priv *bus = priv;
138 devm_free_irq(&bus->func->dev, bus->of_irq, bus);
139 sdio_claim_host(bus->func);
140 ret = sdio_release_irq(bus->func);
141 sdio_release_host(bus->func);
145 static size_t wfx_sdio_align_size(void *priv, size_t size)
147 struct wfx_sdio_priv *bus = priv;
149 return sdio_align_size(bus->func, size);
152 static const struct hwbus_ops wfx_sdio_hwbus_ops = {
153 .copy_from_io = wfx_sdio_copy_from_io,
154 .copy_to_io = wfx_sdio_copy_to_io,
155 .irq_subscribe = wfx_sdio_irq_subscribe,
156 .irq_unsubscribe = wfx_sdio_irq_unsubscribe,
157 .lock = wfx_sdio_lock,
158 .unlock = wfx_sdio_unlock,
159 .align_size = wfx_sdio_align_size,
162 static const struct of_device_id wfx_sdio_of_match[] = {
163 { .compatible = "silabs,wfx-sdio" },
164 { .compatible = "silabs,wf200" },
167 MODULE_DEVICE_TABLE(of, wfx_sdio_of_match);
169 static int wfx_sdio_probe(struct sdio_func *func,
170 const struct sdio_device_id *id)
172 struct device_node *np = func->dev.of_node;
173 struct wfx_sdio_priv *bus;
176 if (func->num != 1) {
177 dev_err(&func->dev, "SDIO function number is %d while it should always be 1 (unsupported chip?)\n",
182 bus = devm_kzalloc(&func->dev, sizeof(*bus), GFP_KERNEL);
187 if (!of_match_node(wfx_sdio_of_match, np)) {
188 dev_warn(&func->dev, "no compatible device found in DT\n");
191 bus->of_irq = irq_of_parse_and_map(np, 0);
194 "device is not declared in DT, features will be limited\n");
195 // FIXME: ignore VID/PID and only rely on device tree
200 sdio_set_drvdata(func, bus);
201 func->card->quirks |= MMC_QUIRK_LENIENT_FN0 |
202 MMC_QUIRK_BLKSZ_FOR_BYTE_MODE |
203 MMC_QUIRK_BROKEN_BYTE_MODE_512;
205 sdio_claim_host(func);
206 ret = sdio_enable_func(func);
207 // Block of 64 bytes is more efficient than 512B for frame sizes < 4k
208 sdio_set_block_size(func, 64);
209 sdio_release_host(func);
213 bus->core = wfx_init_common(&func->dev, &wfx_sdio_pdata,
214 &wfx_sdio_hwbus_ops, bus);
220 ret = wfx_probe(bus->core);
227 sdio_claim_host(func);
228 sdio_disable_func(func);
229 sdio_release_host(func);
234 static void wfx_sdio_remove(struct sdio_func *func)
236 struct wfx_sdio_priv *bus = sdio_get_drvdata(func);
238 wfx_release(bus->core);
239 sdio_claim_host(func);
240 sdio_disable_func(func);
241 sdio_release_host(func);
244 #define SDIO_VENDOR_ID_SILABS 0x0000
245 #define SDIO_DEVICE_ID_SILABS_WF200 0x1000
246 static const struct sdio_device_id wfx_sdio_ids[] = {
247 { SDIO_DEVICE(SDIO_VENDOR_ID_SILABS, SDIO_DEVICE_ID_SILABS_WF200) },
248 // FIXME: ignore VID/PID and only rely on device tree
249 // { SDIO_DEVICE(SDIO_ANY_ID, SDIO_ANY_ID) },
252 MODULE_DEVICE_TABLE(sdio, wfx_sdio_ids);
254 struct sdio_driver wfx_sdio_driver = {
256 .id_table = wfx_sdio_ids,
257 .probe = wfx_sdio_probe,
258 .remove = wfx_sdio_remove,
260 .owner = THIS_MODULE,
261 .of_match_table = wfx_sdio_of_match,