sh-sci.c 41 KB

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