sh-sci.c 79 KB

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