sh-sci.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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. unsigned long flags;
  560. spin_lock_irqsave(&port->lock, flags);
  561. sci_transmit_chars(port);
  562. spin_unlock_irqrestore(&port->lock, flags);
  563. return IRQ_HANDLED;
  564. }
  565. static irqreturn_t sci_er_interrupt(int irq, void *ptr)
  566. {
  567. struct uart_port *port = ptr;
  568. /* Handle errors */
  569. if (port->type == PORT_SCI) {
  570. if (sci_handle_errors(port)) {
  571. /* discard character in rx buffer */
  572. sci_in(port, SCxSR);
  573. sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  574. }
  575. } else {
  576. sci_handle_fifo_overrun(port);
  577. sci_rx_interrupt(irq, ptr);
  578. }
  579. sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
  580. /* Kick the transmission */
  581. sci_tx_interrupt(irq, ptr);
  582. return IRQ_HANDLED;
  583. }
  584. static irqreturn_t sci_br_interrupt(int irq, void *ptr)
  585. {
  586. struct uart_port *port = ptr;
  587. /* Handle BREAKs */
  588. sci_handle_breaks(port);
  589. sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
  590. return IRQ_HANDLED;
  591. }
  592. static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
  593. {
  594. unsigned short ssr_status, scr_status, err_enabled;
  595. struct uart_port *port = ptr;
  596. irqreturn_t ret = IRQ_NONE;
  597. ssr_status = sci_in(port, SCxSR);
  598. scr_status = sci_in(port, SCSCR);
  599. err_enabled = scr_status & (SCI_CTRL_FLAGS_REIE | SCI_CTRL_FLAGS_RIE);
  600. /* Tx Interrupt */
  601. if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCI_CTRL_FLAGS_TIE))
  602. ret = sci_tx_interrupt(irq, ptr);
  603. /* Rx Interrupt */
  604. if ((ssr_status & SCxSR_RDxF(port)) && (scr_status & SCI_CTRL_FLAGS_RIE))
  605. ret = sci_rx_interrupt(irq, ptr);
  606. /* Error Interrupt */
  607. if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
  608. ret = sci_er_interrupt(irq, ptr);
  609. /* Break Interrupt */
  610. if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
  611. ret = sci_br_interrupt(irq, ptr);
  612. return ret;
  613. }
  614. #ifdef CONFIG_HAVE_CLK
  615. /*
  616. * Here we define a transistion notifier so that we can update all of our
  617. * ports' baud rate when the peripheral clock changes.
  618. */
  619. static int sci_notifier(struct notifier_block *self,
  620. unsigned long phase, void *p)
  621. {
  622. struct sh_sci_priv *priv = container_of(self,
  623. struct sh_sci_priv, clk_nb);
  624. struct sci_port *sci_port;
  625. unsigned long flags;
  626. if ((phase == CPUFREQ_POSTCHANGE) ||
  627. (phase == CPUFREQ_RESUMECHANGE)) {
  628. spin_lock_irqsave(&priv->lock, flags);
  629. list_for_each_entry(sci_port, &priv->ports, node)
  630. sci_port->port.uartclk = clk_get_rate(sci_port->dclk);
  631. spin_unlock_irqrestore(&priv->lock, flags);
  632. }
  633. return NOTIFY_OK;
  634. }
  635. static void sci_clk_enable(struct uart_port *port)
  636. {
  637. struct sci_port *sci_port = to_sci_port(port);
  638. clk_enable(sci_port->dclk);
  639. sci_port->port.uartclk = clk_get_rate(sci_port->dclk);
  640. if (sci_port->iclk)
  641. clk_enable(sci_port->iclk);
  642. }
  643. static void sci_clk_disable(struct uart_port *port)
  644. {
  645. struct sci_port *sci_port = to_sci_port(port);
  646. if (sci_port->iclk)
  647. clk_disable(sci_port->iclk);
  648. clk_disable(sci_port->dclk);
  649. }
  650. #endif
  651. static int sci_request_irq(struct sci_port *port)
  652. {
  653. int i;
  654. irqreturn_t (*handlers[4])(int irq, void *ptr) = {
  655. sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
  656. sci_br_interrupt,
  657. };
  658. const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
  659. "SCI Transmit Data Empty", "SCI Break" };
  660. if (port->irqs[0] == port->irqs[1]) {
  661. if (unlikely(!port->irqs[0]))
  662. return -ENODEV;
  663. if (request_irq(port->irqs[0], sci_mpxed_interrupt,
  664. IRQF_DISABLED, "sci", port)) {
  665. dev_err(port->port.dev, "Can't allocate IRQ\n");
  666. return -ENODEV;
  667. }
  668. } else {
  669. for (i = 0; i < ARRAY_SIZE(handlers); i++) {
  670. if (unlikely(!port->irqs[i]))
  671. continue;
  672. if (request_irq(port->irqs[i], handlers[i],
  673. IRQF_DISABLED, desc[i], port)) {
  674. dev_err(port->port.dev, "Can't allocate IRQ\n");
  675. return -ENODEV;
  676. }
  677. }
  678. }
  679. return 0;
  680. }
  681. static void sci_free_irq(struct sci_port *port)
  682. {
  683. int i;
  684. if (port->irqs[0] == port->irqs[1])
  685. free_irq(port->irqs[0], port);
  686. else {
  687. for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
  688. if (!port->irqs[i])
  689. continue;
  690. free_irq(port->irqs[i], port);
  691. }
  692. }
  693. }
  694. static unsigned int sci_tx_empty(struct uart_port *port)
  695. {
  696. /* Can't detect */
  697. return TIOCSER_TEMT;
  698. }
  699. static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
  700. {
  701. /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
  702. /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
  703. /* If you have signals for DTR and DCD, please implement here. */
  704. }
  705. static unsigned int sci_get_mctrl(struct uart_port *port)
  706. {
  707. /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
  708. and CTS/RTS */
  709. return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
  710. }
  711. static void sci_start_tx(struct uart_port *port)
  712. {
  713. unsigned short ctrl;
  714. /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
  715. ctrl = sci_in(port, SCSCR);
  716. ctrl |= SCI_CTRL_FLAGS_TIE;
  717. sci_out(port, SCSCR, ctrl);
  718. }
  719. static void sci_stop_tx(struct uart_port *port)
  720. {
  721. unsigned short ctrl;
  722. /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
  723. ctrl = sci_in(port, SCSCR);
  724. ctrl &= ~SCI_CTRL_FLAGS_TIE;
  725. sci_out(port, SCSCR, ctrl);
  726. }
  727. static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
  728. {
  729. unsigned short ctrl;
  730. /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
  731. ctrl = sci_in(port, SCSCR);
  732. ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
  733. sci_out(port, SCSCR, ctrl);
  734. }
  735. static void sci_stop_rx(struct uart_port *port)
  736. {
  737. unsigned short ctrl;
  738. /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
  739. ctrl = sci_in(port, SCSCR);
  740. ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
  741. sci_out(port, SCSCR, ctrl);
  742. }
  743. static void sci_enable_ms(struct uart_port *port)
  744. {
  745. /* Nothing here yet .. */
  746. }
  747. static void sci_break_ctl(struct uart_port *port, int break_state)
  748. {
  749. /* Nothing here yet .. */
  750. }
  751. static int sci_startup(struct uart_port *port)
  752. {
  753. struct sci_port *s = to_sci_port(port);
  754. if (s->enable)
  755. s->enable(port);
  756. sci_request_irq(s);
  757. sci_start_tx(port);
  758. sci_start_rx(port, 1);
  759. return 0;
  760. }
  761. static void sci_shutdown(struct uart_port *port)
  762. {
  763. struct sci_port *s = to_sci_port(port);
  764. sci_stop_rx(port);
  765. sci_stop_tx(port);
  766. sci_free_irq(s);
  767. if (s->disable)
  768. s->disable(port);
  769. }
  770. static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
  771. struct ktermios *old)
  772. {
  773. unsigned int status, baud, smr_val;
  774. int t = -1;
  775. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  776. if (likely(baud))
  777. t = SCBRR_VALUE(baud, port->uartclk);
  778. do {
  779. status = sci_in(port, SCxSR);
  780. } while (!(status & SCxSR_TEND(port)));
  781. sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
  782. if (port->type != PORT_SCI)
  783. sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
  784. smr_val = sci_in(port, SCSMR) & 3;
  785. if ((termios->c_cflag & CSIZE) == CS7)
  786. smr_val |= 0x40;
  787. if (termios->c_cflag & PARENB)
  788. smr_val |= 0x20;
  789. if (termios->c_cflag & PARODD)
  790. smr_val |= 0x30;
  791. if (termios->c_cflag & CSTOPB)
  792. smr_val |= 0x08;
  793. uart_update_timeout(port, termios->c_cflag, baud);
  794. sci_out(port, SCSMR, smr_val);
  795. if (t > 0) {
  796. if (t >= 256) {
  797. sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
  798. t >>= 2;
  799. } else
  800. sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
  801. sci_out(port, SCBRR, t);
  802. udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
  803. }
  804. sci_init_pins(port, termios->c_cflag);
  805. sci_out(port, SCFCR, (termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0);
  806. sci_out(port, SCSCR, SCSCR_INIT(port));
  807. if ((termios->c_cflag & CREAD) != 0)
  808. sci_start_rx(port, 0);
  809. }
  810. static const char *sci_type(struct uart_port *port)
  811. {
  812. switch (port->type) {
  813. case PORT_IRDA:
  814. return "irda";
  815. case PORT_SCI:
  816. return "sci";
  817. case PORT_SCIF:
  818. return "scif";
  819. case PORT_SCIFA:
  820. return "scifa";
  821. }
  822. return NULL;
  823. }
  824. static void sci_release_port(struct uart_port *port)
  825. {
  826. /* Nothing here yet .. */
  827. }
  828. static int sci_request_port(struct uart_port *port)
  829. {
  830. /* Nothing here yet .. */
  831. return 0;
  832. }
  833. static void sci_config_port(struct uart_port *port, int flags)
  834. {
  835. struct sci_port *s = to_sci_port(port);
  836. port->type = s->type;
  837. if (port->membase)
  838. return;
  839. if (port->flags & UPF_IOREMAP) {
  840. port->membase = ioremap_nocache(port->mapbase, 0x40);
  841. if (IS_ERR(port->membase))
  842. dev_err(port->dev, "can't remap port#%d\n", port->line);
  843. } else {
  844. /*
  845. * For the simple (and majority of) cases where we don't
  846. * need to do any remapping, just cast the cookie
  847. * directly.
  848. */
  849. port->membase = (void __iomem *)port->mapbase;
  850. }
  851. }
  852. static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
  853. {
  854. struct sci_port *s = to_sci_port(port);
  855. if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
  856. return -EINVAL;
  857. if (ser->baud_base < 2400)
  858. /* No paper tape reader for Mitch.. */
  859. return -EINVAL;
  860. return 0;
  861. }
  862. static struct uart_ops sci_uart_ops = {
  863. .tx_empty = sci_tx_empty,
  864. .set_mctrl = sci_set_mctrl,
  865. .get_mctrl = sci_get_mctrl,
  866. .start_tx = sci_start_tx,
  867. .stop_tx = sci_stop_tx,
  868. .stop_rx = sci_stop_rx,
  869. .enable_ms = sci_enable_ms,
  870. .break_ctl = sci_break_ctl,
  871. .startup = sci_startup,
  872. .shutdown = sci_shutdown,
  873. .set_termios = sci_set_termios,
  874. .type = sci_type,
  875. .release_port = sci_release_port,
  876. .request_port = sci_request_port,
  877. .config_port = sci_config_port,
  878. .verify_port = sci_verify_port,
  879. #ifdef CONFIG_CONSOLE_POLL
  880. .poll_get_char = sci_poll_get_char,
  881. .poll_put_char = sci_poll_put_char,
  882. #endif
  883. };
  884. static void __devinit sci_init_single(struct platform_device *dev,
  885. struct sci_port *sci_port,
  886. unsigned int index,
  887. struct plat_sci_port *p)
  888. {
  889. sci_port->port.ops = &sci_uart_ops;
  890. sci_port->port.iotype = UPIO_MEM;
  891. sci_port->port.line = index;
  892. sci_port->port.fifosize = 1;
  893. #if defined(__H8300H__) || defined(__H8300S__)
  894. #ifdef __H8300S__
  895. sci_port->enable = h8300_sci_enable;
  896. sci_port->disable = h8300_sci_disable;
  897. #endif
  898. sci_port->port.uartclk = CONFIG_CPU_CLOCK;
  899. #elif defined(CONFIG_HAVE_CLK)
  900. sci_port->iclk = p->clk ? clk_get(&dev->dev, p->clk) : NULL;
  901. sci_port->dclk = clk_get(&dev->dev, "peripheral_clk");
  902. sci_port->enable = sci_clk_enable;
  903. sci_port->disable = sci_clk_disable;
  904. #else
  905. #error "Need a valid uartclk"
  906. #endif
  907. sci_port->break_timer.data = (unsigned long)sci_port;
  908. sci_port->break_timer.function = sci_break_timer;
  909. init_timer(&sci_port->break_timer);
  910. sci_port->port.mapbase = p->mapbase;
  911. sci_port->port.membase = p->membase;
  912. sci_port->port.irq = p->irqs[SCIx_TXI_IRQ];
  913. sci_port->port.flags = p->flags;
  914. sci_port->port.dev = &dev->dev;
  915. sci_port->type = sci_port->port.type = p->type;
  916. memcpy(&sci_port->irqs, &p->irqs, sizeof(p->irqs));
  917. }
  918. #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
  919. static struct tty_driver *serial_console_device(struct console *co, int *index)
  920. {
  921. struct uart_driver *p = &sci_uart_driver;
  922. *index = co->index;
  923. return p->tty_driver;
  924. }
  925. static void serial_console_putchar(struct uart_port *port, int ch)
  926. {
  927. sci_poll_put_char(port, ch);
  928. }
  929. /*
  930. * Print a string to the serial port trying not to disturb
  931. * any possible real use of the port...
  932. */
  933. static void serial_console_write(struct console *co, const char *s,
  934. unsigned count)
  935. {
  936. struct uart_port *port = co->data;
  937. struct sci_port *sci_port = to_sci_port(port);
  938. unsigned short bits;
  939. if (sci_port->enable)
  940. sci_port->enable(port);
  941. uart_console_write(port, s, count, serial_console_putchar);
  942. /* wait until fifo is empty and last bit has been transmitted */
  943. bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
  944. while ((sci_in(port, SCxSR) & bits) != bits)
  945. cpu_relax();
  946. if (sci_port->disable);
  947. sci_port->disable(port);
  948. }
  949. static int __init serial_console_setup(struct console *co, char *options)
  950. {
  951. struct sci_port *sci_port;
  952. struct uart_port *port;
  953. int baud = 115200;
  954. int bits = 8;
  955. int parity = 'n';
  956. int flow = 'n';
  957. int ret;
  958. /*
  959. * Check whether an invalid uart number has been specified, and
  960. * if so, search for the first available port that does have
  961. * console support.
  962. */
  963. if (co->index >= SCI_NPORTS)
  964. co->index = 0;
  965. sci_port = &sci_ports[co->index];
  966. port = &sci_port->port;
  967. co->data = port;
  968. /*
  969. * Also need to check port->type, we don't actually have any
  970. * UPIO_PORT ports, but uart_report_port() handily misreports
  971. * it anyways if we don't have a port available by the time this is
  972. * called.
  973. */
  974. if (!port->type)
  975. return -ENODEV;
  976. sci_config_port(port, 0);
  977. if (sci_port->enable)
  978. sci_port->enable(port);
  979. if (options)
  980. uart_parse_options(options, &baud, &parity, &bits, &flow);
  981. ret = uart_set_options(port, co, baud, parity, bits, flow);
  982. #if defined(__H8300H__) || defined(__H8300S__)
  983. /* disable rx interrupt */
  984. if (ret == 0)
  985. sci_stop_rx(port);
  986. #endif
  987. /* TODO: disable clock */
  988. return ret;
  989. }
  990. static struct console serial_console = {
  991. .name = "ttySC",
  992. .device = serial_console_device,
  993. .write = serial_console_write,
  994. .setup = serial_console_setup,
  995. .flags = CON_PRINTBUFFER,
  996. .index = -1,
  997. };
  998. static int __init sci_console_init(void)
  999. {
  1000. register_console(&serial_console);
  1001. return 0;
  1002. }
  1003. console_initcall(sci_console_init);
  1004. #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
  1005. #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
  1006. #define SCI_CONSOLE (&serial_console)
  1007. #else
  1008. #define SCI_CONSOLE 0
  1009. #endif
  1010. static char banner[] __initdata =
  1011. KERN_INFO "SuperH SCI(F) driver initialized\n";
  1012. static struct uart_driver sci_uart_driver = {
  1013. .owner = THIS_MODULE,
  1014. .driver_name = "sci",
  1015. .dev_name = "ttySC",
  1016. .major = SCI_MAJOR,
  1017. .minor = SCI_MINOR_START,
  1018. .nr = SCI_NPORTS,
  1019. .cons = SCI_CONSOLE,
  1020. };
  1021. static int sci_remove(struct platform_device *dev)
  1022. {
  1023. struct sh_sci_priv *priv = platform_get_drvdata(dev);
  1024. struct sci_port *p;
  1025. unsigned long flags;
  1026. #ifdef CONFIG_HAVE_CLK
  1027. cpufreq_unregister_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
  1028. #endif
  1029. spin_lock_irqsave(&priv->lock, flags);
  1030. list_for_each_entry(p, &priv->ports, node)
  1031. uart_remove_one_port(&sci_uart_driver, &p->port);
  1032. spin_unlock_irqrestore(&priv->lock, flags);
  1033. kfree(priv);
  1034. return 0;
  1035. }
  1036. static int __devinit sci_probe_single(struct platform_device *dev,
  1037. unsigned int index,
  1038. struct plat_sci_port *p,
  1039. struct sci_port *sciport)
  1040. {
  1041. struct sh_sci_priv *priv = platform_get_drvdata(dev);
  1042. unsigned long flags;
  1043. int ret;
  1044. /* Sanity check */
  1045. if (unlikely(index >= SCI_NPORTS)) {
  1046. dev_notice(&dev->dev, "Attempting to register port "
  1047. "%d when only %d are available.\n",
  1048. index+1, SCI_NPORTS);
  1049. dev_notice(&dev->dev, "Consider bumping "
  1050. "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
  1051. return 0;
  1052. }
  1053. sci_init_single(dev, sciport, index, p);
  1054. ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
  1055. if (ret)
  1056. return ret;
  1057. INIT_LIST_HEAD(&sciport->node);
  1058. spin_lock_irqsave(&priv->lock, flags);
  1059. list_add(&sciport->node, &priv->ports);
  1060. spin_unlock_irqrestore(&priv->lock, flags);
  1061. return 0;
  1062. }
  1063. /*
  1064. * Register a set of serial devices attached to a platform device. The
  1065. * list is terminated with a zero flags entry, which means we expect
  1066. * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
  1067. * remapping (such as sh64) should also set UPF_IOREMAP.
  1068. */
  1069. static int __devinit sci_probe(struct platform_device *dev)
  1070. {
  1071. struct plat_sci_port *p = dev->dev.platform_data;
  1072. struct sh_sci_priv *priv;
  1073. int i, ret = -EINVAL;
  1074. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  1075. if (!priv)
  1076. return -ENOMEM;
  1077. INIT_LIST_HEAD(&priv->ports);
  1078. spin_lock_init(&priv->lock);
  1079. platform_set_drvdata(dev, priv);
  1080. #ifdef CONFIG_HAVE_CLK
  1081. priv->clk_nb.notifier_call = sci_notifier;
  1082. cpufreq_register_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
  1083. #endif
  1084. if (dev->id != -1) {
  1085. ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]);
  1086. if (ret)
  1087. goto err_unreg;
  1088. } else {
  1089. for (i = 0; p && p->flags != 0; p++, i++) {
  1090. ret = sci_probe_single(dev, i, p, &sci_ports[i]);
  1091. if (ret)
  1092. goto err_unreg;
  1093. }
  1094. }
  1095. #ifdef CONFIG_SH_STANDARD_BIOS
  1096. sh_bios_gdb_detach();
  1097. #endif
  1098. return 0;
  1099. err_unreg:
  1100. sci_remove(dev);
  1101. return ret;
  1102. }
  1103. static int sci_suspend(struct device *dev)
  1104. {
  1105. struct sh_sci_priv *priv = dev_get_drvdata(dev);
  1106. struct sci_port *p;
  1107. unsigned long flags;
  1108. spin_lock_irqsave(&priv->lock, flags);
  1109. list_for_each_entry(p, &priv->ports, node)
  1110. uart_suspend_port(&sci_uart_driver, &p->port);
  1111. spin_unlock_irqrestore(&priv->lock, flags);
  1112. return 0;
  1113. }
  1114. static int sci_resume(struct device *dev)
  1115. {
  1116. struct sh_sci_priv *priv = dev_get_drvdata(dev);
  1117. struct sci_port *p;
  1118. unsigned long flags;
  1119. spin_lock_irqsave(&priv->lock, flags);
  1120. list_for_each_entry(p, &priv->ports, node)
  1121. uart_resume_port(&sci_uart_driver, &p->port);
  1122. spin_unlock_irqrestore(&priv->lock, flags);
  1123. return 0;
  1124. }
  1125. static struct dev_pm_ops sci_dev_pm_ops = {
  1126. .suspend = sci_suspend,
  1127. .resume = sci_resume,
  1128. };
  1129. static struct platform_driver sci_driver = {
  1130. .probe = sci_probe,
  1131. .remove = __devexit_p(sci_remove),
  1132. .driver = {
  1133. .name = "sh-sci",
  1134. .owner = THIS_MODULE,
  1135. .pm = &sci_dev_pm_ops,
  1136. },
  1137. };
  1138. static int __init sci_init(void)
  1139. {
  1140. int ret;
  1141. printk(banner);
  1142. ret = uart_register_driver(&sci_uart_driver);
  1143. if (likely(ret == 0)) {
  1144. ret = platform_driver_register(&sci_driver);
  1145. if (unlikely(ret))
  1146. uart_unregister_driver(&sci_uart_driver);
  1147. }
  1148. return ret;
  1149. }
  1150. static void __exit sci_exit(void)
  1151. {
  1152. platform_driver_unregister(&sci_driver);
  1153. uart_unregister_driver(&sci_uart_driver);
  1154. }
  1155. module_init(sci_init);
  1156. module_exit(sci_exit);
  1157. MODULE_LICENSE("GPL");
  1158. MODULE_ALIAS("platform:sh-sci");