1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * smssdio.c - Siano 1xxx SDIO interface driver
5 * Copyright 2008 Pierre Ossman
7 * Based on code by Siano Mobile Silicon, Inc.,
8 * Copyright (C) 2006-2008, Uri Shkolnik
10 * This hardware is a bit odd in that all transfers should be done
11 * to/from the SMSSDIO_DATA register, yet the "increase address" bit
12 * always needs to be set.
14 * Also, buffers from the card are always aligned to 128 byte
19 * General cleanup notes:
21 * - only typedefs should be name *_t
23 * - use ERR_PTR and friends for smscore_register_device()
25 * - smscore_getbuffer should zero fields
30 #include "smscoreapi.h"
32 #include <linux/moduleparam.h>
33 #include <linux/slab.h>
34 #include <linux/firmware.h>
35 #include <linux/delay.h>
36 #include <linux/mmc/card.h>
37 #include <linux/mmc/sdio_func.h>
38 #include <linux/mmc/sdio_ids.h>
39 #include <linux/module.h>
41 #include "sms-cards.h"
42 #include "smsendian.h"
46 #define SMSSDIO_DATA 0x00
47 #define SMSSDIO_INT 0x04
48 #define SMSSDIO_BLOCK_SIZE 128
50 static const struct sdio_device_id smssdio_ids
[] = {
51 {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO
, SDIO_DEVICE_ID_SIANO_STELLAR
),
52 .driver_data
= SMS1XXX_BOARD_SIANO_STELLAR
},
53 {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO
, SDIO_DEVICE_ID_SIANO_NOVA_A0
),
54 .driver_data
= SMS1XXX_BOARD_SIANO_NOVA_A
},
55 {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO
, SDIO_DEVICE_ID_SIANO_NOVA_B0
),
56 .driver_data
= SMS1XXX_BOARD_SIANO_NOVA_B
},
57 {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO
, SDIO_DEVICE_ID_SIANO_VEGA_A0
),
58 .driver_data
= SMS1XXX_BOARD_SIANO_VEGA
},
59 {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO
, SDIO_DEVICE_ID_SIANO_VENICE
),
60 .driver_data
= SMS1XXX_BOARD_SIANO_VEGA
},
61 {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO
, SDIO_DEVICE_ID_SIANO_MING
),
62 .driver_data
= SMS1XXX_BOARD_SIANO_MING
},
63 {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO
, SDIO_DEVICE_ID_SIANO_PELE
),
64 .driver_data
= SMS1XXX_BOARD_SIANO_PELE
},
65 {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO
, SDIO_DEVICE_ID_SIANO_RIO
),
66 .driver_data
= SMS1XXX_BOARD_SIANO_RIO
},
67 {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO
, SDIO_DEVICE_ID_SIANO_DENVER_2160
),
68 .driver_data
= SMS1XXX_BOARD_SIANO_DENVER_2160
},
69 {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO
, SDIO_DEVICE_ID_SIANO_DENVER_1530
),
70 .driver_data
= SMS1XXX_BOARD_SIANO_DENVER_1530
},
71 { /* end: all zeroes */ },
74 MODULE_DEVICE_TABLE(sdio
, smssdio_ids
);
76 struct smssdio_device
{
77 struct sdio_func
*func
;
79 struct smscore_device_t
*coredev
;
81 struct smscore_buffer_t
*split_cb
;
84 /*******************************************************************/
85 /* Siano core callbacks */
86 /*******************************************************************/
88 static int smssdio_sendrequest(void *context
, void *buffer
, size_t size
)
91 struct smssdio_device
*smsdev
;
95 sdio_claim_host(smsdev
->func
);
97 smsendian_handle_tx_message((struct sms_msg_data
*) buffer
);
98 while (size
>= smsdev
->func
->cur_blksize
) {
99 ret
= sdio_memcpy_toio(smsdev
->func
, SMSSDIO_DATA
,
100 buffer
, smsdev
->func
->cur_blksize
);
104 buffer
+= smsdev
->func
->cur_blksize
;
105 size
-= smsdev
->func
->cur_blksize
;
109 ret
= sdio_memcpy_toio(smsdev
->func
, SMSSDIO_DATA
,
114 sdio_release_host(smsdev
->func
);
119 /*******************************************************************/
121 /*******************************************************************/
123 static void smssdio_interrupt(struct sdio_func
*func
)
127 struct smssdio_device
*smsdev
;
128 struct smscore_buffer_t
*cb
;
129 struct sms_msg_hdr
*hdr
;
132 smsdev
= sdio_get_drvdata(func
);
135 * The interrupt register has no defined meaning. It is just
136 * a way of turning of the level triggered interrupt.
138 (void)sdio_readb(func
, SMSSDIO_INT
, &ret
);
140 pr_err("Unable to read interrupt register!\n");
144 if (smsdev
->split_cb
== NULL
) {
145 cb
= smscore_getbuffer(smsdev
->coredev
);
147 pr_err("Unable to allocate data buffer!\n");
151 ret
= sdio_memcpy_fromio(smsdev
->func
,
156 pr_err("Error %d reading initial block!\n", ret
);
162 if (hdr
->msg_flags
& MSG_HDR_FLAG_SPLIT_MSG
) {
163 smsdev
->split_cb
= cb
;
167 if (hdr
->msg_length
> smsdev
->func
->cur_blksize
)
168 size
= hdr
->msg_length
- smsdev
->func
->cur_blksize
;
172 cb
= smsdev
->split_cb
;
175 size
= hdr
->msg_length
- sizeof(struct sms_msg_hdr
);
177 smsdev
->split_cb
= NULL
;
183 buffer
= cb
->p
+ (hdr
->msg_length
- size
);
184 size
= ALIGN(size
, SMSSDIO_BLOCK_SIZE
);
186 BUG_ON(smsdev
->func
->cur_blksize
!= SMSSDIO_BLOCK_SIZE
);
189 * First attempt to transfer all of it in one go...
191 ret
= sdio_memcpy_fromio(smsdev
->func
,
195 if (ret
&& ret
!= -EINVAL
) {
196 smscore_putbuffer(smsdev
->coredev
, cb
);
197 pr_err("Error %d reading data from card!\n", ret
);
202 * ..then fall back to one block at a time if that is
205 * (we have to do this manually because of the
206 * problem with the "increase address" bit)
208 if (ret
== -EINVAL
) {
210 ret
= sdio_memcpy_fromio(smsdev
->func
,
211 buffer
, SMSSDIO_DATA
,
212 smsdev
->func
->cur_blksize
);
214 smscore_putbuffer(smsdev
->coredev
, cb
);
215 pr_err("Error %d reading data from card!\n",
220 buffer
+= smsdev
->func
->cur_blksize
;
221 if (size
> smsdev
->func
->cur_blksize
)
222 size
-= smsdev
->func
->cur_blksize
;
229 cb
->size
= hdr
->msg_length
;
232 smsendian_handle_rx_message((struct sms_msg_data
*) cb
->p
);
233 smscore_onresponse(smsdev
->coredev
, cb
);
236 static int smssdio_probe(struct sdio_func
*func
,
237 const struct sdio_device_id
*id
)
242 struct smssdio_device
*smsdev
;
243 struct smsdevice_params_t params
;
245 board_id
= id
->driver_data
;
247 smsdev
= kzalloc(sizeof(struct smssdio_device
), GFP_KERNEL
);
253 memset(¶ms
, 0, sizeof(struct smsdevice_params_t
));
255 params
.device
= &func
->dev
;
256 params
.buffer_size
= 0x5000; /* ?? */
257 params
.num_buffers
= 22; /* ?? */
258 params
.context
= smsdev
;
260 snprintf(params
.devpath
, sizeof(params
.devpath
),
261 "sdio\\%s", sdio_func_id(func
));
263 params
.sendrequest_handler
= smssdio_sendrequest
;
265 params
.device_type
= sms_get_board(board_id
)->type
;
267 if (params
.device_type
!= SMS_STELLAR
)
268 params
.flags
|= SMS_DEVICE_FAMILY2
;
271 * FIXME: Stellar needs special handling...
277 ret
= smscore_register_device(¶ms
, &smsdev
->coredev
, GFP_DMA
, NULL
);
281 smscore_set_board_id(smsdev
->coredev
, board_id
);
283 sdio_claim_host(func
);
285 ret
= sdio_enable_func(func
);
289 ret
= sdio_set_block_size(func
, SMSSDIO_BLOCK_SIZE
);
293 ret
= sdio_claim_irq(func
, smssdio_interrupt
);
297 sdio_set_drvdata(func
, smsdev
);
299 sdio_release_host(func
);
301 ret
= smscore_start_device(smsdev
->coredev
);
308 sdio_claim_host(func
);
309 sdio_release_irq(func
);
311 sdio_disable_func(func
);
313 sdio_release_host(func
);
314 smscore_unregister_device(smsdev
->coredev
);
321 static void smssdio_remove(struct sdio_func
*func
)
323 struct smssdio_device
*smsdev
;
325 smsdev
= sdio_get_drvdata(func
);
328 if (smsdev
->split_cb
)
329 smscore_putbuffer(smsdev
->coredev
, smsdev
->split_cb
);
331 smscore_unregister_device(smsdev
->coredev
);
333 sdio_claim_host(func
);
334 sdio_release_irq(func
);
335 sdio_disable_func(func
);
336 sdio_release_host(func
);
341 static struct sdio_driver smssdio_driver
= {
343 .id_table
= smssdio_ids
,
344 .probe
= smssdio_probe
,
345 .remove
= smssdio_remove
,
348 /*******************************************************************/
349 /* Module functions */
350 /*******************************************************************/
352 static int __init
smssdio_module_init(void)
356 printk(KERN_INFO
"smssdio: Siano SMS1xxx SDIO driver\n");
357 printk(KERN_INFO
"smssdio: Copyright Pierre Ossman\n");
359 ret
= sdio_register_driver(&smssdio_driver
);
364 static void __exit
smssdio_module_exit(void)
366 sdio_unregister_driver(&smssdio_driver
);
369 module_init(smssdio_module_init
);
370 module_exit(smssdio_module_exit
);
372 MODULE_DESCRIPTION("Siano SMS1xxx SDIO driver");
373 MODULE_AUTHOR("Pierre Ossman");
374 MODULE_LICENSE("GPL");