1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * SDIO interface.
4 *
5 * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
6 * Copyright (c) 2010, ST-Ericsson
7 */
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>
15
16 #include "bus.h"
17 #include "wfx.h"
18 #include "hwio.h"
19 #include "main.h"
20 #include "bh.h"
21
22 static const struct wfx_platform_data wfx_sdio_pdata = {
23 .file_fw = "wfm_wf200",
24 .file_pds = "wf200.pds",
25 };
26
27 struct wfx_sdio_priv {
28 struct sdio_func *func;
29 struct wfx_dev *core;
30 u8 buf_id_tx;
31 u8 buf_id_rx;
32 int of_irq;
33 };
34
wfx_sdio_copy_from_io(void * priv,unsigned int reg_id,void * dst,size_t count)35 static int wfx_sdio_copy_from_io(void *priv, unsigned int reg_id,
36 void *dst, size_t count)
37 {
38 struct wfx_sdio_priv *bus = priv;
39 unsigned int sdio_addr = reg_id << 2;
40 int ret;
41
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");
45
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;
52
53 return ret;
54 }
55
wfx_sdio_copy_to_io(void * priv,unsigned int reg_id,const void * src,size_t count)56 static int wfx_sdio_copy_to_io(void *priv, unsigned int reg_id,
57 const void *src, size_t count)
58 {
59 struct wfx_sdio_priv *bus = priv;
60 unsigned int sdio_addr = reg_id << 2;
61 int ret;
62
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");
66
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;
74
75 return ret;
76 }
77
wfx_sdio_lock(void * priv)78 static void wfx_sdio_lock(void *priv)
79 {
80 struct wfx_sdio_priv *bus = priv;
81
82 sdio_claim_host(bus->func);
83 }
84
wfx_sdio_unlock(void * priv)85 static void wfx_sdio_unlock(void *priv)
86 {
87 struct wfx_sdio_priv *bus = priv;
88
89 sdio_release_host(bus->func);
90 }
91
wfx_sdio_irq_handler(struct sdio_func * func)92 static void wfx_sdio_irq_handler(struct sdio_func *func)
93 {
94 struct wfx_sdio_priv *bus = sdio_get_drvdata(func);
95
96 wfx_bh_request_rx(bus->core);
97 }
98
wfx_sdio_irq_handler_ext(int irq,void * priv)99 static irqreturn_t wfx_sdio_irq_handler_ext(int irq, void *priv)
100 {
101 struct wfx_sdio_priv *bus = priv;
102
103 sdio_claim_host(bus->func);
104 wfx_bh_request_rx(bus->core);
105 sdio_release_host(bus->func);
106 return IRQ_HANDLED;
107 }
108
wfx_sdio_irq_subscribe(void * priv)109 static int wfx_sdio_irq_subscribe(void *priv)
110 {
111 struct wfx_sdio_priv *bus = priv;
112 u32 flags;
113 int ret;
114 u8 cccr;
115
116 if (!bus->of_irq) {
117 sdio_claim_host(bus->func);
118 ret = sdio_claim_irq(bus->func, wfx_sdio_irq_handler);
119 sdio_release_host(bus->func);
120 return ret;
121 }
122
123 sdio_claim_host(bus->func);
124 cccr = sdio_f0_readb(bus->func, SDIO_CCCR_IENx, NULL);
125 cccr |= BIT(0);
126 cccr |= BIT(bus->func->num);
127 sdio_f0_writeb(bus->func, cccr, SDIO_CCCR_IENx, NULL);
128 sdio_release_host(bus->func);
129 flags = irq_get_trigger_type(bus->of_irq);
130 if (!flags)
131 flags = IRQF_TRIGGER_HIGH;
132 flags |= IRQF_ONESHOT;
133 return devm_request_threaded_irq(&bus->func->dev, bus->of_irq, NULL,
134 wfx_sdio_irq_handler_ext, flags,
135 "wfx", bus);
136 }
137
wfx_sdio_irq_unsubscribe(void * priv)138 static int wfx_sdio_irq_unsubscribe(void *priv)
139 {
140 struct wfx_sdio_priv *bus = priv;
141 int ret;
142
143 if (bus->of_irq)
144 devm_free_irq(&bus->func->dev, bus->of_irq, bus);
145 sdio_claim_host(bus->func);
146 ret = sdio_release_irq(bus->func);
147 sdio_release_host(bus->func);
148 return ret;
149 }
150
wfx_sdio_align_size(void * priv,size_t size)151 static size_t wfx_sdio_align_size(void *priv, size_t size)
152 {
153 struct wfx_sdio_priv *bus = priv;
154
155 return sdio_align_size(bus->func, size);
156 }
157
158 static const struct hwbus_ops wfx_sdio_hwbus_ops = {
159 .copy_from_io = wfx_sdio_copy_from_io,
160 .copy_to_io = wfx_sdio_copy_to_io,
161 .irq_subscribe = wfx_sdio_irq_subscribe,
162 .irq_unsubscribe = wfx_sdio_irq_unsubscribe,
163 .lock = wfx_sdio_lock,
164 .unlock = wfx_sdio_unlock,
165 .align_size = wfx_sdio_align_size,
166 };
167
168 static const struct of_device_id wfx_sdio_of_match[] = {
169 { .compatible = "silabs,wfx-sdio" },
170 { .compatible = "silabs,wf200" },
171 { },
172 };
173 MODULE_DEVICE_TABLE(of, wfx_sdio_of_match);
174
wfx_sdio_probe(struct sdio_func * func,const struct sdio_device_id * id)175 static int wfx_sdio_probe(struct sdio_func *func,
176 const struct sdio_device_id *id)
177 {
178 struct device_node *np = func->dev.of_node;
179 struct wfx_sdio_priv *bus;
180 int ret;
181
182 if (func->num != 1) {
183 dev_err(&func->dev, "SDIO function number is %d while it should always be 1 (unsupported chip?)\n",
184 func->num);
185 return -ENODEV;
186 }
187
188 bus = devm_kzalloc(&func->dev, sizeof(*bus), GFP_KERNEL);
189 if (!bus)
190 return -ENOMEM;
191
192 if (np) {
193 if (!of_match_node(wfx_sdio_of_match, np)) {
194 dev_warn(&func->dev, "no compatible device found in DT\n");
195 return -ENODEV;
196 }
197 bus->of_irq = irq_of_parse_and_map(np, 0);
198 } else {
199 dev_warn(&func->dev,
200 "device is not declared in DT, features will be limited\n");
201 // FIXME: ignore VID/PID and only rely on device tree
202 // return -ENODEV;
203 }
204
205 bus->func = func;
206 sdio_set_drvdata(func, bus);
207 func->card->quirks |= MMC_QUIRK_LENIENT_FN0 |
208 MMC_QUIRK_BLKSZ_FOR_BYTE_MODE |
209 MMC_QUIRK_BROKEN_BYTE_MODE_512;
210
211 sdio_claim_host(func);
212 ret = sdio_enable_func(func);
213 // Block of 64 bytes is more efficient than 512B for frame sizes < 4k
214 sdio_set_block_size(func, 64);
215 sdio_release_host(func);
216 if (ret)
217 goto err0;
218
219 bus->core = wfx_init_common(&func->dev, &wfx_sdio_pdata,
220 &wfx_sdio_hwbus_ops, bus);
221 if (!bus->core) {
222 ret = -EIO;
223 goto err1;
224 }
225
226 ret = wfx_probe(bus->core);
227 if (ret)
228 goto err1;
229
230 return 0;
231
232 err1:
233 sdio_claim_host(func);
234 sdio_disable_func(func);
235 sdio_release_host(func);
236 err0:
237 return ret;
238 }
239
wfx_sdio_remove(struct sdio_func * func)240 static void wfx_sdio_remove(struct sdio_func *func)
241 {
242 struct wfx_sdio_priv *bus = sdio_get_drvdata(func);
243
244 wfx_release(bus->core);
245 sdio_claim_host(func);
246 sdio_disable_func(func);
247 sdio_release_host(func);
248 }
249
250 #define SDIO_VENDOR_ID_SILABS 0x0000
251 #define SDIO_DEVICE_ID_SILABS_WF200 0x1000
252 static const struct sdio_device_id wfx_sdio_ids[] = {
253 { SDIO_DEVICE(SDIO_VENDOR_ID_SILABS, SDIO_DEVICE_ID_SILABS_WF200) },
254 // FIXME: ignore VID/PID and only rely on device tree
255 // { SDIO_DEVICE(SDIO_ANY_ID, SDIO_ANY_ID) },
256 { },
257 };
258 MODULE_DEVICE_TABLE(sdio, wfx_sdio_ids);
259
260 struct sdio_driver wfx_sdio_driver = {
261 .name = "wfx-sdio",
262 .id_table = wfx_sdio_ids,
263 .probe = wfx_sdio_probe,
264 .remove = wfx_sdio_remove,
265 .drv = {
266 .owner = THIS_MODULE,
267 .of_match_table = wfx_sdio_of_match,
268 }
269 };
270