sh-sci.c 39 KB

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