sh-sci.c 79 KB

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