sh-sci.c 36 KB

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