sh-sci.c 82 KB

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