sh-sci.c 36 KB

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