/*1* linux/drivers/mmc/core/sdio_io.c2*3* Copyright 2007-2008 Pierre Ossman4*5* This program is free software; you can redistribute it and/or modify6* it under the terms of the GNU General Public License as published by7* the Free Software Foundation; either version 2 of the License, or (at8* your option) any later version.9*/1011#include <linux/mmc/host.h>12#include <linux/mmc/card.h>13#include <linux/mmc/sdio.h>14#include <linux/mmc/sdio_func.h>1516#include "sdio_ops.h"1718/**19* sdio_claim_host - exclusively claim a bus for a certain SDIO function20* @func: SDIO function that will be accessed21*22* Claim a bus for a set of operations. The SDIO function given23* is used to figure out which bus is relevant.24*/25void sdio_claim_host(struct sdio_func *func)26{27BUG_ON(!func);28BUG_ON(!func->card);2930mmc_claim_host(func->card->host);31}32EXPORT_SYMBOL_GPL(sdio_claim_host);3334/**35* sdio_release_host - release a bus for a certain SDIO function36* @func: SDIO function that was accessed37*38* Release a bus, allowing others to claim the bus for their39* operations.40*/41void sdio_release_host(struct sdio_func *func)42{43BUG_ON(!func);44BUG_ON(!func->card);4546mmc_release_host(func->card->host);47}48EXPORT_SYMBOL_GPL(sdio_release_host);4950/**51* sdio_enable_func - enables a SDIO function for usage52* @func: SDIO function to enable53*54* Powers up and activates a SDIO function so that register55* access is possible.56*/57int sdio_enable_func(struct sdio_func *func)58{59int ret;60unsigned char reg;61unsigned long timeout;6263BUG_ON(!func);64BUG_ON(!func->card);6566pr_debug("SDIO: Enabling device %s...\n", sdio_func_id(func));6768ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, ®);69if (ret)70goto err;7172reg |= 1 << func->num;7374ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);75if (ret)76goto err;7778timeout = jiffies + msecs_to_jiffies(func->enable_timeout);7980while (1) {81ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IORx, 0, ®);82if (ret)83goto err;84if (reg & (1 << func->num))85break;86ret = -ETIME;87if (time_after(jiffies, timeout))88goto err;89}9091pr_debug("SDIO: Enabled device %s\n", sdio_func_id(func));9293return 0;9495err:96pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func));97return ret;98}99EXPORT_SYMBOL_GPL(sdio_enable_func);100101/**102* sdio_disable_func - disable a SDIO function103* @func: SDIO function to disable104*105* Powers down and deactivates a SDIO function. Register access106* to this function will fail until the function is reenabled.107*/108int sdio_disable_func(struct sdio_func *func)109{110int ret;111unsigned char reg;112113BUG_ON(!func);114BUG_ON(!func->card);115116pr_debug("SDIO: Disabling device %s...\n", sdio_func_id(func));117118ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, ®);119if (ret)120goto err;121122reg &= ~(1 << func->num);123124ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);125if (ret)126goto err;127128pr_debug("SDIO: Disabled device %s\n", sdio_func_id(func));129130return 0;131132err:133pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func));134return -EIO;135}136EXPORT_SYMBOL_GPL(sdio_disable_func);137138/**139* sdio_set_block_size - set the block size of an SDIO function140* @func: SDIO function to change141* @blksz: new block size or 0 to use the default.142*143* The default block size is the largest supported by both the function144* and the host, with a maximum of 512 to ensure that arbitrarily sized145* data transfer use the optimal (least) number of commands.146*147* A driver may call this to override the default block size set by the148* core. This can be used to set a block size greater than the maximum149* that reported by the card; it is the driver's responsibility to ensure150* it uses a value that the card supports.151*152* Returns 0 on success, -EINVAL if the host does not support the153* requested block size, or -EIO (etc.) if one of the resultant FBR block154* size register writes failed.155*156*/157int sdio_set_block_size(struct sdio_func *func, unsigned blksz)158{159int ret;160161if (blksz > func->card->host->max_blk_size)162return -EINVAL;163164if (blksz == 0) {165blksz = min(func->max_blksize, func->card->host->max_blk_size);166blksz = min(blksz, 512u);167}168169ret = mmc_io_rw_direct(func->card, 1, 0,170SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE,171blksz & 0xff, NULL);172if (ret)173return ret;174ret = mmc_io_rw_direct(func->card, 1, 0,175SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE + 1,176(blksz >> 8) & 0xff, NULL);177if (ret)178return ret;179func->cur_blksize = blksz;180return 0;181}182EXPORT_SYMBOL_GPL(sdio_set_block_size);183184/*185* Calculate the maximum byte mode transfer size186*/187static inline unsigned int sdio_max_byte_size(struct sdio_func *func)188{189unsigned mval = min(func->card->host->max_seg_size,190func->card->host->max_blk_size);191192if (mmc_blksz_for_byte_mode(func->card))193mval = min(mval, func->cur_blksize);194else195mval = min(mval, func->max_blksize);196197return min(mval, 512u); /* maximum size for byte mode */198}199200/**201* sdio_align_size - pads a transfer size to a more optimal value202* @func: SDIO function203* @sz: original transfer size204*205* Pads the original data size with a number of extra bytes in206* order to avoid controller bugs and/or performance hits207* (e.g. some controllers revert to PIO for certain sizes).208*209* If possible, it will also adjust the size so that it can be210* handled in just a single request.211*212* Returns the improved size, which might be unmodified.213*/214unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz)215{216unsigned int orig_sz;217unsigned int blk_sz, byte_sz;218unsigned chunk_sz;219220orig_sz = sz;221222/*223* Do a first check with the controller, in case it224* wants to increase the size up to a point where it225* might need more than one block.226*/227sz = mmc_align_data_size(func->card, sz);228229/*230* If we can still do this with just a byte transfer, then231* we're done.232*/233if (sz <= sdio_max_byte_size(func))234return sz;235236if (func->card->cccr.multi_block) {237/*238* Check if the transfer is already block aligned239*/240if ((sz % func->cur_blksize) == 0)241return sz;242243/*244* Realign it so that it can be done with one request,245* and recheck if the controller still likes it.246*/247blk_sz = ((sz + func->cur_blksize - 1) /248func->cur_blksize) * func->cur_blksize;249blk_sz = mmc_align_data_size(func->card, blk_sz);250251/*252* This value is only good if it is still just253* one request.254*/255if ((blk_sz % func->cur_blksize) == 0)256return blk_sz;257258/*259* We failed to do one request, but at least try to260* pad the remainder properly.261*/262byte_sz = mmc_align_data_size(func->card,263sz % func->cur_blksize);264if (byte_sz <= sdio_max_byte_size(func)) {265blk_sz = sz / func->cur_blksize;266return blk_sz * func->cur_blksize + byte_sz;267}268} else {269/*270* We need multiple requests, so first check that the271* controller can handle the chunk size;272*/273chunk_sz = mmc_align_data_size(func->card,274sdio_max_byte_size(func));275if (chunk_sz == sdio_max_byte_size(func)) {276/*277* Fix up the size of the remainder (if any)278*/279byte_sz = orig_sz % chunk_sz;280if (byte_sz) {281byte_sz = mmc_align_data_size(func->card,282byte_sz);283}284285return (orig_sz / chunk_sz) * chunk_sz + byte_sz;286}287}288289/*290* The controller is simply incapable of transferring the size291* we want in decent manner, so just return the original size.292*/293return orig_sz;294}295EXPORT_SYMBOL_GPL(sdio_align_size);296297/* Split an arbitrarily sized data transfer into several298* IO_RW_EXTENDED commands. */299static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,300unsigned addr, int incr_addr, u8 *buf, unsigned size)301{302unsigned remainder = size;303unsigned max_blocks;304int ret;305306/* Do the bulk of the transfer using block mode (if supported). */307if (func->card->cccr.multi_block && (size > sdio_max_byte_size(func))) {308/* Blocks per command is limited by host count, host transfer309* size (we only use a single sg entry) and the maximum for310* IO_RW_EXTENDED of 511 blocks. */311max_blocks = min(func->card->host->max_blk_count,312func->card->host->max_seg_size / func->cur_blksize);313max_blocks = min(max_blocks, 511u);314315while (remainder > func->cur_blksize) {316unsigned blocks;317318blocks = remainder / func->cur_blksize;319if (blocks > max_blocks)320blocks = max_blocks;321size = blocks * func->cur_blksize;322323ret = mmc_io_rw_extended(func->card, write,324func->num, addr, incr_addr, buf,325blocks, func->cur_blksize);326if (ret)327return ret;328329remainder -= size;330buf += size;331if (incr_addr)332addr += size;333}334}335336/* Write the remainder using byte mode. */337while (remainder > 0) {338size = min(remainder, sdio_max_byte_size(func));339340ret = mmc_io_rw_extended(func->card, write, func->num, addr,341incr_addr, buf, 1, size);342if (ret)343return ret;344345remainder -= size;346buf += size;347if (incr_addr)348addr += size;349}350return 0;351}352353/**354* sdio_readb - read a single byte from a SDIO function355* @func: SDIO function to access356* @addr: address to read357* @err_ret: optional status value from transfer358*359* Reads a single byte from the address space of a given SDIO360* function. If there is a problem reading the address, 0xff361* is returned and @err_ret will contain the error code.362*/363u8 sdio_readb(struct sdio_func *func, unsigned int addr, int *err_ret)364{365int ret;366u8 val;367368BUG_ON(!func);369370if (err_ret)371*err_ret = 0;372373ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);374if (ret) {375if (err_ret)376*err_ret = ret;377return 0xFF;378}379380return val;381}382EXPORT_SYMBOL_GPL(sdio_readb);383384/**385* sdio_writeb - write a single byte to a SDIO function386* @func: SDIO function to access387* @b: byte to write388* @addr: address to write to389* @err_ret: optional status value from transfer390*391* Writes a single byte to the address space of a given SDIO392* function. @err_ret will contain the status of the actual393* transfer.394*/395void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, int *err_ret)396{397int ret;398399BUG_ON(!func);400401ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);402if (err_ret)403*err_ret = ret;404}405EXPORT_SYMBOL_GPL(sdio_writeb);406407/**408* sdio_writeb_readb - write and read a byte from SDIO function409* @func: SDIO function to access410* @write_byte: byte to write411* @addr: address to write to412* @err_ret: optional status value from transfer413*414* Performs a RAW (Read after Write) operation as defined by SDIO spec -415* single byte is written to address space of a given SDIO function and416* response is read back from the same address, both using single request.417* If there is a problem with the operation, 0xff is returned and418* @err_ret will contain the error code.419*/420u8 sdio_writeb_readb(struct sdio_func *func, u8 write_byte,421unsigned int addr, int *err_ret)422{423int ret;424u8 val;425426ret = mmc_io_rw_direct(func->card, 1, func->num, addr,427write_byte, &val);428if (err_ret)429*err_ret = ret;430if (ret)431val = 0xff;432433return val;434}435EXPORT_SYMBOL_GPL(sdio_writeb_readb);436437/**438* sdio_memcpy_fromio - read a chunk of memory from a SDIO function439* @func: SDIO function to access440* @dst: buffer to store the data441* @addr: address to begin reading from442* @count: number of bytes to read443*444* Reads from the address space of a given SDIO function. Return445* value indicates if the transfer succeeded or not.446*/447int sdio_memcpy_fromio(struct sdio_func *func, void *dst,448unsigned int addr, int count)449{450return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);451}452EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);453454/**455* sdio_memcpy_toio - write a chunk of memory to a SDIO function456* @func: SDIO function to access457* @addr: address to start writing to458* @src: buffer that contains the data to write459* @count: number of bytes to write460*461* Writes to the address space of a given SDIO function. Return462* value indicates if the transfer succeeded or not.463*/464int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,465void *src, int count)466{467return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);468}469EXPORT_SYMBOL_GPL(sdio_memcpy_toio);470471/**472* sdio_readsb - read from a FIFO on a SDIO function473* @func: SDIO function to access474* @dst: buffer to store the data475* @addr: address of (single byte) FIFO476* @count: number of bytes to read477*478* Reads from the specified FIFO of a given SDIO function. Return479* value indicates if the transfer succeeded or not.480*/481int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,482int count)483{484return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);485}486EXPORT_SYMBOL_GPL(sdio_readsb);487488/**489* sdio_writesb - write to a FIFO of a SDIO function490* @func: SDIO function to access491* @addr: address of (single byte) FIFO492* @src: buffer that contains the data to write493* @count: number of bytes to write494*495* Writes to the specified FIFO of a given SDIO function. Return496* value indicates if the transfer succeeded or not.497*/498int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,499int count)500{501return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);502}503EXPORT_SYMBOL_GPL(sdio_writesb);504505/**506* sdio_readw - read a 16 bit integer from a SDIO function507* @func: SDIO function to access508* @addr: address to read509* @err_ret: optional status value from transfer510*511* Reads a 16 bit integer from the address space of a given SDIO512* function. If there is a problem reading the address, 0xffff513* is returned and @err_ret will contain the error code.514*/515u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret)516{517int ret;518519if (err_ret)520*err_ret = 0;521522ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);523if (ret) {524if (err_ret)525*err_ret = ret;526return 0xFFFF;527}528529return le16_to_cpup((__le16 *)func->tmpbuf);530}531EXPORT_SYMBOL_GPL(sdio_readw);532533/**534* sdio_writew - write a 16 bit integer to a SDIO function535* @func: SDIO function to access536* @b: integer to write537* @addr: address to write to538* @err_ret: optional status value from transfer539*540* Writes a 16 bit integer to the address space of a given SDIO541* function. @err_ret will contain the status of the actual542* transfer.543*/544void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret)545{546int ret;547548*(__le16 *)func->tmpbuf = cpu_to_le16(b);549550ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);551if (err_ret)552*err_ret = ret;553}554EXPORT_SYMBOL_GPL(sdio_writew);555556/**557* sdio_readl - read a 32 bit integer from a SDIO function558* @func: SDIO function to access559* @addr: address to read560* @err_ret: optional status value from transfer561*562* Reads a 32 bit integer from the address space of a given SDIO563* function. If there is a problem reading the address,564* 0xffffffff is returned and @err_ret will contain the error565* code.566*/567u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret)568{569int ret;570571if (err_ret)572*err_ret = 0;573574ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);575if (ret) {576if (err_ret)577*err_ret = ret;578return 0xFFFFFFFF;579}580581return le32_to_cpup((__le32 *)func->tmpbuf);582}583EXPORT_SYMBOL_GPL(sdio_readl);584585/**586* sdio_writel - write a 32 bit integer to a SDIO function587* @func: SDIO function to access588* @b: integer to write589* @addr: address to write to590* @err_ret: optional status value from transfer591*592* Writes a 32 bit integer to the address space of a given SDIO593* function. @err_ret will contain the status of the actual594* transfer.595*/596void sdio_writel(struct sdio_func *func, u32 b, unsigned int addr, int *err_ret)597{598int ret;599600*(__le32 *)func->tmpbuf = cpu_to_le32(b);601602ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);603if (err_ret)604*err_ret = ret;605}606EXPORT_SYMBOL_GPL(sdio_writel);607608/**609* sdio_f0_readb - read a single byte from SDIO function 0610* @func: an SDIO function of the card611* @addr: address to read612* @err_ret: optional status value from transfer613*614* Reads a single byte from the address space of SDIO function 0.615* If there is a problem reading the address, 0xff is returned616* and @err_ret will contain the error code.617*/618unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,619int *err_ret)620{621int ret;622unsigned char val;623624BUG_ON(!func);625626if (err_ret)627*err_ret = 0;628629ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);630if (ret) {631if (err_ret)632*err_ret = ret;633return 0xFF;634}635636return val;637}638EXPORT_SYMBOL_GPL(sdio_f0_readb);639640/**641* sdio_f0_writeb - write a single byte to SDIO function 0642* @func: an SDIO function of the card643* @b: byte to write644* @addr: address to write to645* @err_ret: optional status value from transfer646*647* Writes a single byte to the address space of SDIO function 0.648* @err_ret will contain the status of the actual transfer.649*650* Only writes to the vendor specific CCCR registers (0xF0 -651* 0xFF) are permiited; @err_ret will be set to -EINVAL for *652* writes outside this range.653*/654void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,655int *err_ret)656{657int ret;658659BUG_ON(!func);660661if ((addr < 0xF0 || addr > 0xFF) && (!mmc_card_lenient_fn0(func->card))) {662if (err_ret)663*err_ret = -EINVAL;664return;665}666667ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);668if (err_ret)669*err_ret = ret;670}671EXPORT_SYMBOL_GPL(sdio_f0_writeb);672673/**674* sdio_get_host_pm_caps - get host power management capabilities675* @func: SDIO function attached to host676*677* Returns a capability bitmask corresponding to power management678* features supported by the host controller that the card function679* might rely upon during a system suspend. The host doesn't need680* to be claimed, nor the function active, for this information to be681* obtained.682*/683mmc_pm_flag_t sdio_get_host_pm_caps(struct sdio_func *func)684{685BUG_ON(!func);686BUG_ON(!func->card);687688return func->card->host->pm_caps;689}690EXPORT_SYMBOL_GPL(sdio_get_host_pm_caps);691692/**693* sdio_set_host_pm_flags - set wanted host power management capabilities694* @func: SDIO function attached to host695*696* Set a capability bitmask corresponding to wanted host controller697* power management features for the upcoming suspend state.698* This must be called, if needed, each time the suspend method of699* the function driver is called, and must contain only bits that700* were returned by sdio_get_host_pm_caps().701* The host doesn't need to be claimed, nor the function active,702* for this information to be set.703*/704int sdio_set_host_pm_flags(struct sdio_func *func, mmc_pm_flag_t flags)705{706struct mmc_host *host;707708BUG_ON(!func);709BUG_ON(!func->card);710711host = func->card->host;712713if (flags & ~host->pm_caps)714return -EINVAL;715716/* function suspend methods are serialized, hence no lock needed */717host->pm_flags |= flags;718return 0;719}720EXPORT_SYMBOL_GPL(sdio_set_host_pm_flags);721722723