sh-sci.c 33 KB

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