ufs_aio_hcd.c 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2015. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. */
  31. #include "ufs_aio_cfg.h"
  32. #include "ufs_aio_types.h"
  33. #include "ufs_aio_compiler.h"
  34. #include "ufs_aio_platform.h"
  35. #include "ufs_aio.h"
  36. #include "ufs_aio_utils.h"
  37. #include "ufs_aio_hcd.h"
  38. #include "ufs_aio_core.h"
  39. #include "ufs_aio_quirks.h"
  40. #include "ufs_aio_unipro.h"
  41. #include "ufs_aio_error.h"
  42. #if defined(MTK_UFS_DRV_PRELOADER)
  43. #if CFG_ENABLE_DCACHE
  44. #include "platform.h"
  45. #endif
  46. #endif
  47. #if defined(MTK_UFS_DRV_DA)
  48. #include <arch/ops.h> /* for cache maintanance APIs */
  49. #include "dev/gpt_timer/gpt_timer.h" /* for gpt4_time2tick_ms APIs */
  50. #include <kernel/event.h> /* for event_wait_timeout APIs */
  51. #include "boot/system_objects.h" /* for sysob_runtime_params and INT_MODE_ENABLE */
  52. #include <dev/irq/irq.h> /* for mt_irq_set_sens APIs */
  53. #include <dev/irq/gic.h> /* for mt_irq_ack APIs */
  54. #endif
  55. #if defined(MTK_UFS_DRV_LK)
  56. #include <arch/ops.h> /* for cache maintanance APIs */
  57. #include <kernel/event.h>
  58. #include <mt_gic.h>
  59. #include <profiling.h>
  60. #include <sync_write.h>
  61. #include <mtk_secure_api.h>
  62. /* used for secure call */
  63. #define MTK_SIP_BL_UFS_CONTROL_AARCH32 0x8200040D
  64. #endif
  65. #if defined(MTK_UFS_DRV_DA) || defined(MTK_UFS_DRV_LK)
  66. static event_t ufshcd_intr_event;
  67. static u32 ufshcd_intr_status;
  68. #endif
  69. #if defined(MTK_UFS_DRV_CTP)
  70. #include <common.h>
  71. #include <api.h> /* for cache maintanance APIs (cache_api.h) */
  72. #include <kernel_to_ctp.h> /* for delay functions */
  73. u8 ufs_crypto_rw_en = 0;
  74. #endif
  75. #if !defined(__WEAK)
  76. #define __WEAK __attribute__((weak))
  77. #endif
  78. struct ufs_hba g_ufs_hba = {0};
  79. struct ufs_platform_info ufs_platform = {0};
  80. static struct utp_transfer_cmd_desc ufs_ucdl[UFS_AIO_MAX_NUTRS] __attribute__((aligned(UFS_ALIGN_UCD)));
  81. static struct utp_transfer_req_desc ufs_utrdl[UTRD_NUTRS] __attribute__((aligned(UFS_ALIGN_UTRD)));
  82. static struct ufshcd_lrb ufs_lrb[UFS_AIO_MAX_NUTRS];
  83. unsigned char ufs_req_upiu[UPIU_CMD_BUF_SIZE]; /* for PIO only */
  84. unsigned char ufs_resp_upiu[UPIU_CMD_BUF_SIZE]; /* for PIO only */
  85. /* static struct utp_task_req_desc ufs_utmrdl[UFS_AIO_MAX_NUTMRS] __ALIGNED(UFS_ALIGN_UTMRD); */
  86. /* static u32 ufs_sense_buf[(UFS_AIO_MAX_NUTRS * SCSI_SENSE_BUFFERSIZE) / sizeof(u32)]; */
  87. /**
  88. * g_ufs_temp_buf, for
  89. * 1. ufshcd_read_desc_param: read partial descriptor case. Size limit: UFS_TEMP_BUF_SIZE (256 bytes)
  90. * 2. ufshcd_read_string_desc: used for destination buffer of utf16s_to_utf8s
  91. */
  92. unsigned char g_ufs_temp_buf[UFS_TEMP_BUF_SIZE] __attribute__((aligned(UFS_PLATFORM_CACHE_LINE_SIZE)));
  93. static u32 ufs_query_desc_max_size[] = {
  94. QUERY_DESC_DEVICE_MAX_SIZE,
  95. QUERY_DESC_CONFIGURAION_MAX_SIZE_HPB_20,
  96. QUERY_DESC_UNIT_MAX_SIZE,
  97. QUERY_DESC_RFU_MAX_SIZE,
  98. QUERY_DESC_INTERCONNECT_MAX_SIZE,
  99. QUERY_DESC_STRING_MAX_SIZE,
  100. QUERY_DESC_RFU_MAX_SIZE,
  101. QUERY_DESC_GEOMETRY_MAX_SIZE,
  102. QUERY_DESC_POWER_MAX_SIZE,
  103. QUERY_DESC_HEALTH_MAX_SIZE,
  104. QUERY_DESC_RFU_MAX_SIZE,
  105. };
  106. int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel, u32 *mib_val, u8 peer);
  107. int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel, u8 attr_set, u32 mib_val, u8 peer);
  108. int ufshcd_wait_command(struct ufs_hba *hba, struct ufshcd_lrb *lrbp, u32 timeout_ms);
  109. static void ufs_aio_reset_device(struct ufs_hba *hba);
  110. static void ufs_aio_advertise_hci_quirks(struct ufs_hba *hba);
  111. static int ufs_aio_bootrom_deputy(struct ufs_hba *hba);
  112. static void ufs_aio_dma_map(ufs_vaddr_t buf, ufs_size_t len, enum dma_data_direction dir);
  113. static void ufs_aio_dma_unmap(ufs_vaddr_t buf, ufs_size_t len, enum dma_data_direction dir);
  114. int ufs_aio_pio_read_flag(struct ufs_hba *hba, unsigned char flag_idx, bool *value);
  115. int ufs_aio_pio_set_flag(struct ufs_hba *hba, unsigned char flag_idx);
  116. static int ufs_aio_post_link(struct ufs_hba *hba);
  117. static int ufs_aio_pre_link(struct ufs_hba *hba);
  118. static int ufs_aio_pre_pwr_change(struct ufs_hba *hba, struct ufs_pa_layer_attr *desired, struct ufs_pa_layer_attr *final);
  119. static int ufs_aio_prepare_new_ufs(struct ufs_hba *hba);
  120. static int ufs_aio_test_unit_ready_all_device(struct ufs_hba *hba);
  121. static void ufshcd_init_desc_sizes(struct ufs_hba *hba);
  122. static int ufshcd_init_vendor_desc_sizes(struct ufs_hba *hba);
  123. #if defined(MTK_UFS_DRV_DA) || defined(MTK_UFS_DRV_LK)
  124. extern void arch_clean_invalidate_cache_range(ufs_vaddr_t start, ufs_size_t len);
  125. extern void arch_sync_cache_range(ufs_vaddr_t start, ufs_size_t len);
  126. void ufshcd_irq_handler(unsigned int irq)
  127. {
  128. struct ufs_hba *hba = &g_ufs_hba;
  129. struct ufshcd_lrb *lrbp;
  130. u32 tag_mask;
  131. u32 tr_doorbell;
  132. tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
  133. lrbp = &hba->lrb[0];
  134. tag_mask = 1 << lrbp->task_tag;
  135. if (!(tag_mask & tr_doorbell)) {
  136. /* Clear_bit(lrbp->task_tag, &hba->outstanding_reqs); */
  137. hba->outstanding_reqs &= ~(tag_mask);
  138. /* Save interrupt status and clear it */
  139. ufshcd_intr_status = ufs_aio_readl(hba, REG_INTERRUPT_STATUS);
  140. ufshcd_writel(hba, ufshcd_intr_status, REG_INTERRUPT_STATUS);
  141. /* dsb make sure clear initerrupt status */
  142. dsb();
  143. /* Wakeup ufshcd_wait_command */
  144. event_signal(&ufshcd_intr_event, 0);
  145. }
  146. /* Let irq handler can enter again, must put in irq handler bottom */
  147. mt_irq_ack(irq);
  148. }
  149. __WEAK int ufs_get_irq_id(void)
  150. {
  151. return 0xFFFFFFFF;
  152. }
  153. static int ufshcd_irq_init(struct ufs_hba *hba)
  154. {
  155. hba->irq = ufs_get_irq_id();
  156. if (hba->irq == 0xFFFFFFFF)
  157. return -1;
  158. /* Set ufs interrupt level, polarity */
  159. mt_irq_set_sens(hba->irq, MT65xx_LEVEL_SENSITIVE);
  160. mt_irq_set_polarity(hba->irq, MT65xx_POLARITY_LOW);
  161. /* Init event for thread function ufshcd_wait_command sleep/wakeup */
  162. event_init(&ufshcd_intr_event, false, EVENT_FLAG_AUTOUNSIGNAL);
  163. /*
  164. * Mask all interrupt first, only unmask before thread sleep and wait.
  165. * Else there may have un-wanted interrupt come in, which is need by
  166. * polling function, like UIC_COMMAND_COMPL
  167. */
  168. mt_irq_mask(hba->irq);
  169. return 0;
  170. }
  171. #endif
  172. /* Platform different need implement it's ufs_get_platform_data */
  173. __WEAK void ufs_get_platform_data(void)
  174. {
  175. ufs_platform.hci_base = (void*) 0x11270000;
  176. ufs_platform.pericfg_base = (void *)0x10003000;
  177. ufs_platform.mphy_base = (void *)0x11FA0000;
  178. ufs_platform.reg_ufs_pericfg = 0x448;
  179. ufs_platform.reg_ufs_pericfg_rst_n_bit = 3;
  180. ufs_platform.reg_ufs_pericfg_ldo_n_bit = 0xF;
  181. ufs_platform.reg_ufs_pericfg_lp_n_bit = 0xF;
  182. }
  183. static inline void ufshcd_get_host_capabilities(struct ufs_hba *hba)
  184. {
  185. /* for AIO driver, only 1 task is enough */
  186. hba->nutrs = UFS_AIO_MAX_NUTRS;
  187. /* hba->nutmrs = UFS_AIO_MAX_NUTMRS; */
  188. }
  189. void ufshcd_power(struct ufs_hba *hba, bool on)
  190. {
  191. }
  192. void ufshcd_clock(struct ufs_hba *hba, u8 on)
  193. {
  194. }
  195. /**
  196. * ufshcd_memory_alloc - allocate memory for hba memory space data structures
  197. * @hba: per adapter instance
  198. *
  199. * 1. Allocate DMA memory for Command Descriptor array
  200. * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
  201. * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
  202. * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
  203. * (UTMRDL)
  204. * 4. Allocate memory for local reference block(lrb).
  205. *
  206. * Returns 0 for success, non-zero in case of failure
  207. */
  208. static int ufshcd_memory_alloc(struct ufs_hba *hba)
  209. {
  210. /* u32 i; */
  211. /* u8 *ptr; */
  212. /* Allocate memory for UTP command descriptors */
  213. hba->ucdl_base_addr = (struct utp_transfer_cmd_desc *)&ufs_ucdl[0];
  214. hba->ucdl_dma_addr = (ufs_paddr_t)hba->ucdl_base_addr;
  215. if (hba->ucdl_dma_addr & (UFS_ALIGN_UCD - 1)) {
  216. UFS_DBG_LOGD("memory alignment failed: ucdl, addr 0x%x, shall align %d\n", (unsigned int)hba->ucdl_dma_addr, UFS_ALIGN_UCD);
  217. goto out;
  218. }
  219. /*
  220. * Allocate memory for UTP Transfer descriptors
  221. * UFSHCI requires 1024 byte alignment of UTRD
  222. */
  223. hba->utrdl_base_addr = (struct utp_transfer_req_desc *)&ufs_utrdl[0];
  224. hba->utrdl_dma_addr = (ufs_paddr_t)hba->utrdl_base_addr;
  225. if (hba->utrdl_dma_addr & (UFS_ALIGN_UTRD - 1)) {
  226. UFS_DBG_LOGD("memory alignment failed: utrdl, addr 0x%x, shall align %d\n", (unsigned int)hba->utrdl_dma_addr, UFS_ALIGN_UTRD);
  227. goto out;
  228. }
  229. /*
  230. * Note. UFSHCI 2.1 requires 1024-byte alignment for UTRD (UTP Transfer Request Descriptor)
  231. *
  232. * SW tricks:
  233. * 1. Fill-in UFS_HCI_REG_UTRLBA a 1024-byte alignment address to assume the 1st UTRD entry (task ID = 0) is in that address.
  234. * 2. Since every UTRD entry is fixed to 32 bytes, now we can use our real UTRD base address by using "its" task ID.
  235. */
  236. hba->active_tr_tag = (u8)(((unsigned long)hba->utrdl_base_addr & UFS_1KB_MASK) / sizeof(struct utp_transfer_req_desc));
  237. #if 0
  238. /*
  239. * Allocate memory for UTP Task Management descriptors
  240. * UFSHCI requires 1024 byte alignment of UTMRD
  241. */
  242. hba->utmrdl_base_addr = (utp_task_req_desc *)&ufs_utmrdl[0];
  243. hba->utmrdl_dma_addr = (ufs_paddr_t)hba->utmrdl_base_addr;
  244. if (hba->utmrdl_dma_addr & (UFS_ALIGN_UTMRD - 1)) {
  245. UFS_DBG_LOGD("memory alignment failed: utmrdl, addr 0x%x, shall align %d\n", hba->utmrdl_dma_addr, UFS_ALIGN_UTMRD);
  246. goto out;
  247. }
  248. hba->active_tm_tag = (hba->utmrdl_base_addr & UFS_1KB_MASK) / sizeof(struct utp_task_req_desc);
  249. #endif
  250. /* Allocate memory for local reference block */
  251. hba->lrb = &ufs_lrb[0];
  252. #if 0
  253. /* Allocate memory for SCSI sense buffer */
  254. ptr = &(ufs_sense_buf[0]);
  255. for (i = 0; i < UFS_AIO_MAX_NUTRS; i++, ptr += SCSI_SENSE_BUFFERSIZE)
  256. hba->sense_buf_base_addr[i] = hba->sense_buf_dma_addr[i] = ptr;
  257. #endif
  258. return 0;
  259. out:
  260. return -1;
  261. }
  262. static void ufshcd_host_memory_configure(struct ufs_hba *hba)
  263. {
  264. int i;
  265. int cmd_desc_size;
  266. u16 prdt_offset;
  267. u16 response_offset;
  268. ufs_paddr_t cmd_desc_element_addr;
  269. ufs_paddr_t cmd_desc_dma_addr;
  270. struct utp_transfer_req_desc *utrdlp;
  271. struct utp_transfer_cmd_desc *cmd_descp;
  272. cmd_descp = hba->ucdl_base_addr;
  273. utrdlp = hba->utrdl_base_addr;
  274. prdt_offset = offsetof(struct utp_transfer_cmd_desc, prd_table);
  275. response_offset = offsetof(struct utp_transfer_cmd_desc, response_upiu);
  276. cmd_desc_dma_addr = hba->ucdl_dma_addr;
  277. cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
  278. for (i = 0; i < hba->nutrs; i++) {
  279. cmd_desc_element_addr =
  280. (cmd_desc_dma_addr + (cmd_desc_size * i));
  281. utrdlp[i].command_desc_base_addr_hi = cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
  282. utrdlp[i].command_desc_base_addr_lo = cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
  283. utrdlp[i].response_upiu_length = cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
  284. utrdlp[i].prd_table_offset = cpu_to_le16((prdt_offset >> 2));
  285. utrdlp[i].response_upiu_offset = cpu_to_le16((response_offset >> 2));
  286. hba->lrb[i].ucd_prdt_ptr = (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
  287. hba->lrb[i].ucd_req_ptr = (struct utp_upiu_req *)(cmd_descp + i);
  288. hba->lrb[i].ucd_rsp_ptr = (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
  289. hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
  290. }
  291. }
  292. /**
  293. * ufshcd_enable_intr - enable interrupts
  294. * @hba: per adapter instance
  295. * @intrs: interrupt bits
  296. */
  297. static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
  298. {
  299. u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
  300. set |= intrs;
  301. ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
  302. }
  303. static int ufshcd_hba_enable(struct ufs_hba *hba)
  304. {
  305. int retry;
  306. if ((ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) == 1) {
  307. ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
  308. msleep(5);
  309. }
  310. /*
  311. * Reset device after hba disabled
  312. * This is to make sure host TX is idle
  313. * when resetting device.
  314. */
  315. ufs_aio_reset_device(hba);
  316. ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
  317. msleep(1);
  318. retry = 20;
  319. while ((ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) == 0) {
  320. if (retry) {
  321. retry--;
  322. } else {
  323. UFS_DBG_LOGE("Controller enable failed\n");
  324. return -1;
  325. }
  326. msleep(5);
  327. }
  328. /* enable UIC related interrupts */
  329. ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
  330. return 0;
  331. }
  332. static int
  333. __ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
  334. {
  335. if (!(ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)) {
  336. UFS_DBG_LOGD("Controller not ready to accept UIC commands\n");
  337. return -1;
  338. }
  339. UFS_DBG_LOGD("[UFS] info: UCMD:%x,%x,%x,%x\n", uic_cmd->command, uic_cmd->argument1, uic_cmd->argument2, uic_cmd->argument3);
  340. hba->active_uic_cmd = uic_cmd;
  341. ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
  342. ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
  343. ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
  344. ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK, REG_UIC_COMMAND);
  345. return 0;
  346. }
  347. static int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
  348. u32 val, unsigned long interval_us, unsigned long timeout_ms)
  349. {
  350. unsigned long elapsed_us = 0;
  351. int err = 0;
  352. val &= mask;
  353. while (val != (ufshcd_readl(hba, reg) & mask)) {
  354. /* wakeup within 50us of expiry */
  355. usleep(interval_us);
  356. elapsed_us += interval_us;
  357. if (elapsed_us > timeout_ms * 1000) {
  358. if ((ufshcd_readl(hba, reg) & mask) != val)
  359. err = -1;
  360. break;
  361. }
  362. }
  363. return err;
  364. }
  365. static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
  366. {
  367. if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
  368. hba->active_uic_cmd->argument2 |=
  369. (ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) & MASK_UIC_COMMAND_RESULT);
  370. hba->active_uic_cmd->argument3 = ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
  371. }
  372. }
  373. static int ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
  374. {
  375. int ret;
  376. ret = ufshcd_wait_for_register(hba, REG_INTERRUPT_STATUS,
  377. UIC_COMMAND_COMPL, UIC_COMMAND_COMPL,
  378. 100, UIC_CMD_TIMEOUT);
  379. if (!ret) {
  380. ufshcd_writel(hba, UIC_COMMAND_COMPL,
  381. REG_INTERRUPT_STATUS);
  382. ufshcd_uic_cmd_compl(hba, UIC_COMMAND_COMPL);
  383. } else {
  384. UFS_DBG_LOGD("ufshcd_wait_for_uic_cmd error! ret: %d\n", ret);
  385. }
  386. hba->active_uic_cmd = NULL;
  387. return ret;
  388. }
  389. static int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
  390. {
  391. int ret;
  392. ret = __ufshcd_send_uic_cmd(hba, uic_cmd);
  393. if (!ret)
  394. ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
  395. return ret;
  396. }
  397. int ufshcd_uic_cmd_run(struct ufs_hba *hba, struct uic_command *cmds, int ncmds)
  398. {
  399. int i;
  400. int err = 0;
  401. for (i = 0; i < ncmds; i++) {
  402. err = ufshcd_send_uic_cmd(hba, &cmds[i]);
  403. if (err) {
  404. UFS_DBG_LOGD("ufshcd_uic_cmd_run fail, cmd: %x, arg1: %x\n", cmds->command, cmds->argument1);
  405. /* send next commands anyway */
  406. }
  407. }
  408. return err;
  409. }
  410. static int ufshcd_dme_link_startup(struct ufs_hba *hba)
  411. {
  412. struct uic_command uic_cmd = {0};
  413. uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
  414. return ufshcd_send_uic_cmd(hba, &uic_cmd);
  415. }
  416. /**
  417. * ufshcd_is_device_present - Check if any device connected to
  418. * the host controller
  419. * @hba: pointer to adapter instance
  420. *
  421. * Returns 1 if device present, 0 if no device detected
  422. */
  423. static inline int ufshcd_is_device_present(struct ufs_hba *hba)
  424. {
  425. return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
  426. DEVICE_PRESENT) ? 1 : 0;
  427. }
  428. /**
  429. * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
  430. * @reg: Register value of host controller status
  431. *
  432. * Returns integer, 0 on Success and positive value if failed
  433. */
  434. static inline int ufshcd_get_lists_status(u32 reg)
  435. {
  436. /*
  437. * The mask 0xFF is for the following HCS register bits
  438. * Bit Description
  439. * 0 Device Present
  440. * 1 UTRLRDY
  441. * 2 UTMRLRDY
  442. * 3 UCRDY
  443. * 4 HEI
  444. * 5 DEI
  445. * 6-7 reserved
  446. */
  447. return (((reg) & (0xFF)) >> 1) ^ (0x07);
  448. }
  449. /**
  450. * ufshcd_enable_run_stop_reg - Enable run-stop registers,
  451. * When run-stop registers are set to 1, it indicates the
  452. * host controller that it can process the requests
  453. * @hba: per adapter instance
  454. */
  455. static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
  456. {
  457. ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
  458. REG_UTP_TASK_REQ_LIST_RUN_STOP);
  459. ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
  460. REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
  461. }
  462. static int ufshcd_make_hba_operational(struct ufs_hba *hba)
  463. {
  464. int err = 0;
  465. u32 reg;
  466. ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
  467. REG_UTP_TRANSFER_REQ_LIST_BASE_H);
  468. ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
  469. REG_UTP_TRANSFER_REQ_LIST_BASE_L);
  470. ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
  471. reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
  472. if ((ufshcd_get_lists_status(reg)) == 0) {
  473. ufshcd_enable_run_stop_reg(hba);
  474. } else {
  475. UFS_DBG_LOGD("Host controller not ready to process requests");
  476. err = -1;
  477. }
  478. return err;
  479. }
  480. /**
  481. * ufshcd_link_startup - Initialize unipro link startup
  482. * @hba: per adapter instance
  483. *
  484. * Returns 0 for success, non-zero in case of failure
  485. */
  486. static int ufshcd_link_startup(struct ufs_hba *hba)
  487. {
  488. int ret;
  489. int retries = DME_LINKSTARTUP_RETRIES;
  490. do {
  491. ufs_aio_pre_link(hba);
  492. ret = ufshcd_dme_link_startup(hba);
  493. if (ret) {
  494. UFS_DBG_LOGE("link: UIC command fail\n");
  495. goto retry;
  496. }
  497. /* check if device is detected by inter-connect layer */
  498. if (!ufshcd_is_device_present(hba)) {
  499. UFS_DBG_LOGE("link: Device not present\n");
  500. ret = -1;
  501. /* directly go through to retry section */
  502. }
  503. retry:
  504. /*
  505. * DME link lost indication is only received when link is up,
  506. * but we can't be sure if the link is up until link startup
  507. * succeeds. So reset the local Uni-Pro and try again.
  508. */
  509. if (ret) {
  510. UFS_DBG_LOGE("link startup fail, retrying the %d times ...\n", DME_LINKSTARTUP_RETRIES - retries + 1);
  511. if (ufshcd_hba_enable(hba)) {
  512. /* critical error: controller is not working now, keep ret and goto out */
  513. goto out;
  514. }
  515. }
  516. } while (ret && retries--);
  517. if (ret)
  518. /* failed to get the link up... retire */
  519. goto out;
  520. /* Include any host controller configuration via UIC commands */
  521. ret = ufs_aio_post_link(hba);
  522. if (ret)
  523. goto out;
  524. ret = ufshcd_make_hba_operational(hba);
  525. out:
  526. if (ret)
  527. UFS_DBG_LOGE("link startup failed %d\n", ret);
  528. return ret;
  529. }
  530. /**
  531. * ufshcd_init_pwr_info - setting the POR (power on reset)
  532. * values in hba power info
  533. * @hba: per-adapter instance
  534. */
  535. static void ufshcd_init_pwr_info(struct ufs_hba *hba)
  536. {
  537. hba->pwr_info.gear_rx = UFS_PWM_G1;
  538. hba->pwr_info.gear_tx = UFS_PWM_G1;
  539. hba->pwr_info.lane_rx = 1;
  540. hba->pwr_info.lane_tx = 1;
  541. hba->pwr_info.pwr_rx = SLOW_MODE;
  542. hba->pwr_info.pwr_tx = SLOW_MODE;
  543. hba->pwr_info.hs_rate = 1;
  544. }
  545. /**
  546. * ufshcd_get_dev_cmd_tag - Get device management command tag
  547. * @hba: per-adapter instance
  548. * @tag: pointer to variable with available slot value
  549. *
  550. * Get a free slot and lock it until device management command
  551. * completes.
  552. *
  553. * Returns FALSE if free slot is unavailable for locking, else
  554. * return TRUE with tag value in @tag.
  555. */
  556. bool ufshcd_get_free_tag(struct ufs_hba *hba, int *tag_out)
  557. {
  558. #ifdef UFS_CFG_SINGLE_COMMAND
  559. if (!tag_out)
  560. return FALSE;
  561. if (hba->lrb_in_use & (1 << hba->active_tr_tag)) {
  562. UFS_DBG_LOGE("[UFS] ufshcd_get_free_tag fail\n");
  563. return FALSE;
  564. }
  565. else {
  566. hba->lrb_in_use |= (1 << hba->active_tr_tag);
  567. *tag_out = hba->active_tr_tag;
  568. return TRUE;
  569. }
  570. #else /* !UFS_CFG_SINGLE_COMMAND */
  571. int tag;
  572. bool ret = FALSE;
  573. unsigned long tmp;
  574. if (!tag_out)
  575. goto out;
  576. do {
  577. tmp = ~hba->lrb_in_use;
  578. tag = find_last_bit(&tmp, hba->nutrs);
  579. if (tag >= hba->nutrs)
  580. goto out;
  581. } while (test_and_set_bit(tag, &hba->lrb_in_use));
  582. *tag_out = tag;
  583. ret = TRUE;
  584. out:
  585. return ret;
  586. #endif /* UFS_CFG_SINGLE_COMMAND */
  587. }
  588. void ufshcd_put_tag(struct ufs_hba *hba, int tag)
  589. {
  590. /* clear_bit(tag, &hba->lrb_in_use); */
  591. hba->lrb_in_use &= ~(1 << tag);
  592. }
  593. static void ufshcd_prepare_req_desc_hdr(struct ufs_hba *hba,
  594. struct ufshcd_lrb *lrbp,
  595. u32 *upiu_flags, enum dma_data_direction cmd_dir)
  596. {
  597. struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
  598. u32 data_direction;
  599. u32 dword_0;
  600. if (cmd_dir == DMA_FROM_DEVICE) {
  601. data_direction = UTP_DEVICE_TO_HOST;
  602. *upiu_flags = UPIU_CMD_FLAGS_READ;
  603. } else if (cmd_dir == DMA_TO_DEVICE) {
  604. data_direction = UTP_HOST_TO_DEVICE;
  605. *upiu_flags = UPIU_CMD_FLAGS_WRITE;
  606. } else {
  607. data_direction = UTP_NO_DATA_TRANSFER;
  608. *upiu_flags = UPIU_CMD_FLAGS_NONE;
  609. }
  610. dword_0 = data_direction;
  611. dword_0 |= 1 << UPIU_COMMAND_TYPE_OFFSET;
  612. #ifdef UFS_CFG_CRYPTO
  613. if (lrbp->crypto_en) {
  614. dword_0 |= (1 << UPIU_COMMAND_CRYPTO_EN_OFFSET); /* crypto enable */
  615. dword_0 |= lrbp->crypto_cfgid;
  616. req_desc->header.dword_1 = cpu_to_le32(lrbp->crypto_dunl);
  617. req_desc->header.dword_3 = cpu_to_le32(lrbp->crypto_dunu);
  618. }
  619. #endif
  620. if (lrbp->intr_cmd)
  621. dword_0 |= UTP_REQ_DESC_INT_CMD;
  622. req_desc->header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
  623. req_desc->header.dword_0 = cpu_to_le32(dword_0);
  624. }
  625. static void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
  626. {
  627. struct utp_upiu_req *ucd_req = lrbp->ucd_req_ptr;
  628. ucd_req->sc.exp_data_transfer_len = cpu_to_be32(lrbp->cmd->exp_len);
  629. ucd_req->header.dword_2 = 0;
  630. ucd_req->header.dword_1 = UPIU_HEADER_DWORD(
  631. UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
  632. ucd_req->header.dword_0 = UPIU_HEADER_DWORD(
  633. UPIU_TRANSACTION_COMMAND, upiu_flags,
  634. lrbp->lun, lrbp->task_tag);
  635. memcpy(ucd_req->sc.cdb, lrbp->cmd->cmd_data, (min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE)));
  636. }
  637. static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
  638. struct ufshcd_lrb *lrbp, u32 upiu_flags)
  639. {
  640. u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
  641. struct ufs_query *query = &hba->dev_cmd.query;
  642. u16 len = be16_to_cpu(query->request.upiu_req.length);
  643. struct utp_upiu_req *ucd_req = lrbp->ucd_req_ptr;
  644. ucd_req->header.dword_2 = UPIU_HEADER_DWORD(
  645. 0, 0, len >> 8, (u8)len);
  646. ucd_req->header.dword_1 = UPIU_HEADER_DWORD(
  647. 0, query->request.query_func, 0, 0);
  648. ucd_req->header.dword_0 = UPIU_HEADER_DWORD(
  649. UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
  650. lrbp->lun, lrbp->task_tag);
  651. /* Copy the Query Request buffer as is */
  652. memcpy(&ucd_req->qr, &query->request.upiu_req, QUERY_OSF_SIZE);
  653. /* Copy the Descriptor */
  654. if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) {
  655. memcpy(descp, query->descriptor, len);
  656. /*
  657. * ufshcd_send_command() only flushes length of sizeof(struct utp_upiu_req).
  658. * For Write Descriptor, we may need to flush more data range.
  659. */
  660. ufs_aio_dma_map((unsigned long)descp, len, DMA_TO_DEVICE);
  661. }
  662. }
  663. static int ufshcd_compose_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
  664. {
  665. u32 upiu_flags;
  666. int ret = 0;
  667. switch (lrbp->command_type) {
  668. case UTP_CMD_TYPE_SCSI:
  669. if (lrbp->cmd) {
  670. /* prepare transfer request descriptor */
  671. ufshcd_prepare_req_desc_hdr(hba, lrbp, &upiu_flags, lrbp->cmd->dir);
  672. /* prepare COMMAND UPIU */
  673. ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
  674. } else {
  675. ret = -1;
  676. }
  677. break;
  678. case UTP_CMD_TYPE_DEV_MANAGE:
  679. ufshcd_prepare_req_desc_hdr(hba, lrbp, &upiu_flags, DMA_NONE);
  680. if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
  681. ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
  682. else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP) {
  683. memset(lrbp->ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
  684. lrbp->ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
  685. }
  686. else
  687. ret = -1;
  688. break;
  689. case UTP_CMD_TYPE_UFS:
  690. /* For UFS native command implementation */
  691. ret = -1;
  692. UFS_DBG_LOGD("UFS native command are not supported\n");
  693. break;
  694. default:
  695. ret = -1;
  696. UFS_DBG_LOGD("unknown command type: 0x%x\n", lrbp->command_type);
  697. break;
  698. } /* end of switch */
  699. return ret;
  700. }
  701. static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
  702. struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
  703. {
  704. lrbp->cmd = NULL;
  705. /* lrbp->sense_bufflen = 0; */
  706. /* lrbp->sense_buffer = NULL; */
  707. lrbp->task_tag = tag;
  708. lrbp->lun = 0; /* device management cmd is not specific to any LUN */
  709. lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
  710. lrbp->intr_cmd = TRUE; /* No interrupt aggregation */
  711. hba->dev_cmd.type = cmd_type;
  712. #ifdef UFS_CFG_CRYPTO
  713. lrbp->crypto_en = 0;
  714. #endif
  715. return ufshcd_compose_upiu(hba, lrbp);
  716. }
  717. /**
  718. * ufshcd_send_command - Send SCSI or device management commands
  719. * @hba: per adapter instance
  720. * @task_tag: Task tag of the command
  721. */
  722. static inline void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
  723. {
  724. volatile u32 intr_status;
  725. /* set_bit(task_tag, &hba->outstanding_reqs); */
  726. hba->outstanding_reqs |= (1 << task_tag);
  727. #ifdef UFS_CFG_SINGLE_COMMAND
  728. /* flush COMMAND UPIU */
  729. ufs_aio_dma_map((unsigned long)hba->lrb->ucd_req_ptr, sizeof(struct utp_upiu_req), DMA_TO_DEVICE);
  730. /* invalidate RESPONSE UPIU */
  731. ufs_aio_dma_map((unsigned long)hba->lrb->ucd_rsp_ptr, sizeof(struct utp_upiu_rsp), DMA_FROM_DEVICE);
  732. /* flush PRDT */
  733. ufs_aio_dma_map((unsigned long)hba->lrb->ucd_prdt_ptr, sizeof(struct ufs_aio_sg_entry) * UFS_AIO_MAX_SG_SEGMENTS, DMA_TO_DEVICE);
  734. /* invalidate transfer request descriptor (UTRD) */
  735. ufs_aio_dma_map((unsigned long)hba->lrb->utr_descriptor_ptr, sizeof(struct utp_transfer_req_desc), DMA_BIDIRECTIONAL);
  736. #else
  737. #error "Err: UFS AIO driver does not support multiple commands now."
  738. #endif
  739. /* clear interrupt status */
  740. intr_status = ufs_aio_readl(hba, REG_INTERRUPT_STATUS);
  741. if (intr_status)
  742. ufs_aio_writel(hba, intr_status, REG_INTERRUPT_STATUS);
  743. ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
  744. }
  745. /**
  746. * ufshcd_exec_dev_cmd - API for sending device management requests
  747. * @hba - UFS hba
  748. * @cmd_type - specifies the type (NOP, Query...)
  749. * @timeout - time in seconds
  750. *
  751. * NOTE: Since there is only one available tag for device management commands,
  752. * it is expected you hold the hba->dev_cmd.lock mutex.
  753. */
  754. static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
  755. enum dev_cmd_type cmd_type, u32 timeout)
  756. {
  757. struct ufshcd_lrb *lrbp;
  758. int err;
  759. int tag;
  760. if (!ufshcd_get_free_tag(hba, &tag))
  761. return -1;
  762. #ifdef UFS_CFG_SINGLE_COMMAND
  763. lrbp = &hba->lrb[0];
  764. #else
  765. lrbp = &hba->lrb[tag];
  766. #endif
  767. err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
  768. if (err)
  769. goto out_put_tag;
  770. ufshcd_send_command(hba, tag);
  771. err = ufshcd_wait_command(hba, lrbp, timeout);
  772. out_put_tag:
  773. ufshcd_put_tag(hba, tag);
  774. return err;
  775. }
  776. static inline void ufshcd_init_query(struct ufs_hba *hba,
  777. struct ufs_query_req **request, struct ufs_query_res **response,
  778. enum query_opcode opcode, u8 idn, u8 index, u8 selector)
  779. {
  780. *request = &hba->dev_cmd.query.request;
  781. *response = &hba->dev_cmd.query.response;
  782. memset(*request, 0, sizeof(struct ufs_query_req));
  783. memset(*response, 0, sizeof(struct ufs_query_res));
  784. (*request)->upiu_req.opcode = opcode;
  785. (*request)->upiu_req.idn = idn;
  786. (*request)->upiu_req.index = index;
  787. (*request)->upiu_req.selector = selector;
  788. }
  789. /**
  790. * ufshcd_complete_dev_init() - checks device readiness
  791. * hba: per-adapter instance
  792. *
  793. * Set fDeviceInit flag and poll until device toggles it.
  794. */
  795. static int ufshcd_complete_dev_init(struct ufs_hba *hba)
  796. {
  797. int retries, err = 0;
  798. bool flag_res = 1;
  799. u32 start_tick, timeout_tick;
  800. for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
  801. /* Set the fDeviceInit flag */
  802. err = hba->query_flag(hba, UPIU_QUERY_OPCODE_SET_FLAG, QUERY_FLAG_IDN_FDEVICEINIT, NULL);
  803. if (!err)
  804. break;
  805. UFS_DBG_LOGD("error %d retrying\n", err);
  806. }
  807. if (err) {
  808. UFS_DBG_LOGD("setting fDeviceInit flag failed with error %d\n", err);
  809. goto out;
  810. }
  811. // get timeout tick
  812. timeout_tick = gpt4_time2tick_us (UFS_FDEVICEINIT_TIMEOUT_US);
  813. start_tick = gpt4_get_current_tick ();
  814. /* poll for max. UFS_FDEVICEINIT_TIMEOUT_US(5s) for fDeviceInit flag to clear */
  815. do {
  816. for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
  817. err = hba->query_flag(hba, UPIU_QUERY_OPCODE_READ_FLAG, QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
  818. if (!err)
  819. break;
  820. UFS_DBG_LOGD("error %d retrying\n", err);
  821. }
  822. } while (!err && flag_res && !gpt4_timeout_tick (start_tick, timeout_tick));
  823. if (err)
  824. UFS_DBG_LOGD("reading fDeviceInit flag failed with error %d\n", err);
  825. else if (flag_res) {
  826. UFS_DBG_LOGD("fDeviceInit was not cleared by the device\n");
  827. err = UFS_ERR_FDEVICEINIT_NOT_CLR;
  828. }
  829. out:
  830. return err;
  831. }
  832. int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel, u32 *mib_val, u8 peer)
  833. {
  834. int ret;
  835. struct uic_command uic_cmd = {0};
  836. uic_cmd.argument1 = attr_sel;
  837. uic_cmd.command = peer ? UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
  838. ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
  839. if (mib_val && !ret)
  840. *mib_val = uic_cmd.argument3;
  841. if (ret)
  842. UFS_DBG_LOGD("attr-id 0x%x error code %d\n", UIC_GET_ATTR_ID(attr_sel), ret);
  843. return ret;
  844. }
  845. /**
  846. * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
  847. * @hba: per adapter instance
  848. * @attr_sel: uic command argument1
  849. * @attr_set: attribute set type as uic command argument2
  850. * @mib_val: setting value as uic command argument3
  851. * @peer: indicate whether peer or local
  852. *
  853. * Returns 0 on success, non-zero value on failure
  854. */
  855. int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel, u8 attr_set, u32 mib_val, u8 peer)
  856. {
  857. struct uic_command uic_cmd = {0};
  858. int ret;
  859. uic_cmd.command = peer ? UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
  860. uic_cmd.argument1 = attr_sel;
  861. uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
  862. uic_cmd.argument3 = mib_val;
  863. ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
  864. if (ret)
  865. UFS_DBG_LOGE("[UFS] err: attr-id 0x%x val 0x%x error code %d (peer %d)\n", UIC_GET_ATTR_ID(attr_sel), mib_val, ret, peer);
  866. return ret;
  867. }
  868. /**
  869. * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
  870. * @hba: per-adapter instance
  871. */
  872. static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
  873. {
  874. struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
  875. if (hba->max_pwr_info.is_valid)
  876. return 0;
  877. pwr_info->pwr_tx = FAST_MODE;
  878. pwr_info->pwr_rx = FAST_MODE;
  879. pwr_info->hs_rate = PA_HS_MODE_B;
  880. /* Get the connected lane count */
  881. hba->dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
  882. &pwr_info->lane_rx);
  883. UFS_DBG_LOGD("[UFS] info: ufshcd_get_max_pwr_mode, lane_rx: %d\n", pwr_info->lane_rx); /* mtk debug */
  884. hba->dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
  885. &pwr_info->lane_tx);
  886. UFS_DBG_LOGD("[UFS] info: ufshcd_get_max_pwr_mode, lane_tx: %d\n", pwr_info->lane_tx); /* mtk debug */
  887. if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
  888. UFS_DBG_LOGD("invalid connected lanes value. rx=%d, tx=%d\n",
  889. pwr_info->lane_rx,
  890. pwr_info->lane_tx);
  891. return -1;
  892. }
  893. /*
  894. * First, get the maximum gears of HS speed.
  895. * If a zero value, it means there is no HSGEAR capability.
  896. * Then, get the maximum gears of PWM speed.
  897. */
  898. hba->dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
  899. if (!pwr_info->gear_rx) {
  900. hba->dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), &pwr_info->gear_rx);
  901. if (!pwr_info->gear_rx) {
  902. UFS_DBG_LOGD("invalid max pwm rx gear read = %d\n", pwr_info->gear_rx);
  903. return -1;
  904. }
  905. pwr_info->pwr_rx = SLOWAUTO_MODE;
  906. } else
  907. UFS_DBG_LOGD("[UFS] info: ufshcd_get_max_pwr_mode, gear_rx: %d\n", pwr_info->gear_rx); /* mtk debug */
  908. hba->dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
  909. &pwr_info->gear_tx);
  910. if (!pwr_info->gear_tx) {
  911. hba->dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
  912. &pwr_info->gear_tx);
  913. if (!pwr_info->gear_tx) {
  914. UFS_DBG_LOGD("invalid max pwm tx gear read = %d\n", pwr_info->gear_tx);
  915. return -1;
  916. }
  917. pwr_info->pwr_tx = SLOWAUTO_MODE;
  918. } else
  919. UFS_DBG_LOGD("[UFS] info: ufshcd_get_max_pwr_mode, gear_tx: %d\n", pwr_info->gear_tx); /* mtk debug */
  920. hba->max_pwr_info.is_valid = TRUE;
  921. return 0;
  922. }
  923. /**
  924. * ufshcd_get_upmcrs - Get the power mode change request status
  925. * @hba: Pointer to adapter instance
  926. *
  927. * This function gets the UPMCRS field of HCS register
  928. * Returns value of UPMCRS field
  929. */
  930. static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
  931. {
  932. return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
  933. }
  934. static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
  935. {
  936. u8 status;
  937. int ret;
  938. ret = __ufshcd_send_uic_cmd(hba, cmd);
  939. if (ret) {
  940. UFS_DBG_LOGD("pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n", cmd->command, cmd->argument3, ret);
  941. goto out;
  942. }
  943. ret = ufshcd_wait_for_uic_cmd(hba, cmd);
  944. if (ret) {
  945. UFS_DBG_LOGD("pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n", cmd->command, cmd->argument3, ret);
  946. goto out;
  947. }
  948. ret = ufshcd_wait_for_register(hba, REG_INTERRUPT_STATUS,
  949. UIC_POWER_MODE, UIC_POWER_MODE,
  950. 100, UIC_CMD_TIMEOUT);
  951. if (ret) {
  952. UFS_DBG_LOGE("[UFS] err: wait UIC_POWER_MODE interrupt timeout\n");
  953. goto out;
  954. }
  955. ufshcd_writel(hba, UIC_POWER_MODE, REG_INTERRUPT_STATUS);
  956. status = ufshcd_get_upmcrs(hba);
  957. if (status != PWR_LOCAL) {
  958. UFS_DBG_LOGE("[UFS] err: pwr ctrl cmd 0x%0x failed, host umpcrs:0x%x\n", cmd->command, status);
  959. ret = (status != PWR_OK) ? status : -1;
  960. }
  961. out:
  962. return ret;
  963. }
  964. static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
  965. {
  966. int ret;
  967. struct uic_command uic_cmd = {0};
  968. uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
  969. uic_cmd.command = UIC_CMD_DME_SET;
  970. uic_cmd.argument3 = mode;
  971. ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
  972. return ret;
  973. }
  974. static int ufshcd_dme_copy_attr(struct ufs_hba *hba,
  975. u32 attr_sel_dst, u8 peer_dst,
  976. u32 attr_sel_src, u8 peer_src)
  977. {
  978. int ret;
  979. u32 mib_val = 0;
  980. ret = ufshcd_dme_get_attr(hba, attr_sel_src, &mib_val, peer_src);
  981. if (ret)
  982. return ret;
  983. return ufshcd_dme_set_attr(hba, attr_sel_dst, ATTR_SET_NOR,
  984. mib_val, peer_dst);
  985. }
  986. static int ufshcd_setup_userdata(struct ufs_hba *hba)
  987. {
  988. int i;
  989. int err = 0;
  990. struct {
  991. u32 attr_userdata;
  992. u32 attr_timeout;
  993. } userdata_config[] = {
  994. {
  995. UIC_ARG_MIB(PA_PWRMODEUSERDATA0),
  996. UIC_ARG_MIB(DL_FC0PROTTIMEOUTVAL),
  997. },
  998. {
  999. UIC_ARG_MIB(PA_PWRMODEUSERDATA1),
  1000. UIC_ARG_MIB(DL_TC0REPLAYTIMEOUTVAL),
  1001. },
  1002. {
  1003. UIC_ARG_MIB(PA_PWRMODEUSERDATA2),
  1004. UIC_ARG_MIB(DL_AFC0REQTIMEOUTVAL),
  1005. },
  1006. };
  1007. for (i = 0; !err && (u32)i < UFS_ARRAY_SIZE(userdata_config); i++) {
  1008. err = ufshcd_dme_copy_attr(hba,
  1009. userdata_config[i].attr_userdata, DME_LOCAL,
  1010. userdata_config[i].attr_timeout, DME_LOCAL);
  1011. }
  1012. return err;
  1013. }
  1014. static u32 get_termination(u32 pwr_mode)
  1015. {
  1016. switch (pwr_mode) {
  1017. case FASTAUTO_MODE:
  1018. case FAST_MODE:
  1019. return 1;
  1020. case SLOWAUTO_MODE:
  1021. case SLOW_MODE:
  1022. return 0;
  1023. }
  1024. return 0;
  1025. }
  1026. static int pwr_mode_change(struct ufs_hba *hba,
  1027. u32 tx_pwr_mode, u32 rx_pwr_mode,
  1028. u32 tx_gear, u32 rx_gear,
  1029. u32 tx_lanes, u32 rx_lanes, u32 hs_series)
  1030. {
  1031. u32 tx_term = get_termination(tx_pwr_mode);
  1032. u32 rx_term = get_termination(rx_pwr_mode);
  1033. struct uic_command cmds[7];
  1034. int ret;
  1035. ret = ufshcd_dme_copy_attr(hba,
  1036. UIC_ARG_MIB(PA_TXTRAILINGCLOCKS), DME_LOCAL,
  1037. UIC_ARG_MIB(PA_MINRXTRAILINGCLOCKS), DME_PEER);
  1038. if (ret)
  1039. return ret;
  1040. ret = ufshcd_dme_copy_attr(hba,
  1041. UIC_ARG_MIB(PA_TXTRAILINGCLOCKS), DME_PEER,
  1042. UIC_ARG_MIB(PA_MINRXTRAILINGCLOCKS), DME_LOCAL);
  1043. if (ret)
  1044. return ret;
  1045. cmds[0] = UIC_CMD_DME_SET(PA_TXTERMINATION, 0, tx_term);
  1046. cmds[1] = UIC_CMD_DME_SET(PA_RXTERMINATION, 0, rx_term);
  1047. cmds[2] = UIC_CMD_DME_SET(PA_TXGEAR, 0, tx_gear);
  1048. cmds[3] = UIC_CMD_DME_SET(PA_RXGEAR, 0, rx_gear);
  1049. cmds[4] = UIC_CMD_DME_SET(PA_ACTIVETXDATALANES, 0, tx_lanes);
  1050. cmds[5] = UIC_CMD_DME_SET(PA_ACTIVERXDATALANES, 0, rx_lanes);
  1051. cmds[6] = UIC_CMD_DME_SET(PA_HSSERIES, 0, hs_series);
  1052. return ufshcd_uic_cmd_run(hba, cmds, UFS_ARRAY_SIZE(cmds));
  1053. }
  1054. int ufshcd_legacy_pwr_mode_change(struct ufs_hba *hba, u32 tx_pwr_mode,
  1055. u32 rx_pwr_mode, u32 tx_gear, u32 rx_gear,
  1056. u32 hs_series, u32 tx_lanes, u32 rx_lanes)
  1057. {
  1058. int err;
  1059. err = ufshcd_setup_userdata(hba);
  1060. if (err)
  1061. return err;
  1062. err = pwr_mode_change(hba, tx_pwr_mode, rx_pwr_mode,
  1063. tx_gear, rx_gear, tx_lanes, rx_lanes, hs_series);
  1064. if (err)
  1065. return err;
  1066. err = ufshcd_uic_change_pwr_mode(hba, rx_pwr_mode << 4 | tx_pwr_mode);
  1067. return err;
  1068. }
  1069. /**
  1070. * ufshcd_config_pwr_mode - configure a new power mode
  1071. * @hba: per-adapter instance
  1072. * @desired_pwr_mode: desired power configuration
  1073. */
  1074. int ufshcd_config_pwr_mode(struct ufs_hba *hba,
  1075. struct ufs_pa_layer_attr *desired_pwr_mode)
  1076. {
  1077. struct ufs_pa_layer_attr final_params = { 0 };
  1078. int ret;
  1079. ufs_aio_pre_pwr_change(hba, desired_pwr_mode, &final_params);
  1080. ret = ufshcd_legacy_pwr_mode_change(hba,
  1081. final_params.pwr_tx, final_params.pwr_rx,
  1082. final_params.gear_tx, final_params.gear_rx,
  1083. final_params.hs_rate,
  1084. final_params.lane_tx, final_params.lane_rx);
  1085. return ret;
  1086. }
  1087. int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
  1088. {
  1089. struct ufshcd_sg_entry *prd_table;
  1090. struct ufs_aio_scsi_cmd *cmd;
  1091. int sg_size = UFS_AIO_MAX_SIZE_PER_SG_SEGMENT;
  1092. int sg_segments;
  1093. int i;
  1094. u32 residual;
  1095. ufs_paddr_t buf_dma_addr;
  1096. cmd = lrbp->cmd;
  1097. if (cmd->exp_len == 0)
  1098. sg_segments = 0;
  1099. else
  1100. sg_segments = ((cmd->exp_len + sg_size - 1) / sg_size);
  1101. if (sg_segments) {
  1102. lrbp->utr_descriptor_ptr->prd_table_length = cpu_to_le16((u16) (sg_segments));
  1103. prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
  1104. buf_dma_addr = (ufs_paddr_t)cmd->data_buf;
  1105. residual = cmd->exp_len;
  1106. for (i = 0; i < sg_segments; i++) {
  1107. /* size should be 32-bit aligned, this is forced by spec */
  1108. if (residual > (u32)sg_size)
  1109. prd_table[i].size = cpu_to_le32((u32)sg_size - 1);
  1110. else
  1111. prd_table[i].size = cpu_to_le32((u32)residual - 1);
  1112. /* addr should be 32-bit aligned, this is forced by spec */
  1113. prd_table[i].base_addr = cpu_to_le32(lower_32_bits(buf_dma_addr));
  1114. prd_table[i].upper_addr = cpu_to_le32(upper_32_bits(buf_dma_addr));
  1115. buf_dma_addr += sg_size;
  1116. residual -= sg_size;
  1117. }
  1118. /* invalidate data buffer if existed */
  1119. ufs_aio_dma_map((ufs_vaddr_t)cmd->data_buf, cmd->exp_len, lrbp->cmd->dir);
  1120. } else {
  1121. lrbp->utr_descriptor_ptr->prd_table_length = 0;
  1122. }
  1123. return 0;
  1124. }
  1125. /**
  1126. * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
  1127. * @lrb: pointer to local command reference block
  1128. *
  1129. * This function is used to get the OCS field from UTRD
  1130. * Returns the OCS field in the UTRD
  1131. */
  1132. static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
  1133. {
  1134. return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
  1135. }
  1136. /**
  1137. * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
  1138. * @ucd_rsp_ptr: pointer to response UPIU
  1139. *
  1140. * This function gets the response status and scsi_status from response UPIU
  1141. * Returns the response result code.
  1142. */
  1143. static inline int
  1144. ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
  1145. {
  1146. return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
  1147. }
  1148. static int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
  1149. {
  1150. struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
  1151. memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
  1152. /* Get the descriptor */
  1153. if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
  1154. u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
  1155. GENERAL_UPIU_REQUEST_SIZE;
  1156. u16 resp_len;
  1157. u16 buf_len;
  1158. /* data segment length */
  1159. resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
  1160. MASK_QUERY_DATA_SEG_LEN;
  1161. buf_len = be16_to_cpu(
  1162. hba->dev_cmd.query.request.upiu_req.length);
  1163. if (buf_len >= resp_len) {
  1164. /*
  1165. * ufshcd_wait_command() only invalidates length of "strcut utp_upiu_rsp".
  1166. * We need to invalidate more data range for Read Descriptor operation.
  1167. */
  1168. ufs_aio_dma_unmap((U32)descp, resp_len, DMA_FROM_DEVICE);
  1169. memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
  1170. } else {
  1171. UFS_DBG_LOGD("Response size is bigger than buffer");
  1172. return -1;
  1173. }
  1174. }
  1175. return 0;
  1176. }
  1177. static inline int ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
  1178. {
  1179. return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
  1180. }
  1181. static int ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
  1182. {
  1183. int err = 0;
  1184. int resp;
  1185. resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
  1186. switch (resp) {
  1187. case UPIU_TRANSACTION_REJECT_UPIU:
  1188. /* TODO: handle Reject UPIU Response */
  1189. err = -1;
  1190. UFS_DBG_LOGD("Reject UPIU not fully implemented\n");
  1191. break;
  1192. case UPIU_TRANSACTION_QUERY_RSP:
  1193. hba->dev_cmd.query.response.response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >> UPIU_RSP_CODE_OFFSET;
  1194. err = hba->dev_cmd.query.response.response ;
  1195. if (!err)
  1196. err = ufshcd_copy_query_response(hba, lrbp);
  1197. break;
  1198. case UPIU_TRANSACTION_NOP_IN:
  1199. if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
  1200. err = -1;
  1201. UFS_DBG_LOGD("unexpected response %x\n", resp);
  1202. }
  1203. break;
  1204. default:
  1205. err = -1;
  1206. UFS_DBG_LOGD("Invalid device management cmd response: %x\n", resp);
  1207. break;
  1208. }
  1209. return err;
  1210. }
  1211. int ufshcd_wait_command(struct ufs_hba *hba, struct ufshcd_lrb *lrbp, u32 timeout_ms)
  1212. {
  1213. u8 ocs;
  1214. u16 resp;
  1215. int ret = UFS_ERR_NONE;
  1216. u32 value;
  1217. u32 tag_mask = 1 << lrbp->task_tag;
  1218. u32 tr_doorbell;
  1219. u32 intr_status;
  1220. #ifdef MTK_UFS_DRV_DA
  1221. u32 time_start;
  1222. u32 time_timeout;
  1223. u32 wait_loop = 0;
  1224. #else
  1225. unsigned long elapsed_ms = 0;
  1226. #endif
  1227. #if defined(MTK_UFS_DRV_DA)
  1228. time_start = gpt4_get_current_tick();
  1229. time_timeout = gpt4_time2tick_ms(timeout_ms);
  1230. #endif
  1231. #if defined(MTK_UFS_DRV_DA) || defined(MTK_UFS_DRV_LK)
  1232. if (hba->drv_status & UFS_DRV_STS_DMA_WAIT_INTERRUPT) {
  1233. /* Unmask interrupt now and wait */
  1234. mt_irq_unmask(hba->irq);
  1235. ret = event_wait_timeout(&ufshcd_intr_event, timeout_ms);
  1236. if (ret != 0)
  1237. UFS_DBG_LOGE("[UFS]: failed to get event timeout:%d\n", timeout_ms);
  1238. /* Mask interrupt */
  1239. mt_irq_mask(hba->irq);
  1240. } else
  1241. #endif
  1242. do {
  1243. tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
  1244. if (!(tag_mask & tr_doorbell)) {
  1245. /* clear_bit(lrbp->task_tag, &hba->outstanding_reqs); */
  1246. hba->outstanding_reqs &= ~(1 << lrbp->task_tag);
  1247. break;
  1248. }
  1249. #ifdef MTK_UFS_DRV_DA
  1250. /* ensure to use timeout detector in consideration of wrap around case in gpt register */
  1251. if (gpt4_timeout_tick(time_start, time_timeout)) /* timeout, stop waiting */
  1252. break;
  1253. wait_loop++;
  1254. if (wait_loop && (wait_loop % 10000) == 0)
  1255. UFS_DBG_LOGD("[UFS] info: waiting for cmd done, loop %d ...\n", wait_loop);
  1256. #else
  1257. msleep(1);
  1258. elapsed_ms += 1;
  1259. if (elapsed_ms > timeout_ms)
  1260. break;
  1261. #endif
  1262. } while (1);
  1263. /* clear_bit(lrbp->task_tag, &hba->lrb_in_use); // need to clear lrb_in_use for polling case, otherwise no one can use this tag in the future. */
  1264. hba->lrb_in_use &= ~(1 << lrbp->task_tag);
  1265. tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
  1266. if (tag_mask & tr_doorbell) {
  1267. UFS_DBG_LOGE("[UFS] err: Query Request timeout.timeout_ms:%d\n", timeout_ms);
  1268. ret = UFS_ERR_TIMEOUT_QUERY_REQUEST;
  1269. goto out;
  1270. }
  1271. /* invalid data buffer */
  1272. if (lrbp->cmd)
  1273. ufs_aio_dma_unmap((unsigned long)lrbp->cmd->data_buf, lrbp->cmd->exp_len, lrbp->cmd->dir);
  1274. /* dummy, just for pairing with ufs_aio_dma_map(UTRD) and ufs_aio_dma_map(PRDT) */
  1275. /* ufs_aio_dma_unmap((unsigned long)lrbp->ucd_req_ptr, sizeof(struct utp_upiu_req), DMA_TO_DEVICE); */
  1276. /* ufs_aio_dma_unmap((unsigned long)hba->lrb->ucd_prdt_ptr, sizeof(struct ufs_aio_sg_entry) * UFS_AIO_MAX_SG_SEGMENTS, DMA_TO_DEVICE); */
  1277. /* check OCS */
  1278. /* invalid UTRD cache */
  1279. ufs_aio_dma_unmap((unsigned long)lrbp->utr_descriptor_ptr, sizeof(struct utp_transfer_req_desc), DMA_BIDIRECTIONAL);
  1280. #if defined(MTK_UFS_DRV_DA) || defined(MTK_UFS_DRV_LK)
  1281. if (hba->drv_status & UFS_DRV_STS_DMA_WAIT_INTERRUPT)
  1282. intr_status = ufshcd_intr_status;
  1283. else
  1284. #endif
  1285. intr_status = ufs_aio_readl(hba, REG_INTERRUPT_STATUS);
  1286. /* check interrupt status if any error happens */
  1287. if (intr_status & UFSHCD_ERROR_MASK) {
  1288. UFS_DBG_LOGE("[UFS] ERR! intr_status: 0x%x\n", intr_status);
  1289. if (intr_status & UIC_ERROR) {
  1290. UFS_DBG_LOGE("[UFS] REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER: 0x%x\n", ufs_aio_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER));
  1291. UFS_DBG_LOGE("[UFS] REG_UIC_ERROR_CODE_DATA_LINK_LAYER: 0x%x\n", ufs_aio_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER));
  1292. UFS_DBG_LOGE("[UFS] REG_UIC_ERROR_CODE_NETWORK_LAYER: 0x%x\n", ufs_aio_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER));
  1293. UFS_DBG_LOGE("[UFS] REG_UIC_ERROR_CODE_TRANSPORT_LAYER: 0x%x\n", ufs_aio_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER));
  1294. UFS_DBG_LOGE("[UFS] REG_UIC_ERROR_CODE_DME: 0x%x\n", ufs_aio_readl(hba, REG_UIC_ERROR_CODE_DME));
  1295. }
  1296. }
  1297. ocs = ufshcd_get_tr_ocs(lrbp);
  1298. if (ocs != OCS_SUCCESS) {
  1299. UFS_DBG_LOGE("[UFS] err: OCS error = %x, T:%d\n", ocs, lrbp->task_tag);
  1300. #if (UFS_DBG_LVL <= UFS_DBG_LVL_DEBUG)
  1301. {
  1302. u8 cnt;
  1303. value = ufshcd_readl(hba, REG_UFS_MTK_OCS_ERR_STATUS);
  1304. UFS_DBG_LOGD("[UFS] err: OCS err status reg = %x\n", value);
  1305. for (cnt = 0; g_ufs_ocs_error_str[cnt].err != 0xFF; cnt++) {
  1306. if (value & (1 << cnt))
  1307. UFS_DBG_LOGD("[UFS] err: %s\n", g_ufs_ocs_error_str[cnt].name);
  1308. }
  1309. }
  1310. #endif /* UFS_DBG_LVL_DEBUG */
  1311. ret = UFS_ERR_OCS_ERROR;
  1312. goto out;
  1313. }
  1314. /* check response code */
  1315. /* invalid RESPONSE UPIU cache
  1316. *
  1317. * NOTE: Here we only invalidate length of "strcut utp_upiu_rsp". For Response UPIU with
  1318. * longer data size (e.g., Read Descriptor), need to invalidate more data.
  1319. * Please see ufshcd_copy_query_response().
  1320. */
  1321. ufs_aio_dma_unmap((U32)lrbp->ucd_rsp_ptr, sizeof(struct utp_upiu_rsp), DMA_FROM_DEVICE);
  1322. if (UTP_CMD_TYPE_DEV_MANAGE == lrbp->command_type) /* for device management commands */
  1323. return ufshcd_dev_cmd_completion(hba, lrbp);
  1324. else { /* for SCSI commands */
  1325. resp = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
  1326. if (resp & MASK_TASK_RESPONSE) {
  1327. value = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
  1328. if (UPIU_TRANSACTION_RESPONSE == value)
  1329. ufs_mtk_dump_asc_ascq(hba, lrbp->ucd_rsp_ptr->sr.sense_data[12], lrbp->ucd_rsp_ptr->sr.sense_data[13]);
  1330. /* sense key is UNIT_ATTENTION, will retry */
  1331. if (lrbp->cmd && lrbp->cmd->cmd_data[0] == TEST_UNIT_READY && (lrbp->ucd_rsp_ptr->sr.sense_data[2] &0xF) == UNIT_ATTENTION)
  1332. ret = UFS_TEST_UNIT_RDY_ERR;
  1333. else {
  1334. ret = UFS_ERR_TASK_RESP_ERROR;
  1335. UFS_DBG_LOGE("[UFS] err: task response error = %x\n", (resp & MASK_TASK_RESPONSE) >> 8);
  1336. }
  1337. goto out;
  1338. }
  1339. }
  1340. out:
  1341. if (lrbp->cmd)
  1342. lrbp->cmd = NULL;
  1343. UFS_DBG_LOGV("[UFS] info: CMD done\n");
  1344. return ret;
  1345. }
  1346. void ufs_aio_crypto_cal_dun(u32 alg_id, u32 lba, u32 *dunl, u32 *dunu)
  1347. {
  1348. if (UFS_CRYPTO_ALGO_BITLOCKER_AES_CBC != alg_id) {
  1349. *dunl = lba;
  1350. *dunu = 0;
  1351. } else { /* bitlocker dun use byte address */
  1352. *dunl = (lba & 0x7FFFF) << 12; /* byte address for lower 32 bit */
  1353. *dunu = (lba >> (32-12)) << 12; /* byte address for higher 32 bit */
  1354. }
  1355. }
  1356. static void ufshcd_queuecommand_timeout(struct ufs_aio_scsi_cmd *cmd, int *timeout)
  1357. {
  1358. if(cmd->cmd_data[0] == UNMAP)
  1359. *timeout = UTP_UNMAP_TIMEOUT_MS;
  1360. else
  1361. *timeout = UTP_TRANSFER_REQ_TIMEOUT_MS;
  1362. }
  1363. int ufshcd_queuecommand(struct ufs_hba *hba, struct ufs_aio_scsi_cmd *cmd)
  1364. {
  1365. struct ufshcd_lrb *lrbp;
  1366. int tag, lun;
  1367. int err = 0;
  1368. #if defined(UFS_CFG_DEBUG)
  1369. u32 blk_cnt;
  1370. #endif
  1371. #if defined(UFS_CFG_DEBUG) || defined(UFS_CFG_CRYPTO)
  1372. u32 lba;
  1373. #endif
  1374. #ifdef UFS_CFG_CRYPTO
  1375. u32 dunl, dunu;
  1376. #endif
  1377. int timeout = UTP_TRANSFER_REQ_TIMEOUT_MS;
  1378. tag = cmd->tag;
  1379. lun = cmd->lun;
  1380. #if defined(UFS_CFG_DEBUG)
  1381. lba = cmd->cmd_data[5] | (cmd->cmd_data[4] << 8) | (cmd->cmd_data[3] << 16) | (cmd->cmd_data[2] << 24);
  1382. blk_cnt = cmd->cmd_data[8] | (cmd->cmd_data[7] << 8);
  1383. UFS_DBG_LOGD("[UFS] info: QCMD,L:%x,T:%d,0x%x,A:0x%x,C:%d\n", lun, tag, cmd->cmd_data[0],lba, blk_cnt);
  1384. #endif
  1385. #ifdef UFS_CFG_SINGLE_COMMAND
  1386. lrbp = &hba->lrb[0];
  1387. #else
  1388. lrbp = &hba->lrb[tag];
  1389. #endif
  1390. #ifdef UFS_CFG_CRYPTO
  1391. if (ufs_crypto_rw_en) {
  1392. lba = ((cmd->cmd_data[2]) << 24) | ((cmd->cmd_data[3]) << 16) |
  1393. ((cmd->cmd_data[4]) << 8) | (cmd->cmd_data[5]);
  1394. ufs_aio_crypto_cal_dun(UFS_CRYPTO_ALGO_ESSIV_AES_CBC, lba, &dunl, &dunu);
  1395. }
  1396. #endif
  1397. lrbp->cmd = cmd;
  1398. /* lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE; */
  1399. /* lrbp->sense_buffer = hba->sense_buf_base_addr[tag]; */
  1400. lrbp->task_tag = tag;
  1401. lrbp->lun = lun;
  1402. lrbp->intr_cmd = TRUE; /* in AIO driver, use interrupt for every commands. */
  1403. lrbp->command_type = UTP_CMD_TYPE_SCSI;
  1404. #ifdef UFS_CFG_CRYPTO
  1405. if (ufs_crypto_rw_en) {
  1406. lrbp->crypto_cfgid = 0;
  1407. lrbp->crypto_dunl = dunl;
  1408. lrbp->crypto_dunu = dunu;
  1409. lrbp->crypto_en = 1;
  1410. } else
  1411. lrbp->crypto_en = 0;
  1412. #endif
  1413. /* form UPIU before issuing the command */
  1414. ufshcd_compose_upiu(hba, lrbp);
  1415. err = ufshcd_map_sg(hba, lrbp);
  1416. if (err) {
  1417. UFS_DBG_LOGD("Err: ufshcd_map_sg_ut fails\n");
  1418. lrbp->cmd = NULL;
  1419. /* clear_bit(tag, &hba->lrb_in_use); */
  1420. hba->lrb_in_use &= ~(1 << tag);
  1421. goto out;
  1422. }
  1423. /* issue command to the controller */
  1424. ufshcd_send_command(hba, tag);
  1425. /* Setting utp transfer req command timeout according to different cmd */
  1426. ufshcd_queuecommand_timeout(cmd, &timeout);
  1427. err = ufshcd_wait_command(hba, lrbp, timeout);
  1428. out:
  1429. return err;
  1430. }
  1431. #ifdef MTK_UFS_DRV_DA
  1432. int ufs_aio_get_ref_clk(struct ufs_hba *hba, u32 *ref)
  1433. {
  1434. int ret;
  1435. /*
  1436. * Reference Clock Frequency value
  1437. * 0h: 19.2MHz
  1438. * 1h: 26MHz
  1439. * 2h: 38.4MHz
  1440. * 3h: 52MHz
  1441. */
  1442. ret = hba->query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR, ATTR_B_REF_CLK_FREQ, 0, 0, ref);
  1443. if (ret != 0)
  1444. UFS_DBG_LOGD("[UFS] err: %s error: %d\n", __func__, ret);
  1445. return ret;
  1446. }
  1447. int ufs_aio_set_ref_clk(struct ufs_hba *hba, u32 ref)
  1448. {
  1449. int ret;
  1450. /*
  1451. * Reference Clock Frequency value
  1452. * 0h: 19.2MHz
  1453. * 1h: 26MHz
  1454. * 2h: 38.4MHz
  1455. * 3h: 52MHz
  1456. */
  1457. ret = hba->query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, ATTR_B_REF_CLK_FREQ, 0, 0, &ref);
  1458. if (ret != 0)
  1459. UFS_DBG_LOGD("[UFS] err: %s error: %d\n", __func__, ret);
  1460. else
  1461. UFS_DBG_LOGD("[UFS] info: %s %d done\n", __func__, ref);
  1462. return ret;
  1463. }
  1464. __WEAK unsigned int config_TW_size_GB(void)
  1465. {
  1466. return 0xffffffff;
  1467. }
  1468. __WEAK unsigned int config_TW_no_reduction(void)
  1469. {
  1470. return 1;
  1471. }
  1472. __WEAK unsigned int config_HPB_size_GB(void)
  1473. {
  1474. return 0xffffffff;
  1475. }
  1476. static void ufshcd_init_custom_info(struct ufs_hba *hba)
  1477. {
  1478. /* Already set done and not changed by UI */
  1479. if (hba->custom_info.custom_flag & CUSTOM_SET_DONE)
  1480. return;
  1481. /*
  1482. * Set to custom value in DA if flag not set.
  1483. * Set to UI config in flag set.
  1484. */
  1485. if ((hba->custom_info.custom_flag & CUSTOM_FORCE_PROVISION) == 0)
  1486. hba->custom_info.force_provision = FALSE;
  1487. if ((hba->custom_info.custom_flag & CUSTOM_TW_GB) == 0)
  1488. hba->custom_info.tw_size_gb = config_TW_size_GB();
  1489. if ((hba->custom_info.custom_flag & CUSTOM_TW_NO_RED) == 0)
  1490. hba->custom_info.tw_no_red = config_TW_no_reduction();
  1491. if ((hba->custom_info.custom_flag & CUSTOM_HPB_REGION_COUNT) == 0)
  1492. hba->custom_info.hpb_size_gb = config_HPB_size_GB();
  1493. /* Set custom info done flag */
  1494. hba->custom_info.custom_flag |= CUSTOM_SET_DONE;
  1495. }
  1496. #endif
  1497. static int ufshcd_probe_hba(struct ufs_hba *hba)
  1498. {
  1499. int ret;
  1500. #ifdef MTK_UFS_DRV_DA
  1501. u32 ref_clk;
  1502. #endif
  1503. ret = ufshcd_link_startup(hba);
  1504. if (ret)
  1505. goto out;
  1506. ufshcd_init_pwr_info(hba);
  1507. ret = hba->nopin_nopout(hba);
  1508. if (ret)
  1509. goto out;
  1510. #if defined(MTK_UFS_DRV_LK)
  1511. PROFILING_START("UFS device init");
  1512. #endif
  1513. ret = ufshcd_complete_dev_init(hba);
  1514. #if defined(MTK_UFS_DRV_LK)
  1515. PROFILING_END();
  1516. #endif
  1517. if (ret)
  1518. goto out;
  1519. /* Init check for device descriptor sizes */
  1520. ufshcd_init_desc_sizes(hba);
  1521. /* get quirks according to Device Descritpor contents */
  1522. ufs_advertise_fixup_device(hba);
  1523. #ifdef MTK_UFS_DRV_DA
  1524. ufshcd_init_custom_info(hba);
  1525. /* check 1. check if bootable UFS */
  1526. /* get device infomation by reading Device Descriptor */
  1527. ufs_aio_get_device_info(hba);
  1528. ret = ufs_aio_check_lu_cfg(hba);
  1529. /* new UFS or invalid LU configuration is found, start (re-)configuration flow */
  1530. if ((hba->dev_info.bootable == 0) ||
  1531. (ret == UFS_ERR_INVALID_LU_CONFIGURATION) ||
  1532. (hba->custom_info.force_provision == TRUE)) {
  1533. hba->custom_info.force_provision = FALSE;
  1534. return ufs_aio_prepare_new_ufs(hba);
  1535. }
  1536. UFS_DBG_LOGI("[UFS] info: LU Configuration Check OK. Bootable UFS\n");
  1537. /* check 2. check if Boot LU is configured as expectation */
  1538. /* Boot LU 1/2 shall set in LU0/1 */
  1539. ret = ufs_aio_read_unit_desc_cfg_param(hba);
  1540. if (!(hba->unit_desc_cfg_param[0].b_lu_enable &&
  1541. hba->unit_desc_cfg_param[0].b_boot_lun_id == 1 &&
  1542. hba->unit_desc_cfg_param[1].b_lu_enable &&
  1543. hba->unit_desc_cfg_param[1].b_boot_lun_id == 2))
  1544. return ufs_aio_prepare_new_ufs(hba);
  1545. UFS_DBG_LOGI("[UFS] info: Boot LU Configuration Check OK. Active Boot LU: LU0\n");
  1546. #else
  1547. ufs_aio_check_lu_cfg(hba);
  1548. #endif /* MTK_UFS_DRV_DA */
  1549. ufs_aio_test_unit_ready_all_device(hba);
  1550. /* ufshcd_force_reset_auto_bkops(hba); */
  1551. #ifdef MTK_UFS_DRV_DA
  1552. /*
  1553. * Change device to 26MHz
  1554. * Need check first. If not, some old device cannot write and return F8
  1555. * as "Parameter already written". which cause ufs init fail.
  1556. */
  1557. ret = ufs_aio_get_ref_clk(hba, &ref_clk);
  1558. if ((ret == 0) && (ref_clk != 1)) {
  1559. ret = ufs_aio_set_ref_clk(hba, 1);
  1560. if (ret) {
  1561. UFS_DBG_LOGE("[UFS] Set device 26Mhz fail! %d\n", ret);
  1562. goto out;
  1563. }
  1564. }
  1565. #endif
  1566. #if !defined(MTK_UFS_DRV_CTP)
  1567. if (ufshcd_get_max_pwr_mode(hba))
  1568. UFS_DBG_LOGE("[UFS] Failed getting max supported power mode\n");
  1569. else {
  1570. ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
  1571. if (ret) {
  1572. UFS_DBG_LOGE("Failed setting power mode, err = %d\n", ret);
  1573. /* ignore power mode change error to let UFS init go through anyway */
  1574. ret = 0;
  1575. }
  1576. }
  1577. #endif
  1578. out:
  1579. return ret;
  1580. }
  1581. /**
  1582. * ufshcd_init - Driver initialization routine
  1583. * Returns 0 on success, non-zero value on failure
  1584. */
  1585. int ufshcd_init(void)
  1586. {
  1587. int err;
  1588. int retry_init_cnt = 0;
  1589. struct ufs_hba *hba = &g_ufs_hba;
  1590. UFS_DBG_LOGD("[UFS] memory (ufs_ucdl): 0x%x\n", (u32)&ufs_ucdl[0]);
  1591. UFS_DBG_LOGD("[UFS] memory (ufs_lrb): 0x%x\n", (u32)&ufs_lrb[0]);
  1592. ufs_get_platform_data();
  1593. hba->hci_base = ufs_platform.hci_base;
  1594. hba->pericfg_base = ufs_platform.pericfg_base;
  1595. hba->mphy_base = ufs_platform.mphy_base;
  1596. /* get hba capabilities */
  1597. ufshcd_get_host_capabilities(hba);
  1598. /* init power */
  1599. ufshcd_power(hba, TRUE);
  1600. /* init clock */
  1601. ufshcd_clock(hba, TRUE);
  1602. ufs_aio_advertise_hci_quirks(hba);
  1603. /* allocate memory for hba memory space */
  1604. err = ufshcd_memory_alloc(hba);
  1605. if (err) {
  1606. UFS_DBG_LOGD("Memory allocation failed\n");
  1607. goto out_error;
  1608. }
  1609. /* Configure LRB */
  1610. ufshcd_host_memory_configure(hba);
  1611. ufshcd_init_reinit_host:
  1612. /* Host controller enable */
  1613. err = ufshcd_hba_enable(hba);
  1614. if (err) {
  1615. UFS_DBG_LOGE("[UFS] err: Host controller enable failed\n");
  1616. goto out_error;
  1617. }
  1618. err = ufshcd_probe_hba(hba);
  1619. if (UFS_ERR_NEED_REINIT_HOST == err) {
  1620. retry_init_cnt++;
  1621. if (retry_init_cnt > 3) {
  1622. UFS_DBG_LOGE("[UFS] err: retry number exceeded (reinit host)\n");
  1623. goto out_error;
  1624. }
  1625. goto ufshcd_init_reinit_host;
  1626. }
  1627. if (err) {
  1628. UFS_DBG_LOGE("[UFS] err: ufshcd_probe_hba failed\n");
  1629. goto out_error;
  1630. }
  1631. #if defined(MTK_UFS_DRV_DA)
  1632. if (sysob_runtime_params.flag & INT_MODE_ENABLE) {
  1633. if (!ufshcd_irq_init(hba))
  1634. hba->drv_status |= UFS_DRV_STS_DMA_WAIT_INTERRUPT;
  1635. }
  1636. #elif defined(MTK_UFS_DRV_LK)
  1637. if (!ufshcd_irq_init(hba))
  1638. hba->drv_status |= UFS_DRV_STS_DMA_WAIT_INTERRUPT;
  1639. #endif
  1640. return 0;
  1641. out_error:
  1642. return err;
  1643. }
  1644. static int ufshcd_query_descriptor(struct ufs_hba *hba,
  1645. enum query_opcode opcode, enum desc_idn idn, u8 index,
  1646. u8 selector, u8 *desc_buf, u32 *buf_len)
  1647. {
  1648. struct ufs_query_req *request = NULL;
  1649. struct ufs_query_res *response = NULL;
  1650. int err;
  1651. BUG_ON(!hba);
  1652. if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
  1653. UFS_DBG_LOGD("descriptor buffer size (%d) is out of range\n", *buf_len);
  1654. err = -1;
  1655. goto out;
  1656. }
  1657. if (!desc_buf) {
  1658. UFS_DBG_LOGD("descriptor buffer required for opcode 0x%x\n", opcode);
  1659. err = -1;
  1660. goto out;
  1661. }
  1662. ufshcd_init_query(hba, &request, &response, opcode, idn, index, selector);
  1663. request->upiu_req.length = cpu_to_be16(*buf_len);
  1664. hba->dev_cmd.query.descriptor = desc_buf;
  1665. switch (opcode) {
  1666. case UPIU_QUERY_OPCODE_READ_DESC:
  1667. request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
  1668. break;
  1669. case UPIU_QUERY_OPCODE_WRITE_DESC:
  1670. request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
  1671. break;
  1672. default:
  1673. UFS_DBG_LOGE("Expected query descriptor opcode but got = 0x%x\n", opcode);
  1674. err = -1;
  1675. goto out;
  1676. }
  1677. err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
  1678. if (err) {
  1679. UFS_DBG_LOGE("opcode 0x%x for idn %d failed, err = %d\n", opcode, idn, err);
  1680. goto out;
  1681. }
  1682. *buf_len = be16_to_cpu(response->upiu_res.length);
  1683. hba->dev_cmd.query.descriptor = NULL;
  1684. out:
  1685. return err;
  1686. }
  1687. static int ufshcd_read_desc_param(struct ufs_hba *hba,
  1688. enum desc_idn desc_id,
  1689. int desc_index,
  1690. u8 selector,
  1691. u8 *param_read_buf,
  1692. u32 param_size)
  1693. {
  1694. u32 desc_len;
  1695. u8 *desc_buf;
  1696. int ret;
  1697. if (desc_id >= QUERY_DESC_IDN_MAX)
  1698. return -1;
  1699. desc_len = ufs_query_desc_max_size[desc_id];
  1700. if (desc_len > param_size)
  1701. desc_len = param_size;
  1702. desc_buf = param_read_buf;
  1703. ret = ufshcd_query_descriptor(hba, UPIU_QUERY_OPCODE_READ_DESC,
  1704. desc_id, desc_index, selector, desc_buf,
  1705. &desc_len);
  1706. if (ret ||
  1707. (desc_len > ufs_query_desc_max_size[desc_id]) ||
  1708. (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id)) {
  1709. UFS_DBG_LOGE("[UFS] failed reading descriptor. desc_id %d desc_len %d ret %d\n", desc_id, desc_len, ret);
  1710. if (!ret)
  1711. ret = UFS_ERR_INVALID_DESCRIPTOR;
  1712. goto out;
  1713. }
  1714. out:
  1715. return ret;
  1716. }
  1717. static int ufshcd_read_desc_length(struct ufs_hba *hba,
  1718. enum desc_idn desc_id,
  1719. int desc_index,
  1720. u8 selector,
  1721. u32 *desc_length)
  1722. {
  1723. int ret;
  1724. u8 header[QUERY_DESC_HDR_SIZE];
  1725. u32 header_len = QUERY_DESC_HDR_SIZE;
  1726. if (desc_id >= QUERY_DESC_IDN_MAX)
  1727. ret = -1;
  1728. ret = ufshcd_query_descriptor(hba, UPIU_QUERY_OPCODE_READ_DESC,
  1729. desc_id, desc_index, selector, header,
  1730. &header_len);
  1731. if (ret) {
  1732. UFS_DBG_LOGE("%s: Failed to get descriptor header id %d\n",
  1733. __func__, desc_id);
  1734. return ret;
  1735. } else if (desc_id != header[QUERY_DESC_DESC_TYPE_OFFSET]) {
  1736. UFS_DBG_LOGE("%s: descriptor header id %d and desc_id %d mismatch\n",
  1737. __func__, header[QUERY_DESC_DESC_TYPE_OFFSET],
  1738. desc_id);
  1739. ret = -1;
  1740. }
  1741. *desc_length = header[QUERY_DESC_LENGTH_OFFSET];
  1742. return ret;
  1743. }
  1744. static void ufshcd_init_desc_sizes(struct ufs_hba *hba)
  1745. {
  1746. int err;
  1747. static int init_done = 0;
  1748. if (init_done)
  1749. return;
  1750. err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_DEVICE, 0, 0,
  1751. &ufs_query_desc_max_size[QUERY_DESC_IDN_DEVICE]);
  1752. if (err)
  1753. ufs_query_desc_max_size[QUERY_DESC_IDN_DEVICE] = QUERY_DESC_DEVICE_MAX_SIZE;
  1754. UFS_DBG_LOGI("device descriptor length = 0x%x\n", ufs_query_desc_max_size[QUERY_DESC_IDN_DEVICE]);
  1755. err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_POWER, 0, 0,
  1756. &ufs_query_desc_max_size[QUERY_DESC_IDN_POWER]);
  1757. if (err)
  1758. ufs_query_desc_max_size[QUERY_DESC_IDN_POWER] = QUERY_DESC_POWER_MAX_SIZE;
  1759. UFS_DBG_LOGI("power descriptor length = 0x%x\n", ufs_query_desc_max_size[QUERY_DESC_IDN_POWER]);
  1760. err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_INTERCONNECT, 0, 0,
  1761. &ufs_query_desc_max_size[QUERY_DESC_IDN_INTERCONNECT]);
  1762. if (err)
  1763. ufs_query_desc_max_size[QUERY_DESC_IDN_INTERCONNECT] = QUERY_DESC_INTERCONNECT_MAX_SIZE;
  1764. UFS_DBG_LOGI("interconnect descriptor length = 0x%x\n", ufs_query_desc_max_size[QUERY_DESC_IDN_INTERCONNECT]);
  1765. err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_CONFIGURATION, 0, 0,
  1766. &ufs_query_desc_max_size[QUERY_DESC_IDN_CONFIGURATION]);
  1767. if (err)
  1768. ufs_query_desc_max_size[QUERY_DESC_IDN_CONFIGURATION] = QUERY_DESC_CONFIGURAION_MAX_SIZE;
  1769. UFS_DBG_LOGI("configuration descriptor length = 0x%x\n", ufs_query_desc_max_size[QUERY_DESC_IDN_CONFIGURATION]);
  1770. err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_UNIT, 0, 0,
  1771. &ufs_query_desc_max_size[QUERY_DESC_IDN_UNIT]);
  1772. if (err)
  1773. ufs_query_desc_max_size[QUERY_DESC_IDN_UNIT] = QUERY_DESC_UNIT_MAX_SIZE;
  1774. UFS_DBG_LOGI("unit descriptor length = 0x%x\n", ufs_query_desc_max_size[QUERY_DESC_IDN_UNIT]);
  1775. err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_GEOMETRY, 0, 0,
  1776. &ufs_query_desc_max_size[QUERY_DESC_IDN_GEOMETRY]);
  1777. if (err)
  1778. ufs_query_desc_max_size[QUERY_DESC_IDN_GEOMETRY] = QUERY_DESC_GEOMETRY_MAX_SIZE;
  1779. UFS_DBG_LOGI("geometry descriptor length = 0x%x\n", ufs_query_desc_max_size[QUERY_DESC_IDN_GEOMETRY]);
  1780. init_done = 1;
  1781. }
  1782. static int ufshcd_init_vendor_desc_sizes(struct ufs_hba *hba)
  1783. {
  1784. static int err = 0;
  1785. static int vendor_init_done = 0;
  1786. if (vendor_init_done)
  1787. return err;
  1788. err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_DEVICE, 0, 1,
  1789. &ufs_query_desc_max_size[QUERY_DESC_IDN_DEVICE]);
  1790. if (err)
  1791. goto out;
  1792. UFS_DBG_LOGI("vendor device descriptor length = 0x%x\n", ufs_query_desc_max_size[QUERY_DESC_IDN_DEVICE]);
  1793. err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_CONFIGURATION, 0, 1,
  1794. &ufs_query_desc_max_size[QUERY_DESC_IDN_CONFIGURATION]);
  1795. if (err)
  1796. goto out;
  1797. UFS_DBG_LOGI("vendor configuration descriptor length = 0x%x\n", ufs_query_desc_max_size[QUERY_DESC_IDN_CONFIGURATION]);
  1798. err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_GEOMETRY, 0, 1,
  1799. &ufs_query_desc_max_size[QUERY_DESC_IDN_GEOMETRY]);
  1800. if (err)
  1801. goto out;
  1802. UFS_DBG_LOGI("vendor geometry descriptor length = 0x%x\n", ufs_query_desc_max_size[QUERY_DESC_IDN_GEOMETRY]);
  1803. out:
  1804. vendor_init_done = 1;
  1805. return err;
  1806. }
  1807. /* replace non-printable or non-ASCII characters with spaces */
  1808. static inline void ufshcd_remove_non_printable(u8 *val)
  1809. {
  1810. if (!val)
  1811. return;
  1812. if (*val < 0x20 || *val > 0x7e)
  1813. *val = ' ';
  1814. }
  1815. static int ufs_aio_pre_pwr_change(struct ufs_hba *hba, struct ufs_pa_layer_attr *desired, struct ufs_pa_layer_attr *final)
  1816. {
  1817. if (0 == hba->dev_info.wmanufacturerid) {
  1818. UFS_DBG_LOGD("bug: manufacturer_id shall not be 0");
  1819. return -1;
  1820. }
  1821. /* fill-in desired gear for different environment */
  1822. /* make sure device capability */
  1823. #if defined(MTK_UFS_DRV_CTP)
  1824. final->gear_rx = desired->gear_rx;
  1825. final->gear_tx = desired->gear_tx;
  1826. final->lane_rx = desired->lane_rx;
  1827. final->lane_tx = desired->lane_tx;
  1828. final->hs_rate = desired->hs_rate;
  1829. final->pwr_rx = desired->pwr_rx;
  1830. final->pwr_tx = desired->pwr_tx;
  1831. #else
  1832. #ifndef UFS_CFG_SAFE_BRING_UP
  1833. if (final->pwr_rx == SLOW_MODE || final->pwr_rx == SLOWAUTO_MODE) {
  1834. final->gear_rx = min_t(u32, UFS_DEV_MAX_GEAR_RX, 4);
  1835. final->gear_tx = min_t(u32, UFS_DEV_MAX_GEAR_TX, 4);
  1836. } else {
  1837. final->gear_rx = min_t(u32, UFS_DEV_MAX_GEAR_RX, desired->gear_rx);
  1838. final->gear_tx = min_t(u32, UFS_DEV_MAX_GEAR_TX, desired->gear_tx);
  1839. }
  1840. final->lane_rx = min_t(u32, UFS_DEV_MAX_LANE_RX, desired->lane_rx);
  1841. final->lane_tx = min_t(u32, UFS_DEV_MAX_LANE_TX, desired->lane_tx);
  1842. #else
  1843. final->gear_rx = UFS_DEV_MAX_GEAR_RX;
  1844. final->gear_tx = UFS_DEV_MAX_GEAR_TX;
  1845. final->lane_rx = UFS_DEV_MAX_LANE_RX;
  1846. final->lane_tx = UFS_DEV_MAX_LANE_TX;
  1847. #endif
  1848. final->hs_rate = UFS_DEV_DEFAULT_HS_RATE;
  1849. final->pwr_rx = UFS_DEV_DEFAULT_PWR_RX;
  1850. final->pwr_tx = UFS_DEV_DEFAULT_PWR_TX;
  1851. #endif
  1852. if (final->pwr_rx == SLOW_MODE || final->pwr_rx == SLOWAUTO_MODE)
  1853. UFS_DBG_LOGI("[UFS] info: PWM-G%d\n", final->gear_rx);
  1854. else
  1855. UFS_DBG_LOGI("[UFS] info: HS-G%d-%d\n", final->gear_rx, final->hs_rate);
  1856. /* Set PAPowerModeUserData[0~5] = 0xffff, default is 0 */
  1857. hba->dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0), 0x1fff);
  1858. hba->dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1), 0xffff);
  1859. hba->dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2), 0x7fff);
  1860. hba->dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA3), 0x1fff);
  1861. hba->dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA4), 0xffff);
  1862. hba->dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA5), 0x7fff);
  1863. return 0;
  1864. }
  1865. int ufs_aio_enable_unipro_cg(struct ufs_hba *hba, bool enable)
  1866. {
  1867. u32 tmp;
  1868. if (enable) {
  1869. hba->dme_get(hba, UIC_ARG_MIB(VENDOR_SAVEPOWERCONTROL), &tmp);
  1870. tmp = tmp | (1 << RX_SYMBOL_CLK_GATE_EN) |
  1871. (1 << SYS_CLK_GATE_EN) |
  1872. (1 << TX_CLK_GATE_EN);
  1873. hba->dme_set(hba, UIC_ARG_MIB(VENDOR_SAVEPOWERCONTROL), tmp);
  1874. hba->dme_get(hba, UIC_ARG_MIB(VENDOR_DEBUGCLOCKENABLE), &tmp);
  1875. tmp = tmp & ~(1 << TX_SYMBOL_CLK_REQ_FORCE);
  1876. hba->dme_set(hba, UIC_ARG_MIB(VENDOR_DEBUGCLOCKENABLE), tmp);
  1877. } else {
  1878. hba->dme_get(hba, UIC_ARG_MIB(VENDOR_SAVEPOWERCONTROL), &tmp);
  1879. tmp = tmp & ~((1 << RX_SYMBOL_CLK_GATE_EN) |
  1880. (1 << SYS_CLK_GATE_EN) |
  1881. (1 << TX_CLK_GATE_EN));
  1882. hba->dme_set(hba, UIC_ARG_MIB(VENDOR_SAVEPOWERCONTROL), tmp);
  1883. hba->dme_get(hba, UIC_ARG_MIB(VENDOR_DEBUGCLOCKENABLE), &tmp);
  1884. tmp = tmp | (1 << TX_SYMBOL_CLK_REQ_FORCE);
  1885. hba->dme_set(hba, UIC_ARG_MIB(VENDOR_DEBUGCLOCKENABLE), tmp);
  1886. }
  1887. return 0;
  1888. }
  1889. static int ufs_aio_pre_link(struct ufs_hba *hba)
  1890. {
  1891. int ret = 0;
  1892. u32 tmp;
  1893. struct uic_command dme_setting_before_link[] = {
  1894. UIC_CMD_DME_SET(PA_LOCALTXLCCENABLE, 0, 0x0), /* set LCC_Enable to 0 */
  1895. };
  1896. ufs_aio_bootrom_deputy(hba);
  1897. ufs_aio_init_mphy(hba);
  1898. if (hba->hci_quirks & UFSHCD_QUIRK_MTK_MPHY_TESTCHIP) {
  1899. /* enable UIC related interrupts */
  1900. ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
  1901. /* apply setting before link startup */
  1902. ret = ufshcd_uic_cmd_run(hba, dme_setting_before_link,
  1903. UFS_ARRAY_SIZE(dme_setting_before_link));
  1904. if (ret)
  1905. UFS_DBG_LOGD("dme_setting_before_link fail\n");
  1906. }
  1907. /* disable deep stall by default */
  1908. hba->dme_get(hba, UIC_ARG_MIB(VENDOR_SAVEPOWERCONTROL), &tmp);
  1909. tmp &= ~(1 << 6);
  1910. hba->dme_set(hba, UIC_ARG_MIB(VENDOR_SAVEPOWERCONTROL), tmp);
  1911. /* disable scrambling by default */
  1912. hba->dme_set(hba, UIC_ARG_MIB(PA_SCRAMBLING), 0);
  1913. /* disable unipro clock gating feature by default */
  1914. ufs_aio_enable_unipro_cg(hba, FALSE);
  1915. if (0 != ret)
  1916. ret = 1;
  1917. return ret;
  1918. }
  1919. static int ufs_aio_post_link(struct ufs_hba *hba)
  1920. {
  1921. int ret = 0;
  1922. u32 tmp;
  1923. struct uic_command dme_setting_after_link[] = {
  1924. UIC_CMD_DME_PEER_SET(PA_LOCALTXLCCENABLE, 0, 0x0), /* device LCC_Enable to 0 */
  1925. };
  1926. if (hba->hci_quirks & UFSHCD_QUIRK_MTK_MPHY_TESTCHIP) {
  1927. /* apply setting after link startup */
  1928. ret = ufshcd_uic_cmd_run(hba, dme_setting_after_link,
  1929. UFS_ARRAY_SIZE(dme_setting_after_link));
  1930. if (ret)
  1931. UFS_DBG_LOGD("dme_setting_after_link fail\n");
  1932. }
  1933. #ifdef UFS_CFG_CRYPTO
  1934. /* init HW FDE feature inlined in HCI */
  1935. ufs_aio_crypto_init(hba);
  1936. #endif
  1937. if (0 != ret)
  1938. ret = 1;
  1939. return ret;
  1940. }
  1941. /*
  1942. * In early-porting stage, because of no bootrom, something finished by bootrom shall be finished here instead.
  1943. * Returns:
  1944. * 0: Successful.
  1945. * Non-zero: Failed.
  1946. */
  1947. static int ufs_aio_bootrom_deputy(struct ufs_hba *hba)
  1948. {
  1949. #ifdef UFS_CFG_FPGA_PLATFORM
  1950. u32 reg;
  1951. u32 mask = 0;
  1952. if (!hba->pericfg_base)
  1953. return 1;
  1954. reg = readl(hba->pericfg_base + ufs_platform.reg_ufs_pericfg);
  1955. if (ufs_platform.reg_ufs_pericfg_ldo_n_bit != 0xF)
  1956. mask |= (1 << ufs_platform.reg_ufs_pericfg_ldo_n_bit);
  1957. if (ufs_platform.reg_ufs_pericfg_rst_n_bit != 0xF)
  1958. mask |= (1 << ufs_platform.reg_ufs_pericfg_rst_n_bit);
  1959. reg = reg & ~mask;
  1960. writel(reg, hba->pericfg_base + ufs_platform.reg_ufs_pericfg);
  1961. udelay(10);
  1962. if (ufs_platform.reg_ufs_pericfg_ldo_n_bit != 0xF) {
  1963. reg = readl(hba->pericfg_base + ufs_platform.reg_ufs_pericfg);
  1964. reg = reg | (1 << ufs_platform.reg_ufs_pericfg_ldo_n_bit);
  1965. writel(reg, hba->pericfg_base + ufs_platform.reg_ufs_pericfg);
  1966. udelay(10);
  1967. }
  1968. if (ufs_platform.reg_ufs_pericfg_rst_n_bit != 0xF) {
  1969. reg = readl(hba->pericfg_base + ufs_platform.reg_ufs_pericfg);
  1970. reg = reg | (1 << ufs_platform.reg_ufs_pericfg_rst_n_bit);
  1971. writel(reg, hba->pericfg_base + ufs_platform.reg_ufs_pericfg);
  1972. udelay(10);
  1973. }
  1974. mdelay(1);
  1975. return 0;
  1976. #else
  1977. return 0;
  1978. #endif
  1979. }
  1980. static int ufs_aio_test_unit_ready_all_device(struct ufs_hba *hba)
  1981. {
  1982. int i, tag;
  1983. struct ufs_aio_scsi_cmd cmd;
  1984. u32 start_tick, timeout_tick;
  1985. int ret = UFS_ERR_NONE;
  1986. if (0 == hba->dev_info.num_active_lu) {
  1987. UFS_DBG_LOGE("bug: active_num_lu shall not be 0");
  1988. return -1;
  1989. }
  1990. if (!ufshcd_get_free_tag(hba, &tag))
  1991. return -1;
  1992. /* send test unit ready to each LUN to remove possible UNIT ATTENTION */
  1993. for (i = 0; i < hba->dev_info.num_active_lu; i++) {
  1994. start_tick = gpt4_get_current_tick ();
  1995. timeout_tick =
  1996. gpt4_time2tick_us(UFS_TEST_UNIT_READY_TIMEOUT_US);
  1997. do {
  1998. ufs_aio_scsi_cmd_test_unit_ready(&cmd, tag, i);
  1999. /* check error code if retrun with UNIT ATTENTION */
  2000. ret = ufshcd_queuecommand(hba, &cmd);
  2001. } while (ret && !gpt4_timeout_tick (start_tick, timeout_tick));
  2002. }
  2003. /* send test unit ready to WRPMB LUN */
  2004. start_tick = gpt4_get_current_tick ();
  2005. timeout_tick = gpt4_time2tick_us(UFS_TEST_UNIT_READY_TIMEOUT_US);
  2006. do {
  2007. ufs_aio_scsi_cmd_test_unit_ready(&cmd, tag, WLUN_RPMB);
  2008. /* check error code if retrun with UNIT ATTENTION */
  2009. ret = ufshcd_queuecommand(hba, &cmd);
  2010. } while (ret && !gpt4_timeout_tick (start_tick, timeout_tick));
  2011. ufshcd_put_tag(hba, tag);
  2012. return 0;
  2013. }
  2014. static void ufs_aio_advertise_hci_quirks(struct ufs_hba *hba)
  2015. {
  2016. #ifdef UFS_CFG_FPGA_PLATFORM
  2017. hba->hci_quirks |= UFSHCD_QUIRK_MTK_MPHY_TESTCHIP;
  2018. #endif
  2019. }
  2020. static void ufs_aio_reset_device(struct ufs_hba *hba)
  2021. {
  2022. #if defined(MTK_UFS_DRV_LK)
  2023. /* reset device */
  2024. mt_secure_call(MTK_SIP_BL_UFS_CONTROL_AARCH32, 2, 0, 0, 0);
  2025. udelay(10);
  2026. mt_secure_call(MTK_SIP_BL_UFS_CONTROL_AARCH32, 2, 1, 0, 0);
  2027. #else
  2028. u32 reg;
  2029. if (!hba->pericfg_base) {
  2030. UFS_DBG_LOGD("[UFS] err: %s: pericfg_base is NULL\n", __func__);
  2031. return;
  2032. }
  2033. reg = readl(hba->pericfg_base + ufs_platform.reg_ufs_pericfg);
  2034. /* reset device */
  2035. reg = reg & ~(1 << ufs_platform.reg_ufs_pericfg_rst_n_bit);
  2036. writel(reg, hba->pericfg_base + ufs_platform.reg_ufs_pericfg);
  2037. udelay(10);
  2038. reg = reg | (1 << ufs_platform.reg_ufs_pericfg_rst_n_bit);
  2039. writel(reg, hba->pericfg_base + ufs_platform.reg_ufs_pericfg);
  2040. #endif
  2041. /* wait awhile after device reset */
  2042. /* We add this as general solution even though only SK-Hynix needs this delay actually. */
  2043. mdelay(10);
  2044. }
  2045. int ufs_aio_set_boot_lu(struct ufs_hba *hba, u32 b_boot_lun_en)
  2046. {
  2047. int ret;
  2048. ret = hba->query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, ATTR_B_BOOT_LUN_EN, 0, 0, &b_boot_lun_en);
  2049. if (0 != ret)
  2050. UFS_DBG_LOGD("[UFS] err: ufs_aio_set_boot_lu error: %d\n", ret);
  2051. else
  2052. UFS_DBG_LOGD("[UFS] info: ufs_aio_set_boot_lu %d done\n", b_boot_lun_en);
  2053. return ret;
  2054. }
  2055. #if defined(MTK_UFS_DRV_PRELOADER)
  2056. int ufs_aio_get_boot_lu(struct ufs_hba *hba, u32 *b_boot_lun)
  2057. {
  2058. int ret;
  2059. ret = hba->query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR, ATTR_B_BOOT_LUN_EN, 0, 0, b_boot_lun);
  2060. if (0 != ret)
  2061. UFS_DBG_LOGD("[UFS] err: ufs_aio_get_boot_lu error: %d\n", ret);
  2062. else
  2063. UFS_DBG_LOGD("[UFS] info: ufs_aio_get_boot_lu %d done\n", *b_boot_lun);
  2064. return ret;
  2065. }
  2066. #endif
  2067. u32 ufs_aio_get_lu_cfg_size(struct ufs_hba *hba, struct ufs_geometry_info *info, u32 lun_id)
  2068. {
  2069. u32 d_num_alloc_units;
  2070. u32 tw_size_gb;
  2071. u32 tw_no_red;
  2072. u32 hpb_region_count;
  2073. u32 hpb_size_gb;
  2074. u64 lu2_size; /* unit : 512B */
  2075. UFS_DBG_LOGE("[UFS] %s: lun %d\n", __func__, lun_id);
  2076. UFS_DBG_LOGE("[UFS] d_segment_size=0x%x\n", info->d_segment_size);
  2077. UFS_DBG_LOGE("[UFS] b_allocation_units_size=0x%x\n", info->b_allocation_units_size);
  2078. UFS_DBG_LOGE("[UFS] q_total_raw_device_capacity=0x%llx\n", info->q_total_raw_device_capacity);
  2079. if (lun_id < 2) { /* boot lu */
  2080. /*
  2081. * Boot size is 4MB which have enhance feature, real size
  2082. * shoule be 4MB * (w_adj_factor_enahnced_1 / 256)
  2083. */
  2084. UFS_DBG_LOGE("[UFS] w_adj_factor_enahnced_1=%d\n", info->w_adj_factor_enahnced_1);
  2085. d_num_alloc_units = ((UFS_BOOT_LU_SIZE_BYTE >> 9) * (info->w_adj_factor_enahnced_1 >> 8)) /
  2086. info->d_segment_size / info->b_allocation_units_size;
  2087. } else { /* user lu */
  2088. d_num_alloc_units = (u32)(((info->q_total_raw_device_capacity) - (u64)(2 * (UFS_BOOT_LU_SIZE_BYTE >> 9) * (info->w_adj_factor_enahnced_1 >> 8))) /
  2089. info->d_segment_size / info->b_allocation_units_size);
  2090. if (hba->dev_info.tw_support) {
  2091. tw_size_gb = hba->custom_info.tw_size_gb;
  2092. tw_no_red = hba->custom_info.tw_no_red;
  2093. /* default use ceiling[1/16 of lu 2] */
  2094. if (tw_size_gb == 0xffffffff)
  2095. tw_size_gb = (((d_num_alloc_units * info->b_allocation_units_size * info->d_segment_size) >> 21) + 15) >> 4;
  2096. /* Samsung 3.0 not follow spec */
  2097. if ((hba->dev_info.wmanufacturerid == UFS_VENDOR_SAMSUNG) && (hba->dev_info.ufs_ver == 0x0300))
  2098. info->d_tw_buf_au = ((tw_size_gb << 21) / info->d_segment_size / info->b_allocation_units_size) * info->b_tw_buf_adj_fac;
  2099. else
  2100. info->d_tw_buf_au = ((tw_size_gb << 21) / info->d_segment_size / info->b_allocation_units_size);
  2101. /* No check error, return err if custom provision fail, auto provision will set to max */
  2102. if (info->d_tw_buf_au > info->d_tw_buf_max_au) {
  2103. if (hba->custom_info.tw_size_gb == 0xffffffff)
  2104. info->d_tw_buf_au = info->d_tw_buf_max_au;
  2105. else
  2106. UFS_DBG_LOGE("[UFS]======================> Err! Max AU=0x%x, but set=0x%x\n", info->d_tw_buf_max_au, info->d_tw_buf_au);
  2107. }
  2108. UFS_DBG_LOGI("[UFS] TW size=%dGB, TW AU=0x%x\n", tw_size_gb, info->d_tw_buf_au);
  2109. if (tw_no_red == 0)
  2110. d_num_alloc_units -= info->d_tw_buf_au;
  2111. }
  2112. if ((hba->dev_info.hpb_support) && (info->q_hpb_region_size)) {
  2113. lu2_size = (u64)d_num_alloc_units * info->b_allocation_units_size * info->d_segment_size;
  2114. info->w_hpb_lu_max_active_regions = (u32)((lu2_size + info->q_hpb_region_size - 1) / info->q_hpb_region_size);
  2115. if (info->w_hpb_lu_max_active_regions > info->w_hpb_device_max_active_regions)
  2116. info->w_hpb_lu_max_active_regions = info->w_hpb_device_max_active_regions;
  2117. /* No check error, return err if provision fail */
  2118. if (hba->custom_info.hpb_size_gb != 0xffffffff) {
  2119. hpb_region_count = hba->custom_info.hpb_size_gb << 21 / info->q_hpb_region_size;
  2120. if (hpb_region_count > info->w_hpb_lu_max_active_regions) {
  2121. UFS_DBG_LOGE("[UFS]======================> Err! Max region count=0x%x, but set=0x%x\n",
  2122. info->w_hpb_lu_max_active_regions, hpb_region_count);
  2123. }
  2124. info->w_hpb_lu_max_active_regions = hpb_region_count;
  2125. }
  2126. hpb_size_gb = (u64)(info->w_hpb_lu_max_active_regions * info->q_hpb_region_size) >> 21;
  2127. UFS_DBG_LOGI("[UFS] HPB size=%dGB, HPB LU regions=0x%x, LU size=0x%llx\n",
  2128. hpb_size_gb, info->w_hpb_lu_max_active_regions, lu2_size);
  2129. }
  2130. }
  2131. UFS_DBG_LOGE("[UFS] d_num_alloc_units=0x%x\n", d_num_alloc_units);
  2132. return d_num_alloc_units;
  2133. }
  2134. static void ufs_aio_util_dump_config_table_hex(u8 *table, u32 bytes, u32 ud0_base_offset, u32 ud_config_len)
  2135. {
  2136. u32 i, ptr;
  2137. ptr = 0;
  2138. for (i = 0; i < ud0_base_offset; i++) {
  2139. UFS_DBG_LOGI("%x ", table[ptr]);
  2140. ptr++;
  2141. if (ptr >= bytes)
  2142. break;
  2143. }
  2144. UFS_DBG_LOGE("\n");
  2145. while (ptr < bytes) {
  2146. for (i = 0; i < ud_config_len; i++) {
  2147. UFS_DBG_LOGI("%x ", table[ptr]);
  2148. ptr++;
  2149. if (ptr >= bytes)
  2150. break;
  2151. }
  2152. UFS_DBG_LOGE("\n");
  2153. if (ptr >= bytes)
  2154. return;
  2155. }
  2156. }
  2157. void ufs_aio_util_dump_table_hex(u8 *table, u32 bytes)
  2158. {
  2159. u32 i, ptr;
  2160. ptr = 0;
  2161. while (ptr < bytes) {
  2162. for (i = 0; i < 16; i++) {
  2163. UFS_DBG_LOGI("%x ", table[ptr]);
  2164. ptr++;
  2165. if (ptr >= bytes)
  2166. break;
  2167. }
  2168. UFS_DBG_LOGE("\n");
  2169. if (ptr >= bytes)
  2170. return;
  2171. }
  2172. }
  2173. int ufs_aio_check_lu_cfg(struct ufs_hba *hba)
  2174. {
  2175. int ret;
  2176. u32 i;
  2177. u8 *p;
  2178. u32 num_active_lu = 0;
  2179. u32 num_boot_lu = 0;
  2180. #ifdef MTK_UFS_DRV_DA
  2181. #ifdef UFS_AIO_DEV_CHECK_LU_SIZE
  2182. u32 d_num_alloc_units_boot_lu;
  2183. u32 d_num_alloc_units_user_lu;
  2184. struct ufs_geometry_info info = {0};
  2185. #endif
  2186. #endif
  2187. /* HPB support, read configuration descriptor with HPB, selector = 1, length = 0xD0(HPB1.0)/0xE2(HPB2.0) */
  2188. if ((hba->dev_info.hpb_support) && (hba->dev_info.wmanufacturerid == UFS_VENDOR_SAMSUNG || hba->dev_info.wmanufacturerid == UFS_VENDOR_MICRON_MP))
  2189. ret = hba->read_descriptor(hba, QUERY_DESC_IDN_CONFIGURATION, 0, 1, (u8 *)&g_ufs_temp_buf[0], ufs_query_desc_max_size[QUERY_DESC_IDN_CONFIGURATION]);
  2190. else
  2191. ret = hba->read_descriptor(hba, QUERY_DESC_IDN_CONFIGURATION, 0, 0, (u8 *)&g_ufs_temp_buf[0], ufs_query_desc_max_size[QUERY_DESC_IDN_CONFIGURATION]);
  2192. if (0 != ret) {
  2193. UFS_DBG_LOGE("[UFS] err: ufs_aio_check_lu_cfg: read config descr error: %d\n", ret);
  2194. return ret;
  2195. }
  2196. UFS_DBG_LOGE("[UFS] %s: original Configuration Desc:\n", __func__);
  2197. ufs_aio_util_dump_config_table_hex(&g_ufs_temp_buf[0], ufs_query_desc_max_size[QUERY_DESC_IDN_CONFIGURATION], hba->dev_info.ud0_base_offset, hba->dev_info.ud_config_len);
  2198. p = (u8 *)&g_ufs_temp_buf[0];
  2199. /* check: number of active LU */
  2200. for (i = 0; i < 8; i++) { /* traverse 8 Unit Descriptor configurable parameters */
  2201. if (i == 0)
  2202. p += hba->dev_info.ud0_base_offset;
  2203. else
  2204. p += hba->dev_info.ud_config_len;
  2205. #ifdef MTK_UFS_DRV_DA
  2206. if ((hba->dev_info.hpb_support) && (i == 2) && (p[CONF_DESC_UNIT_B_LU_ENABLE] == 1)) {
  2207. UFS_DBG_LOGE("[UFS] error: LU cfg error! HPB support, but not enable, need re-configuration.\n");
  2208. return UFS_ERR_INVALID_LU_CONFIGURATION;
  2209. }
  2210. #endif
  2211. /* 01h: Logical Unit enabled, 02h: HPB Logical Unit enabled */
  2212. if ((p[CONF_DESC_UNIT_B_LU_ENABLE] == 1) ||
  2213. ((hba->dev_info.hpb_support) && (p[CONF_DESC_UNIT_B_LU_ENABLE] == 2)))
  2214. num_active_lu++;
  2215. if ((i < 2) && (0 != p[CONF_DESC_UNIT_B_BOOT_LUN_ID])) {
  2216. num_boot_lu++;
  2217. }
  2218. }
  2219. hba->dev_info.num_active_lu = num_active_lu;
  2220. #ifdef MTK_UFS_DRV_DA
  2221. if (3 != num_active_lu) {
  2222. UFS_DBG_LOGE("[UFS] error: LU cfg error! incorrect number of active LUs: %d, need re-configuration.\n", num_active_lu);
  2223. return UFS_ERR_INVALID_LU_CONFIGURATION;
  2224. } else
  2225. UFS_DBG_LOGI("[UFS] info: number of active LU: %d, check OK.\n", num_active_lu);
  2226. /* check: must have 2 Boot LUs in the first 2 LUs */
  2227. if (2 != num_boot_lu) {
  2228. UFS_DBG_LOGE("[UFS] error: LU cfg error! shall have 2 Boot LUs in the first 2 LUs, need re-configuration.\n");
  2229. return UFS_ERR_INVALID_LU_CONFIGURATION;
  2230. } else
  2231. UFS_DBG_LOGI("[UFS] info: number of Boot LU: %d, check OK.\n", num_boot_lu);
  2232. /* todo: check Boot LU size and User LU size. */
  2233. #ifdef UFS_AIO_DEV_CHECK_LU_SIZE
  2234. ret = hba->read_descriptor(hba, QUERY_DESC_IDN_GEOMETRY, 0, (u8 *)&g_ufs_temp_buf[0], ufs_query_desc_max_size[QUERY_DESC_IDN_GEOMETRY]);
  2235. if (ret != 0) {
  2236. UFS_DBG_LOGE("[UFS] err: ufs_aio_configure_new_ufs: read geometry desc error: %d\n", ret);
  2237. return ret;
  2238. }
  2239. UFS_DBG_LOGE("[UFS] %s: Geometry Desc:\n", __func__);
  2240. ufs_aio_util_dump_table_hex(&g_ufs_temp_buf[0], ufs_query_desc_max_size[QUERY_DESC_IDN_GEOMETRY]);
  2241. p = (u8 *)&g_ufs_temp_buf[0];
  2242. info.q_total_raw_device_capacity = ((u64)p[4] << 56) |
  2243. ((u64)p[5] << 48) |
  2244. ((u64)p[6] << 40) |
  2245. ((u64)p[7] << 32) |
  2246. (p[8] << 24) |
  2247. (p[9] << 16) |
  2248. (p[10] << 8) |
  2249. p[11];
  2250. info.d_segment_size = (p[13] << 24) |
  2251. (p[14] << 16) |
  2252. (p[15] << 8) |
  2253. p[16];
  2254. info.b_allocation_units_size = p[17];
  2255. info.w_adj_factor_enahnced_1 = (p[0x30] << 8) |
  2256. p[0x31];
  2257. /* decide LU size */
  2258. d_num_alloc_units_boot_lu = ufs_aio_get_lu_cfg_size(hba, &info, UFS_LU_0);
  2259. d_num_alloc_units_user_lu = ufs_aio_get_lu_cfg_size(hba, &info, UFS_LU_2);
  2260. #endif
  2261. #endif
  2262. return UFS_ERR_NONE;
  2263. }
  2264. #if defined(MTK_UFS_DRV_PRELOADER)
  2265. int ufs_aio_rpmb_get_rw_size(void)
  2266. {
  2267. struct ufs_hba *hba = &g_ufs_hba;
  2268. int ret, size;
  2269. u8 buf[QUERY_DESC_GEOMETRY_MAX_SIZE];
  2270. ret = hba->read_descriptor(hba, QUERY_DESC_IDN_GEOMETRY, 0, 0, buf, QUERY_DESC_GEOMETRY_MAX_SIZE);
  2271. if (ret) {
  2272. UFS_DBG_LOGE("[UFS] err: ufs_aio_rpmb_get_rw_size: read geometry desc error: %d\n", ret);
  2273. return ret;
  2274. }
  2275. size = (int)buf[GEOMETRY_DESC_PARAM_RPMB_READ_WRITE_SIZE];
  2276. UFS_DBG_LOGD("[UFS] rpmb: rw size: %d frames\n", size);
  2277. if (hba->dev_quirks & UFS_DEVICE_QUIRK_LIMITED_RPMB_MAX_RW_SIZE) {
  2278. if (size > UFS_RPMB_DEV_MAX_RW_SIZE_LIMITATION) {
  2279. size = UFS_RPMB_DEV_MAX_RW_SIZE_LIMITATION;
  2280. UFS_DBG_LOGD("[UFS] rpmb: rw size: %d frames (limitation)\n", size);
  2281. }
  2282. }
  2283. return size;
  2284. }
  2285. u64 ufs_aio_rpmb_get_lu_size(void)
  2286. {
  2287. struct ufs_hba *hba = &g_ufs_hba;
  2288. int ret, i;
  2289. u8 buf[QUERY_DESC_UNIT_MAX_SIZE];
  2290. u8 logical_blk_size;
  2291. u32 logical_blk_size_bytes;
  2292. u64 logical_blk_cnt;
  2293. ret = hba->read_descriptor(hba, QUERY_DESC_IDN_UNIT, UFS_UPIU_RPMB_WLUN, 0, buf, QUERY_DESC_UNIT_MAX_SIZE);
  2294. if (ret) {
  2295. UFS_DBG_LOGE("[UFS] err: ufs_aio_rpmb_get_lu_size: read rpmb unit desc error: %d\n", ret);
  2296. return ret;
  2297. }
  2298. logical_blk_size = buf[UNIT_DESC_PARAM_LOGICAL_BLK_SIZE];
  2299. for (i = 0, logical_blk_size_bytes = 1; i < logical_blk_size; i++)
  2300. logical_blk_size_bytes = logical_blk_size_bytes << 1;
  2301. UFS_DBG_LOGD("[UFS] rpmb: logical_blk_size: %d bytes\n", (int)logical_blk_size_bytes);
  2302. logical_blk_cnt = ((u64)buf[UNIT_DESC_PARAM_LOGICAL_BLK_COUNT] << 56) |
  2303. ((u64)buf[UNIT_DESC_PARAM_LOGICAL_BLK_COUNT + 1] << 48) |
  2304. ((u64)buf[UNIT_DESC_PARAM_LOGICAL_BLK_COUNT + 2] << 40) |
  2305. ((u64)buf[UNIT_DESC_PARAM_LOGICAL_BLK_COUNT + 3] << 32) |
  2306. ((u64)buf[UNIT_DESC_PARAM_LOGICAL_BLK_COUNT + 4] << 24) |
  2307. ((u64)buf[UNIT_DESC_PARAM_LOGICAL_BLK_COUNT + 5] << 16) |
  2308. ((u64)buf[UNIT_DESC_PARAM_LOGICAL_BLK_COUNT + 6] << 8) |
  2309. ((u64)buf[UNIT_DESC_PARAM_LOGICAL_BLK_COUNT + 7]);
  2310. UFS_DBG_LOGD("[UFS] rpmb: logical_blk_cnt: 0x%llx\n", logical_blk_cnt);
  2311. return (u64)logical_blk_size_bytes * logical_blk_cnt;
  2312. }
  2313. #endif
  2314. int ufs_aio_configure_new_ufs(struct ufs_hba *hba)
  2315. {
  2316. int ret;
  2317. u32 i;
  2318. u8 *p;
  2319. u32 d_num_alloc_units_boot_lu;
  2320. u32 d_num_alloc_units_user_lu;
  2321. struct ufs_geometry_info info = {0};
  2322. /* HPB support, read geometry descriptor with HPB, selector = 1 */
  2323. if ((hba->dev_info.hpb_support) && (hba->dev_info.wmanufacturerid == UFS_VENDOR_SAMSUNG || hba->dev_info.wmanufacturerid == UFS_VENDOR_MICRON_MP))
  2324. ret = hba->read_descriptor(hba, QUERY_DESC_IDN_GEOMETRY, 0, 1, (u8 *)&g_ufs_temp_buf[0], ufs_query_desc_max_size[QUERY_DESC_IDN_GEOMETRY]);
  2325. else
  2326. ret = hba->read_descriptor(hba, QUERY_DESC_IDN_GEOMETRY, 0, 0, (u8 *)&g_ufs_temp_buf[0], ufs_query_desc_max_size[QUERY_DESC_IDN_GEOMETRY]);
  2327. if (0 != ret) {
  2328. UFS_DBG_LOGE("[UFS] err: ufs_aio_configure_new_ufs: read geometry desc error: %d\n", ret);
  2329. return ret;
  2330. }
  2331. UFS_DBG_LOGE("[UFS] %s: Geometry Desc:\n", __func__);
  2332. ufs_aio_util_dump_table_hex(&g_ufs_temp_buf[0], ufs_query_desc_max_size[QUERY_DESC_IDN_GEOMETRY]);
  2333. p = (u8 *)&g_ufs_temp_buf[0];
  2334. info.q_total_raw_device_capacity = ((u64)p[4] << 56) |
  2335. ((u64)p[5] << 48) |
  2336. ((u64)p[6] << 40) |
  2337. ((u64)p[7] << 32) |
  2338. ((u64)p[8] << 24) |
  2339. ((u64)p[9] << 16) |
  2340. ((u64)p[10] << 8) |
  2341. (u64)p[11];
  2342. info.d_segment_size = (p[13] << 24) |
  2343. (p[14] << 16) |
  2344. (p[15] << 8) |
  2345. p[16];
  2346. info.b_allocation_units_size = p[17];
  2347. info.w_adj_factor_enahnced_1 = (p[0x30] << 8) |
  2348. p[0x31];
  2349. if (hba->dev_info.hpb_support) {
  2350. info.q_hpb_region_size = (u64)1 << p[0x48];
  2351. info.w_hpb_device_max_active_regions = (p[0x4B] << 8) | p[0x4C];
  2352. UFS_DBG_LOGI("[UFS] HPB device max active regions=0x%x, region size=0x%llx\n",
  2353. info.w_hpb_device_max_active_regions, info.q_hpb_region_size);
  2354. }
  2355. if (hba->dev_info.tw_support) {
  2356. info.d_tw_buf_max_au = (p[0x4F] << 24)| (p[0x50] << 16) | (p[0x51] << 8) | p[0x52];
  2357. info.b_tw_buf_adj_fac = p[0x54];
  2358. info.b_tw_support_red_type = p[0x55];
  2359. info.b_tw_support_buf_type = p[0x56];
  2360. UFS_DBG_LOGI("[UFS] TW max AU=0x%x, TW adj=%d, TW reduction=%d, TW buffer type=%d\n",
  2361. info.d_tw_buf_max_au, info.b_tw_buf_adj_fac,
  2362. info.b_tw_support_red_type, info.b_tw_support_buf_type);
  2363. }
  2364. /* prepare configuration descriptor */
  2365. memset(&g_ufs_temp_buf[0], 0, QUERY_DESC_CONFIGURAION_MAX_SIZE_HPB_20);
  2366. p[CONF_DESC_B_LENGTH] = ufs_query_desc_max_size[QUERY_DESC_IDN_CONFIGURATION];
  2367. p[CONF_DESC_B_DESCRIPTOR_TYPE] = QUERY_DESC_IDN_CONFIGURATION;
  2368. p[CONF_DESC_B_BOOT_ENABLE] = 1;
  2369. p[CONF_DESC_B_DESCR_ACCESS_EN] = 1;
  2370. p[CONF_DESC_B_INIT_POWER_MODE] = 1;
  2371. p[CONF_DESC_B_HIGH_PRIORITY_LUN] = 0x7F; /* no high priority LU */
  2372. /* decide LU size */
  2373. d_num_alloc_units_boot_lu = ufs_aio_get_lu_cfg_size(hba, &info, UFS_LU_0);
  2374. d_num_alloc_units_user_lu = ufs_aio_get_lu_cfg_size(hba, &info, UFS_LU_2);
  2375. if (hba->dev_info.hpb_support)
  2376. p[CONF_DESC_B_HPB_CINTROL] = 1; /* Device Control */
  2377. if (hba->dev_info.tw_support) {
  2378. p[CONF_DESC_B_TW_BUF_NO_RED_EN] = hba->custom_info.tw_no_red; /* user space reduction */
  2379. if (info.b_tw_support_buf_type == 0x2) /* Support both */
  2380. p[CONF_DESC_B_TW_BUF_TYPE] = 0; /* LU base WB buffer */
  2381. else /* Only support LU base WB buffer(0), single share(1) */
  2382. p[CONF_DESC_B_TW_BUF_TYPE] = info.b_tw_support_buf_type;
  2383. /* JEDEC version */
  2384. if (hba->dev_info.wmanufacturerid != UFS_VENDOR_SAMSUNG) {
  2385. p[CONF_DESC_D_NUM_SHARED_WB_BUF_AU] = (info.d_tw_buf_au >> 24) & 0xFF;
  2386. p[CONF_DESC_D_NUM_SHARED_WB_BUF_AU + 1] = (info.d_tw_buf_au >> 16) & 0xFF;
  2387. p[CONF_DESC_D_NUM_SHARED_WB_BUF_AU + 2] = (info.d_tw_buf_au >> 8) & 0xFF;
  2388. p[CONF_DESC_D_NUM_SHARED_WB_BUF_AU + 3] = info.d_tw_buf_au & 0xFF;
  2389. }
  2390. }
  2391. for (i = 0; i < 8; i++) { /* traverse 8 Unit Descriptor configurable parameters */
  2392. if (i == 0)
  2393. p += hba->dev_info.ud0_base_offset;
  2394. else
  2395. p += hba->dev_info.ud_config_len;
  2396. /* enable LU0 (boot 1), LU1 (boot 2), LU2 (user) only */
  2397. if (i >= 3) {
  2398. if (hba->dev_quirks & UFS_DEVICE_QUIRK_NEED_CONFIGURATION_FOR_UNUSED_LU) {
  2399. /*
  2400. * Some device does not allow 0x0 in LOGICAL_BLOCK_SIZE even for unused LU.
  2401. *
  2402. * Device vendor shall fix this to ignore any checking for unused LU.
  2403. * Apply this quirk here for development purpose.
  2404. */
  2405. p[CONF_DESC_UNIT_B_LOGICAL_BLOCK_SIZE] = 0xC; /* 4 KB */
  2406. }
  2407. continue;
  2408. }
  2409. /* common configurations for all LUs */
  2410. if ((i == 2) && (hba->dev_info.hpb_support)) {
  2411. /* Enable HPB */
  2412. p[CONF_DESC_UNIT_B_LU_ENABLE] = 2;
  2413. p[CONF_DESC_UNIT_W_MAX_ACTIVE_HPB_REGIONS] = (info.w_hpb_lu_max_active_regions >> 8) & 0xFF;
  2414. p[CONF_DESC_UNIT_W_MAX_ACTIVE_HPB_REGIONS + 1] = info.w_hpb_lu_max_active_regions & 0xFF;
  2415. p[CONF_DESC_UNIT_W_HPB_PINNED_REGION_START_IDX] = 0;
  2416. p[CONF_DESC_UNIT_W_HPB_PINNED_REGION_START_IDX + 1] = 0;
  2417. p[CONF_DESC_UNIT_W_NUM_HPB_PINNED_REGIONS] = 0;
  2418. p[CONF_DESC_UNIT_W_NUM_HPB_PINNED_REGIONS + 1] = 0;
  2419. } else {
  2420. p[CONF_DESC_UNIT_B_LU_ENABLE] = 1;
  2421. }
  2422. if ((i == 2) && (hba->dev_info.tw_support)) {
  2423. p[CONF_DESC_UNIT_D_NUM_TW_BUF_AU] = (info.d_tw_buf_au >> 24) & 0xFF;
  2424. p[CONF_DESC_UNIT_D_NUM_TW_BUF_AU + 1] = (info.d_tw_buf_au >> 16) & 0xFF;
  2425. p[CONF_DESC_UNIT_D_NUM_TW_BUF_AU + 2] = (info.d_tw_buf_au >> 8) & 0xFF;
  2426. p[CONF_DESC_UNIT_D_NUM_TW_BUF_AU + 3] = info.d_tw_buf_au & 0xFF;
  2427. }
  2428. p[CONF_DESC_UNIT_B_LOGICAL_BLOCK_SIZE] = 0xC; /* 4 KB */
  2429. p[CONF_DESC_UNIT_B_PROVISIONING_TYPE] = 0x2; /* thin-provisioning with TPRZ = 0 */
  2430. if (i < 2) { /* boot LU */
  2431. p[CONF_DESC_UNIT_B_DATA_RELIABILITY] = 1;
  2432. p[CONF_DESC_UNIT_B_BOOT_LUN_ID] = i + 1; /* LU0 = Boot A, LU1 = Boot B */
  2433. /*
  2434. * TODO: Set Memory Type of Boot LU according to different vendors
  2435. * SK-Hynix/Samsung: 3
  2436. * Toshiba: 4
  2437. */
  2438. p[CONF_DESC_UNIT_B_MEMORY_TYPE] = UFS_MEMORY_TYPE_ENHANCED_1;
  2439. p[CONF_DESC_UNIT_D_NUM_ALLOC_UNITS] = (d_num_alloc_units_boot_lu >> 24) & 0xFF;
  2440. p[CONF_DESC_UNIT_D_NUM_ALLOC_UNITS + 1] = (d_num_alloc_units_boot_lu >> 16) & 0xFF;
  2441. p[CONF_DESC_UNIT_D_NUM_ALLOC_UNITS + 2] = (d_num_alloc_units_boot_lu >> 8) & 0xFF;
  2442. p[CONF_DESC_UNIT_D_NUM_ALLOC_UNITS + 3] = d_num_alloc_units_boot_lu & 0xFF;
  2443. } else { /* non-boot LU (user LU) */
  2444. p[CONF_DESC_UNIT_B_MEMORY_TYPE] = UFS_MEMORY_TYPE_NORMAL;
  2445. p[CONF_DESC_UNIT_D_NUM_ALLOC_UNITS] = (d_num_alloc_units_user_lu >> 24) & 0xFF;
  2446. p[CONF_DESC_UNIT_D_NUM_ALLOC_UNITS + 1] = (d_num_alloc_units_user_lu >> 16) & 0xFF;
  2447. p[CONF_DESC_UNIT_D_NUM_ALLOC_UNITS + 2] = (d_num_alloc_units_user_lu >> 8) & 0xFF;
  2448. p[CONF_DESC_UNIT_D_NUM_ALLOC_UNITS + 3] = d_num_alloc_units_user_lu & 0xFF;
  2449. }
  2450. }
  2451. #ifdef MTK_UFS_DRV_DA
  2452. hba->unit_desc_cfg_param_valid = 0;
  2453. #endif
  2454. UFS_DBG_LOGE("[UFS] %s: new Configuration Desc:\n", __func__);
  2455. /* HPB support, write configuration descriptor with HPB, selector = 1, length = 0xD0(HPB1.0)/0xE2(HPB2.0) */
  2456. ufs_aio_util_dump_config_table_hex(&g_ufs_temp_buf[0], ufs_query_desc_max_size[QUERY_DESC_IDN_CONFIGURATION], hba->dev_info.ud0_base_offset, hba->dev_info.ud_config_len);
  2457. if ((hba->dev_info.hpb_support) && (hba->dev_info.wmanufacturerid == UFS_VENDOR_SAMSUNG || hba->dev_info.wmanufacturerid == UFS_VENDOR_MICRON_MP))
  2458. ret = hba->write_descriptor(hba, QUERY_DESC_IDN_CONFIGURATION, 0, 1, &g_ufs_temp_buf[0], ufs_query_desc_max_size[QUERY_DESC_IDN_CONFIGURATION]);
  2459. else
  2460. ret = hba->write_descriptor(hba, QUERY_DESC_IDN_CONFIGURATION, 0, 0, &g_ufs_temp_buf[0], ufs_query_desc_max_size[QUERY_DESC_IDN_CONFIGURATION]);
  2461. if (ret) {
  2462. UFS_DBG_LOGE("[UFS] err: ufs_aio_configure_new_ufs: write conf desc error: %d (0x%x)\n", ret, ret);
  2463. return ret;
  2464. } else
  2465. UFS_DBG_LOGD("[UFS] info: ufs_aio_configure_new_ufs: write conf desc done\n");
  2466. ret = ufs_aio_set_boot_lu(hba, ATTR_B_BOOT_LUN_EN_BOOT_LU_A);
  2467. if (ret)
  2468. return ret;
  2469. // lock down configuration descriptor if required
  2470. if (hba->dev_quirks & UFS_DEVICE_QUIRK_NEED_LOCK_CONFIG_DESC)
  2471. {
  2472. i = 0x1;
  2473. if ((ret = (hba->query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, ATTR_B_CONFIG_DESCR_LOCK, 0, 0, &i))))
  2474. return ret;
  2475. }
  2476. return UFS_ERR_NONE;
  2477. }
  2478. static int ufs_aio_prepare_new_ufs(struct ufs_hba *hba)
  2479. {
  2480. int ret;
  2481. /* configure this new UFS device */
  2482. UFS_DBG_LOGI("[UFS] info: new UFS is found, configuring it ...\n");
  2483. ret = ufs_aio_configure_new_ufs(hba);
  2484. if (UFS_ERR_NONE != ret) {
  2485. UFS_DBG_LOGE("[UFS] err: ufs_aio_configure_new_ufs fail\n");
  2486. return ret;
  2487. }
  2488. UFS_DBG_LOGI("[UFS] info: new UFS configuration done\n");
  2489. /* reset device to apply new configurations */
  2490. #ifdef UFS_CFG_DEVICE_RESET_NONPROTECTED
  2491. ufs_aio_reset_device(hba);
  2492. return UFS_ERR_NEED_REINIT_HOST;
  2493. #else
  2494. /* trigger watchdog reset */
  2495. #endif
  2496. return UFS_ERR_NONE; /* shall not happen */
  2497. }
  2498. struct ufs_hba *ufs_aio_get_host(u8 host_id)
  2499. {
  2500. return &g_ufs_hba;
  2501. }
  2502. int ufs_aio_get_device_info(struct ufs_hba *hba)
  2503. {
  2504. struct ufs_aio_scsi_cmd cmd;
  2505. struct ufs_device_info *card_data;
  2506. int err;
  2507. int tag;
  2508. u8 *p;
  2509. int str_len;
  2510. int sn, i;
  2511. char *bp;
  2512. card_data = &hba->dev_info;
  2513. /* Have init and not reconfing device */
  2514. if ((card_data->wmanufacturerid != 0) && (card_data->bootable == 1))
  2515. return 0;
  2516. memset(card_data, 0, sizeof(struct ufs_device_info));
  2517. err = hba->read_descriptor(hba, QUERY_DESC_IDN_DEVICE, 0, 0, (u8 *)&g_ufs_temp_buf[0], ufs_query_desc_max_size[QUERY_DESC_IDN_DEVICE]);
  2518. if (err) {
  2519. UFS_DBG_LOGE("[UFS] err: ufs_aio_get_device_info: failed reading Device Desc. err = %d\n", err);
  2520. return -1;
  2521. }
  2522. p = (u8 *)&g_ufs_temp_buf[0];
  2523. card_data->ufs_ver = p[DEVICE_DESC_PARAM_SPEC_VER] << 8 |
  2524. p[DEVICE_DESC_PARAM_SPEC_VER + 1];
  2525. UFS_DBG_LOGI("[UFS] UFS version %x.%x\n", p[DEVICE_DESC_PARAM_SPEC_VER], p[DEVICE_DESC_PARAM_SPEC_VER + 1]);
  2526. UFS_DBG_LOGI("[UFS] Device Desc:\n");
  2527. ufs_aio_util_dump_table_hex(p, ufs_query_desc_max_size[QUERY_DESC_IDN_DEVICE]);
  2528. if (p[DEVICE_DESC_PARAM_BOOT_ENBL] == 0)
  2529. card_data->bootable = 0;
  2530. else
  2531. card_data->bootable = 1;
  2532. /*
  2533. * getting vendor (manufacturerID) and Bank Index in big endian format
  2534. */
  2535. card_data->wmanufacturerid = p[DEVICE_DESC_PARAM_MANF_ID] << 8 |
  2536. p[DEVICE_DESC_PARAM_MANF_ID + 1];
  2537. if (card_data->wmanufacturerid == UFS_VENDOR_MICRON_MP) {
  2538. err = ufshcd_init_vendor_desc_sizes(hba); /* micron need use selector = 1 to read hpb support */
  2539. if (err) /* not support selector = 1 */
  2540. goto skip_selector;
  2541. err = hba->read_descriptor(hba, QUERY_DESC_IDN_DEVICE, 0, 1,
  2542. (u8 *)&g_ufs_temp_buf[0],
  2543. ufs_query_desc_max_size[QUERY_DESC_IDN_DEVICE]);
  2544. if (err) {
  2545. UFS_DBG_LOGE("[UFS] err: ufs_aio_get_device_info: failed reading HPB Device Desc. err = %d\n", err);
  2546. return -1;
  2547. }
  2548. UFS_DBG_LOGD("[UFS] %s: Device Desc:\n", __func__);
  2549. ufs_aio_util_dump_table_hex(p, ufs_query_desc_max_size[QUERY_DESC_IDN_DEVICE]);
  2550. }
  2551. skip_selector:
  2552. /* Check support HPB feature or not */
  2553. if (p[DEVICE_DESC_PARAM_FEAT_SUP] & 0x80)
  2554. card_data->hpb_support = 1;
  2555. else
  2556. card_data->hpb_support = 0;
  2557. if (card_data->hpb_support) {
  2558. /* Samsung version re-read device descriptor with HPB selector = 1 */
  2559. if (card_data->wmanufacturerid == UFS_VENDOR_SAMSUNG) {
  2560. err = ufshcd_init_vendor_desc_sizes(hba); /* only init when hpb support */
  2561. if (err) /* not support selector = 1 */
  2562. goto skip_selector2;
  2563. err = hba->read_descriptor(hba, QUERY_DESC_IDN_DEVICE, 0, 1,
  2564. (u8 *)&g_ufs_temp_buf[0],
  2565. ufs_query_desc_max_size[QUERY_DESC_IDN_DEVICE]);
  2566. if (err) {
  2567. UFS_DBG_LOGE("[UFS] err: ufs_aio_get_device_info: failed reading HPB Device Desc. err = %d\n", err);
  2568. return -1;
  2569. }
  2570. UFS_DBG_LOGD("[UFS] %s: Device Desc:\n", __func__);
  2571. ufs_aio_util_dump_table_hex(p, ufs_query_desc_max_size[QUERY_DESC_IDN_DEVICE]);
  2572. }
  2573. skip_selector2:
  2574. card_data->hpb_ver = (p[DEVICE_DESC_PARAM_HPB_VER] << 8) |
  2575. p[DEVICE_DESC_PARAM_HPB_VER + 1];
  2576. UFS_DBG_LOGI("HPB Version = %x.%x.%x\n",
  2577. (card_data->hpb_ver >> 8) & 0xFF,
  2578. (card_data->hpb_ver >> 4) & 0xF,
  2579. card_data->hpb_ver & 0xF);
  2580. }
  2581. /* Check support TW feature or not */
  2582. if (p[DEVICE_DESC_PARAM_EXT_FEAT_SUP + 2] & 0x1)
  2583. card_data->tw_support = 1;
  2584. else
  2585. card_data->tw_support = 0;
  2586. if (card_data->tw_support) {
  2587. card_data->tw_red = p[DEVICE_DESC_PARAM_TW_BUF_USER_REDUCED];
  2588. card_data->tw_type = p[DEVICE_DESC_PARAM_TW_BUF_TYPE];
  2589. UFS_DBG_LOGI("TW Reduction = %d, TW Type = %d\n",
  2590. card_data->tw_red,card_data->tw_type);
  2591. if (card_data->wmanufacturerid == UFS_VENDOR_SAMSUNG) {
  2592. card_data->tw_ver = (p[DEVICE_DESC_PARAM_TW_VER] << 8) |
  2593. p[DEVICE_DESC_PARAM_TW_VER + 1];
  2594. UFS_DBG_LOGI("TW Version = %x.%x.%x\n",
  2595. (card_data->tw_ver >> 8) & 0xFF,
  2596. (card_data->tw_ver >> 4) & 0xF,
  2597. card_data->tw_ver & 0xF);
  2598. } else {
  2599. card_data->wb_buf_au = (p[DEVICE_DESC_PARAM_NUM_SHARED_WB_BUF_AU] << 24) |
  2600. (p[DEVICE_DESC_PARAM_NUM_SHARED_WB_BUF_AU + 1] << 16) |
  2601. (p[DEVICE_DESC_PARAM_NUM_SHARED_WB_BUF_AU + 2] << 5) |
  2602. (p[DEVICE_DESC_PARAM_NUM_SHARED_WB_BUF_AU + 3]);
  2603. UFS_DBG_LOGI("Shared WB Buffer AU = 0x%x\n", card_data->wb_buf_au);
  2604. }
  2605. }
  2606. /* Normal device:(0x10, 0x10) / HPB device:(0x10, 0x18) */
  2607. card_data->ud0_base_offset = p[DEVICE_DESC_PARAM_UD_OFFSET];
  2608. card_data->ud_config_len = p[DEVICE_DESC_PARAM_UD_LEN];
  2609. UFS_DBG_LOGI("bbUD0BaseOffset = 0x%d, bUDConfigPLength = 0x%d\n", card_data->ud0_base_offset, card_data->ud_config_len);
  2610. /* Serial number */
  2611. sn = p[DEVICE_DESC_PARAM_SN];
  2612. memset(&g_ufs_temp_buf[0], 0, QUERY_DESC_STRING_MAX_SIZE);
  2613. err = hba->read_descriptor(hba, QUERY_DESC_IDN_STRING, sn, 0,
  2614. (u8 *)&g_ufs_temp_buf[0], QUERY_DESC_STRING_MAX_SIZE);
  2615. if (err) {
  2616. UFS_DBG_LOGE("[UFS] err: read string desc error: %d\n", err);
  2617. } else {
  2618. p = (u8 *)&g_ufs_temp_buf[0];
  2619. card_data->serial_number_len = p[0] - 2;
  2620. UFS_DBG_LOGI("[UFS] String Desc: Serial Number INDEX %d, LENGTH %d\n",
  2621. sn, card_data->serial_number_len);
  2622. ufs_aio_util_dump_table_hex(p, p[0]);
  2623. if (card_data->serial_number_len < MAX_SERAL_NUMBER_LEN) {
  2624. bp = card_data->serial_number;
  2625. for (i = 0; i < card_data->serial_number_len; i++)
  2626. itoa(&bp, (int) p[i+2], 16);
  2627. UFS_DBG_LOGI("[UFS] Serial Number: %s\n", card_data->serial_number);
  2628. } else {
  2629. UFS_DBG_LOGE("[UFS] Serial Number size over reserved 64\n");
  2630. }
  2631. }
  2632. #if defined(MTK_UFS_DRV_DA)
  2633. /* Device Health */
  2634. memset(&g_ufs_temp_buf[0], 0, QUERY_DESC_HEALTH_MAX_SIZE);
  2635. err = hba->read_descriptor(hba, QUERY_DESC_IDN_HEALTH, 0, 0,
  2636. (u8 *)&g_ufs_temp_buf[0], QUERY_DESC_HEALTH_MAX_SIZE);
  2637. if (err) {
  2638. UFS_DBG_LOGE("[UFS] err: read health desc error: %d\n", err);
  2639. } else {
  2640. p = (u8 *)&g_ufs_temp_buf[0];
  2641. card_data->pre_eol_info = p[HEALTH_DESC_B_PRE_EOL_INFO];
  2642. card_data->life_time_est_a = p[HEALTH_DESC_B_DEVICE_LIFE_TIME_EST_A];
  2643. card_data->life_time_est_b = p[HEALTH_DESC_B_DEVICE_LIFE_TIME_EST_B];
  2644. UFS_DBG_LOGI("[UFS] life cycle check, pre_eol_info=%d, life_time_est_a=%d, life_time_est_b=%d\n",
  2645. card_data->pre_eol_info, card_data->life_time_est_a, card_data->life_time_est_b);
  2646. }
  2647. #endif
  2648. /* getting active LU number */
  2649. /* card_data->num_active_lu = p[DEVICE_DESC_PARAM_NUM_LU]; */
  2650. if (!ufshcd_get_free_tag(hba, &tag))
  2651. return -1;
  2652. ufs_aio_scsi_cmd_inquiry(&cmd, tag, 0, (unsigned long *)&g_ufs_temp_buf[0]);
  2653. err = ufshcd_queuecommand(hba, &cmd);
  2654. if (err) {
  2655. UFS_DBG_LOGD("[UFS] err: ufshcd_queuecommand err\n");
  2656. return -1;
  2657. }
  2658. ufshcd_put_tag(hba, tag);
  2659. p = (u8 *)&g_ufs_temp_buf[0];
  2660. /*
  2661. * get product ID and product revision level (fw ver)
  2662. * by INQUIRY command
  2663. */
  2664. /* product ID */
  2665. str_len = ufs_util_sanitize_inquiry_string(&p[16], 16);
  2666. if (str_len) {
  2667. UFS_DBG_LOGD("[UFS] get product id string len 0x%x\n", str_len);
  2668. memcpy(card_data->product_id, &p[16], str_len);
  2669. }
  2670. /* product revision level */
  2671. str_len = ufs_util_sanitize_inquiry_string(&p[32], 4);
  2672. if (str_len) {
  2673. UFS_DBG_LOGD("[UFS] get revision level string len 0x%x\n", str_len);
  2674. memcpy(card_data->product_revision_level, &p[32], str_len);
  2675. }
  2676. return 0;
  2677. }
  2678. #if defined(MTK_UFS_DRV_DA)
  2679. int ufs_aio_get_device_buf_alignment_req(struct ufs_hba *hba)
  2680. {
  2681. if (hba->dev_quirks & UFS_DEVICE_QUIRK_NEED_8_BYTE_ALIGNED_BUF)
  2682. return 8;
  2683. else
  2684. return 4;
  2685. }
  2686. int ufs_aio_read_unit_desc_cfg_param(struct ufs_hba *hba)
  2687. {
  2688. int ret;
  2689. u8 *p;
  2690. int i;
  2691. if (1 == hba->unit_desc_cfg_param_valid)
  2692. return 0;
  2693. /* HPB support, read configuration descriptor with HPB, selector = 1, length = 0xD0(HPB1.0)/0xE2(HPB2.0) */
  2694. if ((hba->dev_info.hpb_support) && (hba->dev_info.wmanufacturerid == UFS_VENDOR_SAMSUNG || hba->dev_info.wmanufacturerid == UFS_VENDOR_MICRON_MP))
  2695. ret = hba->read_descriptor(hba, QUERY_DESC_IDN_CONFIGURATION, 0, 1, (u8 *)&g_ufs_temp_buf[0], ufs_query_desc_max_size[QUERY_DESC_IDN_CONFIGURATION]);
  2696. else
  2697. ret = hba->read_descriptor(hba, QUERY_DESC_IDN_CONFIGURATION, 0, 0, (u8 *)&g_ufs_temp_buf[0], ufs_query_desc_max_size[QUERY_DESC_IDN_CONFIGURATION]);
  2698. if (0 != ret) {
  2699. UFS_DBG_LOGD("[UFS] err: ufs_aio_read_unit_desc_cfg_param read desc error: %d\n", ret);
  2700. return ret;
  2701. }
  2702. p = (u8 *)&g_ufs_temp_buf[0];
  2703. if (((p[0] != ufs_query_desc_max_size[QUERY_DESC_IDN_CONFIGURATION]) && (p[0] != QUERY_DESC_CONFIGURAION_MAX_SIZE_HPB) && (p[0] != QUERY_DESC_CONFIGURAION_MAX_SIZE_HPB_20)) ||
  2704. (p[1] != 0x1)) {
  2705. UFS_DBG_LOGD("[UFS] err: invalid configuration descriptor, len: %d, id: %d\n", p[0], p[1]);
  2706. return -1;
  2707. }
  2708. for (i = 0; i < UFS_UPIU_MAX_GENERAL_LUN; i++) {
  2709. memcpy(&hba->unit_desc_cfg_param[i],
  2710. (p + hba->dev_info.ud0_base_offset + (i * hba->dev_info.ud_config_len)),
  2711. 16);
  2712. }
  2713. hba->unit_desc_cfg_param_valid = 1;
  2714. return 0;
  2715. }
  2716. #endif /* MTK_UFS_DRV_DA */
  2717. int ufs_aio_get_lu_size(struct ufs_hba *hba, u32 lun, u32 *blk_size_in_byte, u32 *blk_cnt)
  2718. {
  2719. struct ufs_aio_scsi_cmd cmd;
  2720. int tag;
  2721. int ret;
  2722. u8 *p;
  2723. u32 blk_cnt_int, blk_size_int;
  2724. if (!ufshcd_get_free_tag(hba, &tag))
  2725. return -1;
  2726. ufs_aio_scsi_cmd_read_capacity(&cmd, (u32)tag, lun, (unsigned long *)&g_ufs_temp_buf[0]);
  2727. ret = ufshcd_queuecommand(hba, &cmd);
  2728. if (UFS_ERR_NONE != ret) {
  2729. UFS_DBG_LOGE("[UFS] err: ufs_aio_get_lu_size: ufshcd_queuecommand err\n");
  2730. return ret;
  2731. }
  2732. ufshcd_put_tag(hba, tag);
  2733. p = (u8 *)&g_ufs_temp_buf[0];
  2734. blk_cnt_int = (((u32)(p[0]) << 24) | ((u32)(p[1]) << 16) | ((u32)(p[2]) << 8) | (u32)(p[3])) + 1;
  2735. if (blk_cnt)
  2736. *blk_cnt = blk_cnt_int;
  2737. blk_size_int = ((u32)(p[4]) << 24) | ((u32)(p[5]) << 16) | ((u32)(p[6]) << 8) | (u32)(p[7]);
  2738. if (blk_size_in_byte)
  2739. *blk_size_in_byte = blk_size_int;
  2740. UFS_DBG_LOGE("[UFS] get lu size: lun=%d, blk_cnt=%d, blk_size_in_byte=%d\n",
  2741. lun, blk_cnt_int, blk_size_int);
  2742. return ret;
  2743. }
  2744. void ufs_aio_dbg_show_reg(struct ufs_hba *hba, u32 addr)
  2745. {
  2746. #if (UFS_DBG_LVL <= UFS_DBG_LVL_DEBUG)
  2747. u32 data;
  2748. data = ufshcd_readl(hba, addr);
  2749. UFS_DBG_LOGD("\t: 0x%x, 0x%x\n", addr, data);
  2750. #endif
  2751. }
  2752. static void ufs_aio_dma_map(ufs_vaddr_t buf, ufs_size_t len, enum dma_data_direction dir)
  2753. {
  2754. if (DMA_FROM_DEVICE == dir) { /* read from device (or write to memory) */
  2755. #if defined(MTK_UFS_DRV_DA) || defined(MTK_UFS_DRV_LK)
  2756. arch_clean_invalidate_cache_range((ufs_vaddr_t)buf, (ufs_size_t)len);
  2757. #elif defined(MTK_UFS_DRV_PRELOADER)
  2758. #if CFG_ENABLE_DCACHE
  2759. plat_clean_invalidate_dcache();
  2760. #endif
  2761. #elif defined(MTK_UFS_DRV_CTP)
  2762. cache_clean_invalidate();
  2763. #else
  2764. #error "MUST PORTING ufs_aio_dma_map()\n"
  2765. #endif
  2766. } else { /* bi-directional or read from memory (or write to device) */
  2767. #if defined(MTK_UFS_DRV_DA) || defined(MTK_UFS_DRV_LK)
  2768. arch_sync_cache_range((ufs_vaddr_t)buf, (ufs_size_t)len);
  2769. #elif defined(MTK_UFS_DRV_PRELOADER)
  2770. #if CFG_ENABLE_DCACHE
  2771. plat_clean_invalidate_dcache();
  2772. #endif
  2773. #elif defined(MTK_UFS_DRV_CTP)
  2774. cache_clean();
  2775. #else
  2776. #error "MUST PORTING ufs_aio_dma_map()\n"
  2777. #endif
  2778. }
  2779. }
  2780. static void ufs_aio_dma_unmap(ufs_vaddr_t buf, ufs_size_t len, enum dma_data_direction dir)
  2781. {
  2782. if (DMA_TO_DEVICE != dir) { /* bi-directional or read from device (or write to memory) */
  2783. #if defined(MTK_UFS_DRV_DA) || defined(MTK_UFS_DRV_LK)
  2784. arch_clean_invalidate_cache_range((ufs_vaddr_t)buf, (ufs_size_t)len);
  2785. #elif defined(MTK_UFS_DRV_PRELOADER)
  2786. #if CFG_ENABLE_DCACHE
  2787. plat_clean_invalidate_dcache();
  2788. #endif
  2789. #elif defined(MTK_UFS_DRV_CTP)
  2790. cache_clean_invalidate();
  2791. #endif
  2792. }
  2793. }
  2794. int ufs_aio_dma_dme_get(struct ufs_hba *hba, u32 attr_sel, u32 *mib_val)
  2795. {
  2796. return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_LOCAL);
  2797. }
  2798. int ufs_aio_dma_dme_peer_get(struct ufs_hba *hba, u32 attr_sel, u32 *mib_val)
  2799. {
  2800. return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_PEER);
  2801. }
  2802. int ufs_aio_dma_dme_set(struct ufs_hba *hba, u32 attr_sel, u32 mib_val)
  2803. {
  2804. return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR, mib_val, DME_LOCAL);
  2805. }
  2806. int ufs_aio_dma_dme_peer_set(struct ufs_hba *hba, u32 attr_sel, u32 mib_val)
  2807. {
  2808. return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR, mib_val, DME_PEER);
  2809. }
  2810. int ufs_aio_dma_nopin_nopout(struct ufs_hba *hba)
  2811. {
  2812. int err = 0;
  2813. int retries;
  2814. for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
  2815. err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
  2816. NOP_OUT_TIMEOUT);
  2817. if (!err)
  2818. break;
  2819. }
  2820. if (err)
  2821. UFS_DBG_LOGE("[UFS] err: NOP OUT failed\n");
  2822. return err;
  2823. }
  2824. int ufs_aio_dma_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
  2825. enum flag_idn idn, bool *flag_res)
  2826. {
  2827. struct ufs_query_req *request = NULL;
  2828. struct ufs_query_res *response = NULL;
  2829. int err, index = 0, selector = 0;
  2830. BUG_ON(!hba);
  2831. ufshcd_init_query(hba, &request, &response, opcode, idn, index,
  2832. selector);
  2833. switch (opcode) {
  2834. case UPIU_QUERY_OPCODE_SET_FLAG:
  2835. case UPIU_QUERY_OPCODE_CLEAR_FLAG:
  2836. case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
  2837. request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
  2838. break;
  2839. case UPIU_QUERY_OPCODE_READ_FLAG:
  2840. request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
  2841. if (!flag_res) {
  2842. /* No dummy reads */
  2843. UFS_DBG_LOGD("Invalid argument for read request\n");
  2844. err = -1;
  2845. goto out_unlock;
  2846. }
  2847. break;
  2848. default:
  2849. UFS_DBG_LOGD("Expected query flag opcode but got = %d\n", opcode);
  2850. err = -1;
  2851. goto out_unlock;
  2852. }
  2853. err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
  2854. if (err) {
  2855. UFS_DBG_LOGD("Sending flag query for idn %d failed, err = %d\n", idn, err);
  2856. goto out_unlock;
  2857. }
  2858. if (flag_res)
  2859. *flag_res = (be32_to_cpu(response->upiu_res.value) &
  2860. MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
  2861. out_unlock:
  2862. return err;
  2863. }
  2864. int ufs_aio_dma_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
  2865. enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
  2866. {
  2867. struct ufs_query_req *request = NULL;
  2868. struct ufs_query_res *response = NULL;
  2869. int err;
  2870. if (!attr_val) {
  2871. UFS_DBG_LOGD("attribute value required for opcode 0x%x\n", opcode);
  2872. err = -1;
  2873. goto out;
  2874. }
  2875. ufshcd_init_query(hba, &request, &response, opcode, idn, index, selector);
  2876. switch (opcode) {
  2877. case UPIU_QUERY_OPCODE_WRITE_ATTR:
  2878. request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
  2879. request->upiu_req.value = cpu_to_be32(*attr_val);
  2880. break;
  2881. case UPIU_QUERY_OPCODE_READ_ATTR:
  2882. request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
  2883. break;
  2884. default:
  2885. UFS_DBG_LOGD("Expected query attr opcode but got = 0x%.2x\n", opcode);
  2886. err = -1;
  2887. goto out;
  2888. }
  2889. err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
  2890. if (err) {
  2891. UFS_DBG_LOGD("opcode 0x%x for idn %d failed, err = %d\n", opcode, idn, err);
  2892. goto out;
  2893. }
  2894. *attr_val = be32_to_cpu(response->upiu_res.value);
  2895. out:
  2896. return err;
  2897. }
  2898. int ufs_aio_dma_read_desc(struct ufs_hba *hba, enum desc_idn desc_id, int desc_index, u8 selector, u8 *buf, u32 size)
  2899. {
  2900. return ufshcd_read_desc_param(hba, desc_id, desc_index, selector, buf, size);
  2901. }
  2902. int ufs_aio_dma_write_desc(struct ufs_hba *hba, enum desc_idn idn, int index, u8 selector, u8 *src_buf, u32 buf_len)
  2903. {
  2904. u32 buf_len_local = buf_len;
  2905. return ufshcd_query_descriptor(hba, UPIU_QUERY_OPCODE_WRITE_DESC, idn, index, selector, src_buf, &buf_len_local);
  2906. }
  2907. #if defined(UFS_CFG_CRYPTO)
  2908. u32 sha256_prebuilt[3][16] = {
  2909. { /*for 128 bit key*/
  2910. 0xa5a5a5a5, 0x5a5a5a5a, 0x55aa55aa, 0xaa55aa55,
  2911. 0x0, 0x0, 0x0, 0x0,
  2912. 0x9309da32, 0x87a0c7fc, 0x1b58a359, 0x14fc38ea,
  2913. 0x68c7b319, 0x0dcb6355, 0xa0e565e9, 0x8765ccb5,
  2914. },
  2915. { /*for 192 bit key*/
  2916. 0xa5a5a5a5, 0x5a5a5a5a, 0x55aa55aa, 0xaa55aa55,
  2917. 0xa5a5a5a5, 0x5a5a5a5a, 0x0, 0x0,
  2918. 0xad168c0f, 0x502b6ae1, 0x30199ad0, 0x69488013,
  2919. 0x42659ad8, 0xf94f95ca, 0xf8084fc1, 0x7a2059e6,
  2920. },
  2921. { /*for 256 bit key*/
  2922. 0xa5a5a5a5, 0x5a5a5a5a, 0x55aa55aa, 0xaa55aa55,
  2923. 0xa5a5a5a5, 0x5a5a5a5a, 0x55aa55aa, 0xaa55aa55,
  2924. 0x6a3d2ed7, 0x228ff865, 0x7ea361c0, 0x88610f0c,
  2925. 0x5f3f1c57, 0x8bc5316e, 0x12ab5119, 0x82e947aa,
  2926. },
  2927. };
  2928. int ufs_aio_crypto_init(struct ufs_hba *hba)
  2929. {
  2930. u32 addr, key_bits, key_bytes, cfg_ptr, alg_id, i;
  2931. u32 key[16];
  2932. union ufs_cap_cfg cpt_cfg;
  2933. union ufs_cpt_cap cpt_cap;
  2934. union ufs_cpt_capx cpt_capx;
  2935. u32 cap_id = 7; /* AES-CBC-ESSIV-128bit */
  2936. u32 cfg_id = 0; /* use slot 0 by default */
  2937. /* in-line encryption feature enable */
  2938. ufs_aio_writel(hba, (ufs_aio_readl(hba, REG_CONTROLLER_ENABLE) | (0x1 << 1)), REG_CONTROLLER_ENABLE);
  2939. /* get algo id */
  2940. cpt_capx.capx_raw = ufs_aio_readl(hba, REG_CRYPTO_CAPABILITY_X + (cap_id << 2));
  2941. alg_id = cpt_capx.capx.alg_id;
  2942. /* get cfg ptr */
  2943. cpt_cap.cap_raw = ufs_aio_readl(hba, REG_CRYPTO_CAPABILITY);
  2944. cfg_ptr = cpt_cap.cap.cfg_ptr;
  2945. addr = (cfg_ptr << 8) + (u32)(cfg_id << 7);
  2946. /* set default key */
  2947. memset(key, 0x5A, sizeof(key));
  2948. /* set crypto cfg */
  2949. memset(&cpt_cfg, 0, sizeof(cpt_cfg));
  2950. cpt_cfg.cfgx.cfg_en = 1;
  2951. cpt_cfg.cfgx.cap_id = (u8)cap_id;
  2952. cpt_cfg.cfgx.du_size = (1 << UFS_CRYPTO_DATA_UNIT_SIZE_4KB);
  2953. /* apply key value according to different algorithms */
  2954. switch (cpt_capx.capx.key_size) {
  2955. case 1:
  2956. key_bits = 128;
  2957. break;
  2958. case 2:
  2959. key_bits = 192;
  2960. break;
  2961. case 3:
  2962. key_bits = 256;
  2963. break;
  2964. case 4:
  2965. key_bits = 512;
  2966. break;
  2967. default :
  2968. key_bits = 128; // shall not happen
  2969. break;
  2970. }
  2971. key_bytes = key_bits >> 3; /* byte count*/
  2972. if (UFS_CRYPTO_ALGO_AES_XTS == alg_id) { // AES-XTS
  2973. memcpy((void *)&(cpt_cfg.cfgx.key[0]), (void *)key, key_bytes);
  2974. memcpy((void *)&(cpt_cfg.cfgx.key[8]), (void *)(key + 16), key_bytes);
  2975. } else if (UFS_CRYPTO_ALGO_ESSIV_AES_CBC == alg_id) { // AES-CBC-ESSIV
  2976. if (128 == key_bits)
  2977. memcpy((void *)&(cpt_cfg.cfgx.key[0]), (void *)sha256_prebuilt[0], 64); // always copy 64 byte = 512 bits, will cover both key and hash(key)
  2978. else if (192 == key_bits)
  2979. memcpy((void *)&(cpt_cfg.cfgx.key[0]), (void *)sha256_prebuilt[1], 64); // always copy 64 byte = 512 bits, will cover both key and hash(key)
  2980. else if (256 == key_bits)
  2981. memcpy((void *)&(cpt_cfg.cfgx.key[0]), (void *)sha256_prebuilt[2], 64); // always copy 64 byte = 512 bits, will cover both key and hash(key)
  2982. } else // AES-ECB
  2983. memcpy((void *)&(cpt_cfg.cfgx.key[0]), (void *)key, key_bytes);
  2984. for (i = 0; i < 32; i++)
  2985. ufs_aio_writel(hba, cpt_cfg.cfgx_raw[i], (addr + i * 4));
  2986. return 0;
  2987. }
  2988. #endif /* MTK_UFS_DRV_CTP */
  2989. #ifdef UFS_CFG_ENABLE_PIO
  2990. /**
  2991. * PIO mode related API bodies
  2992. */
  2993. /* TODO: ensure GPT4 is initialized */
  2994. int ufs_aio_pio_cport_direct_write(struct ufs_hba *hba, const unsigned char *data, unsigned long len, int eom, int retry_ms)
  2995. {
  2996. u32 ctrl = 0, reg = 0;
  2997. u32 val_low = 0, val_high = 0;
  2998. int cnt = 0;
  2999. int last = 0;
  3000. int wordLen;
  3001. int i = 0;
  3002. u32 timeout_tick = 0, start_tick = 0;
  3003. wordLen = 8;
  3004. cnt = len / wordLen;
  3005. last = len % wordLen;
  3006. if (last != 0)
  3007. return UFS_CPORT_DIR_ACC_ALIGN_8BYTE_ERR; /* should be 8-byte align in bootROM */
  3008. UFS_DBG_LOGD("cnt is %d\n", cnt);
  3009. if (cnt) {
  3010. /* write data */
  3011. for (i = 0; i < len ; i += wordLen) {
  3012. if (data) {
  3013. val_low = ((*(data + i)) << 24) | ((*(data + i + 1)) << 16) | ((*(data + i + 2)) << 8) | (*(data + i + 3));
  3014. val_high = ((*(data + i + 4)) << 24) | ((*(data + i + 5)) << 16) | ((*(data + i + 6)) << 8) | (*(data + i + 7));
  3015. } else {
  3016. return UFS_CPORT_DIR_ACC_ERR;
  3017. }
  3018. /* write bit enable and control */
  3019. ctrl = 0xFF00 | (1 << 28) | (1 << 29); /* all bytes enable, CADEN on, word alignment on */
  3020. if ((i >= len-wordLen) && (last == 0) && eom)
  3021. ctrl |= (1<<16);
  3022. ufshcd_writel(hba, ctrl, REG_CDACFG);
  3023. ufshcd_writel(hba, val_high, REG_CDATX1);
  3024. ufshcd_writel(hba, val_low, REG_CDATX2);
  3025. /* get timeout tick */
  3026. #ifdef MTK_UFS_DRV_DA
  3027. timeout_tick = gpt4_time2tick_ms(retry_ms);
  3028. start_tick = gpt4_get_current_tick();
  3029. #endif
  3030. while (1) {
  3031. reg = ufshcd_readl(hba, REG_CDASTA);
  3032. if ((reg & (UFS_HCI_REGISTER_FLAG_CDARES | UFS_HCI_REGISTER_FLAG_CDABUSY)) == 0) {
  3033. break; /* ready and no error */
  3034. } else if ((reg & UFS_HCI_REGISTER_FLAG_CDARES) && ((reg & UFS_HCI_REGISTER_FLAG_CDABUSY) == 0)) { /* ready but error */
  3035. UFS_DBG_LOGD("%s #%d: CPort direct access error. error code: 0x%04x\n", __func__, __LINE__, (reg)>>20);
  3036. return UFS_CPORT_DIR_ACC_ERR;
  3037. } else if (gpt4_timeout_tick(start_tick, timeout_tick)) {
  3038. UFS_DBG_LOGD("%s #%d: CPort direct access time out\n", __func__, __LINE__);
  3039. return UFS_CPORT_DIR_ACC_ERR_TIMEOUT;
  3040. }
  3041. }
  3042. }
  3043. }
  3044. return UFS_ERR_NONE;
  3045. }
  3046. int ufs_aio_pio_cport_direct_read(struct ufs_hba *hba, unsigned char *data, unsigned long buflen, u32 *plen, int retry_ms, bool isDummy, u32 real_byte)
  3047. {
  3048. u32 ctrl = 0, reg = 0;
  3049. u32 val_low_u = 0, val_high_u = 0;
  3050. int i = 0;
  3051. u32 buf_index = 0;
  3052. u32 timeout_tick = 0, start_tick = 0;
  3053. /* UFS_DBG_LOGD("Isdummy: %d real_byte:%d read_bytes: %d\n", isDummy, real_byte, *byte_read); */
  3054. *plen = 0;
  3055. /* get timeout tick */
  3056. #ifdef MTK_UFS_DRV_DA
  3057. timeout_tick = gpt4_time2tick_ms(retry_ms);
  3058. start_tick = gpt4_get_current_tick();
  3059. #endif
  3060. while (1) {
  3061. reg = ufshcd_readl(hba, REG_CDASTA);
  3062. if ((reg & UFS_HCI_REGISTER_FLAG_CDASTA)) {
  3063. break;
  3064. } else if (gpt4_timeout_tick(start_tick, timeout_tick)) {
  3065. UFS_DBG_LOGD("%s #%d: CPort direct access time out\n", __func__, __LINE__);
  3066. return UFS_CPORT_DIR_ACC_ERR_TIMEOUT;
  3067. }
  3068. }
  3069. while (reg & UFS_HCI_REGISTER_FLAG_CDASTA) { /* CDASTA (indicates if there is a new data in the buffer */
  3070. if (buf_index >= buflen) {
  3071. UFS_DBG_LOGD("Buffer full so stop reading. buf_index: %d, buflen: %d\n", (int)buf_index, (int)buflen);
  3072. break;
  3073. }
  3074. val_high_u = ufshcd_readl(hba, REG_CDARX1);
  3075. val_low_u = ufshcd_readl(hba, REG_CDARX2);
  3076. ctrl = (reg & 0xFF00) >> 8;
  3077. #ifdef MSG_DEBUG
  3078. #ifdef UFS_UPIU_DEBUG
  3079. /* for debug only */
  3080. if (buflen == 32) {
  3081. UFS_DBG_LOGD("read RX1: 0x%x\n", val_high_u);
  3082. UFS_DBG_LOGD("read RX2: 0x%x\n", val_low_u);
  3083. UFS_DBG_LOGD("read ctrl: 0x%x\n", ctrl);
  3084. }
  3085. #endif
  3086. #endif
  3087. for (i = 0; i < 4; i++) {
  3088. if (ctrl & 0x80) {
  3089. #if 0
  3090. if ((isDummy == TRUE) && (*byte_read >= real_byte)) {
  3091. buf_index++;
  3092. *byte_read++;
  3093. } else
  3094. #endif
  3095. {
  3096. data[buf_index] = (val_low_u >> (3-i)*8) & 0xff;
  3097. buf_index++;
  3098. #if 0
  3099. if (isDummy == TRUE)
  3100. *byte_read++;
  3101. #endif
  3102. }
  3103. }
  3104. ctrl = ctrl << 1;
  3105. }
  3106. if (buf_index >= buflen) { /* Solve 4-byte align data over read problem. ex: expect: 20 byte read: 32byte */
  3107. UFS_DBG_LOGD("Buffer full so stop reading. buf_index: %d, buflen: %d\n", (int)buf_index, (int)buflen);
  3108. break;
  3109. }
  3110. if (i == 4) {
  3111. for (i = 0; i < 4; i++) {
  3112. if (ctrl & 0x80) {
  3113. #if 0
  3114. if ((isDummy == TRUE) && (*byte_read >= real_byte)) {
  3115. buf_index++;
  3116. *byte_read++;
  3117. } else
  3118. #endif
  3119. {
  3120. data[buf_index] = (val_high_u >> (3 - i) * 8) & 0xff;
  3121. buf_index++;
  3122. #if 0
  3123. if (isDummy == TRUE)
  3124. *byte_read++;
  3125. #endif
  3126. }
  3127. }
  3128. ctrl = ctrl << 1;
  3129. }
  3130. }
  3131. reg = ufshcd_readl(hba, REG_CDASTA);
  3132. if (buf_index < buflen) {
  3133. /* get timeout tick */
  3134. #ifdef MTK_UFS_DRV_DA
  3135. timeout_tick = gpt4_time2tick_ms(retry_ms);
  3136. start_tick = gpt4_get_current_tick();
  3137. #endif
  3138. while (1) {
  3139. reg = ufshcd_readl(hba, REG_CDASTA);
  3140. if ((reg & UFS_HCI_REGISTER_FLAG_CDASTA)) {
  3141. break;
  3142. } else if (gpt4_timeout_tick(start_tick, timeout_tick)) {
  3143. UFS_DBG_LOGD("buf_index: %d, buflen: %d", (int)buf_index, (int)buflen);
  3144. UFS_DBG_LOGD("CPort direct access time out\n");
  3145. return UFS_CPORT_DIR_ACC_ERR_TIMEOUT;
  3146. }
  3147. }
  3148. }
  3149. }
  3150. UFS_DBG_LOGD("data length is 0x%x\n", (int)buf_index);
  3151. *plen = buf_index;
  3152. return UFS_ERR_NONE;
  3153. }
  3154. int ufs_aio_pio_query_flag(struct ufs_hba *hba, enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
  3155. {
  3156. if (UPIU_QUERY_OPCODE_SET_FLAG == opcode)
  3157. return ufs_aio_pio_set_flag(hba, idn);
  3158. else if (UPIU_QUERY_OPCODE_READ_FLAG == opcode)
  3159. return ufs_aio_pio_read_flag(hba, idn, flag_res);
  3160. else
  3161. return -1;
  3162. }
  3163. int ufs_aio_pio_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
  3164. enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
  3165. {
  3166. return -1;
  3167. }
  3168. int ufs_aio_pio_write_desc(struct ufs_hba *hba, enum desc_idn idn, int index, u8 selector, u8 *src_buf, u32 buf_len)
  3169. {
  3170. return -1;
  3171. }
  3172. int ufs_aio_pio_read_flag(struct ufs_hba *hba, unsigned char flag_idx, bool *value)
  3173. {
  3174. int ret = 0;
  3175. /* unsigned char buf[14]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; */
  3176. u32 resp_read = 0;
  3177. unsigned char req_upiu[32] = {0x0};
  3178. unsigned char resp_upiu[32] = {0x0};
  3179. req_upiu[0] = UPIU_TRANSACTION_QUERY_REQ;
  3180. req_upiu[3] = 0;
  3181. req_upiu[5] = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
  3182. req_upiu[12] = UPIU_QUERY_OPCODE_READ_FLAG;
  3183. req_upiu[13] = flag_idx; /* flag_idn */
  3184. ret = ufs_aio_pio_cport_direct_write(hba, req_upiu, LEN_32, 1, LEN_1000); /* send query request upiu */
  3185. UFS_DBG_LOGD("Not sleep, use polling!!!\n");
  3186. ret = ufs_aio_pio_cport_direct_read(hba, resp_upiu, LEN_32, &resp_read, LEN_1000, FALSE, 0); /* get query response upiu */
  3187. if (resp_upiu[6] != UPIU_RESPONSE_SUCCESS)
  3188. return UFS_EXIT_FAILURE;
  3189. UFS_DBG_LOGD("flag read flag_ind:%d value:%d\n", flag_idx, resp_upiu[23]);
  3190. *value = (bool)resp_upiu[23];
  3191. if (resp_read > 0) {
  3192. unsigned long i;
  3193. UFS_DBG_LOGD("get read flag query response upiu (byte %d):\n", resp_read);
  3194. for (i = 0; i < resp_read; i++) {
  3195. UFS_DBG_LOGD("0x%x ", resp_upiu[i]);
  3196. }
  3197. UFS_DBG_LOGD("\n");
  3198. }
  3199. return ret;
  3200. }
  3201. /* @SET FLAG @In2:flag_idn */
  3202. /* ex: tools 57 1 fDeviceInit Set */
  3203. int ufs_aio_pio_set_flag(struct ufs_hba *hba, unsigned char flag_idx)
  3204. {
  3205. int ret = 0;
  3206. u32 resp_read = 0;
  3207. unsigned char resp_upiu[32] = {0x0};
  3208. unsigned char req_upiu[32] = {0x0};
  3209. req_upiu[0] = UPIU_TRANSACTION_QUERY_REQ;
  3210. req_upiu[3] = 0;
  3211. req_upiu[5] = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
  3212. req_upiu[12] = UPIU_QUERY_OPCODE_SET_FLAG;
  3213. req_upiu[13] = flag_idx;
  3214. UFS_DBG_LOGD("flag set flag_ind:%d\n", flag_idx);
  3215. ret = ufs_aio_pio_cport_direct_write(hba, req_upiu, LEN_32, 1, LEN_1000); /* send query request upiu */
  3216. UFS_DBG_LOGD("Not sleep, use polling!!!\n");
  3217. ret = ufs_aio_pio_cport_direct_read(hba, resp_upiu, LEN_32, &resp_read, LEN_1000, FALSE, 0); /* get query response upiu */
  3218. if (resp_upiu[6] != UPIU_RESPONSE_SUCCESS)
  3219. return UFS_EXIT_FAILURE;
  3220. if (resp_read > 0) {
  3221. unsigned long i;
  3222. UFS_DBG_LOGD("get set flag query response upiu (byte: %d):\n", resp_read);
  3223. for (i = 0; i < resp_read; i++) {
  3224. UFS_DBG_LOGD("0x%x ", resp_upiu[i]);
  3225. }
  3226. UFS_DBG_LOGD("\n");
  3227. }
  3228. return ret;
  3229. }
  3230. int ufs_aio_pio_nopin_nopout(struct ufs_hba *hba)
  3231. {
  3232. int ret = 0;
  3233. unsigned char nopout_cmd[UFS_AIO_PIO_UPIU_CMD_BUF_SIZE], nopin_cmd[UFS_AIO_PIO_UPIU_CMD_BUF_SIZE];
  3234. u32 resp_read = 0;
  3235. memset(nopout_cmd, 0, sizeof(nopout_cmd));
  3236. memset(nopin_cmd, 0, sizeof(nopin_cmd));
  3237. ret = ufs_aio_pio_cport_direct_write(hba, nopout_cmd, LEN_32, 1, LEN_1000); /* send nopout */
  3238. if (ret != UFS_ERR_NONE)
  3239. return UFS_NOPOUT_NOPIN_CPORT_TX_ERR;
  3240. ret = ufs_aio_pio_cport_direct_read(hba, nopin_cmd, LEN_32, &resp_read, LEN_1000, FALSE, 0); /* get nopin */
  3241. if (ret != UFS_ERR_NONE)
  3242. return UFS_NOPOUT_NOPIN_CPORT_RX_ERR;
  3243. #ifdef MSG_DEBUG
  3244. #ifdef UFS_UPIU_DEBUG
  3245. if (resp_read > 0) {
  3246. unsigned long i;
  3247. UFS_DBG_LOGD("get nopin upiu (byte:%d):\n", resp_read);
  3248. for (i = 0; i < resp_read; i++) {
  3249. UFS_DBG_LOGD("0x%x ", nopin_cmd[i]);
  3250. }
  3251. UFS_DBG_LOGD("\n");
  3252. }
  3253. #endif
  3254. #endif
  3255. /* Error handling */
  3256. if ((nopin_cmd[0] != UPIU_TRANSACTION_NOP_IN) || (nopin_cmd[6] != UPIU_RESPONSE_SUCCESS))
  3257. return UFS_NOPOUT_NOPIN_ERR;
  3258. return ret;
  3259. }
  3260. int ufs_aio_pio_read_desc(struct ufs_hba *hba, enum desc_idn desc_id, int desc_index, u8 selector, u8 *buf, u32 buf_size)
  3261. {
  3262. int ret = UFS_ERR_NONE;
  3263. u32 resp_read = 0;
  3264. memset(ufs_req_upiu, 0, sizeof(ufs_req_upiu));
  3265. memset(ufs_resp_upiu, 0, sizeof(ufs_resp_upiu));
  3266. memset(buf, 0, buf_size);
  3267. ufs_req_upiu[0] = UPIU_TRANSACTION_QUERY_REQ;
  3268. ufs_req_upiu[3] = 0; /* Task Tag */
  3269. ufs_req_upiu[5] = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
  3270. ufs_req_upiu[12] = UPIU_QUERY_OPCODE_READ_DESC;
  3271. ufs_req_upiu[13] = desc_id; /* Descriptor IDN */
  3272. ufs_req_upiu[14] = desc_index; /* Index */
  3273. ufs_req_upiu[15] = selector; /* Index */
  3274. ufs_req_upiu[10] = 0; /* Data segment length (MSB) */
  3275. ufs_req_upiu[11] = buf_size; /* Data segment length (LSB) */
  3276. ufs_req_upiu[18] = 0; /* Transaction Specific Fields for READ DESCRIPTOR OPCODE: Length (MSB) */
  3277. ufs_req_upiu[19] = buf_size; /* Transaction Specific Fields for READ DESCRIPTOR OPCODE: Length (LSB) */
  3278. ret = ufs_aio_pio_cport_direct_write(hba, ufs_req_upiu, LEN_32, 1, LEN_1000); /* send query request UPIU */
  3279. if (ret != UFS_ERR_NONE)
  3280. return UFS_READ_DESC_CPORT_TX_ERR;
  3281. ret = ufs_aio_pio_cport_direct_read(hba, ufs_resp_upiu, LEN_32, &resp_read, LEN_1000, FALSE, 0); /* get query response UPIU (top 32 bytes, before data) */
  3282. if (ret != UFS_ERR_NONE)
  3283. return UFS_READ_DESC_CPORT_RX_ERR;
  3284. #ifdef MSG_DEBUG
  3285. #ifdef UFS_UPIU_DEBUG
  3286. if (resp_read > 0) {
  3287. unsigned long i;
  3288. UFS_DBG_LOGD("get read device descriptor before data (byte:%d):\n", resp_read);
  3289. for (i = 0; i < resp_read; i++) {
  3290. UFS_DBG_LOGD("0x%x ", ufs_resp_upiu[i]);
  3291. }
  3292. UFS_DBG_LOGD("\n");
  3293. }
  3294. #endif
  3295. #endif
  3296. /* Error handling */
  3297. if ((ufs_resp_upiu[0] != UPIU_TRANSACTION_QUERY_RSP) || (ufs_resp_upiu[6] != UPIU_RESPONSE_SUCCESS))
  3298. return UFS_READ_DESC_ERR;
  3299. ret = ufs_aio_pio_cport_direct_read(hba, buf, ((ufs_resp_upiu[18] << 8) | ufs_resp_upiu[19]), &resp_read, LEN_1000, FALSE, 0); /* get query response UPIU (data part) */
  3300. if (ret != UFS_ERR_NONE)
  3301. return UFS_READ_DESC_CPORT_RX_ERR;
  3302. #ifdef MSG_DEBUG
  3303. #ifdef UFS_UPIU_DEBUG
  3304. if (resp_read > 0) {
  3305. unsigned long i;
  3306. UFS_DBG_LOGD("get device descriptor data (byte:%d)\n", resp_read);
  3307. for (i = 0; i < resp_read; i++) {
  3308. UFS_DBG_LOGD("0x%x ", buf[i]);
  3309. }
  3310. UFS_DBG_LOGD("\n");
  3311. }
  3312. #endif
  3313. #endif
  3314. return ret;
  3315. }
  3316. /* int ufs_aio_pio_dme_set(struct ufs_hba * hba, u32 uic_cmd, uint16 mib_attribute, uint16 gen_select_index, u32 value, u32 *return_code, int retry_ms) */
  3317. int ufs_aio_pio_dme_set_attr(struct ufs_hba *hba, u32 attr_sel, u8 attr_set, u32 mib_val, u8 peer, u32 *return_code, int retry_ms)
  3318. {
  3319. u32 ret = 0;
  3320. u32 timeout_tick = 0, start_tick = 0;
  3321. if (return_code)
  3322. *return_code = 0;
  3323. ufshcd_writel(hba, attr_sel, REG_UIC_COMMAND_ARG_1);
  3324. ufshcd_writel(hba, UIC_ARG_ATTR_TYPE(attr_set), REG_UIC_COMMAND_ARG_2);
  3325. ufshcd_writel(hba, mib_val, REG_UIC_COMMAND_ARG_3);
  3326. ufshcd_writel(hba, (peer ? UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET) & COMMAND_OPCODE_MASK, REG_UIC_COMMAND);
  3327. /* get timeout tick */
  3328. #ifdef MTK_UFS_DRV_DA
  3329. timeout_tick = gpt4_time2tick_ms(retry_ms);
  3330. start_tick = gpt4_get_current_tick();
  3331. #endif
  3332. while (1) {
  3333. ret = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
  3334. if ((ret & 0x400) == 0x400) {
  3335. ufshcd_writel(hba, 0x400, REG_INTERRUPT_STATUS);
  3336. break;
  3337. }
  3338. if (gpt4_timeout_tick(start_tick, timeout_tick)) {
  3339. UFS_DBG_LOGD("ERROR : UIC command fail (timeout).\n");
  3340. return UFS_UIC_CMD_ERR_TIMEOUT;
  3341. }
  3342. }
  3343. ret = ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2);
  3344. if (ret & 0xFF) {
  3345. UFS_DBG_LOGD("ERROR : ufs_aio_pio_dme_set_attr fail (error arg2: 0x%x)\n", ret);
  3346. ufs_aio_dbg_show_reg(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
  3347. ufs_aio_dbg_show_reg(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
  3348. ufs_aio_dbg_show_reg(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
  3349. ufs_aio_dbg_show_reg(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
  3350. ufs_aio_dbg_show_reg(hba, REG_UIC_ERROR_CODE_DME);
  3351. if (return_code)
  3352. *return_code = ret;
  3353. return UFS_UIC_CMD_ERR;
  3354. }
  3355. return UFS_ERR_NONE;
  3356. }
  3357. int ufs_aio_pio_dme_set(struct ufs_hba *hba, u32 attr_sel, u32 mib_val)
  3358. {
  3359. return ufs_aio_pio_dme_set_attr(hba, attr_sel, ATTR_SET_NOR, mib_val, DME_LOCAL, NULL, 1000);
  3360. }
  3361. int ufs_aio_pio_dme_peer_set(struct ufs_hba *hba, u32 attr_sel, u32 mib_val)
  3362. {
  3363. return ufs_aio_pio_dme_set_attr(hba, attr_sel, ATTR_SET_NOR, mib_val, DME_PEER, NULL, 1000);
  3364. }
  3365. int ufs_aio_pio_dme_get_attr(struct ufs_hba *hba, u32 attr_sel, u32 *mib_val, u8 peer, u32 *return_code, int retry_ms)
  3366. {
  3367. u32 ret;
  3368. u32 timeout_tick = 0, start_tick = 0;
  3369. if (return_code)
  3370. *return_code = 0;
  3371. ufshcd_writel(hba, attr_sel, REG_UIC_COMMAND_ARG_1);
  3372. ufshcd_writel(hba, 0, REG_UIC_COMMAND_ARG_2);
  3373. ufshcd_writel(hba, 0, REG_UIC_COMMAND_ARG_3);
  3374. ufshcd_writel(hba, (peer ? UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET) & COMMAND_OPCODE_MASK, REG_UIC_COMMAND);
  3375. /* get timeout tick */
  3376. #ifdef MTK_UFS_DRV_DA
  3377. timeout_tick = gpt4_time2tick_ms(retry_ms);
  3378. start_tick = gpt4_get_current_tick();
  3379. #endif
  3380. while (1) {
  3381. ret = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
  3382. if ((ret & 0x400) == 0x400) {
  3383. ufshcd_writel(hba, 0x400, REG_INTERRUPT_STATUS);
  3384. break;
  3385. }
  3386. if (gpt4_timeout_tick(start_tick, timeout_tick)) {
  3387. UFS_DBG_LOGD("ERROR : UIC command fail (timeout).\n");
  3388. return UFS_UIC_CMD_ERR_TIMEOUT;
  3389. }
  3390. }
  3391. ret = ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2);
  3392. if (ret & 0xFF) {
  3393. UFS_DBG_LOGD("ERROR : UIC command fail.\n");
  3394. ufs_aio_dbg_show_reg(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
  3395. ufs_aio_dbg_show_reg(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
  3396. ufs_aio_dbg_show_reg(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
  3397. ufs_aio_dbg_show_reg(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
  3398. ufs_aio_dbg_show_reg(hba, REG_UIC_ERROR_CODE_DME);
  3399. if (return_code)
  3400. *return_code = ret;
  3401. return UFS_UIC_CMD_ERR;
  3402. }
  3403. *mib_val = ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
  3404. return UFS_ERR_NONE;
  3405. }
  3406. int ufs_aio_pio_dme_get(struct ufs_hba *hba, u32 attr_sel, u32 *mib_val)
  3407. {
  3408. return ufs_aio_pio_dme_get_attr(hba, attr_sel, mib_val, DME_LOCAL, NULL, 1000);
  3409. }
  3410. int ufs_aio_pio_dme_peer_get(struct ufs_hba *hba, u32 attr_sel, u32 *mib_val)
  3411. {
  3412. return ufs_aio_pio_dme_get_attr(hba, attr_sel, mib_val, DME_PEER, NULL, 1000);
  3413. }
  3414. #endif /* UFS_CFG_ENABLE_PIO */