sh-sci.c 37 KB

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