sh-sci.c 52 KB

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