sh-sci.c 53 KB

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