sh-sci.c 58 KB

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