sdio.c 119 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420
  1. /*
  2. * Copyright (c) 2010 Broadcom Corporation
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/types.h>
  17. #include <linux/atomic.h>
  18. #include <linux/kernel.h>
  19. #include <linux/kthread.h>
  20. #include <linux/printk.h>
  21. #include <linux/pci_ids.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/sched/signal.h>
  25. #include <linux/mmc/sdio.h>
  26. #include <linux/mmc/sdio_ids.h>
  27. #include <linux/mmc/sdio_func.h>
  28. #include <linux/mmc/card.h>
  29. #include <linux/semaphore.h>
  30. #include <linux/firmware.h>
  31. #include <linux/module.h>
  32. #include <linux/bcma/bcma.h>
  33. #include <linux/debugfs.h>
  34. #include <linux/vmalloc.h>
  35. #include <asm/unaligned.h>
  36. #include <defs.h>
  37. #include <brcmu_wifi.h>
  38. #include <brcmu_utils.h>
  39. #include <brcm_hw_ids.h>
  40. #include <soc.h>
  41. #include "sdio.h"
  42. #include "chip.h"
  43. #include "firmware.h"
  44. #include "core.h"
  45. #include "common.h"
  46. #include "bcdc.h"
  47. #define DCMD_RESP_TIMEOUT msecs_to_jiffies(2500)
  48. #define CTL_DONE_TIMEOUT msecs_to_jiffies(2500)
  49. /* watermark expressed in number of words */
  50. #define DEFAULT_F2_WATERMARK 0x8
  51. #define CY_4373_F2_WATERMARK 0x40
  52. #ifdef DEBUG
  53. #define BRCMF_TRAP_INFO_SIZE 80
  54. #define CBUF_LEN (128)
  55. /* Device console log buffer state */
  56. #define CONSOLE_BUFFER_MAX 2024
  57. struct rte_log_le {
  58. __le32 buf; /* Can't be pointer on (64-bit) hosts */
  59. __le32 buf_size;
  60. __le32 idx;
  61. char *_buf_compat; /* Redundant pointer for backward compat. */
  62. };
  63. struct rte_console {
  64. /* Virtual UART
  65. * When there is no UART (e.g. Quickturn),
  66. * the host should write a complete
  67. * input line directly into cbuf and then write
  68. * the length into vcons_in.
  69. * This may also be used when there is a real UART
  70. * (at risk of conflicting with
  71. * the real UART). vcons_out is currently unused.
  72. */
  73. uint vcons_in;
  74. uint vcons_out;
  75. /* Output (logging) buffer
  76. * Console output is written to a ring buffer log_buf at index log_idx.
  77. * The host may read the output when it sees log_idx advance.
  78. * Output will be lost if the output wraps around faster than the host
  79. * polls.
  80. */
  81. struct rte_log_le log_le;
  82. /* Console input line buffer
  83. * Characters are read one at a time into cbuf
  84. * until <CR> is received, then
  85. * the buffer is processed as a command line.
  86. * Also used for virtual UART.
  87. */
  88. uint cbuf_idx;
  89. char cbuf[CBUF_LEN];
  90. };
  91. #endif /* DEBUG */
  92. #include <chipcommon.h>
  93. #include "bus.h"
  94. #include "debug.h"
  95. #include "tracepoint.h"
  96. #define TXQLEN 2048 /* bulk tx queue length */
  97. #define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
  98. #define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
  99. #define PRIOMASK 7
  100. #define TXRETRIES 2 /* # of retries for tx frames */
  101. #define BRCMF_RXBOUND 50 /* Default for max rx frames in
  102. one scheduling */
  103. #define BRCMF_TXBOUND 20 /* Default for max tx frames in
  104. one scheduling */
  105. #define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */
  106. #define MEMBLOCK 2048 /* Block size used for downloading
  107. of dongle image */
  108. #define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
  109. biggest possible glom */
  110. #define BRCMF_FIRSTREAD (1 << 6)
  111. #define BRCMF_CONSOLE 10 /* watchdog interval to poll console */
  112. /* SBSDIO_DEVICE_CTL */
  113. /* 1: device will assert busy signal when receiving CMD53 */
  114. #define SBSDIO_DEVCTL_SETBUSY 0x01
  115. /* 1: assertion of sdio interrupt is synchronous to the sdio clock */
  116. #define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02
  117. /* 1: mask all interrupts to host except the chipActive (rev 8) */
  118. #define SBSDIO_DEVCTL_CA_INT_ONLY 0x04
  119. /* 1: isolate internal sdio signals, put external pads in tri-state; requires
  120. * sdio bus power cycle to clear (rev 9) */
  121. #define SBSDIO_DEVCTL_PADS_ISO 0x08
  122. /* 1: enable F2 Watermark */
  123. #define SBSDIO_DEVCTL_F2WM_ENAB 0x10
  124. /* Force SD->SB reset mapping (rev 11) */
  125. #define SBSDIO_DEVCTL_SB_RST_CTL 0x30
  126. /* Determined by CoreControl bit */
  127. #define SBSDIO_DEVCTL_RST_CORECTL 0x00
  128. /* Force backplane reset */
  129. #define SBSDIO_DEVCTL_RST_BPRESET 0x10
  130. /* Force no backplane reset */
  131. #define SBSDIO_DEVCTL_RST_NOBPRESET 0x20
  132. /* direct(mapped) cis space */
  133. /* MAPPED common CIS address */
  134. #define SBSDIO_CIS_BASE_COMMON 0x1000
  135. /* maximum bytes in one CIS */
  136. #define SBSDIO_CIS_SIZE_LIMIT 0x200
  137. /* cis offset addr is < 17 bits */
  138. #define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF
  139. /* manfid tuple length, include tuple, link bytes */
  140. #define SBSDIO_CIS_MANFID_TUPLE_LEN 6
  141. #define SD_REG(field) \
  142. (offsetof(struct sdpcmd_regs, field))
  143. /* SDIO function 1 register CHIPCLKCSR */
  144. /* Force ALP request to backplane */
  145. #define SBSDIO_FORCE_ALP 0x01
  146. /* Force HT request to backplane */
  147. #define SBSDIO_FORCE_HT 0x02
  148. /* Force ILP request to backplane */
  149. #define SBSDIO_FORCE_ILP 0x04
  150. /* Make ALP ready (power up xtal) */
  151. #define SBSDIO_ALP_AVAIL_REQ 0x08
  152. /* Make HT ready (power up PLL) */
  153. #define SBSDIO_HT_AVAIL_REQ 0x10
  154. /* Squelch clock requests from HW */
  155. #define SBSDIO_FORCE_HW_CLKREQ_OFF 0x20
  156. /* Status: ALP is ready */
  157. #define SBSDIO_ALP_AVAIL 0x40
  158. /* Status: HT is ready */
  159. #define SBSDIO_HT_AVAIL 0x80
  160. #define SBSDIO_CSR_MASK 0x1F
  161. #define SBSDIO_AVBITS (SBSDIO_HT_AVAIL | SBSDIO_ALP_AVAIL)
  162. #define SBSDIO_ALPAV(regval) ((regval) & SBSDIO_AVBITS)
  163. #define SBSDIO_HTAV(regval) (((regval) & SBSDIO_AVBITS) == SBSDIO_AVBITS)
  164. #define SBSDIO_ALPONLY(regval) (SBSDIO_ALPAV(regval) && !SBSDIO_HTAV(regval))
  165. #define SBSDIO_CLKAV(regval, alponly) \
  166. (SBSDIO_ALPAV(regval) && (alponly ? 1 : SBSDIO_HTAV(regval)))
  167. /* intstatus */
  168. #define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */
  169. #define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */
  170. #define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */
  171. #define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */
  172. #define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */
  173. #define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */
  174. #define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */
  175. #define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */
  176. #define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */
  177. #define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */
  178. #define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */
  179. #define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */
  180. #define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */
  181. #define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */
  182. #define I_PC (1 << 10) /* descriptor error */
  183. #define I_PD (1 << 11) /* data error */
  184. #define I_DE (1 << 12) /* Descriptor protocol Error */
  185. #define I_RU (1 << 13) /* Receive descriptor Underflow */
  186. #define I_RO (1 << 14) /* Receive fifo Overflow */
  187. #define I_XU (1 << 15) /* Transmit fifo Underflow */
  188. #define I_RI (1 << 16) /* Receive Interrupt */
  189. #define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */
  190. #define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */
  191. #define I_XI (1 << 24) /* Transmit Interrupt */
  192. #define I_RF_TERM (1 << 25) /* Read Frame Terminate */
  193. #define I_WF_TERM (1 << 26) /* Write Frame Terminate */
  194. #define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */
  195. #define I_SBINT (1 << 28) /* sbintstatus Interrupt */
  196. #define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */
  197. #define I_SRESET (1 << 30) /* CCCR RES interrupt */
  198. #define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */
  199. #define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
  200. #define I_DMA (I_RI | I_XI | I_ERRORS)
  201. /* corecontrol */
  202. #define CC_CISRDY (1 << 0) /* CIS Ready */
  203. #define CC_BPRESEN (1 << 1) /* CCCR RES signal */
  204. #define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */
  205. #define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */
  206. #define CC_XMTDATAAVAIL_MODE (1 << 4)
  207. #define CC_XMTDATAAVAIL_CTRL (1 << 5)
  208. /* SDA_FRAMECTRL */
  209. #define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */
  210. #define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */
  211. #define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */
  212. #define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */
  213. /*
  214. * Software allocation of To SB Mailbox resources
  215. */
  216. /* tosbmailbox bits corresponding to intstatus bits */
  217. #define SMB_NAK (1 << 0) /* Frame NAK */
  218. #define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
  219. #define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
  220. #define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
  221. /* tosbmailboxdata */
  222. #define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
  223. /*
  224. * Software allocation of To Host Mailbox resources
  225. */
  226. /* intstatus bits */
  227. #define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
  228. #define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
  229. #define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
  230. #define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
  231. /* tohostmailboxdata */
  232. #define HMB_DATA_NAKHANDLED 0x0001 /* retransmit NAK'd frame */
  233. #define HMB_DATA_DEVREADY 0x0002 /* talk to host after enable */
  234. #define HMB_DATA_FC 0x0004 /* per prio flowcontrol update flag */
  235. #define HMB_DATA_FWREADY 0x0008 /* fw ready for protocol activity */
  236. #define HMB_DATA_FWHALT 0x0010 /* firmware halted */
  237. #define HMB_DATA_FCDATA_MASK 0xff000000
  238. #define HMB_DATA_FCDATA_SHIFT 24
  239. #define HMB_DATA_VERSION_MASK 0x00ff0000
  240. #define HMB_DATA_VERSION_SHIFT 16
  241. /*
  242. * Software-defined protocol header
  243. */
  244. /* Current protocol version */
  245. #define SDPCM_PROT_VERSION 4
  246. /*
  247. * Shared structure between dongle and the host.
  248. * The structure contains pointers to trap or assert information.
  249. */
  250. #define SDPCM_SHARED_VERSION 0x0003
  251. #define SDPCM_SHARED_VERSION_MASK 0x00FF
  252. #define SDPCM_SHARED_ASSERT_BUILT 0x0100
  253. #define SDPCM_SHARED_ASSERT 0x0200
  254. #define SDPCM_SHARED_TRAP 0x0400
  255. /* Space for header read, limit for data packets */
  256. #define MAX_HDR_READ (1 << 6)
  257. #define MAX_RX_DATASZ 2048
  258. /* Bump up limit on waiting for HT to account for first startup;
  259. * if the image is doing a CRC calculation before programming the PMU
  260. * for HT availability, it could take a couple hundred ms more, so
  261. * max out at a 1 second (1000000us).
  262. */
  263. #undef PMU_MAX_TRANSITION_DLY
  264. #define PMU_MAX_TRANSITION_DLY 1000000
  265. /* Value for ChipClockCSR during initial setup */
  266. #define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
  267. SBSDIO_ALP_AVAIL_REQ)
  268. /* Flags for SDH calls */
  269. #define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
  270. #define BRCMF_IDLE_ACTIVE 0 /* Do not request any SD clock change
  271. * when idle
  272. */
  273. #define BRCMF_IDLE_INTERVAL 1
  274. #define KSO_WAIT_US 50
  275. #define MAX_KSO_ATTEMPTS (PMU_MAX_TRANSITION_DLY/KSO_WAIT_US)
  276. #define BRCMF_SDIO_MAX_ACCESS_ERRORS 5
  277. /*
  278. * Conversion of 802.1D priority to precedence level
  279. */
  280. static uint prio2prec(u32 prio)
  281. {
  282. return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ?
  283. (prio^2) : prio;
  284. }
  285. #ifdef DEBUG
  286. /* Device console log buffer state */
  287. struct brcmf_console {
  288. uint count; /* Poll interval msec counter */
  289. uint log_addr; /* Log struct address (fixed) */
  290. struct rte_log_le log_le; /* Log struct (host copy) */
  291. uint bufsize; /* Size of log buffer */
  292. u8 *buf; /* Log buffer (host copy) */
  293. uint last; /* Last buffer read index */
  294. };
  295. struct brcmf_trap_info {
  296. __le32 type;
  297. __le32 epc;
  298. __le32 cpsr;
  299. __le32 spsr;
  300. __le32 r0; /* a1 */
  301. __le32 r1; /* a2 */
  302. __le32 r2; /* a3 */
  303. __le32 r3; /* a4 */
  304. __le32 r4; /* v1 */
  305. __le32 r5; /* v2 */
  306. __le32 r6; /* v3 */
  307. __le32 r7; /* v4 */
  308. __le32 r8; /* v5 */
  309. __le32 r9; /* sb/v6 */
  310. __le32 r10; /* sl/v7 */
  311. __le32 r11; /* fp/v8 */
  312. __le32 r12; /* ip */
  313. __le32 r13; /* sp */
  314. __le32 r14; /* lr */
  315. __le32 pc; /* r15 */
  316. };
  317. #endif /* DEBUG */
  318. struct sdpcm_shared {
  319. u32 flags;
  320. u32 trap_addr;
  321. u32 assert_exp_addr;
  322. u32 assert_file_addr;
  323. u32 assert_line;
  324. u32 console_addr; /* Address of struct rte_console */
  325. u32 msgtrace_addr;
  326. u8 tag[32];
  327. u32 brpt_addr;
  328. };
  329. struct sdpcm_shared_le {
  330. __le32 flags;
  331. __le32 trap_addr;
  332. __le32 assert_exp_addr;
  333. __le32 assert_file_addr;
  334. __le32 assert_line;
  335. __le32 console_addr; /* Address of struct rte_console */
  336. __le32 msgtrace_addr;
  337. u8 tag[32];
  338. __le32 brpt_addr;
  339. };
  340. /* dongle SDIO bus specific header info */
  341. struct brcmf_sdio_hdrinfo {
  342. u8 seq_num;
  343. u8 channel;
  344. u16 len;
  345. u16 len_left;
  346. u16 len_nxtfrm;
  347. u8 dat_offset;
  348. bool lastfrm;
  349. u16 tail_pad;
  350. };
  351. /*
  352. * hold counter variables
  353. */
  354. struct brcmf_sdio_count {
  355. uint intrcount; /* Count of device interrupt callbacks */
  356. uint lastintrs; /* Count as of last watchdog timer */
  357. uint pollcnt; /* Count of active polls */
  358. uint regfails; /* Count of R_REG failures */
  359. uint tx_sderrs; /* Count of tx attempts with sd errors */
  360. uint fcqueued; /* Tx packets that got queued */
  361. uint rxrtx; /* Count of rtx requests (NAK to dongle) */
  362. uint rx_toolong; /* Receive frames too long to receive */
  363. uint rxc_errors; /* SDIO errors when reading control frames */
  364. uint rx_hdrfail; /* SDIO errors on header reads */
  365. uint rx_badhdr; /* Bad received headers (roosync?) */
  366. uint rx_badseq; /* Mismatched rx sequence number */
  367. uint fc_rcvd; /* Number of flow-control events received */
  368. uint fc_xoff; /* Number which turned on flow-control */
  369. uint fc_xon; /* Number which turned off flow-control */
  370. uint rxglomfail; /* Failed deglom attempts */
  371. uint rxglomframes; /* Number of glom frames (superframes) */
  372. uint rxglompkts; /* Number of packets from glom frames */
  373. uint f2rxhdrs; /* Number of header reads */
  374. uint f2rxdata; /* Number of frame data reads */
  375. uint f2txdata; /* Number of f2 frame writes */
  376. uint f1regdata; /* Number of f1 register accesses */
  377. uint tickcnt; /* Number of watchdog been schedule */
  378. ulong tx_ctlerrs; /* Err of sending ctrl frames */
  379. ulong tx_ctlpkts; /* Ctrl frames sent to dongle */
  380. ulong rx_ctlerrs; /* Err of processing rx ctrl frames */
  381. ulong rx_ctlpkts; /* Ctrl frames processed from dongle */
  382. ulong rx_readahead_cnt; /* packets where header read-ahead was used */
  383. };
  384. /* misc chip info needed by some of the routines */
  385. /* Private data for SDIO bus interaction */
  386. struct brcmf_sdio {
  387. struct brcmf_sdio_dev *sdiodev; /* sdio device handler */
  388. struct brcmf_chip *ci; /* Chip info struct */
  389. struct brcmf_core *sdio_core; /* sdio core info struct */
  390. u32 hostintmask; /* Copy of Host Interrupt Mask */
  391. atomic_t intstatus; /* Intstatus bits (events) pending */
  392. atomic_t fcstate; /* State of dongle flow-control */
  393. uint blocksize; /* Block size of SDIO transfers */
  394. uint roundup; /* Max roundup limit */
  395. struct pktq txq; /* Queue length used for flow-control */
  396. u8 flowcontrol; /* per prio flow control bitmask */
  397. u8 tx_seq; /* Transmit sequence number (next) */
  398. u8 tx_max; /* Maximum transmit sequence allowed */
  399. u8 *hdrbuf; /* buffer for handling rx frame */
  400. u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
  401. u8 rx_seq; /* Receive sequence number (expected) */
  402. struct brcmf_sdio_hdrinfo cur_read;
  403. /* info of current read frame */
  404. bool rxskip; /* Skip receive (awaiting NAK ACK) */
  405. bool rxpending; /* Data frame pending in dongle */
  406. uint rxbound; /* Rx frames to read before resched */
  407. uint txbound; /* Tx frames to send before resched */
  408. uint txminmax;
  409. struct sk_buff *glomd; /* Packet containing glomming descriptor */
  410. struct sk_buff_head glom; /* Packet list for glommed superframe */
  411. u8 *rxbuf; /* Buffer for receiving control packets */
  412. uint rxblen; /* Allocated length of rxbuf */
  413. u8 *rxctl; /* Aligned pointer into rxbuf */
  414. u8 *rxctl_orig; /* pointer for freeing rxctl */
  415. uint rxlen; /* Length of valid data in buffer */
  416. spinlock_t rxctl_lock; /* protection lock for ctrl frame resources */
  417. u8 sdpcm_ver; /* Bus protocol reported by dongle */
  418. bool intr; /* Use interrupts */
  419. bool poll; /* Use polling */
  420. atomic_t ipend; /* Device interrupt is pending */
  421. uint spurious; /* Count of spurious interrupts */
  422. uint pollrate; /* Ticks between device polls */
  423. uint polltick; /* Tick counter */
  424. #ifdef DEBUG
  425. uint console_interval;
  426. struct brcmf_console console; /* Console output polling support */
  427. uint console_addr; /* Console address from shared struct */
  428. #endif /* DEBUG */
  429. uint clkstate; /* State of sd and backplane clock(s) */
  430. s32 idletime; /* Control for activity timeout */
  431. s32 idlecount; /* Activity timeout counter */
  432. s32 idleclock; /* How to set bus driver when idle */
  433. bool rxflow_mode; /* Rx flow control mode */
  434. bool rxflow; /* Is rx flow control on */
  435. bool alp_only; /* Don't use HT clock (ALP only) */
  436. u8 *ctrl_frame_buf;
  437. u16 ctrl_frame_len;
  438. bool ctrl_frame_stat;
  439. int ctrl_frame_err;
  440. spinlock_t txq_lock; /* protect bus->txq */
  441. wait_queue_head_t ctrl_wait;
  442. wait_queue_head_t dcmd_resp_wait;
  443. struct timer_list timer;
  444. struct completion watchdog_wait;
  445. struct task_struct *watchdog_tsk;
  446. bool wd_active;
  447. struct workqueue_struct *brcmf_wq;
  448. struct work_struct datawork;
  449. bool dpc_triggered;
  450. bool dpc_running;
  451. bool txoff; /* Transmit flow-controlled */
  452. struct brcmf_sdio_count sdcnt;
  453. bool sr_enabled; /* SaveRestore enabled */
  454. bool sleeping;
  455. u8 tx_hdrlen; /* sdio bus header length for tx packet */
  456. bool txglom; /* host tx glomming enable flag */
  457. u16 head_align; /* buffer pointer alignment */
  458. u16 sgentry_align; /* scatter-gather buffer alignment */
  459. };
  460. /* clkstate */
  461. #define CLK_NONE 0
  462. #define CLK_SDONLY 1
  463. #define CLK_PENDING 2
  464. #define CLK_AVAIL 3
  465. #ifdef DEBUG
  466. static int qcount[NUMPRIO];
  467. #endif /* DEBUG */
  468. #define DEFAULT_SDIO_DRIVE_STRENGTH 6 /* in milliamps */
  469. #define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
  470. /* Limit on rounding up frames */
  471. static const uint max_roundup = 512;
  472. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  473. #define ALIGNMENT 8
  474. #else
  475. #define ALIGNMENT 4
  476. #endif
  477. enum brcmf_sdio_frmtype {
  478. BRCMF_SDIO_FT_NORMAL,
  479. BRCMF_SDIO_FT_SUPER,
  480. BRCMF_SDIO_FT_SUB,
  481. };
  482. #define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
  483. /* SDIO Pad drive strength to select value mappings */
  484. struct sdiod_drive_str {
  485. u8 strength; /* Pad Drive Strength in mA */
  486. u8 sel; /* Chip-specific select value */
  487. };
  488. /* SDIO Drive Strength to sel value table for PMU Rev 11 (1.8V) */
  489. static const struct sdiod_drive_str sdiod_drvstr_tab1_1v8[] = {
  490. {32, 0x6},
  491. {26, 0x7},
  492. {22, 0x4},
  493. {16, 0x5},
  494. {12, 0x2},
  495. {8, 0x3},
  496. {4, 0x0},
  497. {0, 0x1}
  498. };
  499. /* SDIO Drive Strength to sel value table for PMU Rev 13 (1.8v) */
  500. static const struct sdiod_drive_str sdiod_drive_strength_tab5_1v8[] = {
  501. {6, 0x7},
  502. {5, 0x6},
  503. {4, 0x5},
  504. {3, 0x4},
  505. {2, 0x2},
  506. {1, 0x1},
  507. {0, 0x0}
  508. };
  509. /* SDIO Drive Strength to sel value table for PMU Rev 17 (1.8v) */
  510. static const struct sdiod_drive_str sdiod_drvstr_tab6_1v8[] = {
  511. {3, 0x3},
  512. {2, 0x2},
  513. {1, 0x1},
  514. {0, 0x0} };
  515. /* SDIO Drive Strength to sel value table for 43143 PMU Rev 17 (3.3V) */
  516. static const struct sdiod_drive_str sdiod_drvstr_tab2_3v3[] = {
  517. {16, 0x7},
  518. {12, 0x5},
  519. {8, 0x3},
  520. {4, 0x1}
  521. };
  522. BRCMF_FW_DEF(43143, "brcmfmac43143-sdio");
  523. BRCMF_FW_DEF(43241B0, "brcmfmac43241b0-sdio");
  524. BRCMF_FW_DEF(43241B4, "brcmfmac43241b4-sdio");
  525. BRCMF_FW_DEF(43241B5, "brcmfmac43241b5-sdio");
  526. BRCMF_FW_DEF(4329, "brcmfmac4329-sdio");
  527. BRCMF_FW_DEF(4330, "brcmfmac4330-sdio");
  528. BRCMF_FW_DEF(4334, "brcmfmac4334-sdio");
  529. BRCMF_FW_DEF(43340, "brcmfmac43340-sdio");
  530. BRCMF_FW_DEF(4335, "brcmfmac4335-sdio");
  531. BRCMF_FW_DEF(43362, "brcmfmac43362-sdio");
  532. BRCMF_FW_DEF(4339, "brcmfmac4339-sdio");
  533. BRCMF_FW_DEF(43430A0, "brcmfmac43430a0-sdio");
  534. /* Note the names are not postfixed with a1 for backward compatibility */
  535. BRCMF_FW_DEF(43430A1, "brcmfmac43430-sdio");
  536. BRCMF_FW_DEF(43455, "brcmfmac43455-sdio");
  537. BRCMF_FW_DEF(4354, "brcmfmac4354-sdio");
  538. BRCMF_FW_DEF(4356, "brcmfmac4356-sdio");
  539. BRCMF_FW_DEF(4373, "brcmfmac4373-sdio");
  540. static const struct brcmf_firmware_mapping brcmf_sdio_fwnames[] = {
  541. BRCMF_FW_ENTRY(BRCM_CC_43143_CHIP_ID, 0xFFFFFFFF, 43143),
  542. BRCMF_FW_ENTRY(BRCM_CC_43241_CHIP_ID, 0x0000001F, 43241B0),
  543. BRCMF_FW_ENTRY(BRCM_CC_43241_CHIP_ID, 0x00000020, 43241B4),
  544. BRCMF_FW_ENTRY(BRCM_CC_43241_CHIP_ID, 0xFFFFFFC0, 43241B5),
  545. BRCMF_FW_ENTRY(BRCM_CC_4329_CHIP_ID, 0xFFFFFFFF, 4329),
  546. BRCMF_FW_ENTRY(BRCM_CC_4330_CHIP_ID, 0xFFFFFFFF, 4330),
  547. BRCMF_FW_ENTRY(BRCM_CC_4334_CHIP_ID, 0xFFFFFFFF, 4334),
  548. BRCMF_FW_ENTRY(BRCM_CC_43340_CHIP_ID, 0xFFFFFFFF, 43340),
  549. BRCMF_FW_ENTRY(BRCM_CC_43341_CHIP_ID, 0xFFFFFFFF, 43340),
  550. BRCMF_FW_ENTRY(BRCM_CC_4335_CHIP_ID, 0xFFFFFFFF, 4335),
  551. BRCMF_FW_ENTRY(BRCM_CC_43362_CHIP_ID, 0xFFFFFFFE, 43362),
  552. BRCMF_FW_ENTRY(BRCM_CC_4339_CHIP_ID, 0xFFFFFFFF, 4339),
  553. BRCMF_FW_ENTRY(BRCM_CC_43430_CHIP_ID, 0x00000001, 43430A0),
  554. BRCMF_FW_ENTRY(BRCM_CC_43430_CHIP_ID, 0xFFFFFFFE, 43430A1),
  555. BRCMF_FW_ENTRY(BRCM_CC_4345_CHIP_ID, 0xFFFFFFC0, 43455),
  556. BRCMF_FW_ENTRY(BRCM_CC_4354_CHIP_ID, 0xFFFFFFFF, 4354),
  557. BRCMF_FW_ENTRY(BRCM_CC_4356_CHIP_ID, 0xFFFFFFFF, 4356),
  558. BRCMF_FW_ENTRY(CY_CC_4373_CHIP_ID, 0xFFFFFFFF, 4373)
  559. };
  560. static void pkt_align(struct sk_buff *p, int len, int align)
  561. {
  562. uint datalign;
  563. datalign = (unsigned long)(p->data);
  564. datalign = roundup(datalign, (align)) - datalign;
  565. if (datalign)
  566. skb_pull(p, datalign);
  567. __skb_trim(p, len);
  568. }
  569. /* To check if there's window offered */
  570. static bool data_ok(struct brcmf_sdio *bus)
  571. {
  572. return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
  573. ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
  574. }
  575. static int
  576. brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
  577. {
  578. u8 wr_val = 0, rd_val, cmp_val, bmask;
  579. int err = 0;
  580. int err_cnt = 0;
  581. int try_cnt = 0;
  582. brcmf_dbg(TRACE, "Enter: on=%d\n", on);
  583. sdio_retune_crc_disable(bus->sdiodev->func1);
  584. /* Cannot re-tune if device is asleep; defer till we're awake */
  585. if (on)
  586. sdio_retune_hold_now(bus->sdiodev->func1);
  587. wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
  588. /* 1st KSO write goes to AOS wake up core if device is asleep */
  589. brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, wr_val, &err);
  590. if (on) {
  591. /* device WAKEUP through KSO:
  592. * write bit 0 & read back until
  593. * both bits 0 (kso bit) & 1 (dev on status) are set
  594. */
  595. cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
  596. SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
  597. bmask = cmp_val;
  598. usleep_range(2000, 3000);
  599. } else {
  600. /* Put device to sleep, turn off KSO */
  601. cmp_val = 0;
  602. /* only check for bit0, bit1(dev on status) may not
  603. * get cleared right away
  604. */
  605. bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
  606. }
  607. do {
  608. /* reliable KSO bit set/clr:
  609. * the sdiod sleep write access is synced to PMU 32khz clk
  610. * just one write attempt may fail,
  611. * read it back until it matches written value
  612. */
  613. rd_val = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
  614. &err);
  615. if (!err) {
  616. if ((rd_val & bmask) == cmp_val)
  617. break;
  618. err_cnt = 0;
  619. }
  620. /* bail out upon subsequent access errors */
  621. if (err && (err_cnt++ > BRCMF_SDIO_MAX_ACCESS_ERRORS))
  622. break;
  623. udelay(KSO_WAIT_US);
  624. brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, wr_val,
  625. &err);
  626. } while (try_cnt++ < MAX_KSO_ATTEMPTS);
  627. if (try_cnt > 2)
  628. brcmf_dbg(SDIO, "try_cnt=%d rd_val=0x%x err=%d\n", try_cnt,
  629. rd_val, err);
  630. if (try_cnt > MAX_KSO_ATTEMPTS)
  631. brcmf_err("max tries: rd_val=0x%x err=%d\n", rd_val, err);
  632. if (on)
  633. sdio_retune_release(bus->sdiodev->func1);
  634. sdio_retune_crc_enable(bus->sdiodev->func1);
  635. return err;
  636. }
  637. #define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
  638. /* Turn backplane clock on or off */
  639. static int brcmf_sdio_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
  640. {
  641. int err;
  642. u8 clkctl, clkreq, devctl;
  643. unsigned long timeout;
  644. brcmf_dbg(SDIO, "Enter\n");
  645. clkctl = 0;
  646. if (bus->sr_enabled) {
  647. bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
  648. return 0;
  649. }
  650. if (on) {
  651. /* Request HT Avail */
  652. clkreq =
  653. bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
  654. brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
  655. clkreq, &err);
  656. if (err) {
  657. brcmf_err("HT Avail request error: %d\n", err);
  658. return -EBADE;
  659. }
  660. /* Check current status */
  661. clkctl = brcmf_sdiod_readb(bus->sdiodev,
  662. SBSDIO_FUNC1_CHIPCLKCSR, &err);
  663. if (err) {
  664. brcmf_err("HT Avail read error: %d\n", err);
  665. return -EBADE;
  666. }
  667. /* Go to pending and await interrupt if appropriate */
  668. if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
  669. /* Allow only clock-available interrupt */
  670. devctl = brcmf_sdiod_readb(bus->sdiodev,
  671. SBSDIO_DEVICE_CTL, &err);
  672. if (err) {
  673. brcmf_err("Devctl error setting CA: %d\n", err);
  674. return -EBADE;
  675. }
  676. devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
  677. brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_DEVICE_CTL,
  678. devctl, &err);
  679. brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
  680. bus->clkstate = CLK_PENDING;
  681. return 0;
  682. } else if (bus->clkstate == CLK_PENDING) {
  683. /* Cancel CA-only interrupt filter */
  684. devctl = brcmf_sdiod_readb(bus->sdiodev,
  685. SBSDIO_DEVICE_CTL, &err);
  686. devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
  687. brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_DEVICE_CTL,
  688. devctl, &err);
  689. }
  690. /* Otherwise, wait here (polling) for HT Avail */
  691. timeout = jiffies +
  692. msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
  693. while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
  694. clkctl = brcmf_sdiod_readb(bus->sdiodev,
  695. SBSDIO_FUNC1_CHIPCLKCSR,
  696. &err);
  697. if (time_after(jiffies, timeout))
  698. break;
  699. else
  700. usleep_range(5000, 10000);
  701. }
  702. if (err) {
  703. brcmf_err("HT Avail request error: %d\n", err);
  704. return -EBADE;
  705. }
  706. if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
  707. brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
  708. PMU_MAX_TRANSITION_DLY, clkctl);
  709. return -EBADE;
  710. }
  711. /* Mark clock available */
  712. bus->clkstate = CLK_AVAIL;
  713. brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
  714. #if defined(DEBUG)
  715. if (!bus->alp_only) {
  716. if (SBSDIO_ALPONLY(clkctl))
  717. brcmf_err("HT Clock should be on\n");
  718. }
  719. #endif /* defined (DEBUG) */
  720. } else {
  721. clkreq = 0;
  722. if (bus->clkstate == CLK_PENDING) {
  723. /* Cancel CA-only interrupt filter */
  724. devctl = brcmf_sdiod_readb(bus->sdiodev,
  725. SBSDIO_DEVICE_CTL, &err);
  726. devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
  727. brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_DEVICE_CTL,
  728. devctl, &err);
  729. }
  730. bus->clkstate = CLK_SDONLY;
  731. brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
  732. clkreq, &err);
  733. brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
  734. if (err) {
  735. brcmf_err("Failed access turning clock off: %d\n",
  736. err);
  737. return -EBADE;
  738. }
  739. }
  740. return 0;
  741. }
  742. /* Change idle/active SD state */
  743. static int brcmf_sdio_sdclk(struct brcmf_sdio *bus, bool on)
  744. {
  745. brcmf_dbg(SDIO, "Enter\n");
  746. if (on)
  747. bus->clkstate = CLK_SDONLY;
  748. else
  749. bus->clkstate = CLK_NONE;
  750. return 0;
  751. }
  752. /* Transition SD and backplane clock readiness */
  753. static int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
  754. {
  755. #ifdef DEBUG
  756. uint oldstate = bus->clkstate;
  757. #endif /* DEBUG */
  758. brcmf_dbg(SDIO, "Enter\n");
  759. /* Early exit if we're already there */
  760. if (bus->clkstate == target)
  761. return 0;
  762. switch (target) {
  763. case CLK_AVAIL:
  764. /* Make sure SD clock is available */
  765. if (bus->clkstate == CLK_NONE)
  766. brcmf_sdio_sdclk(bus, true);
  767. /* Now request HT Avail on the backplane */
  768. brcmf_sdio_htclk(bus, true, pendok);
  769. break;
  770. case CLK_SDONLY:
  771. /* Remove HT request, or bring up SD clock */
  772. if (bus->clkstate == CLK_NONE)
  773. brcmf_sdio_sdclk(bus, true);
  774. else if (bus->clkstate == CLK_AVAIL)
  775. brcmf_sdio_htclk(bus, false, false);
  776. else
  777. brcmf_err("request for %d -> %d\n",
  778. bus->clkstate, target);
  779. break;
  780. case CLK_NONE:
  781. /* Make sure to remove HT request */
  782. if (bus->clkstate == CLK_AVAIL)
  783. brcmf_sdio_htclk(bus, false, false);
  784. /* Now remove the SD clock */
  785. brcmf_sdio_sdclk(bus, false);
  786. break;
  787. }
  788. #ifdef DEBUG
  789. brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
  790. #endif /* DEBUG */
  791. return 0;
  792. }
  793. static int
  794. brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
  795. {
  796. int err = 0;
  797. u8 clkcsr;
  798. brcmf_dbg(SDIO, "Enter: request %s currently %s\n",
  799. (sleep ? "SLEEP" : "WAKE"),
  800. (bus->sleeping ? "SLEEP" : "WAKE"));
  801. /* If SR is enabled control bus state with KSO */
  802. if (bus->sr_enabled) {
  803. /* Done if we're already in the requested state */
  804. if (sleep == bus->sleeping)
  805. goto end;
  806. /* Going to sleep */
  807. if (sleep) {
  808. clkcsr = brcmf_sdiod_readb(bus->sdiodev,
  809. SBSDIO_FUNC1_CHIPCLKCSR,
  810. &err);
  811. if ((clkcsr & SBSDIO_CSR_MASK) == 0) {
  812. brcmf_dbg(SDIO, "no clock, set ALP\n");
  813. brcmf_sdiod_writeb(bus->sdiodev,
  814. SBSDIO_FUNC1_CHIPCLKCSR,
  815. SBSDIO_ALP_AVAIL_REQ, &err);
  816. }
  817. err = brcmf_sdio_kso_control(bus, false);
  818. } else {
  819. err = brcmf_sdio_kso_control(bus, true);
  820. }
  821. if (err) {
  822. brcmf_err("error while changing bus sleep state %d\n",
  823. err);
  824. goto done;
  825. }
  826. }
  827. end:
  828. /* control clocks */
  829. if (sleep) {
  830. if (!bus->sr_enabled)
  831. brcmf_sdio_clkctl(bus, CLK_NONE, pendok);
  832. } else {
  833. brcmf_sdio_clkctl(bus, CLK_AVAIL, pendok);
  834. brcmf_sdio_wd_timer(bus, true);
  835. }
  836. bus->sleeping = sleep;
  837. brcmf_dbg(SDIO, "new state %s\n",
  838. (sleep ? "SLEEP" : "WAKE"));
  839. done:
  840. brcmf_dbg(SDIO, "Exit: err=%d\n", err);
  841. return err;
  842. }
  843. #ifdef DEBUG
  844. static inline bool brcmf_sdio_valid_shared_address(u32 addr)
  845. {
  846. return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
  847. }
  848. static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
  849. struct sdpcm_shared *sh)
  850. {
  851. u32 addr = 0;
  852. int rv;
  853. u32 shaddr = 0;
  854. struct sdpcm_shared_le sh_le;
  855. __le32 addr_le;
  856. sdio_claim_host(bus->sdiodev->func1);
  857. brcmf_sdio_bus_sleep(bus, false, false);
  858. /*
  859. * Read last word in socram to determine
  860. * address of sdpcm_shared structure
  861. */
  862. shaddr = bus->ci->rambase + bus->ci->ramsize - 4;
  863. if (!bus->ci->rambase && brcmf_chip_sr_capable(bus->ci))
  864. shaddr -= bus->ci->srsize;
  865. rv = brcmf_sdiod_ramrw(bus->sdiodev, false, shaddr,
  866. (u8 *)&addr_le, 4);
  867. if (rv < 0)
  868. goto fail;
  869. /*
  870. * Check if addr is valid.
  871. * NVRAM length at the end of memory should have been overwritten.
  872. */
  873. addr = le32_to_cpu(addr_le);
  874. if (!brcmf_sdio_valid_shared_address(addr)) {
  875. brcmf_err("invalid sdpcm_shared address 0x%08X\n", addr);
  876. rv = -EINVAL;
  877. goto fail;
  878. }
  879. brcmf_dbg(INFO, "sdpcm_shared address 0x%08X\n", addr);
  880. /* Read hndrte_shared structure */
  881. rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
  882. sizeof(struct sdpcm_shared_le));
  883. if (rv < 0)
  884. goto fail;
  885. sdio_release_host(bus->sdiodev->func1);
  886. /* Endianness */
  887. sh->flags = le32_to_cpu(sh_le.flags);
  888. sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
  889. sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
  890. sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
  891. sh->assert_line = le32_to_cpu(sh_le.assert_line);
  892. sh->console_addr = le32_to_cpu(sh_le.console_addr);
  893. sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
  894. if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
  895. brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
  896. SDPCM_SHARED_VERSION,
  897. sh->flags & SDPCM_SHARED_VERSION_MASK);
  898. return -EPROTO;
  899. }
  900. return 0;
  901. fail:
  902. brcmf_err("unable to obtain sdpcm_shared info: rv=%d (addr=0x%x)\n",
  903. rv, addr);
  904. sdio_release_host(bus->sdiodev->func1);
  905. return rv;
  906. }
  907. static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
  908. {
  909. struct sdpcm_shared sh;
  910. if (brcmf_sdio_readshared(bus, &sh) == 0)
  911. bus->console_addr = sh.console_addr;
  912. }
  913. #else
  914. static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
  915. {
  916. }
  917. #endif /* DEBUG */
  918. static u32 brcmf_sdio_hostmail(struct brcmf_sdio *bus)
  919. {
  920. struct brcmf_sdio_dev *sdiod = bus->sdiodev;
  921. struct brcmf_core *core = bus->sdio_core;
  922. u32 intstatus = 0;
  923. u32 hmb_data;
  924. u8 fcbits;
  925. int ret;
  926. brcmf_dbg(SDIO, "Enter\n");
  927. /* Read mailbox data and ack that we did so */
  928. hmb_data = brcmf_sdiod_readl(sdiod,
  929. core->base + SD_REG(tohostmailboxdata),
  930. &ret);
  931. if (!ret)
  932. brcmf_sdiod_writel(sdiod, core->base + SD_REG(tosbmailbox),
  933. SMB_INT_ACK, &ret);
  934. bus->sdcnt.f1regdata += 2;
  935. /* dongle indicates the firmware has halted/crashed */
  936. if (hmb_data & HMB_DATA_FWHALT) {
  937. brcmf_err("mailbox indicates firmware halted\n");
  938. brcmf_dev_coredump(&sdiod->func1->dev);
  939. }
  940. /* Dongle recomposed rx frames, accept them again */
  941. if (hmb_data & HMB_DATA_NAKHANDLED) {
  942. brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
  943. bus->rx_seq);
  944. if (!bus->rxskip)
  945. brcmf_err("unexpected NAKHANDLED!\n");
  946. bus->rxskip = false;
  947. intstatus |= I_HMB_FRAME_IND;
  948. }
  949. /*
  950. * DEVREADY does not occur with gSPI.
  951. */
  952. if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
  953. bus->sdpcm_ver =
  954. (hmb_data & HMB_DATA_VERSION_MASK) >>
  955. HMB_DATA_VERSION_SHIFT;
  956. if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
  957. brcmf_err("Version mismatch, dongle reports %d, "
  958. "expecting %d\n",
  959. bus->sdpcm_ver, SDPCM_PROT_VERSION);
  960. else
  961. brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
  962. bus->sdpcm_ver);
  963. /*
  964. * Retrieve console state address now that firmware should have
  965. * updated it.
  966. */
  967. brcmf_sdio_get_console_addr(bus);
  968. }
  969. /*
  970. * Flow Control has been moved into the RX headers and this out of band
  971. * method isn't used any more.
  972. * remaining backward compatible with older dongles.
  973. */
  974. if (hmb_data & HMB_DATA_FC) {
  975. fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
  976. HMB_DATA_FCDATA_SHIFT;
  977. if (fcbits & ~bus->flowcontrol)
  978. bus->sdcnt.fc_xoff++;
  979. if (bus->flowcontrol & ~fcbits)
  980. bus->sdcnt.fc_xon++;
  981. bus->sdcnt.fc_rcvd++;
  982. bus->flowcontrol = fcbits;
  983. }
  984. /* Shouldn't be any others */
  985. if (hmb_data & ~(HMB_DATA_DEVREADY |
  986. HMB_DATA_NAKHANDLED |
  987. HMB_DATA_FC |
  988. HMB_DATA_FWREADY |
  989. HMB_DATA_FWHALT |
  990. HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
  991. brcmf_err("Unknown mailbox data content: 0x%02x\n",
  992. hmb_data);
  993. return intstatus;
  994. }
  995. static void brcmf_sdio_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
  996. {
  997. struct brcmf_sdio_dev *sdiod = bus->sdiodev;
  998. struct brcmf_core *core = bus->sdio_core;
  999. uint retries = 0;
  1000. u16 lastrbc;
  1001. u8 hi, lo;
  1002. int err;
  1003. brcmf_err("%sterminate frame%s\n",
  1004. abort ? "abort command, " : "",
  1005. rtx ? ", send NAK" : "");
  1006. if (abort)
  1007. brcmf_sdiod_abort(bus->sdiodev, bus->sdiodev->func2);
  1008. brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL, SFC_RF_TERM,
  1009. &err);
  1010. bus->sdcnt.f1regdata++;
  1011. /* Wait until the packet has been flushed (device/FIFO stable) */
  1012. for (lastrbc = retries = 0xffff; retries > 0; retries--) {
  1013. hi = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_FUNC1_RFRAMEBCHI,
  1014. &err);
  1015. lo = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_FUNC1_RFRAMEBCLO,
  1016. &err);
  1017. bus->sdcnt.f1regdata += 2;
  1018. if ((hi == 0) && (lo == 0))
  1019. break;
  1020. if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
  1021. brcmf_err("count growing: last 0x%04x now 0x%04x\n",
  1022. lastrbc, (hi << 8) + lo);
  1023. }
  1024. lastrbc = (hi << 8) + lo;
  1025. }
  1026. if (!retries)
  1027. brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
  1028. else
  1029. brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
  1030. if (rtx) {
  1031. bus->sdcnt.rxrtx++;
  1032. brcmf_sdiod_writel(sdiod, core->base + SD_REG(tosbmailbox),
  1033. SMB_NAK, &err);
  1034. bus->sdcnt.f1regdata++;
  1035. if (err == 0)
  1036. bus->rxskip = true;
  1037. }
  1038. /* Clear partial in any case */
  1039. bus->cur_read.len = 0;
  1040. }
  1041. static void brcmf_sdio_txfail(struct brcmf_sdio *bus)
  1042. {
  1043. struct brcmf_sdio_dev *sdiodev = bus->sdiodev;
  1044. u8 i, hi, lo;
  1045. /* On failure, abort the command and terminate the frame */
  1046. brcmf_err("sdio error, abort command and terminate frame\n");
  1047. bus->sdcnt.tx_sderrs++;
  1048. brcmf_sdiod_abort(sdiodev, sdiodev->func2);
  1049. brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM, NULL);
  1050. bus->sdcnt.f1regdata++;
  1051. for (i = 0; i < 3; i++) {
  1052. hi = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_WFRAMEBCHI, NULL);
  1053. lo = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_WFRAMEBCLO, NULL);
  1054. bus->sdcnt.f1regdata += 2;
  1055. if ((hi == 0) && (lo == 0))
  1056. break;
  1057. }
  1058. }
  1059. /* return total length of buffer chain */
  1060. static uint brcmf_sdio_glom_len(struct brcmf_sdio *bus)
  1061. {
  1062. struct sk_buff *p;
  1063. uint total;
  1064. total = 0;
  1065. skb_queue_walk(&bus->glom, p)
  1066. total += p->len;
  1067. return total;
  1068. }
  1069. static void brcmf_sdio_free_glom(struct brcmf_sdio *bus)
  1070. {
  1071. struct sk_buff *cur, *next;
  1072. skb_queue_walk_safe(&bus->glom, cur, next) {
  1073. skb_unlink(cur, &bus->glom);
  1074. brcmu_pkt_buf_free_skb(cur);
  1075. }
  1076. }
  1077. /**
  1078. * brcmfmac sdio bus specific header
  1079. * This is the lowest layer header wrapped on the packets transmitted between
  1080. * host and WiFi dongle which contains information needed for SDIO core and
  1081. * firmware
  1082. *
  1083. * It consists of 3 parts: hardware header, hardware extension header and
  1084. * software header
  1085. * hardware header (frame tag) - 4 bytes
  1086. * Byte 0~1: Frame length
  1087. * Byte 2~3: Checksum, bit-wise inverse of frame length
  1088. * hardware extension header - 8 bytes
  1089. * Tx glom mode only, N/A for Rx or normal Tx
  1090. * Byte 0~1: Packet length excluding hw frame tag
  1091. * Byte 2: Reserved
  1092. * Byte 3: Frame flags, bit 0: last frame indication
  1093. * Byte 4~5: Reserved
  1094. * Byte 6~7: Tail padding length
  1095. * software header - 8 bytes
  1096. * Byte 0: Rx/Tx sequence number
  1097. * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
  1098. * Byte 2: Length of next data frame, reserved for Tx
  1099. * Byte 3: Data offset
  1100. * Byte 4: Flow control bits, reserved for Tx
  1101. * Byte 5: Maximum Sequence number allowed by firmware for Tx, N/A for Tx packet
  1102. * Byte 6~7: Reserved
  1103. */
  1104. #define SDPCM_HWHDR_LEN 4
  1105. #define SDPCM_HWEXT_LEN 8
  1106. #define SDPCM_SWHDR_LEN 8
  1107. #define SDPCM_HDRLEN (SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN)
  1108. /* software header */
  1109. #define SDPCM_SEQ_MASK 0x000000ff
  1110. #define SDPCM_SEQ_WRAP 256
  1111. #define SDPCM_CHANNEL_MASK 0x00000f00
  1112. #define SDPCM_CHANNEL_SHIFT 8
  1113. #define SDPCM_CONTROL_CHANNEL 0 /* Control */
  1114. #define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication */
  1115. #define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv */
  1116. #define SDPCM_GLOM_CHANNEL 3 /* Coalesced packets */
  1117. #define SDPCM_TEST_CHANNEL 15 /* Test/debug packets */
  1118. #define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
  1119. #define SDPCM_NEXTLEN_MASK 0x00ff0000
  1120. #define SDPCM_NEXTLEN_SHIFT 16
  1121. #define SDPCM_DOFFSET_MASK 0xff000000
  1122. #define SDPCM_DOFFSET_SHIFT 24
  1123. #define SDPCM_FCMASK_MASK 0x000000ff
  1124. #define SDPCM_WINDOW_MASK 0x0000ff00
  1125. #define SDPCM_WINDOW_SHIFT 8
  1126. static inline u8 brcmf_sdio_getdatoffset(u8 *swheader)
  1127. {
  1128. u32 hdrvalue;
  1129. hdrvalue = *(u32 *)swheader;
  1130. return (u8)((hdrvalue & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT);
  1131. }
  1132. static inline bool brcmf_sdio_fromevntchan(u8 *swheader)
  1133. {
  1134. u32 hdrvalue;
  1135. u8 ret;
  1136. hdrvalue = *(u32 *)swheader;
  1137. ret = (u8)((hdrvalue & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT);
  1138. return (ret == SDPCM_EVENT_CHANNEL);
  1139. }
  1140. static int brcmf_sdio_hdparse(struct brcmf_sdio *bus, u8 *header,
  1141. struct brcmf_sdio_hdrinfo *rd,
  1142. enum brcmf_sdio_frmtype type)
  1143. {
  1144. u16 len, checksum;
  1145. u8 rx_seq, fc, tx_seq_max;
  1146. u32 swheader;
  1147. trace_brcmf_sdpcm_hdr(SDPCM_RX, header);
  1148. /* hw header */
  1149. len = get_unaligned_le16(header);
  1150. checksum = get_unaligned_le16(header + sizeof(u16));
  1151. /* All zero means no more to read */
  1152. if (!(len | checksum)) {
  1153. bus->rxpending = false;
  1154. return -ENODATA;
  1155. }
  1156. if ((u16)(~(len ^ checksum))) {
  1157. brcmf_err("HW header checksum error\n");
  1158. bus->sdcnt.rx_badhdr++;
  1159. brcmf_sdio_rxfail(bus, false, false);
  1160. return -EIO;
  1161. }
  1162. if (len < SDPCM_HDRLEN) {
  1163. brcmf_err("HW header length error\n");
  1164. return -EPROTO;
  1165. }
  1166. if (type == BRCMF_SDIO_FT_SUPER &&
  1167. (roundup(len, bus->blocksize) != rd->len)) {
  1168. brcmf_err("HW superframe header length error\n");
  1169. return -EPROTO;
  1170. }
  1171. if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
  1172. brcmf_err("HW subframe header length error\n");
  1173. return -EPROTO;
  1174. }
  1175. rd->len = len;
  1176. /* software header */
  1177. header += SDPCM_HWHDR_LEN;
  1178. swheader = le32_to_cpu(*(__le32 *)header);
  1179. if (type == BRCMF_SDIO_FT_SUPER && SDPCM_GLOMDESC(header)) {
  1180. brcmf_err("Glom descriptor found in superframe head\n");
  1181. rd->len = 0;
  1182. return -EINVAL;
  1183. }
  1184. rx_seq = (u8)(swheader & SDPCM_SEQ_MASK);
  1185. rd->channel = (swheader & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT;
  1186. if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
  1187. type != BRCMF_SDIO_FT_SUPER) {
  1188. brcmf_err("HW header length too long\n");
  1189. bus->sdcnt.rx_toolong++;
  1190. brcmf_sdio_rxfail(bus, false, false);
  1191. rd->len = 0;
  1192. return -EPROTO;
  1193. }
  1194. if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
  1195. brcmf_err("Wrong channel for superframe\n");
  1196. rd->len = 0;
  1197. return -EINVAL;
  1198. }
  1199. if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
  1200. rd->channel != SDPCM_EVENT_CHANNEL) {
  1201. brcmf_err("Wrong channel for subframe\n");
  1202. rd->len = 0;
  1203. return -EINVAL;
  1204. }
  1205. rd->dat_offset = brcmf_sdio_getdatoffset(header);
  1206. if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
  1207. brcmf_err("seq %d: bad data offset\n", rx_seq);
  1208. bus->sdcnt.rx_badhdr++;
  1209. brcmf_sdio_rxfail(bus, false, false);
  1210. rd->len = 0;
  1211. return -ENXIO;
  1212. }
  1213. if (rd->seq_num != rx_seq) {
  1214. brcmf_dbg(SDIO, "seq %d, expected %d\n", rx_seq, rd->seq_num);
  1215. bus->sdcnt.rx_badseq++;
  1216. rd->seq_num = rx_seq;
  1217. }
  1218. /* no need to check the reset for subframe */
  1219. if (type == BRCMF_SDIO_FT_SUB)
  1220. return 0;
  1221. rd->len_nxtfrm = (swheader & SDPCM_NEXTLEN_MASK) >> SDPCM_NEXTLEN_SHIFT;
  1222. if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
  1223. /* only warm for NON glom packet */
  1224. if (rd->channel != SDPCM_GLOM_CHANNEL)
  1225. brcmf_err("seq %d: next length error\n", rx_seq);
  1226. rd->len_nxtfrm = 0;
  1227. }
  1228. swheader = le32_to_cpu(*(__le32 *)(header + 4));
  1229. fc = swheader & SDPCM_FCMASK_MASK;
  1230. if (bus->flowcontrol != fc) {
  1231. if (~bus->flowcontrol & fc)
  1232. bus->sdcnt.fc_xoff++;
  1233. if (bus->flowcontrol & ~fc)
  1234. bus->sdcnt.fc_xon++;
  1235. bus->sdcnt.fc_rcvd++;
  1236. bus->flowcontrol = fc;
  1237. }
  1238. tx_seq_max = (swheader & SDPCM_WINDOW_MASK) >> SDPCM_WINDOW_SHIFT;
  1239. if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
  1240. brcmf_err("seq %d: max tx seq number error\n", rx_seq);
  1241. tx_seq_max = bus->tx_seq + 2;
  1242. }
  1243. bus->tx_max = tx_seq_max;
  1244. return 0;
  1245. }
  1246. static inline void brcmf_sdio_update_hwhdr(u8 *header, u16 frm_length)
  1247. {
  1248. *(__le16 *)header = cpu_to_le16(frm_length);
  1249. *(((__le16 *)header) + 1) = cpu_to_le16(~frm_length);
  1250. }
  1251. static void brcmf_sdio_hdpack(struct brcmf_sdio *bus, u8 *header,
  1252. struct brcmf_sdio_hdrinfo *hd_info)
  1253. {
  1254. u32 hdrval;
  1255. u8 hdr_offset;
  1256. brcmf_sdio_update_hwhdr(header, hd_info->len);
  1257. hdr_offset = SDPCM_HWHDR_LEN;
  1258. if (bus->txglom) {
  1259. hdrval = (hd_info->len - hdr_offset) | (hd_info->lastfrm << 24);
  1260. *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
  1261. hdrval = (u16)hd_info->tail_pad << 16;
  1262. *(((__le32 *)(header + hdr_offset)) + 1) = cpu_to_le32(hdrval);
  1263. hdr_offset += SDPCM_HWEXT_LEN;
  1264. }
  1265. hdrval = hd_info->seq_num;
  1266. hdrval |= (hd_info->channel << SDPCM_CHANNEL_SHIFT) &
  1267. SDPCM_CHANNEL_MASK;
  1268. hdrval |= (hd_info->dat_offset << SDPCM_DOFFSET_SHIFT) &
  1269. SDPCM_DOFFSET_MASK;
  1270. *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
  1271. *(((__le32 *)(header + hdr_offset)) + 1) = 0;
  1272. trace_brcmf_sdpcm_hdr(SDPCM_TX + !!(bus->txglom), header);
  1273. }
  1274. static u8 brcmf_sdio_rxglom(struct brcmf_sdio *bus, u8 rxseq)
  1275. {
  1276. u16 dlen, totlen;
  1277. u8 *dptr, num = 0;
  1278. u16 sublen;
  1279. struct sk_buff *pfirst, *pnext;
  1280. int errcode;
  1281. u8 doff, sfdoff;
  1282. struct brcmf_sdio_hdrinfo rd_new;
  1283. /* If packets, issue read(s) and send up packet chain */
  1284. /* Return sequence numbers consumed? */
  1285. brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
  1286. bus->glomd, skb_peek(&bus->glom));
  1287. /* If there's a descriptor, generate the packet chain */
  1288. if (bus->glomd) {
  1289. pfirst = pnext = NULL;
  1290. dlen = (u16) (bus->glomd->len);
  1291. dptr = bus->glomd->data;
  1292. if (!dlen || (dlen & 1)) {
  1293. brcmf_err("bad glomd len(%d), ignore descriptor\n",
  1294. dlen);
  1295. dlen = 0;
  1296. }
  1297. for (totlen = num = 0; dlen; num++) {
  1298. /* Get (and move past) next length */
  1299. sublen = get_unaligned_le16(dptr);
  1300. dlen -= sizeof(u16);
  1301. dptr += sizeof(u16);
  1302. if ((sublen < SDPCM_HDRLEN) ||
  1303. ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
  1304. brcmf_err("descriptor len %d bad: %d\n",
  1305. num, sublen);
  1306. pnext = NULL;
  1307. break;
  1308. }
  1309. if (sublen % bus->sgentry_align) {
  1310. brcmf_err("sublen %d not multiple of %d\n",
  1311. sublen, bus->sgentry_align);
  1312. }
  1313. totlen += sublen;
  1314. /* For last frame, adjust read len so total
  1315. is a block multiple */
  1316. if (!dlen) {
  1317. sublen +=
  1318. (roundup(totlen, bus->blocksize) - totlen);
  1319. totlen = roundup(totlen, bus->blocksize);
  1320. }
  1321. /* Allocate/chain packet for next subframe */
  1322. pnext = brcmu_pkt_buf_get_skb(sublen + bus->sgentry_align);
  1323. if (pnext == NULL) {
  1324. brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
  1325. num, sublen);
  1326. break;
  1327. }
  1328. skb_queue_tail(&bus->glom, pnext);
  1329. /* Adhere to start alignment requirements */
  1330. pkt_align(pnext, sublen, bus->sgentry_align);
  1331. }
  1332. /* If all allocations succeeded, save packet chain
  1333. in bus structure */
  1334. if (pnext) {
  1335. brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
  1336. totlen, num);
  1337. if (BRCMF_GLOM_ON() && bus->cur_read.len &&
  1338. totlen != bus->cur_read.len) {
  1339. brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
  1340. bus->cur_read.len, totlen, rxseq);
  1341. }
  1342. pfirst = pnext = NULL;
  1343. } else {
  1344. brcmf_sdio_free_glom(bus);
  1345. num = 0;
  1346. }
  1347. /* Done with descriptor packet */
  1348. brcmu_pkt_buf_free_skb(bus->glomd);
  1349. bus->glomd = NULL;
  1350. bus->cur_read.len = 0;
  1351. }
  1352. /* Ok -- either we just generated a packet chain,
  1353. or had one from before */
  1354. if (!skb_queue_empty(&bus->glom)) {
  1355. if (BRCMF_GLOM_ON()) {
  1356. brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
  1357. skb_queue_walk(&bus->glom, pnext) {
  1358. brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
  1359. pnext, (u8 *) (pnext->data),
  1360. pnext->len, pnext->len);
  1361. }
  1362. }
  1363. pfirst = skb_peek(&bus->glom);
  1364. dlen = (u16) brcmf_sdio_glom_len(bus);
  1365. /* Do an SDIO read for the superframe. Configurable iovar to
  1366. * read directly into the chained packet, or allocate a large
  1367. * packet and and copy into the chain.
  1368. */
  1369. sdio_claim_host(bus->sdiodev->func1);
  1370. errcode = brcmf_sdiod_recv_chain(bus->sdiodev,
  1371. &bus->glom, dlen);
  1372. sdio_release_host(bus->sdiodev->func1);
  1373. bus->sdcnt.f2rxdata++;
  1374. /* On failure, kill the superframe */
  1375. if (errcode < 0) {
  1376. brcmf_err("glom read of %d bytes failed: %d\n",
  1377. dlen, errcode);
  1378. sdio_claim_host(bus->sdiodev->func1);
  1379. brcmf_sdio_rxfail(bus, true, false);
  1380. bus->sdcnt.rxglomfail++;
  1381. brcmf_sdio_free_glom(bus);
  1382. sdio_release_host(bus->sdiodev->func1);
  1383. return 0;
  1384. }
  1385. brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
  1386. pfirst->data, min_t(int, pfirst->len, 48),
  1387. "SUPERFRAME:\n");
  1388. rd_new.seq_num = rxseq;
  1389. rd_new.len = dlen;
  1390. sdio_claim_host(bus->sdiodev->func1);
  1391. errcode = brcmf_sdio_hdparse(bus, pfirst->data, &rd_new,
  1392. BRCMF_SDIO_FT_SUPER);
  1393. sdio_release_host(bus->sdiodev->func1);
  1394. bus->cur_read.len = rd_new.len_nxtfrm << 4;
  1395. /* Remove superframe header, remember offset */
  1396. skb_pull(pfirst, rd_new.dat_offset);
  1397. sfdoff = rd_new.dat_offset;
  1398. num = 0;
  1399. /* Validate all the subframe headers */
  1400. skb_queue_walk(&bus->glom, pnext) {
  1401. /* leave when invalid subframe is found */
  1402. if (errcode)
  1403. break;
  1404. rd_new.len = pnext->len;
  1405. rd_new.seq_num = rxseq++;
  1406. sdio_claim_host(bus->sdiodev->func1);
  1407. errcode = brcmf_sdio_hdparse(bus, pnext->data, &rd_new,
  1408. BRCMF_SDIO_FT_SUB);
  1409. sdio_release_host(bus->sdiodev->func1);
  1410. brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
  1411. pnext->data, 32, "subframe:\n");
  1412. num++;
  1413. }
  1414. if (errcode) {
  1415. /* Terminate frame on error */
  1416. sdio_claim_host(bus->sdiodev->func1);
  1417. brcmf_sdio_rxfail(bus, true, false);
  1418. bus->sdcnt.rxglomfail++;
  1419. brcmf_sdio_free_glom(bus);
  1420. sdio_release_host(bus->sdiodev->func1);
  1421. bus->cur_read.len = 0;
  1422. return 0;
  1423. }
  1424. /* Basic SD framing looks ok - process each packet (header) */
  1425. skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
  1426. dptr = (u8 *) (pfirst->data);
  1427. sublen = get_unaligned_le16(dptr);
  1428. doff = brcmf_sdio_getdatoffset(&dptr[SDPCM_HWHDR_LEN]);
  1429. brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
  1430. dptr, pfirst->len,
  1431. "Rx Subframe Data:\n");
  1432. __skb_trim(pfirst, sublen);
  1433. skb_pull(pfirst, doff);
  1434. if (pfirst->len == 0) {
  1435. skb_unlink(pfirst, &bus->glom);
  1436. brcmu_pkt_buf_free_skb(pfirst);
  1437. continue;
  1438. }
  1439. brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
  1440. pfirst->data,
  1441. min_t(int, pfirst->len, 32),
  1442. "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
  1443. bus->glom.qlen, pfirst, pfirst->data,
  1444. pfirst->len, pfirst->next,
  1445. pfirst->prev);
  1446. skb_unlink(pfirst, &bus->glom);
  1447. if (brcmf_sdio_fromevntchan(&dptr[SDPCM_HWHDR_LEN]))
  1448. brcmf_rx_event(bus->sdiodev->dev, pfirst);
  1449. else
  1450. brcmf_rx_frame(bus->sdiodev->dev, pfirst,
  1451. false);
  1452. bus->sdcnt.rxglompkts++;
  1453. }
  1454. bus->sdcnt.rxglomframes++;
  1455. }
  1456. return num;
  1457. }
  1458. static int brcmf_sdio_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
  1459. bool *pending)
  1460. {
  1461. DECLARE_WAITQUEUE(wait, current);
  1462. int timeout = DCMD_RESP_TIMEOUT;
  1463. /* Wait until control frame is available */
  1464. add_wait_queue(&bus->dcmd_resp_wait, &wait);
  1465. set_current_state(TASK_INTERRUPTIBLE);
  1466. while (!(*condition) && (!signal_pending(current) && timeout))
  1467. timeout = schedule_timeout(timeout);
  1468. if (signal_pending(current))
  1469. *pending = true;
  1470. set_current_state(TASK_RUNNING);
  1471. remove_wait_queue(&bus->dcmd_resp_wait, &wait);
  1472. return timeout;
  1473. }
  1474. static int brcmf_sdio_dcmd_resp_wake(struct brcmf_sdio *bus)
  1475. {
  1476. wake_up_interruptible(&bus->dcmd_resp_wait);
  1477. return 0;
  1478. }
  1479. static void
  1480. brcmf_sdio_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
  1481. {
  1482. uint rdlen, pad;
  1483. u8 *buf = NULL, *rbuf;
  1484. int sdret;
  1485. brcmf_dbg(SDIO, "Enter\n");
  1486. if (bus->rxblen)
  1487. buf = vzalloc(bus->rxblen);
  1488. if (!buf)
  1489. goto done;
  1490. rbuf = bus->rxbuf;
  1491. pad = ((unsigned long)rbuf % bus->head_align);
  1492. if (pad)
  1493. rbuf += (bus->head_align - pad);
  1494. /* Copy the already-read portion over */
  1495. memcpy(buf, hdr, BRCMF_FIRSTREAD);
  1496. if (len <= BRCMF_FIRSTREAD)
  1497. goto gotpkt;
  1498. /* Raise rdlen to next SDIO block to avoid tail command */
  1499. rdlen = len - BRCMF_FIRSTREAD;
  1500. if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
  1501. pad = bus->blocksize - (rdlen % bus->blocksize);
  1502. if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
  1503. ((len + pad) < bus->sdiodev->bus_if->maxctl))
  1504. rdlen += pad;
  1505. } else if (rdlen % bus->head_align) {
  1506. rdlen += bus->head_align - (rdlen % bus->head_align);
  1507. }
  1508. /* Drop if the read is too big or it exceeds our maximum */
  1509. if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
  1510. brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
  1511. rdlen, bus->sdiodev->bus_if->maxctl);
  1512. brcmf_sdio_rxfail(bus, false, false);
  1513. goto done;
  1514. }
  1515. if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
  1516. brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
  1517. len, len - doff, bus->sdiodev->bus_if->maxctl);
  1518. bus->sdcnt.rx_toolong++;
  1519. brcmf_sdio_rxfail(bus, false, false);
  1520. goto done;
  1521. }
  1522. /* Read remain of frame body */
  1523. sdret = brcmf_sdiod_recv_buf(bus->sdiodev, rbuf, rdlen);
  1524. bus->sdcnt.f2rxdata++;
  1525. /* Control frame failures need retransmission */
  1526. if (sdret < 0) {
  1527. brcmf_err("read %d control bytes failed: %d\n",
  1528. rdlen, sdret);
  1529. bus->sdcnt.rxc_errors++;
  1530. brcmf_sdio_rxfail(bus, true, true);
  1531. goto done;
  1532. } else
  1533. memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
  1534. gotpkt:
  1535. brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
  1536. buf, len, "RxCtrl:\n");
  1537. /* Point to valid data and indicate its length */
  1538. spin_lock_bh(&bus->rxctl_lock);
  1539. if (bus->rxctl) {
  1540. brcmf_err("last control frame is being processed.\n");
  1541. spin_unlock_bh(&bus->rxctl_lock);
  1542. vfree(buf);
  1543. goto done;
  1544. }
  1545. bus->rxctl = buf + doff;
  1546. bus->rxctl_orig = buf;
  1547. bus->rxlen = len - doff;
  1548. spin_unlock_bh(&bus->rxctl_lock);
  1549. done:
  1550. /* Awake any waiters */
  1551. brcmf_sdio_dcmd_resp_wake(bus);
  1552. }
  1553. /* Pad read to blocksize for efficiency */
  1554. static void brcmf_sdio_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
  1555. {
  1556. if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
  1557. *pad = bus->blocksize - (*rdlen % bus->blocksize);
  1558. if (*pad <= bus->roundup && *pad < bus->blocksize &&
  1559. *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
  1560. *rdlen += *pad;
  1561. } else if (*rdlen % bus->head_align) {
  1562. *rdlen += bus->head_align - (*rdlen % bus->head_align);
  1563. }
  1564. }
  1565. static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
  1566. {
  1567. struct sk_buff *pkt; /* Packet for event or data frames */
  1568. u16 pad; /* Number of pad bytes to read */
  1569. uint rxleft = 0; /* Remaining number of frames allowed */
  1570. int ret; /* Return code from calls */
  1571. uint rxcount = 0; /* Total frames read */
  1572. struct brcmf_sdio_hdrinfo *rd = &bus->cur_read, rd_new;
  1573. u8 head_read = 0;
  1574. brcmf_dbg(SDIO, "Enter\n");
  1575. /* Not finished unless we encounter no more frames indication */
  1576. bus->rxpending = true;
  1577. for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
  1578. !bus->rxskip && rxleft && bus->sdiodev->state == BRCMF_SDIOD_DATA;
  1579. rd->seq_num++, rxleft--) {
  1580. /* Handle glomming separately */
  1581. if (bus->glomd || !skb_queue_empty(&bus->glom)) {
  1582. u8 cnt;
  1583. brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
  1584. bus->glomd, skb_peek(&bus->glom));
  1585. cnt = brcmf_sdio_rxglom(bus, rd->seq_num);
  1586. brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
  1587. rd->seq_num += cnt - 1;
  1588. rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
  1589. continue;
  1590. }
  1591. rd->len_left = rd->len;
  1592. /* read header first for unknow frame length */
  1593. sdio_claim_host(bus->sdiodev->func1);
  1594. if (!rd->len) {
  1595. ret = brcmf_sdiod_recv_buf(bus->sdiodev,
  1596. bus->rxhdr, BRCMF_FIRSTREAD);
  1597. bus->sdcnt.f2rxhdrs++;
  1598. if (ret < 0) {
  1599. brcmf_err("RXHEADER FAILED: %d\n",
  1600. ret);
  1601. bus->sdcnt.rx_hdrfail++;
  1602. brcmf_sdio_rxfail(bus, true, true);
  1603. sdio_release_host(bus->sdiodev->func1);
  1604. continue;
  1605. }
  1606. brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
  1607. bus->rxhdr, SDPCM_HDRLEN,
  1608. "RxHdr:\n");
  1609. if (brcmf_sdio_hdparse(bus, bus->rxhdr, rd,
  1610. BRCMF_SDIO_FT_NORMAL)) {
  1611. sdio_release_host(bus->sdiodev->func1);
  1612. if (!bus->rxpending)
  1613. break;
  1614. else
  1615. continue;
  1616. }
  1617. if (rd->channel == SDPCM_CONTROL_CHANNEL) {
  1618. brcmf_sdio_read_control(bus, bus->rxhdr,
  1619. rd->len,
  1620. rd->dat_offset);
  1621. /* prepare the descriptor for the next read */
  1622. rd->len = rd->len_nxtfrm << 4;
  1623. rd->len_nxtfrm = 0;
  1624. /* treat all packet as event if we don't know */
  1625. rd->channel = SDPCM_EVENT_CHANNEL;
  1626. sdio_release_host(bus->sdiodev->func1);
  1627. continue;
  1628. }
  1629. rd->len_left = rd->len > BRCMF_FIRSTREAD ?
  1630. rd->len - BRCMF_FIRSTREAD : 0;
  1631. head_read = BRCMF_FIRSTREAD;
  1632. }
  1633. brcmf_sdio_pad(bus, &pad, &rd->len_left);
  1634. pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
  1635. bus->head_align);
  1636. if (!pkt) {
  1637. /* Give up on data, request rtx of events */
  1638. brcmf_err("brcmu_pkt_buf_get_skb failed\n");
  1639. brcmf_sdio_rxfail(bus, false,
  1640. RETRYCHAN(rd->channel));
  1641. sdio_release_host(bus->sdiodev->func1);
  1642. continue;
  1643. }
  1644. skb_pull(pkt, head_read);
  1645. pkt_align(pkt, rd->len_left, bus->head_align);
  1646. ret = brcmf_sdiod_recv_pkt(bus->sdiodev, pkt);
  1647. bus->sdcnt.f2rxdata++;
  1648. sdio_release_host(bus->sdiodev->func1);
  1649. if (ret < 0) {
  1650. brcmf_err("read %d bytes from channel %d failed: %d\n",
  1651. rd->len, rd->channel, ret);
  1652. brcmu_pkt_buf_free_skb(pkt);
  1653. sdio_claim_host(bus->sdiodev->func1);
  1654. brcmf_sdio_rxfail(bus, true,
  1655. RETRYCHAN(rd->channel));
  1656. sdio_release_host(bus->sdiodev->func1);
  1657. continue;
  1658. }
  1659. if (head_read) {
  1660. skb_push(pkt, head_read);
  1661. memcpy(pkt->data, bus->rxhdr, head_read);
  1662. head_read = 0;
  1663. } else {
  1664. memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
  1665. rd_new.seq_num = rd->seq_num;
  1666. sdio_claim_host(bus->sdiodev->func1);
  1667. if (brcmf_sdio_hdparse(bus, bus->rxhdr, &rd_new,
  1668. BRCMF_SDIO_FT_NORMAL)) {
  1669. rd->len = 0;
  1670. brcmu_pkt_buf_free_skb(pkt);
  1671. continue;
  1672. }
  1673. bus->sdcnt.rx_readahead_cnt++;
  1674. if (rd->len != roundup(rd_new.len, 16)) {
  1675. brcmf_err("frame length mismatch:read %d, should be %d\n",
  1676. rd->len,
  1677. roundup(rd_new.len, 16) >> 4);
  1678. rd->len = 0;
  1679. brcmf_sdio_rxfail(bus, true, true);
  1680. sdio_release_host(bus->sdiodev->func1);
  1681. brcmu_pkt_buf_free_skb(pkt);
  1682. continue;
  1683. }
  1684. sdio_release_host(bus->sdiodev->func1);
  1685. rd->len_nxtfrm = rd_new.len_nxtfrm;
  1686. rd->channel = rd_new.channel;
  1687. rd->dat_offset = rd_new.dat_offset;
  1688. brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
  1689. BRCMF_DATA_ON()) &&
  1690. BRCMF_HDRS_ON(),
  1691. bus->rxhdr, SDPCM_HDRLEN,
  1692. "RxHdr:\n");
  1693. if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
  1694. brcmf_err("readahead on control packet %d?\n",
  1695. rd_new.seq_num);
  1696. /* Force retry w/normal header read */
  1697. rd->len = 0;
  1698. sdio_claim_host(bus->sdiodev->func1);
  1699. brcmf_sdio_rxfail(bus, false, true);
  1700. sdio_release_host(bus->sdiodev->func1);
  1701. brcmu_pkt_buf_free_skb(pkt);
  1702. continue;
  1703. }
  1704. }
  1705. brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
  1706. pkt->data, rd->len, "Rx Data:\n");
  1707. /* Save superframe descriptor and allocate packet frame */
  1708. if (rd->channel == SDPCM_GLOM_CHANNEL) {
  1709. if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_HWHDR_LEN])) {
  1710. brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
  1711. rd->len);
  1712. brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
  1713. pkt->data, rd->len,
  1714. "Glom Data:\n");
  1715. __skb_trim(pkt, rd->len);
  1716. skb_pull(pkt, SDPCM_HDRLEN);
  1717. bus->glomd = pkt;
  1718. } else {
  1719. brcmf_err("%s: glom superframe w/o "
  1720. "descriptor!\n", __func__);
  1721. sdio_claim_host(bus->sdiodev->func1);
  1722. brcmf_sdio_rxfail(bus, false, false);
  1723. sdio_release_host(bus->sdiodev->func1);
  1724. }
  1725. /* prepare the descriptor for the next read */
  1726. rd->len = rd->len_nxtfrm << 4;
  1727. rd->len_nxtfrm = 0;
  1728. /* treat all packet as event if we don't know */
  1729. rd->channel = SDPCM_EVENT_CHANNEL;
  1730. continue;
  1731. }
  1732. /* Fill in packet len and prio, deliver upward */
  1733. __skb_trim(pkt, rd->len);
  1734. skb_pull(pkt, rd->dat_offset);
  1735. if (pkt->len == 0)
  1736. brcmu_pkt_buf_free_skb(pkt);
  1737. else if (rd->channel == SDPCM_EVENT_CHANNEL)
  1738. brcmf_rx_event(bus->sdiodev->dev, pkt);
  1739. else
  1740. brcmf_rx_frame(bus->sdiodev->dev, pkt,
  1741. false);
  1742. /* prepare the descriptor for the next read */
  1743. rd->len = rd->len_nxtfrm << 4;
  1744. rd->len_nxtfrm = 0;
  1745. /* treat all packet as event if we don't know */
  1746. rd->channel = SDPCM_EVENT_CHANNEL;
  1747. }
  1748. rxcount = maxframes - rxleft;
  1749. /* Message if we hit the limit */
  1750. if (!rxleft)
  1751. brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
  1752. else
  1753. brcmf_dbg(DATA, "processed %d frames\n", rxcount);
  1754. /* Back off rxseq if awaiting rtx, update rx_seq */
  1755. if (bus->rxskip)
  1756. rd->seq_num--;
  1757. bus->rx_seq = rd->seq_num;
  1758. return rxcount;
  1759. }
  1760. static void
  1761. brcmf_sdio_wait_event_wakeup(struct brcmf_sdio *bus)
  1762. {
  1763. wake_up_interruptible(&bus->ctrl_wait);
  1764. return;
  1765. }
  1766. static int brcmf_sdio_txpkt_hdalign(struct brcmf_sdio *bus, struct sk_buff *pkt)
  1767. {
  1768. struct brcmf_bus_stats *stats;
  1769. u16 head_pad;
  1770. u8 *dat_buf;
  1771. dat_buf = (u8 *)(pkt->data);
  1772. /* Check head padding */
  1773. head_pad = ((unsigned long)dat_buf % bus->head_align);
  1774. if (head_pad) {
  1775. if (skb_headroom(pkt) < head_pad) {
  1776. stats = &bus->sdiodev->bus_if->stats;
  1777. atomic_inc(&stats->pktcowed);
  1778. if (skb_cow_head(pkt, head_pad)) {
  1779. atomic_inc(&stats->pktcow_failed);
  1780. return -ENOMEM;
  1781. }
  1782. head_pad = 0;
  1783. }
  1784. skb_push(pkt, head_pad);
  1785. dat_buf = (u8 *)(pkt->data);
  1786. }
  1787. memset(dat_buf, 0, head_pad + bus->tx_hdrlen);
  1788. return head_pad;
  1789. }
  1790. /*
  1791. * struct brcmf_skbuff_cb reserves first two bytes in sk_buff::cb for
  1792. * bus layer usage.
  1793. */
  1794. /* flag marking a dummy skb added for DMA alignment requirement */
  1795. #define ALIGN_SKB_FLAG 0x8000
  1796. /* bit mask of data length chopped from the previous packet */
  1797. #define ALIGN_SKB_CHOP_LEN_MASK 0x7fff
  1798. static int brcmf_sdio_txpkt_prep_sg(struct brcmf_sdio *bus,
  1799. struct sk_buff_head *pktq,
  1800. struct sk_buff *pkt, u16 total_len)
  1801. {
  1802. struct brcmf_sdio_dev *sdiodev;
  1803. struct sk_buff *pkt_pad;
  1804. u16 tail_pad, tail_chop, chain_pad;
  1805. unsigned int blksize;
  1806. bool lastfrm;
  1807. int ntail, ret;
  1808. sdiodev = bus->sdiodev;
  1809. blksize = sdiodev->func2->cur_blksize;
  1810. /* sg entry alignment should be a divisor of block size */
  1811. WARN_ON(blksize % bus->sgentry_align);
  1812. /* Check tail padding */
  1813. lastfrm = skb_queue_is_last(pktq, pkt);
  1814. tail_pad = 0;
  1815. tail_chop = pkt->len % bus->sgentry_align;
  1816. if (tail_chop)
  1817. tail_pad = bus->sgentry_align - tail_chop;
  1818. chain_pad = (total_len + tail_pad) % blksize;
  1819. if (lastfrm && chain_pad)
  1820. tail_pad += blksize - chain_pad;
  1821. if (skb_tailroom(pkt) < tail_pad && pkt->len > blksize) {
  1822. pkt_pad = brcmu_pkt_buf_get_skb(tail_pad + tail_chop +
  1823. bus->head_align);
  1824. if (pkt_pad == NULL)
  1825. return -ENOMEM;
  1826. ret = brcmf_sdio_txpkt_hdalign(bus, pkt_pad);
  1827. if (unlikely(ret < 0)) {
  1828. kfree_skb(pkt_pad);
  1829. return ret;
  1830. }
  1831. memcpy(pkt_pad->data,
  1832. pkt->data + pkt->len - tail_chop,
  1833. tail_chop);
  1834. *(u16 *)(pkt_pad->cb) = ALIGN_SKB_FLAG + tail_chop;
  1835. skb_trim(pkt, pkt->len - tail_chop);
  1836. skb_trim(pkt_pad, tail_pad + tail_chop);
  1837. __skb_queue_after(pktq, pkt, pkt_pad);
  1838. } else {
  1839. ntail = pkt->data_len + tail_pad -
  1840. (pkt->end - pkt->tail);
  1841. if (skb_cloned(pkt) || ntail > 0)
  1842. if (pskb_expand_head(pkt, 0, ntail, GFP_ATOMIC))
  1843. return -ENOMEM;
  1844. if (skb_linearize(pkt))
  1845. return -ENOMEM;
  1846. __skb_put(pkt, tail_pad);
  1847. }
  1848. return tail_pad;
  1849. }
  1850. /**
  1851. * brcmf_sdio_txpkt_prep - packet preparation for transmit
  1852. * @bus: brcmf_sdio structure pointer
  1853. * @pktq: packet list pointer
  1854. * @chan: virtual channel to transmit the packet
  1855. *
  1856. * Processes to be applied to the packet
  1857. * - Align data buffer pointer
  1858. * - Align data buffer length
  1859. * - Prepare header
  1860. * Return: negative value if there is error
  1861. */
  1862. static int
  1863. brcmf_sdio_txpkt_prep(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
  1864. uint chan)
  1865. {
  1866. u16 head_pad, total_len;
  1867. struct sk_buff *pkt_next;
  1868. u8 txseq;
  1869. int ret;
  1870. struct brcmf_sdio_hdrinfo hd_info = {0};
  1871. txseq = bus->tx_seq;
  1872. total_len = 0;
  1873. skb_queue_walk(pktq, pkt_next) {
  1874. /* alignment packet inserted in previous
  1875. * loop cycle can be skipped as it is
  1876. * already properly aligned and does not
  1877. * need an sdpcm header.
  1878. */
  1879. if (*(u16 *)(pkt_next->cb) & ALIGN_SKB_FLAG)
  1880. continue;
  1881. /* align packet data pointer */
  1882. ret = brcmf_sdio_txpkt_hdalign(bus, pkt_next);
  1883. if (ret < 0)
  1884. return ret;
  1885. head_pad = (u16)ret;
  1886. if (head_pad)
  1887. memset(pkt_next->data + bus->tx_hdrlen, 0, head_pad);
  1888. total_len += pkt_next->len;
  1889. hd_info.len = pkt_next->len;
  1890. hd_info.lastfrm = skb_queue_is_last(pktq, pkt_next);
  1891. if (bus->txglom && pktq->qlen > 1) {
  1892. ret = brcmf_sdio_txpkt_prep_sg(bus, pktq,
  1893. pkt_next, total_len);
  1894. if (ret < 0)
  1895. return ret;
  1896. hd_info.tail_pad = (u16)ret;
  1897. total_len += (u16)ret;
  1898. }
  1899. hd_info.channel = chan;
  1900. hd_info.dat_offset = head_pad + bus->tx_hdrlen;
  1901. hd_info.seq_num = txseq++;
  1902. /* Now fill the header */
  1903. brcmf_sdio_hdpack(bus, pkt_next->data, &hd_info);
  1904. if (BRCMF_BYTES_ON() &&
  1905. ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
  1906. (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)))
  1907. brcmf_dbg_hex_dump(true, pkt_next->data, hd_info.len,
  1908. "Tx Frame:\n");
  1909. else if (BRCMF_HDRS_ON())
  1910. brcmf_dbg_hex_dump(true, pkt_next->data,
  1911. head_pad + bus->tx_hdrlen,
  1912. "Tx Header:\n");
  1913. }
  1914. /* Hardware length tag of the first packet should be total
  1915. * length of the chain (including padding)
  1916. */
  1917. if (bus->txglom)
  1918. brcmf_sdio_update_hwhdr(pktq->next->data, total_len);
  1919. return 0;
  1920. }
  1921. /**
  1922. * brcmf_sdio_txpkt_postp - packet post processing for transmit
  1923. * @bus: brcmf_sdio structure pointer
  1924. * @pktq: packet list pointer
  1925. *
  1926. * Processes to be applied to the packet
  1927. * - Remove head padding
  1928. * - Remove tail padding
  1929. */
  1930. static void
  1931. brcmf_sdio_txpkt_postp(struct brcmf_sdio *bus, struct sk_buff_head *pktq)
  1932. {
  1933. u8 *hdr;
  1934. u32 dat_offset;
  1935. u16 tail_pad;
  1936. u16 dummy_flags, chop_len;
  1937. struct sk_buff *pkt_next, *tmp, *pkt_prev;
  1938. skb_queue_walk_safe(pktq, pkt_next, tmp) {
  1939. dummy_flags = *(u16 *)(pkt_next->cb);
  1940. if (dummy_flags & ALIGN_SKB_FLAG) {
  1941. chop_len = dummy_flags & ALIGN_SKB_CHOP_LEN_MASK;
  1942. if (chop_len) {
  1943. pkt_prev = pkt_next->prev;
  1944. skb_put(pkt_prev, chop_len);
  1945. }
  1946. __skb_unlink(pkt_next, pktq);
  1947. brcmu_pkt_buf_free_skb(pkt_next);
  1948. } else {
  1949. hdr = pkt_next->data + bus->tx_hdrlen - SDPCM_SWHDR_LEN;
  1950. dat_offset = le32_to_cpu(*(__le32 *)hdr);
  1951. dat_offset = (dat_offset & SDPCM_DOFFSET_MASK) >>
  1952. SDPCM_DOFFSET_SHIFT;
  1953. skb_pull(pkt_next, dat_offset);
  1954. if (bus->txglom) {
  1955. tail_pad = le16_to_cpu(*(__le16 *)(hdr - 2));
  1956. skb_trim(pkt_next, pkt_next->len - tail_pad);
  1957. }
  1958. }
  1959. }
  1960. }
  1961. /* Writes a HW/SW header into the packet and sends it. */
  1962. /* Assumes: (a) header space already there, (b) caller holds lock */
  1963. static int brcmf_sdio_txpkt(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
  1964. uint chan)
  1965. {
  1966. int ret;
  1967. struct sk_buff *pkt_next, *tmp;
  1968. brcmf_dbg(TRACE, "Enter\n");
  1969. ret = brcmf_sdio_txpkt_prep(bus, pktq, chan);
  1970. if (ret)
  1971. goto done;
  1972. sdio_claim_host(bus->sdiodev->func1);
  1973. ret = brcmf_sdiod_send_pkt(bus->sdiodev, pktq);
  1974. bus->sdcnt.f2txdata++;
  1975. if (ret < 0)
  1976. brcmf_sdio_txfail(bus);
  1977. sdio_release_host(bus->sdiodev->func1);
  1978. done:
  1979. brcmf_sdio_txpkt_postp(bus, pktq);
  1980. if (ret == 0)
  1981. bus->tx_seq = (bus->tx_seq + pktq->qlen) % SDPCM_SEQ_WRAP;
  1982. skb_queue_walk_safe(pktq, pkt_next, tmp) {
  1983. __skb_unlink(pkt_next, pktq);
  1984. brcmf_proto_bcdc_txcomplete(bus->sdiodev->dev, pkt_next,
  1985. ret == 0);
  1986. }
  1987. return ret;
  1988. }
  1989. static uint brcmf_sdio_sendfromq(struct brcmf_sdio *bus, uint maxframes)
  1990. {
  1991. struct sk_buff *pkt;
  1992. struct sk_buff_head pktq;
  1993. u32 intstat_addr = bus->sdio_core->base + SD_REG(intstatus);
  1994. u32 intstatus = 0;
  1995. int ret = 0, prec_out, i;
  1996. uint cnt = 0;
  1997. u8 tx_prec_map, pkt_num;
  1998. brcmf_dbg(TRACE, "Enter\n");
  1999. tx_prec_map = ~bus->flowcontrol;
  2000. /* Send frames until the limit or some other event */
  2001. for (cnt = 0; (cnt < maxframes) && data_ok(bus);) {
  2002. pkt_num = 1;
  2003. if (bus->txglom)
  2004. pkt_num = min_t(u8, bus->tx_max - bus->tx_seq,
  2005. bus->sdiodev->txglomsz);
  2006. pkt_num = min_t(u32, pkt_num,
  2007. brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol));
  2008. __skb_queue_head_init(&pktq);
  2009. spin_lock_bh(&bus->txq_lock);
  2010. for (i = 0; i < pkt_num; i++) {
  2011. pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map,
  2012. &prec_out);
  2013. if (pkt == NULL)
  2014. break;
  2015. __skb_queue_tail(&pktq, pkt);
  2016. }
  2017. spin_unlock_bh(&bus->txq_lock);
  2018. if (i == 0)
  2019. break;
  2020. ret = brcmf_sdio_txpkt(bus, &pktq, SDPCM_DATA_CHANNEL);
  2021. cnt += i;
  2022. /* In poll mode, need to check for other events */
  2023. if (!bus->intr) {
  2024. /* Check device status, signal pending interrupt */
  2025. sdio_claim_host(bus->sdiodev->func1);
  2026. intstatus = brcmf_sdiod_readl(bus->sdiodev,
  2027. intstat_addr, &ret);
  2028. sdio_release_host(bus->sdiodev->func1);
  2029. bus->sdcnt.f2txdata++;
  2030. if (ret != 0)
  2031. break;
  2032. if (intstatus & bus->hostintmask)
  2033. atomic_set(&bus->ipend, 1);
  2034. }
  2035. }
  2036. /* Deflow-control stack if needed */
  2037. if ((bus->sdiodev->state == BRCMF_SDIOD_DATA) &&
  2038. bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
  2039. bus->txoff = false;
  2040. brcmf_proto_bcdc_txflowblock(bus->sdiodev->dev, false);
  2041. }
  2042. return cnt;
  2043. }
  2044. static int brcmf_sdio_tx_ctrlframe(struct brcmf_sdio *bus, u8 *frame, u16 len)
  2045. {
  2046. u8 doff;
  2047. u16 pad;
  2048. uint retries = 0;
  2049. struct brcmf_sdio_hdrinfo hd_info = {0};
  2050. int ret;
  2051. brcmf_dbg(SDIO, "Enter\n");
  2052. /* Back the pointer to make room for bus header */
  2053. frame -= bus->tx_hdrlen;
  2054. len += bus->tx_hdrlen;
  2055. /* Add alignment padding (optional for ctl frames) */
  2056. doff = ((unsigned long)frame % bus->head_align);
  2057. if (doff) {
  2058. frame -= doff;
  2059. len += doff;
  2060. memset(frame + bus->tx_hdrlen, 0, doff);
  2061. }
  2062. /* Round send length to next SDIO block */
  2063. pad = 0;
  2064. if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
  2065. pad = bus->blocksize - (len % bus->blocksize);
  2066. if ((pad > bus->roundup) || (pad >= bus->blocksize))
  2067. pad = 0;
  2068. } else if (len % bus->head_align) {
  2069. pad = bus->head_align - (len % bus->head_align);
  2070. }
  2071. len += pad;
  2072. hd_info.len = len - pad;
  2073. hd_info.channel = SDPCM_CONTROL_CHANNEL;
  2074. hd_info.dat_offset = doff + bus->tx_hdrlen;
  2075. hd_info.seq_num = bus->tx_seq;
  2076. hd_info.lastfrm = true;
  2077. hd_info.tail_pad = pad;
  2078. brcmf_sdio_hdpack(bus, frame, &hd_info);
  2079. if (bus->txglom)
  2080. brcmf_sdio_update_hwhdr(frame, len);
  2081. brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
  2082. frame, len, "Tx Frame:\n");
  2083. brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
  2084. BRCMF_HDRS_ON(),
  2085. frame, min_t(u16, len, 16), "TxHdr:\n");
  2086. do {
  2087. ret = brcmf_sdiod_send_buf(bus->sdiodev, frame, len);
  2088. if (ret < 0)
  2089. brcmf_sdio_txfail(bus);
  2090. else
  2091. bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
  2092. } while (ret < 0 && retries++ < TXRETRIES);
  2093. return ret;
  2094. }
  2095. static void brcmf_sdio_bus_stop(struct device *dev)
  2096. {
  2097. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  2098. struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
  2099. struct brcmf_sdio *bus = sdiodev->bus;
  2100. struct brcmf_core *core = bus->sdio_core;
  2101. u32 local_hostintmask;
  2102. u8 saveclk;
  2103. int err;
  2104. brcmf_dbg(TRACE, "Enter\n");
  2105. if (bus->watchdog_tsk) {
  2106. send_sig(SIGTERM, bus->watchdog_tsk, 1);
  2107. kthread_stop(bus->watchdog_tsk);
  2108. bus->watchdog_tsk = NULL;
  2109. }
  2110. if (sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
  2111. sdio_claim_host(sdiodev->func1);
  2112. /* Enable clock for device interrupts */
  2113. brcmf_sdio_bus_sleep(bus, false, false);
  2114. /* Disable and clear interrupts at the chip level also */
  2115. brcmf_sdiod_writel(sdiodev, core->base + SD_REG(hostintmask),
  2116. 0, NULL);
  2117. local_hostintmask = bus->hostintmask;
  2118. bus->hostintmask = 0;
  2119. /* Force backplane clocks to assure F2 interrupt propagates */
  2120. saveclk = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
  2121. &err);
  2122. if (!err)
  2123. brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
  2124. (saveclk | SBSDIO_FORCE_HT), &err);
  2125. if (err)
  2126. brcmf_err("Failed to force clock for F2: err %d\n",
  2127. err);
  2128. /* Turn off the bus (F2), free any pending packets */
  2129. brcmf_dbg(INTR, "disable SDIO interrupts\n");
  2130. sdio_disable_func(sdiodev->func2);
  2131. /* Clear any pending interrupts now that F2 is disabled */
  2132. brcmf_sdiod_writel(sdiodev, core->base + SD_REG(intstatus),
  2133. local_hostintmask, NULL);
  2134. sdio_release_host(sdiodev->func1);
  2135. }
  2136. /* Clear the data packet queues */
  2137. brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
  2138. /* Clear any held glomming stuff */
  2139. brcmu_pkt_buf_free_skb(bus->glomd);
  2140. brcmf_sdio_free_glom(bus);
  2141. /* Clear rx control and wake any waiters */
  2142. spin_lock_bh(&bus->rxctl_lock);
  2143. bus->rxlen = 0;
  2144. spin_unlock_bh(&bus->rxctl_lock);
  2145. brcmf_sdio_dcmd_resp_wake(bus);
  2146. /* Reset some F2 state stuff */
  2147. bus->rxskip = false;
  2148. bus->tx_seq = bus->rx_seq = 0;
  2149. }
  2150. static inline void brcmf_sdio_clrintr(struct brcmf_sdio *bus)
  2151. {
  2152. struct brcmf_sdio_dev *sdiodev;
  2153. unsigned long flags;
  2154. sdiodev = bus->sdiodev;
  2155. if (sdiodev->oob_irq_requested) {
  2156. spin_lock_irqsave(&sdiodev->irq_en_lock, flags);
  2157. if (!sdiodev->irq_en && !atomic_read(&bus->ipend)) {
  2158. enable_irq(sdiodev->settings->bus.sdio.oob_irq_nr);
  2159. sdiodev->irq_en = true;
  2160. }
  2161. spin_unlock_irqrestore(&sdiodev->irq_en_lock, flags);
  2162. }
  2163. }
  2164. static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
  2165. {
  2166. struct brcmf_core *core = bus->sdio_core;
  2167. u32 addr;
  2168. unsigned long val;
  2169. int ret;
  2170. addr = core->base + SD_REG(intstatus);
  2171. val = brcmf_sdiod_readl(bus->sdiodev, addr, &ret);
  2172. bus->sdcnt.f1regdata++;
  2173. if (ret != 0)
  2174. return ret;
  2175. val &= bus->hostintmask;
  2176. atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
  2177. /* Clear interrupts */
  2178. if (val) {
  2179. brcmf_sdiod_writel(bus->sdiodev, addr, val, &ret);
  2180. bus->sdcnt.f1regdata++;
  2181. atomic_or(val, &bus->intstatus);
  2182. }
  2183. return ret;
  2184. }
  2185. static void brcmf_sdio_dpc(struct brcmf_sdio *bus)
  2186. {
  2187. struct brcmf_sdio_dev *sdiod = bus->sdiodev;
  2188. u32 newstatus = 0;
  2189. u32 intstat_addr = bus->sdio_core->base + SD_REG(intstatus);
  2190. unsigned long intstatus;
  2191. uint txlimit = bus->txbound; /* Tx frames to send before resched */
  2192. uint framecnt; /* Temporary counter of tx/rx frames */
  2193. int err = 0;
  2194. brcmf_dbg(SDIO, "Enter\n");
  2195. sdio_claim_host(bus->sdiodev->func1);
  2196. /* If waiting for HTAVAIL, check status */
  2197. if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
  2198. u8 clkctl, devctl = 0;
  2199. #ifdef DEBUG
  2200. /* Check for inconsistent device control */
  2201. devctl = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_DEVICE_CTL,
  2202. &err);
  2203. #endif /* DEBUG */
  2204. /* Read CSR, if clock on switch to AVAIL, else ignore */
  2205. clkctl = brcmf_sdiod_readb(bus->sdiodev,
  2206. SBSDIO_FUNC1_CHIPCLKCSR, &err);
  2207. brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
  2208. devctl, clkctl);
  2209. if (SBSDIO_HTAV(clkctl)) {
  2210. devctl = brcmf_sdiod_readb(bus->sdiodev,
  2211. SBSDIO_DEVICE_CTL, &err);
  2212. devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
  2213. brcmf_sdiod_writeb(bus->sdiodev,
  2214. SBSDIO_DEVICE_CTL, devctl, &err);
  2215. bus->clkstate = CLK_AVAIL;
  2216. }
  2217. }
  2218. /* Make sure backplane clock is on */
  2219. brcmf_sdio_bus_sleep(bus, false, true);
  2220. /* Pending interrupt indicates new device status */
  2221. if (atomic_read(&bus->ipend) > 0) {
  2222. atomic_set(&bus->ipend, 0);
  2223. err = brcmf_sdio_intr_rstatus(bus);
  2224. }
  2225. /* Start with leftover status bits */
  2226. intstatus = atomic_xchg(&bus->intstatus, 0);
  2227. /* Handle flow-control change: read new state in case our ack
  2228. * crossed another change interrupt. If change still set, assume
  2229. * FC ON for safety, let next loop through do the debounce.
  2230. */
  2231. if (intstatus & I_HMB_FC_CHANGE) {
  2232. intstatus &= ~I_HMB_FC_CHANGE;
  2233. brcmf_sdiod_writel(sdiod, intstat_addr, I_HMB_FC_CHANGE, &err);
  2234. newstatus = brcmf_sdiod_readl(sdiod, intstat_addr, &err);
  2235. bus->sdcnt.f1regdata += 2;
  2236. atomic_set(&bus->fcstate,
  2237. !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
  2238. intstatus |= (newstatus & bus->hostintmask);
  2239. }
  2240. /* Handle host mailbox indication */
  2241. if (intstatus & I_HMB_HOST_INT) {
  2242. intstatus &= ~I_HMB_HOST_INT;
  2243. intstatus |= brcmf_sdio_hostmail(bus);
  2244. }
  2245. sdio_release_host(bus->sdiodev->func1);
  2246. /* Generally don't ask for these, can get CRC errors... */
  2247. if (intstatus & I_WR_OOSYNC) {
  2248. brcmf_err("Dongle reports WR_OOSYNC\n");
  2249. intstatus &= ~I_WR_OOSYNC;
  2250. }
  2251. if (intstatus & I_RD_OOSYNC) {
  2252. brcmf_err("Dongle reports RD_OOSYNC\n");
  2253. intstatus &= ~I_RD_OOSYNC;
  2254. }
  2255. if (intstatus & I_SBINT) {
  2256. brcmf_err("Dongle reports SBINT\n");
  2257. intstatus &= ~I_SBINT;
  2258. }
  2259. /* Would be active due to wake-wlan in gSPI */
  2260. if (intstatus & I_CHIPACTIVE) {
  2261. brcmf_dbg(SDIO, "Dongle reports CHIPACTIVE\n");
  2262. intstatus &= ~I_CHIPACTIVE;
  2263. }
  2264. /* Ignore frame indications if rxskip is set */
  2265. if (bus->rxskip)
  2266. intstatus &= ~I_HMB_FRAME_IND;
  2267. /* On frame indication, read available frames */
  2268. if ((intstatus & I_HMB_FRAME_IND) && (bus->clkstate == CLK_AVAIL)) {
  2269. brcmf_sdio_readframes(bus, bus->rxbound);
  2270. if (!bus->rxpending)
  2271. intstatus &= ~I_HMB_FRAME_IND;
  2272. }
  2273. /* Keep still-pending events for next scheduling */
  2274. if (intstatus)
  2275. atomic_or(intstatus, &bus->intstatus);
  2276. brcmf_sdio_clrintr(bus);
  2277. if (bus->ctrl_frame_stat && (bus->clkstate == CLK_AVAIL) &&
  2278. data_ok(bus)) {
  2279. sdio_claim_host(bus->sdiodev->func1);
  2280. if (bus->ctrl_frame_stat) {
  2281. err = brcmf_sdio_tx_ctrlframe(bus, bus->ctrl_frame_buf,
  2282. bus->ctrl_frame_len);
  2283. bus->ctrl_frame_err = err;
  2284. wmb();
  2285. bus->ctrl_frame_stat = false;
  2286. }
  2287. sdio_release_host(bus->sdiodev->func1);
  2288. brcmf_sdio_wait_event_wakeup(bus);
  2289. }
  2290. /* Send queued frames (limit 1 if rx may still be pending) */
  2291. if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
  2292. brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit &&
  2293. data_ok(bus)) {
  2294. framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
  2295. txlimit;
  2296. brcmf_sdio_sendfromq(bus, framecnt);
  2297. }
  2298. if ((bus->sdiodev->state != BRCMF_SDIOD_DATA) || (err != 0)) {
  2299. brcmf_err("failed backplane access over SDIO, halting operation\n");
  2300. atomic_set(&bus->intstatus, 0);
  2301. if (bus->ctrl_frame_stat) {
  2302. sdio_claim_host(bus->sdiodev->func1);
  2303. if (bus->ctrl_frame_stat) {
  2304. bus->ctrl_frame_err = -ENODEV;
  2305. wmb();
  2306. bus->ctrl_frame_stat = false;
  2307. brcmf_sdio_wait_event_wakeup(bus);
  2308. }
  2309. sdio_release_host(bus->sdiodev->func1);
  2310. }
  2311. } else if (atomic_read(&bus->intstatus) ||
  2312. atomic_read(&bus->ipend) > 0 ||
  2313. (!atomic_read(&bus->fcstate) &&
  2314. brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
  2315. data_ok(bus))) {
  2316. bus->dpc_triggered = true;
  2317. }
  2318. }
  2319. static struct pktq *brcmf_sdio_bus_gettxq(struct device *dev)
  2320. {
  2321. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  2322. struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
  2323. struct brcmf_sdio *bus = sdiodev->bus;
  2324. return &bus->txq;
  2325. }
  2326. static bool brcmf_sdio_prec_enq(struct pktq *q, struct sk_buff *pkt, int prec)
  2327. {
  2328. struct sk_buff *p;
  2329. int eprec = -1; /* precedence to evict from */
  2330. /* Fast case, precedence queue is not full and we are also not
  2331. * exceeding total queue length
  2332. */
  2333. if (!pktq_pfull(q, prec) && !pktq_full(q)) {
  2334. brcmu_pktq_penq(q, prec, pkt);
  2335. return true;
  2336. }
  2337. /* Determine precedence from which to evict packet, if any */
  2338. if (pktq_pfull(q, prec)) {
  2339. eprec = prec;
  2340. } else if (pktq_full(q)) {
  2341. p = brcmu_pktq_peek_tail(q, &eprec);
  2342. if (eprec > prec)
  2343. return false;
  2344. }
  2345. /* Evict if needed */
  2346. if (eprec >= 0) {
  2347. /* Detect queueing to unconfigured precedence */
  2348. if (eprec == prec)
  2349. return false; /* refuse newer (incoming) packet */
  2350. /* Evict packet according to discard policy */
  2351. p = brcmu_pktq_pdeq_tail(q, eprec);
  2352. if (p == NULL)
  2353. brcmf_err("brcmu_pktq_pdeq_tail() failed\n");
  2354. brcmu_pkt_buf_free_skb(p);
  2355. }
  2356. /* Enqueue */
  2357. p = brcmu_pktq_penq(q, prec, pkt);
  2358. if (p == NULL)
  2359. brcmf_err("brcmu_pktq_penq() failed\n");
  2360. return p != NULL;
  2361. }
  2362. static int brcmf_sdio_bus_txdata(struct device *dev, struct sk_buff *pkt)
  2363. {
  2364. int ret = -EBADE;
  2365. uint prec;
  2366. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  2367. struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
  2368. struct brcmf_sdio *bus = sdiodev->bus;
  2369. brcmf_dbg(TRACE, "Enter: pkt: data %p len %d\n", pkt->data, pkt->len);
  2370. if (sdiodev->state != BRCMF_SDIOD_DATA)
  2371. return -EIO;
  2372. /* Add space for the header */
  2373. skb_push(pkt, bus->tx_hdrlen);
  2374. /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
  2375. prec = prio2prec((pkt->priority & PRIOMASK));
  2376. /* Check for existing queue, current flow-control,
  2377. pending event, or pending clock */
  2378. brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
  2379. bus->sdcnt.fcqueued++;
  2380. /* Priority based enq */
  2381. spin_lock_bh(&bus->txq_lock);
  2382. /* reset bus_flags in packet cb */
  2383. *(u16 *)(pkt->cb) = 0;
  2384. if (!brcmf_sdio_prec_enq(&bus->txq, pkt, prec)) {
  2385. skb_pull(pkt, bus->tx_hdrlen);
  2386. brcmf_err("out of bus->txq !!!\n");
  2387. ret = -ENOSR;
  2388. } else {
  2389. ret = 0;
  2390. }
  2391. if (pktq_len(&bus->txq) >= TXHI) {
  2392. bus->txoff = true;
  2393. brcmf_proto_bcdc_txflowblock(dev, true);
  2394. }
  2395. spin_unlock_bh(&bus->txq_lock);
  2396. #ifdef DEBUG
  2397. if (pktq_plen(&bus->txq, prec) > qcount[prec])
  2398. qcount[prec] = pktq_plen(&bus->txq, prec);
  2399. #endif
  2400. brcmf_sdio_trigger_dpc(bus);
  2401. return ret;
  2402. }
  2403. #ifdef DEBUG
  2404. #define CONSOLE_LINE_MAX 192
  2405. static int brcmf_sdio_readconsole(struct brcmf_sdio *bus)
  2406. {
  2407. struct brcmf_console *c = &bus->console;
  2408. u8 line[CONSOLE_LINE_MAX], ch;
  2409. u32 n, idx, addr;
  2410. int rv;
  2411. /* Don't do anything until FWREADY updates console address */
  2412. if (bus->console_addr == 0)
  2413. return 0;
  2414. /* Read console log struct */
  2415. addr = bus->console_addr + offsetof(struct rte_console, log_le);
  2416. rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
  2417. sizeof(c->log_le));
  2418. if (rv < 0)
  2419. return rv;
  2420. /* Allocate console buffer (one time only) */
  2421. if (c->buf == NULL) {
  2422. c->bufsize = le32_to_cpu(c->log_le.buf_size);
  2423. c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
  2424. if (c->buf == NULL)
  2425. return -ENOMEM;
  2426. }
  2427. idx = le32_to_cpu(c->log_le.idx);
  2428. /* Protect against corrupt value */
  2429. if (idx > c->bufsize)
  2430. return -EBADE;
  2431. /* Skip reading the console buffer if the index pointer
  2432. has not moved */
  2433. if (idx == c->last)
  2434. return 0;
  2435. /* Read the console buffer */
  2436. addr = le32_to_cpu(c->log_le.buf);
  2437. rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
  2438. if (rv < 0)
  2439. return rv;
  2440. while (c->last != idx) {
  2441. for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
  2442. if (c->last == idx) {
  2443. /* This would output a partial line.
  2444. * Instead, back up
  2445. * the buffer pointer and output this
  2446. * line next time around.
  2447. */
  2448. if (c->last >= n)
  2449. c->last -= n;
  2450. else
  2451. c->last = c->bufsize - n;
  2452. goto break2;
  2453. }
  2454. ch = c->buf[c->last];
  2455. c->last = (c->last + 1) % c->bufsize;
  2456. if (ch == '\n')
  2457. break;
  2458. line[n] = ch;
  2459. }
  2460. if (n > 0) {
  2461. if (line[n - 1] == '\r')
  2462. n--;
  2463. line[n] = 0;
  2464. pr_debug("CONSOLE: %s\n", line);
  2465. }
  2466. }
  2467. break2:
  2468. return 0;
  2469. }
  2470. #endif /* DEBUG */
  2471. static int
  2472. brcmf_sdio_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
  2473. {
  2474. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  2475. struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
  2476. struct brcmf_sdio *bus = sdiodev->bus;
  2477. int ret;
  2478. brcmf_dbg(TRACE, "Enter\n");
  2479. if (sdiodev->state != BRCMF_SDIOD_DATA)
  2480. return -EIO;
  2481. /* Send from dpc */
  2482. bus->ctrl_frame_buf = msg;
  2483. bus->ctrl_frame_len = msglen;
  2484. wmb();
  2485. bus->ctrl_frame_stat = true;
  2486. brcmf_sdio_trigger_dpc(bus);
  2487. wait_event_interruptible_timeout(bus->ctrl_wait, !bus->ctrl_frame_stat,
  2488. CTL_DONE_TIMEOUT);
  2489. ret = 0;
  2490. if (bus->ctrl_frame_stat) {
  2491. sdio_claim_host(bus->sdiodev->func1);
  2492. if (bus->ctrl_frame_stat) {
  2493. brcmf_dbg(SDIO, "ctrl_frame timeout\n");
  2494. bus->ctrl_frame_stat = false;
  2495. ret = -ETIMEDOUT;
  2496. }
  2497. sdio_release_host(bus->sdiodev->func1);
  2498. }
  2499. if (!ret) {
  2500. brcmf_dbg(SDIO, "ctrl_frame complete, err=%d\n",
  2501. bus->ctrl_frame_err);
  2502. rmb();
  2503. ret = bus->ctrl_frame_err;
  2504. }
  2505. if (ret)
  2506. bus->sdcnt.tx_ctlerrs++;
  2507. else
  2508. bus->sdcnt.tx_ctlpkts++;
  2509. return ret;
  2510. }
  2511. #ifdef DEBUG
  2512. static int brcmf_sdio_dump_console(struct seq_file *seq, struct brcmf_sdio *bus,
  2513. struct sdpcm_shared *sh)
  2514. {
  2515. u32 addr, console_ptr, console_size, console_index;
  2516. char *conbuf = NULL;
  2517. __le32 sh_val;
  2518. int rv;
  2519. /* obtain console information from device memory */
  2520. addr = sh->console_addr + offsetof(struct rte_console, log_le);
  2521. rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
  2522. (u8 *)&sh_val, sizeof(u32));
  2523. if (rv < 0)
  2524. return rv;
  2525. console_ptr = le32_to_cpu(sh_val);
  2526. addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
  2527. rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
  2528. (u8 *)&sh_val, sizeof(u32));
  2529. if (rv < 0)
  2530. return rv;
  2531. console_size = le32_to_cpu(sh_val);
  2532. addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
  2533. rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
  2534. (u8 *)&sh_val, sizeof(u32));
  2535. if (rv < 0)
  2536. return rv;
  2537. console_index = le32_to_cpu(sh_val);
  2538. /* allocate buffer for console data */
  2539. if (console_size <= CONSOLE_BUFFER_MAX)
  2540. conbuf = vzalloc(console_size+1);
  2541. if (!conbuf)
  2542. return -ENOMEM;
  2543. /* obtain the console data from device */
  2544. conbuf[console_size] = '\0';
  2545. rv = brcmf_sdiod_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
  2546. console_size);
  2547. if (rv < 0)
  2548. goto done;
  2549. rv = seq_write(seq, conbuf + console_index,
  2550. console_size - console_index);
  2551. if (rv < 0)
  2552. goto done;
  2553. if (console_index > 0)
  2554. rv = seq_write(seq, conbuf, console_index - 1);
  2555. done:
  2556. vfree(conbuf);
  2557. return rv;
  2558. }
  2559. static int brcmf_sdio_trap_info(struct seq_file *seq, struct brcmf_sdio *bus,
  2560. struct sdpcm_shared *sh)
  2561. {
  2562. int error;
  2563. struct brcmf_trap_info tr;
  2564. if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
  2565. brcmf_dbg(INFO, "no trap in firmware\n");
  2566. return 0;
  2567. }
  2568. error = brcmf_sdiod_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
  2569. sizeof(struct brcmf_trap_info));
  2570. if (error < 0)
  2571. return error;
  2572. seq_printf(seq,
  2573. "dongle trap info: type 0x%x @ epc 0x%08x\n"
  2574. " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
  2575. " lr 0x%08x pc 0x%08x offset 0x%x\n"
  2576. " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
  2577. " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
  2578. le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
  2579. le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
  2580. le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
  2581. le32_to_cpu(tr.pc), sh->trap_addr,
  2582. le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
  2583. le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
  2584. le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
  2585. le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
  2586. return 0;
  2587. }
  2588. static int brcmf_sdio_assert_info(struct seq_file *seq, struct brcmf_sdio *bus,
  2589. struct sdpcm_shared *sh)
  2590. {
  2591. int error = 0;
  2592. char file[80] = "?";
  2593. char expr[80] = "<???>";
  2594. if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
  2595. brcmf_dbg(INFO, "firmware not built with -assert\n");
  2596. return 0;
  2597. } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
  2598. brcmf_dbg(INFO, "no assert in dongle\n");
  2599. return 0;
  2600. }
  2601. sdio_claim_host(bus->sdiodev->func1);
  2602. if (sh->assert_file_addr != 0) {
  2603. error = brcmf_sdiod_ramrw(bus->sdiodev, false,
  2604. sh->assert_file_addr, (u8 *)file, 80);
  2605. if (error < 0)
  2606. return error;
  2607. }
  2608. if (sh->assert_exp_addr != 0) {
  2609. error = brcmf_sdiod_ramrw(bus->sdiodev, false,
  2610. sh->assert_exp_addr, (u8 *)expr, 80);
  2611. if (error < 0)
  2612. return error;
  2613. }
  2614. sdio_release_host(bus->sdiodev->func1);
  2615. seq_printf(seq, "dongle assert: %s:%d: assert(%s)\n",
  2616. file, sh->assert_line, expr);
  2617. return 0;
  2618. }
  2619. static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
  2620. {
  2621. int error;
  2622. struct sdpcm_shared sh;
  2623. error = brcmf_sdio_readshared(bus, &sh);
  2624. if (error < 0)
  2625. return error;
  2626. if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
  2627. brcmf_dbg(INFO, "firmware not built with -assert\n");
  2628. else if (sh.flags & SDPCM_SHARED_ASSERT)
  2629. brcmf_err("assertion in dongle\n");
  2630. if (sh.flags & SDPCM_SHARED_TRAP)
  2631. brcmf_err("firmware trap in dongle\n");
  2632. return 0;
  2633. }
  2634. static int brcmf_sdio_died_dump(struct seq_file *seq, struct brcmf_sdio *bus)
  2635. {
  2636. int error = 0;
  2637. struct sdpcm_shared sh;
  2638. error = brcmf_sdio_readshared(bus, &sh);
  2639. if (error < 0)
  2640. goto done;
  2641. error = brcmf_sdio_assert_info(seq, bus, &sh);
  2642. if (error < 0)
  2643. goto done;
  2644. error = brcmf_sdio_trap_info(seq, bus, &sh);
  2645. if (error < 0)
  2646. goto done;
  2647. error = brcmf_sdio_dump_console(seq, bus, &sh);
  2648. done:
  2649. return error;
  2650. }
  2651. static int brcmf_sdio_forensic_read(struct seq_file *seq, void *data)
  2652. {
  2653. struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
  2654. struct brcmf_sdio *bus = bus_if->bus_priv.sdio->bus;
  2655. return brcmf_sdio_died_dump(seq, bus);
  2656. }
  2657. static int brcmf_debugfs_sdio_count_read(struct seq_file *seq, void *data)
  2658. {
  2659. struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
  2660. struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
  2661. struct brcmf_sdio_count *sdcnt = &sdiodev->bus->sdcnt;
  2662. seq_printf(seq,
  2663. "intrcount: %u\nlastintrs: %u\n"
  2664. "pollcnt: %u\nregfails: %u\n"
  2665. "tx_sderrs: %u\nfcqueued: %u\n"
  2666. "rxrtx: %u\nrx_toolong: %u\n"
  2667. "rxc_errors: %u\nrx_hdrfail: %u\n"
  2668. "rx_badhdr: %u\nrx_badseq: %u\n"
  2669. "fc_rcvd: %u\nfc_xoff: %u\n"
  2670. "fc_xon: %u\nrxglomfail: %u\n"
  2671. "rxglomframes: %u\nrxglompkts: %u\n"
  2672. "f2rxhdrs: %u\nf2rxdata: %u\n"
  2673. "f2txdata: %u\nf1regdata: %u\n"
  2674. "tickcnt: %u\ntx_ctlerrs: %lu\n"
  2675. "tx_ctlpkts: %lu\nrx_ctlerrs: %lu\n"
  2676. "rx_ctlpkts: %lu\nrx_readahead: %lu\n",
  2677. sdcnt->intrcount, sdcnt->lastintrs,
  2678. sdcnt->pollcnt, sdcnt->regfails,
  2679. sdcnt->tx_sderrs, sdcnt->fcqueued,
  2680. sdcnt->rxrtx, sdcnt->rx_toolong,
  2681. sdcnt->rxc_errors, sdcnt->rx_hdrfail,
  2682. sdcnt->rx_badhdr, sdcnt->rx_badseq,
  2683. sdcnt->fc_rcvd, sdcnt->fc_xoff,
  2684. sdcnt->fc_xon, sdcnt->rxglomfail,
  2685. sdcnt->rxglomframes, sdcnt->rxglompkts,
  2686. sdcnt->f2rxhdrs, sdcnt->f2rxdata,
  2687. sdcnt->f2txdata, sdcnt->f1regdata,
  2688. sdcnt->tickcnt, sdcnt->tx_ctlerrs,
  2689. sdcnt->tx_ctlpkts, sdcnt->rx_ctlerrs,
  2690. sdcnt->rx_ctlpkts, sdcnt->rx_readahead_cnt);
  2691. return 0;
  2692. }
  2693. static void brcmf_sdio_debugfs_create(struct device *dev)
  2694. {
  2695. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  2696. struct brcmf_pub *drvr = bus_if->drvr;
  2697. struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
  2698. struct brcmf_sdio *bus = sdiodev->bus;
  2699. struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
  2700. if (IS_ERR_OR_NULL(dentry))
  2701. return;
  2702. bus->console_interval = BRCMF_CONSOLE;
  2703. brcmf_debugfs_add_entry(drvr, "forensics", brcmf_sdio_forensic_read);
  2704. brcmf_debugfs_add_entry(drvr, "counters",
  2705. brcmf_debugfs_sdio_count_read);
  2706. debugfs_create_u32("console_interval", 0644, dentry,
  2707. &bus->console_interval);
  2708. }
  2709. #else
  2710. static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
  2711. {
  2712. return 0;
  2713. }
  2714. static void brcmf_sdio_debugfs_create(struct device *dev)
  2715. {
  2716. }
  2717. #endif /* DEBUG */
  2718. static int
  2719. brcmf_sdio_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
  2720. {
  2721. int timeleft;
  2722. uint rxlen = 0;
  2723. bool pending;
  2724. u8 *buf;
  2725. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  2726. struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
  2727. struct brcmf_sdio *bus = sdiodev->bus;
  2728. brcmf_dbg(TRACE, "Enter\n");
  2729. if (sdiodev->state != BRCMF_SDIOD_DATA)
  2730. return -EIO;
  2731. /* Wait until control frame is available */
  2732. timeleft = brcmf_sdio_dcmd_resp_wait(bus, &bus->rxlen, &pending);
  2733. spin_lock_bh(&bus->rxctl_lock);
  2734. rxlen = bus->rxlen;
  2735. memcpy(msg, bus->rxctl, min(msglen, rxlen));
  2736. bus->rxctl = NULL;
  2737. buf = bus->rxctl_orig;
  2738. bus->rxctl_orig = NULL;
  2739. bus->rxlen = 0;
  2740. spin_unlock_bh(&bus->rxctl_lock);
  2741. vfree(buf);
  2742. if (rxlen) {
  2743. brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
  2744. rxlen, msglen);
  2745. } else if (timeleft == 0) {
  2746. brcmf_err("resumed on timeout\n");
  2747. brcmf_sdio_checkdied(bus);
  2748. } else if (pending) {
  2749. brcmf_dbg(CTL, "cancelled\n");
  2750. return -ERESTARTSYS;
  2751. } else {
  2752. brcmf_dbg(CTL, "resumed for unknown reason?\n");
  2753. brcmf_sdio_checkdied(bus);
  2754. }
  2755. if (rxlen)
  2756. bus->sdcnt.rx_ctlpkts++;
  2757. else
  2758. bus->sdcnt.rx_ctlerrs++;
  2759. return rxlen ? (int)rxlen : -ETIMEDOUT;
  2760. }
  2761. #ifdef DEBUG
  2762. static bool
  2763. brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
  2764. u8 *ram_data, uint ram_sz)
  2765. {
  2766. char *ram_cmp;
  2767. int err;
  2768. bool ret = true;
  2769. int address;
  2770. int offset;
  2771. int len;
  2772. /* read back and verify */
  2773. brcmf_dbg(INFO, "Compare RAM dl & ul at 0x%08x; size=%d\n", ram_addr,
  2774. ram_sz);
  2775. ram_cmp = kmalloc(MEMBLOCK, GFP_KERNEL);
  2776. /* do not proceed while no memory but */
  2777. if (!ram_cmp)
  2778. return true;
  2779. address = ram_addr;
  2780. offset = 0;
  2781. while (offset < ram_sz) {
  2782. len = ((offset + MEMBLOCK) < ram_sz) ? MEMBLOCK :
  2783. ram_sz - offset;
  2784. err = brcmf_sdiod_ramrw(sdiodev, false, address, ram_cmp, len);
  2785. if (err) {
  2786. brcmf_err("error %d on reading %d membytes at 0x%08x\n",
  2787. err, len, address);
  2788. ret = false;
  2789. break;
  2790. } else if (memcmp(ram_cmp, &ram_data[offset], len)) {
  2791. brcmf_err("Downloaded RAM image is corrupted, block offset is %d, len is %d\n",
  2792. offset, len);
  2793. ret = false;
  2794. break;
  2795. }
  2796. offset += len;
  2797. address += len;
  2798. }
  2799. kfree(ram_cmp);
  2800. return ret;
  2801. }
  2802. #else /* DEBUG */
  2803. static bool
  2804. brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
  2805. u8 *ram_data, uint ram_sz)
  2806. {
  2807. return true;
  2808. }
  2809. #endif /* DEBUG */
  2810. static int brcmf_sdio_download_code_file(struct brcmf_sdio *bus,
  2811. const struct firmware *fw)
  2812. {
  2813. int err;
  2814. brcmf_dbg(TRACE, "Enter\n");
  2815. err = brcmf_sdiod_ramrw(bus->sdiodev, true, bus->ci->rambase,
  2816. (u8 *)fw->data, fw->size);
  2817. if (err)
  2818. brcmf_err("error %d on writing %d membytes at 0x%08x\n",
  2819. err, (int)fw->size, bus->ci->rambase);
  2820. else if (!brcmf_sdio_verifymemory(bus->sdiodev, bus->ci->rambase,
  2821. (u8 *)fw->data, fw->size))
  2822. err = -EIO;
  2823. return err;
  2824. }
  2825. static int brcmf_sdio_download_nvram(struct brcmf_sdio *bus,
  2826. void *vars, u32 varsz)
  2827. {
  2828. int address;
  2829. int err;
  2830. brcmf_dbg(TRACE, "Enter\n");
  2831. address = bus->ci->ramsize - varsz + bus->ci->rambase;
  2832. err = brcmf_sdiod_ramrw(bus->sdiodev, true, address, vars, varsz);
  2833. if (err)
  2834. brcmf_err("error %d on writing %d nvram bytes at 0x%08x\n",
  2835. err, varsz, address);
  2836. else if (!brcmf_sdio_verifymemory(bus->sdiodev, address, vars, varsz))
  2837. err = -EIO;
  2838. return err;
  2839. }
  2840. static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus,
  2841. const struct firmware *fw,
  2842. void *nvram, u32 nvlen)
  2843. {
  2844. int bcmerror;
  2845. u32 rstvec;
  2846. sdio_claim_host(bus->sdiodev->func1);
  2847. brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
  2848. rstvec = get_unaligned_le32(fw->data);
  2849. brcmf_dbg(SDIO, "firmware rstvec: %x\n", rstvec);
  2850. bcmerror = brcmf_sdio_download_code_file(bus, fw);
  2851. release_firmware(fw);
  2852. if (bcmerror) {
  2853. brcmf_err("dongle image file download failed\n");
  2854. brcmf_fw_nvram_free(nvram);
  2855. goto err;
  2856. }
  2857. bcmerror = brcmf_sdio_download_nvram(bus, nvram, nvlen);
  2858. brcmf_fw_nvram_free(nvram);
  2859. if (bcmerror) {
  2860. brcmf_err("dongle nvram file download failed\n");
  2861. goto err;
  2862. }
  2863. /* Take arm out of reset */
  2864. if (!brcmf_chip_set_active(bus->ci, rstvec)) {
  2865. brcmf_err("error getting out of ARM core reset\n");
  2866. goto err;
  2867. }
  2868. err:
  2869. brcmf_sdio_clkctl(bus, CLK_SDONLY, false);
  2870. sdio_release_host(bus->sdiodev->func1);
  2871. return bcmerror;
  2872. }
  2873. static void brcmf_sdio_sr_init(struct brcmf_sdio *bus)
  2874. {
  2875. int err = 0;
  2876. u8 val;
  2877. brcmf_dbg(TRACE, "Enter\n");
  2878. val = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, &err);
  2879. if (err) {
  2880. brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
  2881. return;
  2882. }
  2883. val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
  2884. brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, val, &err);
  2885. if (err) {
  2886. brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
  2887. return;
  2888. }
  2889. /* Add CMD14 Support */
  2890. brcmf_sdiod_func0_wb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
  2891. (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
  2892. SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
  2893. &err);
  2894. if (err) {
  2895. brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
  2896. return;
  2897. }
  2898. brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
  2899. SBSDIO_FORCE_HT, &err);
  2900. if (err) {
  2901. brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
  2902. return;
  2903. }
  2904. /* set flag */
  2905. bus->sr_enabled = true;
  2906. brcmf_dbg(INFO, "SR enabled\n");
  2907. }
  2908. /* enable KSO bit */
  2909. static int brcmf_sdio_kso_init(struct brcmf_sdio *bus)
  2910. {
  2911. struct brcmf_core *core = bus->sdio_core;
  2912. u8 val;
  2913. int err = 0;
  2914. brcmf_dbg(TRACE, "Enter\n");
  2915. /* KSO bit added in SDIO core rev 12 */
  2916. if (core->rev < 12)
  2917. return 0;
  2918. val = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, &err);
  2919. if (err) {
  2920. brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
  2921. return err;
  2922. }
  2923. if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
  2924. val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
  2925. SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
  2926. brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
  2927. val, &err);
  2928. if (err) {
  2929. brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
  2930. return err;
  2931. }
  2932. }
  2933. return 0;
  2934. }
  2935. static int brcmf_sdio_bus_preinit(struct device *dev)
  2936. {
  2937. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  2938. struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
  2939. struct brcmf_sdio *bus = sdiodev->bus;
  2940. struct brcmf_core *core = bus->sdio_core;
  2941. uint pad_size;
  2942. u32 value;
  2943. int err;
  2944. /* maxctl provided by common layer */
  2945. if (WARN_ON(!bus_if->maxctl))
  2946. return -EINVAL;
  2947. /* Allocate control receive buffer */
  2948. bus_if->maxctl += bus->roundup;
  2949. value = roundup((bus_if->maxctl + SDPCM_HDRLEN), ALIGNMENT);
  2950. value += bus->head_align;
  2951. bus->rxbuf = kmalloc(value, GFP_ATOMIC);
  2952. if (bus->rxbuf)
  2953. bus->rxblen = value;
  2954. /* the commands below use the terms tx and rx from
  2955. * a device perspective, ie. bus:txglom affects the
  2956. * bus transfers from device to host.
  2957. */
  2958. if (core->rev < 12) {
  2959. /* for sdio core rev < 12, disable txgloming */
  2960. value = 0;
  2961. err = brcmf_iovar_data_set(dev, "bus:txglom", &value,
  2962. sizeof(u32));
  2963. } else {
  2964. /* otherwise, set txglomalign */
  2965. value = sdiodev->settings->bus.sdio.sd_sgentry_align;
  2966. /* SDIO ADMA requires at least 32 bit alignment */
  2967. value = max_t(u32, value, ALIGNMENT);
  2968. err = brcmf_iovar_data_set(dev, "bus:txglomalign", &value,
  2969. sizeof(u32));
  2970. }
  2971. if (err < 0)
  2972. goto done;
  2973. bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
  2974. if (sdiodev->sg_support) {
  2975. bus->txglom = false;
  2976. value = 1;
  2977. pad_size = bus->sdiodev->func2->cur_blksize << 1;
  2978. err = brcmf_iovar_data_set(bus->sdiodev->dev, "bus:rxglom",
  2979. &value, sizeof(u32));
  2980. if (err < 0) {
  2981. /* bus:rxglom is allowed to fail */
  2982. err = 0;
  2983. } else {
  2984. bus->txglom = true;
  2985. bus->tx_hdrlen += SDPCM_HWEXT_LEN;
  2986. }
  2987. }
  2988. brcmf_bus_add_txhdrlen(bus->sdiodev->dev, bus->tx_hdrlen);
  2989. done:
  2990. return err;
  2991. }
  2992. static size_t brcmf_sdio_bus_get_ramsize(struct device *dev)
  2993. {
  2994. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  2995. struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
  2996. struct brcmf_sdio *bus = sdiodev->bus;
  2997. return bus->ci->ramsize - bus->ci->srsize;
  2998. }
  2999. static int brcmf_sdio_bus_get_memdump(struct device *dev, void *data,
  3000. size_t mem_size)
  3001. {
  3002. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  3003. struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
  3004. struct brcmf_sdio *bus = sdiodev->bus;
  3005. int err;
  3006. int address;
  3007. int offset;
  3008. int len;
  3009. brcmf_dbg(INFO, "dump at 0x%08x: size=%zu\n", bus->ci->rambase,
  3010. mem_size);
  3011. address = bus->ci->rambase;
  3012. offset = err = 0;
  3013. sdio_claim_host(sdiodev->func1);
  3014. while (offset < mem_size) {
  3015. len = ((offset + MEMBLOCK) < mem_size) ? MEMBLOCK :
  3016. mem_size - offset;
  3017. err = brcmf_sdiod_ramrw(sdiodev, false, address, data, len);
  3018. if (err) {
  3019. brcmf_err("error %d on reading %d membytes at 0x%08x\n",
  3020. err, len, address);
  3021. goto done;
  3022. }
  3023. data += len;
  3024. offset += len;
  3025. address += len;
  3026. }
  3027. done:
  3028. sdio_release_host(sdiodev->func1);
  3029. return err;
  3030. }
  3031. void brcmf_sdio_trigger_dpc(struct brcmf_sdio *bus)
  3032. {
  3033. if (!bus->dpc_triggered) {
  3034. bus->dpc_triggered = true;
  3035. queue_work(bus->brcmf_wq, &bus->datawork);
  3036. }
  3037. }
  3038. void brcmf_sdio_isr(struct brcmf_sdio *bus)
  3039. {
  3040. brcmf_dbg(TRACE, "Enter\n");
  3041. if (!bus) {
  3042. brcmf_err("bus is null pointer, exiting\n");
  3043. return;
  3044. }
  3045. /* Count the interrupt call */
  3046. bus->sdcnt.intrcount++;
  3047. if (in_interrupt())
  3048. atomic_set(&bus->ipend, 1);
  3049. else
  3050. if (brcmf_sdio_intr_rstatus(bus)) {
  3051. brcmf_err("failed backplane access\n");
  3052. }
  3053. /* Disable additional interrupts (is this needed now)? */
  3054. if (!bus->intr)
  3055. brcmf_err("isr w/o interrupt configured!\n");
  3056. bus->dpc_triggered = true;
  3057. queue_work(bus->brcmf_wq, &bus->datawork);
  3058. }
  3059. static void brcmf_sdio_bus_watchdog(struct brcmf_sdio *bus)
  3060. {
  3061. brcmf_dbg(TIMER, "Enter\n");
  3062. /* Poll period: check device if appropriate. */
  3063. if (!bus->sr_enabled &&
  3064. bus->poll && (++bus->polltick >= bus->pollrate)) {
  3065. u32 intstatus = 0;
  3066. /* Reset poll tick */
  3067. bus->polltick = 0;
  3068. /* Check device if no interrupts */
  3069. if (!bus->intr ||
  3070. (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
  3071. if (!bus->dpc_triggered) {
  3072. u8 devpend;
  3073. sdio_claim_host(bus->sdiodev->func1);
  3074. devpend = brcmf_sdiod_func0_rb(bus->sdiodev,
  3075. SDIO_CCCR_INTx, NULL);
  3076. sdio_release_host(bus->sdiodev->func1);
  3077. intstatus = devpend & (INTR_STATUS_FUNC1 |
  3078. INTR_STATUS_FUNC2);
  3079. }
  3080. /* If there is something, make like the ISR and
  3081. schedule the DPC */
  3082. if (intstatus) {
  3083. bus->sdcnt.pollcnt++;
  3084. atomic_set(&bus->ipend, 1);
  3085. bus->dpc_triggered = true;
  3086. queue_work(bus->brcmf_wq, &bus->datawork);
  3087. }
  3088. }
  3089. /* Update interrupt tracking */
  3090. bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
  3091. }
  3092. #ifdef DEBUG
  3093. /* Poll for console output periodically */
  3094. if (bus->sdiodev->state == BRCMF_SDIOD_DATA && BRCMF_FWCON_ON() &&
  3095. bus->console_interval != 0) {
  3096. bus->console.count += jiffies_to_msecs(BRCMF_WD_POLL);
  3097. if (bus->console.count >= bus->console_interval) {
  3098. bus->console.count -= bus->console_interval;
  3099. sdio_claim_host(bus->sdiodev->func1);
  3100. /* Make sure backplane clock is on */
  3101. brcmf_sdio_bus_sleep(bus, false, false);
  3102. if (brcmf_sdio_readconsole(bus) < 0)
  3103. /* stop on error */
  3104. bus->console_interval = 0;
  3105. sdio_release_host(bus->sdiodev->func1);
  3106. }
  3107. }
  3108. #endif /* DEBUG */
  3109. /* On idle timeout clear activity flag and/or turn off clock */
  3110. if (!bus->dpc_triggered) {
  3111. rmb();
  3112. if ((!bus->dpc_running) && (bus->idletime > 0) &&
  3113. (bus->clkstate == CLK_AVAIL)) {
  3114. bus->idlecount++;
  3115. if (bus->idlecount > bus->idletime) {
  3116. brcmf_dbg(SDIO, "idle\n");
  3117. sdio_claim_host(bus->sdiodev->func1);
  3118. brcmf_sdio_wd_timer(bus, false);
  3119. bus->idlecount = 0;
  3120. brcmf_sdio_bus_sleep(bus, true, false);
  3121. sdio_release_host(bus->sdiodev->func1);
  3122. }
  3123. } else {
  3124. bus->idlecount = 0;
  3125. }
  3126. } else {
  3127. bus->idlecount = 0;
  3128. }
  3129. }
  3130. static void brcmf_sdio_dataworker(struct work_struct *work)
  3131. {
  3132. struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
  3133. datawork);
  3134. bus->dpc_running = true;
  3135. wmb();
  3136. while (READ_ONCE(bus->dpc_triggered)) {
  3137. bus->dpc_triggered = false;
  3138. brcmf_sdio_dpc(bus);
  3139. bus->idlecount = 0;
  3140. }
  3141. bus->dpc_running = false;
  3142. if (brcmf_sdiod_freezing(bus->sdiodev)) {
  3143. brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DOWN);
  3144. brcmf_sdiod_try_freeze(bus->sdiodev);
  3145. brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
  3146. }
  3147. }
  3148. static void
  3149. brcmf_sdio_drivestrengthinit(struct brcmf_sdio_dev *sdiodev,
  3150. struct brcmf_chip *ci, u32 drivestrength)
  3151. {
  3152. const struct sdiod_drive_str *str_tab = NULL;
  3153. u32 str_mask;
  3154. u32 str_shift;
  3155. u32 i;
  3156. u32 drivestrength_sel = 0;
  3157. u32 cc_data_temp;
  3158. u32 addr;
  3159. if (!(ci->cc_caps & CC_CAP_PMU))
  3160. return;
  3161. switch (SDIOD_DRVSTR_KEY(ci->chip, ci->pmurev)) {
  3162. case SDIOD_DRVSTR_KEY(BRCM_CC_4330_CHIP_ID, 12):
  3163. str_tab = sdiod_drvstr_tab1_1v8;
  3164. str_mask = 0x00003800;
  3165. str_shift = 11;
  3166. break;
  3167. case SDIOD_DRVSTR_KEY(BRCM_CC_4334_CHIP_ID, 17):
  3168. str_tab = sdiod_drvstr_tab6_1v8;
  3169. str_mask = 0x00001800;
  3170. str_shift = 11;
  3171. break;
  3172. case SDIOD_DRVSTR_KEY(BRCM_CC_43143_CHIP_ID, 17):
  3173. /* note: 43143 does not support tristate */
  3174. i = ARRAY_SIZE(sdiod_drvstr_tab2_3v3) - 1;
  3175. if (drivestrength >= sdiod_drvstr_tab2_3v3[i].strength) {
  3176. str_tab = sdiod_drvstr_tab2_3v3;
  3177. str_mask = 0x00000007;
  3178. str_shift = 0;
  3179. } else
  3180. brcmf_err("Invalid SDIO Drive strength for chip %s, strength=%d\n",
  3181. ci->name, drivestrength);
  3182. break;
  3183. case SDIOD_DRVSTR_KEY(BRCM_CC_43362_CHIP_ID, 13):
  3184. str_tab = sdiod_drive_strength_tab5_1v8;
  3185. str_mask = 0x00003800;
  3186. str_shift = 11;
  3187. break;
  3188. default:
  3189. brcmf_dbg(INFO, "No SDIO driver strength init needed for chip %s rev %d pmurev %d\n",
  3190. ci->name, ci->chiprev, ci->pmurev);
  3191. break;
  3192. }
  3193. if (str_tab != NULL) {
  3194. struct brcmf_core *pmu = brcmf_chip_get_pmu(ci);
  3195. for (i = 0; str_tab[i].strength != 0; i++) {
  3196. if (drivestrength >= str_tab[i].strength) {
  3197. drivestrength_sel = str_tab[i].sel;
  3198. break;
  3199. }
  3200. }
  3201. addr = CORE_CC_REG(pmu->base, chipcontrol_addr);
  3202. brcmf_sdiod_writel(sdiodev, addr, 1, NULL);
  3203. cc_data_temp = brcmf_sdiod_readl(sdiodev, addr, NULL);
  3204. cc_data_temp &= ~str_mask;
  3205. drivestrength_sel <<= str_shift;
  3206. cc_data_temp |= drivestrength_sel;
  3207. brcmf_sdiod_writel(sdiodev, addr, cc_data_temp, NULL);
  3208. brcmf_dbg(INFO, "SDIO: %d mA (req=%d mA) drive strength selected, set to 0x%08x\n",
  3209. str_tab[i].strength, drivestrength, cc_data_temp);
  3210. }
  3211. }
  3212. static int brcmf_sdio_buscoreprep(void *ctx)
  3213. {
  3214. struct brcmf_sdio_dev *sdiodev = ctx;
  3215. int err = 0;
  3216. u8 clkval, clkset;
  3217. /* Try forcing SDIO core to do ALPAvail request only */
  3218. clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
  3219. brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
  3220. if (err) {
  3221. brcmf_err("error writing for HT off\n");
  3222. return err;
  3223. }
  3224. /* If register supported, wait for ALPAvail and then force ALP */
  3225. /* This may take up to 15 milliseconds */
  3226. clkval = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, NULL);
  3227. if ((clkval & ~SBSDIO_AVBITS) != clkset) {
  3228. brcmf_err("ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
  3229. clkset, clkval);
  3230. return -EACCES;
  3231. }
  3232. SPINWAIT(((clkval = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
  3233. NULL)),
  3234. !SBSDIO_ALPAV(clkval)),
  3235. PMU_MAX_TRANSITION_DLY);
  3236. if (!SBSDIO_ALPAV(clkval)) {
  3237. brcmf_err("timeout on ALPAV wait, clkval 0x%02x\n",
  3238. clkval);
  3239. return -EBUSY;
  3240. }
  3241. clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP;
  3242. brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
  3243. udelay(65);
  3244. /* Also, disable the extra SDIO pull-ups */
  3245. brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_SDIOPULLUP, 0, NULL);
  3246. return 0;
  3247. }
  3248. static void brcmf_sdio_buscore_activate(void *ctx, struct brcmf_chip *chip,
  3249. u32 rstvec)
  3250. {
  3251. struct brcmf_sdio_dev *sdiodev = ctx;
  3252. struct brcmf_core *core = sdiodev->bus->sdio_core;
  3253. u32 reg_addr;
  3254. /* clear all interrupts */
  3255. reg_addr = core->base + SD_REG(intstatus);
  3256. brcmf_sdiod_writel(sdiodev, reg_addr, 0xFFFFFFFF, NULL);
  3257. if (rstvec)
  3258. /* Write reset vector to address 0 */
  3259. brcmf_sdiod_ramrw(sdiodev, true, 0, (void *)&rstvec,
  3260. sizeof(rstvec));
  3261. }
  3262. static u32 brcmf_sdio_buscore_read32(void *ctx, u32 addr)
  3263. {
  3264. struct brcmf_sdio_dev *sdiodev = ctx;
  3265. u32 val, rev;
  3266. val = brcmf_sdiod_readl(sdiodev, addr, NULL);
  3267. /*
  3268. * this is a bit of special handling if reading the chipcommon chipid
  3269. * register. The 4339 is a next-gen of the 4335. It uses the same
  3270. * SDIO device id as 4335 and the chipid register returns 4335 as well.
  3271. * It can be identified as 4339 by looking at the chip revision. It
  3272. * is corrected here so the chip.c module has the right info.
  3273. */
  3274. if (addr == CORE_CC_REG(SI_ENUM_BASE, chipid) &&
  3275. (sdiodev->func1->device == SDIO_DEVICE_ID_BROADCOM_4339 ||
  3276. sdiodev->func1->device == SDIO_DEVICE_ID_BROADCOM_4335_4339)) {
  3277. rev = (val & CID_REV_MASK) >> CID_REV_SHIFT;
  3278. if (rev >= 2) {
  3279. val &= ~CID_ID_MASK;
  3280. val |= BRCM_CC_4339_CHIP_ID;
  3281. }
  3282. }
  3283. return val;
  3284. }
  3285. static void brcmf_sdio_buscore_write32(void *ctx, u32 addr, u32 val)
  3286. {
  3287. struct brcmf_sdio_dev *sdiodev = ctx;
  3288. brcmf_sdiod_writel(sdiodev, addr, val, NULL);
  3289. }
  3290. static const struct brcmf_buscore_ops brcmf_sdio_buscore_ops = {
  3291. .prepare = brcmf_sdio_buscoreprep,
  3292. .activate = brcmf_sdio_buscore_activate,
  3293. .read32 = brcmf_sdio_buscore_read32,
  3294. .write32 = brcmf_sdio_buscore_write32,
  3295. };
  3296. static bool
  3297. brcmf_sdio_probe_attach(struct brcmf_sdio *bus)
  3298. {
  3299. struct brcmf_sdio_dev *sdiodev;
  3300. u8 clkctl = 0;
  3301. int err = 0;
  3302. int reg_addr;
  3303. u32 reg_val;
  3304. u32 drivestrength;
  3305. sdiodev = bus->sdiodev;
  3306. sdio_claim_host(sdiodev->func1);
  3307. pr_debug("F1 signature read @0x18000000=0x%4x\n",
  3308. brcmf_sdiod_readl(sdiodev, SI_ENUM_BASE, NULL));
  3309. /*
  3310. * Force PLL off until brcmf_chip_attach()
  3311. * programs PLL control regs
  3312. */
  3313. brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, BRCMF_INIT_CLKCTL1,
  3314. &err);
  3315. if (!err)
  3316. clkctl = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
  3317. &err);
  3318. if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
  3319. brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
  3320. err, BRCMF_INIT_CLKCTL1, clkctl);
  3321. goto fail;
  3322. }
  3323. bus->ci = brcmf_chip_attach(sdiodev, &brcmf_sdio_buscore_ops);
  3324. if (IS_ERR(bus->ci)) {
  3325. brcmf_err("brcmf_chip_attach failed!\n");
  3326. bus->ci = NULL;
  3327. goto fail;
  3328. }
  3329. /* Pick up the SDIO core info struct from chip.c */
  3330. bus->sdio_core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
  3331. if (!bus->sdio_core)
  3332. goto fail;
  3333. /* Pick up the CHIPCOMMON core info struct, for bulk IO in bcmsdh.c */
  3334. sdiodev->cc_core = brcmf_chip_get_core(bus->ci, BCMA_CORE_CHIPCOMMON);
  3335. if (!sdiodev->cc_core)
  3336. goto fail;
  3337. sdiodev->settings = brcmf_get_module_param(sdiodev->dev,
  3338. BRCMF_BUSTYPE_SDIO,
  3339. bus->ci->chip,
  3340. bus->ci->chiprev);
  3341. if (!sdiodev->settings) {
  3342. brcmf_err("Failed to get device parameters\n");
  3343. goto fail;
  3344. }
  3345. /* platform specific configuration:
  3346. * alignments must be at least 4 bytes for ADMA
  3347. */
  3348. bus->head_align = ALIGNMENT;
  3349. bus->sgentry_align = ALIGNMENT;
  3350. if (sdiodev->settings->bus.sdio.sd_head_align > ALIGNMENT)
  3351. bus->head_align = sdiodev->settings->bus.sdio.sd_head_align;
  3352. if (sdiodev->settings->bus.sdio.sd_sgentry_align > ALIGNMENT)
  3353. bus->sgentry_align =
  3354. sdiodev->settings->bus.sdio.sd_sgentry_align;
  3355. /* allocate scatter-gather table. sg support
  3356. * will be disabled upon allocation failure.
  3357. */
  3358. brcmf_sdiod_sgtable_alloc(sdiodev);
  3359. #ifdef CONFIG_PM_SLEEP
  3360. /* wowl can be supported when KEEP_POWER is true and (WAKE_SDIO_IRQ
  3361. * is true or when platform data OOB irq is true).
  3362. */
  3363. if ((sdio_get_host_pm_caps(sdiodev->func1) & MMC_PM_KEEP_POWER) &&
  3364. ((sdio_get_host_pm_caps(sdiodev->func1) & MMC_PM_WAKE_SDIO_IRQ) ||
  3365. (sdiodev->settings->bus.sdio.oob_irq_supported)))
  3366. sdiodev->bus_if->wowl_supported = true;
  3367. #endif
  3368. if (brcmf_sdio_kso_init(bus)) {
  3369. brcmf_err("error enabling KSO\n");
  3370. goto fail;
  3371. }
  3372. if (sdiodev->settings->bus.sdio.drive_strength)
  3373. drivestrength = sdiodev->settings->bus.sdio.drive_strength;
  3374. else
  3375. drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
  3376. brcmf_sdio_drivestrengthinit(sdiodev, bus->ci, drivestrength);
  3377. /* Set card control so an SDIO card reset does a WLAN backplane reset */
  3378. reg_val = brcmf_sdiod_func0_rb(sdiodev, SDIO_CCCR_BRCM_CARDCTRL, &err);
  3379. if (err)
  3380. goto fail;
  3381. reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
  3382. brcmf_sdiod_func0_wb(sdiodev, SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
  3383. if (err)
  3384. goto fail;
  3385. /* set PMUControl so a backplane reset does PMU state reload */
  3386. reg_addr = CORE_CC_REG(brcmf_chip_get_pmu(bus->ci)->base, pmucontrol);
  3387. reg_val = brcmf_sdiod_readl(sdiodev, reg_addr, &err);
  3388. if (err)
  3389. goto fail;
  3390. reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
  3391. brcmf_sdiod_writel(sdiodev, reg_addr, reg_val, &err);
  3392. if (err)
  3393. goto fail;
  3394. sdio_release_host(sdiodev->func1);
  3395. brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
  3396. /* allocate header buffer */
  3397. bus->hdrbuf = kzalloc(MAX_HDR_READ + bus->head_align, GFP_KERNEL);
  3398. if (!bus->hdrbuf)
  3399. return false;
  3400. /* Locate an appropriately-aligned portion of hdrbuf */
  3401. bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
  3402. bus->head_align);
  3403. /* Set the poll and/or interrupt flags */
  3404. bus->intr = true;
  3405. bus->poll = false;
  3406. if (bus->poll)
  3407. bus->pollrate = 1;
  3408. return true;
  3409. fail:
  3410. sdio_release_host(sdiodev->func1);
  3411. return false;
  3412. }
  3413. static int
  3414. brcmf_sdio_watchdog_thread(void *data)
  3415. {
  3416. struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
  3417. int wait;
  3418. allow_signal(SIGTERM);
  3419. /* Run until signal received */
  3420. brcmf_sdiod_freezer_count(bus->sdiodev);
  3421. while (1) {
  3422. if (kthread_should_stop())
  3423. break;
  3424. brcmf_sdiod_freezer_uncount(bus->sdiodev);
  3425. wait = wait_for_completion_interruptible(&bus->watchdog_wait);
  3426. brcmf_sdiod_freezer_count(bus->sdiodev);
  3427. brcmf_sdiod_try_freeze(bus->sdiodev);
  3428. if (!wait) {
  3429. brcmf_sdio_bus_watchdog(bus);
  3430. /* Count the tick for reference */
  3431. bus->sdcnt.tickcnt++;
  3432. reinit_completion(&bus->watchdog_wait);
  3433. } else
  3434. break;
  3435. }
  3436. return 0;
  3437. }
  3438. static void
  3439. brcmf_sdio_watchdog(struct timer_list *t)
  3440. {
  3441. struct brcmf_sdio *bus = from_timer(bus, t, timer);
  3442. if (bus->watchdog_tsk) {
  3443. complete(&bus->watchdog_wait);
  3444. /* Reschedule the watchdog */
  3445. if (bus->wd_active)
  3446. mod_timer(&bus->timer,
  3447. jiffies + BRCMF_WD_POLL);
  3448. }
  3449. }
  3450. static
  3451. int brcmf_sdio_get_fwname(struct device *dev, const char *ext, u8 *fw_name)
  3452. {
  3453. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  3454. struct brcmf_fw_request *fwreq;
  3455. struct brcmf_fw_name fwnames[] = {
  3456. { ext, fw_name },
  3457. };
  3458. fwreq = brcmf_fw_alloc_request(bus_if->chip, bus_if->chiprev,
  3459. brcmf_sdio_fwnames,
  3460. ARRAY_SIZE(brcmf_sdio_fwnames),
  3461. fwnames, ARRAY_SIZE(fwnames));
  3462. if (!fwreq)
  3463. return -ENOMEM;
  3464. kfree(fwreq);
  3465. return 0;
  3466. }
  3467. static const struct brcmf_bus_ops brcmf_sdio_bus_ops = {
  3468. .stop = brcmf_sdio_bus_stop,
  3469. .preinit = brcmf_sdio_bus_preinit,
  3470. .txdata = brcmf_sdio_bus_txdata,
  3471. .txctl = brcmf_sdio_bus_txctl,
  3472. .rxctl = brcmf_sdio_bus_rxctl,
  3473. .gettxq = brcmf_sdio_bus_gettxq,
  3474. .wowl_config = brcmf_sdio_wowl_config,
  3475. .get_ramsize = brcmf_sdio_bus_get_ramsize,
  3476. .get_memdump = brcmf_sdio_bus_get_memdump,
  3477. .get_fwname = brcmf_sdio_get_fwname,
  3478. .debugfs_create = brcmf_sdio_debugfs_create
  3479. };
  3480. #define BRCMF_SDIO_FW_CODE 0
  3481. #define BRCMF_SDIO_FW_NVRAM 1
  3482. static void brcmf_sdio_firmware_callback(struct device *dev, int err,
  3483. struct brcmf_fw_request *fwreq)
  3484. {
  3485. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  3486. struct brcmf_sdio_dev *sdiod = bus_if->bus_priv.sdio;
  3487. struct brcmf_sdio *bus = sdiod->bus;
  3488. struct brcmf_core *core = bus->sdio_core;
  3489. const struct firmware *code;
  3490. void *nvram;
  3491. u32 nvram_len;
  3492. u8 saveclk;
  3493. u8 devctl;
  3494. brcmf_dbg(TRACE, "Enter: dev=%s, err=%d\n", dev_name(dev), err);
  3495. if (err)
  3496. goto fail;
  3497. code = fwreq->items[BRCMF_SDIO_FW_CODE].binary;
  3498. nvram = fwreq->items[BRCMF_SDIO_FW_NVRAM].nv_data.data;
  3499. nvram_len = fwreq->items[BRCMF_SDIO_FW_NVRAM].nv_data.len;
  3500. kfree(fwreq);
  3501. /* try to download image and nvram to the dongle */
  3502. bus->alp_only = true;
  3503. err = brcmf_sdio_download_firmware(bus, code, nvram, nvram_len);
  3504. if (err)
  3505. goto fail;
  3506. bus->alp_only = false;
  3507. /* Start the watchdog timer */
  3508. bus->sdcnt.tickcnt = 0;
  3509. brcmf_sdio_wd_timer(bus, true);
  3510. sdio_claim_host(sdiod->func1);
  3511. /* Make sure backplane clock is on, needed to generate F2 interrupt */
  3512. brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
  3513. if (bus->clkstate != CLK_AVAIL)
  3514. goto release;
  3515. /* Force clocks on backplane to be sure F2 interrupt propagates */
  3516. saveclk = brcmf_sdiod_readb(sdiod, SBSDIO_FUNC1_CHIPCLKCSR, &err);
  3517. if (!err) {
  3518. brcmf_sdiod_writeb(sdiod, SBSDIO_FUNC1_CHIPCLKCSR,
  3519. (saveclk | SBSDIO_FORCE_HT), &err);
  3520. }
  3521. if (err) {
  3522. brcmf_err("Failed to force clock for F2: err %d\n", err);
  3523. goto release;
  3524. }
  3525. /* Enable function 2 (frame transfers) */
  3526. brcmf_sdiod_writel(sdiod, core->base + SD_REG(tosbmailboxdata),
  3527. SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT, NULL);
  3528. err = sdio_enable_func(sdiod->func2);
  3529. brcmf_dbg(INFO, "enable F2: err=%d\n", err);
  3530. /* If F2 successfully enabled, set core and enable interrupts */
  3531. if (!err) {
  3532. /* Set up the interrupt mask and enable interrupts */
  3533. bus->hostintmask = HOSTINTMASK;
  3534. brcmf_sdiod_writel(sdiod, core->base + SD_REG(hostintmask),
  3535. bus->hostintmask, NULL);
  3536. switch (sdiod->func1->device) {
  3537. case SDIO_DEVICE_ID_CYPRESS_4373:
  3538. brcmf_dbg(INFO, "set F2 watermark to 0x%x*4 bytes\n",
  3539. CY_4373_F2_WATERMARK);
  3540. brcmf_sdiod_writeb(sdiod, SBSDIO_WATERMARK,
  3541. CY_4373_F2_WATERMARK, &err);
  3542. devctl = brcmf_sdiod_readb(sdiod, SBSDIO_DEVICE_CTL,
  3543. &err);
  3544. devctl |= SBSDIO_DEVCTL_F2WM_ENAB;
  3545. brcmf_sdiod_writeb(sdiod, SBSDIO_DEVICE_CTL, devctl,
  3546. &err);
  3547. brcmf_sdiod_writeb(sdiod, SBSDIO_FUNC1_MESBUSYCTRL,
  3548. CY_4373_F2_WATERMARK |
  3549. SBSDIO_MESBUSYCTRL_ENAB, &err);
  3550. break;
  3551. default:
  3552. brcmf_sdiod_writeb(sdiod, SBSDIO_WATERMARK,
  3553. DEFAULT_F2_WATERMARK, &err);
  3554. break;
  3555. }
  3556. } else {
  3557. /* Disable F2 again */
  3558. sdio_disable_func(sdiod->func2);
  3559. goto release;
  3560. }
  3561. if (brcmf_chip_sr_capable(bus->ci)) {
  3562. brcmf_sdio_sr_init(bus);
  3563. } else {
  3564. /* Restore previous clock setting */
  3565. brcmf_sdiod_writeb(sdiod, SBSDIO_FUNC1_CHIPCLKCSR,
  3566. saveclk, &err);
  3567. }
  3568. if (err == 0) {
  3569. /* Allow full data communication using DPC from now on. */
  3570. brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
  3571. err = brcmf_sdiod_intr_register(sdiod);
  3572. if (err != 0)
  3573. brcmf_err("intr register failed:%d\n", err);
  3574. }
  3575. /* If we didn't come up, turn off backplane clock */
  3576. if (err != 0)
  3577. brcmf_sdio_clkctl(bus, CLK_NONE, false);
  3578. sdio_release_host(sdiod->func1);
  3579. /* Assign bus interface call back */
  3580. sdiod->bus_if->dev = sdiod->dev;
  3581. sdiod->bus_if->ops = &brcmf_sdio_bus_ops;
  3582. sdiod->bus_if->chip = bus->ci->chip;
  3583. sdiod->bus_if->chiprev = bus->ci->chiprev;
  3584. /* Attach to the common layer, reserve hdr space */
  3585. err = brcmf_attach(sdiod->dev, sdiod->settings);
  3586. if (err != 0) {
  3587. brcmf_err("brcmf_attach failed\n");
  3588. goto fail;
  3589. }
  3590. /* ready */
  3591. return;
  3592. release:
  3593. sdio_release_host(sdiod->func1);
  3594. fail:
  3595. brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), err);
  3596. device_release_driver(&sdiod->func2->dev);
  3597. device_release_driver(dev);
  3598. }
  3599. static struct brcmf_fw_request *
  3600. brcmf_sdio_prepare_fw_request(struct brcmf_sdio *bus)
  3601. {
  3602. struct brcmf_fw_request *fwreq;
  3603. struct brcmf_fw_name fwnames[] = {
  3604. { ".bin", bus->sdiodev->fw_name },
  3605. { ".txt", bus->sdiodev->nvram_name },
  3606. };
  3607. fwreq = brcmf_fw_alloc_request(bus->ci->chip, bus->ci->chiprev,
  3608. brcmf_sdio_fwnames,
  3609. ARRAY_SIZE(brcmf_sdio_fwnames),
  3610. fwnames, ARRAY_SIZE(fwnames));
  3611. if (!fwreq)
  3612. return NULL;
  3613. fwreq->items[BRCMF_SDIO_FW_CODE].type = BRCMF_FW_TYPE_BINARY;
  3614. fwreq->items[BRCMF_SDIO_FW_NVRAM].type = BRCMF_FW_TYPE_NVRAM;
  3615. return fwreq;
  3616. }
  3617. struct brcmf_sdio *brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
  3618. {
  3619. int ret;
  3620. struct brcmf_sdio *bus;
  3621. struct workqueue_struct *wq;
  3622. struct brcmf_fw_request *fwreq;
  3623. brcmf_dbg(TRACE, "Enter\n");
  3624. /* Allocate private bus interface state */
  3625. bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
  3626. if (!bus)
  3627. goto fail;
  3628. bus->sdiodev = sdiodev;
  3629. sdiodev->bus = bus;
  3630. skb_queue_head_init(&bus->glom);
  3631. bus->txbound = BRCMF_TXBOUND;
  3632. bus->rxbound = BRCMF_RXBOUND;
  3633. bus->txminmax = BRCMF_TXMINMAX;
  3634. bus->tx_seq = SDPCM_SEQ_WRAP - 1;
  3635. /* single-threaded workqueue */
  3636. wq = alloc_ordered_workqueue("brcmf_wq/%s", WQ_MEM_RECLAIM,
  3637. dev_name(&sdiodev->func1->dev));
  3638. if (!wq) {
  3639. brcmf_err("insufficient memory to create txworkqueue\n");
  3640. goto fail;
  3641. }
  3642. brcmf_sdiod_freezer_count(sdiodev);
  3643. INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
  3644. bus->brcmf_wq = wq;
  3645. /* attempt to attach to the dongle */
  3646. if (!(brcmf_sdio_probe_attach(bus))) {
  3647. brcmf_err("brcmf_sdio_probe_attach failed\n");
  3648. goto fail;
  3649. }
  3650. spin_lock_init(&bus->rxctl_lock);
  3651. spin_lock_init(&bus->txq_lock);
  3652. init_waitqueue_head(&bus->ctrl_wait);
  3653. init_waitqueue_head(&bus->dcmd_resp_wait);
  3654. /* Set up the watchdog timer */
  3655. timer_setup(&bus->timer, brcmf_sdio_watchdog, 0);
  3656. /* Initialize watchdog thread */
  3657. init_completion(&bus->watchdog_wait);
  3658. bus->watchdog_tsk = kthread_run(brcmf_sdio_watchdog_thread,
  3659. bus, "brcmf_wdog/%s",
  3660. dev_name(&sdiodev->func1->dev));
  3661. if (IS_ERR(bus->watchdog_tsk)) {
  3662. pr_warn("brcmf_watchdog thread failed to start\n");
  3663. bus->watchdog_tsk = NULL;
  3664. }
  3665. /* Initialize DPC thread */
  3666. bus->dpc_triggered = false;
  3667. bus->dpc_running = false;
  3668. /* default sdio bus header length for tx packet */
  3669. bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
  3670. /* Query the F2 block size, set roundup accordingly */
  3671. bus->blocksize = bus->sdiodev->func2->cur_blksize;
  3672. bus->roundup = min(max_roundup, bus->blocksize);
  3673. sdio_claim_host(bus->sdiodev->func1);
  3674. /* Disable F2 to clear any intermediate frame state on the dongle */
  3675. sdio_disable_func(bus->sdiodev->func2);
  3676. bus->rxflow = false;
  3677. /* Done with backplane-dependent accesses, can drop clock... */
  3678. brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
  3679. sdio_release_host(bus->sdiodev->func1);
  3680. /* ...and initialize clock/power states */
  3681. bus->clkstate = CLK_SDONLY;
  3682. bus->idletime = BRCMF_IDLE_INTERVAL;
  3683. bus->idleclock = BRCMF_IDLE_ACTIVE;
  3684. /* SR state */
  3685. bus->sr_enabled = false;
  3686. brcmf_dbg(INFO, "completed!!\n");
  3687. fwreq = brcmf_sdio_prepare_fw_request(bus);
  3688. if (!fwreq) {
  3689. ret = -ENOMEM;
  3690. goto fail;
  3691. }
  3692. ret = brcmf_fw_get_firmwares(sdiodev->dev, fwreq,
  3693. brcmf_sdio_firmware_callback);
  3694. if (ret != 0) {
  3695. brcmf_err("async firmware request failed: %d\n", ret);
  3696. kfree(fwreq);
  3697. goto fail;
  3698. }
  3699. return bus;
  3700. fail:
  3701. brcmf_sdio_remove(bus);
  3702. return NULL;
  3703. }
  3704. /* Detach and free everything */
  3705. void brcmf_sdio_remove(struct brcmf_sdio *bus)
  3706. {
  3707. brcmf_dbg(TRACE, "Enter\n");
  3708. if (bus) {
  3709. /* Stop watchdog task */
  3710. if (bus->watchdog_tsk) {
  3711. send_sig(SIGTERM, bus->watchdog_tsk, 1);
  3712. kthread_stop(bus->watchdog_tsk);
  3713. bus->watchdog_tsk = NULL;
  3714. }
  3715. /* De-register interrupt handler */
  3716. brcmf_sdiod_intr_unregister(bus->sdiodev);
  3717. brcmf_detach(bus->sdiodev->dev);
  3718. cancel_work_sync(&bus->datawork);
  3719. if (bus->brcmf_wq)
  3720. destroy_workqueue(bus->brcmf_wq);
  3721. if (bus->ci) {
  3722. if (bus->sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
  3723. sdio_claim_host(bus->sdiodev->func1);
  3724. brcmf_sdio_wd_timer(bus, false);
  3725. brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
  3726. /* Leave the device in state where it is
  3727. * 'passive'. This is done by resetting all
  3728. * necessary cores.
  3729. */
  3730. msleep(20);
  3731. brcmf_chip_set_passive(bus->ci);
  3732. brcmf_sdio_clkctl(bus, CLK_NONE, false);
  3733. sdio_release_host(bus->sdiodev->func1);
  3734. }
  3735. brcmf_chip_detach(bus->ci);
  3736. }
  3737. if (bus->sdiodev->settings)
  3738. brcmf_release_module_param(bus->sdiodev->settings);
  3739. kfree(bus->rxbuf);
  3740. kfree(bus->hdrbuf);
  3741. kfree(bus);
  3742. }
  3743. brcmf_dbg(TRACE, "Disconnected\n");
  3744. }
  3745. void brcmf_sdio_wd_timer(struct brcmf_sdio *bus, bool active)
  3746. {
  3747. /* Totally stop the timer */
  3748. if (!active && bus->wd_active) {
  3749. del_timer_sync(&bus->timer);
  3750. bus->wd_active = false;
  3751. return;
  3752. }
  3753. /* don't start the wd until fw is loaded */
  3754. if (bus->sdiodev->state != BRCMF_SDIOD_DATA)
  3755. return;
  3756. if (active) {
  3757. if (!bus->wd_active) {
  3758. /* Create timer again when watchdog period is
  3759. dynamically changed or in the first instance
  3760. */
  3761. bus->timer.expires = jiffies + BRCMF_WD_POLL;
  3762. add_timer(&bus->timer);
  3763. bus->wd_active = true;
  3764. } else {
  3765. /* Re arm the timer, at last watchdog period */
  3766. mod_timer(&bus->timer, jiffies + BRCMF_WD_POLL);
  3767. }
  3768. }
  3769. }
  3770. int brcmf_sdio_sleep(struct brcmf_sdio *bus, bool sleep)
  3771. {
  3772. int ret;
  3773. sdio_claim_host(bus->sdiodev->func1);
  3774. ret = brcmf_sdio_bus_sleep(bus, sleep, false);
  3775. sdio_release_host(bus->sdiodev->func1);
  3776. return ret;
  3777. }