sh-sci.c 36 KB

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