sh-sci.c 49 KB

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