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 flags = irq_get_trigger_type(bus->of_irq);
124 if (!flags)
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,
129 "wfx", bus);
130 if (ret)
131 return ret;
132 sdio_claim_host(bus->func);
133 cccr = sdio_f0_readb(bus->func, SDIO_CCCR_IENx, NULL);
134 cccr |= BIT(0);
135 cccr |= BIT(bus->func->num);
136 sdio_f0_writeb(bus->func, cccr, SDIO_CCCR_IENx, NULL);
137 sdio_release_host(bus->func);
138 return 0;
139 }
140
wfx_sdio_irq_unsubscribe(void * priv)141 static int wfx_sdio_irq_unsubscribe(void *priv)
142 {
143 struct wfx_sdio_priv *bus = priv;
144 int ret;
145
146 if (bus->of_irq)
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);
151 return ret;
152 }
153
wfx_sdio_align_size(void * priv,size_t size)154 static size_t wfx_sdio_align_size(void *priv, size_t size)
155 {
156 struct wfx_sdio_priv *bus = priv;
157
158 return sdio_align_size(bus->func, size);
159 }
160
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,
169 };
170
171 static const struct of_device_id wfx_sdio_of_match[] = {
172 { .compatible = "silabs,wfx-sdio" },
173 { .compatible = "silabs,wf200" },
174 { },
175 };
176 MODULE_DEVICE_TABLE(of, wfx_sdio_of_match);
177
wfx_sdio_probe(struct sdio_func * func,const struct sdio_device_id * id)178 static int wfx_sdio_probe(struct sdio_func *func,
179 const struct sdio_device_id *id)
180 {
181 struct device_node *np = func->dev.of_node;
182 struct wfx_sdio_priv *bus;
183 int ret;
184
185 if (func->num != 1) {
186 dev_err(&func->dev, "SDIO function number is %d while it should always be 1 (unsupported chip?)\n",
187 func->num);
188 return -ENODEV;
189 }
190
191 bus = devm_kzalloc(&func->dev, sizeof(*bus), GFP_KERNEL);
192 if (!bus)
193 return -ENOMEM;
194
195 if (np) {
196 if (!of_match_node(wfx_sdio_of_match, np)) {
197 dev_warn(&func->dev, "no compatible device found in DT\n");
198 return -ENODEV;
199 }
200 bus->of_irq = irq_of_parse_and_map(np, 0);
201 } else {
202 dev_warn(&func->dev,
203 "device is not declared in DT, features will be limited\n");
204 // FIXME: ignore VID/PID and only rely on device tree
205 // return -ENODEV;
206 }
207
208 bus->func = func;
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;
213
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);
219 if (ret)
220 goto err0;
221
222 bus->core = wfx_init_common(&func->dev, &wfx_sdio_pdata,
223 &wfx_sdio_hwbus_ops, bus);
224 if (!bus->core) {
225 ret = -EIO;
226 goto err1;
227 }
228
229 ret = wfx_probe(bus->core);
230 if (ret)
231 goto err1;
232
233 return 0;
234
235 err1:
236 sdio_claim_host(func);
237 sdio_disable_func(func);
238 sdio_release_host(func);
239 err0:
240 return ret;
241 }
242
wfx_sdio_remove(struct sdio_func * func)243 static void wfx_sdio_remove(struct sdio_func *func)
244 {
245 struct wfx_sdio_priv *bus = sdio_get_drvdata(func);
246
247 wfx_release(bus->core);
248 sdio_claim_host(func);
249 sdio_disable_func(func);
250 sdio_release_host(func);
251 }
252
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) },
259 { },
260 };
261 MODULE_DEVICE_TABLE(sdio, wfx_sdio_ids);
262
263 struct sdio_driver wfx_sdio_driver = {
264 .name = "wfx-sdio",
265 .id_table = wfx_sdio_ids,
266 .probe = wfx_sdio_probe,
267 .remove = wfx_sdio_remove,
268 .drv = {
269 .owner = THIS_MODULE,
270 .of_match_table = wfx_sdio_of_match,
271 }
272 };
273