sh-sci.c 79 KB

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