sh-sci.c 93 KB

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