sh-sci.c 64 KB

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