sh-sci.c 33 KB

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