sh-sci.c 60 KB

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