sh-sci.c 34 KB

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