2 * SDIO device driver API.
4 * Copyright (C) 2007-2008 Cambridge Silicon Radio Ltd.
6 * Refer to LICENSE.txt included with this source code for details on
13 * @defgroup fdriver SDIO function driver API
15 * @brief The SDIO function driver API is used to implement drivers
16 * for SDIO card functions.
18 * Function drivers register with the SDIO driver core
19 * (sdio_register_driver()), listing which functions it supports and
20 * providing callback functions for card inserts, removes and
23 * @par \anchor card_io_ops Card I/O operations:
25 * - \link sdioemb_read8(struct sdioemb_dev *, uint32_t, uint8_t *) sdioemb_read8()\endlink
26 * - \link sdioemb_read16(struct sdioemb_dev *, uint32_t, uint16_t *) sdioemb_read16()\endlink
27 * - \link sdioemb_write8(struct sdioemb_dev *, uint32_t, uint8_t) sdioemb_write8()\endlink
28 * - \link sdioemb_write16(struct sdioemb_dev *, uint32_t, uint16_t) sdioemb_write16()\endlink
29 * - \link sdioemb_f0_read8(struct sdioemb_dev *, uint32_t, uint8_t *) sdioemb_f0_read8()\endlink
30 * - \link sdioemb_f0_write8(struct sdioemb_dev *, uint32_t, uint8_t) sdioemb_f0_write8()\endlink
31 * - \link sdioemb_read(struct sdioemb_dev *, uint32_t, void *, size_t) sdioemb_read()\endlink
32 * - \link sdioemb_write(struct sdioemb_dev *, uint32_t, const void *, size_t) sdioemb_write()\endlink
35 struct sdioemb_func_driver;
37 struct sdioemb_dev_priv;
42 * Each SDIO card will have an sdio_dev for each function.
44 * None of the fields (except for drv_data) should be written.
49 struct sdioemb_func_driver *driver; /**< Function driver for this device. */
50 uint16_t vendor_id; /**< Vendor ID of the card. */
51 uint16_t device_id; /**< Device ID of the card. */
52 int function; /**< Function number of this device. */
53 uint8_t interface; /**< SDIO standard interface number. */
54 uint16_t max_blocksize; /**< Maximum block size supported. */
55 uint16_t blocksize; /**< Blocksize in use. */
56 int slot_id; /**< ID of the slot this card is inserted into. */
57 void * os_device; /**< Pointer to an OS-specific device structure. */
58 struct sdioemb_dev_priv *priv; /**< Data private to the SDIO core. */
59 void * drv_data; /**< Data private to the function driver. */
62 #define SDIOEMB_ANY_ID 0xffff
63 #define SDIOEMB_UIF_FUNC 0
64 #define SDIOEMB_ANY_FUNC 0xff
65 #define SDIOEMB_ANY_IFACE 0xff
68 * An entry for an SDIO device ID table.
70 * Functions are matched to drivers using any combination of vendor
71 * ID, device ID, function number or standard interface.
73 * Matching on #function == SDIOEMB_UIF_FUNC is reserved for the SDIO
74 * Userspace Interface driver. Card management drivers can match on
75 * #function == 0, these will be probed before any function drivers.
79 struct sdioemb_id_table {
80 uint16_t vendor_id; /**< Vendor ID to match or SDIOEMB_ANY_ID */
81 uint16_t device_id; /**< Device ID to match or SDIOEMB_ANY_ID */
82 int function; /**< Function number to match or SDIOEMB_ANY_FUNC */
83 uint8_t interface; /**< SDIO standard interface to match or SDIOEMB_ANY_IFACE */
87 * A driver for an SDIO function.
91 struct sdioemb_func_driver {
93 * Driver name used in diagnostics.
98 * 0 terminated array of functions supported by this device.
100 * The driver may (for example) match on a number of vendor
101 * ID/device ID/function number triplets or on an SDIO standard
104 struct sdioemb_id_table *id_table;
107 * Called by the core when an inserted card has functions which
108 * match those listed in id_table.
110 * The driver's implementation should (if required):
112 * - perform any additional probing
113 * - do function specific initialization
114 * - allocate and register any function/OS specific devices or interfaces.
116 * Called in: thread context.
118 * @param fdev the newly inserted device.
120 * @return 0 on success; -ve on error.
122 int (*probe)(struct sdioemb_dev *fdev);
125 * Called by the core when a card is removed. This is only called
126 * if the probe() call succeeded.
128 * The driver's implementation should (if required);
130 * - do any function specific shutdown.
131 * - cleanup any data structures created/registers during probe().
133 * Called in: thread context.
135 * @param fdev the device being removed.
137 void (*remove)(struct sdioemb_dev *fdev);
140 * Called by the core to signal an SDIO interrupt for this card
141 * occurs, if interrupts have been enabled with
142 * sdioemb_interrupt_enable().
144 * The driver's implementation should signal a thread (or similar)
145 * to actually handle the interrupt as no card I/O may be
146 * performed whilst in interrupt context. When the interrupt is
147 * handled, the driver should call sdioemb_interrupt_acknowledge() to
148 * enable further interrupts to be signalled.
150 * Called in: interrupt context.
152 * @param fdev the device which may have raised the interrupt.
154 void (*card_int_handler)(struct sdioemb_dev *fdev);
157 * Called by the core to signal a suspend power management
160 * The driver's implementation should (if required)
161 * set the card to a low power mode and return as soon
162 * as possible. After this function returns, the
163 * driver should not start any SDIO commands.
165 * Called in: thread context.
167 * @param fdev the device handler.
169 void (*suspend)(struct sdioemb_dev *fdev);
172 * Called by the core to signal a resume power management
175 * The driver's implementation should (if required)
176 * initialise the card to an operational mode and return
177 * as soon as possible. If the card has been powered off
178 * during suspend, the driver would have to initialise
179 * the card from scratch (f/w download, h/w initialisation, etc.).
181 * Called in: thread context.
183 * @param fdev the device handler.
185 void (*resume)(struct sdioemb_dev *fdev);
188 int sdioemb_driver_register(struct sdioemb_func_driver *fdriver);
189 void sdioemb_driver_unregister(struct sdioemb_func_driver *fdriver);
191 int sdioemb_driver_probe(struct sdioemb_func_driver *fdriver, struct sdioemb_dev *fdev);
192 void sdioemb_driver_remove(struct sdioemb_func_driver *fdriver, struct sdioemb_dev *fdev);
194 /* For backward compatibility. */
195 #define sdio_register_driver sdioemb_driver_register
196 #define sdio_unregister_driver sdioemb_driver_unregister
198 int sdioemb_set_block_size(struct sdioemb_dev *fdev, uint16_t blksz);
199 void sdioemb_set_max_bus_freq(struct sdioemb_dev *fdev, int max_freq);
200 int sdioemb_set_bus_width(struct sdioemb_dev *fdev, int bus_width);
202 int sdioemb_enable_function(struct sdioemb_dev *fdev);
203 int sdioemb_disable_function(struct sdioemb_dev *fdev);
204 int sdioemb_reenable_csr_function(struct sdioemb_dev *dev);
205 void sdioemb_idle_function(struct sdioemb_dev *fdev);
207 int sdioemb_read8(struct sdioemb_dev *fdev, uint32_t addr, uint8_t *val);
208 int sdioemb_read16(struct sdioemb_dev *fdev, uint32_t addr, uint16_t *val);
209 int sdioemb_write8(struct sdioemb_dev *fdev, uint32_t addr, uint8_t val);
210 int sdioemb_write16(struct sdioemb_dev *fdev, uint32_t addr, uint16_t val);
211 int sdioemb_f0_read8(struct sdioemb_dev *fdev, uint32_t addr, uint8_t *val);
212 int sdioemb_f0_write8(struct sdioemb_dev *fdev, uint32_t addr, uint8_t val);
213 int sdioemb_read(struct sdioemb_dev *fdev, uint32_t addr, void *data, size_t len);
214 int sdioemb_write(struct sdioemb_dev *fdev, uint32_t addr, const void *data, size_t len);
216 int sdioemb_hard_reset(struct sdioemb_dev *fdev);
218 void sdioemb_power_on(struct sdioemb_dev *fdev);
219 void sdioemb_power_off(struct sdioemb_dev *fdev);
221 int sdioemb_interrupt_enable(struct sdioemb_dev *fdev);
222 int sdioemb_interrupt_disable(struct sdioemb_dev *fdev);
223 void sdioemb_interrupt_acknowledge(struct sdioemb_dev *fdev);
225 int sdioemb_cis_get_tuple(struct sdioemb_dev *fdev, uint8_t tuple,
226 void *buf, size_t len);
228 void sdioemb_suspend_function(struct sdioemb_dev *fdev);
229 void sdioemb_resume_function(struct sdioemb_dev *fdev);
232 * SDIO command status.
236 enum sdioemb_cmd_status {
237 SDIOEMB_CMD_OK = 0x00, /**< Command successful. */
239 SDIOEMB_CMD_ERR_CMD = 0x01,
240 SDIOEMB_CMD_ERR_DAT = 0x02,
242 SDIOEMB_CMD_ERR_CRC = 0x10,
243 SDIOEMB_CMD_ERR_TIMEOUT = 0x20,
244 SDIOEMB_CMD_ERR_OTHER = 0x40,
246 SDIOEMB_CMD_ERR_CMD_CRC = SDIOEMB_CMD_ERR_CMD | SDIOEMB_CMD_ERR_CRC, /**< Response CRC error. */
247 SDIOEMB_CMD_ERR_CMD_TIMEOUT = SDIOEMB_CMD_ERR_CMD | SDIOEMB_CMD_ERR_TIMEOUT, /**< Response time out. */
248 SDIOEMB_CMD_ERR_CMD_OTHER = SDIOEMB_CMD_ERR_CMD | SDIOEMB_CMD_ERR_OTHER, /**< Other response error. */
249 SDIOEMB_CMD_ERR_DAT_CRC = SDIOEMB_CMD_ERR_DAT | SDIOEMB_CMD_ERR_CRC, /**< Data CRC error. */
250 SDIOEMB_CMD_ERR_DAT_TIMEOUT = SDIOEMB_CMD_ERR_DAT | SDIOEMB_CMD_ERR_TIMEOUT, /**< Data receive time out. */
251 SDIOEMB_CMD_ERR_DAT_OTHER = SDIOEMB_CMD_ERR_DAT | SDIOEMB_CMD_ERR_OTHER, /**< Other data error. */
253 SDIOEMB_CMD_ERR_NO_CARD = 0x04, /**< No card present. */
255 SDIOEMB_CMD_IN_PROGRESS = 0xff, /**< Command still in progress. */
259 * A response to an SDIO command.
261 * For R1, R4, R5, and R6 responses only the middle 32 bits of the
262 * response are stored, the leading octet (start and direction bits
263 * and command index) and trailing octet (CRC and stop bit) are
266 * @bug R2 and R3 responses are not used by SDIO and are not
271 union sdioemb_response {
279 * SDIO command parameters and response.
281 struct sdioemb_cmd_resp {
282 uint8_t cmd; /**< Command index (0 to 63). */
283 uint32_t arg; /**< Command argument. */
284 union sdioemb_response response; /**< Response to the command. Valid
285 iff the command has completed and
286 (sdio_cmd::status & SDIOEMB_CMD_ERR_CMD) == 0.*/
290 * CSPI command parameters and response.
292 struct cspi_cmd_resp {
293 unsigned cmd : 8; /**< Command octet (type, and function). */
294 unsigned addr: 24; /**< 24 bit address. */
295 uint16_t val; /**< Word to write or read from the card (for non-burst commands). */
296 uint8_t response; /**< Response octet. Valid iff the command has completed and
297 (sdio_cmd::status & SDIOEMB_CMD_ERR_CMD) == 0. */
302 * An SDIO command, its status and response.
304 * sdio_cmd is used to submit SDIO commands to a device and return its
305 * status and any response or data.
311 * The SDIO device which submitted the command. Set by the
314 struct sdioemb_dev *owner;
317 * Called by the core when the command has been completed.
319 * Called in: interrupt context.
321 * @param cmd the completed command.
323 void (*callback)(struct sdioemb_cmd *cmd);
326 * Set of flags specifying the response type, data transfer
327 * direction and other parameters.
329 * For SDIO commands set at least one of the response types:
330 * - #SDIOEMB_CMD_FLAG_RESP_NONE
331 * - #SDIOEMB_CMD_FLAG_RESP_R1
332 * - #SDIOEMB_CMD_FLAG_RESP_R1B
333 * - #SDIOEMB_CMD_FLAG_RESP_R2
334 * - #SDIOEMB_CMD_FLAG_RESP_R3
335 * - #SDIOEMB_CMD_FLAG_RESP_R4
336 * - #SDIOEMB_CMD_FLAG_RESP_R5
337 * - #SDIOEMB_CMD_FLAG_RESP_R5B
338 * - #SDIOEMB_CMD_FLAG_RESP_R6
340 * and any of the additional flags:
341 * - #SDIOEMB_CMD_FLAG_READ
343 * For CSPI commands set:
344 * - #SDIOEMB_CMD_FLAG_CSPI
349 * SDIO command parameters and response.
351 * Valid only if #SDIOEMB_CMD_FLAG_CSPI is \e not set in #flags.
353 struct sdioemb_cmd_resp sdio;
356 * CSPI command parameters and response.
358 * Valid only if #SDIOEMB_CMD_FLAG_CSPI is set in #flags.
360 struct cspi_cmd_resp cspi;
363 * Buffer of data to read or write.
365 * Must be set to NULL if the command is not a data transfer.
370 * Length of #data in octets.
372 * len must be either: less than the device's sdio_dev::blocksize;
373 * or a multiple of the device's sdio_dev::blocksize.
378 * Status of the command after it has completed.
380 enum sdioemb_cmd_status status;
383 * Data private to caller of sdioemb_start_cmd().
388 /** @addtogroup fdriver
390 #define SDIOEMB_CMD_FLAG_RESP_NONE 0x00 /**< No response. */
391 #define SDIOEMB_CMD_FLAG_RESP_R1 0x01 /**< R1 response. */
392 #define SDIOEMB_CMD_FLAG_RESP_R1B 0x02 /**< R1b response. */
393 #define SDIOEMB_CMD_FLAG_RESP_R2 0x03 /**< R2 response. */
394 #define SDIOEMB_CMD_FLAG_RESP_R3 0x04 /**< R3 response. */
395 #define SDIOEMB_CMD_FLAG_RESP_R4 0x05 /**< R4 response. */
396 #define SDIOEMB_CMD_FLAG_RESP_R5 0x06 /**< R5 response. */
397 #define SDIOEMB_CMD_FLAG_RESP_R5B 0x07 /**< R5b response. */
398 #define SDIOEMB_CMD_FLAG_RESP_R6 0x08 /**< R6 response. */
399 #define SDIOEMB_CMD_FLAG_RESP_MASK 0xff /**< Mask for response type. */
400 #define SDIOEMB_CMD_FLAG_RAW 0x0100 /**< @internal Bypass the command queues. */
401 #define SDIOEMB_CMD_FLAG_READ 0x0200 /**< Data transfer is a read, not a write. */
402 #define SDIOEMB_CMD_FLAG_CSPI 0x0400 /**< CSPI transfer, not SDIO or SDIO-SPI. */
403 #define SDIOEMB_CMD_FLAG_ABORT 0x0800 /**< Data transfer abort command. */
406 int sdioemb_start_cmd(struct sdioemb_dev *fdev, struct sdioemb_cmd *cmd);
408 #endif /* #ifndef _SDIO_API_H */