sh-sci.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  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. #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. #if defined(CONFIG_CPU_SUBTYPE_SH7343) || defined(CONFIG_CPU_SUBTYPE_SH7366)
  334. /* Nothing */
  335. #elif defined(CONFIG_CPU_SUBTYPE_SH7763) || \
  336. defined(CONFIG_CPU_SUBTYPE_SH7780) || \
  337. defined(CONFIG_CPU_SUBTYPE_SH7785) || \
  338. defined(CONFIG_CPU_SUBTYPE_SHX3)
  339. ctrl_outw(0x0080, SCSPTR0); /* Set RTS = 1 */
  340. #else
  341. ctrl_outw(0x0080, SCSPTR2); /* Set RTS = 1 */
  342. #endif
  343. }
  344. sci_out(port, SCFCR, fcr_val);
  345. }
  346. #endif
  347. #if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
  348. defined(CONFIG_CPU_SUBTYPE_SH7763) || \
  349. defined(CONFIG_CPU_SUBTYPE_SH7780) || \
  350. defined(CONFIG_CPU_SUBTYPE_SH7785)
  351. static inline int scif_txroom(struct uart_port *port)
  352. {
  353. return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
  354. }
  355. static inline int scif_rxroom(struct uart_port *port)
  356. {
  357. return sci_in(port, SCRFDR) & 0xff;
  358. }
  359. #else
  360. static inline int scif_txroom(struct uart_port *port)
  361. {
  362. return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
  363. }
  364. static inline int scif_rxroom(struct uart_port *port)
  365. {
  366. return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
  367. }
  368. #endif
  369. #endif /* SCIF_ONLY || SCI_AND_SCIF */
  370. static inline int sci_txroom(struct uart_port *port)
  371. {
  372. return ((sci_in(port, SCxSR) & SCI_TDRE) != 0);
  373. }
  374. static inline int sci_rxroom(struct uart_port *port)
  375. {
  376. return ((sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0);
  377. }
  378. /* ********************************************************************** *
  379. * the interrupt related routines *
  380. * ********************************************************************** */
  381. static void sci_transmit_chars(struct uart_port *port)
  382. {
  383. struct circ_buf *xmit = &port->info->xmit;
  384. unsigned int stopped = uart_tx_stopped(port);
  385. unsigned short status;
  386. unsigned short ctrl;
  387. int count;
  388. status = sci_in(port, SCxSR);
  389. if (!(status & SCxSR_TDxE(port))) {
  390. ctrl = sci_in(port, SCSCR);
  391. if (uart_circ_empty(xmit)) {
  392. ctrl &= ~SCI_CTRL_FLAGS_TIE;
  393. } else {
  394. ctrl |= SCI_CTRL_FLAGS_TIE;
  395. }
  396. sci_out(port, SCSCR, ctrl);
  397. return;
  398. }
  399. #ifndef SCI_ONLY
  400. if (port->type == PORT_SCIF)
  401. count = scif_txroom(port);
  402. else
  403. #endif
  404. count = sci_txroom(port);
  405. do {
  406. unsigned char c;
  407. if (port->x_char) {
  408. c = port->x_char;
  409. port->x_char = 0;
  410. } else if (!uart_circ_empty(xmit) && !stopped) {
  411. c = xmit->buf[xmit->tail];
  412. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  413. } else {
  414. break;
  415. }
  416. sci_out(port, SCxTDR, c);
  417. port->icount.tx++;
  418. } while (--count > 0);
  419. sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
  420. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  421. uart_write_wakeup(port);
  422. if (uart_circ_empty(xmit)) {
  423. sci_stop_tx(port);
  424. } else {
  425. ctrl = sci_in(port, SCSCR);
  426. #if !defined(SCI_ONLY)
  427. if (port->type == PORT_SCIF) {
  428. sci_in(port, SCxSR); /* Dummy read */
  429. sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
  430. }
  431. #endif
  432. ctrl |= SCI_CTRL_FLAGS_TIE;
  433. sci_out(port, SCSCR, ctrl);
  434. }
  435. }
  436. /* On SH3, SCIF may read end-of-break as a space->mark char */
  437. #define STEPFN(c) ({int __c=(c); (((__c-1)|(__c)) == -1); })
  438. static inline void sci_receive_chars(struct uart_port *port)
  439. {
  440. struct sci_port *sci_port = (struct sci_port *)port;
  441. struct tty_struct *tty = port->info->tty;
  442. int i, count, copied = 0;
  443. unsigned short status;
  444. unsigned char flag;
  445. status = sci_in(port, SCxSR);
  446. if (!(status & SCxSR_RDxF(port)))
  447. return;
  448. while (1) {
  449. #if !defined(SCI_ONLY)
  450. if (port->type == PORT_SCIF)
  451. count = scif_rxroom(port);
  452. else
  453. #endif
  454. count = sci_rxroom(port);
  455. /* Don't copy more bytes than there is room for in the buffer */
  456. count = tty_buffer_request_room(tty, count);
  457. /* If for any reason we can't copy more data, we're done! */
  458. if (count == 0)
  459. break;
  460. if (port->type == PORT_SCI) {
  461. char c = sci_in(port, SCxRDR);
  462. if (uart_handle_sysrq_char(port, c) || sci_port->break_flag)
  463. count = 0;
  464. else {
  465. tty_insert_flip_char(tty, c, TTY_NORMAL);
  466. }
  467. } else {
  468. for (i=0; i<count; i++) {
  469. char c = sci_in(port, SCxRDR);
  470. status = sci_in(port, SCxSR);
  471. #if defined(CONFIG_CPU_SH3)
  472. /* Skip "chars" during break */
  473. if (sci_port->break_flag) {
  474. if ((c == 0) &&
  475. (status & SCxSR_FER(port))) {
  476. count--; i--;
  477. continue;
  478. }
  479. /* Nonzero => end-of-break */
  480. pr_debug("scif: debounce<%02x>\n", c);
  481. sci_port->break_flag = 0;
  482. if (STEPFN(c)) {
  483. count--; i--;
  484. continue;
  485. }
  486. }
  487. #endif /* CONFIG_CPU_SH3 */
  488. if (uart_handle_sysrq_char(port, c)) {
  489. count--; i--;
  490. continue;
  491. }
  492. /* Store data and status */
  493. if (status&SCxSR_FER(port)) {
  494. flag = TTY_FRAME;
  495. pr_debug("sci: frame error\n");
  496. } else if (status&SCxSR_PER(port)) {
  497. flag = TTY_PARITY;
  498. pr_debug("sci: parity error\n");
  499. } else
  500. flag = TTY_NORMAL;
  501. tty_insert_flip_char(tty, c, flag);
  502. }
  503. }
  504. sci_in(port, SCxSR); /* dummy read */
  505. sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  506. copied += count;
  507. port->icount.rx += count;
  508. }
  509. if (copied) {
  510. /* Tell the rest of the system the news. New characters! */
  511. tty_flip_buffer_push(tty);
  512. } else {
  513. sci_in(port, SCxSR); /* dummy read */
  514. sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  515. }
  516. }
  517. #define SCI_BREAK_JIFFIES (HZ/20)
  518. /* The sci generates interrupts during the break,
  519. * 1 per millisecond or so during the break period, for 9600 baud.
  520. * So dont bother disabling interrupts.
  521. * But dont want more than 1 break event.
  522. * Use a kernel timer to periodically poll the rx line until
  523. * the break is finished.
  524. */
  525. static void sci_schedule_break_timer(struct sci_port *port)
  526. {
  527. port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
  528. add_timer(&port->break_timer);
  529. }
  530. /* Ensure that two consecutive samples find the break over. */
  531. static void sci_break_timer(unsigned long data)
  532. {
  533. struct sci_port *port = (struct sci_port *)data;
  534. if (sci_rxd_in(&port->port) == 0) {
  535. port->break_flag = 1;
  536. sci_schedule_break_timer(port);
  537. } else if (port->break_flag == 1) {
  538. /* break is over. */
  539. port->break_flag = 2;
  540. sci_schedule_break_timer(port);
  541. } else
  542. port->break_flag = 0;
  543. }
  544. static inline int sci_handle_errors(struct uart_port *port)
  545. {
  546. int copied = 0;
  547. unsigned short status = sci_in(port, SCxSR);
  548. struct tty_struct *tty = port->info->tty;
  549. if (status & SCxSR_ORER(port)) {
  550. /* overrun error */
  551. if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
  552. copied++;
  553. pr_debug("sci: overrun error\n");
  554. }
  555. if (status & SCxSR_FER(port)) {
  556. if (sci_rxd_in(port) == 0) {
  557. /* Notify of BREAK */
  558. struct sci_port *sci_port = (struct sci_port *)port;
  559. if (!sci_port->break_flag) {
  560. sci_port->break_flag = 1;
  561. sci_schedule_break_timer(sci_port);
  562. /* Do sysrq handling. */
  563. if (uart_handle_break(port))
  564. return 0;
  565. pr_debug("sci: BREAK detected\n");
  566. if (tty_insert_flip_char(tty, 0, TTY_BREAK))
  567. copied++;
  568. }
  569. } else {
  570. /* frame error */
  571. if (tty_insert_flip_char(tty, 0, TTY_FRAME))
  572. copied++;
  573. pr_debug("sci: frame error\n");
  574. }
  575. }
  576. if (status & SCxSR_PER(port)) {
  577. /* parity error */
  578. if (tty_insert_flip_char(tty, 0, TTY_PARITY))
  579. copied++;
  580. pr_debug("sci: parity error\n");
  581. }
  582. if (copied)
  583. tty_flip_buffer_push(tty);
  584. return copied;
  585. }
  586. static inline int sci_handle_breaks(struct uart_port *port)
  587. {
  588. int copied = 0;
  589. unsigned short status = sci_in(port, SCxSR);
  590. struct tty_struct *tty = port->info->tty;
  591. struct sci_port *s = &sci_ports[port->line];
  592. if (uart_handle_break(port))
  593. return 0;
  594. if (!s->break_flag && status & SCxSR_BRK(port)) {
  595. #if defined(CONFIG_CPU_SH3)
  596. /* Debounce break */
  597. s->break_flag = 1;
  598. #endif
  599. /* Notify of BREAK */
  600. if (tty_insert_flip_char(tty, 0, TTY_BREAK))
  601. copied++;
  602. pr_debug("sci: BREAK detected\n");
  603. }
  604. #if defined(SCIF_ORER)
  605. /* XXX: Handle SCIF overrun error */
  606. if (port->type == PORT_SCIF && (sci_in(port, SCLSR) & SCIF_ORER) != 0) {
  607. sci_out(port, SCLSR, 0);
  608. if (tty_insert_flip_char(tty, 0, TTY_OVERRUN)) {
  609. copied++;
  610. pr_debug("sci: overrun error\n");
  611. }
  612. }
  613. #endif
  614. if (copied)
  615. tty_flip_buffer_push(tty);
  616. return copied;
  617. }
  618. static irqreturn_t sci_rx_interrupt(int irq, void *port)
  619. {
  620. /* I think sci_receive_chars has to be called irrespective
  621. * of whether the I_IXOFF is set, otherwise, how is the interrupt
  622. * to be disabled?
  623. */
  624. sci_receive_chars(port);
  625. return IRQ_HANDLED;
  626. }
  627. static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
  628. {
  629. struct uart_port *port = ptr;
  630. spin_lock_irq(&port->lock);
  631. sci_transmit_chars(port);
  632. spin_unlock_irq(&port->lock);
  633. return IRQ_HANDLED;
  634. }
  635. static irqreturn_t sci_er_interrupt(int irq, void *ptr)
  636. {
  637. struct uart_port *port = ptr;
  638. /* Handle errors */
  639. if (port->type == PORT_SCI) {
  640. if (sci_handle_errors(port)) {
  641. /* discard character in rx buffer */
  642. sci_in(port, SCxSR);
  643. sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  644. }
  645. } else {
  646. #if defined(SCIF_ORER)
  647. if((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
  648. struct tty_struct *tty = port->info->tty;
  649. sci_out(port, SCLSR, 0);
  650. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  651. tty_flip_buffer_push(tty);
  652. pr_debug("scif: overrun error\n");
  653. }
  654. #endif
  655. sci_rx_interrupt(irq, ptr);
  656. }
  657. sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
  658. /* Kick the transmission */
  659. sci_tx_interrupt(irq, ptr);
  660. return IRQ_HANDLED;
  661. }
  662. static irqreturn_t sci_br_interrupt(int irq, void *ptr)
  663. {
  664. struct uart_port *port = ptr;
  665. /* Handle BREAKs */
  666. sci_handle_breaks(port);
  667. sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
  668. return IRQ_HANDLED;
  669. }
  670. static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
  671. {
  672. unsigned short ssr_status, scr_status;
  673. struct uart_port *port = ptr;
  674. ssr_status = sci_in(port,SCxSR);
  675. scr_status = sci_in(port,SCSCR);
  676. /* Tx Interrupt */
  677. if ((ssr_status & 0x0020) && (scr_status & 0x0080))
  678. sci_tx_interrupt(irq, ptr);
  679. /* Rx Interrupt */
  680. if ((ssr_status & 0x0002) && (scr_status & 0x0040))
  681. sci_rx_interrupt(irq, ptr);
  682. /* Error Interrupt */
  683. if ((ssr_status & 0x0080) && (scr_status & 0x0400))
  684. sci_er_interrupt(irq, ptr);
  685. /* Break Interrupt */
  686. if ((ssr_status & 0x0010) && (scr_status & 0x0200))
  687. sci_br_interrupt(irq, ptr);
  688. return IRQ_HANDLED;
  689. }
  690. #ifdef CONFIG_CPU_FREQ
  691. /*
  692. * Here we define a transistion notifier so that we can update all of our
  693. * ports' baud rate when the peripheral clock changes.
  694. */
  695. static int sci_notifier(struct notifier_block *self,
  696. unsigned long phase, void *p)
  697. {
  698. struct cpufreq_freqs *freqs = p;
  699. int i;
  700. if ((phase == CPUFREQ_POSTCHANGE) ||
  701. (phase == CPUFREQ_RESUMECHANGE)){
  702. for (i = 0; i < SCI_NPORTS; i++) {
  703. struct uart_port *port = &sci_ports[i].port;
  704. struct clk *clk;
  705. /*
  706. * Update the uartclk per-port if frequency has
  707. * changed, since it will no longer necessarily be
  708. * consistent with the old frequency.
  709. *
  710. * Really we want to be able to do something like
  711. * uart_change_speed() or something along those lines
  712. * here to implicitly reset the per-port baud rate..
  713. *
  714. * Clean this up later..
  715. */
  716. clk = clk_get(NULL, "module_clk");
  717. port->uartclk = clk_get_rate(clk) * 16;
  718. clk_put(clk);
  719. }
  720. printk(KERN_INFO "%s: got a postchange notification "
  721. "for cpu %d (old %d, new %d)\n",
  722. __FUNCTION__, freqs->cpu, freqs->old, freqs->new);
  723. }
  724. return NOTIFY_OK;
  725. }
  726. static struct notifier_block sci_nb = { &sci_notifier, NULL, 0 };
  727. #endif /* CONFIG_CPU_FREQ */
  728. static int sci_request_irq(struct sci_port *port)
  729. {
  730. int i;
  731. irqreturn_t (*handlers[4])(int irq, void *ptr) = {
  732. sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
  733. sci_br_interrupt,
  734. };
  735. const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
  736. "SCI Transmit Data Empty", "SCI Break" };
  737. if (port->irqs[0] == port->irqs[1]) {
  738. if (!port->irqs[0]) {
  739. printk(KERN_ERR "sci: Cannot allocate irq.(IRQ=0)\n");
  740. return -ENODEV;
  741. }
  742. if (request_irq(port->irqs[0], sci_mpxed_interrupt,
  743. IRQF_DISABLED, "sci", port)) {
  744. printk(KERN_ERR "sci: Cannot allocate irq.\n");
  745. return -ENODEV;
  746. }
  747. } else {
  748. for (i = 0; i < ARRAY_SIZE(handlers); i++) {
  749. if (!port->irqs[i])
  750. continue;
  751. if (request_irq(port->irqs[i], handlers[i],
  752. IRQF_DISABLED, desc[i], port)) {
  753. printk(KERN_ERR "sci: Cannot allocate irq.\n");
  754. return -ENODEV;
  755. }
  756. }
  757. }
  758. return 0;
  759. }
  760. static void sci_free_irq(struct sci_port *port)
  761. {
  762. int i;
  763. if (port->irqs[0] == port->irqs[1]) {
  764. if (!port->irqs[0])
  765. printk("sci: sci_free_irq error\n");
  766. else
  767. free_irq(port->irqs[0], port);
  768. } else {
  769. for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
  770. if (!port->irqs[i])
  771. continue;
  772. free_irq(port->irqs[i], port);
  773. }
  774. }
  775. }
  776. static unsigned int sci_tx_empty(struct uart_port *port)
  777. {
  778. /* Can't detect */
  779. return TIOCSER_TEMT;
  780. }
  781. static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
  782. {
  783. /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
  784. /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
  785. /* If you have signals for DTR and DCD, please implement here. */
  786. }
  787. static unsigned int sci_get_mctrl(struct uart_port *port)
  788. {
  789. /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
  790. and CTS/RTS */
  791. return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
  792. }
  793. static void sci_start_tx(struct uart_port *port)
  794. {
  795. unsigned short ctrl;
  796. /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
  797. ctrl = sci_in(port, SCSCR);
  798. ctrl |= SCI_CTRL_FLAGS_TIE;
  799. sci_out(port, SCSCR, ctrl);
  800. }
  801. static void sci_stop_tx(struct uart_port *port)
  802. {
  803. unsigned short ctrl;
  804. /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
  805. ctrl = sci_in(port, SCSCR);
  806. ctrl &= ~SCI_CTRL_FLAGS_TIE;
  807. sci_out(port, SCSCR, ctrl);
  808. }
  809. static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
  810. {
  811. unsigned short ctrl;
  812. /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
  813. ctrl = sci_in(port, SCSCR);
  814. ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
  815. sci_out(port, SCSCR, ctrl);
  816. }
  817. static void sci_stop_rx(struct uart_port *port)
  818. {
  819. unsigned short ctrl;
  820. /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
  821. ctrl = sci_in(port, SCSCR);
  822. ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
  823. sci_out(port, SCSCR, ctrl);
  824. }
  825. static void sci_enable_ms(struct uart_port *port)
  826. {
  827. /* Nothing here yet .. */
  828. }
  829. static void sci_break_ctl(struct uart_port *port, int break_state)
  830. {
  831. /* Nothing here yet .. */
  832. }
  833. static int sci_startup(struct uart_port *port)
  834. {
  835. struct sci_port *s = &sci_ports[port->line];
  836. if (s->enable)
  837. s->enable(port);
  838. #if defined(CONFIG_SUPERH) && !defined(CONFIG_SUPERH64)
  839. s->clk = clk_get(NULL, "module_clk");
  840. #endif
  841. sci_request_irq(s);
  842. sci_start_tx(port);
  843. sci_start_rx(port, 1);
  844. return 0;
  845. }
  846. static void sci_shutdown(struct uart_port *port)
  847. {
  848. struct sci_port *s = &sci_ports[port->line];
  849. sci_stop_rx(port);
  850. sci_stop_tx(port);
  851. sci_free_irq(s);
  852. if (s->disable)
  853. s->disable(port);
  854. #if defined(CONFIG_SUPERH) && !defined(CONFIG_SUPERH64)
  855. clk_put(s->clk);
  856. s->clk = NULL;
  857. #endif
  858. }
  859. static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
  860. struct ktermios *old)
  861. {
  862. struct sci_port *s = &sci_ports[port->line];
  863. unsigned int status, baud, smr_val;
  864. int t;
  865. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  866. switch (baud) {
  867. case 0:
  868. t = -1;
  869. break;
  870. default:
  871. {
  872. #if defined(CONFIG_SUPERH) && !defined(CONFIG_SUPERH64)
  873. t = SCBRR_VALUE(baud, clk_get_rate(s->clk));
  874. #else
  875. t = SCBRR_VALUE(baud);
  876. #endif
  877. break;
  878. }
  879. }
  880. do {
  881. status = sci_in(port, SCxSR);
  882. } while (!(status & SCxSR_TEND(port)));
  883. sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
  884. #if !defined(SCI_ONLY)
  885. if (port->type == PORT_SCIF)
  886. sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
  887. #endif
  888. smr_val = sci_in(port, SCSMR) & 3;
  889. if ((termios->c_cflag & CSIZE) == CS7)
  890. smr_val |= 0x40;
  891. if (termios->c_cflag & PARENB)
  892. smr_val |= 0x20;
  893. if (termios->c_cflag & PARODD)
  894. smr_val |= 0x30;
  895. if (termios->c_cflag & CSTOPB)
  896. smr_val |= 0x08;
  897. uart_update_timeout(port, termios->c_cflag, baud);
  898. sci_out(port, SCSMR, smr_val);
  899. if (t > 0) {
  900. if(t >= 256) {
  901. sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
  902. t >>= 2;
  903. } else {
  904. sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
  905. }
  906. sci_out(port, SCBRR, t);
  907. udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
  908. }
  909. if (likely(s->init_pins))
  910. s->init_pins(port, termios->c_cflag);
  911. sci_out(port, SCSCR, SCSCR_INIT(port));
  912. if ((termios->c_cflag & CREAD) != 0)
  913. sci_start_rx(port,0);
  914. }
  915. static const char *sci_type(struct uart_port *port)
  916. {
  917. switch (port->type) {
  918. case PORT_SCI: return "sci";
  919. case PORT_SCIF: return "scif";
  920. case PORT_IRDA: return "irda";
  921. }
  922. return 0;
  923. }
  924. static void sci_release_port(struct uart_port *port)
  925. {
  926. /* Nothing here yet .. */
  927. }
  928. static int sci_request_port(struct uart_port *port)
  929. {
  930. /* Nothing here yet .. */
  931. return 0;
  932. }
  933. static void sci_config_port(struct uart_port *port, int flags)
  934. {
  935. struct sci_port *s = &sci_ports[port->line];
  936. port->type = s->type;
  937. switch (port->type) {
  938. case PORT_SCI:
  939. s->init_pins = sci_init_pins_sci;
  940. break;
  941. case PORT_SCIF:
  942. s->init_pins = sci_init_pins_scif;
  943. break;
  944. case PORT_IRDA:
  945. s->init_pins = sci_init_pins_irda;
  946. break;
  947. }
  948. #if defined(CONFIG_CPU_SUBTYPE_SH5_101) || defined(CONFIG_CPU_SUBTYPE_SH5_103)
  949. if (port->mapbase == 0)
  950. port->mapbase = onchip_remap(SCIF_ADDR_SH5, 1024, "SCIF");
  951. port->membase = (void __iomem *)port->mapbase;
  952. #endif
  953. }
  954. static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
  955. {
  956. struct sci_port *s = &sci_ports[port->line];
  957. if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > NR_IRQS)
  958. return -EINVAL;
  959. if (ser->baud_base < 2400)
  960. /* No paper tape reader for Mitch.. */
  961. return -EINVAL;
  962. return 0;
  963. }
  964. static struct uart_ops sci_uart_ops = {
  965. .tx_empty = sci_tx_empty,
  966. .set_mctrl = sci_set_mctrl,
  967. .get_mctrl = sci_get_mctrl,
  968. .start_tx = sci_start_tx,
  969. .stop_tx = sci_stop_tx,
  970. .stop_rx = sci_stop_rx,
  971. .enable_ms = sci_enable_ms,
  972. .break_ctl = sci_break_ctl,
  973. .startup = sci_startup,
  974. .shutdown = sci_shutdown,
  975. .set_termios = sci_set_termios,
  976. .type = sci_type,
  977. .release_port = sci_release_port,
  978. .request_port = sci_request_port,
  979. .config_port = sci_config_port,
  980. .verify_port = sci_verify_port,
  981. };
  982. static void __init sci_init_ports(void)
  983. {
  984. static int first = 1;
  985. int i;
  986. if (!first)
  987. return;
  988. first = 0;
  989. for (i = 0; i < SCI_NPORTS; i++) {
  990. sci_ports[i].port.ops = &sci_uart_ops;
  991. sci_ports[i].port.iotype = UPIO_MEM;
  992. sci_ports[i].port.line = i;
  993. sci_ports[i].port.fifosize = 1;
  994. #if defined(__H8300H__) || defined(__H8300S__)
  995. #ifdef __H8300S__
  996. sci_ports[i].enable = h8300_sci_enable;
  997. sci_ports[i].disable = h8300_sci_disable;
  998. #endif
  999. sci_ports[i].port.uartclk = CONFIG_CPU_CLOCK;
  1000. #elif defined(CONFIG_SUPERH64)
  1001. sci_ports[i].port.uartclk = current_cpu_data.module_clock * 16;
  1002. #else
  1003. /*
  1004. * XXX: We should use a proper SCI/SCIF clock
  1005. */
  1006. {
  1007. struct clk *clk = clk_get(NULL, "module_clk");
  1008. sci_ports[i].port.uartclk = clk_get_rate(clk) * 16;
  1009. clk_put(clk);
  1010. }
  1011. #endif
  1012. sci_ports[i].break_timer.data = (unsigned long)&sci_ports[i];
  1013. sci_ports[i].break_timer.function = sci_break_timer;
  1014. init_timer(&sci_ports[i].break_timer);
  1015. }
  1016. }
  1017. int __init early_sci_setup(struct uart_port *port)
  1018. {
  1019. if (unlikely(port->line > SCI_NPORTS))
  1020. return -ENODEV;
  1021. sci_init_ports();
  1022. sci_ports[port->line].port.membase = port->membase;
  1023. sci_ports[port->line].port.mapbase = port->mapbase;
  1024. sci_ports[port->line].port.type = port->type;
  1025. return 0;
  1026. }
  1027. #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
  1028. /*
  1029. * Print a string to the serial port trying not to disturb
  1030. * any possible real use of the port...
  1031. */
  1032. static void serial_console_write(struct console *co, const char *s,
  1033. unsigned count)
  1034. {
  1035. put_string(serial_console_port, s, count);
  1036. }
  1037. static int __init serial_console_setup(struct console *co, char *options)
  1038. {
  1039. struct uart_port *port;
  1040. int baud = 115200;
  1041. int bits = 8;
  1042. int parity = 'n';
  1043. int flow = 'n';
  1044. int ret;
  1045. /*
  1046. * Check whether an invalid uart number has been specified, and
  1047. * if so, search for the first available port that does have
  1048. * console support.
  1049. */
  1050. if (co->index >= SCI_NPORTS)
  1051. co->index = 0;
  1052. serial_console_port = &sci_ports[co->index];
  1053. port = &serial_console_port->port;
  1054. /*
  1055. * Also need to check port->type, we don't actually have any
  1056. * UPIO_PORT ports, but uart_report_port() handily misreports
  1057. * it anyways if we don't have a port available by the time this is
  1058. * called.
  1059. */
  1060. if (!port->type)
  1061. return -ENODEV;
  1062. if (!port->membase || !port->mapbase)
  1063. return -ENODEV;
  1064. port->type = serial_console_port->type;
  1065. #if defined(CONFIG_SUPERH) && !defined(CONFIG_SUPERH64)
  1066. if (!serial_console_port->clk)
  1067. serial_console_port->clk = clk_get(NULL, "module_clk");
  1068. #endif
  1069. if (port->flags & UPF_IOREMAP)
  1070. sci_config_port(port, 0);
  1071. if (serial_console_port->enable)
  1072. serial_console_port->enable(port);
  1073. if (options)
  1074. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1075. ret = uart_set_options(port, co, baud, parity, bits, flow);
  1076. #if defined(__H8300H__) || defined(__H8300S__)
  1077. /* disable rx interrupt */
  1078. if (ret == 0)
  1079. sci_stop_rx(port);
  1080. #endif
  1081. return ret;
  1082. }
  1083. static struct console serial_console = {
  1084. .name = "ttySC",
  1085. .device = uart_console_device,
  1086. .write = serial_console_write,
  1087. .setup = serial_console_setup,
  1088. .flags = CON_PRINTBUFFER,
  1089. .index = -1,
  1090. .data = &sci_uart_driver,
  1091. };
  1092. static int __init sci_console_init(void)
  1093. {
  1094. sci_init_ports();
  1095. register_console(&serial_console);
  1096. return 0;
  1097. }
  1098. console_initcall(sci_console_init);
  1099. #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
  1100. #ifdef CONFIG_SH_KGDB_CONSOLE
  1101. /*
  1102. * FIXME: Most of this can go away.. at the moment, we rely on
  1103. * arch/sh/kernel/setup.c to do the command line parsing for kgdb, though
  1104. * most of that can easily be done here instead.
  1105. *
  1106. * For the time being, just accept the values that were parsed earlier..
  1107. */
  1108. static void __init kgdb_console_get_options(struct uart_port *port, int *baud,
  1109. int *parity, int *bits)
  1110. {
  1111. *baud = kgdb_baud;
  1112. *parity = tolower(kgdb_parity);
  1113. *bits = kgdb_bits - '0';
  1114. }
  1115. /*
  1116. * The naming here is somewhat misleading, since kgdb_console_setup() takes
  1117. * care of the early-on initialization for kgdb, regardless of whether we
  1118. * actually use kgdb as a console or not.
  1119. *
  1120. * On the plus side, this lets us kill off the old kgdb_sci_setup() nonsense.
  1121. */
  1122. int __init kgdb_console_setup(struct console *co, char *options)
  1123. {
  1124. struct uart_port *port = &sci_ports[kgdb_portnum].port;
  1125. int baud = 38400;
  1126. int bits = 8;
  1127. int parity = 'n';
  1128. int flow = 'n';
  1129. if (co->index != kgdb_portnum)
  1130. co->index = kgdb_portnum;
  1131. kgdb_sci_port = &sci_ports[co->index];
  1132. port = &kgdb_sci_port->port;
  1133. /*
  1134. * Also need to check port->type, we don't actually have any
  1135. * UPIO_PORT ports, but uart_report_port() handily misreports
  1136. * it anyways if we don't have a port available by the time this is
  1137. * called.
  1138. */
  1139. if (!port->type)
  1140. return -ENODEV;
  1141. if (!port->membase || !port->mapbase)
  1142. return -ENODEV;
  1143. if (options)
  1144. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1145. else
  1146. kgdb_console_get_options(port, &baud, &parity, &bits);
  1147. kgdb_getchar = kgdb_sci_getchar;
  1148. kgdb_putchar = kgdb_sci_putchar;
  1149. return uart_set_options(port, co, baud, parity, bits, flow);
  1150. }
  1151. static struct console kgdb_console = {
  1152. .name = "ttySC",
  1153. .device = uart_console_device,
  1154. .write = kgdb_console_write,
  1155. .setup = kgdb_console_setup,
  1156. .flags = CON_PRINTBUFFER,
  1157. .index = -1,
  1158. .data = &sci_uart_driver,
  1159. };
  1160. /* Register the KGDB console so we get messages (d'oh!) */
  1161. static int __init kgdb_console_init(void)
  1162. {
  1163. sci_init_ports();
  1164. register_console(&kgdb_console);
  1165. return 0;
  1166. }
  1167. console_initcall(kgdb_console_init);
  1168. #endif /* CONFIG_SH_KGDB_CONSOLE */
  1169. #if defined(CONFIG_SH_KGDB_CONSOLE)
  1170. #define SCI_CONSOLE &kgdb_console
  1171. #elif defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
  1172. #define SCI_CONSOLE &serial_console
  1173. #else
  1174. #define SCI_CONSOLE 0
  1175. #endif
  1176. static char banner[] __initdata =
  1177. KERN_INFO "SuperH SCI(F) driver initialized\n";
  1178. static struct uart_driver sci_uart_driver = {
  1179. .owner = THIS_MODULE,
  1180. .driver_name = "sci",
  1181. .dev_name = "ttySC",
  1182. .major = SCI_MAJOR,
  1183. .minor = SCI_MINOR_START,
  1184. .nr = SCI_NPORTS,
  1185. .cons = SCI_CONSOLE,
  1186. };
  1187. /*
  1188. * Register a set of serial devices attached to a platform device. The
  1189. * list is terminated with a zero flags entry, which means we expect
  1190. * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
  1191. * remapping (such as sh64) should also set UPF_IOREMAP.
  1192. */
  1193. static int __devinit sci_probe(struct platform_device *dev)
  1194. {
  1195. struct plat_sci_port *p = dev->dev.platform_data;
  1196. int i;
  1197. for (i = 0; p && p->flags != 0; p++, i++) {
  1198. struct sci_port *sciport = &sci_ports[i];
  1199. /* Sanity check */
  1200. if (unlikely(i == SCI_NPORTS)) {
  1201. dev_notice(&dev->dev, "Attempting to register port "
  1202. "%d when only %d are available.\n",
  1203. i+1, SCI_NPORTS);
  1204. dev_notice(&dev->dev, "Consider bumping "
  1205. "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
  1206. break;
  1207. }
  1208. sciport->port.mapbase = p->mapbase;
  1209. /*
  1210. * For the simple (and majority of) cases where we don't need
  1211. * to do any remapping, just cast the cookie directly.
  1212. */
  1213. if (p->mapbase && !p->membase && !(p->flags & UPF_IOREMAP))
  1214. p->membase = (void __iomem *)p->mapbase;
  1215. sciport->port.membase = p->membase;
  1216. sciport->port.irq = p->irqs[SCIx_TXI_IRQ];
  1217. sciport->port.flags = p->flags;
  1218. sciport->port.dev = &dev->dev;
  1219. sciport->type = sciport->port.type = p->type;
  1220. memcpy(&sciport->irqs, &p->irqs, sizeof(p->irqs));
  1221. uart_add_one_port(&sci_uart_driver, &sciport->port);
  1222. }
  1223. #if defined(CONFIG_SH_KGDB) && !defined(CONFIG_SH_KGDB_CONSOLE)
  1224. kgdb_sci_port = &sci_ports[kgdb_portnum];
  1225. kgdb_getchar = kgdb_sci_getchar;
  1226. kgdb_putchar = kgdb_sci_putchar;
  1227. #endif
  1228. #ifdef CONFIG_CPU_FREQ
  1229. cpufreq_register_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER);
  1230. dev_info(&dev->dev, "CPU frequency notifier registered\n");
  1231. #endif
  1232. #ifdef CONFIG_SH_STANDARD_BIOS
  1233. sh_bios_gdb_detach();
  1234. #endif
  1235. return 0;
  1236. }
  1237. static int __devexit sci_remove(struct platform_device *dev)
  1238. {
  1239. int i;
  1240. for (i = 0; i < SCI_NPORTS; i++)
  1241. uart_remove_one_port(&sci_uart_driver, &sci_ports[i].port);
  1242. return 0;
  1243. }
  1244. static int sci_suspend(struct platform_device *dev, pm_message_t state)
  1245. {
  1246. int i;
  1247. for (i = 0; i < SCI_NPORTS; i++) {
  1248. struct sci_port *p = &sci_ports[i];
  1249. if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
  1250. uart_suspend_port(&sci_uart_driver, &p->port);
  1251. }
  1252. return 0;
  1253. }
  1254. static int sci_resume(struct platform_device *dev)
  1255. {
  1256. int i;
  1257. for (i = 0; i < SCI_NPORTS; i++) {
  1258. struct sci_port *p = &sci_ports[i];
  1259. if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
  1260. uart_resume_port(&sci_uart_driver, &p->port);
  1261. }
  1262. return 0;
  1263. }
  1264. static struct platform_driver sci_driver = {
  1265. .probe = sci_probe,
  1266. .remove = __devexit_p(sci_remove),
  1267. .suspend = sci_suspend,
  1268. .resume = sci_resume,
  1269. .driver = {
  1270. .name = "sh-sci",
  1271. .owner = THIS_MODULE,
  1272. },
  1273. };
  1274. static int __init sci_init(void)
  1275. {
  1276. int ret;
  1277. printk(banner);
  1278. sci_init_ports();
  1279. ret = uart_register_driver(&sci_uart_driver);
  1280. if (likely(ret == 0)) {
  1281. ret = platform_driver_register(&sci_driver);
  1282. if (unlikely(ret))
  1283. uart_unregister_driver(&sci_uart_driver);
  1284. }
  1285. return ret;
  1286. }
  1287. static void __exit sci_exit(void)
  1288. {
  1289. platform_driver_unregister(&sci_driver);
  1290. uart_unregister_driver(&sci_uart_driver);
  1291. }
  1292. module_init(sci_init);
  1293. module_exit(sci_exit);
  1294. MODULE_LICENSE("GPL");
  1295. MODULE_ALIAS("platform:sh-sci");