sh-sci.c 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
  4. *
  5. * Copyright (C) 2002 - 2011 Paul Mundt
  6. * Copyright (C) 2015 Glider bvba
  7. * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
  8. *
  9. * based off of the old drivers/char/sh-sci.c by:
  10. *
  11. * Copyright (C) 1999, 2000 Niibe Yutaka
  12. * Copyright (C) 2000 Sugioka Toshinobu
  13. * Modified to support multiple serial ports. Stuart Menefy (May 2000).
  14. * Modified to support SecureEdge. David McCullough (2002)
  15. * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
  16. * Removed SH7300 support (Jul 2007).
  17. */
  18. #undef DEBUG
  19. #include <linux/bitops.h>
  20. #include <linux/clk.h>
  21. #include <linux/console.h>
  22. #include <linux/cpufreq.h>
  23. #include <linux/ctype.h>
  24. #include <linux/delay.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/dmaengine.h>
  27. #include <linux/err.h>
  28. #include <linux/errno.h>
  29. #include <linux/init.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/io.h>
  32. #include <linux/ioport.h>
  33. #include <linux/ktime.h>
  34. #include <linux/major.h>
  35. #include <linux/minmax.h>
  36. #include <linux/mm.h>
  37. #include <linux/module.h>
  38. #include <linux/of.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/pm_runtime.h>
  41. #include <linux/reset.h>
  42. #include <linux/scatterlist.h>
  43. #include <linux/serial.h>
  44. #include <linux/serial_core.h>
  45. #include <linux/serial_sci.h>
  46. #include <linux/sh_dma.h>
  47. #include <linux/slab.h>
  48. #include <linux/string.h>
  49. #include <linux/sysrq.h>
  50. #include <linux/timer.h>
  51. #include <linux/tty.h>
  52. #include <linux/tty_flip.h>
  53. #ifdef CONFIG_SUPERH
  54. #include <asm/platform_early.h>
  55. #include <asm/sh_bios.h>
  56. #endif
  57. #include "rsci.h"
  58. #include "serial_mctrl_gpio.h"
  59. #include "sh-sci-common.h"
  60. #define SCI_MAJOR 204
  61. #define SCI_MINOR_START 8
  62. /*
  63. * SCI register subset common for all port types.
  64. * Not all registers will exist on all parts.
  65. */
  66. enum {
  67. SCSMR, /* Serial Mode Register */
  68. SCBRR, /* Bit Rate Register */
  69. SCSCR, /* Serial Control Register */
  70. SCxSR, /* Serial Status Register */
  71. SCFCR, /* FIFO Control Register */
  72. SCFDR, /* FIFO Data Count Register */
  73. SCxTDR, /* Transmit (FIFO) Data Register */
  74. SCxRDR, /* Receive (FIFO) Data Register */
  75. SCLSR, /* Line Status Register */
  76. SCTFDR, /* Transmit FIFO Data Count Register */
  77. SCRFDR, /* Receive FIFO Data Count Register */
  78. SCSPTR, /* Serial Port Register */
  79. HSSRR, /* Sampling Rate Register */
  80. SCPCR, /* Serial Port Control Register */
  81. SCPDR, /* Serial Port Data Register */
  82. SCDL, /* BRG Frequency Division Register */
  83. SCCKS, /* BRG Clock Select Register */
  84. HSRTRGR, /* Rx FIFO Data Count Trigger Register */
  85. HSTTRGR, /* Tx FIFO Data Count Trigger Register */
  86. SEMR, /* Serial extended mode register */
  87. };
  88. /* SCSMR (Serial Mode Register) */
  89. #define SCSMR_C_A BIT(7) /* Communication Mode */
  90. #define SCSMR_CSYNC BIT(7) /* - Clocked synchronous mode */
  91. #define SCSMR_ASYNC 0 /* - Asynchronous mode */
  92. #define SCSMR_CHR BIT(6) /* 7-bit Character Length */
  93. #define SCSMR_PE BIT(5) /* Parity Enable */
  94. #define SCSMR_ODD BIT(4) /* Odd Parity */
  95. #define SCSMR_STOP BIT(3) /* Stop Bit Length */
  96. #define SCSMR_CKS 0x0003 /* Clock Select */
  97. /* Serial Mode Register, SCIFA/SCIFB only bits */
  98. #define SCSMR_CKEDG BIT(12) /* Transmit/Receive Clock Edge Select */
  99. #define SCSMR_SRC_MASK 0x0700 /* Sampling Control */
  100. #define SCSMR_SRC_16 0x0000 /* Sampling rate 1/16 */
  101. #define SCSMR_SRC_5 0x0100 /* Sampling rate 1/5 */
  102. #define SCSMR_SRC_7 0x0200 /* Sampling rate 1/7 */
  103. #define SCSMR_SRC_11 0x0300 /* Sampling rate 1/11 */
  104. #define SCSMR_SRC_13 0x0400 /* Sampling rate 1/13 */
  105. #define SCSMR_SRC_17 0x0500 /* Sampling rate 1/17 */
  106. #define SCSMR_SRC_19 0x0600 /* Sampling rate 1/19 */
  107. #define SCSMR_SRC_27 0x0700 /* Sampling rate 1/27 */
  108. /* Serial Control Register, SCI only bits */
  109. #define SCSCR_TEIE BIT(2) /* Transmit End Interrupt Enable */
  110. /* Serial Control Register, SCIFA/SCIFB only bits */
  111. #define SCSCR_TDRQE BIT(15) /* Tx Data Transfer Request Enable */
  112. #define SCSCR_RDRQE BIT(14) /* Rx Data Transfer Request Enable */
  113. /* Serial Control Register, HSCIF-only bits */
  114. #define HSSCR_TOT_SHIFT 14
  115. /* SCxSR (Serial Status Register) on SCI */
  116. #define SCI_TDRE BIT(7) /* Transmit Data Register Empty */
  117. #define SCI_RDRF BIT(6) /* Receive Data Register Full */
  118. #define SCI_ORER BIT(5) /* Overrun Error */
  119. #define SCI_FER BIT(4) /* Framing Error */
  120. #define SCI_PER BIT(3) /* Parity Error */
  121. #define SCI_TEND BIT(2) /* Transmit End */
  122. #define SCI_RESERVED 0x03 /* All reserved bits */
  123. #define SCI_DEFAULT_ERROR_MASK (SCI_PER | SCI_FER)
  124. #define SCI_RDxF_CLEAR (u32)(~(SCI_RESERVED | SCI_RDRF))
  125. #define SCI_ERROR_CLEAR (u32)(~(SCI_RESERVED | SCI_PER | SCI_FER | SCI_ORER))
  126. #define SCI_TDxE_CLEAR (u32)(~(SCI_RESERVED | SCI_TEND | SCI_TDRE))
  127. #define SCI_BREAK_CLEAR (u32)(~(SCI_RESERVED | SCI_PER | SCI_FER | SCI_ORER))
  128. /* SCxSR (Serial Status Register) on SCIF, SCIFA, SCIFB, HSCIF */
  129. #define SCIF_ER BIT(7) /* Receive Error */
  130. #define SCIF_TEND BIT(6) /* Transmission End */
  131. #define SCIF_TDFE BIT(5) /* Transmit FIFO Data Empty */
  132. #define SCIF_BRK BIT(4) /* Break Detect */
  133. #define SCIF_FER BIT(3) /* Framing Error */
  134. #define SCIF_PER BIT(2) /* Parity Error */
  135. #define SCIF_RDF BIT(1) /* Receive FIFO Data Full */
  136. #define SCIF_DR BIT(0) /* Receive Data Ready */
  137. /* SCIF only (optional) */
  138. #define SCIF_PERC 0xf000 /* Number of Parity Errors */
  139. #define SCIF_FERC 0x0f00 /* Number of Framing Errors */
  140. /*SCIFA/SCIFB and SCIF on SH7705/SH7720/SH7721 only */
  141. #define SCIFA_ORER BIT(9) /* Overrun Error */
  142. #define SCIF_DEFAULT_ERROR_MASK (SCIF_PER | SCIF_FER | SCIF_BRK | SCIF_ER)
  143. #define SCIF_RDxF_CLEAR (u32)(~(SCIF_DR | SCIF_RDF))
  144. #define SCIF_ERROR_CLEAR (u32)(~(SCIF_PER | SCIF_FER | SCIF_ER))
  145. #define SCIF_TDxE_CLEAR (u32)(~(SCIF_TDFE))
  146. #define SCIF_BREAK_CLEAR (u32)(~(SCIF_PER | SCIF_FER | SCIF_BRK))
  147. /* SCFCR (FIFO Control Register) */
  148. #define SCFCR_RTRG1 BIT(7) /* Receive FIFO Data Count Trigger */
  149. #define SCFCR_RTRG0 BIT(6)
  150. #define SCFCR_TTRG1 BIT(5) /* Transmit FIFO Data Count Trigger */
  151. #define SCFCR_TTRG0 BIT(4)
  152. #define SCFCR_MCE BIT(3) /* Modem Control Enable */
  153. #define SCFCR_TFRST BIT(2) /* Transmit FIFO Data Register Reset */
  154. #define SCFCR_RFRST BIT(1) /* Receive FIFO Data Register Reset */
  155. #define SCFCR_LOOP BIT(0) /* Loopback Test */
  156. /* SCLSR (Line Status Register) on (H)SCIF */
  157. #define SCLSR_TO BIT(2) /* Timeout */
  158. #define SCLSR_ORER BIT(0) /* Overrun Error */
  159. /* SCSPTR (Serial Port Register), optional */
  160. #define SCSPTR_RTSIO BIT(7) /* Serial Port RTS# Pin Input/Output */
  161. #define SCSPTR_RTSDT BIT(6) /* Serial Port RTS# Pin Data */
  162. #define SCSPTR_CTSIO BIT(5) /* Serial Port CTS# Pin Input/Output */
  163. #define SCSPTR_CTSDT BIT(4) /* Serial Port CTS# Pin Data */
  164. #define SCSPTR_SCKIO BIT(3) /* Serial Port Clock Pin Input/Output */
  165. #define SCSPTR_SCKDT BIT(2) /* Serial Port Clock Pin Data */
  166. #define SCSPTR_SPB2IO BIT(1) /* Serial Port Break Input/Output */
  167. #define SCSPTR_SPB2DT BIT(0) /* Serial Port Break Data */
  168. /* HSSRR HSCIF */
  169. #define HSCIF_SRE BIT(15) /* Sampling Rate Register Enable */
  170. #define HSCIF_SRDE BIT(14) /* Sampling Point Register Enable */
  171. #define HSCIF_SRHP_SHIFT 8
  172. #define HSCIF_SRHP_MASK 0x0f00
  173. /* SCPCR (Serial Port Control Register), SCIFA/SCIFB only */
  174. #define SCPCR_RTSC BIT(4) /* Serial Port RTS# Pin / Output Pin */
  175. #define SCPCR_CTSC BIT(3) /* Serial Port CTS# Pin / Input Pin */
  176. #define SCPCR_SCKC BIT(2) /* Serial Port SCK Pin / Output Pin */
  177. #define SCPCR_RXDC BIT(1) /* Serial Port RXD Pin / Input Pin */
  178. #define SCPCR_TXDC BIT(0) /* Serial Port TXD Pin / Output Pin */
  179. /* SCPDR (Serial Port Data Register), SCIFA/SCIFB only */
  180. #define SCPDR_RTSD BIT(4) /* Serial Port RTS# Output Pin Data */
  181. #define SCPDR_CTSD BIT(3) /* Serial Port CTS# Input Pin Data */
  182. #define SCPDR_SCKD BIT(2) /* Serial Port SCK Output Pin Data */
  183. #define SCPDR_RXDD BIT(1) /* Serial Port RXD Input Pin Data */
  184. #define SCPDR_TXDD BIT(0) /* Serial Port TXD Output Pin Data */
  185. /*
  186. * BRG Clock Select Register (Some SCIF and HSCIF)
  187. * The Baud Rate Generator for external clock can provide a clock source for
  188. * the sampling clock. It outputs either its frequency divided clock, or the
  189. * (undivided) (H)SCK external clock.
  190. */
  191. #define SCCKS_CKS BIT(15) /* Select (H)SCK (1) or divided SC_CLK (0) */
  192. #define SCCKS_XIN BIT(14) /* SC_CLK uses bus clock (1) or SCIF_CLK (0) */
  193. #define SCxSR_TEND(port) (((port)->type == PORT_SCI) ? SCI_TEND : SCIF_TEND)
  194. #define SCxSR_RDxF(port) (((port)->type == PORT_SCI) ? SCI_RDRF : SCIF_DR | SCIF_RDF)
  195. #define SCxSR_TDxE(port) (((port)->type == PORT_SCI) ? SCI_TDRE : SCIF_TDFE)
  196. #define SCxSR_FER(port) (((port)->type == PORT_SCI) ? SCI_FER : SCIF_FER)
  197. #define SCxSR_PER(port) (((port)->type == PORT_SCI) ? SCI_PER : SCIF_PER)
  198. #define SCxSR_BRK(port) (((port)->type == PORT_SCI) ? 0x00 : SCIF_BRK)
  199. #define SCxSR_ERRORS(port) (to_sci_port(port)->params->error_mask)
  200. #define SCxSR_RDxF_CLEAR(port) \
  201. (((port)->type == PORT_SCI) ? SCI_RDxF_CLEAR : SCIF_RDxF_CLEAR)
  202. #define SCxSR_ERROR_CLEAR(port) \
  203. (to_sci_port(port)->params->error_clear)
  204. #define SCxSR_TDxE_CLEAR(port) \
  205. (((port)->type == PORT_SCI) ? SCI_TDxE_CLEAR : SCIF_TDxE_CLEAR)
  206. #define SCxSR_BREAK_CLEAR(port) \
  207. (((port)->type == PORT_SCI) ? SCI_BREAK_CLEAR : SCIF_BREAK_CLEAR)
  208. #define SCIx_IRQ_IS_MUXED(port) \
  209. ((port)->irqs[SCIx_ERI_IRQ] == \
  210. (port)->irqs[SCIx_RXI_IRQ]) || \
  211. ((port)->irqs[SCIx_ERI_IRQ] && \
  212. ((port)->irqs[SCIx_RXI_IRQ] < 0))
  213. #define SCI_SR_SCIFAB SCI_SR(5) | SCI_SR(7) | SCI_SR(11) | \
  214. SCI_SR(13) | SCI_SR(16) | SCI_SR(17) | \
  215. SCI_SR(19) | SCI_SR(27)
  216. /* Iterate over all supported sampling rates, from high to low */
  217. #define for_each_sr(_sr, _port) \
  218. for ((_sr) = max_sr(_port); (_sr) >= min_sr(_port); (_sr)--) \
  219. if ((_port)->sampling_rate_mask & SCI_SR((_sr)))
  220. #define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
  221. #define SCI_PUBLIC_PORT_ID(port) (((port) & BIT(7)) ? PORT_GENERIC : (port))
  222. static struct sci_port sci_ports[SCI_NPORTS];
  223. static unsigned long sci_ports_in_use;
  224. static struct uart_driver sci_uart_driver;
  225. static bool sci_uart_earlycon;
  226. static bool sci_uart_earlycon_dev_probing;
  227. static const struct sci_port_params_bits sci_sci_port_params_bits = {
  228. .rxtx_enable = SCSCR_RE | SCSCR_TE,
  229. .te_clear = SCSCR_TE | SCSCR_TEIE,
  230. .poll_sent_bits = SCI_TDRE | SCI_TEND
  231. };
  232. static const struct sci_port_params_bits sci_scif_port_params_bits = {
  233. .rxtx_enable = SCSCR_RE | SCSCR_TE,
  234. .te_clear = SCSCR_TE | SCSCR_TEIE,
  235. .poll_sent_bits = SCIF_TDFE | SCIF_TEND
  236. };
  237. static const struct sci_common_regs sci_common_regs = {
  238. .status = SCxSR,
  239. .control = SCSCR,
  240. };
  241. struct sci_suspend_regs {
  242. u16 scdl;
  243. u16 sccks;
  244. u16 scsmr;
  245. u16 scscr;
  246. u16 scfcr;
  247. u16 scsptr;
  248. u16 hssrr;
  249. u16 scpcr;
  250. u16 scpdr;
  251. u8 scbrr;
  252. u8 semr;
  253. };
  254. static size_t sci_suspend_regs_size(void)
  255. {
  256. return sizeof(struct sci_suspend_regs);
  257. }
  258. static const struct sci_port_params sci_port_params[SCIx_NR_REGTYPES] = {
  259. /*
  260. * Common SCI definitions, dependent on the port's regshift
  261. * value.
  262. */
  263. [SCIx_SCI_REGTYPE] = {
  264. .regs = {
  265. [SCSMR] = { 0x00, 8 },
  266. [SCBRR] = { 0x01, 8 },
  267. [SCSCR] = { 0x02, 8 },
  268. [SCxTDR] = { 0x03, 8 },
  269. [SCxSR] = { 0x04, 8 },
  270. [SCxRDR] = { 0x05, 8 },
  271. },
  272. .fifosize = 1,
  273. .overrun_reg = SCxSR,
  274. .overrun_mask = SCI_ORER,
  275. .sampling_rate_mask = SCI_SR(32),
  276. .error_mask = SCI_DEFAULT_ERROR_MASK | SCI_ORER,
  277. .error_clear = SCI_ERROR_CLEAR & ~SCI_ORER,
  278. .param_bits = &sci_sci_port_params_bits,
  279. .common_regs = &sci_common_regs,
  280. },
  281. /*
  282. * Common definitions for legacy IrDA ports.
  283. */
  284. [SCIx_IRDA_REGTYPE] = {
  285. .regs = {
  286. [SCSMR] = { 0x00, 8 },
  287. [SCBRR] = { 0x02, 8 },
  288. [SCSCR] = { 0x04, 8 },
  289. [SCxTDR] = { 0x06, 8 },
  290. [SCxSR] = { 0x08, 16 },
  291. [SCxRDR] = { 0x0a, 8 },
  292. [SCFCR] = { 0x0c, 8 },
  293. [SCFDR] = { 0x0e, 16 },
  294. },
  295. .fifosize = 1,
  296. .overrun_reg = SCxSR,
  297. .overrun_mask = SCI_ORER,
  298. .sampling_rate_mask = SCI_SR(32),
  299. .error_mask = SCI_DEFAULT_ERROR_MASK | SCI_ORER,
  300. .error_clear = SCI_ERROR_CLEAR & ~SCI_ORER,
  301. .param_bits = &sci_scif_port_params_bits,
  302. .common_regs = &sci_common_regs,
  303. },
  304. /*
  305. * Common SCIFA definitions.
  306. */
  307. [SCIx_SCIFA_REGTYPE] = {
  308. .regs = {
  309. [SCSMR] = { 0x00, 16 },
  310. [SCBRR] = { 0x04, 8 },
  311. [SCSCR] = { 0x08, 16 },
  312. [SCxTDR] = { 0x20, 8 },
  313. [SCxSR] = { 0x14, 16 },
  314. [SCxRDR] = { 0x24, 8 },
  315. [SCFCR] = { 0x18, 16 },
  316. [SCFDR] = { 0x1c, 16 },
  317. [SCPCR] = { 0x30, 16 },
  318. [SCPDR] = { 0x34, 16 },
  319. },
  320. .fifosize = 64,
  321. .overrun_reg = SCxSR,
  322. .overrun_mask = SCIFA_ORER,
  323. .sampling_rate_mask = SCI_SR_SCIFAB,
  324. .error_mask = SCIF_DEFAULT_ERROR_MASK | SCIFA_ORER,
  325. .error_clear = SCIF_ERROR_CLEAR & ~SCIFA_ORER,
  326. .param_bits = &sci_scif_port_params_bits,
  327. .common_regs = &sci_common_regs,
  328. },
  329. /*
  330. * Common SCIFB definitions.
  331. */
  332. [SCIx_SCIFB_REGTYPE] = {
  333. .regs = {
  334. [SCSMR] = { 0x00, 16 },
  335. [SCBRR] = { 0x04, 8 },
  336. [SCSCR] = { 0x08, 16 },
  337. [SCxTDR] = { 0x40, 8 },
  338. [SCxSR] = { 0x14, 16 },
  339. [SCxRDR] = { 0x60, 8 },
  340. [SCFCR] = { 0x18, 16 },
  341. [SCTFDR] = { 0x38, 16 },
  342. [SCRFDR] = { 0x3c, 16 },
  343. [SCPCR] = { 0x30, 16 },
  344. [SCPDR] = { 0x34, 16 },
  345. },
  346. .fifosize = 256,
  347. .overrun_reg = SCxSR,
  348. .overrun_mask = SCIFA_ORER,
  349. .sampling_rate_mask = SCI_SR_SCIFAB,
  350. .error_mask = SCIF_DEFAULT_ERROR_MASK | SCIFA_ORER,
  351. .error_clear = SCIF_ERROR_CLEAR & ~SCIFA_ORER,
  352. .param_bits = &sci_scif_port_params_bits,
  353. .common_regs = &sci_common_regs,
  354. },
  355. /*
  356. * Common SH-2(A) SCIF definitions for ports with FIFO data
  357. * count registers.
  358. */
  359. [SCIx_SH2_SCIF_FIFODATA_REGTYPE] = {
  360. .regs = {
  361. [SCSMR] = { 0x00, 16 },
  362. [SCBRR] = { 0x04, 8 },
  363. [SCSCR] = { 0x08, 16 },
  364. [SCxTDR] = { 0x0c, 8 },
  365. [SCxSR] = { 0x10, 16 },
  366. [SCxRDR] = { 0x14, 8 },
  367. [SCFCR] = { 0x18, 16 },
  368. [SCFDR] = { 0x1c, 16 },
  369. [SCSPTR] = { 0x20, 16 },
  370. [SCLSR] = { 0x24, 16 },
  371. },
  372. .fifosize = 16,
  373. .overrun_reg = SCLSR,
  374. .overrun_mask = SCLSR_ORER,
  375. .sampling_rate_mask = SCI_SR(32),
  376. .error_mask = SCIF_DEFAULT_ERROR_MASK,
  377. .error_clear = SCIF_ERROR_CLEAR,
  378. .param_bits = &sci_scif_port_params_bits,
  379. .common_regs = &sci_common_regs,
  380. },
  381. /*
  382. * The "SCIFA" that is in RZ/A2, RZ/G2L and RZ/T1.
  383. * It looks like a normal SCIF with FIFO data, but with a
  384. * compressed address space. Also, the break out of interrupts
  385. * are different: ERI/BRI, RXI, TXI, TEI, DRI.
  386. */
  387. [SCIx_RZ_SCIFA_REGTYPE] = {
  388. .regs = {
  389. [SCSMR] = { 0x00, 16 },
  390. [SCBRR] = { 0x02, 8 },
  391. [SCSCR] = { 0x04, 16 },
  392. [SCxTDR] = { 0x06, 8 },
  393. [SCxSR] = { 0x08, 16 },
  394. [SCxRDR] = { 0x0A, 8 },
  395. [SCFCR] = { 0x0C, 16 },
  396. [SCFDR] = { 0x0E, 16 },
  397. [SCSPTR] = { 0x10, 16 },
  398. [SCLSR] = { 0x12, 16 },
  399. [SEMR] = { 0x14, 8 },
  400. },
  401. .fifosize = 16,
  402. .overrun_reg = SCLSR,
  403. .overrun_mask = SCLSR_ORER,
  404. .sampling_rate_mask = SCI_SR(32),
  405. .error_mask = SCIF_DEFAULT_ERROR_MASK,
  406. .error_clear = SCIF_ERROR_CLEAR,
  407. .param_bits = &sci_scif_port_params_bits,
  408. .common_regs = &sci_common_regs,
  409. },
  410. /*
  411. * The "SCIF" that is in RZ/V2H(P) SoC is similar to one found on RZ/G2L SoC
  412. * with below differences,
  413. * - Break out of interrupts are different: ERI, BRI, RXI, TXI, TEI, DRI,
  414. * TEI-DRI, RXI-EDGE and TXI-EDGE.
  415. * - SCSMR register does not have CM bit (BIT(7)) ie it does not support synchronous mode.
  416. * - SCFCR register does not have SCFCR_MCE bit.
  417. * - SCSPTR register has only bits SCSPTR_SPB2DT and SCSPTR_SPB2IO.
  418. */
  419. [SCIx_RZV2H_SCIF_REGTYPE] = {
  420. .regs = {
  421. [SCSMR] = { 0x00, 16 },
  422. [SCBRR] = { 0x02, 8 },
  423. [SCSCR] = { 0x04, 16 },
  424. [SCxTDR] = { 0x06, 8 },
  425. [SCxSR] = { 0x08, 16 },
  426. [SCxRDR] = { 0x0a, 8 },
  427. [SCFCR] = { 0x0c, 16 },
  428. [SCFDR] = { 0x0e, 16 },
  429. [SCSPTR] = { 0x10, 16 },
  430. [SCLSR] = { 0x12, 16 },
  431. [SEMR] = { 0x14, 8 },
  432. },
  433. .fifosize = 16,
  434. .overrun_reg = SCLSR,
  435. .overrun_mask = SCLSR_ORER,
  436. .sampling_rate_mask = SCI_SR(32),
  437. .error_mask = SCIF_DEFAULT_ERROR_MASK,
  438. .error_clear = SCIF_ERROR_CLEAR,
  439. .param_bits = &sci_scif_port_params_bits,
  440. .common_regs = &sci_common_regs,
  441. },
  442. /*
  443. * Common SH-3 SCIF definitions.
  444. */
  445. [SCIx_SH3_SCIF_REGTYPE] = {
  446. .regs = {
  447. [SCSMR] = { 0x00, 8 },
  448. [SCBRR] = { 0x02, 8 },
  449. [SCSCR] = { 0x04, 8 },
  450. [SCxTDR] = { 0x06, 8 },
  451. [SCxSR] = { 0x08, 16 },
  452. [SCxRDR] = { 0x0a, 8 },
  453. [SCFCR] = { 0x0c, 8 },
  454. [SCFDR] = { 0x0e, 16 },
  455. },
  456. .fifosize = 16,
  457. .overrun_reg = SCLSR,
  458. .overrun_mask = SCLSR_ORER,
  459. .sampling_rate_mask = SCI_SR(32),
  460. .error_mask = SCIF_DEFAULT_ERROR_MASK,
  461. .error_clear = SCIF_ERROR_CLEAR,
  462. .param_bits = &sci_scif_port_params_bits,
  463. .common_regs = &sci_common_regs,
  464. },
  465. /*
  466. * Common SH-4(A) SCIF(B) definitions.
  467. */
  468. [SCIx_SH4_SCIF_REGTYPE] = {
  469. .regs = {
  470. [SCSMR] = { 0x00, 16 },
  471. [SCBRR] = { 0x04, 8 },
  472. [SCSCR] = { 0x08, 16 },
  473. [SCxTDR] = { 0x0c, 8 },
  474. [SCxSR] = { 0x10, 16 },
  475. [SCxRDR] = { 0x14, 8 },
  476. [SCFCR] = { 0x18, 16 },
  477. [SCFDR] = { 0x1c, 16 },
  478. [SCSPTR] = { 0x20, 16 },
  479. [SCLSR] = { 0x24, 16 },
  480. },
  481. .fifosize = 16,
  482. .overrun_reg = SCLSR,
  483. .overrun_mask = SCLSR_ORER,
  484. .sampling_rate_mask = SCI_SR(32),
  485. .error_mask = SCIF_DEFAULT_ERROR_MASK,
  486. .error_clear = SCIF_ERROR_CLEAR,
  487. .param_bits = &sci_scif_port_params_bits,
  488. .common_regs = &sci_common_regs,
  489. },
  490. /*
  491. * Common SCIF definitions for ports with a Baud Rate Generator for
  492. * External Clock (BRG).
  493. */
  494. [SCIx_SH4_SCIF_BRG_REGTYPE] = {
  495. .regs = {
  496. [SCSMR] = { 0x00, 16 },
  497. [SCBRR] = { 0x04, 8 },
  498. [SCSCR] = { 0x08, 16 },
  499. [SCxTDR] = { 0x0c, 8 },
  500. [SCxSR] = { 0x10, 16 },
  501. [SCxRDR] = { 0x14, 8 },
  502. [SCFCR] = { 0x18, 16 },
  503. [SCFDR] = { 0x1c, 16 },
  504. [SCSPTR] = { 0x20, 16 },
  505. [SCLSR] = { 0x24, 16 },
  506. [SCDL] = { 0x30, 16 },
  507. [SCCKS] = { 0x34, 16 },
  508. },
  509. .fifosize = 16,
  510. .overrun_reg = SCLSR,
  511. .overrun_mask = SCLSR_ORER,
  512. .sampling_rate_mask = SCI_SR(32),
  513. .error_mask = SCIF_DEFAULT_ERROR_MASK,
  514. .error_clear = SCIF_ERROR_CLEAR,
  515. .param_bits = &sci_scif_port_params_bits,
  516. .common_regs = &sci_common_regs,
  517. },
  518. /*
  519. * Common HSCIF definitions.
  520. */
  521. [SCIx_HSCIF_REGTYPE] = {
  522. .regs = {
  523. [SCSMR] = { 0x00, 16 },
  524. [SCBRR] = { 0x04, 8 },
  525. [SCSCR] = { 0x08, 16 },
  526. [SCxTDR] = { 0x0c, 8 },
  527. [SCxSR] = { 0x10, 16 },
  528. [SCxRDR] = { 0x14, 8 },
  529. [SCFCR] = { 0x18, 16 },
  530. [SCFDR] = { 0x1c, 16 },
  531. [SCSPTR] = { 0x20, 16 },
  532. [SCLSR] = { 0x24, 16 },
  533. [HSSRR] = { 0x40, 16 },
  534. [SCDL] = { 0x30, 16 },
  535. [SCCKS] = { 0x34, 16 },
  536. [HSRTRGR] = { 0x54, 16 },
  537. [HSTTRGR] = { 0x58, 16 },
  538. },
  539. .fifosize = 128,
  540. .overrun_reg = SCLSR,
  541. .overrun_mask = SCLSR_ORER,
  542. .sampling_rate_mask = SCI_SR_RANGE(8, 32),
  543. .error_mask = SCIF_DEFAULT_ERROR_MASK,
  544. .error_clear = SCIF_ERROR_CLEAR,
  545. .param_bits = &sci_scif_port_params_bits,
  546. .common_regs = &sci_common_regs,
  547. },
  548. /*
  549. * Common SH-4(A) SCIF(B) definitions for ports without an SCSPTR
  550. * register.
  551. */
  552. [SCIx_SH4_SCIF_NO_SCSPTR_REGTYPE] = {
  553. .regs = {
  554. [SCSMR] = { 0x00, 16 },
  555. [SCBRR] = { 0x04, 8 },
  556. [SCSCR] = { 0x08, 16 },
  557. [SCxTDR] = { 0x0c, 8 },
  558. [SCxSR] = { 0x10, 16 },
  559. [SCxRDR] = { 0x14, 8 },
  560. [SCFCR] = { 0x18, 16 },
  561. [SCFDR] = { 0x1c, 16 },
  562. [SCLSR] = { 0x24, 16 },
  563. },
  564. .fifosize = 16,
  565. .overrun_reg = SCLSR,
  566. .overrun_mask = SCLSR_ORER,
  567. .sampling_rate_mask = SCI_SR(32),
  568. .error_mask = SCIF_DEFAULT_ERROR_MASK,
  569. .error_clear = SCIF_ERROR_CLEAR,
  570. .param_bits = &sci_scif_port_params_bits,
  571. .common_regs = &sci_common_regs,
  572. },
  573. /*
  574. * Common SH-4(A) SCIF(B) definitions for ports with FIFO data
  575. * count registers.
  576. */
  577. [SCIx_SH4_SCIF_FIFODATA_REGTYPE] = {
  578. .regs = {
  579. [SCSMR] = { 0x00, 16 },
  580. [SCBRR] = { 0x04, 8 },
  581. [SCSCR] = { 0x08, 16 },
  582. [SCxTDR] = { 0x0c, 8 },
  583. [SCxSR] = { 0x10, 16 },
  584. [SCxRDR] = { 0x14, 8 },
  585. [SCFCR] = { 0x18, 16 },
  586. [SCFDR] = { 0x1c, 16 },
  587. [SCTFDR] = { 0x1c, 16 }, /* aliased to SCFDR */
  588. [SCRFDR] = { 0x20, 16 },
  589. [SCSPTR] = { 0x24, 16 },
  590. [SCLSR] = { 0x28, 16 },
  591. },
  592. .fifosize = 16,
  593. .overrun_reg = SCLSR,
  594. .overrun_mask = SCLSR_ORER,
  595. .sampling_rate_mask = SCI_SR(32),
  596. .error_mask = SCIF_DEFAULT_ERROR_MASK,
  597. .error_clear = SCIF_ERROR_CLEAR,
  598. .param_bits = &sci_scif_port_params_bits,
  599. .common_regs = &sci_common_regs,
  600. },
  601. /*
  602. * SH7705-style SCIF(B) ports, lacking both SCSPTR and SCLSR
  603. * registers.
  604. */
  605. [SCIx_SH7705_SCIF_REGTYPE] = {
  606. .regs = {
  607. [SCSMR] = { 0x00, 16 },
  608. [SCBRR] = { 0x04, 8 },
  609. [SCSCR] = { 0x08, 16 },
  610. [SCxTDR] = { 0x20, 8 },
  611. [SCxSR] = { 0x14, 16 },
  612. [SCxRDR] = { 0x24, 8 },
  613. [SCFCR] = { 0x18, 16 },
  614. [SCFDR] = { 0x1c, 16 },
  615. },
  616. .fifosize = 64,
  617. .overrun_reg = SCxSR,
  618. .overrun_mask = SCIFA_ORER,
  619. .sampling_rate_mask = SCI_SR(16),
  620. .error_mask = SCIF_DEFAULT_ERROR_MASK | SCIFA_ORER,
  621. .error_clear = SCIF_ERROR_CLEAR & ~SCIFA_ORER,
  622. .param_bits = &sci_scif_port_params_bits,
  623. .common_regs = &sci_common_regs,
  624. },
  625. };
  626. #define sci_getreg(up, offset) (&to_sci_port(up)->params->regs[offset])
  627. /*
  628. * The "offset" here is rather misleading, in that it refers to an enum
  629. * value relative to the port mapping rather than the fixed offset
  630. * itself, which needs to be manually retrieved from the platform's
  631. * register map for the given port.
  632. */
  633. static unsigned int sci_serial_in(struct uart_port *p, int offset)
  634. {
  635. const struct plat_sci_reg *reg = sci_getreg(p, offset);
  636. if (reg->size == 8)
  637. return ioread8(p->membase + (reg->offset << p->regshift));
  638. else if (reg->size == 16)
  639. return ioread16(p->membase + (reg->offset << p->regshift));
  640. else
  641. WARN(1, "Invalid register access\n");
  642. return 0;
  643. }
  644. static void sci_serial_out(struct uart_port *p, int offset, int value)
  645. {
  646. const struct plat_sci_reg *reg = sci_getreg(p, offset);
  647. if (reg->size == 8)
  648. iowrite8(value, p->membase + (reg->offset << p->regshift));
  649. else if (reg->size == 16)
  650. iowrite16(value, p->membase + (reg->offset << p->regshift));
  651. else
  652. WARN(1, "Invalid register access\n");
  653. }
  654. void sci_port_enable(struct sci_port *sci_port)
  655. {
  656. unsigned int i;
  657. if (!sci_port->port.dev)
  658. return;
  659. pm_runtime_get_sync(sci_port->port.dev);
  660. for (i = 0; i < SCI_NUM_CLKS; i++) {
  661. clk_prepare_enable(sci_port->clks[i]);
  662. sci_port->clk_rates[i] = clk_get_rate(sci_port->clks[i]);
  663. }
  664. sci_port->port.uartclk = sci_port->clk_rates[SCI_FCK];
  665. }
  666. EXPORT_SYMBOL_NS_GPL(sci_port_enable, "SH_SCI");
  667. void sci_port_disable(struct sci_port *sci_port)
  668. {
  669. unsigned int i;
  670. if (!sci_port->port.dev)
  671. return;
  672. for (i = SCI_NUM_CLKS; i-- > 0; )
  673. clk_disable_unprepare(sci_port->clks[i]);
  674. pm_runtime_put_sync(sci_port->port.dev);
  675. }
  676. EXPORT_SYMBOL_NS_GPL(sci_port_disable, "SH_SCI");
  677. static inline unsigned long port_rx_irq_mask(struct uart_port *port)
  678. {
  679. /*
  680. * Not all ports (such as SCIFA) will support REIE. Rather than
  681. * special-casing the port type, we check the port initialization
  682. * IRQ enable mask to see whether the IRQ is desired at all. If
  683. * it's unset, it's logically inferred that there's no point in
  684. * testing for it.
  685. */
  686. return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
  687. }
  688. static void sci_start_tx(struct uart_port *port)
  689. {
  690. struct sci_port *s = to_sci_port(port);
  691. unsigned short ctrl;
  692. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  693. if (s->type == PORT_SCIFA || s->type == PORT_SCIFB) {
  694. u16 new, scr = sci_serial_in(port, SCSCR);
  695. if (s->chan_tx)
  696. new = scr | SCSCR_TDRQE;
  697. else
  698. new = scr & ~SCSCR_TDRQE;
  699. if (new != scr)
  700. sci_serial_out(port, SCSCR, new);
  701. }
  702. if (s->chan_tx && !kfifo_is_empty(&port->state->port.xmit_fifo) &&
  703. dma_submit_error(s->cookie_tx)) {
  704. if (s->regtype == SCIx_RZ_SCIFA_REGTYPE)
  705. /* Switch irq from SCIF to DMA */
  706. disable_irq_nosync(s->irqs[SCIx_TXI_IRQ]);
  707. s->cookie_tx = 0;
  708. schedule_work(&s->work_tx);
  709. }
  710. #endif
  711. if (!s->chan_tx || s->regtype == SCIx_RZ_SCIFA_REGTYPE ||
  712. s->type == PORT_SCIFA || s->type == PORT_SCIFB) {
  713. /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
  714. ctrl = sci_serial_in(port, SCSCR);
  715. /*
  716. * For SCI, TE (transmit enable) must be set after setting TIE
  717. * (transmit interrupt enable) or in the same instruction to start
  718. * the transmit process.
  719. */
  720. if (s->type == PORT_SCI)
  721. ctrl |= SCSCR_TE;
  722. sci_serial_out(port, SCSCR, ctrl | SCSCR_TIE);
  723. }
  724. }
  725. static void sci_stop_tx(struct uart_port *port)
  726. {
  727. struct sci_port *s = to_sci_port(port);
  728. unsigned short ctrl;
  729. /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
  730. ctrl = sci_serial_in(port, SCSCR);
  731. if (s->type == PORT_SCIFA || s->type == PORT_SCIFB)
  732. ctrl &= ~SCSCR_TDRQE;
  733. ctrl &= ~SCSCR_TIE;
  734. sci_serial_out(port, SCSCR, ctrl);
  735. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  736. if (s->chan_tx &&
  737. !dma_submit_error(s->cookie_tx)) {
  738. dmaengine_terminate_async(s->chan_tx);
  739. s->cookie_tx = -EINVAL;
  740. }
  741. #endif
  742. }
  743. static void sci_start_rx(struct uart_port *port)
  744. {
  745. struct sci_port *s = to_sci_port(port);
  746. unsigned short ctrl;
  747. ctrl = sci_serial_in(port, SCSCR) | port_rx_irq_mask(port);
  748. if (s->type == PORT_SCIFA || s->type == PORT_SCIFB)
  749. ctrl &= ~SCSCR_RDRQE;
  750. sci_serial_out(port, SCSCR, ctrl);
  751. }
  752. static void sci_stop_rx(struct uart_port *port)
  753. {
  754. struct sci_port *s = to_sci_port(port);
  755. unsigned short ctrl;
  756. ctrl = sci_serial_in(port, SCSCR);
  757. if (s->type == PORT_SCIFA || s->type == PORT_SCIFB)
  758. ctrl &= ~SCSCR_RDRQE;
  759. ctrl &= ~port_rx_irq_mask(port);
  760. sci_serial_out(port, SCSCR, ctrl);
  761. }
  762. static void sci_clear_SCxSR(struct uart_port *port, unsigned int mask)
  763. {
  764. struct sci_port *s = to_sci_port(port);
  765. if (s->type == PORT_SCI) {
  766. /* Just store the mask */
  767. sci_serial_out(port, SCxSR, mask);
  768. } else if (s->params->overrun_mask == SCIFA_ORER) {
  769. /* SCIFA/SCIFB and SCIF on SH7705/SH7720/SH7721 */
  770. /* Only clear the status bits we want to clear */
  771. sci_serial_out(port, SCxSR, sci_serial_in(port, SCxSR) & mask);
  772. } else {
  773. /* Store the mask, clear parity/framing errors */
  774. sci_serial_out(port, SCxSR, mask & ~(SCIF_FERC | SCIF_PERC));
  775. }
  776. }
  777. #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || \
  778. defined(CONFIG_SERIAL_SH_SCI_EARLYCON)
  779. #ifdef CONFIG_CONSOLE_POLL
  780. static int sci_poll_get_char(struct uart_port *port)
  781. {
  782. unsigned short status;
  783. struct sci_port *s = to_sci_port(port);
  784. int c;
  785. do {
  786. status = sci_serial_in(port, SCxSR);
  787. if (status & SCxSR_ERRORS(port)) {
  788. s->ops->clear_SCxSR(port, SCxSR_ERROR_CLEAR(port));
  789. continue;
  790. }
  791. break;
  792. } while (1);
  793. if (!(status & SCxSR_RDxF(port)))
  794. return NO_POLL_CHAR;
  795. c = sci_serial_in(port, SCxRDR);
  796. /* Dummy read */
  797. sci_serial_in(port, SCxSR);
  798. s->ops->clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
  799. return c;
  800. }
  801. #endif
  802. static void sci_poll_put_char(struct uart_port *port, unsigned char c)
  803. {
  804. struct sci_port *s = to_sci_port(port);
  805. const struct sci_common_regs *regs = s->params->common_regs;
  806. unsigned int status;
  807. do {
  808. status = s->ops->read_reg(port, regs->status);
  809. } while (!(status & SCxSR_TDxE(port)));
  810. sci_serial_out(port, SCxTDR, c);
  811. s->ops->clear_SCxSR(port, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
  812. }
  813. #endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE ||
  814. CONFIG_SERIAL_SH_SCI_EARLYCON */
  815. static void sci_init_pins(struct uart_port *port, unsigned int cflag)
  816. {
  817. struct sci_port *s = to_sci_port(port);
  818. /*
  819. * Use port-specific handler if provided.
  820. */
  821. if (s->cfg->ops && s->cfg->ops->init_pins) {
  822. s->cfg->ops->init_pins(port, cflag);
  823. return;
  824. }
  825. if (s->type == PORT_SCIFA || s->type == PORT_SCIFB) {
  826. u16 data = sci_serial_in(port, SCPDR);
  827. u16 ctrl = sci_serial_in(port, SCPCR);
  828. /* Enable RXD and TXD pin functions */
  829. ctrl &= ~(SCPCR_RXDC | SCPCR_TXDC);
  830. if (s->has_rtscts) {
  831. /* RTS# is output, active low, unless autorts */
  832. if (!(port->mctrl & TIOCM_RTS)) {
  833. ctrl |= SCPCR_RTSC;
  834. data |= SCPDR_RTSD;
  835. } else if (!s->autorts) {
  836. ctrl |= SCPCR_RTSC;
  837. data &= ~SCPDR_RTSD;
  838. } else {
  839. /* Enable RTS# pin function */
  840. ctrl &= ~SCPCR_RTSC;
  841. }
  842. /* Enable CTS# pin function */
  843. ctrl &= ~SCPCR_CTSC;
  844. }
  845. sci_serial_out(port, SCPDR, data);
  846. sci_serial_out(port, SCPCR, ctrl);
  847. } else if (sci_getreg(port, SCSPTR)->size && s->regtype != SCIx_RZV2H_SCIF_REGTYPE) {
  848. u16 status = sci_serial_in(port, SCSPTR);
  849. /* RTS# is always output; and active low, unless autorts */
  850. status |= SCSPTR_RTSIO;
  851. if (!(port->mctrl & TIOCM_RTS))
  852. status |= SCSPTR_RTSDT;
  853. else if (!s->autorts)
  854. status &= ~SCSPTR_RTSDT;
  855. /* CTS# and SCK are inputs */
  856. status &= ~(SCSPTR_CTSIO | SCSPTR_SCKIO);
  857. sci_serial_out(port, SCSPTR, status);
  858. }
  859. }
  860. static int sci_txfill(struct uart_port *port)
  861. {
  862. struct sci_port *s = to_sci_port(port);
  863. unsigned int fifo_mask = (s->params->fifosize << 1) - 1;
  864. const struct plat_sci_reg *reg;
  865. reg = sci_getreg(port, SCTFDR);
  866. if (reg->size)
  867. return sci_serial_in(port, SCTFDR) & fifo_mask;
  868. reg = sci_getreg(port, SCFDR);
  869. if (reg->size)
  870. return sci_serial_in(port, SCFDR) >> 8;
  871. return !(sci_serial_in(port, SCxSR) & SCI_TDRE);
  872. }
  873. static int sci_txroom(struct uart_port *port)
  874. {
  875. return port->fifosize - sci_txfill(port);
  876. }
  877. static int sci_rxfill(struct uart_port *port)
  878. {
  879. struct sci_port *s = to_sci_port(port);
  880. unsigned int fifo_mask = (s->params->fifosize << 1) - 1;
  881. const struct plat_sci_reg *reg;
  882. reg = sci_getreg(port, SCRFDR);
  883. if (reg->size)
  884. return sci_serial_in(port, SCRFDR) & fifo_mask;
  885. reg = sci_getreg(port, SCFDR);
  886. if (reg->size)
  887. return sci_serial_in(port, SCFDR) & fifo_mask;
  888. return (sci_serial_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
  889. }
  890. /* ********************************************************************** *
  891. * the interrupt related routines *
  892. * ********************************************************************** */
  893. static void sci_transmit_chars(struct uart_port *port)
  894. {
  895. struct tty_port *tport = &port->state->port;
  896. unsigned int stopped = uart_tx_stopped(port);
  897. struct sci_port *s = to_sci_port(port);
  898. unsigned short status;
  899. unsigned short ctrl;
  900. int count;
  901. status = sci_serial_in(port, SCxSR);
  902. if (!(status & SCxSR_TDxE(port))) {
  903. ctrl = sci_serial_in(port, SCSCR);
  904. if (kfifo_is_empty(&tport->xmit_fifo))
  905. ctrl &= ~SCSCR_TIE;
  906. else
  907. ctrl |= SCSCR_TIE;
  908. sci_serial_out(port, SCSCR, ctrl);
  909. return;
  910. }
  911. count = sci_txroom(port);
  912. do {
  913. unsigned char c;
  914. if (port->x_char) {
  915. c = port->x_char;
  916. port->x_char = 0;
  917. } else if (stopped || !kfifo_get(&tport->xmit_fifo, &c)) {
  918. if (s->type == PORT_SCI &&
  919. kfifo_is_empty(&tport->xmit_fifo)) {
  920. ctrl = sci_serial_in(port, SCSCR);
  921. ctrl &= ~SCSCR_TE;
  922. sci_serial_out(port, SCSCR, ctrl);
  923. return;
  924. }
  925. break;
  926. }
  927. sci_serial_out(port, SCxTDR, c);
  928. s->tx_occurred = true;
  929. port->icount.tx++;
  930. } while (--count > 0);
  931. s->ops->clear_SCxSR(port, SCxSR_TDxE_CLEAR(port));
  932. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  933. uart_write_wakeup(port);
  934. if (kfifo_is_empty(&tport->xmit_fifo)) {
  935. if (s->type == PORT_SCI) {
  936. ctrl = sci_serial_in(port, SCSCR);
  937. ctrl &= ~SCSCR_TIE;
  938. ctrl |= SCSCR_TEIE;
  939. sci_serial_out(port, SCSCR, ctrl);
  940. }
  941. sci_stop_tx(port);
  942. }
  943. }
  944. static void sci_receive_chars(struct uart_port *port)
  945. {
  946. struct tty_port *tport = &port->state->port;
  947. struct sci_port *s = to_sci_port(port);
  948. int i, count, copied = 0;
  949. unsigned short status;
  950. unsigned char flag;
  951. status = sci_serial_in(port, SCxSR);
  952. if (!(status & SCxSR_RDxF(port)))
  953. return;
  954. while (1) {
  955. /* Don't copy more bytes than there is room for in the buffer */
  956. count = tty_buffer_request_room(tport, sci_rxfill(port));
  957. /* If for any reason we can't copy more data, we're done! */
  958. if (count == 0)
  959. break;
  960. if (s->type == PORT_SCI) {
  961. char c = sci_serial_in(port, SCxRDR);
  962. if (uart_handle_sysrq_char(port, c))
  963. count = 0;
  964. else
  965. tty_insert_flip_char(tport, c, TTY_NORMAL);
  966. } else {
  967. for (i = 0; i < count; i++) {
  968. char c;
  969. if (s->type == PORT_SCIF ||
  970. s->type == PORT_HSCIF) {
  971. status = sci_serial_in(port, SCxSR);
  972. c = sci_serial_in(port, SCxRDR);
  973. } else {
  974. c = sci_serial_in(port, SCxRDR);
  975. status = sci_serial_in(port, SCxSR);
  976. }
  977. if (uart_handle_sysrq_char(port, c)) {
  978. count--; i--;
  979. continue;
  980. }
  981. /* Store data and status */
  982. if (status & SCxSR_FER(port)) {
  983. flag = TTY_FRAME;
  984. port->icount.frame++;
  985. } else if (status & SCxSR_PER(port)) {
  986. flag = TTY_PARITY;
  987. port->icount.parity++;
  988. } else
  989. flag = TTY_NORMAL;
  990. tty_insert_flip_char(tport, c, flag);
  991. }
  992. }
  993. sci_serial_in(port, SCxSR); /* dummy read */
  994. s->ops->clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
  995. copied += count;
  996. port->icount.rx += count;
  997. }
  998. if (copied) {
  999. /* Tell the rest of the system the news. New characters! */
  1000. tty_flip_buffer_push(tport);
  1001. } else {
  1002. /* TTY buffers full; read from RX reg to prevent lockup */
  1003. sci_serial_in(port, SCxRDR);
  1004. sci_serial_in(port, SCxSR); /* dummy read */
  1005. s->ops->clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
  1006. }
  1007. }
  1008. static int sci_handle_errors(struct uart_port *port)
  1009. {
  1010. int copied = 0;
  1011. struct sci_port *s = to_sci_port(port);
  1012. const struct sci_common_regs *regs = s->params->common_regs;
  1013. unsigned int status = s->ops->read_reg(port, regs->status);
  1014. struct tty_port *tport = &port->state->port;
  1015. /* Handle overruns */
  1016. if (status & s->params->overrun_mask) {
  1017. port->icount.overrun++;
  1018. /* overrun error */
  1019. if (tty_insert_flip_char(tport, 0, TTY_OVERRUN))
  1020. copied++;
  1021. }
  1022. if (status & SCxSR_FER(port)) {
  1023. /* frame error */
  1024. port->icount.frame++;
  1025. if (tty_insert_flip_char(tport, 0, TTY_FRAME))
  1026. copied++;
  1027. }
  1028. if (status & SCxSR_PER(port)) {
  1029. /* parity error */
  1030. port->icount.parity++;
  1031. if (tty_insert_flip_char(tport, 0, TTY_PARITY))
  1032. copied++;
  1033. }
  1034. if (copied)
  1035. tty_flip_buffer_push(tport);
  1036. return copied;
  1037. }
  1038. static bool sci_is_rsci_type(u8 type)
  1039. {
  1040. return (type == RSCI_PORT_SCIF16 || type == RSCI_PORT_SCIF32);
  1041. }
  1042. static int sci_handle_fifo_overrun(struct uart_port *port)
  1043. {
  1044. struct tty_port *tport = &port->state->port;
  1045. struct sci_port *s = to_sci_port(port);
  1046. const struct plat_sci_reg *reg;
  1047. int copied = 0;
  1048. u32 status;
  1049. if (!sci_is_rsci_type(s->type)) {
  1050. reg = sci_getreg(port, s->params->overrun_reg);
  1051. if (!reg->size)
  1052. return 0;
  1053. }
  1054. status = s->ops->read_reg(port, s->params->overrun_reg);
  1055. if (status & s->params->overrun_mask) {
  1056. if (sci_is_rsci_type(s->type)) {
  1057. /*
  1058. * All of the CFCLR_*C clearing bits match the corresponding
  1059. * CSR_*status bits. So, reuse the overrun mask for clearing.
  1060. */
  1061. s->ops->clear_SCxSR(port, s->params->overrun_mask);
  1062. } else {
  1063. status &= ~s->params->overrun_mask;
  1064. s->ops->write_reg(port, s->params->overrun_reg, status);
  1065. }
  1066. port->icount.overrun++;
  1067. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  1068. tty_flip_buffer_push(tport);
  1069. copied++;
  1070. }
  1071. return copied;
  1072. }
  1073. static int sci_handle_breaks(struct uart_port *port)
  1074. {
  1075. int copied = 0;
  1076. unsigned short status = sci_serial_in(port, SCxSR);
  1077. struct tty_port *tport = &port->state->port;
  1078. if (uart_handle_break(port))
  1079. return 0;
  1080. if (status & SCxSR_BRK(port)) {
  1081. port->icount.brk++;
  1082. /* Notify of BREAK */
  1083. if (tty_insert_flip_char(tport, 0, TTY_BREAK))
  1084. copied++;
  1085. }
  1086. if (copied)
  1087. tty_flip_buffer_push(tport);
  1088. copied += sci_handle_fifo_overrun(port);
  1089. return copied;
  1090. }
  1091. static int scif_set_rtrg(struct uart_port *port, int rx_trig)
  1092. {
  1093. struct sci_port *s = to_sci_port(port);
  1094. unsigned int bits;
  1095. if (rx_trig >= port->fifosize)
  1096. rx_trig = port->fifosize - 1;
  1097. if (rx_trig < 1)
  1098. rx_trig = 1;
  1099. /* HSCIF can be set to an arbitrary level. */
  1100. if (sci_getreg(port, HSRTRGR)->size) {
  1101. sci_serial_out(port, HSRTRGR, rx_trig);
  1102. return rx_trig;
  1103. }
  1104. switch (s->type) {
  1105. case PORT_SCIF:
  1106. if (rx_trig < 4) {
  1107. bits = 0;
  1108. rx_trig = 1;
  1109. } else if (rx_trig < 8) {
  1110. bits = SCFCR_RTRG0;
  1111. rx_trig = 4;
  1112. } else if (rx_trig < 14) {
  1113. bits = SCFCR_RTRG1;
  1114. rx_trig = 8;
  1115. } else {
  1116. bits = SCFCR_RTRG0 | SCFCR_RTRG1;
  1117. rx_trig = 14;
  1118. }
  1119. break;
  1120. case PORT_SCIFA:
  1121. case PORT_SCIFB:
  1122. if (rx_trig < 16) {
  1123. bits = 0;
  1124. rx_trig = 1;
  1125. } else if (rx_trig < 32) {
  1126. bits = SCFCR_RTRG0;
  1127. rx_trig = 16;
  1128. } else if (rx_trig < 48) {
  1129. bits = SCFCR_RTRG1;
  1130. rx_trig = 32;
  1131. } else {
  1132. bits = SCFCR_RTRG0 | SCFCR_RTRG1;
  1133. rx_trig = 48;
  1134. }
  1135. break;
  1136. default:
  1137. WARN(1, "unknown FIFO configuration");
  1138. return 1;
  1139. }
  1140. sci_serial_out(port, SCFCR,
  1141. (sci_serial_in(port, SCFCR) &
  1142. ~(SCFCR_RTRG1 | SCFCR_RTRG0)) | bits);
  1143. return rx_trig;
  1144. }
  1145. static int scif_rtrg_enabled(struct uart_port *port)
  1146. {
  1147. if (sci_getreg(port, HSRTRGR)->size)
  1148. return sci_serial_in(port, HSRTRGR) != 0;
  1149. else
  1150. return (sci_serial_in(port, SCFCR) &
  1151. (SCFCR_RTRG0 | SCFCR_RTRG1)) != 0;
  1152. }
  1153. static void rx_fifo_timer_fn(struct timer_list *t)
  1154. {
  1155. struct sci_port *s = timer_container_of(s, t, rx_fifo_timer);
  1156. struct uart_port *port = &s->port;
  1157. dev_dbg(port->dev, "Rx timed out\n");
  1158. s->ops->set_rtrg(port, 1);
  1159. }
  1160. static ssize_t rx_fifo_trigger_show(struct device *dev,
  1161. struct device_attribute *attr, char *buf)
  1162. {
  1163. struct uart_port *port = dev_get_drvdata(dev);
  1164. struct sci_port *sci = to_sci_port(port);
  1165. return sprintf(buf, "%d\n", sci->rx_trigger);
  1166. }
  1167. static ssize_t rx_fifo_trigger_store(struct device *dev,
  1168. struct device_attribute *attr,
  1169. const char *buf, size_t count)
  1170. {
  1171. struct uart_port *port = dev_get_drvdata(dev);
  1172. struct sci_port *sci = to_sci_port(port);
  1173. int ret;
  1174. long r;
  1175. ret = kstrtol(buf, 0, &r);
  1176. if (ret)
  1177. return ret;
  1178. sci->rx_trigger = sci->ops->set_rtrg(port, r);
  1179. if (sci->type == PORT_SCIFA || sci->type == PORT_SCIFB)
  1180. sci->ops->set_rtrg(port, 1);
  1181. return count;
  1182. }
  1183. static DEVICE_ATTR_RW(rx_fifo_trigger);
  1184. static ssize_t rx_fifo_timeout_show(struct device *dev,
  1185. struct device_attribute *attr,
  1186. char *buf)
  1187. {
  1188. struct uart_port *port = dev_get_drvdata(dev);
  1189. struct sci_port *sci = to_sci_port(port);
  1190. int v;
  1191. if (sci->type == PORT_HSCIF)
  1192. v = sci->hscif_tot >> HSSCR_TOT_SHIFT;
  1193. else
  1194. v = sci->rx_fifo_timeout;
  1195. return sprintf(buf, "%d\n", v);
  1196. }
  1197. static ssize_t rx_fifo_timeout_store(struct device *dev,
  1198. struct device_attribute *attr,
  1199. const char *buf,
  1200. size_t count)
  1201. {
  1202. struct uart_port *port = dev_get_drvdata(dev);
  1203. struct sci_port *sci = to_sci_port(port);
  1204. int ret;
  1205. long r;
  1206. ret = kstrtol(buf, 0, &r);
  1207. if (ret)
  1208. return ret;
  1209. if (sci->type == PORT_HSCIF) {
  1210. if (r < 0 || r > 3)
  1211. return -EINVAL;
  1212. sci->hscif_tot = r << HSSCR_TOT_SHIFT;
  1213. } else {
  1214. sci->rx_fifo_timeout = r;
  1215. sci->ops->set_rtrg(port, 1);
  1216. if (r > 0)
  1217. timer_setup(&sci->rx_fifo_timer, rx_fifo_timer_fn, 0);
  1218. }
  1219. return count;
  1220. }
  1221. static DEVICE_ATTR_RW(rx_fifo_timeout);
  1222. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  1223. static void sci_dma_tx_complete(void *arg)
  1224. {
  1225. struct sci_port *s = arg;
  1226. struct uart_port *port = &s->port;
  1227. struct tty_port *tport = &port->state->port;
  1228. unsigned long flags;
  1229. dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
  1230. uart_port_lock_irqsave(port, &flags);
  1231. uart_xmit_advance(port, s->tx_dma_len);
  1232. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  1233. uart_write_wakeup(port);
  1234. s->tx_occurred = true;
  1235. if (!kfifo_is_empty(&tport->xmit_fifo)) {
  1236. s->cookie_tx = 0;
  1237. schedule_work(&s->work_tx);
  1238. } else {
  1239. s->cookie_tx = -EINVAL;
  1240. if (s->type == PORT_SCIFA || s->type == PORT_SCIFB ||
  1241. s->regtype == SCIx_RZ_SCIFA_REGTYPE) {
  1242. u16 ctrl = sci_serial_in(port, SCSCR);
  1243. sci_serial_out(port, SCSCR, ctrl & ~SCSCR_TIE);
  1244. if (s->regtype == SCIx_RZ_SCIFA_REGTYPE) {
  1245. /* Switch irq from DMA to SCIF */
  1246. dmaengine_pause(s->chan_tx_saved);
  1247. enable_irq(s->irqs[SCIx_TXI_IRQ]);
  1248. }
  1249. }
  1250. }
  1251. uart_port_unlock_irqrestore(port, flags);
  1252. }
  1253. /* Locking: called with port lock held */
  1254. static int sci_dma_rx_push(struct sci_port *s, void *buf, size_t count)
  1255. {
  1256. struct uart_port *port = &s->port;
  1257. struct tty_port *tport = &port->state->port;
  1258. int copied;
  1259. copied = tty_insert_flip_string(tport, buf, count);
  1260. if (copied < count)
  1261. port->icount.buf_overrun++;
  1262. port->icount.rx += copied;
  1263. return copied;
  1264. }
  1265. static int sci_dma_rx_find_active(struct sci_port *s)
  1266. {
  1267. unsigned int i;
  1268. for (i = 0; i < ARRAY_SIZE(s->cookie_rx); i++)
  1269. if (s->active_rx == s->cookie_rx[i])
  1270. return i;
  1271. return -1;
  1272. }
  1273. /* Must only be called with uart_port_lock taken */
  1274. static void sci_dma_rx_chan_invalidate(struct sci_port *s)
  1275. {
  1276. unsigned int i;
  1277. s->chan_rx = NULL;
  1278. for (i = 0; i < ARRAY_SIZE(s->cookie_rx); i++)
  1279. s->cookie_rx[i] = -EINVAL;
  1280. s->active_rx = 0;
  1281. }
  1282. static void sci_dma_rx_release(struct sci_port *s)
  1283. {
  1284. struct dma_chan *chan = s->chan_rx_saved;
  1285. struct uart_port *port = &s->port;
  1286. unsigned long flags;
  1287. uart_port_lock_irqsave(port, &flags);
  1288. s->chan_rx_saved = NULL;
  1289. sci_dma_rx_chan_invalidate(s);
  1290. uart_port_unlock_irqrestore(port, flags);
  1291. dmaengine_terminate_sync(chan);
  1292. dma_free_coherent(chan->device->dev, s->buf_len_rx * 2, s->rx_buf[0],
  1293. sg_dma_address(&s->sg_rx[0]));
  1294. dma_release_channel(chan);
  1295. }
  1296. static void start_hrtimer_us(struct hrtimer *hrt, unsigned long usec)
  1297. {
  1298. long sec = usec / 1000000;
  1299. long nsec = (usec % 1000000) * 1000;
  1300. ktime_t t = ktime_set(sec, nsec);
  1301. hrtimer_start(hrt, t, HRTIMER_MODE_REL);
  1302. }
  1303. static void sci_dma_rx_reenable_irq(struct sci_port *s)
  1304. {
  1305. struct uart_port *port = &s->port;
  1306. u16 scr;
  1307. /* Direct new serial port interrupts back to CPU */
  1308. scr = sci_serial_in(port, SCSCR);
  1309. if (s->type == PORT_SCIFA || s->type == PORT_SCIFB ||
  1310. s->regtype == SCIx_RZ_SCIFA_REGTYPE) {
  1311. enable_irq(s->irqs[SCIx_RXI_IRQ]);
  1312. if (s->regtype == SCIx_RZ_SCIFA_REGTYPE)
  1313. s->ops->set_rtrg(port, s->rx_trigger);
  1314. else
  1315. scr &= ~SCSCR_RDRQE;
  1316. }
  1317. sci_serial_out(port, SCSCR, scr | SCSCR_RIE);
  1318. }
  1319. static void sci_dma_rx_complete(void *arg)
  1320. {
  1321. struct sci_port *s = arg;
  1322. struct dma_chan *chan = s->chan_rx;
  1323. struct uart_port *port = &s->port;
  1324. struct dma_async_tx_descriptor *desc;
  1325. unsigned long flags;
  1326. int active, count = 0;
  1327. dev_dbg(port->dev, "%s(%d) active cookie %d\n", __func__, port->line,
  1328. s->active_rx);
  1329. hrtimer_cancel(&s->rx_timer);
  1330. uart_port_lock_irqsave(port, &flags);
  1331. active = sci_dma_rx_find_active(s);
  1332. if (active >= 0)
  1333. count = sci_dma_rx_push(s, s->rx_buf[active], s->buf_len_rx);
  1334. if (count)
  1335. tty_flip_buffer_push(&port->state->port);
  1336. desc = dmaengine_prep_slave_sg(s->chan_rx, &s->sg_rx[active], 1,
  1337. DMA_DEV_TO_MEM,
  1338. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1339. if (!desc)
  1340. goto fail;
  1341. desc->callback = sci_dma_rx_complete;
  1342. desc->callback_param = s;
  1343. s->cookie_rx[active] = dmaengine_submit(desc);
  1344. if (dma_submit_error(s->cookie_rx[active]))
  1345. goto fail;
  1346. s->active_rx = s->cookie_rx[!active];
  1347. dma_async_issue_pending(chan);
  1348. uart_port_unlock_irqrestore(port, flags);
  1349. dev_dbg(port->dev, "%s: cookie %d #%d, new active cookie %d\n",
  1350. __func__, s->cookie_rx[active], active, s->active_rx);
  1351. start_hrtimer_us(&s->rx_timer, s->rx_timeout);
  1352. return;
  1353. fail:
  1354. /* Switch to PIO */
  1355. dmaengine_terminate_async(chan);
  1356. sci_dma_rx_chan_invalidate(s);
  1357. sci_dma_rx_reenable_irq(s);
  1358. uart_port_unlock_irqrestore(port, flags);
  1359. dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
  1360. }
  1361. static void sci_dma_tx_release(struct sci_port *s)
  1362. {
  1363. struct dma_chan *chan = s->chan_tx_saved;
  1364. cancel_work_sync(&s->work_tx);
  1365. s->chan_tx_saved = s->chan_tx = NULL;
  1366. s->cookie_tx = -EINVAL;
  1367. dmaengine_terminate_sync(chan);
  1368. dma_unmap_single(chan->device->dev, s->tx_dma_addr, UART_XMIT_SIZE,
  1369. DMA_TO_DEVICE);
  1370. dma_release_channel(chan);
  1371. }
  1372. static int sci_dma_rx_submit(struct sci_port *s, bool port_lock_held)
  1373. {
  1374. struct dma_chan *chan = s->chan_rx;
  1375. struct uart_port *port = &s->port;
  1376. unsigned long flags;
  1377. int i;
  1378. for (i = 0; i < 2; i++) {
  1379. struct scatterlist *sg = &s->sg_rx[i];
  1380. struct dma_async_tx_descriptor *desc;
  1381. desc = dmaengine_prep_slave_sg(chan,
  1382. sg, 1, DMA_DEV_TO_MEM,
  1383. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1384. if (!desc)
  1385. goto fail;
  1386. desc->callback = sci_dma_rx_complete;
  1387. desc->callback_param = s;
  1388. s->cookie_rx[i] = dmaengine_submit(desc);
  1389. if (dma_submit_error(s->cookie_rx[i]))
  1390. goto fail;
  1391. }
  1392. s->active_rx = s->cookie_rx[0];
  1393. dma_async_issue_pending(chan);
  1394. return 0;
  1395. fail:
  1396. /* Switch to PIO */
  1397. if (!port_lock_held)
  1398. uart_port_lock_irqsave(port, &flags);
  1399. if (i)
  1400. dmaengine_terminate_async(chan);
  1401. sci_dma_rx_chan_invalidate(s);
  1402. sci_start_rx(port);
  1403. if (!port_lock_held)
  1404. uart_port_unlock_irqrestore(port, flags);
  1405. return -EAGAIN;
  1406. }
  1407. static void sci_dma_tx_work_fn(struct work_struct *work)
  1408. {
  1409. struct sci_port *s = container_of(work, struct sci_port, work_tx);
  1410. struct dma_async_tx_descriptor *desc;
  1411. struct dma_chan *chan = s->chan_tx;
  1412. struct uart_port *port = &s->port;
  1413. struct tty_port *tport = &port->state->port;
  1414. unsigned long flags;
  1415. unsigned int tail;
  1416. dma_addr_t buf;
  1417. /*
  1418. * DMA is idle now.
  1419. * Port xmit buffer is already mapped, and it is one page... Just adjust
  1420. * offsets and lengths. Since it is a circular buffer, we have to
  1421. * transmit till the end, and then the rest. Take the port lock to get a
  1422. * consistent xmit buffer state.
  1423. */
  1424. uart_port_lock_irq(port);
  1425. s->tx_dma_len = kfifo_out_linear(&tport->xmit_fifo, &tail,
  1426. UART_XMIT_SIZE);
  1427. buf = s->tx_dma_addr + tail;
  1428. if (!s->tx_dma_len) {
  1429. /* Transmit buffer has been flushed */
  1430. uart_port_unlock_irq(port);
  1431. return;
  1432. }
  1433. desc = dmaengine_prep_slave_single(chan, buf, s->tx_dma_len,
  1434. DMA_MEM_TO_DEV,
  1435. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1436. if (!desc) {
  1437. uart_port_unlock_irq(port);
  1438. dev_warn(port->dev, "Failed preparing Tx DMA descriptor\n");
  1439. goto switch_to_pio;
  1440. }
  1441. dma_sync_single_for_device(chan->device->dev, buf, s->tx_dma_len,
  1442. DMA_TO_DEVICE);
  1443. desc->callback = sci_dma_tx_complete;
  1444. desc->callback_param = s;
  1445. s->cookie_tx = dmaengine_submit(desc);
  1446. if (dma_submit_error(s->cookie_tx)) {
  1447. uart_port_unlock_irq(port);
  1448. dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
  1449. goto switch_to_pio;
  1450. }
  1451. uart_port_unlock_irq(port);
  1452. dev_dbg(port->dev, "%s: %p: %u, cookie %d\n",
  1453. __func__, tport->xmit_buf, tail, s->cookie_tx);
  1454. dma_async_issue_pending(chan);
  1455. return;
  1456. switch_to_pio:
  1457. uart_port_lock_irqsave(port, &flags);
  1458. s->chan_tx = NULL;
  1459. sci_start_tx(port);
  1460. uart_port_unlock_irqrestore(port, flags);
  1461. return;
  1462. }
  1463. static enum hrtimer_restart sci_dma_rx_timer_fn(struct hrtimer *t)
  1464. {
  1465. struct sci_port *s = container_of(t, struct sci_port, rx_timer);
  1466. struct dma_chan *chan = s->chan_rx;
  1467. struct uart_port *port = &s->port;
  1468. struct dma_tx_state state;
  1469. enum dma_status status;
  1470. unsigned long flags;
  1471. unsigned int read;
  1472. int active, count;
  1473. dev_dbg(port->dev, "DMA Rx timed out\n");
  1474. uart_port_lock_irqsave(port, &flags);
  1475. active = sci_dma_rx_find_active(s);
  1476. if (active < 0) {
  1477. uart_port_unlock_irqrestore(port, flags);
  1478. return HRTIMER_NORESTART;
  1479. }
  1480. status = dmaengine_tx_status(s->chan_rx, s->active_rx, &state);
  1481. if (status == DMA_COMPLETE) {
  1482. uart_port_unlock_irqrestore(port, flags);
  1483. dev_dbg(port->dev, "Cookie %d #%d has already completed\n",
  1484. s->active_rx, active);
  1485. /* Let packet complete handler take care of the packet */
  1486. return HRTIMER_NORESTART;
  1487. }
  1488. dmaengine_pause(chan);
  1489. /*
  1490. * sometimes DMA transfer doesn't stop even if it is stopped and
  1491. * data keeps on coming until transaction is complete so check
  1492. * for DMA_COMPLETE again
  1493. * Let packet complete handler take care of the packet
  1494. */
  1495. status = dmaengine_tx_status(s->chan_rx, s->active_rx, &state);
  1496. if (status == DMA_COMPLETE) {
  1497. uart_port_unlock_irqrestore(port, flags);
  1498. dev_dbg(port->dev, "Transaction complete after DMA engine was stopped");
  1499. return HRTIMER_NORESTART;
  1500. }
  1501. /* Handle incomplete DMA receive */
  1502. dmaengine_terminate_async(s->chan_rx);
  1503. read = sg_dma_len(&s->sg_rx[active]) - state.residue;
  1504. if (read) {
  1505. count = sci_dma_rx_push(s, s->rx_buf[active], read);
  1506. if (count)
  1507. tty_flip_buffer_push(&port->state->port);
  1508. }
  1509. if (s->type == PORT_SCIFA || s->type == PORT_SCIFB ||
  1510. s->regtype == SCIx_RZ_SCIFA_REGTYPE)
  1511. sci_dma_rx_submit(s, true);
  1512. sci_dma_rx_reenable_irq(s);
  1513. uart_port_unlock_irqrestore(port, flags);
  1514. return HRTIMER_NORESTART;
  1515. }
  1516. static struct dma_chan *sci_request_dma_chan(struct uart_port *port,
  1517. enum dma_transfer_direction dir)
  1518. {
  1519. struct dma_chan *chan;
  1520. struct dma_slave_config cfg;
  1521. int ret;
  1522. chan = dma_request_chan(port->dev, dir == DMA_MEM_TO_DEV ? "tx" : "rx");
  1523. if (IS_ERR(chan)) {
  1524. dev_dbg(port->dev, "dma_request_chan failed\n");
  1525. return NULL;
  1526. }
  1527. memset(&cfg, 0, sizeof(cfg));
  1528. cfg.direction = dir;
  1529. cfg.dst_addr = port->mapbase +
  1530. (sci_getreg(port, SCxTDR)->offset << port->regshift);
  1531. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  1532. cfg.src_addr = port->mapbase +
  1533. (sci_getreg(port, SCxRDR)->offset << port->regshift);
  1534. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  1535. ret = dmaengine_slave_config(chan, &cfg);
  1536. if (ret) {
  1537. dev_warn(port->dev, "dmaengine_slave_config failed %d\n", ret);
  1538. dma_release_channel(chan);
  1539. return NULL;
  1540. }
  1541. return chan;
  1542. }
  1543. static void sci_request_dma(struct uart_port *port)
  1544. {
  1545. struct sci_port *s = to_sci_port(port);
  1546. struct tty_port *tport = &port->state->port;
  1547. struct dma_chan *chan;
  1548. dev_dbg(port->dev, "%s: port %d\n", __func__, port->line);
  1549. /*
  1550. * DMA on console may interfere with Kernel log messages which use
  1551. * plain putchar(). So, simply don't use it with a console.
  1552. */
  1553. if (uart_console(port))
  1554. return;
  1555. if (!port->dev->of_node)
  1556. return;
  1557. s->cookie_tx = -EINVAL;
  1558. /*
  1559. * Don't request a dma channel if no channel was specified
  1560. * in the device tree.
  1561. */
  1562. if (!of_property_present(port->dev->of_node, "dmas"))
  1563. return;
  1564. chan = sci_request_dma_chan(port, DMA_MEM_TO_DEV);
  1565. dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
  1566. if (chan) {
  1567. /* UART circular tx buffer is an aligned page. */
  1568. s->tx_dma_addr = dma_map_single(chan->device->dev,
  1569. tport->xmit_buf,
  1570. UART_XMIT_SIZE,
  1571. DMA_TO_DEVICE);
  1572. if (dma_mapping_error(chan->device->dev, s->tx_dma_addr)) {
  1573. dev_warn(port->dev, "Failed mapping Tx DMA descriptor\n");
  1574. dma_release_channel(chan);
  1575. } else {
  1576. dev_dbg(port->dev, "%s: mapped %lu@%p to %pad\n",
  1577. __func__, UART_XMIT_SIZE,
  1578. tport->xmit_buf, &s->tx_dma_addr);
  1579. INIT_WORK(&s->work_tx, sci_dma_tx_work_fn);
  1580. s->chan_tx_saved = s->chan_tx = chan;
  1581. }
  1582. }
  1583. chan = sci_request_dma_chan(port, DMA_DEV_TO_MEM);
  1584. dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
  1585. if (chan) {
  1586. unsigned int i;
  1587. dma_addr_t dma;
  1588. void *buf;
  1589. s->buf_len_rx = 2 * max_t(size_t, 16, port->fifosize);
  1590. buf = dma_alloc_coherent(chan->device->dev, s->buf_len_rx * 2,
  1591. &dma, GFP_KERNEL);
  1592. if (!buf) {
  1593. dev_warn(port->dev,
  1594. "Failed to allocate Rx dma buffer, using PIO\n");
  1595. dma_release_channel(chan);
  1596. return;
  1597. }
  1598. for (i = 0; i < 2; i++) {
  1599. struct scatterlist *sg = &s->sg_rx[i];
  1600. sg_init_table(sg, 1);
  1601. s->rx_buf[i] = buf;
  1602. sg_dma_address(sg) = dma;
  1603. sg_dma_len(sg) = s->buf_len_rx;
  1604. buf += s->buf_len_rx;
  1605. dma += s->buf_len_rx;
  1606. }
  1607. hrtimer_setup(&s->rx_timer, sci_dma_rx_timer_fn, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1608. s->chan_rx_saved = s->chan_rx = chan;
  1609. if (s->type == PORT_SCIFA || s->type == PORT_SCIFB ||
  1610. s->regtype == SCIx_RZ_SCIFA_REGTYPE)
  1611. sci_dma_rx_submit(s, false);
  1612. }
  1613. }
  1614. static void sci_free_dma(struct uart_port *port)
  1615. {
  1616. struct sci_port *s = to_sci_port(port);
  1617. if (s->chan_tx_saved)
  1618. sci_dma_tx_release(s);
  1619. if (s->chan_rx_saved)
  1620. sci_dma_rx_release(s);
  1621. }
  1622. static void sci_flush_buffer(struct uart_port *port)
  1623. {
  1624. struct sci_port *s = to_sci_port(port);
  1625. /*
  1626. * In uart_flush_buffer(), the xmit circular buffer has just been
  1627. * cleared, so we have to reset tx_dma_len accordingly, and stop any
  1628. * pending transfers
  1629. */
  1630. s->tx_dma_len = 0;
  1631. if (s->chan_tx) {
  1632. dmaengine_terminate_async(s->chan_tx);
  1633. s->cookie_tx = -EINVAL;
  1634. }
  1635. }
  1636. static void sci_dma_check_tx_occurred(struct sci_port *s)
  1637. {
  1638. struct dma_tx_state state;
  1639. enum dma_status status;
  1640. if (!s->chan_tx || s->cookie_tx <= 0)
  1641. return;
  1642. status = dmaengine_tx_status(s->chan_tx, s->cookie_tx, &state);
  1643. if (status == DMA_COMPLETE || status == DMA_IN_PROGRESS)
  1644. s->tx_occurred = true;
  1645. }
  1646. #else /* !CONFIG_SERIAL_SH_SCI_DMA */
  1647. static inline void sci_request_dma(struct uart_port *port)
  1648. {
  1649. }
  1650. static inline void sci_free_dma(struct uart_port *port)
  1651. {
  1652. }
  1653. static void sci_dma_check_tx_occurred(struct sci_port *s)
  1654. {
  1655. }
  1656. #define sci_flush_buffer NULL
  1657. #endif /* !CONFIG_SERIAL_SH_SCI_DMA */
  1658. static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
  1659. {
  1660. struct uart_port *port = ptr;
  1661. struct sci_port *s = to_sci_port(port);
  1662. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  1663. if (s->chan_rx) {
  1664. u16 scr = sci_serial_in(port, SCSCR);
  1665. u16 ssr = sci_serial_in(port, SCxSR);
  1666. /* Disable future Rx interrupts */
  1667. if (s->type == PORT_SCIFA || s->type == PORT_SCIFB ||
  1668. s->regtype == SCIx_RZ_SCIFA_REGTYPE) {
  1669. disable_irq_nosync(s->irqs[SCIx_RXI_IRQ]);
  1670. if (s->regtype == SCIx_RZ_SCIFA_REGTYPE) {
  1671. s->ops->set_rtrg(port, 1);
  1672. scr |= SCSCR_RIE;
  1673. } else {
  1674. scr |= SCSCR_RDRQE;
  1675. }
  1676. } else {
  1677. if (sci_dma_rx_submit(s, false) < 0)
  1678. goto handle_pio;
  1679. scr &= ~SCSCR_RIE;
  1680. }
  1681. sci_serial_out(port, SCSCR, scr);
  1682. /* Clear current interrupt */
  1683. sci_serial_out(port, SCxSR,
  1684. ssr & ~(SCIF_DR | SCxSR_RDxF(port)));
  1685. dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u us\n",
  1686. jiffies, s->rx_timeout);
  1687. start_hrtimer_us(&s->rx_timer, s->rx_timeout);
  1688. return IRQ_HANDLED;
  1689. }
  1690. handle_pio:
  1691. #endif
  1692. if (s->rx_trigger > 1 && s->rx_fifo_timeout > 0) {
  1693. if (!s->ops->rtrg_enabled(port))
  1694. s->ops->set_rtrg(port, s->rx_trigger);
  1695. mod_timer(&s->rx_fifo_timer, jiffies + DIV_ROUND_UP(
  1696. s->rx_frame * HZ * s->rx_fifo_timeout, 1000000));
  1697. }
  1698. /* I think sci_receive_chars has to be called irrespective
  1699. * of whether the I_IXOFF is set, otherwise, how is the interrupt
  1700. * to be disabled?
  1701. */
  1702. s->ops->receive_chars(port);
  1703. return IRQ_HANDLED;
  1704. }
  1705. static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
  1706. {
  1707. struct uart_port *port = ptr;
  1708. unsigned long flags;
  1709. struct sci_port *s = to_sci_port(port);
  1710. uart_port_lock_irqsave(port, &flags);
  1711. s->ops->transmit_chars(port);
  1712. uart_port_unlock_irqrestore(port, flags);
  1713. return IRQ_HANDLED;
  1714. }
  1715. static irqreturn_t sci_tx_end_interrupt(int irq, void *ptr)
  1716. {
  1717. struct uart_port *port = ptr;
  1718. struct sci_port *s = to_sci_port(port);
  1719. const struct sci_common_regs *regs = s->params->common_regs;
  1720. unsigned long flags;
  1721. u32 ctrl;
  1722. if (s->type != PORT_SCI && !sci_is_rsci_type(s->type))
  1723. return sci_tx_interrupt(irq, ptr);
  1724. uart_port_lock_irqsave(port, &flags);
  1725. ctrl = s->ops->read_reg(port, regs->control) &
  1726. ~(s->params->param_bits->te_clear);
  1727. s->ops->write_reg(port, regs->control, ctrl);
  1728. uart_port_unlock_irqrestore(port, flags);
  1729. return IRQ_HANDLED;
  1730. }
  1731. static irqreturn_t sci_br_interrupt(int irq, void *ptr)
  1732. {
  1733. struct uart_port *port = ptr;
  1734. struct sci_port *s = to_sci_port(port);
  1735. /* Handle BREAKs */
  1736. sci_handle_breaks(port);
  1737. /* drop invalid character received before break was detected */
  1738. sci_serial_in(port, SCxRDR);
  1739. s->ops->clear_SCxSR(port, SCxSR_BREAK_CLEAR(port));
  1740. return IRQ_HANDLED;
  1741. }
  1742. static irqreturn_t sci_er_interrupt(int irq, void *ptr)
  1743. {
  1744. struct uart_port *port = ptr;
  1745. struct sci_port *s = to_sci_port(port);
  1746. if (s->irqs[SCIx_ERI_IRQ] == s->irqs[SCIx_BRI_IRQ]) {
  1747. /* Break and Error interrupts are muxed */
  1748. unsigned short ssr_status = sci_serial_in(port, SCxSR);
  1749. /* Break Interrupt */
  1750. if (ssr_status & SCxSR_BRK(port))
  1751. sci_br_interrupt(irq, ptr);
  1752. /* Break only? */
  1753. if (!(ssr_status & SCxSR_ERRORS(port)))
  1754. return IRQ_HANDLED;
  1755. }
  1756. /* Handle errors */
  1757. if (s->type == PORT_SCI) {
  1758. if (sci_handle_errors(port)) {
  1759. /* discard character in rx buffer */
  1760. sci_serial_in(port, SCxSR);
  1761. s->ops->clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
  1762. }
  1763. } else {
  1764. sci_handle_fifo_overrun(port);
  1765. if (!s->chan_rx)
  1766. s->ops->receive_chars(port);
  1767. }
  1768. s->ops->clear_SCxSR(port, SCxSR_ERROR_CLEAR(port));
  1769. /* Kick the transmission */
  1770. if (!s->chan_tx)
  1771. sci_tx_interrupt(irq, ptr);
  1772. return IRQ_HANDLED;
  1773. }
  1774. static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
  1775. {
  1776. unsigned short ssr_status, scr_status, err_enabled, orer_status = 0;
  1777. struct uart_port *port = ptr;
  1778. struct sci_port *s = to_sci_port(port);
  1779. irqreturn_t ret = IRQ_NONE;
  1780. ssr_status = sci_serial_in(port, SCxSR);
  1781. scr_status = sci_serial_in(port, SCSCR);
  1782. if (s->params->overrun_reg == SCxSR)
  1783. orer_status = ssr_status;
  1784. else if (sci_getreg(port, s->params->overrun_reg)->size)
  1785. orer_status = sci_serial_in(port, s->params->overrun_reg);
  1786. err_enabled = scr_status & port_rx_irq_mask(port);
  1787. /* Tx Interrupt */
  1788. if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
  1789. !s->chan_tx)
  1790. ret = sci_tx_interrupt(irq, ptr);
  1791. /*
  1792. * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
  1793. * DR flags
  1794. */
  1795. if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
  1796. (scr_status & SCSCR_RIE))
  1797. ret = sci_rx_interrupt(irq, ptr);
  1798. /* Error Interrupt */
  1799. if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
  1800. ret = sci_er_interrupt(irq, ptr);
  1801. /* Break Interrupt */
  1802. if (s->irqs[SCIx_ERI_IRQ] != s->irqs[SCIx_BRI_IRQ] &&
  1803. (ssr_status & SCxSR_BRK(port)) && err_enabled)
  1804. ret = sci_br_interrupt(irq, ptr);
  1805. /* Overrun Interrupt */
  1806. if (orer_status & s->params->overrun_mask) {
  1807. sci_handle_fifo_overrun(port);
  1808. ret = IRQ_HANDLED;
  1809. }
  1810. return ret;
  1811. }
  1812. static const struct sci_irq_desc {
  1813. const char *desc;
  1814. irq_handler_t handler;
  1815. } sci_irq_desc[] = {
  1816. /*
  1817. * Split out handlers, the default case.
  1818. */
  1819. [SCIx_ERI_IRQ] = {
  1820. .desc = "rx err",
  1821. .handler = sci_er_interrupt,
  1822. },
  1823. [SCIx_RXI_IRQ] = {
  1824. .desc = "rx full",
  1825. .handler = sci_rx_interrupt,
  1826. },
  1827. [SCIx_TXI_IRQ] = {
  1828. .desc = "tx empty",
  1829. .handler = sci_tx_interrupt,
  1830. },
  1831. [SCIx_BRI_IRQ] = {
  1832. .desc = "break",
  1833. .handler = sci_br_interrupt,
  1834. },
  1835. [SCIx_DRI_IRQ] = {
  1836. .desc = "rx ready",
  1837. .handler = sci_rx_interrupt,
  1838. },
  1839. [SCIx_TEI_IRQ] = {
  1840. .desc = "tx end",
  1841. .handler = sci_tx_end_interrupt,
  1842. },
  1843. /*
  1844. * Special muxed handler.
  1845. */
  1846. [SCIx_MUX_IRQ] = {
  1847. .desc = "mux",
  1848. .handler = sci_mpxed_interrupt,
  1849. },
  1850. };
  1851. static int sci_request_irq(struct sci_port *port)
  1852. {
  1853. struct uart_port *up = &port->port;
  1854. int i, j, w, ret = 0;
  1855. for (i = j = 0; i < SCIx_NR_IRQS; i++, j++) {
  1856. const struct sci_irq_desc *desc;
  1857. int irq;
  1858. /* Check if already registered (muxed) */
  1859. for (w = 0; w < i; w++)
  1860. if (port->irqs[w] == port->irqs[i])
  1861. w = i + 1;
  1862. if (w > i)
  1863. continue;
  1864. if (SCIx_IRQ_IS_MUXED(port)) {
  1865. i = SCIx_MUX_IRQ;
  1866. irq = up->irq;
  1867. } else {
  1868. irq = port->irqs[i];
  1869. /*
  1870. * Certain port types won't support all of the
  1871. * available interrupt sources.
  1872. */
  1873. if (unlikely(irq < 0))
  1874. continue;
  1875. }
  1876. desc = sci_irq_desc + i;
  1877. port->irqstr[j] = kasprintf(GFP_KERNEL, "%s:%s",
  1878. dev_name(up->dev), desc->desc);
  1879. if (!port->irqstr[j]) {
  1880. ret = -ENOMEM;
  1881. goto out_nomem;
  1882. }
  1883. ret = request_irq(irq, desc->handler, up->irqflags,
  1884. port->irqstr[j], port);
  1885. if (unlikely(ret)) {
  1886. dev_err(up->dev, "Can't allocate %s IRQ\n", desc->desc);
  1887. goto out_noirq;
  1888. }
  1889. }
  1890. return 0;
  1891. out_noirq:
  1892. while (--i >= 0)
  1893. free_irq(port->irqs[i], port);
  1894. out_nomem:
  1895. while (--j >= 0)
  1896. kfree(port->irqstr[j]);
  1897. return ret;
  1898. }
  1899. static void sci_free_irq(struct sci_port *port)
  1900. {
  1901. int i, j;
  1902. /*
  1903. * Intentionally in reverse order so we iterate over the muxed
  1904. * IRQ first.
  1905. */
  1906. for (i = 0; i < SCIx_NR_IRQS; i++) {
  1907. int irq = port->irqs[i];
  1908. /*
  1909. * Certain port types won't support all of the available
  1910. * interrupt sources.
  1911. */
  1912. if (unlikely(irq < 0))
  1913. continue;
  1914. /* Check if already freed (irq was muxed) */
  1915. for (j = 0; j < i; j++)
  1916. if (port->irqs[j] == irq)
  1917. j = i + 1;
  1918. if (j > i)
  1919. continue;
  1920. free_irq(port->irqs[i], port);
  1921. kfree(port->irqstr[i]);
  1922. if (SCIx_IRQ_IS_MUXED(port)) {
  1923. /* If there's only one IRQ, we're done. */
  1924. return;
  1925. }
  1926. }
  1927. }
  1928. static unsigned int sci_tx_empty(struct uart_port *port)
  1929. {
  1930. unsigned short status = sci_serial_in(port, SCxSR);
  1931. unsigned short in_tx_fifo = sci_txfill(port);
  1932. struct sci_port *s = to_sci_port(port);
  1933. sci_dma_check_tx_occurred(s);
  1934. if (!s->tx_occurred)
  1935. return TIOCSER_TEMT;
  1936. return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
  1937. }
  1938. static void sci_set_rts(struct uart_port *port, bool state)
  1939. {
  1940. struct sci_port *s = to_sci_port(port);
  1941. if (s->type == PORT_SCIFA || s->type == PORT_SCIFB) {
  1942. u16 data = sci_serial_in(port, SCPDR);
  1943. /* Active low */
  1944. if (state)
  1945. data &= ~SCPDR_RTSD;
  1946. else
  1947. data |= SCPDR_RTSD;
  1948. sci_serial_out(port, SCPDR, data);
  1949. /* RTS# is output */
  1950. sci_serial_out(port, SCPCR,
  1951. sci_serial_in(port, SCPCR) | SCPCR_RTSC);
  1952. } else if (sci_getreg(port, SCSPTR)->size) {
  1953. u16 ctrl = sci_serial_in(port, SCSPTR);
  1954. /* Active low */
  1955. if (state)
  1956. ctrl &= ~SCSPTR_RTSDT;
  1957. else
  1958. ctrl |= SCSPTR_RTSDT;
  1959. sci_serial_out(port, SCSPTR, ctrl);
  1960. }
  1961. }
  1962. static bool sci_get_cts(struct uart_port *port)
  1963. {
  1964. struct sci_port *s = to_sci_port(port);
  1965. if (s->type == PORT_SCIFA || s->type == PORT_SCIFB) {
  1966. /* Active low */
  1967. return !(sci_serial_in(port, SCPDR) & SCPDR_CTSD);
  1968. } else if (sci_getreg(port, SCSPTR)->size) {
  1969. /* Active low */
  1970. return !(sci_serial_in(port, SCSPTR) & SCSPTR_CTSDT);
  1971. }
  1972. return true;
  1973. }
  1974. /*
  1975. * Modem control is a bit of a mixed bag for SCI(F) ports. Generally
  1976. * CTS/RTS is supported in hardware by at least one port and controlled
  1977. * via SCSPTR (SCxPCR for SCIFA/B parts), or external pins (presently
  1978. * handled via the ->init_pins() op, which is a bit of a one-way street,
  1979. * lacking any ability to defer pin control -- this will later be
  1980. * converted over to the GPIO framework).
  1981. *
  1982. * Other modes (such as loopback) are supported generically on certain
  1983. * port types, but not others. For these it's sufficient to test for the
  1984. * existence of the support register and simply ignore the port type.
  1985. */
  1986. static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1987. {
  1988. struct sci_port *s = to_sci_port(port);
  1989. if (mctrl & TIOCM_LOOP) {
  1990. const struct plat_sci_reg *reg;
  1991. /*
  1992. * Standard loopback mode for SCFCR ports.
  1993. */
  1994. reg = sci_getreg(port, SCFCR);
  1995. if (reg->size)
  1996. sci_serial_out(port, SCFCR,
  1997. sci_serial_in(port, SCFCR) | SCFCR_LOOP);
  1998. }
  1999. mctrl_gpio_set(s->gpios, mctrl);
  2000. if (!s->has_rtscts)
  2001. return;
  2002. if (!(mctrl & TIOCM_RTS)) {
  2003. /* Disable Auto RTS */
  2004. if (s->regtype != SCIx_RZV2H_SCIF_REGTYPE)
  2005. sci_serial_out(port, SCFCR,
  2006. sci_serial_in(port, SCFCR) & ~SCFCR_MCE);
  2007. /* Clear RTS */
  2008. sci_set_rts(port, 0);
  2009. } else if (s->autorts) {
  2010. if (s->type == PORT_SCIFA || s->type == PORT_SCIFB) {
  2011. /* Enable RTS# pin function */
  2012. sci_serial_out(port, SCPCR,
  2013. sci_serial_in(port, SCPCR) & ~SCPCR_RTSC);
  2014. }
  2015. /* Enable Auto RTS */
  2016. if (s->regtype != SCIx_RZV2H_SCIF_REGTYPE)
  2017. sci_serial_out(port, SCFCR,
  2018. sci_serial_in(port, SCFCR) | SCFCR_MCE);
  2019. } else {
  2020. /* Set RTS */
  2021. sci_set_rts(port, 1);
  2022. }
  2023. }
  2024. static unsigned int sci_get_mctrl(struct uart_port *port)
  2025. {
  2026. struct sci_port *s = to_sci_port(port);
  2027. struct mctrl_gpios *gpios = s->gpios;
  2028. unsigned int mctrl = 0;
  2029. mctrl_gpio_get(gpios, &mctrl);
  2030. /*
  2031. * CTS/RTS is handled in hardware when supported, while nothing
  2032. * else is wired up.
  2033. */
  2034. if (s->autorts) {
  2035. if (sci_get_cts(port))
  2036. mctrl |= TIOCM_CTS;
  2037. } else if (!mctrl_gpio_to_gpiod(gpios, UART_GPIO_CTS)) {
  2038. mctrl |= TIOCM_CTS;
  2039. }
  2040. if (!mctrl_gpio_to_gpiod(gpios, UART_GPIO_DSR))
  2041. mctrl |= TIOCM_DSR;
  2042. if (!mctrl_gpio_to_gpiod(gpios, UART_GPIO_DCD))
  2043. mctrl |= TIOCM_CAR;
  2044. return mctrl;
  2045. }
  2046. static void sci_enable_ms(struct uart_port *port)
  2047. {
  2048. mctrl_gpio_enable_ms(to_sci_port(port)->gpios);
  2049. }
  2050. static void sci_break_ctl(struct uart_port *port, int break_state)
  2051. {
  2052. unsigned short scscr, scsptr;
  2053. unsigned long flags;
  2054. /* check whether the port has SCSPTR */
  2055. if (!sci_getreg(port, SCSPTR)->size) {
  2056. /*
  2057. * Not supported by hardware. Most parts couple break and rx
  2058. * interrupts together, with break detection always enabled.
  2059. */
  2060. return;
  2061. }
  2062. uart_port_lock_irqsave(port, &flags);
  2063. scsptr = sci_serial_in(port, SCSPTR);
  2064. scscr = sci_serial_in(port, SCSCR);
  2065. if (break_state == -1) {
  2066. scsptr = (scsptr | SCSPTR_SPB2IO) & ~SCSPTR_SPB2DT;
  2067. scscr &= ~SCSCR_TE;
  2068. } else {
  2069. scsptr = (scsptr | SCSPTR_SPB2DT) & ~SCSPTR_SPB2IO;
  2070. scscr |= SCSCR_TE;
  2071. }
  2072. sci_serial_out(port, SCSPTR, scsptr);
  2073. sci_serial_out(port, SCSCR, scscr);
  2074. uart_port_unlock_irqrestore(port, flags);
  2075. }
  2076. static void sci_shutdown_complete(struct uart_port *port)
  2077. {
  2078. struct sci_port *s = to_sci_port(port);
  2079. u16 scr;
  2080. scr = sci_serial_in(port, SCSCR);
  2081. sci_serial_out(port, SCSCR,
  2082. scr & (SCSCR_CKE1 | SCSCR_CKE0 | s->hscif_tot));
  2083. }
  2084. int sci_startup(struct uart_port *port)
  2085. {
  2086. struct sci_port *s = to_sci_port(port);
  2087. int ret;
  2088. dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
  2089. s->tx_occurred = false;
  2090. sci_request_dma(port);
  2091. ret = sci_request_irq(s);
  2092. if (unlikely(ret < 0)) {
  2093. sci_free_dma(port);
  2094. return ret;
  2095. }
  2096. return 0;
  2097. }
  2098. EXPORT_SYMBOL_NS_GPL(sci_startup, "SH_SCI");
  2099. void sci_shutdown(struct uart_port *port)
  2100. {
  2101. struct sci_port *s = to_sci_port(port);
  2102. unsigned long flags;
  2103. dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
  2104. s->autorts = false;
  2105. mctrl_gpio_disable_ms_sync(to_sci_port(port)->gpios);
  2106. uart_port_lock_irqsave(port, &flags);
  2107. s->port.ops->stop_rx(port);
  2108. s->port.ops->stop_tx(port);
  2109. s->ops->shutdown_complete(port);
  2110. uart_port_unlock_irqrestore(port, flags);
  2111. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  2112. if (s->chan_rx_saved) {
  2113. dev_dbg(port->dev, "%s(%d) deleting rx_timer\n", __func__,
  2114. port->line);
  2115. hrtimer_cancel(&s->rx_timer);
  2116. }
  2117. #endif
  2118. if (s->rx_trigger > 1 && s->rx_fifo_timeout > 0)
  2119. timer_delete_sync(&s->rx_fifo_timer);
  2120. sci_free_irq(s);
  2121. sci_free_dma(port);
  2122. }
  2123. EXPORT_SYMBOL_NS_GPL(sci_shutdown, "SH_SCI");
  2124. static int sci_sck_calc(struct sci_port *s, unsigned int bps,
  2125. unsigned int *srr)
  2126. {
  2127. unsigned long freq = s->clk_rates[SCI_SCK];
  2128. int err, min_err = INT_MAX;
  2129. unsigned int sr;
  2130. if (s->type != PORT_HSCIF)
  2131. freq *= 2;
  2132. for_each_sr(sr, s) {
  2133. err = DIV_ROUND_CLOSEST(freq, sr) - bps;
  2134. if (abs(err) >= abs(min_err))
  2135. continue;
  2136. min_err = err;
  2137. *srr = sr - 1;
  2138. if (!err)
  2139. break;
  2140. }
  2141. dev_dbg(s->port.dev, "SCK: %u%+d bps using SR %u\n", bps, min_err,
  2142. *srr + 1);
  2143. return min_err;
  2144. }
  2145. static int sci_brg_calc(struct sci_port *s, unsigned int bps,
  2146. unsigned long freq, unsigned int *dlr,
  2147. unsigned int *srr)
  2148. {
  2149. int err, min_err = INT_MAX;
  2150. unsigned int sr, dl;
  2151. if (s->type != PORT_HSCIF)
  2152. freq *= 2;
  2153. for_each_sr(sr, s) {
  2154. dl = DIV_ROUND_CLOSEST(freq, sr * bps);
  2155. dl = clamp(dl, 1U, 65535U);
  2156. err = DIV_ROUND_CLOSEST(freq, sr * dl) - bps;
  2157. if (abs(err) >= abs(min_err))
  2158. continue;
  2159. min_err = err;
  2160. *dlr = dl;
  2161. *srr = sr - 1;
  2162. if (!err)
  2163. break;
  2164. }
  2165. dev_dbg(s->port.dev, "BRG: %u%+d bps using DL %u SR %u\n", bps,
  2166. min_err, *dlr, *srr + 1);
  2167. return min_err;
  2168. }
  2169. /* calculate sample rate, BRR, and clock select */
  2170. int sci_scbrr_calc(struct sci_port *s, unsigned int bps, unsigned int *brr,
  2171. unsigned int *srr, unsigned int *cks)
  2172. {
  2173. unsigned long freq = s->clk_rates[SCI_FCK];
  2174. unsigned int sr, br, prediv, scrate, c;
  2175. int err, min_err = INT_MAX;
  2176. if (s->type != PORT_HSCIF)
  2177. freq *= 2;
  2178. /*
  2179. * Find the combination of sample rate and clock select with the
  2180. * smallest deviation from the desired baud rate.
  2181. * Prefer high sample rates to maximise the receive margin.
  2182. *
  2183. * M: Receive margin (%)
  2184. * N: Ratio of bit rate to clock (N = sampling rate)
  2185. * D: Clock duty (D = 0 to 1.0)
  2186. * L: Frame length (L = 9 to 12)
  2187. * F: Absolute value of clock frequency deviation
  2188. *
  2189. * M = |(0.5 - 1 / 2 * N) - ((L - 0.5) * F) -
  2190. * (|D - 0.5| / N * (1 + F))|
  2191. * NOTE: Usually, treat D for 0.5, F is 0 by this calculation.
  2192. */
  2193. for_each_sr(sr, s) {
  2194. for (c = 0; c <= 3; c++) {
  2195. /* integerized formulas from HSCIF documentation */
  2196. prediv = sr << (2 * c + 1);
  2197. /*
  2198. * We need to calculate:
  2199. *
  2200. * br = freq / (prediv * bps) clamped to [1..256]
  2201. * err = freq / (br * prediv) - bps
  2202. *
  2203. * Watch out for overflow when calculating the desired
  2204. * sampling clock rate!
  2205. */
  2206. if (bps > UINT_MAX / prediv)
  2207. break;
  2208. scrate = prediv * bps;
  2209. br = DIV_ROUND_CLOSEST(freq, scrate);
  2210. br = clamp(br, 1U, 256U);
  2211. err = DIV_ROUND_CLOSEST(freq, br * prediv) - bps;
  2212. if (abs(err) >= abs(min_err))
  2213. continue;
  2214. min_err = err;
  2215. *brr = br - 1;
  2216. *srr = sr - 1;
  2217. *cks = c;
  2218. if (!err)
  2219. goto found;
  2220. }
  2221. }
  2222. found:
  2223. dev_dbg(s->port.dev, "BRR: %u%+d bps using N %u SR %u cks %u\n", bps,
  2224. min_err, *brr, *srr + 1, *cks);
  2225. return min_err;
  2226. }
  2227. EXPORT_SYMBOL_NS_GPL(sci_scbrr_calc, "SH_SCI");
  2228. static void sci_reset(struct uart_port *port)
  2229. {
  2230. const struct plat_sci_reg *reg;
  2231. unsigned int status;
  2232. struct sci_port *s = to_sci_port(port);
  2233. sci_serial_out(port, SCSCR, s->hscif_tot); /* TE=0, RE=0, CKE1=0 */
  2234. reg = sci_getreg(port, SCFCR);
  2235. if (reg->size)
  2236. sci_serial_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
  2237. s->ops->clear_SCxSR(port,
  2238. SCxSR_RDxF_CLEAR(port) & SCxSR_ERROR_CLEAR(port) &
  2239. SCxSR_BREAK_CLEAR(port));
  2240. if (sci_getreg(port, SCLSR)->size) {
  2241. status = sci_serial_in(port, SCLSR);
  2242. status &= ~(SCLSR_TO | SCLSR_ORER);
  2243. sci_serial_out(port, SCLSR, status);
  2244. }
  2245. if (s->rx_trigger > 1) {
  2246. if (s->rx_fifo_timeout) {
  2247. s->ops->set_rtrg(port, 1);
  2248. timer_setup(&s->rx_fifo_timer, rx_fifo_timer_fn, 0);
  2249. } else {
  2250. if (s->type == PORT_SCIFA ||
  2251. s->type == PORT_SCIFB)
  2252. s->ops->set_rtrg(port, 1);
  2253. else
  2254. s->ops->set_rtrg(port, s->rx_trigger);
  2255. }
  2256. }
  2257. }
  2258. static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
  2259. const struct ktermios *old)
  2260. {
  2261. unsigned int baud, smr_val = SCSMR_ASYNC, scr_val = 0, i, bits;
  2262. unsigned int brr = 255, cks = 0, srr = 15, dl = 0, sccks = 0;
  2263. unsigned int brr1 = 255, cks1 = 0, srr1 = 15, dl1 = 0;
  2264. struct sci_port *s = to_sci_port(port);
  2265. const struct plat_sci_reg *reg;
  2266. int min_err = INT_MAX, err;
  2267. unsigned long max_freq = 0;
  2268. int best_clk = -1;
  2269. unsigned long flags;
  2270. if ((termios->c_cflag & CSIZE) == CS7) {
  2271. smr_val |= SCSMR_CHR;
  2272. } else {
  2273. termios->c_cflag &= ~CSIZE;
  2274. termios->c_cflag |= CS8;
  2275. }
  2276. if (termios->c_cflag & PARENB)
  2277. smr_val |= SCSMR_PE;
  2278. if (termios->c_cflag & PARODD)
  2279. smr_val |= SCSMR_PE | SCSMR_ODD;
  2280. if (termios->c_cflag & CSTOPB)
  2281. smr_val |= SCSMR_STOP;
  2282. /*
  2283. * earlyprintk comes here early on with port->uartclk set to zero.
  2284. * the clock framework is not up and running at this point so here
  2285. * we assume that 115200 is the maximum baud rate. please note that
  2286. * the baud rate is not programmed during earlyprintk - it is assumed
  2287. * that the previous boot loader has enabled required clocks and
  2288. * setup the baud rate generator hardware for us already.
  2289. */
  2290. if (!port->uartclk) {
  2291. baud = uart_get_baud_rate(port, termios, old, 0, 115200);
  2292. goto done;
  2293. }
  2294. for (i = 0; i < SCI_NUM_CLKS; i++)
  2295. max_freq = max(max_freq, s->clk_rates[i]);
  2296. baud = uart_get_baud_rate(port, termios, old, 0, max_freq / min_sr(s));
  2297. if (!baud)
  2298. goto done;
  2299. /*
  2300. * There can be multiple sources for the sampling clock. Find the one
  2301. * that gives us the smallest deviation from the desired baud rate.
  2302. */
  2303. /* Optional Undivided External Clock */
  2304. if (s->clk_rates[SCI_SCK] && s->type != PORT_SCIFA &&
  2305. s->type != PORT_SCIFB) {
  2306. err = sci_sck_calc(s, baud, &srr1);
  2307. if (abs(err) < abs(min_err)) {
  2308. best_clk = SCI_SCK;
  2309. scr_val = SCSCR_CKE1;
  2310. sccks = SCCKS_CKS;
  2311. min_err = err;
  2312. srr = srr1;
  2313. if (!err)
  2314. goto done;
  2315. }
  2316. }
  2317. /* Optional BRG Frequency Divided External Clock */
  2318. if (s->clk_rates[SCI_SCIF_CLK] && sci_getreg(port, SCDL)->size) {
  2319. err = sci_brg_calc(s, baud, s->clk_rates[SCI_SCIF_CLK], &dl1,
  2320. &srr1);
  2321. if (abs(err) < abs(min_err)) {
  2322. best_clk = SCI_SCIF_CLK;
  2323. scr_val = SCSCR_CKE1;
  2324. sccks = 0;
  2325. min_err = err;
  2326. dl = dl1;
  2327. srr = srr1;
  2328. if (!err)
  2329. goto done;
  2330. }
  2331. }
  2332. /* Optional BRG Frequency Divided Internal Clock */
  2333. if (s->clk_rates[SCI_BRG_INT] && sci_getreg(port, SCDL)->size) {
  2334. err = sci_brg_calc(s, baud, s->clk_rates[SCI_BRG_INT], &dl1,
  2335. &srr1);
  2336. if (abs(err) < abs(min_err)) {
  2337. best_clk = SCI_BRG_INT;
  2338. scr_val = SCSCR_CKE1;
  2339. sccks = SCCKS_XIN;
  2340. min_err = err;
  2341. dl = dl1;
  2342. srr = srr1;
  2343. if (!min_err)
  2344. goto done;
  2345. }
  2346. }
  2347. /* Divided Functional Clock using standard Bit Rate Register */
  2348. err = sci_scbrr_calc(s, baud, &brr1, &srr1, &cks1);
  2349. if (abs(err) < abs(min_err)) {
  2350. best_clk = SCI_FCK;
  2351. scr_val = 0;
  2352. min_err = err;
  2353. brr = brr1;
  2354. srr = srr1;
  2355. cks = cks1;
  2356. }
  2357. done:
  2358. if (best_clk >= 0)
  2359. dev_dbg(port->dev, "Using clk %pC for %u%+d bps\n",
  2360. s->clks[best_clk], baud, min_err);
  2361. sci_port_enable(s);
  2362. /*
  2363. * Program the optional External Baud Rate Generator (BRG) first.
  2364. * It controls the mux to select (H)SCK or frequency divided clock.
  2365. */
  2366. if (best_clk >= 0 && sci_getreg(port, SCCKS)->size) {
  2367. sci_serial_out(port, SCDL, dl);
  2368. sci_serial_out(port, SCCKS, sccks);
  2369. }
  2370. uart_port_lock_irqsave(port, &flags);
  2371. sci_reset(port);
  2372. uart_update_timeout(port, termios->c_cflag, baud);
  2373. /* byte size and parity */
  2374. bits = tty_get_frame_size(termios->c_cflag);
  2375. if (sci_getreg(port, SEMR)->size)
  2376. sci_serial_out(port, SEMR, 0);
  2377. if (best_clk >= 0) {
  2378. if (s->type == PORT_SCIFA || s->type == PORT_SCIFB)
  2379. switch (srr + 1) {
  2380. case 5: smr_val |= SCSMR_SRC_5; break;
  2381. case 7: smr_val |= SCSMR_SRC_7; break;
  2382. case 11: smr_val |= SCSMR_SRC_11; break;
  2383. case 13: smr_val |= SCSMR_SRC_13; break;
  2384. case 16: smr_val |= SCSMR_SRC_16; break;
  2385. case 17: smr_val |= SCSMR_SRC_17; break;
  2386. case 19: smr_val |= SCSMR_SRC_19; break;
  2387. case 27: smr_val |= SCSMR_SRC_27; break;
  2388. }
  2389. smr_val |= cks;
  2390. sci_serial_out(port, SCSCR, scr_val | s->hscif_tot);
  2391. sci_serial_out(port, SCSMR, smr_val);
  2392. sci_serial_out(port, SCBRR, brr);
  2393. if (sci_getreg(port, HSSRR)->size) {
  2394. unsigned int hssrr = srr | HSCIF_SRE;
  2395. /* Calculate deviation from intended rate at the
  2396. * center of the last stop bit in sampling clocks.
  2397. */
  2398. int last_stop = bits * 2 - 1;
  2399. int deviation = DIV_ROUND_CLOSEST(min_err * last_stop *
  2400. (int)(srr + 1),
  2401. 2 * (int)baud);
  2402. if (abs(deviation) >= 2) {
  2403. /* At least two sampling clocks off at the
  2404. * last stop bit; we can increase the error
  2405. * margin by shifting the sampling point.
  2406. */
  2407. int shift = clamp(deviation / 2, -8, 7);
  2408. hssrr |= (shift << HSCIF_SRHP_SHIFT) &
  2409. HSCIF_SRHP_MASK;
  2410. hssrr |= HSCIF_SRDE;
  2411. }
  2412. sci_serial_out(port, HSSRR, hssrr);
  2413. }
  2414. /* Wait one bit interval */
  2415. udelay((1000000 + (baud - 1)) / baud);
  2416. } else {
  2417. /* Don't touch the bit rate configuration */
  2418. scr_val = s->cfg->scscr & (SCSCR_CKE1 | SCSCR_CKE0);
  2419. smr_val |= sci_serial_in(port, SCSMR) &
  2420. (SCSMR_CKEDG | SCSMR_SRC_MASK | SCSMR_CKS);
  2421. sci_serial_out(port, SCSCR, scr_val | s->hscif_tot);
  2422. sci_serial_out(port, SCSMR, smr_val);
  2423. }
  2424. sci_init_pins(port, termios->c_cflag);
  2425. port->status &= ~UPSTAT_AUTOCTS;
  2426. s->autorts = false;
  2427. reg = sci_getreg(port, SCFCR);
  2428. if (reg->size) {
  2429. unsigned short ctrl = sci_serial_in(port, SCFCR);
  2430. if ((port->flags & UPF_HARD_FLOW) &&
  2431. (termios->c_cflag & CRTSCTS)) {
  2432. /* There is no CTS interrupt to restart the hardware */
  2433. port->status |= UPSTAT_AUTOCTS;
  2434. /* MCE is enabled when RTS is raised */
  2435. s->autorts = true;
  2436. }
  2437. /*
  2438. * As we've done a sci_reset() above, ensure we don't
  2439. * interfere with the FIFOs while toggling MCE. As the
  2440. * reset values could still be set, simply mask them out.
  2441. */
  2442. ctrl &= ~(SCFCR_RFRST | SCFCR_TFRST);
  2443. sci_serial_out(port, SCFCR, ctrl);
  2444. }
  2445. if (port->flags & UPF_HARD_FLOW) {
  2446. /* Refresh (Auto) RTS */
  2447. sci_set_mctrl(port, port->mctrl);
  2448. }
  2449. /*
  2450. * For SCI, TE (transmit enable) must be set after setting TIE
  2451. * (transmit interrupt enable) or in the same instruction to
  2452. * start the transmitting process. So skip setting TE here for SCI.
  2453. */
  2454. if (s->type != PORT_SCI)
  2455. scr_val |= SCSCR_TE;
  2456. scr_val |= SCSCR_RE | (s->cfg->scscr & ~(SCSCR_CKE1 | SCSCR_CKE0));
  2457. sci_serial_out(port, SCSCR, scr_val | s->hscif_tot);
  2458. if ((srr + 1 == 5) &&
  2459. (s->type == PORT_SCIFA || s->type == PORT_SCIFB)) {
  2460. /*
  2461. * In asynchronous mode, when the sampling rate is 1/5, first
  2462. * received data may become invalid on some SCIFA and SCIFB.
  2463. * To avoid this problem wait more than 1 serial data time (1
  2464. * bit time x serial data number) after setting SCSCR.RE = 1.
  2465. */
  2466. udelay(DIV_ROUND_UP(10 * 1000000, baud));
  2467. }
  2468. /* Calculate delay for 2 DMA buffers (4 FIFO). */
  2469. s->rx_frame = (10000 * bits) / (baud / 100);
  2470. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  2471. s->rx_timeout = s->buf_len_rx * 2 * s->rx_frame;
  2472. #endif
  2473. if ((termios->c_cflag & CREAD) != 0)
  2474. sci_start_rx(port);
  2475. uart_port_unlock_irqrestore(port, flags);
  2476. sci_port_disable(s);
  2477. if (UART_ENABLE_MS(port, termios->c_cflag))
  2478. sci_enable_ms(port);
  2479. }
  2480. void sci_pm(struct uart_port *port, unsigned int state,
  2481. unsigned int oldstate)
  2482. {
  2483. struct sci_port *sci_port = to_sci_port(port);
  2484. switch (state) {
  2485. case UART_PM_STATE_OFF:
  2486. sci_port_disable(sci_port);
  2487. break;
  2488. default:
  2489. sci_port_enable(sci_port);
  2490. break;
  2491. }
  2492. }
  2493. EXPORT_SYMBOL_NS_GPL(sci_pm, "SH_SCI");
  2494. static const char *sci_type(struct uart_port *port)
  2495. {
  2496. struct sci_port *s = to_sci_port(port);
  2497. switch (s->type) {
  2498. case PORT_IRDA:
  2499. return "irda";
  2500. case PORT_SCI:
  2501. return "sci";
  2502. case PORT_SCIF:
  2503. return "scif";
  2504. case PORT_SCIFA:
  2505. return "scifa";
  2506. case PORT_SCIFB:
  2507. return "scifb";
  2508. case PORT_HSCIF:
  2509. return "hscif";
  2510. }
  2511. return NULL;
  2512. }
  2513. static int sci_remap_port(struct uart_port *port)
  2514. {
  2515. struct sci_port *sport = to_sci_port(port);
  2516. /*
  2517. * Nothing to do if there's already an established membase.
  2518. */
  2519. if (port->membase)
  2520. return 0;
  2521. if (port->dev->of_node || (port->flags & UPF_IOREMAP)) {
  2522. port->membase = ioremap(port->mapbase, sport->reg_size);
  2523. if (unlikely(!port->membase)) {
  2524. dev_err(port->dev, "can't remap port#%d\n", port->line);
  2525. return -ENXIO;
  2526. }
  2527. } else {
  2528. /*
  2529. * For the simple (and majority of) cases where we don't
  2530. * need to do any remapping, just cast the cookie
  2531. * directly.
  2532. */
  2533. port->membase = (void __iomem *)(uintptr_t)port->mapbase;
  2534. }
  2535. return 0;
  2536. }
  2537. void sci_release_port(struct uart_port *port)
  2538. {
  2539. struct sci_port *sport = to_sci_port(port);
  2540. if (port->dev->of_node || (port->flags & UPF_IOREMAP)) {
  2541. iounmap(port->membase);
  2542. port->membase = NULL;
  2543. }
  2544. release_mem_region(port->mapbase, sport->reg_size);
  2545. }
  2546. EXPORT_SYMBOL_NS_GPL(sci_release_port, "SH_SCI");
  2547. int sci_request_port(struct uart_port *port)
  2548. {
  2549. struct resource *res;
  2550. struct sci_port *sport = to_sci_port(port);
  2551. int ret;
  2552. res = request_mem_region(port->mapbase, sport->reg_size,
  2553. dev_name(port->dev));
  2554. if (unlikely(res == NULL)) {
  2555. dev_err(port->dev, "request_mem_region failed.");
  2556. return -EBUSY;
  2557. }
  2558. ret = sci_remap_port(port);
  2559. if (unlikely(ret != 0)) {
  2560. release_resource(res);
  2561. return ret;
  2562. }
  2563. return 0;
  2564. }
  2565. EXPORT_SYMBOL_NS_GPL(sci_request_port, "SH_SCI");
  2566. void sci_config_port(struct uart_port *port, int flags)
  2567. {
  2568. if (flags & UART_CONFIG_TYPE) {
  2569. struct sci_port *sport = to_sci_port(port);
  2570. port->type = SCI_PUBLIC_PORT_ID(sport->type);
  2571. sci_request_port(port);
  2572. }
  2573. }
  2574. EXPORT_SYMBOL_NS_GPL(sci_config_port, "SH_SCI");
  2575. int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
  2576. {
  2577. if (ser->baud_base < 2400)
  2578. /* No paper tape reader for Mitch.. */
  2579. return -EINVAL;
  2580. return 0;
  2581. }
  2582. EXPORT_SYMBOL_NS_GPL(sci_verify_port, "SH_SCI");
  2583. static void sci_prepare_console_write(struct uart_port *port, u32 ctrl)
  2584. {
  2585. struct sci_port *s = to_sci_port(port);
  2586. u32 ctrl_temp =
  2587. s->params->param_bits->rxtx_enable |
  2588. (s->cfg->scscr & ~(SCSCR_CKE1 | SCSCR_CKE0)) |
  2589. (ctrl & (SCSCR_CKE1 | SCSCR_CKE0)) |
  2590. s->hscif_tot;
  2591. sci_serial_out(port, SCSCR, ctrl_temp);
  2592. }
  2593. static void sci_console_save(struct uart_port *port)
  2594. {
  2595. struct sci_port *s = to_sci_port(port);
  2596. struct sci_suspend_regs *regs = s->suspend_regs;
  2597. if (sci_getreg(port, SCDL)->size)
  2598. regs->scdl = sci_serial_in(port, SCDL);
  2599. if (sci_getreg(port, SCCKS)->size)
  2600. regs->sccks = sci_serial_in(port, SCCKS);
  2601. if (sci_getreg(port, SCSMR)->size)
  2602. regs->scsmr = sci_serial_in(port, SCSMR);
  2603. if (sci_getreg(port, SCSCR)->size)
  2604. regs->scscr = sci_serial_in(port, SCSCR);
  2605. if (sci_getreg(port, SCFCR)->size)
  2606. regs->scfcr = sci_serial_in(port, SCFCR);
  2607. if (sci_getreg(port, SCSPTR)->size)
  2608. regs->scsptr = sci_serial_in(port, SCSPTR);
  2609. if (sci_getreg(port, SCBRR)->size)
  2610. regs->scbrr = sci_serial_in(port, SCBRR);
  2611. if (sci_getreg(port, HSSRR)->size)
  2612. regs->hssrr = sci_serial_in(port, HSSRR);
  2613. if (sci_getreg(port, SCPCR)->size)
  2614. regs->scpcr = sci_serial_in(port, SCPCR);
  2615. if (sci_getreg(port, SCPDR)->size)
  2616. regs->scpdr = sci_serial_in(port, SCPDR);
  2617. if (sci_getreg(port, SEMR)->size)
  2618. regs->semr = sci_serial_in(port, SEMR);
  2619. }
  2620. static void sci_console_restore(struct uart_port *port)
  2621. {
  2622. struct sci_port *s = to_sci_port(port);
  2623. struct sci_suspend_regs *regs = s->suspend_regs;
  2624. if (sci_getreg(port, SCDL)->size)
  2625. sci_serial_out(port, SCDL, regs->scdl);
  2626. if (sci_getreg(port, SCCKS)->size)
  2627. sci_serial_out(port, SCCKS, regs->sccks);
  2628. if (sci_getreg(port, SCSMR)->size)
  2629. sci_serial_out(port, SCSMR, regs->scsmr);
  2630. if (sci_getreg(port, SCSCR)->size)
  2631. sci_serial_out(port, SCSCR, regs->scscr);
  2632. if (sci_getreg(port, SCFCR)->size)
  2633. sci_serial_out(port, SCFCR, regs->scfcr);
  2634. if (sci_getreg(port, SCSPTR)->size)
  2635. sci_serial_out(port, SCSPTR, regs->scsptr);
  2636. if (sci_getreg(port, SCBRR)->size)
  2637. sci_serial_out(port, SCBRR, regs->scbrr);
  2638. if (sci_getreg(port, HSSRR)->size)
  2639. sci_serial_out(port, HSSRR, regs->hssrr);
  2640. if (sci_getreg(port, SCPCR)->size)
  2641. sci_serial_out(port, SCPCR, regs->scpcr);
  2642. if (sci_getreg(port, SCPDR)->size)
  2643. sci_serial_out(port, SCPDR, regs->scpdr);
  2644. if (sci_getreg(port, SEMR)->size)
  2645. sci_serial_out(port, SEMR, regs->semr);
  2646. }
  2647. static const struct uart_ops sci_uart_ops = {
  2648. .tx_empty = sci_tx_empty,
  2649. .set_mctrl = sci_set_mctrl,
  2650. .get_mctrl = sci_get_mctrl,
  2651. .start_tx = sci_start_tx,
  2652. .stop_tx = sci_stop_tx,
  2653. .stop_rx = sci_stop_rx,
  2654. .enable_ms = sci_enable_ms,
  2655. .break_ctl = sci_break_ctl,
  2656. .startup = sci_startup,
  2657. .shutdown = sci_shutdown,
  2658. .flush_buffer = sci_flush_buffer,
  2659. .set_termios = sci_set_termios,
  2660. .pm = sci_pm,
  2661. .type = sci_type,
  2662. .release_port = sci_release_port,
  2663. .request_port = sci_request_port,
  2664. .config_port = sci_config_port,
  2665. .verify_port = sci_verify_port,
  2666. #ifdef CONFIG_CONSOLE_POLL
  2667. .poll_get_char = sci_poll_get_char,
  2668. .poll_put_char = sci_poll_put_char,
  2669. #endif
  2670. };
  2671. static const struct sci_port_ops sci_port_ops = {
  2672. .read_reg = sci_serial_in,
  2673. .write_reg = sci_serial_out,
  2674. .clear_SCxSR = sci_clear_SCxSR,
  2675. .transmit_chars = sci_transmit_chars,
  2676. .receive_chars = sci_receive_chars,
  2677. #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || \
  2678. defined(CONFIG_SERIAL_SH_SCI_EARLYCON)
  2679. .poll_put_char = sci_poll_put_char,
  2680. #endif
  2681. .set_rtrg = scif_set_rtrg,
  2682. .rtrg_enabled = scif_rtrg_enabled,
  2683. .shutdown_complete = sci_shutdown_complete,
  2684. .prepare_console_write = sci_prepare_console_write,
  2685. .console_save = sci_console_save,
  2686. .console_restore = sci_console_restore,
  2687. .suspend_regs_size = sci_suspend_regs_size,
  2688. };
  2689. static int sci_init_clocks(struct sci_port *sci_port, struct device *dev)
  2690. {
  2691. const char *clk_names[] = {
  2692. [SCI_FCK] = "fck",
  2693. [SCI_SCK] = "sck",
  2694. [SCI_BRG_INT] = "brg_int",
  2695. [SCI_SCIF_CLK] = "scif_clk",
  2696. [SCI_FCK_DIV4] = "tclk_div4",
  2697. [SCI_FCK_DIV16] = "tclk_div16",
  2698. [SCI_FCK_DIV64] = "tclk_div64",
  2699. };
  2700. struct clk *clk;
  2701. unsigned int i;
  2702. if (sci_port->type == PORT_HSCIF) {
  2703. clk_names[SCI_SCK] = "hsck";
  2704. } else if (sci_port->type == RSCI_PORT_SCIF16) {
  2705. clk_names[SCI_FCK] = "operation";
  2706. clk_names[SCI_BRG_INT] = "bus";
  2707. } else if (sci_port->type == RSCI_PORT_SCIF32) {
  2708. clk_names[SCI_FCK] = "tclk";
  2709. clk_names[SCI_BRG_INT] = "pclk";
  2710. }
  2711. for (i = 0; i < SCI_NUM_CLKS; i++) {
  2712. const char *name = clk_names[i];
  2713. clk = devm_clk_get_optional(dev, name);
  2714. if (IS_ERR(clk))
  2715. return PTR_ERR(clk);
  2716. if (!clk && sci_port->type == RSCI_PORT_SCIF16 &&
  2717. (i == SCI_FCK || i == SCI_BRG_INT))
  2718. return dev_err_probe(dev, -ENODEV, "failed to get %s\n", name);
  2719. if (!clk && sci_port->type == RSCI_PORT_SCIF32 &&
  2720. (i != SCI_SCK && i != SCI_SCIF_CLK))
  2721. return dev_err_probe(dev, -ENODEV, "failed to get %s\n", name);
  2722. if (!clk && i == SCI_FCK) {
  2723. /*
  2724. * Not all SH platforms declare a clock lookup entry
  2725. * for SCI devices, in which case we need to get the
  2726. * global "peripheral_clk" clock.
  2727. */
  2728. clk = devm_clk_get(dev, "peripheral_clk");
  2729. if (IS_ERR(clk))
  2730. return dev_err_probe(dev, PTR_ERR(clk), "failed to get %s\n", name);
  2731. }
  2732. if (!clk)
  2733. dev_dbg(dev, "failed to get %s\n", name);
  2734. else
  2735. dev_dbg(dev, "clk %s is %pC rate %lu\n", name, clk, clk_get_rate(clk));
  2736. sci_port->clks[i] = clk;
  2737. }
  2738. return 0;
  2739. }
  2740. static const struct sci_port_params *
  2741. sci_probe_regmap(const struct plat_sci_port *cfg, struct sci_port *sci_port)
  2742. {
  2743. unsigned int regtype;
  2744. sci_port->ops = &sci_port_ops;
  2745. sci_port->port.ops = &sci_uart_ops;
  2746. if (cfg->regtype != SCIx_PROBE_REGTYPE)
  2747. return &sci_port_params[cfg->regtype];
  2748. switch (cfg->type) {
  2749. case PORT_SCI:
  2750. regtype = SCIx_SCI_REGTYPE;
  2751. break;
  2752. case PORT_IRDA:
  2753. regtype = SCIx_IRDA_REGTYPE;
  2754. break;
  2755. case PORT_SCIFA:
  2756. regtype = SCIx_SCIFA_REGTYPE;
  2757. break;
  2758. case PORT_SCIFB:
  2759. regtype = SCIx_SCIFB_REGTYPE;
  2760. break;
  2761. case PORT_SCIF:
  2762. /*
  2763. * The SH-4 is a bit of a misnomer here, although that's
  2764. * where this particular port layout originated. This
  2765. * configuration (or some slight variation thereof)
  2766. * remains the dominant model for all SCIFs.
  2767. */
  2768. regtype = SCIx_SH4_SCIF_REGTYPE;
  2769. break;
  2770. case PORT_HSCIF:
  2771. regtype = SCIx_HSCIF_REGTYPE;
  2772. break;
  2773. default:
  2774. pr_err("Can't probe register map for given port\n");
  2775. return NULL;
  2776. }
  2777. return &sci_port_params[regtype];
  2778. }
  2779. static int sci_init_single(struct platform_device *dev,
  2780. struct sci_port *sci_port, unsigned int index,
  2781. const struct plat_sci_port *p, bool early)
  2782. {
  2783. struct uart_port *port = &sci_port->port;
  2784. const struct resource *res;
  2785. unsigned int i;
  2786. int ret;
  2787. sci_port->cfg = p;
  2788. sci_port->type = p->type;
  2789. sci_port->regtype = p->regtype;
  2790. port->iotype = UPIO_MEM;
  2791. port->line = index;
  2792. port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_SH_SCI_CONSOLE);
  2793. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  2794. if (res == NULL)
  2795. return -ENOMEM;
  2796. port->mapbase = res->start;
  2797. sci_port->reg_size = resource_size(res);
  2798. for (i = 0; i < ARRAY_SIZE(sci_port->irqs); ++i) {
  2799. if (i)
  2800. sci_port->irqs[i] = platform_get_irq_optional(dev, i);
  2801. else
  2802. sci_port->irqs[i] = platform_get_irq(dev, i);
  2803. }
  2804. /*
  2805. * The fourth interrupt on SCI and RSCI port is transmit end interrupt, so
  2806. * shuffle the interrupts.
  2807. */
  2808. if (p->type == PORT_SCI || sci_is_rsci_type(p->type))
  2809. swap(sci_port->irqs[SCIx_BRI_IRQ], sci_port->irqs[SCIx_TEI_IRQ]);
  2810. /* The SCI generates several interrupts. They can be muxed together or
  2811. * connected to different interrupt lines. In the muxed case only one
  2812. * interrupt resource is specified as there is only one interrupt ID.
  2813. * In the non-muxed case, up to 6 interrupt signals might be generated
  2814. * from the SCI, however those signals might have their own individual
  2815. * interrupt ID numbers, or muxed together with another interrupt.
  2816. */
  2817. if (sci_port->irqs[0] < 0)
  2818. return -ENXIO;
  2819. if (sci_port->irqs[1] < 0)
  2820. for (i = 1; i < ARRAY_SIZE(sci_port->irqs); i++)
  2821. sci_port->irqs[i] = sci_port->irqs[0];
  2822. switch (p->type) {
  2823. case PORT_SCIFB:
  2824. sci_port->rx_trigger = 48;
  2825. break;
  2826. case PORT_HSCIF:
  2827. sci_port->rx_trigger = 64;
  2828. break;
  2829. case PORT_SCIFA:
  2830. case RSCI_PORT_SCIF32:
  2831. sci_port->rx_trigger = 32;
  2832. break;
  2833. case PORT_SCIF:
  2834. if (p->regtype == SCIx_SH7705_SCIF_REGTYPE)
  2835. /* RX triggering not implemented for this IP */
  2836. sci_port->rx_trigger = 1;
  2837. else
  2838. sci_port->rx_trigger = 8;
  2839. break;
  2840. case RSCI_PORT_SCIF16:
  2841. sci_port->rx_trigger = 16;
  2842. break;
  2843. default:
  2844. sci_port->rx_trigger = 1;
  2845. break;
  2846. }
  2847. sci_port->rx_fifo_timeout = 0;
  2848. sci_port->hscif_tot = 0;
  2849. /* SCIFA on sh7723 and sh7724 need a custom sampling rate that doesn't
  2850. * match the SoC datasheet, this should be investigated. Let platform
  2851. * data override the sampling rate for now.
  2852. */
  2853. sci_port->sampling_rate_mask = p->sampling_rate
  2854. ? SCI_SR(p->sampling_rate)
  2855. : sci_port->params->sampling_rate_mask;
  2856. if (!early) {
  2857. ret = sci_init_clocks(sci_port, &dev->dev);
  2858. if (ret < 0)
  2859. return ret;
  2860. }
  2861. port->type = SCI_PUBLIC_PORT_ID(p->type);
  2862. port->flags = UPF_FIXED_PORT | UPF_BOOT_AUTOCONF | p->flags;
  2863. port->fifosize = sci_port->params->fifosize;
  2864. if (p->type == PORT_SCI && !dev->dev.of_node) {
  2865. if (sci_port->reg_size >= 0x20)
  2866. port->regshift = 2;
  2867. else
  2868. port->regshift = 1;
  2869. }
  2870. /*
  2871. * The UART port needs an IRQ value, so we peg this to the RX IRQ
  2872. * for the multi-IRQ ports, which is where we are primarily
  2873. * concerned with the shutdown path synchronization.
  2874. *
  2875. * For the muxed case there's nothing more to do.
  2876. */
  2877. port->irq = sci_port->irqs[SCIx_RXI_IRQ];
  2878. port->irqflags = 0;
  2879. return 0;
  2880. }
  2881. #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || \
  2882. defined(CONFIG_SERIAL_SH_SCI_EARLYCON)
  2883. static void serial_console_putchar(struct uart_port *port, unsigned char ch)
  2884. {
  2885. to_sci_port(port)->ops->poll_put_char(port, ch);
  2886. }
  2887. /*
  2888. * Print a string to the serial port trying not to disturb
  2889. * any possible real use of the port...
  2890. */
  2891. static void serial_console_write(struct console *co, const char *s,
  2892. unsigned count)
  2893. {
  2894. struct sci_port *sci_port = &sci_ports[co->index];
  2895. struct uart_port *port = &sci_port->port;
  2896. const struct sci_common_regs *regs = sci_port->params->common_regs;
  2897. unsigned int bits;
  2898. u32 ctrl;
  2899. unsigned long flags;
  2900. int locked = 1;
  2901. if (port->sysrq)
  2902. locked = 0;
  2903. else if (oops_in_progress)
  2904. locked = uart_port_trylock_irqsave(port, &flags);
  2905. else
  2906. uart_port_lock_irqsave(port, &flags);
  2907. /* first save SCSCR then disable interrupts, keep clock source */
  2908. ctrl = sci_port->ops->read_reg(port, regs->control);
  2909. sci_port->ops->prepare_console_write(port, ctrl);
  2910. uart_console_write(port, s, count, serial_console_putchar);
  2911. /* wait until fifo is empty and last bit has been transmitted */
  2912. bits = sci_port->params->param_bits->poll_sent_bits;
  2913. while ((sci_port->ops->read_reg(port, regs->status) & bits) != bits)
  2914. cpu_relax();
  2915. /* restore the SCSCR */
  2916. if (sci_port->ops->finish_console_write)
  2917. sci_port->ops->finish_console_write(port, ctrl);
  2918. else
  2919. sci_port->ops->write_reg(port, regs->control, ctrl);
  2920. if (locked)
  2921. uart_port_unlock_irqrestore(port, flags);
  2922. }
  2923. static int serial_console_setup(struct console *co, char *options)
  2924. {
  2925. struct sci_port *sci_port;
  2926. struct uart_port *port;
  2927. int baud = 115200;
  2928. int bits = 8;
  2929. int parity = 'n';
  2930. int flow = 'n';
  2931. int ret;
  2932. /*
  2933. * Refuse to handle any bogus ports.
  2934. */
  2935. if (co->index < 0 || co->index >= SCI_NPORTS)
  2936. return -ENODEV;
  2937. sci_port = &sci_ports[co->index];
  2938. port = &sci_port->port;
  2939. /*
  2940. * Refuse to handle uninitialized ports.
  2941. */
  2942. if (!port->ops)
  2943. return -ENODEV;
  2944. ret = sci_remap_port(port);
  2945. if (unlikely(ret != 0))
  2946. return ret;
  2947. if (options)
  2948. uart_parse_options(options, &baud, &parity, &bits, &flow);
  2949. return uart_set_options(port, co, baud, parity, bits, flow);
  2950. }
  2951. static struct console serial_console = {
  2952. .name = "ttySC",
  2953. .device = uart_console_device,
  2954. .write = serial_console_write,
  2955. .setup = serial_console_setup,
  2956. .flags = CON_PRINTBUFFER,
  2957. .index = -1,
  2958. .data = &sci_uart_driver,
  2959. };
  2960. #ifdef CONFIG_SUPERH
  2961. static char early_serial_buf[32];
  2962. static int early_serial_console_setup(struct console *co, char *options)
  2963. {
  2964. /*
  2965. * This early console is always registered using the earlyprintk=
  2966. * parameter, which does not call add_preferred_console(). Thus
  2967. * @options is always NULL and the options for this early console
  2968. * are passed using a custom buffer.
  2969. */
  2970. WARN_ON(options);
  2971. return serial_console_setup(co, early_serial_buf);
  2972. }
  2973. static struct console early_serial_console = {
  2974. .name = "early_ttySC",
  2975. .write = serial_console_write,
  2976. .setup = early_serial_console_setup,
  2977. .flags = CON_PRINTBUFFER,
  2978. .index = -1,
  2979. };
  2980. static int sci_probe_earlyprintk(struct platform_device *pdev)
  2981. {
  2982. const struct plat_sci_port *cfg = dev_get_platdata(&pdev->dev);
  2983. struct sci_port *sp = &sci_ports[pdev->id];
  2984. if (early_serial_console.data)
  2985. return -EEXIST;
  2986. early_serial_console.index = pdev->id;
  2987. sp->params = sci_probe_regmap(cfg, sp);
  2988. if (!sp->params)
  2989. return -ENODEV;
  2990. sci_init_single(pdev, sp, pdev->id, cfg, true);
  2991. if (!strstr(early_serial_buf, "keep"))
  2992. early_serial_console.flags |= CON_BOOT;
  2993. register_console(&early_serial_console);
  2994. return 0;
  2995. }
  2996. #endif
  2997. #define SCI_CONSOLE (&serial_console)
  2998. #else
  2999. static inline int sci_probe_earlyprintk(struct platform_device *pdev)
  3000. {
  3001. return -EINVAL;
  3002. }
  3003. #define SCI_CONSOLE NULL
  3004. #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE || CONFIG_SERIAL_SH_SCI_EARLYCON */
  3005. static const char banner[] __initconst = "SuperH (H)SCI(F) driver initialized";
  3006. static DEFINE_MUTEX(sci_uart_registration_lock);
  3007. static struct uart_driver sci_uart_driver = {
  3008. .owner = THIS_MODULE,
  3009. .driver_name = "sci",
  3010. .dev_name = "ttySC",
  3011. .major = SCI_MAJOR,
  3012. .minor = SCI_MINOR_START,
  3013. .nr = SCI_NPORTS,
  3014. .cons = SCI_CONSOLE,
  3015. };
  3016. static void sci_remove(struct platform_device *dev)
  3017. {
  3018. struct sci_port *s = platform_get_drvdata(dev);
  3019. sci_ports_in_use &= ~BIT(s->port.line);
  3020. uart_remove_one_port(&sci_uart_driver, &s->port);
  3021. if (s->port.fifosize > 1) {
  3022. device_remove_file(&dev->dev, &dev_attr_rx_fifo_trigger);
  3023. device_remove_file(&dev->dev, &dev_attr_rx_fifo_timeout);
  3024. }
  3025. }
  3026. static const struct sci_of_data of_sci_scif_sh2 = {
  3027. .type = PORT_SCIF,
  3028. .regtype = SCIx_SH2_SCIF_FIFODATA_REGTYPE,
  3029. .ops = &sci_port_ops,
  3030. .uart_ops = &sci_uart_ops,
  3031. .params = &sci_port_params[SCIx_SH2_SCIF_FIFODATA_REGTYPE],
  3032. };
  3033. static const struct sci_of_data of_sci_scif_rz_scifa = {
  3034. .type = PORT_SCIF,
  3035. .regtype = SCIx_RZ_SCIFA_REGTYPE,
  3036. .ops = &sci_port_ops,
  3037. .uart_ops = &sci_uart_ops,
  3038. .params = &sci_port_params[SCIx_RZ_SCIFA_REGTYPE],
  3039. };
  3040. static const struct sci_of_data of_sci_scif_rzv2h = {
  3041. .type = PORT_SCIF,
  3042. .regtype = SCIx_RZV2H_SCIF_REGTYPE,
  3043. .ops = &sci_port_ops,
  3044. .uart_ops = &sci_uart_ops,
  3045. .params = &sci_port_params[SCIx_RZV2H_SCIF_REGTYPE],
  3046. };
  3047. static const struct sci_of_data of_sci_rcar_scif = {
  3048. .type = PORT_SCIF,
  3049. .regtype = SCIx_SH4_SCIF_BRG_REGTYPE,
  3050. .ops = &sci_port_ops,
  3051. .uart_ops = &sci_uart_ops,
  3052. .params = &sci_port_params[SCIx_SH4_SCIF_BRG_REGTYPE],
  3053. };
  3054. static const struct sci_of_data of_sci_scif_sh4 = {
  3055. .type = PORT_SCIF,
  3056. .regtype = SCIx_SH4_SCIF_REGTYPE,
  3057. .ops = &sci_port_ops,
  3058. .uart_ops = &sci_uart_ops,
  3059. .params = &sci_port_params[SCIx_SH4_SCIF_REGTYPE],
  3060. };
  3061. static const struct sci_of_data of_sci_scifa = {
  3062. .type = PORT_SCIFA,
  3063. .regtype = SCIx_SCIFA_REGTYPE,
  3064. .ops = &sci_port_ops,
  3065. .uart_ops = &sci_uart_ops,
  3066. .params = &sci_port_params[SCIx_SCIFA_REGTYPE],
  3067. };
  3068. static const struct sci_of_data of_sci_scifb = {
  3069. .type = PORT_SCIFB,
  3070. .regtype = SCIx_SCIFB_REGTYPE,
  3071. .ops = &sci_port_ops,
  3072. .uart_ops = &sci_uart_ops,
  3073. .params = &sci_port_params[SCIx_SCIFB_REGTYPE],
  3074. };
  3075. static const struct sci_of_data of_sci_hscif = {
  3076. .type = PORT_HSCIF,
  3077. .regtype = SCIx_HSCIF_REGTYPE,
  3078. .ops = &sci_port_ops,
  3079. .uart_ops = &sci_uart_ops,
  3080. .params = &sci_port_params[SCIx_HSCIF_REGTYPE],
  3081. };
  3082. static const struct sci_of_data of_sci_sci = {
  3083. .type = PORT_SCI,
  3084. .regtype = SCIx_SCI_REGTYPE,
  3085. .ops = &sci_port_ops,
  3086. .uart_ops = &sci_uart_ops,
  3087. .params = &sci_port_params[SCIx_SCI_REGTYPE],
  3088. };
  3089. static const struct of_device_id of_sci_match[] __maybe_unused = {
  3090. /* SoC-specific types */
  3091. {
  3092. .compatible = "renesas,scif-r7s72100",
  3093. .data = &of_sci_scif_sh2,
  3094. },
  3095. {
  3096. .compatible = "renesas,scif-r7s9210",
  3097. .data = &of_sci_scif_rz_scifa,
  3098. },
  3099. {
  3100. .compatible = "renesas,scif-r9a07g044",
  3101. .data = &of_sci_scif_rz_scifa,
  3102. },
  3103. {
  3104. .compatible = "renesas,scif-r9a09g057",
  3105. .data = &of_sci_scif_rzv2h,
  3106. },
  3107. #ifdef CONFIG_SERIAL_RSCI
  3108. {
  3109. .compatible = "renesas,r9a09g047-rsci",
  3110. .data = &of_rsci_rzg3e_data,
  3111. },
  3112. {
  3113. .compatible = "renesas,r9a09g077-rsci",
  3114. .data = &of_rsci_rzt2h_data,
  3115. },
  3116. #endif /* CONFIG_SERIAL_RSCI */
  3117. /* Family-specific types */
  3118. {
  3119. .compatible = "renesas,rcar-gen1-scif",
  3120. .data = &of_sci_rcar_scif,
  3121. }, {
  3122. .compatible = "renesas,rcar-gen2-scif",
  3123. .data = &of_sci_rcar_scif,
  3124. }, {
  3125. .compatible = "renesas,rcar-gen3-scif",
  3126. .data = &of_sci_rcar_scif
  3127. }, {
  3128. .compatible = "renesas,rcar-gen4-scif",
  3129. .data = &of_sci_rcar_scif
  3130. }, {
  3131. .compatible = "renesas,rcar-gen5-scif",
  3132. .data = &of_sci_rcar_scif
  3133. },
  3134. /* Generic types */
  3135. {
  3136. .compatible = "renesas,scif",
  3137. .data = &of_sci_scif_sh4,
  3138. }, {
  3139. .compatible = "renesas,scifa",
  3140. .data = &of_sci_scifa,
  3141. }, {
  3142. .compatible = "renesas,scifb",
  3143. .data = &of_sci_scifb,
  3144. }, {
  3145. .compatible = "renesas,hscif",
  3146. .data = &of_sci_hscif,
  3147. }, {
  3148. .compatible = "renesas,sci",
  3149. .data = &of_sci_sci,
  3150. }, {
  3151. /* Terminator */
  3152. },
  3153. };
  3154. MODULE_DEVICE_TABLE(of, of_sci_match);
  3155. static void sci_reset_control_assert(void *data)
  3156. {
  3157. reset_control_assert(data);
  3158. }
  3159. static struct plat_sci_port *sci_parse_dt(struct platform_device *pdev,
  3160. unsigned int *dev_id)
  3161. {
  3162. struct device_node *np = pdev->dev.of_node;
  3163. struct reset_control *rstc;
  3164. struct plat_sci_port *p;
  3165. struct sci_port *sp;
  3166. const struct sci_of_data *data;
  3167. int id, ret;
  3168. if (!IS_ENABLED(CONFIG_OF) || !np)
  3169. return ERR_PTR(-EINVAL);
  3170. data = of_device_get_match_data(&pdev->dev);
  3171. rstc = devm_reset_control_array_get_optional_exclusive(&pdev->dev);
  3172. if (IS_ERR(rstc))
  3173. return ERR_PTR(dev_err_probe(&pdev->dev, PTR_ERR(rstc),
  3174. "failed to get reset ctrl\n"));
  3175. ret = reset_control_deassert(rstc);
  3176. if (ret) {
  3177. dev_err(&pdev->dev, "failed to deassert reset %d\n", ret);
  3178. return ERR_PTR(ret);
  3179. }
  3180. ret = devm_add_action_or_reset(&pdev->dev, sci_reset_control_assert, rstc);
  3181. if (ret) {
  3182. dev_err(&pdev->dev, "failed to register assert devm action, %d\n",
  3183. ret);
  3184. return ERR_PTR(ret);
  3185. }
  3186. p = devm_kzalloc(&pdev->dev, sizeof(struct plat_sci_port), GFP_KERNEL);
  3187. if (!p)
  3188. return ERR_PTR(-ENOMEM);
  3189. /* Get the line number from the aliases node. */
  3190. id = of_alias_get_id(np, "serial");
  3191. if (id < 0 && ~sci_ports_in_use)
  3192. id = ffz(sci_ports_in_use);
  3193. if (id < 0) {
  3194. dev_err(&pdev->dev, "failed to get alias id (%d)\n", id);
  3195. return ERR_PTR(-EINVAL);
  3196. }
  3197. if (id >= ARRAY_SIZE(sci_ports)) {
  3198. dev_err(&pdev->dev, "serial%d out of range\n", id);
  3199. return ERR_PTR(-EINVAL);
  3200. }
  3201. sp = &sci_ports[id];
  3202. sp->rstc = rstc;
  3203. *dev_id = id;
  3204. p->type = data->type;
  3205. p->regtype = data->regtype;
  3206. sp->ops = data->ops;
  3207. sp->port.ops = data->uart_ops;
  3208. sp->params = data->params;
  3209. sp->has_rtscts = of_property_read_bool(np, "uart-has-rtscts");
  3210. return p;
  3211. }
  3212. static int sci_probe_single(struct platform_device *dev,
  3213. unsigned int index,
  3214. struct plat_sci_port *p,
  3215. struct sci_port *sciport,
  3216. struct resource *sci_res)
  3217. {
  3218. int ret;
  3219. /* Sanity check */
  3220. if (unlikely(index >= SCI_NPORTS)) {
  3221. dev_notice(&dev->dev, "Attempting to register port %d when only %d are available\n",
  3222. index+1, SCI_NPORTS);
  3223. dev_notice(&dev->dev, "Consider bumping CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
  3224. return -EINVAL;
  3225. }
  3226. BUILD_BUG_ON(SCI_NPORTS > sizeof(sci_ports_in_use) * 8);
  3227. if (sci_ports_in_use & BIT(index))
  3228. return -EBUSY;
  3229. mutex_lock(&sci_uart_registration_lock);
  3230. if (!sci_uart_driver.state) {
  3231. ret = uart_register_driver(&sci_uart_driver);
  3232. if (ret) {
  3233. mutex_unlock(&sci_uart_registration_lock);
  3234. return ret;
  3235. }
  3236. }
  3237. mutex_unlock(&sci_uart_registration_lock);
  3238. ret = sci_init_single(dev, sciport, index, p, false);
  3239. if (ret)
  3240. return ret;
  3241. sciport->port.dev = &dev->dev;
  3242. ret = devm_pm_runtime_enable(&dev->dev);
  3243. if (ret)
  3244. return ret;
  3245. sciport->gpios = mctrl_gpio_init(&sciport->port, 0);
  3246. if (IS_ERR(sciport->gpios))
  3247. return PTR_ERR(sciport->gpios);
  3248. if (sciport->has_rtscts) {
  3249. if (mctrl_gpio_to_gpiod(sciport->gpios, UART_GPIO_CTS) ||
  3250. mctrl_gpio_to_gpiod(sciport->gpios, UART_GPIO_RTS)) {
  3251. dev_err(&dev->dev, "Conflicting RTS/CTS config\n");
  3252. return -EINVAL;
  3253. }
  3254. sciport->port.flags |= UPF_HARD_FLOW;
  3255. }
  3256. if (sci_uart_earlycon && sci_ports[0].port.mapbase == sci_res->start) {
  3257. /*
  3258. * In case:
  3259. * - this is the earlycon port (mapped on index 0 in sci_ports[]) and
  3260. * - it now maps to an alias other than zero and
  3261. * - the earlycon is still alive (e.g., "earlycon keep_bootcon" is
  3262. * available in bootargs)
  3263. *
  3264. * we need to avoid disabling clocks and PM domains through the runtime
  3265. * PM APIs called in __device_attach(). For this, increment the runtime
  3266. * PM reference counter (the clocks and PM domains were already enabled
  3267. * by the bootloader). Otherwise the earlycon may access the HW when it
  3268. * has no clocks enabled leading to failures (infinite loop in
  3269. * sci_poll_put_char()).
  3270. */
  3271. pm_runtime_get_noresume(&dev->dev);
  3272. /*
  3273. * Skip cleanup the sci_port[0] in early_console_exit(), this
  3274. * port is the same as the earlycon one.
  3275. */
  3276. sci_uart_earlycon_dev_probing = true;
  3277. }
  3278. return uart_add_one_port(&sci_uart_driver, &sciport->port);
  3279. }
  3280. static int sci_probe(struct platform_device *dev)
  3281. {
  3282. struct plat_sci_port *p;
  3283. struct resource *res;
  3284. struct sci_port *sp;
  3285. unsigned int dev_id;
  3286. int ret;
  3287. /*
  3288. * If we've come here via earlyprintk initialization, head off to
  3289. * the special early probe. We don't have sufficient device state
  3290. * to make it beyond this yet.
  3291. */
  3292. #ifdef CONFIG_SUPERH
  3293. if (is_sh_early_platform_device(dev))
  3294. return sci_probe_earlyprintk(dev);
  3295. #endif
  3296. if (dev->dev.of_node) {
  3297. p = sci_parse_dt(dev, &dev_id);
  3298. if (IS_ERR(p))
  3299. return PTR_ERR(p);
  3300. sp = &sci_ports[dev_id];
  3301. } else {
  3302. p = dev->dev.platform_data;
  3303. if (p == NULL) {
  3304. dev_err(&dev->dev, "no platform data supplied\n");
  3305. return -EINVAL;
  3306. }
  3307. dev_id = dev->id;
  3308. sp = &sci_ports[dev_id];
  3309. sp->params = sci_probe_regmap(p, sp);
  3310. if (!sp->params)
  3311. return -ENODEV;
  3312. }
  3313. sp->suspend_regs = devm_kzalloc(&dev->dev,
  3314. sp->ops->suspend_regs_size(),
  3315. GFP_KERNEL);
  3316. if (!sp->suspend_regs)
  3317. return -ENOMEM;
  3318. /*
  3319. * In case:
  3320. * - the probed port alias is zero (as the one used by earlycon), and
  3321. * - the earlycon is still active (e.g., "earlycon keep_bootcon" in
  3322. * bootargs)
  3323. *
  3324. * defer the probe of this serial. This is a debug scenario and the user
  3325. * must be aware of it.
  3326. *
  3327. * Except when the probed port is the same as the earlycon port.
  3328. */
  3329. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  3330. if (!res)
  3331. return -ENODEV;
  3332. if (sci_uart_earlycon && sp == &sci_ports[0] && sp->port.mapbase != res->start)
  3333. return dev_err_probe(&dev->dev, -EBUSY, "sci_port[0] is used by earlycon!\n");
  3334. platform_set_drvdata(dev, sp);
  3335. ret = sci_probe_single(dev, dev_id, p, sp, res);
  3336. if (ret)
  3337. return ret;
  3338. if (sp->port.fifosize > 1) {
  3339. ret = device_create_file(&dev->dev, &dev_attr_rx_fifo_trigger);
  3340. if (ret)
  3341. return ret;
  3342. ret = device_create_file(&dev->dev, &dev_attr_rx_fifo_timeout);
  3343. if (ret) {
  3344. device_remove_file(&dev->dev, &dev_attr_rx_fifo_trigger);
  3345. return ret;
  3346. }
  3347. }
  3348. #ifdef CONFIG_SH_STANDARD_BIOS
  3349. sh_bios_gdb_detach();
  3350. #endif
  3351. sci_ports_in_use |= BIT(dev_id);
  3352. return 0;
  3353. }
  3354. static int sci_suspend(struct device *dev)
  3355. {
  3356. struct sci_port *sport = dev_get_drvdata(dev);
  3357. if (sport) {
  3358. uart_suspend_port(&sci_uart_driver, &sport->port);
  3359. if (!console_suspend_enabled && uart_console(&sport->port)) {
  3360. if (sport->ops->console_save)
  3361. sport->ops->console_save(&sport->port);
  3362. }
  3363. else
  3364. return reset_control_assert(sport->rstc);
  3365. }
  3366. return 0;
  3367. }
  3368. static int sci_resume(struct device *dev)
  3369. {
  3370. struct sci_port *sport = dev_get_drvdata(dev);
  3371. if (sport) {
  3372. if (!console_suspend_enabled && uart_console(&sport->port)) {
  3373. if (sport->ops->console_restore)
  3374. sport->ops->console_restore(&sport->port);
  3375. } else {
  3376. int ret = reset_control_deassert(sport->rstc);
  3377. if (ret)
  3378. return ret;
  3379. }
  3380. uart_resume_port(&sci_uart_driver, &sport->port);
  3381. }
  3382. return 0;
  3383. }
  3384. static DEFINE_SIMPLE_DEV_PM_OPS(sci_dev_pm_ops, sci_suspend, sci_resume);
  3385. static struct platform_driver sci_driver = {
  3386. .probe = sci_probe,
  3387. .remove = sci_remove,
  3388. .driver = {
  3389. .name = "sh-sci",
  3390. .pm = pm_sleep_ptr(&sci_dev_pm_ops),
  3391. .of_match_table = of_match_ptr(of_sci_match),
  3392. },
  3393. };
  3394. static int __init sci_init(void)
  3395. {
  3396. pr_info("%s\n", banner);
  3397. return platform_driver_register(&sci_driver);
  3398. }
  3399. static void __exit sci_exit(void)
  3400. {
  3401. platform_driver_unregister(&sci_driver);
  3402. if (sci_uart_driver.state)
  3403. uart_unregister_driver(&sci_uart_driver);
  3404. }
  3405. #if defined(CONFIG_SUPERH) && defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
  3406. sh_early_platform_init_buffer("earlyprintk", &sci_driver,
  3407. early_serial_buf, ARRAY_SIZE(early_serial_buf));
  3408. #endif
  3409. #ifdef CONFIG_SERIAL_SH_SCI_EARLYCON
  3410. static struct plat_sci_port port_cfg;
  3411. static int early_console_exit(struct console *co)
  3412. {
  3413. struct sci_port *sci_port = &sci_ports[0];
  3414. /*
  3415. * Clean the slot used by earlycon. A new SCI device might
  3416. * map to this slot.
  3417. */
  3418. if (!sci_uart_earlycon_dev_probing) {
  3419. memset(sci_port, 0, sizeof(*sci_port));
  3420. sci_uart_earlycon = false;
  3421. }
  3422. return 0;
  3423. }
  3424. int __init scix_early_console_setup(struct earlycon_device *device,
  3425. const struct sci_of_data *data)
  3426. {
  3427. const struct sci_common_regs *regs;
  3428. if (!device->port.membase)
  3429. return -ENODEV;
  3430. device->port.type = SCI_PUBLIC_PORT_ID(data->type);
  3431. sci_ports[0].port = device->port;
  3432. sci_ports[0].type = data->type;
  3433. sci_ports[0].regtype = data->regtype;
  3434. port_cfg.type = data->type;
  3435. port_cfg.regtype = data->regtype;
  3436. sci_ports[0].cfg = &port_cfg;
  3437. sci_ports[0].params = data->params;
  3438. sci_ports[0].ops = data->ops;
  3439. sci_ports[0].port.ops = data->uart_ops;
  3440. sci_uart_earlycon = true;
  3441. regs = sci_ports[0].params->common_regs;
  3442. port_cfg.scscr = sci_ports[0].ops->read_reg(&sci_ports[0].port, regs->control);
  3443. sci_ports[0].ops->write_reg(&sci_ports[0].port,
  3444. regs->control,
  3445. sci_ports[0].params->param_bits->rxtx_enable | port_cfg.scscr);
  3446. device->con->write = serial_console_write;
  3447. device->con->exit = early_console_exit;
  3448. return 0;
  3449. }
  3450. static int __init sci_early_console_setup(struct earlycon_device *device,
  3451. const char *opt)
  3452. {
  3453. return scix_early_console_setup(device, &of_sci_sci);
  3454. }
  3455. static int __init scif_early_console_setup(struct earlycon_device *device,
  3456. const char *opt)
  3457. {
  3458. return scix_early_console_setup(device, &of_sci_scif_sh4);
  3459. }
  3460. static int __init rzscifa_early_console_setup(struct earlycon_device *device,
  3461. const char *opt)
  3462. {
  3463. return scix_early_console_setup(device, &of_sci_scif_rz_scifa);
  3464. }
  3465. static int __init rzv2hscif_early_console_setup(struct earlycon_device *device,
  3466. const char *opt)
  3467. {
  3468. return scix_early_console_setup(device, &of_sci_scif_rzv2h);
  3469. }
  3470. static int __init scifa_early_console_setup(struct earlycon_device *device,
  3471. const char *opt)
  3472. {
  3473. return scix_early_console_setup(device, &of_sci_scifa);
  3474. }
  3475. static int __init scifb_early_console_setup(struct earlycon_device *device,
  3476. const char *opt)
  3477. {
  3478. return scix_early_console_setup(device, &of_sci_scifb);
  3479. }
  3480. static int __init hscif_early_console_setup(struct earlycon_device *device,
  3481. const char *opt)
  3482. {
  3483. return scix_early_console_setup(device, &of_sci_hscif);
  3484. }
  3485. OF_EARLYCON_DECLARE(sci, "renesas,sci", sci_early_console_setup);
  3486. OF_EARLYCON_DECLARE(scif, "renesas,scif", scif_early_console_setup);
  3487. OF_EARLYCON_DECLARE(scif, "renesas,scif-r7s9210", rzscifa_early_console_setup);
  3488. OF_EARLYCON_DECLARE(scif, "renesas,scif-r9a07g044", rzscifa_early_console_setup);
  3489. OF_EARLYCON_DECLARE(scif, "renesas,scif-r9a09g057", rzv2hscif_early_console_setup);
  3490. OF_EARLYCON_DECLARE(scifa, "renesas,scifa", scifa_early_console_setup);
  3491. OF_EARLYCON_DECLARE(scifb, "renesas,scifb", scifb_early_console_setup);
  3492. OF_EARLYCON_DECLARE(hscif, "renesas,hscif", hscif_early_console_setup);
  3493. #endif /* CONFIG_SERIAL_SH_SCI_EARLYCON */
  3494. module_init(sci_init);
  3495. module_exit(sci_exit);
  3496. MODULE_LICENSE("GPL");
  3497. MODULE_ALIAS("platform:sh-sci");
  3498. MODULE_AUTHOR("Paul Mundt");
  3499. MODULE_DESCRIPTION("SuperH (H)SCI(F) serial driver");