sh-sci.c 59 KB

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