xref: /openbmc/linux/drivers/media/mmc/siano/smssdio.c (revision 3213486f)
1 /*
2  *  smssdio.c - Siano 1xxx SDIO interface driver
3  *
4  *  Copyright 2008 Pierre Ossman
5  *
6  * Based on code by Siano Mobile Silicon, Inc.,
7  * Copyright (C) 2006-2008, Uri Shkolnik
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or (at
12  * your option) any later version.
13  *
14  *
15  * This hardware is a bit odd in that all transfers should be done
16  * to/from the SMSSDIO_DATA register, yet the "increase address" bit
17  * always needs to be set.
18  *
19  * Also, buffers from the card are always aligned to 128 byte
20  * boundaries.
21  */
22 
23 /*
24  * General cleanup notes:
25  *
26  * - only typedefs should be name *_t
27  *
28  * - use ERR_PTR and friends for smscore_register_device()
29  *
30  * - smscore_getbuffer should zero fields
31  *
32  * Fix stop command
33  */
34 
35 #include "smscoreapi.h"
36 
37 #include <linux/moduleparam.h>
38 #include <linux/slab.h>
39 #include <linux/firmware.h>
40 #include <linux/delay.h>
41 #include <linux/mmc/card.h>
42 #include <linux/mmc/sdio_func.h>
43 #include <linux/mmc/sdio_ids.h>
44 #include <linux/module.h>
45 
46 #include "sms-cards.h"
47 #include "smsendian.h"
48 
49 /* Registers */
50 
51 #define SMSSDIO_DATA		0x00
52 #define SMSSDIO_INT		0x04
53 #define SMSSDIO_BLOCK_SIZE	128
54 
55 static const struct sdio_device_id smssdio_ids[] = {
56 	{SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_STELLAR),
57 	 .driver_data = SMS1XXX_BOARD_SIANO_STELLAR},
58 	{SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_NOVA_A0),
59 	 .driver_data = SMS1XXX_BOARD_SIANO_NOVA_A},
60 	{SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_NOVA_B0),
61 	 .driver_data = SMS1XXX_BOARD_SIANO_NOVA_B},
62 	{SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_VEGA_A0),
63 	 .driver_data = SMS1XXX_BOARD_SIANO_VEGA},
64 	{SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_VENICE),
65 	 .driver_data = SMS1XXX_BOARD_SIANO_VEGA},
66 	{SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, 0x302),
67 	.driver_data = SMS1XXX_BOARD_SIANO_MING},
68 	{SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, 0x500),
69 	.driver_data = SMS1XXX_BOARD_SIANO_PELE},
70 	{SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, 0x600),
71 	.driver_data = SMS1XXX_BOARD_SIANO_RIO},
72 	{SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, 0x700),
73 	.driver_data = SMS1XXX_BOARD_SIANO_DENVER_2160},
74 	{SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, 0x800),
75 	.driver_data = SMS1XXX_BOARD_SIANO_DENVER_1530},
76 	{ /* end: all zeroes */ },
77 };
78 
79 MODULE_DEVICE_TABLE(sdio, smssdio_ids);
80 
81 struct smssdio_device {
82 	struct sdio_func *func;
83 
84 	struct smscore_device_t *coredev;
85 
86 	struct smscore_buffer_t *split_cb;
87 };
88 
89 /*******************************************************************/
90 /* Siano core callbacks                                            */
91 /*******************************************************************/
92 
93 static int smssdio_sendrequest(void *context, void *buffer, size_t size)
94 {
95 	int ret = 0;
96 	struct smssdio_device *smsdev;
97 
98 	smsdev = context;
99 
100 	sdio_claim_host(smsdev->func);
101 
102 	smsendian_handle_tx_message((struct sms_msg_data *) buffer);
103 	while (size >= smsdev->func->cur_blksize) {
104 		ret = sdio_memcpy_toio(smsdev->func, SMSSDIO_DATA,
105 					buffer, smsdev->func->cur_blksize);
106 		if (ret)
107 			goto out;
108 
109 		buffer += smsdev->func->cur_blksize;
110 		size -= smsdev->func->cur_blksize;
111 	}
112 
113 	if (size) {
114 		ret = sdio_memcpy_toio(smsdev->func, SMSSDIO_DATA,
115 					buffer, size);
116 	}
117 
118 out:
119 	sdio_release_host(smsdev->func);
120 
121 	return ret;
122 }
123 
124 /*******************************************************************/
125 /* SDIO callbacks                                                  */
126 /*******************************************************************/
127 
128 static void smssdio_interrupt(struct sdio_func *func)
129 {
130 	int ret;
131 
132 	struct smssdio_device *smsdev;
133 	struct smscore_buffer_t *cb;
134 	struct sms_msg_hdr *hdr;
135 	size_t size;
136 
137 	smsdev = sdio_get_drvdata(func);
138 
139 	/*
140 	 * The interrupt register has no defined meaning. It is just
141 	 * a way of turning of the level triggered interrupt.
142 	 */
143 	(void)sdio_readb(func, SMSSDIO_INT, &ret);
144 	if (ret) {
145 		pr_err("Unable to read interrupt register!\n");
146 		return;
147 	}
148 
149 	if (smsdev->split_cb == NULL) {
150 		cb = smscore_getbuffer(smsdev->coredev);
151 		if (!cb) {
152 			pr_err("Unable to allocate data buffer!\n");
153 			return;
154 		}
155 
156 		ret = sdio_memcpy_fromio(smsdev->func,
157 					 cb->p,
158 					 SMSSDIO_DATA,
159 					 SMSSDIO_BLOCK_SIZE);
160 		if (ret) {
161 			pr_err("Error %d reading initial block!\n", ret);
162 			return;
163 		}
164 
165 		hdr = cb->p;
166 
167 		if (hdr->msg_flags & MSG_HDR_FLAG_SPLIT_MSG) {
168 			smsdev->split_cb = cb;
169 			return;
170 		}
171 
172 		if (hdr->msg_length > smsdev->func->cur_blksize)
173 			size = hdr->msg_length - smsdev->func->cur_blksize;
174 		else
175 			size = 0;
176 	} else {
177 		cb = smsdev->split_cb;
178 		hdr = cb->p;
179 
180 		size = hdr->msg_length - sizeof(struct sms_msg_hdr);
181 
182 		smsdev->split_cb = NULL;
183 	}
184 
185 	if (size) {
186 		void *buffer;
187 
188 		buffer = cb->p + (hdr->msg_length - size);
189 		size = ALIGN(size, SMSSDIO_BLOCK_SIZE);
190 
191 		BUG_ON(smsdev->func->cur_blksize != SMSSDIO_BLOCK_SIZE);
192 
193 		/*
194 		 * First attempt to transfer all of it in one go...
195 		 */
196 		ret = sdio_memcpy_fromio(smsdev->func,
197 					 buffer,
198 					 SMSSDIO_DATA,
199 					 size);
200 		if (ret && ret != -EINVAL) {
201 			smscore_putbuffer(smsdev->coredev, cb);
202 			pr_err("Error %d reading data from card!\n", ret);
203 			return;
204 		}
205 
206 		/*
207 		 * ..then fall back to one block at a time if that is
208 		 * not possible...
209 		 *
210 		 * (we have to do this manually because of the
211 		 * problem with the "increase address" bit)
212 		 */
213 		if (ret == -EINVAL) {
214 			while (size) {
215 				ret = sdio_memcpy_fromio(smsdev->func,
216 						  buffer, SMSSDIO_DATA,
217 						  smsdev->func->cur_blksize);
218 				if (ret) {
219 					smscore_putbuffer(smsdev->coredev, cb);
220 					pr_err("Error %d reading data from card!\n",
221 					       ret);
222 					return;
223 				}
224 
225 				buffer += smsdev->func->cur_blksize;
226 				if (size > smsdev->func->cur_blksize)
227 					size -= smsdev->func->cur_blksize;
228 				else
229 					size = 0;
230 			}
231 		}
232 	}
233 
234 	cb->size = hdr->msg_length;
235 	cb->offset = 0;
236 
237 	smsendian_handle_rx_message((struct sms_msg_data *) cb->p);
238 	smscore_onresponse(smsdev->coredev, cb);
239 }
240 
241 static int smssdio_probe(struct sdio_func *func,
242 			 const struct sdio_device_id *id)
243 {
244 	int ret;
245 
246 	int board_id;
247 	struct smssdio_device *smsdev;
248 	struct smsdevice_params_t params;
249 
250 	board_id = id->driver_data;
251 
252 	smsdev = kzalloc(sizeof(struct smssdio_device), GFP_KERNEL);
253 	if (!smsdev)
254 		return -ENOMEM;
255 
256 	smsdev->func = func;
257 
258 	memset(&params, 0, sizeof(struct smsdevice_params_t));
259 
260 	params.device = &func->dev;
261 	params.buffer_size = 0x5000;	/* ?? */
262 	params.num_buffers = 22;	/* ?? */
263 	params.context = smsdev;
264 
265 	snprintf(params.devpath, sizeof(params.devpath),
266 		 "sdio\\%s", sdio_func_id(func));
267 
268 	params.sendrequest_handler = smssdio_sendrequest;
269 
270 	params.device_type = sms_get_board(board_id)->type;
271 
272 	if (params.device_type != SMS_STELLAR)
273 		params.flags |= SMS_DEVICE_FAMILY2;
274 	else {
275 		/*
276 		 * FIXME: Stellar needs special handling...
277 		 */
278 		ret = -ENODEV;
279 		goto free;
280 	}
281 
282 	ret = smscore_register_device(&params, &smsdev->coredev, GFP_DMA, NULL);
283 	if (ret < 0)
284 		goto free;
285 
286 	smscore_set_board_id(smsdev->coredev, board_id);
287 
288 	sdio_claim_host(func);
289 
290 	ret = sdio_enable_func(func);
291 	if (ret)
292 		goto release;
293 
294 	ret = sdio_set_block_size(func, SMSSDIO_BLOCK_SIZE);
295 	if (ret)
296 		goto disable;
297 
298 	ret = sdio_claim_irq(func, smssdio_interrupt);
299 	if (ret)
300 		goto disable;
301 
302 	sdio_set_drvdata(func, smsdev);
303 
304 	sdio_release_host(func);
305 
306 	ret = smscore_start_device(smsdev->coredev);
307 	if (ret < 0)
308 		goto reclaim;
309 
310 	return 0;
311 
312 reclaim:
313 	sdio_claim_host(func);
314 	sdio_release_irq(func);
315 disable:
316 	sdio_disable_func(func);
317 release:
318 	sdio_release_host(func);
319 	smscore_unregister_device(smsdev->coredev);
320 free:
321 	kfree(smsdev);
322 
323 	return ret;
324 }
325 
326 static void smssdio_remove(struct sdio_func *func)
327 {
328 	struct smssdio_device *smsdev;
329 
330 	smsdev = sdio_get_drvdata(func);
331 
332 	/* FIXME: racy! */
333 	if (smsdev->split_cb)
334 		smscore_putbuffer(smsdev->coredev, smsdev->split_cb);
335 
336 	smscore_unregister_device(smsdev->coredev);
337 
338 	sdio_claim_host(func);
339 	sdio_release_irq(func);
340 	sdio_disable_func(func);
341 	sdio_release_host(func);
342 
343 	kfree(smsdev);
344 }
345 
346 static struct sdio_driver smssdio_driver = {
347 	.name = "smssdio",
348 	.id_table = smssdio_ids,
349 	.probe = smssdio_probe,
350 	.remove = smssdio_remove,
351 };
352 
353 /*******************************************************************/
354 /* Module functions                                                */
355 /*******************************************************************/
356 
357 static int __init smssdio_module_init(void)
358 {
359 	int ret = 0;
360 
361 	printk(KERN_INFO "smssdio: Siano SMS1xxx SDIO driver\n");
362 	printk(KERN_INFO "smssdio: Copyright Pierre Ossman\n");
363 
364 	ret = sdio_register_driver(&smssdio_driver);
365 
366 	return ret;
367 }
368 
369 static void __exit smssdio_module_exit(void)
370 {
371 	sdio_unregister_driver(&smssdio_driver);
372 }
373 
374 module_init(smssdio_module_init);
375 module_exit(smssdio_module_exit);
376 
377 MODULE_DESCRIPTION("Siano SMS1xxx SDIO driver");
378 MODULE_AUTHOR("Pierre Ossman");
379 MODULE_LICENSE("GPL");
380