sh-sci.c 62 KB

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