sh-sci.c 48 KB

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