sh-sci.c 35 KB

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