sh-sci.c 81 KB

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