sh-sci.c 56 KB

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