sh-sci.c 37 KB

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