sh-sci.c 49 KB

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