sh-sci.c 37 KB

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