sh-sci.c 35 KB

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