sh-sci.c 35 KB

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