sh-sci.c 34 KB

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