123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745 |
- #include <linux/blkdev.h>
- #include <linux/clk.h>
- #include <linux/debugfs.h>
- #include <linux/device.h>
- #include <linux/dmaengine.h>
- #include <linux/dma-mapping.h>
- #include <linux/err.h>
- #include <linux/gpio.h>
- #include <linux/init.h>
- #include <linux/interrupt.h>
- #include <linux/io.h>
- #include <linux/ioport.h>
- #include <linux/module.h>
- #include <linux/of.h>
- #include <linux/of_device.h>
- #include <linux/of_gpio.h>
- #include <linux/platform_device.h>
- #include <linux/scatterlist.h>
- #include <linux/seq_file.h>
- #include <linux/slab.h>
- #include <linux/stat.h>
- #include <linux/types.h>
- #include <linux/mmc/host.h>
- #include <linux/mmc/sdio.h>
- #include <linux/atmel-mci.h>
- #include <linux/atmel_pdc.h>
- #include <linux/pm.h>
- #include <linux/pm_runtime.h>
- #include <linux/pinctrl/consumer.h>
- #include <asm/cacheflush.h>
- #include <asm/io.h>
- #include <asm/unaligned.h>
- #define ATMCI_CR 0x0000
- #define ATMCI_CR_MCIEN BIT(0)
- #define ATMCI_CR_MCIDIS BIT(1)
- #define ATMCI_CR_PWSEN BIT(2)
- #define ATMCI_CR_PWSDIS BIT(3)
- #define ATMCI_CR_SWRST BIT(7)
- #define ATMCI_MR 0x0004
- #define ATMCI_MR_CLKDIV(x) ((x) << 0)
- #define ATMCI_MR_PWSDIV(x) ((x) << 8)
- #define ATMCI_MR_RDPROOF BIT(11)
- #define ATMCI_MR_WRPROOF BIT(12)
- #define ATMCI_MR_PDCFBYTE BIT(13)
- #define ATMCI_MR_PDCPADV BIT(14)
- #define ATMCI_MR_PDCMODE BIT(15)
- #define ATMCI_MR_CLKODD(x) ((x) << 16)
- #define ATMCI_DTOR 0x0008
- #define ATMCI_DTOCYC(x) ((x) << 0)
- #define ATMCI_DTOMUL(x) ((x) << 4)
- #define ATMCI_SDCR 0x000c
- #define ATMCI_SDCSEL_SLOT_A (0 << 0)
- #define ATMCI_SDCSEL_SLOT_B (1 << 0)
- #define ATMCI_SDCSEL_MASK (3 << 0)
- #define ATMCI_SDCBUS_1BIT (0 << 6)
- #define ATMCI_SDCBUS_4BIT (2 << 6)
- #define ATMCI_SDCBUS_8BIT (3 << 6)
- #define ATMCI_SDCBUS_MASK (3 << 6)
- #define ATMCI_ARGR 0x0010
- #define ATMCI_CMDR 0x0014
- #define ATMCI_CMDR_CMDNB(x) ((x) << 0)
- #define ATMCI_CMDR_RSPTYP_NONE (0 << 6)
- #define ATMCI_CMDR_RSPTYP_48BIT (1 << 6)
- #define ATMCI_CMDR_RSPTYP_136BIT (2 << 6)
- #define ATMCI_CMDR_SPCMD_INIT (1 << 8)
- #define ATMCI_CMDR_SPCMD_SYNC (2 << 8)
- #define ATMCI_CMDR_SPCMD_INT (4 << 8)
- #define ATMCI_CMDR_SPCMD_INTRESP (5 << 8)
- #define ATMCI_CMDR_OPDCMD (1 << 11)
- #define ATMCI_CMDR_MAXLAT_5CYC (0 << 12)
- #define ATMCI_CMDR_MAXLAT_64CYC (1 << 12)
- #define ATMCI_CMDR_START_XFER (1 << 16)
- #define ATMCI_CMDR_STOP_XFER (2 << 16)
- #define ATMCI_CMDR_TRDIR_WRITE (0 << 18)
- #define ATMCI_CMDR_TRDIR_READ (1 << 18)
- #define ATMCI_CMDR_BLOCK (0 << 19)
- #define ATMCI_CMDR_MULTI_BLOCK (1 << 19)
- #define ATMCI_CMDR_STREAM (2 << 19)
- #define ATMCI_CMDR_SDIO_BYTE (4 << 19)
- #define ATMCI_CMDR_SDIO_BLOCK (5 << 19)
- #define ATMCI_CMDR_SDIO_SUSPEND (1 << 24)
- #define ATMCI_CMDR_SDIO_RESUME (2 << 24)
- #define ATMCI_BLKR 0x0018
- #define ATMCI_BCNT(x) ((x) << 0)
- #define ATMCI_BLKLEN(x) ((x) << 16)
- #define ATMCI_CSTOR 0x001c
- #define ATMCI_CSTOCYC(x) ((x) << 0)
- #define ATMCI_CSTOMUL(x) ((x) << 4)
- #define ATMCI_RSPR 0x0020
- #define ATMCI_RSPR1 0x0024
- #define ATMCI_RSPR2 0x0028
- #define ATMCI_RSPR3 0x002c
- #define ATMCI_RDR 0x0030
- #define ATMCI_TDR 0x0034
- #define ATMCI_SR 0x0040
- #define ATMCI_IER 0x0044
- #define ATMCI_IDR 0x0048
- #define ATMCI_IMR 0x004c
- #define ATMCI_CMDRDY BIT(0)
- #define ATMCI_RXRDY BIT(1)
- #define ATMCI_TXRDY BIT(2)
- #define ATMCI_BLKE BIT(3)
- #define ATMCI_DTIP BIT(4)
- #define ATMCI_NOTBUSY BIT(5)
- #define ATMCI_ENDRX BIT(6)
- #define ATMCI_ENDTX BIT(7)
- #define ATMCI_SDIOIRQA BIT(8)
- #define ATMCI_SDIOIRQB BIT(9)
- #define ATMCI_SDIOWAIT BIT(12)
- #define ATMCI_CSRCV BIT(13)
- #define ATMCI_RXBUFF BIT(14)
- #define ATMCI_TXBUFE BIT(15)
- #define ATMCI_RINDE BIT(16)
- #define ATMCI_RDIRE BIT(17)
- #define ATMCI_RCRCE BIT(18)
- #define ATMCI_RENDE BIT(19)
- #define ATMCI_RTOE BIT(20)
- #define ATMCI_DCRCE BIT(21)
- #define ATMCI_DTOE BIT(22)
- #define ATMCI_CSTOE BIT(23)
- #define ATMCI_BLKOVRE BIT(24)
- #define ATMCI_DMADONE BIT(25)
- #define ATMCI_FIFOEMPTY BIT(26)
- #define ATMCI_XFRDONE BIT(27)
- #define ATMCI_ACKRCV BIT(28)
- #define ATMCI_ACKRCVE BIT(29)
- #define ATMCI_OVRE BIT(30)
- #define ATMCI_UNRE BIT(31)
- #define ATMCI_DMA 0x0050
- #define ATMCI_DMA_OFFSET(x) ((x) << 0)
- #define ATMCI_DMA_CHKSIZE(x) ((x) << 4)
- #define ATMCI_DMAEN BIT(8)
- #define ATMCI_CFG 0x0054
- #define ATMCI_CFG_FIFOMODE_1DATA BIT(0)
- #define ATMCI_CFG_FERRCTRL_COR BIT(4)
- #define ATMCI_CFG_HSMODE BIT(8)
- #define ATMCI_CFG_LSYNC BIT(12)
- #define ATMCI_WPMR 0x00e4
- #define ATMCI_WP_EN BIT(0)
- #define ATMCI_WP_KEY (0x4d4349 << 8)
- #define ATMCI_WPSR 0x00e8
- #define ATMCI_GET_WP_VS(x) ((x) & 0x0f)
- #define ATMCI_GET_WP_VSRC(x) (((x) >> 8) & 0xffff)
- #define ATMCI_VERSION 0x00FC
- #define ATMCI_FIFO_APERTURE 0x0200
- #define ATMCI_REGS_SIZE 0x100
- #define atmci_readl(port, reg) \
- __raw_readl((port)->regs + reg)
- #define atmci_writel(port, reg, value) \
- __raw_writel((value), (port)->regs + reg)
- #ifdef CONFIG_AVR32
- # define ATMCI_PDC_CONNECTED 0
- #else
- # define ATMCI_PDC_CONNECTED 1
- #endif
- #define AUTOSUSPEND_DELAY 50
- #define ATMCI_DATA_ERROR_FLAGS (ATMCI_DCRCE | ATMCI_DTOE | ATMCI_OVRE | ATMCI_UNRE)
- #define ATMCI_DMA_THRESHOLD 16
- enum {
- EVENT_CMD_RDY = 0,
- EVENT_XFER_COMPLETE,
- EVENT_NOTBUSY,
- EVENT_DATA_ERROR,
- };
- enum atmel_mci_state {
- STATE_IDLE = 0,
- STATE_SENDING_CMD,
- STATE_DATA_XFER,
- STATE_WAITING_NOTBUSY,
- STATE_SENDING_STOP,
- STATE_END_REQUEST,
- };
- enum atmci_xfer_dir {
- XFER_RECEIVE = 0,
- XFER_TRANSMIT,
- };
- enum atmci_pdc_buf {
- PDC_FIRST_BUF = 0,
- PDC_SECOND_BUF,
- };
- struct atmel_mci_caps {
- bool has_dma_conf_reg;
- bool has_pdc;
- bool has_cfg_reg;
- bool has_cstor_reg;
- bool has_highspeed;
- bool has_rwproof;
- bool has_odd_clk_div;
- bool has_bad_data_ordering;
- bool need_reset_after_xfer;
- bool need_blksz_mul_4;
- bool need_notbusy_for_read_ops;
- };
- struct atmel_mci_dma {
- struct dma_chan *chan;
- struct dma_async_tx_descriptor *data_desc;
- };
- struct atmel_mci {
- spinlock_t lock;
- void __iomem *regs;
- struct scatterlist *sg;
- unsigned int sg_len;
- unsigned int pio_offset;
- unsigned int *buffer;
- unsigned int buf_size;
- dma_addr_t buf_phys_addr;
- struct atmel_mci_slot *cur_slot;
- struct mmc_request *mrq;
- struct mmc_command *cmd;
- struct mmc_data *data;
- unsigned int data_size;
- struct atmel_mci_dma dma;
- struct dma_chan *data_chan;
- struct dma_slave_config dma_conf;
- u32 cmd_status;
- u32 data_status;
- u32 stop_cmdr;
- struct tasklet_struct tasklet;
- unsigned long pending_events;
- unsigned long completed_events;
- enum atmel_mci_state state;
- struct list_head queue;
- bool need_clock_update;
- bool need_reset;
- struct timer_list timer;
- u32 mode_reg;
- u32 cfg_reg;
- unsigned long bus_hz;
- unsigned long mapbase;
- struct clk *mck;
- struct platform_device *pdev;
- struct atmel_mci_slot *slot[ATMCI_MAX_NR_SLOTS];
- struct atmel_mci_caps caps;
- u32 (*prepare_data)(struct atmel_mci *host, struct mmc_data *data);
- void (*submit_data)(struct atmel_mci *host, struct mmc_data *data);
- void (*stop_transfer)(struct atmel_mci *host);
- };
- struct atmel_mci_slot {
- struct mmc_host *mmc;
- struct atmel_mci *host;
- u32 sdc_reg;
- u32 sdio_irq;
- struct mmc_request *mrq;
- struct list_head queue_node;
- unsigned int clock;
- unsigned long flags;
- #define ATMCI_CARD_PRESENT 0
- #define ATMCI_CARD_NEED_INIT 1
- #define ATMCI_SHUTDOWN 2
- int detect_pin;
- int wp_pin;
- bool detect_is_active_high;
- struct timer_list detect_timer;
- };
- #define atmci_test_and_clear_pending(host, event) \
- test_and_clear_bit(event, &host->pending_events)
- #define atmci_set_completed(host, event) \
- set_bit(event, &host->completed_events)
- #define atmci_set_pending(host, event) \
- set_bit(event, &host->pending_events)
- static int atmci_req_show(struct seq_file *s, void *v)
- {
- struct atmel_mci_slot *slot = s->private;
- struct mmc_request *mrq;
- struct mmc_command *cmd;
- struct mmc_command *stop;
- struct mmc_data *data;
-
- spin_lock_bh(&slot->host->lock);
- mrq = slot->mrq;
- if (mrq) {
- cmd = mrq->cmd;
- data = mrq->data;
- stop = mrq->stop;
- if (cmd)
- seq_printf(s,
- "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
- cmd->opcode, cmd->arg, cmd->flags,
- cmd->resp[0], cmd->resp[1], cmd->resp[2],
- cmd->resp[3], cmd->error);
- if (data)
- seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
- data->bytes_xfered, data->blocks,
- data->blksz, data->flags, data->error);
- if (stop)
- seq_printf(s,
- "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
- stop->opcode, stop->arg, stop->flags,
- stop->resp[0], stop->resp[1], stop->resp[2],
- stop->resp[3], stop->error);
- }
- spin_unlock_bh(&slot->host->lock);
- return 0;
- }
- static int atmci_req_open(struct inode *inode, struct file *file)
- {
- return single_open(file, atmci_req_show, inode->i_private);
- }
- static const struct file_operations atmci_req_fops = {
- .owner = THIS_MODULE,
- .open = atmci_req_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = single_release,
- };
- static void atmci_show_status_reg(struct seq_file *s,
- const char *regname, u32 value)
- {
- static const char *sr_bit[] = {
- [0] = "CMDRDY",
- [1] = "RXRDY",
- [2] = "TXRDY",
- [3] = "BLKE",
- [4] = "DTIP",
- [5] = "NOTBUSY",
- [6] = "ENDRX",
- [7] = "ENDTX",
- [8] = "SDIOIRQA",
- [9] = "SDIOIRQB",
- [12] = "SDIOWAIT",
- [14] = "RXBUFF",
- [15] = "TXBUFE",
- [16] = "RINDE",
- [17] = "RDIRE",
- [18] = "RCRCE",
- [19] = "RENDE",
- [20] = "RTOE",
- [21] = "DCRCE",
- [22] = "DTOE",
- [23] = "CSTOE",
- [24] = "BLKOVRE",
- [25] = "DMADONE",
- [26] = "FIFOEMPTY",
- [27] = "XFRDONE",
- [30] = "OVRE",
- [31] = "UNRE",
- };
- unsigned int i;
- seq_printf(s, "%s:\t0x%08x", regname, value);
- for (i = 0; i < ARRAY_SIZE(sr_bit); i++) {
- if (value & (1 << i)) {
- if (sr_bit[i])
- seq_printf(s, " %s", sr_bit[i]);
- else
- seq_puts(s, " UNKNOWN");
- }
- }
- seq_putc(s, '\n');
- }
- static int atmci_regs_show(struct seq_file *s, void *v)
- {
- struct atmel_mci *host = s->private;
- u32 *buf;
- int ret = 0;
- buf = kmalloc(ATMCI_REGS_SIZE, GFP_KERNEL);
- if (!buf)
- return -ENOMEM;
- pm_runtime_get_sync(&host->pdev->dev);
-
- spin_lock_bh(&host->lock);
- memcpy_fromio(buf, host->regs, ATMCI_REGS_SIZE);
- spin_unlock_bh(&host->lock);
- pm_runtime_mark_last_busy(&host->pdev->dev);
- pm_runtime_put_autosuspend(&host->pdev->dev);
- seq_printf(s, "MR:\t0x%08x%s%s ",
- buf[ATMCI_MR / 4],
- buf[ATMCI_MR / 4] & ATMCI_MR_RDPROOF ? " RDPROOF" : "",
- buf[ATMCI_MR / 4] & ATMCI_MR_WRPROOF ? " WRPROOF" : "");
- if (host->caps.has_odd_clk_div)
- seq_printf(s, "{CLKDIV,CLKODD}=%u\n",
- ((buf[ATMCI_MR / 4] & 0xff) << 1)
- | ((buf[ATMCI_MR / 4] >> 16) & 1));
- else
- seq_printf(s, "CLKDIV=%u\n",
- (buf[ATMCI_MR / 4] & 0xff));
- seq_printf(s, "DTOR:\t0x%08x\n", buf[ATMCI_DTOR / 4]);
- seq_printf(s, "SDCR:\t0x%08x\n", buf[ATMCI_SDCR / 4]);
- seq_printf(s, "ARGR:\t0x%08x\n", buf[ATMCI_ARGR / 4]);
- seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
- buf[ATMCI_BLKR / 4],
- buf[ATMCI_BLKR / 4] & 0xffff,
- (buf[ATMCI_BLKR / 4] >> 16) & 0xffff);
- if (host->caps.has_cstor_reg)
- seq_printf(s, "CSTOR:\t0x%08x\n", buf[ATMCI_CSTOR / 4]);
-
- atmci_show_status_reg(s, "SR", buf[ATMCI_SR / 4]);
- atmci_show_status_reg(s, "IMR", buf[ATMCI_IMR / 4]);
- if (host->caps.has_dma_conf_reg) {
- u32 val;
- val = buf[ATMCI_DMA / 4];
- seq_printf(s, "DMA:\t0x%08x OFFSET=%u CHKSIZE=%u%s\n",
- val, val & 3,
- ((val >> 4) & 3) ?
- 1 << (((val >> 4) & 3) + 1) : 1,
- val & ATMCI_DMAEN ? " DMAEN" : "");
- }
- if (host->caps.has_cfg_reg) {
- u32 val;
- val = buf[ATMCI_CFG / 4];
- seq_printf(s, "CFG:\t0x%08x%s%s%s%s\n",
- val,
- val & ATMCI_CFG_FIFOMODE_1DATA ? " FIFOMODE_ONE_DATA" : "",
- val & ATMCI_CFG_FERRCTRL_COR ? " FERRCTRL_CLEAR_ON_READ" : "",
- val & ATMCI_CFG_HSMODE ? " HSMODE" : "",
- val & ATMCI_CFG_LSYNC ? " LSYNC" : "");
- }
- kfree(buf);
- return ret;
- }
- static int atmci_regs_open(struct inode *inode, struct file *file)
- {
- return single_open(file, atmci_regs_show, inode->i_private);
- }
- static const struct file_operations atmci_regs_fops = {
- .owner = THIS_MODULE,
- .open = atmci_regs_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = single_release,
- };
- static void atmci_init_debugfs(struct atmel_mci_slot *slot)
- {
- struct mmc_host *mmc = slot->mmc;
- struct atmel_mci *host = slot->host;
- struct dentry *root;
- struct dentry *node;
- root = mmc->debugfs_root;
- if (!root)
- return;
- node = debugfs_create_file("regs", S_IRUSR, root, host,
- &atmci_regs_fops);
- if (IS_ERR(node))
- return;
- if (!node)
- goto err;
- node = debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops);
- if (!node)
- goto err;
- node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
- if (!node)
- goto err;
- node = debugfs_create_x32("pending_events", S_IRUSR, root,
- (u32 *)&host->pending_events);
- if (!node)
- goto err;
- node = debugfs_create_x32("completed_events", S_IRUSR, root,
- (u32 *)&host->completed_events);
- if (!node)
- goto err;
- return;
- err:
- dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
- }
- #if defined(CONFIG_OF)
- static const struct of_device_id atmci_dt_ids[] = {
- { .compatible = "atmel,hsmci" },
- { }
- };
- MODULE_DEVICE_TABLE(of, atmci_dt_ids);
- static struct mci_platform_data*
- atmci_of_init(struct platform_device *pdev)
- {
- struct device_node *np = pdev->dev.of_node;
- struct device_node *cnp;
- struct mci_platform_data *pdata;
- u32 slot_id;
- if (!np) {
- dev_err(&pdev->dev, "device node not found\n");
- return ERR_PTR(-EINVAL);
- }
- pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
- if (!pdata) {
- dev_err(&pdev->dev, "could not allocate memory for pdata\n");
- return ERR_PTR(-ENOMEM);
- }
- for_each_child_of_node(np, cnp) {
- if (of_property_read_u32(cnp, "reg", &slot_id)) {
- dev_warn(&pdev->dev, "reg property is missing for %s\n",
- cnp->full_name);
- continue;
- }
- if (slot_id >= ATMCI_MAX_NR_SLOTS) {
- dev_warn(&pdev->dev, "can't have more than %d slots\n",
- ATMCI_MAX_NR_SLOTS);
- break;
- }
- if (of_property_read_u32(cnp, "bus-width",
- &pdata->slot[slot_id].bus_width))
- pdata->slot[slot_id].bus_width = 1;
- pdata->slot[slot_id].detect_pin =
- of_get_named_gpio(cnp, "cd-gpios", 0);
- pdata->slot[slot_id].detect_is_active_high =
- of_property_read_bool(cnp, "cd-inverted");
- pdata->slot[slot_id].non_removable =
- of_property_read_bool(cnp, "non-removable");
- pdata->slot[slot_id].wp_pin =
- of_get_named_gpio(cnp, "wp-gpios", 0);
- }
- return pdata;
- }
- #else
- static inline struct mci_platform_data*
- atmci_of_init(struct platform_device *dev)
- {
- return ERR_PTR(-EINVAL);
- }
- #endif
- static inline unsigned int atmci_get_version(struct atmel_mci *host)
- {
- return atmci_readl(host, ATMCI_VERSION) & 0x00000fff;
- }
- static inline unsigned int atmci_convert_chksize(struct atmel_mci *host,
- unsigned int maxburst)
- {
- unsigned int version = atmci_get_version(host);
- unsigned int offset = 2;
- if (version >= 0x600)
- offset = 1;
- if (maxburst > 1)
- return fls(maxburst) - offset;
- else
- return 0;
- }
- static void atmci_timeout_timer(unsigned long data)
- {
- struct atmel_mci *host;
- host = (struct atmel_mci *)data;
- dev_dbg(&host->pdev->dev, "software timeout\n");
- if (host->mrq->cmd->data) {
- host->mrq->cmd->data->error = -ETIMEDOUT;
- host->data = NULL;
-
- if (host->state == STATE_DATA_XFER)
- host->stop_transfer(host);
- } else {
- host->mrq->cmd->error = -ETIMEDOUT;
- host->cmd = NULL;
- }
- host->need_reset = 1;
- host->state = STATE_END_REQUEST;
- smp_wmb();
- tasklet_schedule(&host->tasklet);
- }
- static inline unsigned int atmci_ns_to_clocks(struct atmel_mci *host,
- unsigned int ns)
- {
-
- unsigned int us = DIV_ROUND_UP(ns, 1000);
-
- return us * (DIV_ROUND_UP(host->bus_hz, 2000000));
- }
- static void atmci_set_timeout(struct atmel_mci *host,
- struct atmel_mci_slot *slot, struct mmc_data *data)
- {
- static unsigned dtomul_to_shift[] = {
- 0, 4, 7, 8, 10, 12, 16, 20
- };
- unsigned timeout;
- unsigned dtocyc;
- unsigned dtomul;
- timeout = atmci_ns_to_clocks(host, data->timeout_ns)
- + data->timeout_clks;
- for (dtomul = 0; dtomul < 8; dtomul++) {
- unsigned shift = dtomul_to_shift[dtomul];
- dtocyc = (timeout + (1 << shift) - 1) >> shift;
- if (dtocyc < 15)
- break;
- }
- if (dtomul >= 8) {
- dtomul = 7;
- dtocyc = 15;
- }
- dev_vdbg(&slot->mmc->class_dev, "setting timeout to %u cycles\n",
- dtocyc << dtomul_to_shift[dtomul]);
- atmci_writel(host, ATMCI_DTOR, (ATMCI_DTOMUL(dtomul) | ATMCI_DTOCYC(dtocyc)));
- }
- static u32 atmci_prepare_command(struct mmc_host *mmc,
- struct mmc_command *cmd)
- {
- struct mmc_data *data;
- u32 cmdr;
- cmd->error = -EINPROGRESS;
- cmdr = ATMCI_CMDR_CMDNB(cmd->opcode);
- if (cmd->flags & MMC_RSP_PRESENT) {
- if (cmd->flags & MMC_RSP_136)
- cmdr |= ATMCI_CMDR_RSPTYP_136BIT;
- else
- cmdr |= ATMCI_CMDR_RSPTYP_48BIT;
- }
-
- cmdr |= ATMCI_CMDR_MAXLAT_64CYC;
- if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
- cmdr |= ATMCI_CMDR_OPDCMD;
- data = cmd->data;
- if (data) {
- cmdr |= ATMCI_CMDR_START_XFER;
- if (cmd->opcode == SD_IO_RW_EXTENDED) {
- cmdr |= ATMCI_CMDR_SDIO_BLOCK;
- } else {
- if (data->blocks > 1)
- cmdr |= ATMCI_CMDR_MULTI_BLOCK;
- else
- cmdr |= ATMCI_CMDR_BLOCK;
- }
- if (data->flags & MMC_DATA_READ)
- cmdr |= ATMCI_CMDR_TRDIR_READ;
- }
- return cmdr;
- }
- static void atmci_send_command(struct atmel_mci *host,
- struct mmc_command *cmd, u32 cmd_flags)
- {
- WARN_ON(host->cmd);
- host->cmd = cmd;
- dev_vdbg(&host->pdev->dev,
- "start command: ARGR=0x%08x CMDR=0x%08x\n",
- cmd->arg, cmd_flags);
- atmci_writel(host, ATMCI_ARGR, cmd->arg);
- atmci_writel(host, ATMCI_CMDR, cmd_flags);
- }
- static void atmci_send_stop_cmd(struct atmel_mci *host, struct mmc_data *data)
- {
- dev_dbg(&host->pdev->dev, "send stop command\n");
- atmci_send_command(host, data->stop, host->stop_cmdr);
- atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY);
- }
- static void atmci_pdc_set_single_buf(struct atmel_mci *host,
- enum atmci_xfer_dir dir, enum atmci_pdc_buf buf_nb)
- {
- u32 pointer_reg, counter_reg;
- unsigned int buf_size;
- if (dir == XFER_RECEIVE) {
- pointer_reg = ATMEL_PDC_RPR;
- counter_reg = ATMEL_PDC_RCR;
- } else {
- pointer_reg = ATMEL_PDC_TPR;
- counter_reg = ATMEL_PDC_TCR;
- }
- if (buf_nb == PDC_SECOND_BUF) {
- pointer_reg += ATMEL_PDC_SCND_BUF_OFF;
- counter_reg += ATMEL_PDC_SCND_BUF_OFF;
- }
- if (!host->caps.has_rwproof) {
- buf_size = host->buf_size;
- atmci_writel(host, pointer_reg, host->buf_phys_addr);
- } else {
- buf_size = sg_dma_len(host->sg);
- atmci_writel(host, pointer_reg, sg_dma_address(host->sg));
- }
- if (host->data_size <= buf_size) {
- if (host->data_size & 0x3) {
-
- atmci_writel(host, counter_reg, host->data_size);
- atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCFBYTE);
- } else {
-
- atmci_writel(host, counter_reg, host->data_size / 4);
- }
- host->data_size = 0;
- } else {
-
- atmci_writel(host, counter_reg, sg_dma_len(host->sg) / 4);
- host->data_size -= sg_dma_len(host->sg);
- if (host->data_size)
- host->sg = sg_next(host->sg);
- }
- }
- static void atmci_pdc_set_both_buf(struct atmel_mci *host, int dir)
- {
- atmci_pdc_set_single_buf(host, dir, PDC_FIRST_BUF);
- if (host->data_size)
- atmci_pdc_set_single_buf(host, dir, PDC_SECOND_BUF);
- }
- static void atmci_pdc_cleanup(struct atmel_mci *host)
- {
- struct mmc_data *data = host->data;
- if (data)
- dma_unmap_sg(&host->pdev->dev,
- data->sg, data->sg_len,
- ((data->flags & MMC_DATA_WRITE)
- ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
- }
- static void atmci_pdc_complete(struct atmel_mci *host)
- {
- int transfer_size = host->data->blocks * host->data->blksz;
- int i;
- atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
- if ((!host->caps.has_rwproof)
- && (host->data->flags & MMC_DATA_READ)) {
- if (host->caps.has_bad_data_ordering)
- for (i = 0; i < transfer_size; i++)
- host->buffer[i] = swab32(host->buffer[i]);
- sg_copy_from_buffer(host->data->sg, host->data->sg_len,
- host->buffer, transfer_size);
- }
- atmci_pdc_cleanup(host);
- dev_dbg(&host->pdev->dev, "(%s) set pending xfer complete\n", __func__);
- atmci_set_pending(host, EVENT_XFER_COMPLETE);
- tasklet_schedule(&host->tasklet);
- }
- static void atmci_dma_cleanup(struct atmel_mci *host)
- {
- struct mmc_data *data = host->data;
- if (data)
- dma_unmap_sg(host->dma.chan->device->dev,
- data->sg, data->sg_len,
- ((data->flags & MMC_DATA_WRITE)
- ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
- }
- static void atmci_dma_complete(void *arg)
- {
- struct atmel_mci *host = arg;
- struct mmc_data *data = host->data;
- dev_vdbg(&host->pdev->dev, "DMA complete\n");
- if (host->caps.has_dma_conf_reg)
-
- atmci_writel(host, ATMCI_DMA, atmci_readl(host, ATMCI_DMA) & ~ATMCI_DMAEN);
- atmci_dma_cleanup(host);
-
- if (data) {
- dev_dbg(&host->pdev->dev,
- "(%s) set pending xfer complete\n", __func__);
- atmci_set_pending(host, EVENT_XFER_COMPLETE);
- tasklet_schedule(&host->tasklet);
-
- atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
- }
- }
- static u32 atmci_prepare_data(struct atmel_mci *host, struct mmc_data *data)
- {
- u32 iflags;
- data->error = -EINPROGRESS;
- host->sg = data->sg;
- host->sg_len = data->sg_len;
- host->data = data;
- host->data_chan = NULL;
- iflags = ATMCI_DATA_ERROR_FLAGS;
-
- if (data->blocks * data->blksz < 12
- || (data->blocks * data->blksz) & 3)
- host->need_reset = true;
- host->pio_offset = 0;
- if (data->flags & MMC_DATA_READ)
- iflags |= ATMCI_RXRDY;
- else
- iflags |= ATMCI_TXRDY;
- return iflags;
- }
- static u32
- atmci_prepare_data_pdc(struct atmel_mci *host, struct mmc_data *data)
- {
- u32 iflags, tmp;
- unsigned int sg_len;
- enum dma_data_direction dir;
- int i;
- data->error = -EINPROGRESS;
- host->data = data;
- host->sg = data->sg;
- iflags = ATMCI_DATA_ERROR_FLAGS;
-
- atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCMODE);
- if (data->flags & MMC_DATA_READ) {
- dir = DMA_FROM_DEVICE;
- iflags |= ATMCI_ENDRX | ATMCI_RXBUFF;
- } else {
- dir = DMA_TO_DEVICE;
- iflags |= ATMCI_ENDTX | ATMCI_TXBUFE | ATMCI_BLKE;
- }
-
- tmp = atmci_readl(host, ATMCI_MR);
- tmp &= 0x0000ffff;
- tmp |= ATMCI_BLKLEN(data->blksz);
- atmci_writel(host, ATMCI_MR, tmp);
-
- host->data_size = data->blocks * data->blksz;
- sg_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, dir);
- if ((!host->caps.has_rwproof)
- && (host->data->flags & MMC_DATA_WRITE)) {
- sg_copy_to_buffer(host->data->sg, host->data->sg_len,
- host->buffer, host->data_size);
- if (host->caps.has_bad_data_ordering)
- for (i = 0; i < host->data_size; i++)
- host->buffer[i] = swab32(host->buffer[i]);
- }
- if (host->data_size)
- atmci_pdc_set_both_buf(host,
- ((dir == DMA_FROM_DEVICE) ? XFER_RECEIVE : XFER_TRANSMIT));
- return iflags;
- }
- static u32
- atmci_prepare_data_dma(struct atmel_mci *host, struct mmc_data *data)
- {
- struct dma_chan *chan;
- struct dma_async_tx_descriptor *desc;
- struct scatterlist *sg;
- unsigned int i;
- enum dma_data_direction direction;
- enum dma_transfer_direction slave_dirn;
- unsigned int sglen;
- u32 maxburst;
- u32 iflags;
- data->error = -EINPROGRESS;
- WARN_ON(host->data);
- host->sg = NULL;
- host->data = data;
- iflags = ATMCI_DATA_ERROR_FLAGS;
-
- if (data->blocks * data->blksz < ATMCI_DMA_THRESHOLD)
- return atmci_prepare_data(host, data);
- if (data->blksz & 3)
- return atmci_prepare_data(host, data);
- for_each_sg(data->sg, sg, data->sg_len, i) {
- if (sg->offset & 3 || sg->length & 3)
- return atmci_prepare_data(host, data);
- }
-
- chan = host->dma.chan;
- if (chan)
- host->data_chan = chan;
- if (!chan)
- return -ENODEV;
- if (data->flags & MMC_DATA_READ) {
- direction = DMA_FROM_DEVICE;
- host->dma_conf.direction = slave_dirn = DMA_DEV_TO_MEM;
- maxburst = atmci_convert_chksize(host,
- host->dma_conf.src_maxburst);
- } else {
- direction = DMA_TO_DEVICE;
- host->dma_conf.direction = slave_dirn = DMA_MEM_TO_DEV;
- maxburst = atmci_convert_chksize(host,
- host->dma_conf.dst_maxburst);
- }
- if (host->caps.has_dma_conf_reg)
- atmci_writel(host, ATMCI_DMA, ATMCI_DMA_CHKSIZE(maxburst) |
- ATMCI_DMAEN);
- sglen = dma_map_sg(chan->device->dev, data->sg,
- data->sg_len, direction);
- dmaengine_slave_config(chan, &host->dma_conf);
- desc = dmaengine_prep_slave_sg(chan,
- data->sg, sglen, slave_dirn,
- DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
- if (!desc)
- goto unmap_exit;
- host->dma.data_desc = desc;
- desc->callback = atmci_dma_complete;
- desc->callback_param = host;
- return iflags;
- unmap_exit:
- dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, direction);
- return -ENOMEM;
- }
- static void
- atmci_submit_data(struct atmel_mci *host, struct mmc_data *data)
- {
- return;
- }
- static void
- atmci_submit_data_pdc(struct atmel_mci *host, struct mmc_data *data)
- {
- if (data->flags & MMC_DATA_READ)
- atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
- else
- atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
- }
- static void
- atmci_submit_data_dma(struct atmel_mci *host, struct mmc_data *data)
- {
- struct dma_chan *chan = host->data_chan;
- struct dma_async_tx_descriptor *desc = host->dma.data_desc;
- if (chan) {
- dmaengine_submit(desc);
- dma_async_issue_pending(chan);
- }
- }
- static void atmci_stop_transfer(struct atmel_mci *host)
- {
- dev_dbg(&host->pdev->dev,
- "(%s) set pending xfer complete\n", __func__);
- atmci_set_pending(host, EVENT_XFER_COMPLETE);
- atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
- }
- static void atmci_stop_transfer_pdc(struct atmel_mci *host)
- {
- atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
- }
- static void atmci_stop_transfer_dma(struct atmel_mci *host)
- {
- struct dma_chan *chan = host->data_chan;
- if (chan) {
- dmaengine_terminate_all(chan);
- atmci_dma_cleanup(host);
- } else {
-
- dev_dbg(&host->pdev->dev,
- "(%s) set pending xfer complete\n", __func__);
- atmci_set_pending(host, EVENT_XFER_COMPLETE);
- atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
- }
- }
- static void atmci_start_request(struct atmel_mci *host,
- struct atmel_mci_slot *slot)
- {
- struct mmc_request *mrq;
- struct mmc_command *cmd;
- struct mmc_data *data;
- u32 iflags;
- u32 cmdflags;
- mrq = slot->mrq;
- host->cur_slot = slot;
- host->mrq = mrq;
- host->pending_events = 0;
- host->completed_events = 0;
- host->cmd_status = 0;
- host->data_status = 0;
- dev_dbg(&host->pdev->dev, "start request: cmd %u\n", mrq->cmd->opcode);
- if (host->need_reset || host->caps.need_reset_after_xfer) {
- iflags = atmci_readl(host, ATMCI_IMR);
- iflags &= (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB);
- atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
- atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
- atmci_writel(host, ATMCI_MR, host->mode_reg);
- if (host->caps.has_cfg_reg)
- atmci_writel(host, ATMCI_CFG, host->cfg_reg);
- atmci_writel(host, ATMCI_IER, iflags);
- host->need_reset = false;
- }
- atmci_writel(host, ATMCI_SDCR, slot->sdc_reg);
- iflags = atmci_readl(host, ATMCI_IMR);
- if (iflags & ~(ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
- dev_dbg(&slot->mmc->class_dev, "WARNING: IMR=0x%08x\n",
- iflags);
- if (unlikely(test_and_clear_bit(ATMCI_CARD_NEED_INIT, &slot->flags))) {
-
- atmci_writel(host, ATMCI_CMDR, ATMCI_CMDR_SPCMD_INIT);
- while (!(atmci_readl(host, ATMCI_SR) & ATMCI_CMDRDY))
- cpu_relax();
- }
- iflags = 0;
- data = mrq->data;
- if (data) {
- atmci_set_timeout(host, slot, data);
-
- atmci_writel(host, ATMCI_BLKR, ATMCI_BCNT(data->blocks)
- | ATMCI_BLKLEN(data->blksz));
- dev_vdbg(&slot->mmc->class_dev, "BLKR=0x%08x\n",
- ATMCI_BCNT(data->blocks) | ATMCI_BLKLEN(data->blksz));
- iflags |= host->prepare_data(host, data);
- }
- iflags |= ATMCI_CMDRDY;
- cmd = mrq->cmd;
- cmdflags = atmci_prepare_command(slot->mmc, cmd);
-
- if (host->submit_data != &atmci_submit_data_dma)
- atmci_send_command(host, cmd, cmdflags);
- if (data)
- host->submit_data(host, data);
- if (host->submit_data == &atmci_submit_data_dma)
- atmci_send_command(host, cmd, cmdflags);
- if (mrq->stop) {
- host->stop_cmdr = atmci_prepare_command(slot->mmc, mrq->stop);
- host->stop_cmdr |= ATMCI_CMDR_STOP_XFER;
- if (!(data->flags & MMC_DATA_WRITE))
- host->stop_cmdr |= ATMCI_CMDR_TRDIR_READ;
- host->stop_cmdr |= ATMCI_CMDR_MULTI_BLOCK;
- }
-
- atmci_writel(host, ATMCI_IER, iflags);
- mod_timer(&host->timer, jiffies + msecs_to_jiffies(2000));
- }
- static void atmci_queue_request(struct atmel_mci *host,
- struct atmel_mci_slot *slot, struct mmc_request *mrq)
- {
- dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
- host->state);
- spin_lock_bh(&host->lock);
- slot->mrq = mrq;
- if (host->state == STATE_IDLE) {
- host->state = STATE_SENDING_CMD;
- atmci_start_request(host, slot);
- } else {
- dev_dbg(&host->pdev->dev, "queue request\n");
- list_add_tail(&slot->queue_node, &host->queue);
- }
- spin_unlock_bh(&host->lock);
- }
- static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
- {
- struct atmel_mci_slot *slot = mmc_priv(mmc);
- struct atmel_mci *host = slot->host;
- struct mmc_data *data;
- WARN_ON(slot->mrq);
- dev_dbg(&host->pdev->dev, "MRQ: cmd %u\n", mrq->cmd->opcode);
-
- if (!test_bit(ATMCI_CARD_PRESENT, &slot->flags)) {
- mrq->cmd->error = -ENOMEDIUM;
- mmc_request_done(mmc, mrq);
- return;
- }
-
- data = mrq->data;
- if (data && data->blocks > 1 && data->blksz & 3) {
- mrq->cmd->error = -EINVAL;
- mmc_request_done(mmc, mrq);
- }
- atmci_queue_request(host, slot, mrq);
- }
- static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
- {
- struct atmel_mci_slot *slot = mmc_priv(mmc);
- struct atmel_mci *host = slot->host;
- unsigned int i;
- slot->sdc_reg &= ~ATMCI_SDCBUS_MASK;
- switch (ios->bus_width) {
- case MMC_BUS_WIDTH_1:
- slot->sdc_reg |= ATMCI_SDCBUS_1BIT;
- break;
- case MMC_BUS_WIDTH_4:
- slot->sdc_reg |= ATMCI_SDCBUS_4BIT;
- break;
- }
- if (ios->clock) {
- unsigned int clock_min = ~0U;
- int clkdiv;
- spin_lock_bh(&host->lock);
- if (!host->mode_reg) {
- atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
- atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
- if (host->caps.has_cfg_reg)
- atmci_writel(host, ATMCI_CFG, host->cfg_reg);
- }
-
- slot->clock = ios->clock;
- for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
- if (host->slot[i] && host->slot[i]->clock
- && host->slot[i]->clock < clock_min)
- clock_min = host->slot[i]->clock;
- }
-
- if (host->caps.has_odd_clk_div) {
- clkdiv = DIV_ROUND_UP(host->bus_hz, clock_min) - 2;
- if (clkdiv < 0) {
- dev_warn(&mmc->class_dev,
- "clock %u too fast; using %lu\n",
- clock_min, host->bus_hz / 2);
- clkdiv = 0;
- } else if (clkdiv > 511) {
- dev_warn(&mmc->class_dev,
- "clock %u too slow; using %lu\n",
- clock_min, host->bus_hz / (511 + 2));
- clkdiv = 511;
- }
- host->mode_reg = ATMCI_MR_CLKDIV(clkdiv >> 1)
- | ATMCI_MR_CLKODD(clkdiv & 1);
- } else {
- clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * clock_min) - 1;
- if (clkdiv > 255) {
- dev_warn(&mmc->class_dev,
- "clock %u too slow; using %lu\n",
- clock_min, host->bus_hz / (2 * 256));
- clkdiv = 255;
- }
- host->mode_reg = ATMCI_MR_CLKDIV(clkdiv);
- }
-
- if (host->caps.has_rwproof)
- host->mode_reg |= (ATMCI_MR_WRPROOF | ATMCI_MR_RDPROOF);
- if (host->caps.has_cfg_reg) {
-
- if (ios->timing == MMC_TIMING_SD_HS)
- host->cfg_reg |= ATMCI_CFG_HSMODE;
- else
- host->cfg_reg &= ~ATMCI_CFG_HSMODE;
- }
- if (list_empty(&host->queue)) {
- atmci_writel(host, ATMCI_MR, host->mode_reg);
- if (host->caps.has_cfg_reg)
- atmci_writel(host, ATMCI_CFG, host->cfg_reg);
- } else {
- host->need_clock_update = true;
- }
- spin_unlock_bh(&host->lock);
- } else {
- bool any_slot_active = false;
- spin_lock_bh(&host->lock);
- slot->clock = 0;
- for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
- if (host->slot[i] && host->slot[i]->clock) {
- any_slot_active = true;
- break;
- }
- }
- if (!any_slot_active) {
- atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
- if (host->mode_reg) {
- atmci_readl(host, ATMCI_MR);
- }
- host->mode_reg = 0;
- }
- spin_unlock_bh(&host->lock);
- }
- switch (ios->power_mode) {
- case MMC_POWER_OFF:
- if (!IS_ERR(mmc->supply.vmmc))
- mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
- break;
- case MMC_POWER_UP:
- set_bit(ATMCI_CARD_NEED_INIT, &slot->flags);
- if (!IS_ERR(mmc->supply.vmmc))
- mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
- break;
- default:
-
- break;
- }
- }
- static int atmci_get_ro(struct mmc_host *mmc)
- {
- int read_only = -ENOSYS;
- struct atmel_mci_slot *slot = mmc_priv(mmc);
- if (gpio_is_valid(slot->wp_pin)) {
- read_only = gpio_get_value(slot->wp_pin);
- dev_dbg(&mmc->class_dev, "card is %s\n",
- read_only ? "read-only" : "read-write");
- }
- return read_only;
- }
- static int atmci_get_cd(struct mmc_host *mmc)
- {
- int present = -ENOSYS;
- struct atmel_mci_slot *slot = mmc_priv(mmc);
- if (gpio_is_valid(slot->detect_pin)) {
- present = !(gpio_get_value(slot->detect_pin) ^
- slot->detect_is_active_high);
- dev_dbg(&mmc->class_dev, "card is %spresent\n",
- present ? "" : "not ");
- }
- return present;
- }
- static void atmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
- {
- struct atmel_mci_slot *slot = mmc_priv(mmc);
- struct atmel_mci *host = slot->host;
- if (enable)
- atmci_writel(host, ATMCI_IER, slot->sdio_irq);
- else
- atmci_writel(host, ATMCI_IDR, slot->sdio_irq);
- }
- static const struct mmc_host_ops atmci_ops = {
- .request = atmci_request,
- .set_ios = atmci_set_ios,
- .get_ro = atmci_get_ro,
- .get_cd = atmci_get_cd,
- .enable_sdio_irq = atmci_enable_sdio_irq,
- };
- static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq)
- __releases(&host->lock)
- __acquires(&host->lock)
- {
- struct atmel_mci_slot *slot = NULL;
- struct mmc_host *prev_mmc = host->cur_slot->mmc;
- WARN_ON(host->cmd || host->data);
-
- if (host->need_clock_update) {
- atmci_writel(host, ATMCI_MR, host->mode_reg);
- if (host->caps.has_cfg_reg)
- atmci_writel(host, ATMCI_CFG, host->cfg_reg);
- }
- host->cur_slot->mrq = NULL;
- host->mrq = NULL;
- if (!list_empty(&host->queue)) {
- slot = list_entry(host->queue.next,
- struct atmel_mci_slot, queue_node);
- list_del(&slot->queue_node);
- dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
- mmc_hostname(slot->mmc));
- host->state = STATE_SENDING_CMD;
- atmci_start_request(host, slot);
- } else {
- dev_vdbg(&host->pdev->dev, "list empty\n");
- host->state = STATE_IDLE;
- }
- del_timer(&host->timer);
- spin_unlock(&host->lock);
- mmc_request_done(prev_mmc, mrq);
- spin_lock(&host->lock);
- }
- static void atmci_command_complete(struct atmel_mci *host,
- struct mmc_command *cmd)
- {
- u32 status = host->cmd_status;
-
- cmd->resp[0] = atmci_readl(host, ATMCI_RSPR);
- cmd->resp[1] = atmci_readl(host, ATMCI_RSPR);
- cmd->resp[2] = atmci_readl(host, ATMCI_RSPR);
- cmd->resp[3] = atmci_readl(host, ATMCI_RSPR);
- if (status & ATMCI_RTOE)
- cmd->error = -ETIMEDOUT;
- else if ((cmd->flags & MMC_RSP_CRC) && (status & ATMCI_RCRCE))
- cmd->error = -EILSEQ;
- else if (status & (ATMCI_RINDE | ATMCI_RDIRE | ATMCI_RENDE))
- cmd->error = -EIO;
- else if (host->mrq->data && (host->mrq->data->blksz & 3)) {
- if (host->caps.need_blksz_mul_4) {
- cmd->error = -EINVAL;
- host->need_reset = 1;
- }
- } else
- cmd->error = 0;
- }
- static void atmci_detect_change(unsigned long data)
- {
- struct atmel_mci_slot *slot = (struct atmel_mci_slot *)data;
- bool present;
- bool present_old;
-
- smp_rmb();
- if (test_bit(ATMCI_SHUTDOWN, &slot->flags))
- return;
- enable_irq(gpio_to_irq(slot->detect_pin));
- present = !(gpio_get_value(slot->detect_pin) ^
- slot->detect_is_active_high);
- present_old = test_bit(ATMCI_CARD_PRESENT, &slot->flags);
- dev_vdbg(&slot->mmc->class_dev, "detect change: %d (was %d)\n",
- present, present_old);
- if (present != present_old) {
- struct atmel_mci *host = slot->host;
- struct mmc_request *mrq;
- dev_dbg(&slot->mmc->class_dev, "card %s\n",
- present ? "inserted" : "removed");
- spin_lock(&host->lock);
- if (!present)
- clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
- else
- set_bit(ATMCI_CARD_PRESENT, &slot->flags);
-
- mrq = slot->mrq;
- if (mrq) {
- if (mrq == host->mrq) {
-
- atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
- atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
- atmci_writel(host, ATMCI_MR, host->mode_reg);
- if (host->caps.has_cfg_reg)
- atmci_writel(host, ATMCI_CFG, host->cfg_reg);
- host->data = NULL;
- host->cmd = NULL;
- switch (host->state) {
- case STATE_IDLE:
- break;
- case STATE_SENDING_CMD:
- mrq->cmd->error = -ENOMEDIUM;
- if (mrq->data)
- host->stop_transfer(host);
- break;
- case STATE_DATA_XFER:
- mrq->data->error = -ENOMEDIUM;
- host->stop_transfer(host);
- break;
- case STATE_WAITING_NOTBUSY:
- mrq->data->error = -ENOMEDIUM;
- break;
- case STATE_SENDING_STOP:
- mrq->stop->error = -ENOMEDIUM;
- break;
- case STATE_END_REQUEST:
- break;
- }
- atmci_request_end(host, mrq);
- } else {
- list_del(&slot->queue_node);
- mrq->cmd->error = -ENOMEDIUM;
- if (mrq->data)
- mrq->data->error = -ENOMEDIUM;
- if (mrq->stop)
- mrq->stop->error = -ENOMEDIUM;
- spin_unlock(&host->lock);
- mmc_request_done(slot->mmc, mrq);
- spin_lock(&host->lock);
- }
- }
- spin_unlock(&host->lock);
- mmc_detect_change(slot->mmc, 0);
- }
- }
- static void atmci_tasklet_func(unsigned long priv)
- {
- struct atmel_mci *host = (struct atmel_mci *)priv;
- struct mmc_request *mrq = host->mrq;
- struct mmc_data *data = host->data;
- enum atmel_mci_state state = host->state;
- enum atmel_mci_state prev_state;
- u32 status;
- spin_lock(&host->lock);
- state = host->state;
- dev_vdbg(&host->pdev->dev,
- "tasklet: state %u pending/completed/mask %lx/%lx/%x\n",
- state, host->pending_events, host->completed_events,
- atmci_readl(host, ATMCI_IMR));
- do {
- prev_state = state;
- dev_dbg(&host->pdev->dev, "FSM: state=%d\n", state);
- switch (state) {
- case STATE_IDLE:
- break;
- case STATE_SENDING_CMD:
-
- dev_dbg(&host->pdev->dev, "FSM: cmd ready?\n");
- if (!atmci_test_and_clear_pending(host,
- EVENT_CMD_RDY))
- break;
- dev_dbg(&host->pdev->dev, "set completed cmd ready\n");
- host->cmd = NULL;
- atmci_set_completed(host, EVENT_CMD_RDY);
- atmci_command_complete(host, mrq->cmd);
- if (mrq->data) {
- dev_dbg(&host->pdev->dev,
- "command with data transfer");
-
- if (mrq->cmd->error) {
- host->stop_transfer(host);
- host->data = NULL;
- atmci_writel(host, ATMCI_IDR,
- ATMCI_TXRDY | ATMCI_RXRDY
- | ATMCI_DATA_ERROR_FLAGS);
- state = STATE_END_REQUEST;
- } else
- state = STATE_DATA_XFER;
- } else if ((!mrq->data) && (mrq->cmd->flags & MMC_RSP_BUSY)) {
- dev_dbg(&host->pdev->dev,
- "command response need waiting notbusy");
- atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
- state = STATE_WAITING_NOTBUSY;
- } else
- state = STATE_END_REQUEST;
- break;
- case STATE_DATA_XFER:
- if (atmci_test_and_clear_pending(host,
- EVENT_DATA_ERROR)) {
- dev_dbg(&host->pdev->dev, "set completed data error\n");
- atmci_set_completed(host, EVENT_DATA_ERROR);
- state = STATE_END_REQUEST;
- break;
- }
-
- dev_dbg(&host->pdev->dev, "FSM: xfer complete?\n");
- if (!atmci_test_and_clear_pending(host,
- EVENT_XFER_COMPLETE))
- break;
- dev_dbg(&host->pdev->dev,
- "(%s) set completed xfer complete\n",
- __func__);
- atmci_set_completed(host, EVENT_XFER_COMPLETE);
- if (host->caps.need_notbusy_for_read_ops ||
- (host->data->flags & MMC_DATA_WRITE)) {
- atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
- state = STATE_WAITING_NOTBUSY;
- } else if (host->mrq->stop) {
- atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY);
- atmci_send_stop_cmd(host, data);
- state = STATE_SENDING_STOP;
- } else {
- host->data = NULL;
- data->bytes_xfered = data->blocks * data->blksz;
- data->error = 0;
- state = STATE_END_REQUEST;
- }
- break;
- case STATE_WAITING_NOTBUSY:
-
- dev_dbg(&host->pdev->dev, "FSM: not busy?\n");
- if (!atmci_test_and_clear_pending(host,
- EVENT_NOTBUSY))
- break;
- dev_dbg(&host->pdev->dev, "set completed not busy\n");
- atmci_set_completed(host, EVENT_NOTBUSY);
- if (host->data) {
-
- if (host->mrq->stop) {
- atmci_writel(host, ATMCI_IER,
- ATMCI_CMDRDY);
- atmci_send_stop_cmd(host, data);
- state = STATE_SENDING_STOP;
- } else {
- host->data = NULL;
- data->bytes_xfered = data->blocks
- * data->blksz;
- data->error = 0;
- state = STATE_END_REQUEST;
- }
- } else
- state = STATE_END_REQUEST;
- break;
- case STATE_SENDING_STOP:
-
- dev_dbg(&host->pdev->dev, "FSM: cmd ready?\n");
- if (!atmci_test_and_clear_pending(host,
- EVENT_CMD_RDY))
- break;
- dev_dbg(&host->pdev->dev, "FSM: cmd ready\n");
- host->cmd = NULL;
- data->bytes_xfered = data->blocks * data->blksz;
- data->error = 0;
- atmci_command_complete(host, mrq->stop);
- if (mrq->stop->error) {
- host->stop_transfer(host);
- atmci_writel(host, ATMCI_IDR,
- ATMCI_TXRDY | ATMCI_RXRDY
- | ATMCI_DATA_ERROR_FLAGS);
- state = STATE_END_REQUEST;
- } else {
- atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
- state = STATE_WAITING_NOTBUSY;
- }
- host->data = NULL;
- break;
- case STATE_END_REQUEST:
- atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY | ATMCI_RXRDY
- | ATMCI_DATA_ERROR_FLAGS);
- status = host->data_status;
- if (unlikely(status)) {
- host->stop_transfer(host);
- host->data = NULL;
- if (data) {
- if (status & ATMCI_DTOE) {
- data->error = -ETIMEDOUT;
- } else if (status & ATMCI_DCRCE) {
- data->error = -EILSEQ;
- } else {
- data->error = -EIO;
- }
- }
- }
- atmci_request_end(host, host->mrq);
- state = STATE_IDLE;
- break;
- }
- } while (state != prev_state);
- host->state = state;
- spin_unlock(&host->lock);
- }
- static void atmci_read_data_pio(struct atmel_mci *host)
- {
- struct scatterlist *sg = host->sg;
- void *buf = sg_virt(sg);
- unsigned int offset = host->pio_offset;
- struct mmc_data *data = host->data;
- u32 value;
- u32 status;
- unsigned int nbytes = 0;
- do {
- value = atmci_readl(host, ATMCI_RDR);
- if (likely(offset + 4 <= sg->length)) {
- put_unaligned(value, (u32 *)(buf + offset));
- offset += 4;
- nbytes += 4;
- if (offset == sg->length) {
- flush_dcache_page(sg_page(sg));
- host->sg = sg = sg_next(sg);
- host->sg_len--;
- if (!sg || !host->sg_len)
- goto done;
- offset = 0;
- buf = sg_virt(sg);
- }
- } else {
- unsigned int remaining = sg->length - offset;
- memcpy(buf + offset, &value, remaining);
- nbytes += remaining;
- flush_dcache_page(sg_page(sg));
- host->sg = sg = sg_next(sg);
- host->sg_len--;
- if (!sg || !host->sg_len)
- goto done;
- offset = 4 - remaining;
- buf = sg_virt(sg);
- memcpy(buf, (u8 *)&value + remaining, offset);
- nbytes += offset;
- }
- status = atmci_readl(host, ATMCI_SR);
- if (status & ATMCI_DATA_ERROR_FLAGS) {
- atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_RXRDY
- | ATMCI_DATA_ERROR_FLAGS));
- host->data_status = status;
- data->bytes_xfered += nbytes;
- return;
- }
- } while (status & ATMCI_RXRDY);
- host->pio_offset = offset;
- data->bytes_xfered += nbytes;
- return;
- done:
- atmci_writel(host, ATMCI_IDR, ATMCI_RXRDY);
- atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
- data->bytes_xfered += nbytes;
- smp_wmb();
- atmci_set_pending(host, EVENT_XFER_COMPLETE);
- }
- static void atmci_write_data_pio(struct atmel_mci *host)
- {
- struct scatterlist *sg = host->sg;
- void *buf = sg_virt(sg);
- unsigned int offset = host->pio_offset;
- struct mmc_data *data = host->data;
- u32 value;
- u32 status;
- unsigned int nbytes = 0;
- do {
- if (likely(offset + 4 <= sg->length)) {
- value = get_unaligned((u32 *)(buf + offset));
- atmci_writel(host, ATMCI_TDR, value);
- offset += 4;
- nbytes += 4;
- if (offset == sg->length) {
- host->sg = sg = sg_next(sg);
- host->sg_len--;
- if (!sg || !host->sg_len)
- goto done;
- offset = 0;
- buf = sg_virt(sg);
- }
- } else {
- unsigned int remaining = sg->length - offset;
- value = 0;
- memcpy(&value, buf + offset, remaining);
- nbytes += remaining;
- host->sg = sg = sg_next(sg);
- host->sg_len--;
- if (!sg || !host->sg_len) {
- atmci_writel(host, ATMCI_TDR, value);
- goto done;
- }
- offset = 4 - remaining;
- buf = sg_virt(sg);
- memcpy((u8 *)&value + remaining, buf, offset);
- atmci_writel(host, ATMCI_TDR, value);
- nbytes += offset;
- }
- status = atmci_readl(host, ATMCI_SR);
- if (status & ATMCI_DATA_ERROR_FLAGS) {
- atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_TXRDY
- | ATMCI_DATA_ERROR_FLAGS));
- host->data_status = status;
- data->bytes_xfered += nbytes;
- return;
- }
- } while (status & ATMCI_TXRDY);
- host->pio_offset = offset;
- data->bytes_xfered += nbytes;
- return;
- done:
- atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY);
- atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
- data->bytes_xfered += nbytes;
- smp_wmb();
- atmci_set_pending(host, EVENT_XFER_COMPLETE);
- }
- static void atmci_sdio_interrupt(struct atmel_mci *host, u32 status)
- {
- int i;
- for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
- struct atmel_mci_slot *slot = host->slot[i];
- if (slot && (status & slot->sdio_irq)) {
- mmc_signal_sdio_irq(slot->mmc);
- }
- }
- }
- static irqreturn_t atmci_interrupt(int irq, void *dev_id)
- {
- struct atmel_mci *host = dev_id;
- u32 status, mask, pending;
- unsigned int pass_count = 0;
- do {
- status = atmci_readl(host, ATMCI_SR);
- mask = atmci_readl(host, ATMCI_IMR);
- pending = status & mask;
- if (!pending)
- break;
- if (pending & ATMCI_DATA_ERROR_FLAGS) {
- dev_dbg(&host->pdev->dev, "IRQ: data error\n");
- atmci_writel(host, ATMCI_IDR, ATMCI_DATA_ERROR_FLAGS
- | ATMCI_RXRDY | ATMCI_TXRDY
- | ATMCI_ENDRX | ATMCI_ENDTX
- | ATMCI_RXBUFF | ATMCI_TXBUFE);
- host->data_status = status;
- dev_dbg(&host->pdev->dev, "set pending data error\n");
- smp_wmb();
- atmci_set_pending(host, EVENT_DATA_ERROR);
- tasklet_schedule(&host->tasklet);
- }
- if (pending & ATMCI_TXBUFE) {
- dev_dbg(&host->pdev->dev, "IRQ: tx buffer empty\n");
- atmci_writel(host, ATMCI_IDR, ATMCI_TXBUFE);
- atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
-
- if (host->data_size) {
- atmci_pdc_set_both_buf(host, XFER_TRANSMIT);
- atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
- atmci_writel(host, ATMCI_IER, ATMCI_TXBUFE);
- } else {
- atmci_pdc_complete(host);
- }
- } else if (pending & ATMCI_ENDTX) {
- dev_dbg(&host->pdev->dev, "IRQ: end of tx buffer\n");
- atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
- if (host->data_size) {
- atmci_pdc_set_single_buf(host,
- XFER_TRANSMIT, PDC_SECOND_BUF);
- atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
- }
- }
- if (pending & ATMCI_RXBUFF) {
- dev_dbg(&host->pdev->dev, "IRQ: rx buffer full\n");
- atmci_writel(host, ATMCI_IDR, ATMCI_RXBUFF);
- atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
-
- if (host->data_size) {
- atmci_pdc_set_both_buf(host, XFER_RECEIVE);
- atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
- atmci_writel(host, ATMCI_IER, ATMCI_RXBUFF);
- } else {
- atmci_pdc_complete(host);
- }
- } else if (pending & ATMCI_ENDRX) {
- dev_dbg(&host->pdev->dev, "IRQ: end of rx buffer\n");
- atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
- if (host->data_size) {
- atmci_pdc_set_single_buf(host,
- XFER_RECEIVE, PDC_SECOND_BUF);
- atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
- }
- }
-
- if (pending & ATMCI_BLKE) {
- dev_dbg(&host->pdev->dev, "IRQ: blke\n");
- atmci_writel(host, ATMCI_IDR, ATMCI_BLKE);
- smp_wmb();
- dev_dbg(&host->pdev->dev, "set pending notbusy\n");
- atmci_set_pending(host, EVENT_NOTBUSY);
- tasklet_schedule(&host->tasklet);
- }
- if (pending & ATMCI_NOTBUSY) {
- dev_dbg(&host->pdev->dev, "IRQ: not_busy\n");
- atmci_writel(host, ATMCI_IDR, ATMCI_NOTBUSY);
- smp_wmb();
- dev_dbg(&host->pdev->dev, "set pending notbusy\n");
- atmci_set_pending(host, EVENT_NOTBUSY);
- tasklet_schedule(&host->tasklet);
- }
- if (pending & ATMCI_RXRDY)
- atmci_read_data_pio(host);
- if (pending & ATMCI_TXRDY)
- atmci_write_data_pio(host);
- if (pending & ATMCI_CMDRDY) {
- dev_dbg(&host->pdev->dev, "IRQ: cmd ready\n");
- atmci_writel(host, ATMCI_IDR, ATMCI_CMDRDY);
- host->cmd_status = status;
- smp_wmb();
- dev_dbg(&host->pdev->dev, "set pending cmd rdy\n");
- atmci_set_pending(host, EVENT_CMD_RDY);
- tasklet_schedule(&host->tasklet);
- }
- if (pending & (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
- atmci_sdio_interrupt(host, status);
- } while (pass_count++ < 5);
- return pass_count ? IRQ_HANDLED : IRQ_NONE;
- }
- static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
- {
- struct atmel_mci_slot *slot = dev_id;
-
- disable_irq_nosync(irq);
- mod_timer(&slot->detect_timer, jiffies + msecs_to_jiffies(20));
- return IRQ_HANDLED;
- }
- static int atmci_init_slot(struct atmel_mci *host,
- struct mci_slot_pdata *slot_data, unsigned int id,
- u32 sdc_reg, u32 sdio_irq)
- {
- struct mmc_host *mmc;
- struct atmel_mci_slot *slot;
- mmc = mmc_alloc_host(sizeof(struct atmel_mci_slot), &host->pdev->dev);
- if (!mmc)
- return -ENOMEM;
- slot = mmc_priv(mmc);
- slot->mmc = mmc;
- slot->host = host;
- slot->detect_pin = slot_data->detect_pin;
- slot->wp_pin = slot_data->wp_pin;
- slot->detect_is_active_high = slot_data->detect_is_active_high;
- slot->sdc_reg = sdc_reg;
- slot->sdio_irq = sdio_irq;
- dev_dbg(&mmc->class_dev,
- "slot[%u]: bus_width=%u, detect_pin=%d, "
- "detect_is_active_high=%s, wp_pin=%d\n",
- id, slot_data->bus_width, slot_data->detect_pin,
- slot_data->detect_is_active_high ? "true" : "false",
- slot_data->wp_pin);
- mmc->ops = &atmci_ops;
- mmc->f_min = DIV_ROUND_UP(host->bus_hz, 512);
- mmc->f_max = host->bus_hz / 2;
- mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
- if (sdio_irq)
- mmc->caps |= MMC_CAP_SDIO_IRQ;
- if (host->caps.has_highspeed)
- mmc->caps |= MMC_CAP_SD_HIGHSPEED;
-
- if ((slot_data->bus_width >= 4) && host->caps.has_rwproof)
- mmc->caps |= MMC_CAP_4_BIT_DATA;
- if (atmci_get_version(host) < 0x200) {
- mmc->max_segs = 256;
- mmc->max_blk_size = 4095;
- mmc->max_blk_count = 256;
- mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
- mmc->max_seg_size = mmc->max_blk_size * mmc->max_segs;
- } else {
- mmc->max_segs = 64;
- mmc->max_req_size = 32768 * 512;
- mmc->max_blk_size = 32768;
- mmc->max_blk_count = 512;
- }
-
- set_bit(ATMCI_CARD_PRESENT, &slot->flags);
- if (gpio_is_valid(slot->detect_pin)) {
- if (devm_gpio_request(&host->pdev->dev, slot->detect_pin,
- "mmc_detect")) {
- dev_dbg(&mmc->class_dev, "no detect pin available\n");
- slot->detect_pin = -EBUSY;
- } else if (gpio_get_value(slot->detect_pin) ^
- slot->detect_is_active_high) {
- clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
- }
- }
- if (!gpio_is_valid(slot->detect_pin)) {
- if (slot_data->non_removable)
- mmc->caps |= MMC_CAP_NONREMOVABLE;
- else
- mmc->caps |= MMC_CAP_NEEDS_POLL;
- }
- if (gpio_is_valid(slot->wp_pin)) {
- if (devm_gpio_request(&host->pdev->dev, slot->wp_pin,
- "mmc_wp")) {
- dev_dbg(&mmc->class_dev, "no WP pin available\n");
- slot->wp_pin = -EBUSY;
- }
- }
- host->slot[id] = slot;
- mmc_regulator_get_supply(mmc);
- mmc_add_host(mmc);
- if (gpio_is_valid(slot->detect_pin)) {
- int ret;
- setup_timer(&slot->detect_timer, atmci_detect_change,
- (unsigned long)slot);
- ret = request_irq(gpio_to_irq(slot->detect_pin),
- atmci_detect_interrupt,
- IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
- "mmc-detect", slot);
- if (ret) {
- dev_dbg(&mmc->class_dev,
- "could not request IRQ %d for detect pin\n",
- gpio_to_irq(slot->detect_pin));
- slot->detect_pin = -EBUSY;
- }
- }
- atmci_init_debugfs(slot);
- return 0;
- }
- static void atmci_cleanup_slot(struct atmel_mci_slot *slot,
- unsigned int id)
- {
-
- set_bit(ATMCI_SHUTDOWN, &slot->flags);
- smp_wmb();
- mmc_remove_host(slot->mmc);
- if (gpio_is_valid(slot->detect_pin)) {
- int pin = slot->detect_pin;
- free_irq(gpio_to_irq(pin), slot);
- del_timer_sync(&slot->detect_timer);
- }
- slot->host->slot[id] = NULL;
- mmc_free_host(slot->mmc);
- }
- static int atmci_configure_dma(struct atmel_mci *host)
- {
- host->dma.chan = dma_request_slave_channel_reason(&host->pdev->dev,
- "rxtx");
- if (PTR_ERR(host->dma.chan) == -ENODEV) {
- struct mci_platform_data *pdata = host->pdev->dev.platform_data;
- dma_cap_mask_t mask;
- if (!pdata || !pdata->dma_filter)
- return -ENODEV;
- dma_cap_zero(mask);
- dma_cap_set(DMA_SLAVE, mask);
- host->dma.chan = dma_request_channel(mask, pdata->dma_filter,
- pdata->dma_slave);
- if (!host->dma.chan)
- host->dma.chan = ERR_PTR(-ENODEV);
- }
- if (IS_ERR(host->dma.chan))
- return PTR_ERR(host->dma.chan);
- dev_info(&host->pdev->dev, "using %s for DMA transfers\n",
- dma_chan_name(host->dma.chan));
- host->dma_conf.src_addr = host->mapbase + ATMCI_RDR;
- host->dma_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
- host->dma_conf.src_maxburst = 1;
- host->dma_conf.dst_addr = host->mapbase + ATMCI_TDR;
- host->dma_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
- host->dma_conf.dst_maxburst = 1;
- host->dma_conf.device_fc = false;
- return 0;
- }
- static void atmci_get_cap(struct atmel_mci *host)
- {
- unsigned int version;
- version = atmci_get_version(host);
- dev_info(&host->pdev->dev,
- "version: 0x%x\n", version);
- host->caps.has_dma_conf_reg = 0;
- host->caps.has_pdc = ATMCI_PDC_CONNECTED;
- host->caps.has_cfg_reg = 0;
- host->caps.has_cstor_reg = 0;
- host->caps.has_highspeed = 0;
- host->caps.has_rwproof = 0;
- host->caps.has_odd_clk_div = 0;
- host->caps.has_bad_data_ordering = 1;
- host->caps.need_reset_after_xfer = 1;
- host->caps.need_blksz_mul_4 = 1;
- host->caps.need_notbusy_for_read_ops = 0;
-
- switch (version & 0xf00) {
- case 0x600:
- case 0x500:
- host->caps.has_odd_clk_div = 1;
- case 0x400:
- case 0x300:
- host->caps.has_dma_conf_reg = 1;
- host->caps.has_pdc = 0;
- host->caps.has_cfg_reg = 1;
- host->caps.has_cstor_reg = 1;
- host->caps.has_highspeed = 1;
- case 0x200:
- host->caps.has_rwproof = 1;
- host->caps.need_blksz_mul_4 = 0;
- host->caps.need_notbusy_for_read_ops = 1;
- case 0x100:
- host->caps.has_bad_data_ordering = 0;
- host->caps.need_reset_after_xfer = 0;
- case 0x0:
- break;
- default:
- host->caps.has_pdc = 0;
- dev_warn(&host->pdev->dev,
- "Unmanaged mci version, set minimum capabilities\n");
- break;
- }
- }
- static int atmci_probe(struct platform_device *pdev)
- {
- struct mci_platform_data *pdata;
- struct atmel_mci *host;
- struct resource *regs;
- unsigned int nr_slots;
- int irq;
- int ret, i;
- regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
- if (!regs)
- return -ENXIO;
- pdata = pdev->dev.platform_data;
- if (!pdata) {
- pdata = atmci_of_init(pdev);
- if (IS_ERR(pdata)) {
- dev_err(&pdev->dev, "platform data not available\n");
- return PTR_ERR(pdata);
- }
- }
- irq = platform_get_irq(pdev, 0);
- if (irq < 0)
- return irq;
- host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
- if (!host)
- return -ENOMEM;
- host->pdev = pdev;
- spin_lock_init(&host->lock);
- INIT_LIST_HEAD(&host->queue);
- host->mck = devm_clk_get(&pdev->dev, "mci_clk");
- if (IS_ERR(host->mck))
- return PTR_ERR(host->mck);
- host->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
- if (!host->regs)
- return -ENOMEM;
- ret = clk_prepare_enable(host->mck);
- if (ret)
- return ret;
- atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
- host->bus_hz = clk_get_rate(host->mck);
- host->mapbase = regs->start;
- tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)host);
- ret = request_irq(irq, atmci_interrupt, 0, dev_name(&pdev->dev), host);
- if (ret) {
- clk_disable_unprepare(host->mck);
- return ret;
- }
-
- atmci_get_cap(host);
- ret = atmci_configure_dma(host);
- if (ret == -EPROBE_DEFER)
- goto err_dma_probe_defer;
- if (ret == 0) {
- host->prepare_data = &atmci_prepare_data_dma;
- host->submit_data = &atmci_submit_data_dma;
- host->stop_transfer = &atmci_stop_transfer_dma;
- } else if (host->caps.has_pdc) {
- dev_info(&pdev->dev, "using PDC\n");
- host->prepare_data = &atmci_prepare_data_pdc;
- host->submit_data = &atmci_submit_data_pdc;
- host->stop_transfer = &atmci_stop_transfer_pdc;
- } else {
- dev_info(&pdev->dev, "using PIO\n");
- host->prepare_data = &atmci_prepare_data;
- host->submit_data = &atmci_submit_data;
- host->stop_transfer = &atmci_stop_transfer;
- }
- platform_set_drvdata(pdev, host);
- setup_timer(&host->timer, atmci_timeout_timer, (unsigned long)host);
- pm_runtime_get_noresume(&pdev->dev);
- pm_runtime_set_active(&pdev->dev);
- pm_runtime_set_autosuspend_delay(&pdev->dev, AUTOSUSPEND_DELAY);
- pm_runtime_use_autosuspend(&pdev->dev);
- pm_runtime_enable(&pdev->dev);
-
- nr_slots = 0;
- ret = -ENODEV;
- if (pdata->slot[0].bus_width) {
- ret = atmci_init_slot(host, &pdata->slot[0],
- 0, ATMCI_SDCSEL_SLOT_A, ATMCI_SDIOIRQA);
- if (!ret) {
- nr_slots++;
- host->buf_size = host->slot[0]->mmc->max_req_size;
- }
- }
- if (pdata->slot[1].bus_width) {
- ret = atmci_init_slot(host, &pdata->slot[1],
- 1, ATMCI_SDCSEL_SLOT_B, ATMCI_SDIOIRQB);
- if (!ret) {
- nr_slots++;
- if (host->slot[1]->mmc->max_req_size > host->buf_size)
- host->buf_size =
- host->slot[1]->mmc->max_req_size;
- }
- }
- if (!nr_slots) {
- dev_err(&pdev->dev, "init failed: no slot defined\n");
- goto err_init_slot;
- }
- if (!host->caps.has_rwproof) {
- host->buffer = dma_alloc_coherent(&pdev->dev, host->buf_size,
- &host->buf_phys_addr,
- GFP_KERNEL);
- if (!host->buffer) {
- ret = -ENOMEM;
- dev_err(&pdev->dev, "buffer allocation failed\n");
- goto err_dma_alloc;
- }
- }
- dev_info(&pdev->dev,
- "Atmel MCI controller at 0x%08lx irq %d, %u slots\n",
- host->mapbase, irq, nr_slots);
- pm_runtime_mark_last_busy(&host->pdev->dev);
- pm_runtime_put_autosuspend(&pdev->dev);
- return 0;
- err_dma_alloc:
- for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
- if (host->slot[i])
- atmci_cleanup_slot(host->slot[i], i);
- }
- err_init_slot:
- clk_disable_unprepare(host->mck);
- pm_runtime_disable(&pdev->dev);
- pm_runtime_put_noidle(&pdev->dev);
- del_timer_sync(&host->timer);
- if (!IS_ERR(host->dma.chan))
- dma_release_channel(host->dma.chan);
- err_dma_probe_defer:
- free_irq(irq, host);
- return ret;
- }
- static int atmci_remove(struct platform_device *pdev)
- {
- struct atmel_mci *host = platform_get_drvdata(pdev);
- unsigned int i;
- pm_runtime_get_sync(&pdev->dev);
- if (host->buffer)
- dma_free_coherent(&pdev->dev, host->buf_size,
- host->buffer, host->buf_phys_addr);
- for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
- if (host->slot[i])
- atmci_cleanup_slot(host->slot[i], i);
- }
- atmci_writel(host, ATMCI_IDR, ~0UL);
- atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
- atmci_readl(host, ATMCI_SR);
- del_timer_sync(&host->timer);
- if (!IS_ERR(host->dma.chan))
- dma_release_channel(host->dma.chan);
- free_irq(platform_get_irq(pdev, 0), host);
- clk_disable_unprepare(host->mck);
- pm_runtime_disable(&pdev->dev);
- pm_runtime_put_noidle(&pdev->dev);
- return 0;
- }
- #ifdef CONFIG_PM
- static int atmci_runtime_suspend(struct device *dev)
- {
- struct atmel_mci *host = dev_get_drvdata(dev);
- clk_disable_unprepare(host->mck);
- pinctrl_pm_select_sleep_state(dev);
- return 0;
- }
- static int atmci_runtime_resume(struct device *dev)
- {
- struct atmel_mci *host = dev_get_drvdata(dev);
- pinctrl_pm_select_default_state(dev);
- return clk_prepare_enable(host->mck);
- }
- #endif
- static const struct dev_pm_ops atmci_dev_pm_ops = {
- SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
- pm_runtime_force_resume)
- SET_RUNTIME_PM_OPS(atmci_runtime_suspend, atmci_runtime_resume, NULL)
- };
- static struct platform_driver atmci_driver = {
- .probe = atmci_probe,
- .remove = atmci_remove,
- .driver = {
- .name = "atmel_mci",
- .of_match_table = of_match_ptr(atmci_dt_ids),
- .pm = &atmci_dev_pm_ops,
- },
- };
- module_platform_driver(atmci_driver);
- MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
- MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
- MODULE_LICENSE("GPL v2");
|