sh-sci.c 84 KB

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