sh-sci.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692
  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, 2003, 2004 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/config.h>
  22. #include <linux/module.h>
  23. #include <linux/errno.h>
  24. #include <linux/signal.h>
  25. #include <linux/sched.h>
  26. #include <linux/timer.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/tty.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/serial.h>
  31. #include <linux/major.h>
  32. #include <linux/string.h>
  33. #include <linux/sysrq.h>
  34. #include <linux/fcntl.h>
  35. #include <linux/ptrace.h>
  36. #include <linux/ioport.h>
  37. #include <linux/mm.h>
  38. #include <linux/slab.h>
  39. #include <linux/init.h>
  40. #include <linux/delay.h>
  41. #include <linux/console.h>
  42. #include <linux/bitops.h>
  43. #ifdef CONFIG_CPU_FREQ
  44. #include <linux/notifier.h>
  45. #include <linux/cpufreq.h>
  46. #endif
  47. #include <asm/system.h>
  48. #include <asm/io.h>
  49. #include <asm/irq.h>
  50. #include <asm/uaccess.h>
  51. #include <linux/generic_serial.h>
  52. #ifdef CONFIG_SH_STANDARD_BIOS
  53. #include <asm/sh_bios.h>
  54. #endif
  55. #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  56. #define SUPPORT_SYSRQ
  57. #endif
  58. #include "sh-sci.h"
  59. #ifdef CONFIG_SH_KGDB
  60. #include <asm/kgdb.h>
  61. static int kgdb_get_char(struct sci_port *port);
  62. static void kgdb_put_char(struct sci_port *port, char c);
  63. static void kgdb_handle_error(struct sci_port *port);
  64. static struct sci_port *kgdb_sci_port;
  65. #endif /* CONFIG_SH_KGDB */
  66. #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
  67. static struct sci_port *serial_console_port = 0;
  68. #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
  69. /* Function prototypes */
  70. static void sci_stop_tx(struct uart_port *port);
  71. static void sci_start_tx(struct uart_port *port);
  72. static void sci_start_rx(struct uart_port *port, unsigned int tty_start);
  73. static void sci_stop_rx(struct uart_port *port);
  74. static int sci_request_irq(struct sci_port *port);
  75. static void sci_free_irq(struct sci_port *port);
  76. static struct sci_port sci_ports[SCI_NPORTS];
  77. static struct uart_driver sci_uart_driver;
  78. #if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
  79. static void handle_error(struct uart_port *port)
  80. { /* Clear error flags */
  81. sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
  82. }
  83. static int get_char(struct uart_port *port)
  84. {
  85. unsigned long flags;
  86. unsigned short status;
  87. int c;
  88. local_irq_save(flags);
  89. do {
  90. status = sci_in(port, SCxSR);
  91. if (status & SCxSR_ERRORS(port)) {
  92. handle_error(port);
  93. continue;
  94. }
  95. } while (!(status & SCxSR_RDxF(port)));
  96. c = sci_in(port, SCxRDR);
  97. sci_in(port, SCxSR); /* Dummy read */
  98. sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  99. local_irq_restore(flags);
  100. return c;
  101. }
  102. /* Taken from sh-stub.c of GDB 4.18 */
  103. static const char hexchars[] = "0123456789abcdef";
  104. static __inline__ char highhex(int x)
  105. {
  106. return hexchars[(x >> 4) & 0xf];
  107. }
  108. static __inline__ char lowhex(int x)
  109. {
  110. return hexchars[x & 0xf];
  111. }
  112. #endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
  113. /*
  114. * Send the packet in buffer. The host gets one chance to read it.
  115. * This routine does not wait for a positive acknowledge.
  116. */
  117. #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
  118. static void put_char(struct uart_port *port, char c)
  119. {
  120. unsigned long flags;
  121. unsigned short status;
  122. local_irq_save(flags);
  123. do {
  124. status = sci_in(port, SCxSR);
  125. } while (!(status & SCxSR_TDxE(port)));
  126. sci_out(port, SCxTDR, c);
  127. sci_in(port, SCxSR); /* Dummy read */
  128. sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
  129. local_irq_restore(flags);
  130. }
  131. static void put_string(struct sci_port *sci_port, const char *buffer, int count)
  132. {
  133. struct uart_port *port = &sci_port->port;
  134. const unsigned char *p = buffer;
  135. int i;
  136. #if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
  137. int checksum;
  138. int usegdb=0;
  139. #ifdef CONFIG_SH_STANDARD_BIOS
  140. /* This call only does a trap the first time it is
  141. * called, and so is safe to do here unconditionally
  142. */
  143. usegdb |= sh_bios_in_gdb_mode();
  144. #endif
  145. #ifdef CONFIG_SH_KGDB
  146. usegdb |= (kgdb_in_gdb_mode && (port == kgdb_sci_port));
  147. #endif
  148. if (usegdb) {
  149. /* $<packet info>#<checksum>. */
  150. do {
  151. unsigned char c;
  152. put_char(port, '$');
  153. put_char(port, 'O'); /* 'O'utput to console */
  154. checksum = 'O';
  155. for (i=0; i<count; i++) { /* Don't use run length encoding */
  156. int h, l;
  157. c = *p++;
  158. h = highhex(c);
  159. l = lowhex(c);
  160. put_char(port, h);
  161. put_char(port, l);
  162. checksum += h + l;
  163. }
  164. put_char(port, '#');
  165. put_char(port, highhex(checksum));
  166. put_char(port, lowhex(checksum));
  167. } while (get_char(port) != '+');
  168. } else
  169. #endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
  170. for (i=0; i<count; i++) {
  171. if (*p == 10)
  172. put_char(port, '\r');
  173. put_char(port, *p++);
  174. }
  175. }
  176. #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
  177. #ifdef CONFIG_SH_KGDB
  178. /* Is the SCI ready, ie is there a char waiting? */
  179. static int kgdb_is_char_ready(struct sci_port *port)
  180. {
  181. unsigned short status = sci_in(port, SCxSR);
  182. if (status & (SCxSR_ERRORS(port) | SCxSR_BRK(port)))
  183. kgdb_handle_error(port);
  184. return (status & SCxSR_RDxF(port));
  185. }
  186. /* Write a char */
  187. static void kgdb_put_char(struct sci_port *port, char c)
  188. {
  189. unsigned short status;
  190. do
  191. status = sci_in(port, SCxSR);
  192. while (!(status & SCxSR_TDxE(port)));
  193. sci_out(port, SCxTDR, c);
  194. sci_in(port, SCxSR); /* Dummy read */
  195. sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
  196. }
  197. /* Get a char if there is one, else ret -1 */
  198. static int kgdb_get_char(struct sci_port *port)
  199. {
  200. int c;
  201. if (kgdb_is_char_ready(port) == 0)
  202. c = -1;
  203. else {
  204. c = sci_in(port, SCxRDR);
  205. sci_in(port, SCxSR); /* Dummy read */
  206. sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  207. }
  208. return c;
  209. }
  210. /* Called from kgdbstub.c to get a character, i.e. is blocking */
  211. static int kgdb_sci_getchar(void)
  212. {
  213. volatile int c;
  214. /* Keep trying to read a character, this could be neater */
  215. while ((c = kgdb_get_char(kgdb_sci_port)) < 0);
  216. return c;
  217. }
  218. /* Called from kgdbstub.c to put a character, just a wrapper */
  219. static void kgdb_sci_putchar(int c)
  220. {
  221. kgdb_put_char(kgdb_sci_port, c);
  222. }
  223. /* Clear any errors on the SCI */
  224. static void kgdb_handle_error(struct sci_port *port)
  225. {
  226. sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port)); /* Clear error flags */
  227. }
  228. /* Breakpoint if there's a break sent on the serial port */
  229. static void kgdb_break_interrupt(int irq, void *ptr, struct pt_regs *regs)
  230. {
  231. struct sci_port *port = ptr;
  232. unsigned short status = sci_in(port, SCxSR);
  233. if (status & SCxSR_BRK(port)) {
  234. /* Break into the debugger if a break is detected */
  235. BREAKPOINT();
  236. /* Clear */
  237. sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
  238. }
  239. }
  240. #endif /* CONFIG_SH_KGDB */
  241. #if defined(__H8300S__)
  242. enum { sci_disable, sci_enable };
  243. static void h8300_sci_enable(struct uart_port* port, unsigned int ctrl)
  244. {
  245. volatile unsigned char *mstpcrl=(volatile unsigned char *)MSTPCRL;
  246. int ch = (port->mapbase - SMR0) >> 3;
  247. unsigned char mask = 1 << (ch+1);
  248. if (ctrl == sci_disable) {
  249. *mstpcrl |= mask;
  250. } else {
  251. *mstpcrl &= ~mask;
  252. }
  253. }
  254. #endif
  255. #if defined(SCI_ONLY) || defined(SCI_AND_SCIF)
  256. #if defined(__H8300H__) || defined(__H8300S__)
  257. static void sci_init_pins_sci(struct uart_port* port, unsigned int cflag)
  258. {
  259. int ch = (port->mapbase - SMR0) >> 3;
  260. /* set DDR regs */
  261. H8300_GPIO_DDR(h8300_sci_pins[ch].port,h8300_sci_pins[ch].rx,H8300_GPIO_INPUT);
  262. H8300_GPIO_DDR(h8300_sci_pins[ch].port,h8300_sci_pins[ch].tx,H8300_GPIO_OUTPUT);
  263. /* tx mark output*/
  264. H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
  265. }
  266. #else
  267. static void sci_init_pins_sci(struct uart_port *port, unsigned int cflag)
  268. {
  269. }
  270. #endif
  271. #endif
  272. #if defined(SCIF_ONLY) || defined(SCI_AND_SCIF)
  273. #if defined(CONFIG_CPU_SH3)
  274. /* For SH7705, SH7707, SH7709, SH7709A, SH7729, SH7300*/
  275. static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
  276. {
  277. unsigned int fcr_val = 0;
  278. #if !defined(CONFIG_CPU_SUBTYPE_SH7300) /* SH7300 doesn't use RTS/CTS */
  279. {
  280. unsigned short data;
  281. /* We need to set SCPCR to enable RTS/CTS */
  282. data = ctrl_inw(SCPCR);
  283. /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
  284. ctrl_outw(data&0x0fcf, SCPCR);
  285. }
  286. if (cflag & CRTSCTS)
  287. fcr_val |= SCFCR_MCE;
  288. else {
  289. unsigned short data;
  290. /* We need to set SCPCR to enable RTS/CTS */
  291. data = ctrl_inw(SCPCR);
  292. /* Clear out SCP7MD1,0, SCP4MD1,0,
  293. Set SCP6MD1,0 = {01} (output) */
  294. ctrl_outw((data&0x0fcf)|0x1000, SCPCR);
  295. data = ctrl_inb(SCPDR);
  296. /* Set /RTS2 (bit6) = 0 */
  297. ctrl_outb(data&0xbf, SCPDR);
  298. }
  299. #endif
  300. sci_out(port, SCFCR, fcr_val);
  301. }
  302. static void sci_init_pins_irda(struct uart_port *port, unsigned int cflag)
  303. {
  304. unsigned int fcr_val = 0;
  305. if (cflag & CRTSCTS)
  306. fcr_val |= SCFCR_MCE;
  307. sci_out(port, SCFCR, fcr_val);
  308. }
  309. #else
  310. /* For SH7750 */
  311. static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
  312. {
  313. unsigned int fcr_val = 0;
  314. if (cflag & CRTSCTS) {
  315. fcr_val |= SCFCR_MCE;
  316. } else {
  317. ctrl_outw(0x0080, SCSPTR2); /* Set RTS = 1 */
  318. }
  319. sci_out(port, SCFCR, fcr_val);
  320. }
  321. #endif
  322. #endif /* SCIF_ONLY || SCI_AND_SCIF */
  323. /* ********************************************************************** *
  324. * the interrupt related routines *
  325. * ********************************************************************** */
  326. static void sci_transmit_chars(struct uart_port *port)
  327. {
  328. struct circ_buf *xmit = &port->info->xmit;
  329. unsigned int stopped = uart_tx_stopped(port);
  330. unsigned long flags;
  331. unsigned short status;
  332. unsigned short ctrl;
  333. int count, txroom;
  334. status = sci_in(port, SCxSR);
  335. if (!(status & SCxSR_TDxE(port))) {
  336. local_irq_save(flags);
  337. ctrl = sci_in(port, SCSCR);
  338. if (uart_circ_empty(xmit)) {
  339. ctrl &= ~SCI_CTRL_FLAGS_TIE;
  340. } else {
  341. ctrl |= SCI_CTRL_FLAGS_TIE;
  342. }
  343. sci_out(port, SCSCR, ctrl);
  344. local_irq_restore(flags);
  345. return;
  346. }
  347. #if !defined(SCI_ONLY)
  348. if (port->type == PORT_SCIF) {
  349. txroom = SCIF_TXROOM_MAX - (sci_in(port, SCFDR)>>8);
  350. } else {
  351. txroom = (sci_in(port, SCxSR) & SCI_TDRE)?1:0;
  352. }
  353. #else
  354. txroom = (sci_in(port, SCxSR) & SCI_TDRE)?1:0;
  355. #endif
  356. count = txroom;
  357. do {
  358. unsigned char c;
  359. if (port->x_char) {
  360. c = port->x_char;
  361. port->x_char = 0;
  362. } else if (!uart_circ_empty(xmit) && !stopped) {
  363. c = xmit->buf[xmit->tail];
  364. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  365. } else {
  366. break;
  367. }
  368. sci_out(port, SCxTDR, c);
  369. port->icount.tx++;
  370. } while (--count > 0);
  371. sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
  372. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  373. uart_write_wakeup(port);
  374. if (uart_circ_empty(xmit)) {
  375. sci_stop_tx(port);
  376. } else {
  377. local_irq_save(flags);
  378. ctrl = sci_in(port, SCSCR);
  379. #if !defined(SCI_ONLY)
  380. if (port->type == PORT_SCIF) {
  381. sci_in(port, SCxSR); /* Dummy read */
  382. sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
  383. }
  384. #endif
  385. ctrl |= SCI_CTRL_FLAGS_TIE;
  386. sci_out(port, SCSCR, ctrl);
  387. local_irq_restore(flags);
  388. }
  389. }
  390. /* On SH3, SCIF may read end-of-break as a space->mark char */
  391. #define STEPFN(c) ({int __c=(c); (((__c-1)|(__c)) == -1); })
  392. static inline void sci_receive_chars(struct uart_port *port,
  393. struct pt_regs *regs)
  394. {
  395. struct tty_struct *tty = port->info->tty;
  396. int i, count, copied = 0;
  397. unsigned short status;
  398. status = sci_in(port, SCxSR);
  399. if (!(status & SCxSR_RDxF(port)))
  400. return;
  401. while (1) {
  402. #if !defined(SCI_ONLY)
  403. if (port->type == PORT_SCIF) {
  404. count = sci_in(port, SCFDR)&SCIF_RFDC_MASK ;
  405. } else {
  406. count = (sci_in(port, SCxSR)&SCxSR_RDxF(port))?1:0;
  407. }
  408. #else
  409. count = (sci_in(port, SCxSR)&SCxSR_RDxF(port))?1:0;
  410. #endif
  411. /* Don't copy more bytes than there is room for in the buffer */
  412. if (tty->flip.count + count > TTY_FLIPBUF_SIZE)
  413. count = TTY_FLIPBUF_SIZE - tty->flip.count;
  414. /* If for any reason we can't copy more data, we're done! */
  415. if (count == 0)
  416. break;
  417. if (port->type == PORT_SCI) {
  418. char c = sci_in(port, SCxRDR);
  419. if(((struct sci_port *)port)->break_flag
  420. || uart_handle_sysrq_char(port, c, regs)) {
  421. count = 0;
  422. } else {
  423. tty->flip.char_buf_ptr[0] = c;
  424. tty->flip.flag_buf_ptr[0] = TTY_NORMAL;
  425. }
  426. } else {
  427. for (i=0; i<count; i++) {
  428. char c = sci_in(port, SCxRDR);
  429. status = sci_in(port, SCxSR);
  430. #if defined(CONFIG_CPU_SH3)
  431. /* Skip "chars" during break */
  432. if (((struct sci_port *)port)->break_flag) {
  433. if ((c == 0) &&
  434. (status & SCxSR_FER(port))) {
  435. count--; i--;
  436. continue;
  437. }
  438. /* Nonzero => end-of-break */
  439. pr_debug("scif: debounce<%02x>\n", c);
  440. ((struct sci_port *)port)->break_flag = 0;
  441. if (STEPFN(c)) {
  442. count--; i--;
  443. continue;
  444. }
  445. }
  446. #endif /* CONFIG_CPU_SH3 */
  447. if (uart_handle_sysrq_char(port, c, regs)) {
  448. count--; i--;
  449. continue;
  450. }
  451. /* Store data and status */
  452. tty->flip.char_buf_ptr[i] = c;
  453. if (status&SCxSR_FER(port)) {
  454. tty->flip.flag_buf_ptr[i] = TTY_FRAME;
  455. pr_debug("sci: frame error\n");
  456. } else if (status&SCxSR_PER(port)) {
  457. tty->flip.flag_buf_ptr[i] = TTY_PARITY;
  458. pr_debug("sci: parity error\n");
  459. } else {
  460. tty->flip.flag_buf_ptr[i] = TTY_NORMAL;
  461. }
  462. }
  463. }
  464. sci_in(port, SCxSR); /* dummy read */
  465. sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  466. /* Update the kernel buffer end */
  467. tty->flip.count += count;
  468. tty->flip.char_buf_ptr += count;
  469. tty->flip.flag_buf_ptr += count;
  470. copied += count;
  471. port->icount.rx += count;
  472. }
  473. if (copied) {
  474. /* Tell the rest of the system the news. New characters! */
  475. tty_flip_buffer_push(tty);
  476. } else {
  477. sci_in(port, SCxSR); /* dummy read */
  478. sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  479. }
  480. }
  481. #define SCI_BREAK_JIFFIES (HZ/20)
  482. /* The sci generates interrupts during the break,
  483. * 1 per millisecond or so during the break period, for 9600 baud.
  484. * So dont bother disabling interrupts.
  485. * But dont want more than 1 break event.
  486. * Use a kernel timer to periodically poll the rx line until
  487. * the break is finished.
  488. */
  489. static void sci_schedule_break_timer(struct sci_port *port)
  490. {
  491. port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
  492. add_timer(&port->break_timer);
  493. }
  494. /* Ensure that two consecutive samples find the break over. */
  495. static void sci_break_timer(unsigned long data)
  496. {
  497. struct sci_port * port = (struct sci_port *)data;
  498. if(sci_rxd_in(&port->port) == 0) {
  499. port->break_flag = 1;
  500. sci_schedule_break_timer(port);
  501. } else if(port->break_flag == 1){
  502. /* break is over. */
  503. port->break_flag = 2;
  504. sci_schedule_break_timer(port);
  505. } else port->break_flag = 0;
  506. }
  507. static inline int sci_handle_errors(struct uart_port *port)
  508. {
  509. int copied = 0;
  510. unsigned short status = sci_in(port, SCxSR);
  511. struct tty_struct *tty = port->info->tty;
  512. if (status&SCxSR_ORER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
  513. /* overrun error */
  514. copied++;
  515. *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
  516. pr_debug("sci: overrun error\n");
  517. }
  518. if (status&SCxSR_FER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
  519. if (sci_rxd_in(port) == 0) {
  520. /* Notify of BREAK */
  521. struct sci_port * sci_port = (struct sci_port *)port;
  522. if(!sci_port->break_flag) {
  523. sci_port->break_flag = 1;
  524. sci_schedule_break_timer((struct sci_port *)port);
  525. /* Do sysrq handling. */
  526. if(uart_handle_break(port)) {
  527. return 0;
  528. }
  529. pr_debug("sci: BREAK detected\n");
  530. copied++;
  531. *tty->flip.flag_buf_ptr++ = TTY_BREAK;
  532. }
  533. }
  534. else {
  535. /* frame error */
  536. copied++;
  537. *tty->flip.flag_buf_ptr++ = TTY_FRAME;
  538. pr_debug("sci: frame error\n");
  539. }
  540. }
  541. if (status&SCxSR_PER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
  542. /* parity error */
  543. copied++;
  544. *tty->flip.flag_buf_ptr++ = TTY_PARITY;
  545. pr_debug("sci: parity error\n");
  546. }
  547. if (copied) {
  548. tty->flip.count += copied;
  549. tty_flip_buffer_push(tty);
  550. }
  551. return copied;
  552. }
  553. static inline int sci_handle_breaks(struct uart_port *port)
  554. {
  555. int copied = 0;
  556. unsigned short status = sci_in(port, SCxSR);
  557. struct tty_struct *tty = port->info->tty;
  558. struct sci_port *s = &sci_ports[port->line];
  559. if (!s->break_flag && status & SCxSR_BRK(port) &&
  560. tty->flip.count < TTY_FLIPBUF_SIZE) {
  561. #if defined(CONFIG_CPU_SH3)
  562. /* Debounce break */
  563. s->break_flag = 1;
  564. #endif
  565. /* Notify of BREAK */
  566. copied++;
  567. *tty->flip.flag_buf_ptr++ = TTY_BREAK;
  568. pr_debug("sci: BREAK detected\n");
  569. }
  570. #if defined(SCIF_ORER)
  571. /* XXX: Handle SCIF overrun error */
  572. if (port->type == PORT_SCIF && (sci_in(port, SCLSR) & SCIF_ORER) != 0) {
  573. sci_out(port, SCLSR, 0);
  574. if(tty->flip.count<TTY_FLIPBUF_SIZE) {
  575. copied++;
  576. *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
  577. pr_debug("sci: overrun error\n");
  578. }
  579. }
  580. #endif
  581. if (copied) {
  582. tty->flip.count += copied;
  583. tty_flip_buffer_push(tty);
  584. }
  585. return copied;
  586. }
  587. static irqreturn_t sci_rx_interrupt(int irq, void *ptr, struct pt_regs *regs)
  588. {
  589. struct uart_port *port = ptr;
  590. /* I think sci_receive_chars has to be called irrespective
  591. * of whether the I_IXOFF is set, otherwise, how is the interrupt
  592. * to be disabled?
  593. */
  594. sci_receive_chars(port, regs);
  595. return IRQ_HANDLED;
  596. }
  597. static irqreturn_t sci_tx_interrupt(int irq, void *ptr, struct pt_regs *regs)
  598. {
  599. struct uart_port *port = ptr;
  600. sci_transmit_chars(port);
  601. return IRQ_HANDLED;
  602. }
  603. static irqreturn_t sci_er_interrupt(int irq, void *ptr, struct pt_regs *regs)
  604. {
  605. struct uart_port *port = ptr;
  606. /* Handle errors */
  607. if (port->type == PORT_SCI) {
  608. if (sci_handle_errors(port)) {
  609. /* discard character in rx buffer */
  610. sci_in(port, SCxSR);
  611. sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  612. }
  613. } else {
  614. #if defined(SCIF_ORER)
  615. if((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
  616. struct tty_struct *tty = port->info->tty;
  617. sci_out(port, SCLSR, 0);
  618. if(tty->flip.count<TTY_FLIPBUF_SIZE) {
  619. *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
  620. tty->flip.count++;
  621. tty_flip_buffer_push(tty);
  622. pr_debug("scif: overrun error\n");
  623. }
  624. }
  625. #endif
  626. sci_rx_interrupt(irq, ptr, regs);
  627. }
  628. sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
  629. /* Kick the transmission */
  630. sci_tx_interrupt(irq, ptr, regs);
  631. return IRQ_HANDLED;
  632. }
  633. static irqreturn_t sci_br_interrupt(int irq, void *ptr, struct pt_regs *regs)
  634. {
  635. struct uart_port *port = ptr;
  636. /* Handle BREAKs */
  637. sci_handle_breaks(port);
  638. sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
  639. return IRQ_HANDLED;
  640. }
  641. static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr, struct pt_regs *regs)
  642. {
  643. unsigned short ssr_status, scr_status;
  644. struct uart_port *port = ptr;
  645. ssr_status = sci_in(port,SCxSR);
  646. scr_status = sci_in(port,SCSCR);
  647. /* Tx Interrupt */
  648. if ((ssr_status&0x0020) && (scr_status&0x0080))
  649. sci_tx_interrupt(irq, ptr, regs);
  650. /* Rx Interrupt */
  651. if ((ssr_status&0x0002) && (scr_status&0x0040))
  652. sci_rx_interrupt(irq, ptr, regs);
  653. /* Error Interrupt */
  654. if ((ssr_status&0x0080) && (scr_status&0x0400))
  655. sci_er_interrupt(irq, ptr, regs);
  656. /* Break Interrupt */
  657. if ((ssr_status&0x0010) && (scr_status&0x0200))
  658. sci_br_interrupt(irq, ptr, regs);
  659. return IRQ_HANDLED;
  660. }
  661. #ifdef CONFIG_CPU_FREQ
  662. /*
  663. * Here we define a transistion notifier so that we can update all of our
  664. * ports' baud rate when the peripheral clock changes.
  665. */
  666. static int sci_notifier(struct notifier_block *self, unsigned long phase, void *p)
  667. {
  668. struct cpufreq_freqs *freqs = p;
  669. int i;
  670. if ((phase == CPUFREQ_POSTCHANGE) ||
  671. (phase == CPUFREQ_RESUMECHANGE)){
  672. for (i = 0; i < SCI_NPORTS; i++) {
  673. struct uart_port *port = &sci_ports[i].port;
  674. /*
  675. * Update the uartclk per-port if frequency has
  676. * changed, since it will no longer necessarily be
  677. * consistent with the old frequency.
  678. *
  679. * Really we want to be able to do something like
  680. * uart_change_speed() or something along those lines
  681. * here to implicitly reset the per-port baud rate..
  682. *
  683. * Clean this up later..
  684. */
  685. port->uartclk = current_cpu_data.module_clock * 16;
  686. }
  687. printk("%s: got a postchange notification for cpu %d (old %d, new %d)\n",
  688. __FUNCTION__, freqs->cpu, freqs->old, freqs->new);
  689. }
  690. return NOTIFY_OK;
  691. }
  692. static struct notifier_block sci_nb = { &sci_notifier, NULL, 0 };
  693. #endif /* CONFIG_CPU_FREQ */
  694. static int sci_request_irq(struct sci_port *port)
  695. {
  696. int i;
  697. irqreturn_t (*handlers[4])(int irq, void *ptr, struct pt_regs *regs) = {
  698. sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
  699. sci_br_interrupt,
  700. };
  701. const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
  702. "SCI Transmit Data Empty", "SCI Break" };
  703. if (port->irqs[0] == port->irqs[1]) {
  704. if (!port->irqs[0]) {
  705. printk(KERN_ERR "sci: Cannot allocate irq.(IRQ=0)\n");
  706. return -ENODEV;
  707. }
  708. if (request_irq(port->irqs[0], sci_mpxed_interrupt, SA_INTERRUPT,
  709. "sci", port)) {
  710. printk(KERN_ERR "sci: Cannot allocate irq.\n");
  711. return -ENODEV;
  712. }
  713. } else {
  714. for (i = 0; i < ARRAY_SIZE(handlers); i++) {
  715. if (!port->irqs[i])
  716. continue;
  717. if (request_irq(port->irqs[i], handlers[i], SA_INTERRUPT,
  718. desc[i], port)) {
  719. printk(KERN_ERR "sci: Cannot allocate irq.\n");
  720. return -ENODEV;
  721. }
  722. }
  723. }
  724. return 0;
  725. }
  726. static void sci_free_irq(struct sci_port *port)
  727. {
  728. int i;
  729. if (port->irqs[0] == port->irqs[1]) {
  730. if (!port->irqs[0])
  731. printk("sci: sci_free_irq error\n");
  732. else
  733. free_irq(port->irqs[0], port);
  734. } else {
  735. for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
  736. if (!port->irqs[i])
  737. continue;
  738. free_irq(port->irqs[i], port);
  739. }
  740. }
  741. }
  742. static unsigned int sci_tx_empty(struct uart_port *port)
  743. {
  744. /* Can't detect */
  745. return TIOCSER_TEMT;
  746. }
  747. static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
  748. {
  749. /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
  750. /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
  751. /* If you have signals for DTR and DCD, please implement here. */
  752. }
  753. static unsigned int sci_get_mctrl(struct uart_port *port)
  754. {
  755. /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
  756. and CTS/RTS */
  757. return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
  758. }
  759. static void sci_start_tx(struct uart_port *port)
  760. {
  761. struct sci_port *s = &sci_ports[port->line];
  762. disable_irq(s->irqs[SCIx_TXI_IRQ]);
  763. sci_transmit_chars(port);
  764. enable_irq(s->irqs[SCIx_TXI_IRQ]);
  765. }
  766. static void sci_stop_tx(struct uart_port *port)
  767. {
  768. unsigned long flags;
  769. unsigned short ctrl;
  770. /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
  771. local_irq_save(flags);
  772. ctrl = sci_in(port, SCSCR);
  773. ctrl &= ~SCI_CTRL_FLAGS_TIE;
  774. sci_out(port, SCSCR, ctrl);
  775. local_irq_restore(flags);
  776. }
  777. static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
  778. {
  779. unsigned long flags;
  780. unsigned short ctrl;
  781. /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
  782. local_irq_save(flags);
  783. ctrl = sci_in(port, SCSCR);
  784. ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
  785. sci_out(port, SCSCR, ctrl);
  786. local_irq_restore(flags);
  787. }
  788. static void sci_stop_rx(struct uart_port *port)
  789. {
  790. unsigned long flags;
  791. unsigned short ctrl;
  792. /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
  793. local_irq_save(flags);
  794. ctrl = sci_in(port, SCSCR);
  795. ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
  796. sci_out(port, SCSCR, ctrl);
  797. local_irq_restore(flags);
  798. }
  799. static void sci_enable_ms(struct uart_port *port)
  800. {
  801. /* Nothing here yet .. */
  802. }
  803. static void sci_break_ctl(struct uart_port *port, int break_state)
  804. {
  805. /* Nothing here yet .. */
  806. }
  807. static int sci_startup(struct uart_port *port)
  808. {
  809. struct sci_port *s = &sci_ports[port->line];
  810. #if defined(__H8300S__)
  811. h8300_sci_enable(port, sci_enable);
  812. #endif
  813. sci_request_irq(s);
  814. sci_start_tx(port, 1);
  815. sci_start_rx(port, 1);
  816. return 0;
  817. }
  818. static void sci_shutdown(struct uart_port *port)
  819. {
  820. struct sci_port *s = &sci_ports[port->line];
  821. sci_stop_rx(port);
  822. sci_stop_tx(port);
  823. sci_free_irq(s);
  824. #if defined(__H8300S__)
  825. h8300_sci_enable(port, sci_disable);
  826. #endif
  827. }
  828. static void sci_set_termios(struct uart_port *port, struct termios *termios,
  829. struct termios *old)
  830. {
  831. struct sci_port *s = &sci_ports[port->line];
  832. unsigned int status, baud, smr_val;
  833. unsigned long flags;
  834. int t;
  835. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  836. spin_lock_irqsave(&port->lock, flags);
  837. do {
  838. status = sci_in(port, SCxSR);
  839. } while (!(status & SCxSR_TEND(port)));
  840. sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
  841. #if !defined(SCI_ONLY)
  842. if (port->type == PORT_SCIF) {
  843. sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
  844. }
  845. #endif
  846. smr_val = sci_in(port, SCSMR) & 3;
  847. if ((termios->c_cflag & CSIZE) == CS7)
  848. smr_val |= 0x40;
  849. if (termios->c_cflag & PARENB)
  850. smr_val |= 0x20;
  851. if (termios->c_cflag & PARODD)
  852. smr_val |= 0x30;
  853. if (termios->c_cflag & CSTOPB)
  854. smr_val |= 0x08;
  855. uart_update_timeout(port, termios->c_cflag, baud);
  856. sci_out(port, SCSMR, smr_val);
  857. switch (baud) {
  858. case 0: t = -1; break;
  859. case 2400: t = BPS_2400; break;
  860. case 4800: t = BPS_4800; break;
  861. case 9600: t = BPS_9600; break;
  862. case 19200: t = BPS_19200; break;
  863. case 38400: t = BPS_38400; break;
  864. case 57600: t = BPS_57600; break;
  865. case 115200: t = BPS_115200; break;
  866. default: t = SCBRR_VALUE(baud); break;
  867. }
  868. if (t > 0) {
  869. if(t >= 256) {
  870. sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
  871. t >>= 2;
  872. } else {
  873. sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
  874. }
  875. sci_out(port, SCBRR, t);
  876. udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
  877. }
  878. s->init_pins(port, termios->c_cflag);
  879. sci_out(port, SCSCR, SCSCR_INIT(port));
  880. if ((termios->c_cflag & CREAD) != 0)
  881. sci_start_rx(port,0);
  882. spin_unlock_irqrestore(&port->lock, flags);
  883. }
  884. static const char *sci_type(struct uart_port *port)
  885. {
  886. switch (port->type) {
  887. case PORT_SCI: return "sci";
  888. case PORT_SCIF: return "scif";
  889. case PORT_IRDA: return "irda";
  890. }
  891. return 0;
  892. }
  893. static void sci_release_port(struct uart_port *port)
  894. {
  895. /* Nothing here yet .. */
  896. }
  897. static int sci_request_port(struct uart_port *port)
  898. {
  899. /* Nothing here yet .. */
  900. return 0;
  901. }
  902. static void sci_config_port(struct uart_port *port, int flags)
  903. {
  904. struct sci_port *s = &sci_ports[port->line];
  905. port->type = s->type;
  906. #if defined(CONFIG_CPU_SUBTYPE_SH5_101) || defined(CONFIG_CPU_SUBTYPE_SH5_103)
  907. if (port->mapbase == 0)
  908. port->mapbase = onchip_remap(SCIF_ADDR_SH5, 1024, "SCIF");
  909. port->membase = (void *)port->mapbase;
  910. #endif
  911. }
  912. static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
  913. {
  914. struct sci_port *s = &sci_ports[port->line];
  915. if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > NR_IRQS)
  916. return -EINVAL;
  917. if (ser->baud_base < 2400)
  918. /* No paper tape reader for Mitch.. */
  919. return -EINVAL;
  920. return 0;
  921. }
  922. static struct uart_ops sci_uart_ops = {
  923. .tx_empty = sci_tx_empty,
  924. .set_mctrl = sci_set_mctrl,
  925. .get_mctrl = sci_get_mctrl,
  926. .start_tx = sci_start_tx,
  927. .stop_tx = sci_stop_tx,
  928. .stop_rx = sci_stop_rx,
  929. .enable_ms = sci_enable_ms,
  930. .break_ctl = sci_break_ctl,
  931. .startup = sci_startup,
  932. .shutdown = sci_shutdown,
  933. .set_termios = sci_set_termios,
  934. .type = sci_type,
  935. .release_port = sci_release_port,
  936. .request_port = sci_request_port,
  937. .config_port = sci_config_port,
  938. .verify_port = sci_verify_port,
  939. };
  940. static struct sci_port sci_ports[SCI_NPORTS] = {
  941. #if defined(CONFIG_CPU_SUBTYPE_SH7708)
  942. {
  943. .port = {
  944. .membase = (void *)0xfffffe80,
  945. .mapbase = 0xfffffe80,
  946. .iotype = SERIAL_IO_MEM,
  947. .irq = 25,
  948. .ops = &sci_uart_ops,
  949. .flags = ASYNC_BOOT_AUTOCONF,
  950. .line = 0,
  951. },
  952. .type = PORT_SCI,
  953. .irqs = SCI_IRQS,
  954. .init_pins = sci_init_pins_sci,
  955. },
  956. #elif defined(CONFIG_CPU_SUBTYPE_SH7705)
  957. {
  958. .port = {
  959. .membase = (void *)SCIF0,
  960. .mapbase = SCIF0,
  961. .iotype = SERIAL_IO_MEM,
  962. .irq = 55,
  963. .ops = &sci_uart_ops,
  964. .flags = ASYNC_BOOT_AUTOCONF,
  965. .line = 0,
  966. },
  967. .type = PORT_SCIF,
  968. .irqs = SH3_IRDA_IRQS,
  969. .init_pins = sci_init_pins_scif,
  970. },
  971. {
  972. .port = {
  973. .membase = (void *)SCIF2,
  974. .mapbase = SCIF2,
  975. .iotype = SERIAL_IO_MEM,
  976. .irq = 59,
  977. .ops = &sci_uart_ops,
  978. .flags = ASYNC_BOOT_AUTOCONF,
  979. .line = 1,
  980. },
  981. .type = PORT_SCIF,
  982. .irqs = SH3_SCIF_IRQS,
  983. .init_pins = sci_init_pins_scif,
  984. }
  985. #elif defined(CONFIG_CPU_SUBTYPE_SH7707) || defined(CONFIG_CPU_SUBTYPE_SH7709)
  986. {
  987. .port = {
  988. .membase = (void *)0xfffffe80,
  989. .mapbase = 0xfffffe80,
  990. .iotype = SERIAL_IO_MEM,
  991. .irq = 25,
  992. .ops = &sci_uart_ops,
  993. .flags = ASYNC_BOOT_AUTOCONF,
  994. .line = 0,
  995. },
  996. .type = PORT_SCI,
  997. .irqs = SCI_IRQS,
  998. .init_pins = sci_init_pins_sci,
  999. },
  1000. {
  1001. .port = {
  1002. .membase = (void *)0xa4000150,
  1003. .mapbase = 0xa4000150,
  1004. .iotype = SERIAL_IO_MEM,
  1005. .irq = 59,
  1006. .ops = &sci_uart_ops,
  1007. .flags = ASYNC_BOOT_AUTOCONF,
  1008. .line = 1,
  1009. },
  1010. .type = PORT_SCIF,
  1011. .irqs = SH3_SCIF_IRQS,
  1012. .init_pins = sci_init_pins_scif,
  1013. },
  1014. {
  1015. .port = {
  1016. .membase = (void *)0xa4000140,
  1017. .mapbase = 0xa4000140,
  1018. .iotype = SERIAL_IO_MEM,
  1019. .irq = 55,
  1020. .ops = &sci_uart_ops,
  1021. .flags = ASYNC_BOOT_AUTOCONF,
  1022. .line = 2,
  1023. },
  1024. .type = PORT_IRDA,
  1025. .irqs = SH3_IRDA_IRQS,
  1026. .init_pins = sci_init_pins_irda,
  1027. }
  1028. #elif defined(CONFIG_CPU_SUBTYPE_SH7300)
  1029. {
  1030. .port = {
  1031. .membase = (void *)0xA4430000,
  1032. .mapbase = 0xA4430000,
  1033. .iotype = SERIAL_IO_MEM,
  1034. .irq = 25,
  1035. .ops = &sci_uart_ops,
  1036. .flags = ASYNC_BOOT_AUTOCONF,
  1037. .line = 0,
  1038. },
  1039. .type = PORT_SCIF,
  1040. .irqs = SH7300_SCIF0_IRQS,
  1041. .init_pins = sci_init_pins_scif,
  1042. },
  1043. #elif defined(CONFIG_CPU_SUBTYPE_SH73180)
  1044. {
  1045. .port = {
  1046. .membase = (void *)0xffe00000,
  1047. .mapbase = 0xffe00000,
  1048. .iotype = SERIAL_IO_MEM,
  1049. .irq = 25,
  1050. .ops = &sci_uart_ops,
  1051. .flags = ASYNC_BOOT_AUTOCONF,
  1052. .line = 0,
  1053. },
  1054. .type = PORT_SCIF,
  1055. .irqs = SH73180_SCIF_IRQS,
  1056. .init_pins = sci_init_pins_scif,
  1057. },
  1058. #elif defined(CONFIG_SH_RTS7751R2D)
  1059. {
  1060. .port = {
  1061. .membase = (void *)0xffe80000,
  1062. .mapbase = 0xffe80000,
  1063. .iotype = SERIAL_IO_MEM,
  1064. .irq = 43,
  1065. .ops = &sci_uart_ops,
  1066. .flags = ASYNC_BOOT_AUTOCONF,
  1067. .line = 0,
  1068. },
  1069. .type = PORT_SCIF,
  1070. .irqs = SH4_SCIF_IRQS,
  1071. .init_pins = sci_init_pins_scif,
  1072. },
  1073. #elif defined(CONFIG_CPU_SUBTYPE_SH7750) || defined(CONFIG_CPU_SUBTYPE_SH7751)
  1074. {
  1075. .port = {
  1076. .membase = (void *)0xffe00000,
  1077. .mapbase = 0xffe00000,
  1078. .iotype = SERIAL_IO_MEM,
  1079. .irq = 25,
  1080. .ops = &sci_uart_ops,
  1081. .flags = ASYNC_BOOT_AUTOCONF,
  1082. .line = 0,
  1083. },
  1084. .type = PORT_SCI,
  1085. .irqs = SCI_IRQS,
  1086. .init_pins = sci_init_pins_sci,
  1087. },
  1088. {
  1089. .port = {
  1090. .membase = (void *)0xffe80000,
  1091. .mapbase = 0xffe80000,
  1092. .iotype = SERIAL_IO_MEM,
  1093. .irq = 43,
  1094. .ops = &sci_uart_ops,
  1095. .flags = ASYNC_BOOT_AUTOCONF,
  1096. .line = 1,
  1097. },
  1098. .type = PORT_SCIF,
  1099. .irqs = SH4_SCIF_IRQS,
  1100. .init_pins = sci_init_pins_scif,
  1101. },
  1102. #elif defined(CONFIG_CPU_SUBTYPE_SH7760)
  1103. {
  1104. .port = {
  1105. .membase = (void *)0xfe600000,
  1106. .mapbase = 0xfe600000,
  1107. .iotype = SERIAL_IO_MEM,
  1108. .irq = 55,
  1109. .ops = &sci_uart_ops,
  1110. .flags = ASYNC_BOOT_AUTOCONF,
  1111. .line = 0,
  1112. },
  1113. .type = PORT_SCIF,
  1114. .irqs = SH7760_SCIF0_IRQS,
  1115. .init_pins = sci_init_pins_scif,
  1116. },
  1117. {
  1118. .port = {
  1119. .membase = (void *)0xfe610000,
  1120. .mapbase = 0xfe610000,
  1121. .iotype = SERIAL_IO_MEM,
  1122. .irq = 75,
  1123. .ops = &sci_uart_ops,
  1124. .flags = ASYNC_BOOT_AUTOCONF,
  1125. .line = 1,
  1126. },
  1127. .type = PORT_SCIF,
  1128. .irqs = SH7760_SCIF1_IRQS,
  1129. .init_pins = sci_init_pins_scif,
  1130. },
  1131. {
  1132. .port = {
  1133. .membase = (void *)0xfe620000,
  1134. .mapbase = 0xfe620000,
  1135. .iotype = SERIAL_IO_MEM,
  1136. .irq = 79,
  1137. .ops = &sci_uart_ops,
  1138. .flags = ASYNC_BOOT_AUTOCONF,
  1139. .line = 2,
  1140. },
  1141. .type = PORT_SCIF,
  1142. .irqs = SH7760_SCIF2_IRQS,
  1143. .init_pins = sci_init_pins_scif,
  1144. },
  1145. #elif defined(CONFIG_CPU_SUBTYPE_SH4_202)
  1146. {
  1147. .port = {
  1148. .membase = (void *)0xffe80000,
  1149. .mapbase = 0xffe80000,
  1150. .iotype = SERIAL_IO_MEM,
  1151. .irq = 43,
  1152. .ops = &sci_uart_ops,
  1153. .flags = ASYNC_BOOT_AUTOCONF,
  1154. .line = 0,
  1155. },
  1156. .type = PORT_SCIF,
  1157. .irqs = SH4_SCIF_IRQS,
  1158. .init_pins = sci_init_pins_scif,
  1159. },
  1160. #elif defined(CONFIG_CPU_SUBTYPE_ST40STB1)
  1161. {
  1162. .port = {
  1163. .membase = (void *)0xffe00000,
  1164. .mapbase = 0xffe00000,
  1165. .iotype = SERIAL_IO_MEM,
  1166. .irq = 26,
  1167. .ops = &sci_uart_ops,
  1168. .flags = ASYNC_BOOT_AUTOCONF,
  1169. .line = 0,
  1170. },
  1171. .type = PORT_SCIF,
  1172. .irqs = STB1_SCIF1_IRQS,
  1173. .init_pins = sci_init_pins_scif,
  1174. },
  1175. {
  1176. .port = {
  1177. .membase = (void *)0xffe80000,
  1178. .mapbase = 0xffe80000,
  1179. .iotype = SERIAL_IO_MEM,
  1180. .irq = 43,
  1181. .ops = &sci_uart_ops,
  1182. .flags = ASYNC_BOOT_AUTOCONF,
  1183. .line = 1,
  1184. },
  1185. .type = PORT_SCIF,
  1186. .irqs = SH4_SCIF_IRQS,
  1187. .init_pins = sci_init_pins_scif,
  1188. },
  1189. #elif defined(CONFIG_CPU_SUBTYPE_SH5_101) || defined(CONFIG_CPU_SUBTYPE_SH5_103)
  1190. {
  1191. .port = {
  1192. .iotype = SERIAL_IO_MEM,
  1193. .irq = 42,
  1194. .ops = &sci_uart_ops,
  1195. .flags = ASYNC_BOOT_AUTOCONF,
  1196. .line = 0,
  1197. },
  1198. .type = PORT_SCIF,
  1199. .irqs = SH5_SCIF_IRQS,
  1200. .init_pins = sci_init_pins_scif,
  1201. },
  1202. #elif defined(CONFIG_H83007) || defined(CONFIG_H83068)
  1203. {
  1204. .port = {
  1205. .membase = (void *)0x00ffffb0,
  1206. .mapbase = 0x00ffffb0,
  1207. .iotype = SERIAL_IO_MEM,
  1208. .irq = 54,
  1209. .ops = &sci_uart_ops,
  1210. .flags = ASYNC_BOOT_AUTOCONF,
  1211. .line = 0,
  1212. },
  1213. .type = PORT_SCI,
  1214. .irqs = H8300H_SCI_IRQS0,
  1215. .init_pins = sci_init_pins_sci,
  1216. },
  1217. {
  1218. .port = {
  1219. .membase = (void *)0x00ffffb8,
  1220. .mapbase = 0x00ffffb8,
  1221. .iotype = SERIAL_IO_MEM,
  1222. .irq = 58,
  1223. .ops = &sci_uart_ops,
  1224. .flags = ASYNC_BOOT_AUTOCONF,
  1225. .line = 1,
  1226. },
  1227. .type = PORT_SCI,
  1228. .irqs = H8300H_SCI_IRQS1,
  1229. .init_pins = sci_init_pins_sci,
  1230. },
  1231. {
  1232. .port = {
  1233. .membase = (void *)0x00ffffc0,
  1234. .mapbase = 0x00ffffc0,
  1235. .iotype = SERIAL_IO_MEM,
  1236. .irq = 62,
  1237. .ops = &sci_uart_ops,
  1238. .flags = ASYNC_BOOT_AUTOCONF,
  1239. .line = 2,
  1240. },
  1241. .type = PORT_SCI,
  1242. .irqs = H8300H_SCI_IRQS2,
  1243. .init_pins = sci_init_pins_sci,
  1244. },
  1245. #elif defined(CONFIG_H8S2678)
  1246. {
  1247. .port = {
  1248. .membase = (void *)0x00ffff78,
  1249. .mapbase = 0x00ffff78,
  1250. .iotype = SERIAL_IO_MEM,
  1251. .irq = 90,
  1252. .ops = &sci_uart_ops,
  1253. .flags = ASYNC_BOOT_AUTOCONF,
  1254. .line = 0,
  1255. },
  1256. .type = PORT_SCI,
  1257. .irqs = H8S_SCI_IRQS0,
  1258. .init_pins = sci_init_pins_sci,
  1259. },
  1260. {
  1261. .port = {
  1262. .membase = (void *)0x00ffff80,
  1263. .mapbase = 0x00ffff80,
  1264. .iotype = SERIAL_IO_MEM,
  1265. .irq = 94,
  1266. .ops = &sci_uart_ops,
  1267. .flags = ASYNC_BOOT_AUTOCONF,
  1268. .line = 1,
  1269. },
  1270. .type = PORT_SCI,
  1271. .irqs = H8S_SCI_IRQS1,
  1272. .init_pins = sci_init_pins_sci,
  1273. },
  1274. {
  1275. .port = {
  1276. .membase = (void *)0x00ffff88,
  1277. .mapbase = 0x00ffff88,
  1278. .iotype = SERIAL_IO_MEM,
  1279. .irq = 98,
  1280. .ops = &sci_uart_ops,
  1281. .flags = ASYNC_BOOT_AUTOCONF,
  1282. .line = 2,
  1283. },
  1284. .type = PORT_SCI,
  1285. .irqs = H8S_SCI_IRQS2,
  1286. .init_pins = sci_init_pins_sci,
  1287. },
  1288. #else
  1289. #error "CPU subtype not defined"
  1290. #endif
  1291. };
  1292. #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
  1293. /*
  1294. * Print a string to the serial port trying not to disturb
  1295. * any possible real use of the port...
  1296. */
  1297. static void serial_console_write(struct console *co, const char *s,
  1298. unsigned count)
  1299. {
  1300. put_string(serial_console_port, s, count);
  1301. }
  1302. static int __init serial_console_setup(struct console *co, char *options)
  1303. {
  1304. struct uart_port *port;
  1305. int baud = 115200;
  1306. int bits = 8;
  1307. int parity = 'n';
  1308. int flow = 'n';
  1309. int ret;
  1310. if (co->index >= SCI_NPORTS)
  1311. co->index = 0;
  1312. serial_console_port = &sci_ports[co->index];
  1313. port = &serial_console_port->port;
  1314. port->type = serial_console_port->type;
  1315. #ifdef CONFIG_SUPERH64
  1316. /* This is especially needed on sh64 to remap the SCIF */
  1317. sci_config_port(port, 0);
  1318. #endif
  1319. /*
  1320. * We need to set the initial uartclk here, since otherwise it will
  1321. * only ever be setup at sci_init() time.
  1322. */
  1323. #if !defined(__H8300H__) && !defined(__H8300S__)
  1324. port->uartclk = current_cpu_data.module_clock * 16;
  1325. #else
  1326. port->uartclk = CONFIG_CPU_CLOCK;
  1327. #endif
  1328. #if defined(__H8300S__)
  1329. h8300_sci_enable(port, sci_enable);
  1330. #endif
  1331. if (options)
  1332. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1333. ret = uart_set_options(port, co, baud, parity, bits, flow);
  1334. #if defined(__H8300H__) || defined(__H8300S__)
  1335. /* disable rx interrupt */
  1336. if (ret == 0)
  1337. sci_stop_rx(port);
  1338. #endif
  1339. return ret;
  1340. }
  1341. static struct console serial_console = {
  1342. .name = "ttySC",
  1343. .device = uart_console_device,
  1344. .write = serial_console_write,
  1345. .setup = serial_console_setup,
  1346. .flags = CON_PRINTBUFFER,
  1347. .index = -1,
  1348. .data = &sci_uart_driver,
  1349. };
  1350. static int __init sci_console_init(void)
  1351. {
  1352. register_console(&serial_console);
  1353. return 0;
  1354. }
  1355. console_initcall(sci_console_init);
  1356. #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
  1357. #ifdef CONFIG_SH_KGDB
  1358. /*
  1359. * FIXME: Most of this can go away.. at the moment, we rely on
  1360. * arch/sh/kernel/setup.c to do the command line parsing for kgdb, though
  1361. * most of that can easily be done here instead.
  1362. *
  1363. * For the time being, just accept the values that were parsed earlier..
  1364. */
  1365. static void __init kgdb_console_get_options(struct uart_port *port, int *baud,
  1366. int *parity, int *bits)
  1367. {
  1368. *baud = kgdb_baud;
  1369. *parity = tolower(kgdb_parity);
  1370. *bits = kgdb_bits - '0';
  1371. }
  1372. /*
  1373. * The naming here is somewhat misleading, since kgdb_console_setup() takes
  1374. * care of the early-on initialization for kgdb, regardless of whether we
  1375. * actually use kgdb as a console or not.
  1376. *
  1377. * On the plus side, this lets us kill off the old kgdb_sci_setup() nonsense.
  1378. */
  1379. int __init kgdb_console_setup(struct console *co, char *options)
  1380. {
  1381. struct uart_port *port = &sci_ports[kgdb_portnum].port;
  1382. int baud = 38400;
  1383. int bits = 8;
  1384. int parity = 'n';
  1385. int flow = 'n';
  1386. if (co->index >= SCI_NPORTS || co->index != kgdb_portnum)
  1387. co->index = kgdb_portnum;
  1388. if (options)
  1389. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1390. else
  1391. kgdb_console_get_options(port, &baud, &parity, &bits);
  1392. kgdb_getchar = kgdb_sci_getchar;
  1393. kgdb_putchar = kgdb_sci_putchar;
  1394. return uart_set_options(port, co, baud, parity, bits, flow);
  1395. }
  1396. #endif /* CONFIG_SH_KGDB */
  1397. #ifdef CONFIG_SH_KGDB_CONSOLE
  1398. static struct console kgdb_console = {
  1399. .name = "ttySC",
  1400. .write = kgdb_console_write,
  1401. .setup = kgdb_console_setup,
  1402. .flags = CON_PRINTBUFFER | CON_ENABLED,
  1403. .index = -1,
  1404. .data = &sci_uart_driver,
  1405. };
  1406. /* Register the KGDB console so we get messages (d'oh!) */
  1407. static int __init kgdb_console_init(void)
  1408. {
  1409. register_console(&kgdb_console);
  1410. return 0;
  1411. }
  1412. console_initcall(kgdb_console_init);
  1413. #endif /* CONFIG_SH_KGDB_CONSOLE */
  1414. #if defined(CONFIG_SH_KGDB_CONSOLE)
  1415. #define SCI_CONSOLE &kgdb_console
  1416. #elif defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
  1417. #define SCI_CONSOLE &serial_console
  1418. #else
  1419. #define SCI_CONSOLE 0
  1420. #endif
  1421. static char banner[] __initdata =
  1422. KERN_INFO "SuperH SCI(F) driver initialized\n";
  1423. static struct uart_driver sci_uart_driver = {
  1424. .owner = THIS_MODULE,
  1425. .driver_name = "sci",
  1426. #ifdef CONFIG_DEVFS_FS
  1427. .devfs_name = "ttsc/",
  1428. #endif
  1429. .dev_name = "ttySC",
  1430. .major = SCI_MAJOR,
  1431. .minor = SCI_MINOR_START,
  1432. .nr = SCI_NPORTS,
  1433. .cons = SCI_CONSOLE,
  1434. };
  1435. static int __init sci_init(void)
  1436. {
  1437. int chan, ret;
  1438. printk("%s", banner);
  1439. ret = uart_register_driver(&sci_uart_driver);
  1440. if (ret == 0) {
  1441. for (chan = 0; chan < SCI_NPORTS; chan++) {
  1442. struct sci_port *sciport = &sci_ports[chan];
  1443. #if !defined(__H8300H__) && !defined(__H8300S__)
  1444. sciport->port.uartclk = (current_cpu_data.module_clock * 16);
  1445. #else
  1446. sciport->port.uartclk = CONFIG_CPU_CLOCK;
  1447. #endif
  1448. uart_add_one_port(&sci_uart_driver, &sciport->port);
  1449. sciport->break_timer.data = (unsigned long)sciport;
  1450. sciport->break_timer.function = sci_break_timer;
  1451. init_timer(&sciport->break_timer);
  1452. }
  1453. }
  1454. #ifdef CONFIG_CPU_FREQ
  1455. cpufreq_register_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER);
  1456. printk("sci: CPU frequency notifier registered\n");
  1457. #endif
  1458. #ifdef CONFIG_SH_STANDARD_BIOS
  1459. sh_bios_gdb_detach();
  1460. #endif
  1461. return ret;
  1462. }
  1463. static void __exit sci_exit(void)
  1464. {
  1465. int chan;
  1466. for (chan = 0; chan < SCI_NPORTS; chan++)
  1467. uart_remove_one_port(&sci_uart_driver, &sci_ports[chan].port);
  1468. uart_unregister_driver(&sci_uart_driver);
  1469. }
  1470. module_init(sci_init);
  1471. module_exit(sci_exit);