sh-sci.c 35 KB

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