sh-sci.c 37 KB

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