sh-sci.c 41 KB

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