sh-sci.c 37 KB

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