sh-sci.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  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. #ifdef CONFIG_SUPERH
  50. #include <asm/clock.h>
  51. #include <asm/sh_bios.h>
  52. #endif
  53. #include "sh-sci.h"
  54. struct sci_port {
  55. struct uart_port port;
  56. /* Port type */
  57. unsigned int type;
  58. /* Port IRQs: ERI, RXI, TXI, BRI (optional) */
  59. unsigned int irqs[SCIx_NR_IRQS];
  60. /* Port enable callback */
  61. void (*enable)(struct uart_port *port);
  62. /* Port disable callback */
  63. void (*disable)(struct uart_port *port);
  64. /* Break timer */
  65. struct timer_list break_timer;
  66. int break_flag;
  67. #ifdef CONFIG_HAVE_CLK
  68. /* Port clock */
  69. struct clk *clk;
  70. #endif
  71. struct list_head node;
  72. };
  73. struct sh_sci_priv {
  74. spinlock_t lock;
  75. struct list_head ports;
  76. #ifdef CONFIG_HAVE_CLK
  77. struct notifier_block clk_nb;
  78. #endif
  79. };
  80. /* Function prototypes */
  81. static void sci_stop_tx(struct uart_port *port);
  82. #define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
  83. static struct sci_port sci_ports[SCI_NPORTS];
  84. static struct uart_driver sci_uart_driver;
  85. static inline struct sci_port *
  86. to_sci_port(struct uart_port *uart)
  87. {
  88. return container_of(uart, struct sci_port, port);
  89. }
  90. #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
  91. #ifdef CONFIG_CONSOLE_POLL
  92. static inline void handle_error(struct uart_port *port)
  93. {
  94. /* Clear error flags */
  95. sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
  96. }
  97. static int sci_poll_get_char(struct uart_port *port)
  98. {
  99. unsigned short status;
  100. int c;
  101. do {
  102. status = sci_in(port, SCxSR);
  103. if (status & SCxSR_ERRORS(port)) {
  104. handle_error(port);
  105. continue;
  106. }
  107. } while (!(status & SCxSR_RDxF(port)));
  108. c = sci_in(port, SCxRDR);
  109. /* Dummy read */
  110. sci_in(port, SCxSR);
  111. sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  112. return c;
  113. }
  114. #endif
  115. static void sci_poll_put_char(struct uart_port *port, unsigned char c)
  116. {
  117. unsigned short status;
  118. do {
  119. status = sci_in(port, SCxSR);
  120. } while (!(status & SCxSR_TDxE(port)));
  121. sci_in(port, SCxSR); /* Dummy read */
  122. sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
  123. sci_out(port, SCxTDR, c);
  124. }
  125. #endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
  126. #if defined(__H8300S__)
  127. enum { sci_disable, sci_enable };
  128. static void h8300_sci_config(struct uart_port *port, unsigned int ctrl)
  129. {
  130. volatile unsigned char *mstpcrl = (volatile unsigned char *)MSTPCRL;
  131. int ch = (port->mapbase - SMR0) >> 3;
  132. unsigned char mask = 1 << (ch+1);
  133. if (ctrl == sci_disable)
  134. *mstpcrl |= mask;
  135. else
  136. *mstpcrl &= ~mask;
  137. }
  138. static inline void h8300_sci_enable(struct uart_port *port)
  139. {
  140. h8300_sci_config(port, sci_enable);
  141. }
  142. static inline void h8300_sci_disable(struct uart_port *port)
  143. {
  144. h8300_sci_config(port, sci_disable);
  145. }
  146. #endif
  147. #if defined(__H8300H__) || defined(__H8300S__)
  148. static void sci_init_pins(struct uart_port *port, unsigned int cflag)
  149. {
  150. int ch = (port->mapbase - SMR0) >> 3;
  151. /* set DDR regs */
  152. H8300_GPIO_DDR(h8300_sci_pins[ch].port,
  153. h8300_sci_pins[ch].rx,
  154. H8300_GPIO_INPUT);
  155. H8300_GPIO_DDR(h8300_sci_pins[ch].port,
  156. h8300_sci_pins[ch].tx,
  157. H8300_GPIO_OUTPUT);
  158. /* tx mark output*/
  159. H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
  160. }
  161. #elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
  162. static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
  163. {
  164. if (port->mapbase == 0xA4400000) {
  165. __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
  166. __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
  167. } else if (port->mapbase == 0xA4410000)
  168. __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
  169. }
  170. #elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
  171. static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
  172. {
  173. unsigned short data;
  174. if (cflag & CRTSCTS) {
  175. /* enable RTS/CTS */
  176. if (port->mapbase == 0xa4430000) { /* SCIF0 */
  177. /* Clear PTCR bit 9-2; enable all scif pins but sck */
  178. data = __raw_readw(PORT_PTCR);
  179. __raw_writew((data & 0xfc03), PORT_PTCR);
  180. } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
  181. /* Clear PVCR bit 9-2 */
  182. data = __raw_readw(PORT_PVCR);
  183. __raw_writew((data & 0xfc03), PORT_PVCR);
  184. }
  185. } else {
  186. if (port->mapbase == 0xa4430000) { /* SCIF0 */
  187. /* Clear PTCR bit 5-2; enable only tx and rx */
  188. data = __raw_readw(PORT_PTCR);
  189. __raw_writew((data & 0xffc3), PORT_PTCR);
  190. } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
  191. /* Clear PVCR bit 5-2 */
  192. data = __raw_readw(PORT_PVCR);
  193. __raw_writew((data & 0xffc3), PORT_PVCR);
  194. }
  195. }
  196. }
  197. #elif defined(CONFIG_CPU_SH3)
  198. /* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
  199. static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
  200. {
  201. unsigned short data;
  202. /* We need to set SCPCR to enable RTS/CTS */
  203. data = __raw_readw(SCPCR);
  204. /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
  205. __raw_writew(data & 0x0fcf, SCPCR);
  206. if (!(cflag & CRTSCTS)) {
  207. /* We need to set SCPCR to enable RTS/CTS */
  208. data = __raw_readw(SCPCR);
  209. /* Clear out SCP7MD1,0, SCP4MD1,0,
  210. Set SCP6MD1,0 = {01} (output) */
  211. __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
  212. data = ctrl_inb(SCPDR);
  213. /* Set /RTS2 (bit6) = 0 */
  214. ctrl_outb(data & 0xbf, SCPDR);
  215. }
  216. }
  217. #elif defined(CONFIG_CPU_SUBTYPE_SH7722)
  218. static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
  219. {
  220. unsigned short data;
  221. if (port->mapbase == 0xffe00000) {
  222. data = __raw_readw(PSCR);
  223. data &= ~0x03cf;
  224. if (!(cflag & CRTSCTS))
  225. data |= 0x0340;
  226. __raw_writew(data, PSCR);
  227. }
  228. }
  229. #elif defined(CONFIG_CPU_SUBTYPE_SH7763) || \
  230. defined(CONFIG_CPU_SUBTYPE_SH7780) || \
  231. defined(CONFIG_CPU_SUBTYPE_SH7785) || \
  232. defined(CONFIG_CPU_SUBTYPE_SH7786) || \
  233. defined(CONFIG_CPU_SUBTYPE_SHX3)
  234. static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
  235. {
  236. if (!(cflag & CRTSCTS))
  237. __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
  238. }
  239. #elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
  240. static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
  241. {
  242. if (!(cflag & CRTSCTS))
  243. __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
  244. }
  245. #else
  246. static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
  247. {
  248. /* Nothing to do */
  249. }
  250. #endif
  251. #if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
  252. defined(CONFIG_CPU_SUBTYPE_SH7780) || \
  253. defined(CONFIG_CPU_SUBTYPE_SH7785) || \
  254. defined(CONFIG_CPU_SUBTYPE_SH7786)
  255. static inline int scif_txroom(struct uart_port *port)
  256. {
  257. return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
  258. }
  259. static inline int scif_rxroom(struct uart_port *port)
  260. {
  261. return sci_in(port, SCRFDR) & 0xff;
  262. }
  263. #elif defined(CONFIG_CPU_SUBTYPE_SH7763)
  264. static inline int scif_txroom(struct uart_port *port)
  265. {
  266. if ((port->mapbase == 0xffe00000) ||
  267. (port->mapbase == 0xffe08000)) {
  268. /* SCIF0/1*/
  269. return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
  270. } else {
  271. /* SCIF2 */
  272. return SCIF2_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
  273. }
  274. }
  275. static inline int scif_rxroom(struct uart_port *port)
  276. {
  277. if ((port->mapbase == 0xffe00000) ||
  278. (port->mapbase == 0xffe08000)) {
  279. /* SCIF0/1*/
  280. return sci_in(port, SCRFDR) & 0xff;
  281. } else {
  282. /* SCIF2 */
  283. return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
  284. }
  285. }
  286. #else
  287. static inline int scif_txroom(struct uart_port *port)
  288. {
  289. return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
  290. }
  291. static inline int scif_rxroom(struct uart_port *port)
  292. {
  293. return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
  294. }
  295. #endif
  296. static inline int sci_txroom(struct uart_port *port)
  297. {
  298. return (sci_in(port, SCxSR) & SCI_TDRE) != 0;
  299. }
  300. static inline int sci_rxroom(struct uart_port *port)
  301. {
  302. return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
  303. }
  304. /* ********************************************************************** *
  305. * the interrupt related routines *
  306. * ********************************************************************** */
  307. static void sci_transmit_chars(struct uart_port *port)
  308. {
  309. struct circ_buf *xmit = &port->info->xmit;
  310. unsigned int stopped = uart_tx_stopped(port);
  311. unsigned short status;
  312. unsigned short ctrl;
  313. int count;
  314. status = sci_in(port, SCxSR);
  315. if (!(status & SCxSR_TDxE(port))) {
  316. ctrl = sci_in(port, SCSCR);
  317. if (uart_circ_empty(xmit))
  318. ctrl &= ~SCI_CTRL_FLAGS_TIE;
  319. else
  320. ctrl |= SCI_CTRL_FLAGS_TIE;
  321. sci_out(port, SCSCR, ctrl);
  322. return;
  323. }
  324. if (port->type == PORT_SCI)
  325. count = sci_txroom(port);
  326. else
  327. count = scif_txroom(port);
  328. do {
  329. unsigned char c;
  330. if (port->x_char) {
  331. c = port->x_char;
  332. port->x_char = 0;
  333. } else if (!uart_circ_empty(xmit) && !stopped) {
  334. c = xmit->buf[xmit->tail];
  335. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  336. } else {
  337. break;
  338. }
  339. sci_out(port, SCxTDR, c);
  340. port->icount.tx++;
  341. } while (--count > 0);
  342. sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
  343. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  344. uart_write_wakeup(port);
  345. if (uart_circ_empty(xmit)) {
  346. sci_stop_tx(port);
  347. } else {
  348. ctrl = sci_in(port, SCSCR);
  349. if (port->type != PORT_SCI) {
  350. sci_in(port, SCxSR); /* Dummy read */
  351. sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
  352. }
  353. ctrl |= SCI_CTRL_FLAGS_TIE;
  354. sci_out(port, SCSCR, ctrl);
  355. }
  356. }
  357. /* On SH3, SCIF may read end-of-break as a space->mark char */
  358. #define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
  359. static inline void sci_receive_chars(struct uart_port *port)
  360. {
  361. struct sci_port *sci_port = to_sci_port(port);
  362. struct tty_struct *tty = port->info->port.tty;
  363. int i, count, copied = 0;
  364. unsigned short status;
  365. unsigned char flag;
  366. status = sci_in(port, SCxSR);
  367. if (!(status & SCxSR_RDxF(port)))
  368. return;
  369. while (1) {
  370. if (port->type == PORT_SCI)
  371. count = sci_rxroom(port);
  372. else
  373. count = scif_rxroom(port);
  374. /* Don't copy more bytes than there is room for in the buffer */
  375. count = tty_buffer_request_room(tty, count);
  376. /* If for any reason we can't copy more data, we're done! */
  377. if (count == 0)
  378. break;
  379. if (port->type == PORT_SCI) {
  380. char c = sci_in(port, SCxRDR);
  381. if (uart_handle_sysrq_char(port, c) ||
  382. sci_port->break_flag)
  383. count = 0;
  384. else
  385. tty_insert_flip_char(tty, c, TTY_NORMAL);
  386. } else {
  387. for (i = 0; i < count; i++) {
  388. char c = sci_in(port, SCxRDR);
  389. status = sci_in(port, SCxSR);
  390. #if defined(CONFIG_CPU_SH3)
  391. /* Skip "chars" during break */
  392. if (sci_port->break_flag) {
  393. if ((c == 0) &&
  394. (status & SCxSR_FER(port))) {
  395. count--; i--;
  396. continue;
  397. }
  398. /* Nonzero => end-of-break */
  399. dev_dbg(port->dev, "debounce<%02x>\n", c);
  400. sci_port->break_flag = 0;
  401. if (STEPFN(c)) {
  402. count--; i--;
  403. continue;
  404. }
  405. }
  406. #endif /* CONFIG_CPU_SH3 */
  407. if (uart_handle_sysrq_char(port, c)) {
  408. count--; i--;
  409. continue;
  410. }
  411. /* Store data and status */
  412. if (status&SCxSR_FER(port)) {
  413. flag = TTY_FRAME;
  414. dev_notice(port->dev, "frame error\n");
  415. } else if (status&SCxSR_PER(port)) {
  416. flag = TTY_PARITY;
  417. dev_notice(port->dev, "parity error\n");
  418. } else
  419. flag = TTY_NORMAL;
  420. tty_insert_flip_char(tty, c, flag);
  421. }
  422. }
  423. sci_in(port, SCxSR); /* dummy read */
  424. sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  425. copied += count;
  426. port->icount.rx += count;
  427. }
  428. if (copied) {
  429. /* Tell the rest of the system the news. New characters! */
  430. tty_flip_buffer_push(tty);
  431. } else {
  432. sci_in(port, SCxSR); /* dummy read */
  433. sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  434. }
  435. }
  436. #define SCI_BREAK_JIFFIES (HZ/20)
  437. /* The sci generates interrupts during the break,
  438. * 1 per millisecond or so during the break period, for 9600 baud.
  439. * So dont bother disabling interrupts.
  440. * But dont want more than 1 break event.
  441. * Use a kernel timer to periodically poll the rx line until
  442. * the break is finished.
  443. */
  444. static void sci_schedule_break_timer(struct sci_port *port)
  445. {
  446. port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
  447. add_timer(&port->break_timer);
  448. }
  449. /* Ensure that two consecutive samples find the break over. */
  450. static void sci_break_timer(unsigned long data)
  451. {
  452. struct sci_port *port = (struct sci_port *)data;
  453. if (sci_rxd_in(&port->port) == 0) {
  454. port->break_flag = 1;
  455. sci_schedule_break_timer(port);
  456. } else if (port->break_flag == 1) {
  457. /* break is over. */
  458. port->break_flag = 2;
  459. sci_schedule_break_timer(port);
  460. } else
  461. port->break_flag = 0;
  462. }
  463. static inline int sci_handle_errors(struct uart_port *port)
  464. {
  465. int copied = 0;
  466. unsigned short status = sci_in(port, SCxSR);
  467. struct tty_struct *tty = port->info->port.tty;
  468. if (status & SCxSR_ORER(port)) {
  469. /* overrun error */
  470. if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
  471. copied++;
  472. dev_notice(port->dev, "overrun error");
  473. }
  474. if (status & SCxSR_FER(port)) {
  475. if (sci_rxd_in(port) == 0) {
  476. /* Notify of BREAK */
  477. struct sci_port *sci_port = to_sci_port(port);
  478. if (!sci_port->break_flag) {
  479. sci_port->break_flag = 1;
  480. sci_schedule_break_timer(sci_port);
  481. /* Do sysrq handling. */
  482. if (uart_handle_break(port))
  483. return 0;
  484. dev_dbg(port->dev, "BREAK detected\n");
  485. if (tty_insert_flip_char(tty, 0, TTY_BREAK))
  486. copied++;
  487. }
  488. } else {
  489. /* frame error */
  490. if (tty_insert_flip_char(tty, 0, TTY_FRAME))
  491. copied++;
  492. dev_notice(port->dev, "frame error\n");
  493. }
  494. }
  495. if (status & SCxSR_PER(port)) {
  496. /* parity error */
  497. if (tty_insert_flip_char(tty, 0, TTY_PARITY))
  498. copied++;
  499. dev_notice(port->dev, "parity error");
  500. }
  501. if (copied)
  502. tty_flip_buffer_push(tty);
  503. return copied;
  504. }
  505. static inline int sci_handle_fifo_overrun(struct uart_port *port)
  506. {
  507. struct tty_struct *tty = port->info->port.tty;
  508. int copied = 0;
  509. if (port->type != PORT_SCIF)
  510. return 0;
  511. if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
  512. sci_out(port, SCLSR, 0);
  513. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  514. tty_flip_buffer_push(tty);
  515. dev_notice(port->dev, "overrun error\n");
  516. copied++;
  517. }
  518. return copied;
  519. }
  520. static inline int sci_handle_breaks(struct uart_port *port)
  521. {
  522. int copied = 0;
  523. unsigned short status = sci_in(port, SCxSR);
  524. struct tty_struct *tty = port->info->port.tty;
  525. struct sci_port *s = to_sci_port(port);
  526. if (uart_handle_break(port))
  527. return 0;
  528. if (!s->break_flag && status & SCxSR_BRK(port)) {
  529. #if defined(CONFIG_CPU_SH3)
  530. /* Debounce break */
  531. s->break_flag = 1;
  532. #endif
  533. /* Notify of BREAK */
  534. if (tty_insert_flip_char(tty, 0, TTY_BREAK))
  535. copied++;
  536. dev_dbg(port->dev, "BREAK detected\n");
  537. }
  538. if (copied)
  539. tty_flip_buffer_push(tty);
  540. copied += sci_handle_fifo_overrun(port);
  541. return copied;
  542. }
  543. static irqreturn_t sci_rx_interrupt(int irq, void *port)
  544. {
  545. /* I think sci_receive_chars has to be called irrespective
  546. * of whether the I_IXOFF is set, otherwise, how is the interrupt
  547. * to be disabled?
  548. */
  549. sci_receive_chars(port);
  550. return IRQ_HANDLED;
  551. }
  552. static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
  553. {
  554. struct uart_port *port = ptr;
  555. spin_lock_irq(&port->lock);
  556. sci_transmit_chars(port);
  557. spin_unlock_irq(&port->lock);
  558. return IRQ_HANDLED;
  559. }
  560. static irqreturn_t sci_er_interrupt(int irq, void *ptr)
  561. {
  562. struct uart_port *port = ptr;
  563. /* Handle errors */
  564. if (port->type == PORT_SCI) {
  565. if (sci_handle_errors(port)) {
  566. /* discard character in rx buffer */
  567. sci_in(port, SCxSR);
  568. sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  569. }
  570. } else {
  571. sci_handle_fifo_overrun(port);
  572. sci_rx_interrupt(irq, ptr);
  573. }
  574. sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
  575. /* Kick the transmission */
  576. sci_tx_interrupt(irq, ptr);
  577. return IRQ_HANDLED;
  578. }
  579. static irqreturn_t sci_br_interrupt(int irq, void *ptr)
  580. {
  581. struct uart_port *port = ptr;
  582. /* Handle BREAKs */
  583. sci_handle_breaks(port);
  584. sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
  585. return IRQ_HANDLED;
  586. }
  587. static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
  588. {
  589. unsigned short ssr_status, scr_status;
  590. struct uart_port *port = ptr;
  591. irqreturn_t ret = IRQ_NONE;
  592. ssr_status = sci_in(port, SCxSR);
  593. scr_status = sci_in(port, SCSCR);
  594. /* Tx Interrupt */
  595. if ((ssr_status & 0x0020) && (scr_status & SCI_CTRL_FLAGS_TIE))
  596. ret = sci_tx_interrupt(irq, ptr);
  597. /* Rx Interrupt */
  598. if ((ssr_status & 0x0002) && (scr_status & SCI_CTRL_FLAGS_RIE))
  599. ret = sci_rx_interrupt(irq, ptr);
  600. /* Error Interrupt */
  601. if ((ssr_status & 0x0080) && (scr_status & SCI_CTRL_FLAGS_REIE))
  602. ret = sci_er_interrupt(irq, ptr);
  603. /* Break Interrupt */
  604. if ((ssr_status & 0x0010) && (scr_status & SCI_CTRL_FLAGS_REIE))
  605. ret = sci_br_interrupt(irq, ptr);
  606. return ret;
  607. }
  608. #ifdef CONFIG_HAVE_CLK
  609. /*
  610. * Here we define a transistion notifier so that we can update all of our
  611. * ports' baud rate when the peripheral clock changes.
  612. */
  613. static int sci_notifier(struct notifier_block *self,
  614. unsigned long phase, void *p)
  615. {
  616. struct sh_sci_priv *priv = container_of(self,
  617. struct sh_sci_priv, clk_nb);
  618. struct sci_port *sci_port;
  619. unsigned long flags;
  620. if ((phase == CPUFREQ_POSTCHANGE) ||
  621. (phase == CPUFREQ_RESUMECHANGE)) {
  622. spin_lock_irqsave(&priv->lock, flags);
  623. list_for_each_entry(sci_port, &priv->ports, node)
  624. sci_port->port.uartclk = clk_get_rate(sci_port->clk);
  625. spin_unlock_irqrestore(&priv->lock, flags);
  626. }
  627. return NOTIFY_OK;
  628. }
  629. #endif
  630. static int sci_request_irq(struct sci_port *port)
  631. {
  632. int i;
  633. irqreturn_t (*handlers[4])(int irq, void *ptr) = {
  634. sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
  635. sci_br_interrupt,
  636. };
  637. const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
  638. "SCI Transmit Data Empty", "SCI Break" };
  639. if (port->irqs[0] == port->irqs[1]) {
  640. if (unlikely(!port->irqs[0]))
  641. return -ENODEV;
  642. if (request_irq(port->irqs[0], sci_mpxed_interrupt,
  643. IRQF_DISABLED, "sci", port)) {
  644. dev_err(port->port.dev, "Can't allocate IRQ\n");
  645. return -ENODEV;
  646. }
  647. } else {
  648. for (i = 0; i < ARRAY_SIZE(handlers); i++) {
  649. if (unlikely(!port->irqs[i]))
  650. continue;
  651. if (request_irq(port->irqs[i], handlers[i],
  652. IRQF_DISABLED, desc[i], port)) {
  653. dev_err(port->port.dev, "Can't allocate IRQ\n");
  654. return -ENODEV;
  655. }
  656. }
  657. }
  658. return 0;
  659. }
  660. static void sci_free_irq(struct sci_port *port)
  661. {
  662. int i;
  663. if (port->irqs[0] == port->irqs[1])
  664. free_irq(port->irqs[0], port);
  665. else {
  666. for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
  667. if (!port->irqs[i])
  668. continue;
  669. free_irq(port->irqs[i], port);
  670. }
  671. }
  672. }
  673. static unsigned int sci_tx_empty(struct uart_port *port)
  674. {
  675. /* Can't detect */
  676. return TIOCSER_TEMT;
  677. }
  678. static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
  679. {
  680. /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
  681. /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
  682. /* If you have signals for DTR and DCD, please implement here. */
  683. }
  684. static unsigned int sci_get_mctrl(struct uart_port *port)
  685. {
  686. /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
  687. and CTS/RTS */
  688. return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
  689. }
  690. static void sci_start_tx(struct uart_port *port)
  691. {
  692. unsigned short ctrl;
  693. /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
  694. ctrl = sci_in(port, SCSCR);
  695. ctrl |= SCI_CTRL_FLAGS_TIE;
  696. sci_out(port, SCSCR, ctrl);
  697. }
  698. static void sci_stop_tx(struct uart_port *port)
  699. {
  700. unsigned short ctrl;
  701. /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
  702. ctrl = sci_in(port, SCSCR);
  703. ctrl &= ~SCI_CTRL_FLAGS_TIE;
  704. sci_out(port, SCSCR, ctrl);
  705. }
  706. static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
  707. {
  708. unsigned short ctrl;
  709. /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
  710. ctrl = sci_in(port, SCSCR);
  711. ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
  712. sci_out(port, SCSCR, ctrl);
  713. }
  714. static void sci_stop_rx(struct uart_port *port)
  715. {
  716. unsigned short ctrl;
  717. /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
  718. ctrl = sci_in(port, SCSCR);
  719. ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
  720. sci_out(port, SCSCR, ctrl);
  721. }
  722. static void sci_enable_ms(struct uart_port *port)
  723. {
  724. /* Nothing here yet .. */
  725. }
  726. static void sci_break_ctl(struct uart_port *port, int break_state)
  727. {
  728. /* Nothing here yet .. */
  729. }
  730. static int sci_startup(struct uart_port *port)
  731. {
  732. struct sci_port *s = to_sci_port(port);
  733. if (s->enable)
  734. s->enable(port);
  735. #ifdef CONFIG_HAVE_CLK
  736. s->clk = clk_get(NULL, "module_clk");
  737. #endif
  738. sci_request_irq(s);
  739. sci_start_tx(port);
  740. sci_start_rx(port, 1);
  741. return 0;
  742. }
  743. static void sci_shutdown(struct uart_port *port)
  744. {
  745. struct sci_port *s = to_sci_port(port);
  746. sci_stop_rx(port);
  747. sci_stop_tx(port);
  748. sci_free_irq(s);
  749. if (s->disable)
  750. s->disable(port);
  751. #ifdef CONFIG_HAVE_CLK
  752. clk_put(s->clk);
  753. s->clk = NULL;
  754. #endif
  755. }
  756. static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
  757. struct ktermios *old)
  758. {
  759. unsigned int status, baud, smr_val;
  760. int t = -1;
  761. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  762. if (likely(baud))
  763. t = SCBRR_VALUE(baud, port->uartclk);
  764. do {
  765. status = sci_in(port, SCxSR);
  766. } while (!(status & SCxSR_TEND(port)));
  767. sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
  768. if (port->type != PORT_SCI)
  769. sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
  770. smr_val = sci_in(port, SCSMR) & 3;
  771. if ((termios->c_cflag & CSIZE) == CS7)
  772. smr_val |= 0x40;
  773. if (termios->c_cflag & PARENB)
  774. smr_val |= 0x20;
  775. if (termios->c_cflag & PARODD)
  776. smr_val |= 0x30;
  777. if (termios->c_cflag & CSTOPB)
  778. smr_val |= 0x08;
  779. uart_update_timeout(port, termios->c_cflag, baud);
  780. sci_out(port, SCSMR, smr_val);
  781. if (t > 0) {
  782. if (t >= 256) {
  783. sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
  784. t >>= 2;
  785. } else
  786. sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
  787. sci_out(port, SCBRR, t);
  788. udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
  789. }
  790. sci_init_pins(port, termios->c_cflag);
  791. sci_out(port, SCFCR, (termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0);
  792. sci_out(port, SCSCR, SCSCR_INIT(port));
  793. if ((termios->c_cflag & CREAD) != 0)
  794. sci_start_rx(port, 0);
  795. }
  796. static const char *sci_type(struct uart_port *port)
  797. {
  798. switch (port->type) {
  799. case PORT_IRDA:
  800. return "irda";
  801. case PORT_SCI:
  802. return "sci";
  803. case PORT_SCIF:
  804. return "scif";
  805. case PORT_SCIFA:
  806. return "scifa";
  807. }
  808. return NULL;
  809. }
  810. static void sci_release_port(struct uart_port *port)
  811. {
  812. /* Nothing here yet .. */
  813. }
  814. static int sci_request_port(struct uart_port *port)
  815. {
  816. /* Nothing here yet .. */
  817. return 0;
  818. }
  819. static void sci_config_port(struct uart_port *port, int flags)
  820. {
  821. struct sci_port *s = to_sci_port(port);
  822. port->type = s->type;
  823. if (port->flags & UPF_IOREMAP && !port->membase) {
  824. port->membase = ioremap_nocache(port->mapbase, 0x40);
  825. dev_err(port->dev, "can't remap port#%d\n", port->line);
  826. }
  827. }
  828. static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
  829. {
  830. struct sci_port *s = to_sci_port(port);
  831. if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
  832. return -EINVAL;
  833. if (ser->baud_base < 2400)
  834. /* No paper tape reader for Mitch.. */
  835. return -EINVAL;
  836. return 0;
  837. }
  838. static struct uart_ops sci_uart_ops = {
  839. .tx_empty = sci_tx_empty,
  840. .set_mctrl = sci_set_mctrl,
  841. .get_mctrl = sci_get_mctrl,
  842. .start_tx = sci_start_tx,
  843. .stop_tx = sci_stop_tx,
  844. .stop_rx = sci_stop_rx,
  845. .enable_ms = sci_enable_ms,
  846. .break_ctl = sci_break_ctl,
  847. .startup = sci_startup,
  848. .shutdown = sci_shutdown,
  849. .set_termios = sci_set_termios,
  850. .type = sci_type,
  851. .release_port = sci_release_port,
  852. .request_port = sci_request_port,
  853. .config_port = sci_config_port,
  854. .verify_port = sci_verify_port,
  855. #ifdef CONFIG_CONSOLE_POLL
  856. .poll_get_char = sci_poll_get_char,
  857. .poll_put_char = sci_poll_put_char,
  858. #endif
  859. };
  860. static int __devinit sci_init_single(struct sci_port *sci_port,
  861. unsigned int index,
  862. struct plat_sci_port *p)
  863. {
  864. sci_port->port.ops = &sci_uart_ops;
  865. sci_port->port.iotype = UPIO_MEM;
  866. sci_port->port.line = index;
  867. sci_port->port.fifosize = 1;
  868. #if defined(__H8300H__) || defined(__H8300S__)
  869. #ifdef __H8300S__
  870. sci_port->enable = h8300_sci_enable;
  871. sci_port->disable = h8300_sci_disable;
  872. #endif
  873. sci_port->port.uartclk = CONFIG_CPU_CLOCK;
  874. #elif defined(CONFIG_HAVE_CLK)
  875. /*
  876. * XXX: We should use a proper SCI/SCIF clock
  877. */
  878. {
  879. struct clk *clk = clk_get(NULL, "module_clk");
  880. sci_port->port.uartclk = clk_get_rate(clk);
  881. clk_put(clk);
  882. }
  883. #else
  884. #error "Need a valid uartclk"
  885. #endif
  886. sci_port->break_timer.data = (unsigned long)sci_port;
  887. sci_port->break_timer.function = sci_break_timer;
  888. init_timer(&sci_port->break_timer);
  889. sci_port->port.mapbase = p->mapbase;
  890. if (p->mapbase && !p->membase) {
  891. if (p->flags & UPF_IOREMAP) {
  892. p->membase = ioremap_nocache(p->mapbase, 0x40);
  893. if (IS_ERR(p->membase))
  894. return PTR_ERR(p->membase);
  895. } else {
  896. /*
  897. * For the simple (and majority of) cases
  898. * where we don't need to do any remapping,
  899. * just cast the cookie directly.
  900. */
  901. p->membase = (void __iomem *)p->mapbase;
  902. }
  903. }
  904. sci_port->port.membase = p->membase;
  905. sci_port->port.irq = p->irqs[SCIx_TXI_IRQ];
  906. sci_port->port.flags = p->flags;
  907. sci_port->type = sci_port->port.type = p->type;
  908. memcpy(&sci_port->irqs, &p->irqs, sizeof(p->irqs));
  909. return 0;
  910. }
  911. #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
  912. static struct tty_driver *serial_console_device(struct console *co, int *index)
  913. {
  914. struct uart_driver *p = &sci_uart_driver;
  915. *index = co->index;
  916. return p->tty_driver;
  917. }
  918. static void serial_console_putchar(struct uart_port *port, int ch)
  919. {
  920. sci_poll_put_char(port, ch);
  921. }
  922. /*
  923. * Print a string to the serial port trying not to disturb
  924. * any possible real use of the port...
  925. */
  926. static void serial_console_write(struct console *co, const char *s,
  927. unsigned count)
  928. {
  929. struct uart_port *port = co->data;
  930. unsigned short bits;
  931. uart_console_write(co->data, s, count, serial_console_putchar);
  932. /* wait until fifo is empty and last bit has been transmitted */
  933. bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
  934. while ((sci_in(port, SCxSR) & bits) != bits)
  935. cpu_relax();
  936. }
  937. static int __init serial_console_setup(struct console *co, char *options)
  938. {
  939. struct sci_port *sci_port;
  940. struct uart_port *port;
  941. int baud = 115200;
  942. int bits = 8;
  943. int parity = 'n';
  944. int flow = 'n';
  945. int ret;
  946. /*
  947. * Check whether an invalid uart number has been specified, and
  948. * if so, search for the first available port that does have
  949. * console support.
  950. */
  951. if (co->index >= SCI_NPORTS)
  952. co->index = 0;
  953. sci_port = &sci_ports[co->index];
  954. port = &sci_port->port;
  955. co->data = port;
  956. /*
  957. * Also need to check port->type, we don't actually have any
  958. * UPIO_PORT ports, but uart_report_port() handily misreports
  959. * it anyways if we don't have a port available by the time this is
  960. * called.
  961. */
  962. if (!port->type)
  963. return -ENODEV;
  964. #ifdef CONFIG_HAVE_CLK
  965. if (!sci_port->clk)
  966. sci_port->clk = clk_get(NULL, "module_clk");
  967. #endif
  968. if (port->flags & UPF_IOREMAP)
  969. sci_config_port(port, 0);
  970. if (sci_port->enable)
  971. sci_port->enable(port);
  972. if (options)
  973. uart_parse_options(options, &baud, &parity, &bits, &flow);
  974. ret = uart_set_options(port, co, baud, parity, bits, flow);
  975. #if defined(__H8300H__) || defined(__H8300S__)
  976. /* disable rx interrupt */
  977. if (ret == 0)
  978. sci_stop_rx(port);
  979. #endif
  980. return ret;
  981. }
  982. static struct console serial_console = {
  983. .name = "ttySC",
  984. .device = serial_console_device,
  985. .write = serial_console_write,
  986. .setup = serial_console_setup,
  987. .flags = CON_PRINTBUFFER,
  988. .index = -1,
  989. };
  990. static int __init sci_console_init(void)
  991. {
  992. register_console(&serial_console);
  993. return 0;
  994. }
  995. console_initcall(sci_console_init);
  996. #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
  997. #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
  998. #define SCI_CONSOLE (&serial_console)
  999. #else
  1000. #define SCI_CONSOLE 0
  1001. #endif
  1002. static char banner[] __initdata =
  1003. KERN_INFO "SuperH SCI(F) driver initialized\n";
  1004. static struct uart_driver sci_uart_driver = {
  1005. .owner = THIS_MODULE,
  1006. .driver_name = "sci",
  1007. .dev_name = "ttySC",
  1008. .major = SCI_MAJOR,
  1009. .minor = SCI_MINOR_START,
  1010. .nr = SCI_NPORTS,
  1011. .cons = SCI_CONSOLE,
  1012. };
  1013. static int __devexit sci_remove(struct platform_device *dev)
  1014. {
  1015. struct sh_sci_priv *priv = platform_get_drvdata(dev);
  1016. struct sci_port *p;
  1017. unsigned long flags;
  1018. #ifdef CONFIG_HAVE_CLK
  1019. cpufreq_unregister_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
  1020. #endif
  1021. spin_lock_irqsave(&priv->lock, flags);
  1022. list_for_each_entry(p, &priv->ports, node)
  1023. uart_remove_one_port(&sci_uart_driver, &p->port);
  1024. spin_unlock_irqrestore(&priv->lock, flags);
  1025. kfree(priv);
  1026. return 0;
  1027. }
  1028. static int __devinit sci_probe_single(struct platform_device *dev,
  1029. unsigned int index,
  1030. struct plat_sci_port *p,
  1031. struct sci_port *sciport)
  1032. {
  1033. struct sh_sci_priv *priv = platform_get_drvdata(dev);
  1034. unsigned long flags;
  1035. int ret;
  1036. /* Sanity check */
  1037. if (unlikely(index >= SCI_NPORTS)) {
  1038. dev_notice(&dev->dev, "Attempting to register port "
  1039. "%d when only %d are available.\n",
  1040. index+1, SCI_NPORTS);
  1041. dev_notice(&dev->dev, "Consider bumping "
  1042. "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
  1043. return 0;
  1044. }
  1045. sciport->port.dev = &dev->dev;
  1046. ret = sci_init_single(sciport, index, p);
  1047. if (ret)
  1048. return ret;
  1049. ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
  1050. if (ret) {
  1051. if (p->flags & UPF_IOREMAP)
  1052. iounmap(p->membase);
  1053. return ret;
  1054. }
  1055. INIT_LIST_HEAD(&sciport->node);
  1056. spin_lock_irqsave(&priv->lock, flags);
  1057. list_add(&sciport->node, &priv->ports);
  1058. spin_unlock_irqrestore(&priv->lock, flags);
  1059. return 0;
  1060. }
  1061. /*
  1062. * Register a set of serial devices attached to a platform device. The
  1063. * list is terminated with a zero flags entry, which means we expect
  1064. * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
  1065. * remapping (such as sh64) should also set UPF_IOREMAP.
  1066. */
  1067. static int __devinit sci_probe(struct platform_device *dev)
  1068. {
  1069. struct plat_sci_port *p = dev->dev.platform_data;
  1070. struct sh_sci_priv *priv;
  1071. int i, ret = -EINVAL;
  1072. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  1073. if (!priv)
  1074. return -ENOMEM;
  1075. INIT_LIST_HEAD(&priv->ports);
  1076. spin_lock_init(&priv->lock);
  1077. platform_set_drvdata(dev, priv);
  1078. #ifdef CONFIG_HAVE_CLK
  1079. priv->clk_nb.notifier_call = sci_notifier;
  1080. cpufreq_register_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
  1081. #endif
  1082. if (dev->id != -1) {
  1083. ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]);
  1084. if (ret)
  1085. goto err_unreg;
  1086. } else {
  1087. for (i = 0; p && p->flags != 0; p++, i++) {
  1088. ret = sci_probe_single(dev, i, p, &sci_ports[i]);
  1089. if (ret)
  1090. goto err_unreg;
  1091. }
  1092. }
  1093. #ifdef CONFIG_SH_STANDARD_BIOS
  1094. sh_bios_gdb_detach();
  1095. #endif
  1096. return 0;
  1097. err_unreg:
  1098. sci_remove(dev);
  1099. return ret;
  1100. }
  1101. static int sci_suspend(struct platform_device *dev, pm_message_t state)
  1102. {
  1103. struct sh_sci_priv *priv = platform_get_drvdata(dev);
  1104. struct sci_port *p;
  1105. unsigned long flags;
  1106. spin_lock_irqsave(&priv->lock, flags);
  1107. list_for_each_entry(p, &priv->ports, node)
  1108. uart_suspend_port(&sci_uart_driver, &p->port);
  1109. spin_unlock_irqrestore(&priv->lock, flags);
  1110. return 0;
  1111. }
  1112. static int sci_resume(struct platform_device *dev)
  1113. {
  1114. struct sh_sci_priv *priv = platform_get_drvdata(dev);
  1115. struct sci_port *p;
  1116. unsigned long flags;
  1117. spin_lock_irqsave(&priv->lock, flags);
  1118. list_for_each_entry(p, &priv->ports, node)
  1119. uart_resume_port(&sci_uart_driver, &p->port);
  1120. spin_unlock_irqrestore(&priv->lock, flags);
  1121. return 0;
  1122. }
  1123. static struct platform_driver sci_driver = {
  1124. .probe = sci_probe,
  1125. .remove = __devexit_p(sci_remove),
  1126. .suspend = sci_suspend,
  1127. .resume = sci_resume,
  1128. .driver = {
  1129. .name = "sh-sci",
  1130. .owner = THIS_MODULE,
  1131. },
  1132. };
  1133. static int __init sci_init(void)
  1134. {
  1135. int ret;
  1136. printk(banner);
  1137. ret = uart_register_driver(&sci_uart_driver);
  1138. if (likely(ret == 0)) {
  1139. ret = platform_driver_register(&sci_driver);
  1140. if (unlikely(ret))
  1141. uart_unregister_driver(&sci_uart_driver);
  1142. }
  1143. return ret;
  1144. }
  1145. static void __exit sci_exit(void)
  1146. {
  1147. platform_driver_unregister(&sci_driver);
  1148. uart_unregister_driver(&sci_uart_driver);
  1149. }
  1150. module_init(sci_init);
  1151. module_exit(sci_exit);
  1152. MODULE_LICENSE("GPL");
  1153. MODULE_ALIAS("platform:sh-sci");