sh-sci.c 33 KB

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