sh-sci.c 35 KB

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