sh-sci.c 79 KB

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