sh-sci.c 33 KB

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