sh-sci.c 78 KB

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