sh-sci.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079
  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 - 2011 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. #include <linux/dmaengine.h>
  50. #include <linux/scatterlist.h>
  51. #include <linux/slab.h>
  52. #ifdef CONFIG_SUPERH
  53. #include <asm/sh_bios.h>
  54. #endif
  55. #ifdef CONFIG_H8300
  56. #include <asm/gpio.h>
  57. #endif
  58. #include "sh-sci.h"
  59. struct sci_port {
  60. struct uart_port port;
  61. /* Port type */
  62. unsigned int type;
  63. /* Port IRQs: ERI, RXI, TXI, BRI (optional) */
  64. unsigned int irqs[SCIx_NR_IRQS];
  65. /* Port enable callback */
  66. void (*enable)(struct uart_port *port);
  67. /* Port disable callback */
  68. void (*disable)(struct uart_port *port);
  69. /* Break timer */
  70. struct timer_list break_timer;
  71. int break_flag;
  72. /* SCSCR initialization */
  73. unsigned int scscr;
  74. /* SCBRR calculation algo */
  75. unsigned int scbrr_algo_id;
  76. /* Interface clock */
  77. struct clk *iclk;
  78. /* Function clock */
  79. struct clk *fclk;
  80. struct list_head node;
  81. struct dma_chan *chan_tx;
  82. struct dma_chan *chan_rx;
  83. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  84. struct device *dma_dev;
  85. unsigned int slave_tx;
  86. unsigned int slave_rx;
  87. struct dma_async_tx_descriptor *desc_tx;
  88. struct dma_async_tx_descriptor *desc_rx[2];
  89. dma_cookie_t cookie_tx;
  90. dma_cookie_t cookie_rx[2];
  91. dma_cookie_t active_rx;
  92. struct scatterlist sg_tx;
  93. unsigned int sg_len_tx;
  94. struct scatterlist sg_rx[2];
  95. size_t buf_len_rx;
  96. struct sh_dmae_slave param_tx;
  97. struct sh_dmae_slave param_rx;
  98. struct work_struct work_tx;
  99. struct work_struct work_rx;
  100. struct timer_list rx_timer;
  101. unsigned int rx_timeout;
  102. #endif
  103. };
  104. struct sh_sci_priv {
  105. spinlock_t lock;
  106. struct list_head ports;
  107. struct notifier_block clk_nb;
  108. };
  109. /* Function prototypes */
  110. static void sci_stop_tx(struct uart_port *port);
  111. #define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
  112. static struct sci_port sci_ports[SCI_NPORTS];
  113. static struct uart_driver sci_uart_driver;
  114. static inline struct sci_port *
  115. to_sci_port(struct uart_port *uart)
  116. {
  117. return container_of(uart, struct sci_port, port);
  118. }
  119. #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
  120. #ifdef CONFIG_CONSOLE_POLL
  121. static inline void handle_error(struct uart_port *port)
  122. {
  123. /* Clear error flags */
  124. sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
  125. }
  126. static int sci_poll_get_char(struct uart_port *port)
  127. {
  128. unsigned short status;
  129. int c;
  130. do {
  131. status = sci_in(port, SCxSR);
  132. if (status & SCxSR_ERRORS(port)) {
  133. handle_error(port);
  134. continue;
  135. }
  136. break;
  137. } while (1);
  138. if (!(status & SCxSR_RDxF(port)))
  139. return NO_POLL_CHAR;
  140. c = sci_in(port, SCxRDR);
  141. /* Dummy read */
  142. sci_in(port, SCxSR);
  143. sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  144. return c;
  145. }
  146. #endif
  147. static void sci_poll_put_char(struct uart_port *port, unsigned char c)
  148. {
  149. unsigned short status;
  150. do {
  151. status = sci_in(port, SCxSR);
  152. } while (!(status & SCxSR_TDxE(port)));
  153. sci_out(port, SCxTDR, c);
  154. sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
  155. }
  156. #endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
  157. #if defined(__H8300H__) || defined(__H8300S__)
  158. static void sci_init_pins(struct uart_port *port, unsigned int cflag)
  159. {
  160. int ch = (port->mapbase - SMR0) >> 3;
  161. /* set DDR regs */
  162. H8300_GPIO_DDR(h8300_sci_pins[ch].port,
  163. h8300_sci_pins[ch].rx,
  164. H8300_GPIO_INPUT);
  165. H8300_GPIO_DDR(h8300_sci_pins[ch].port,
  166. h8300_sci_pins[ch].tx,
  167. H8300_GPIO_OUTPUT);
  168. /* tx mark output*/
  169. H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
  170. }
  171. #elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
  172. static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
  173. {
  174. if (port->mapbase == 0xA4400000) {
  175. __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
  176. __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
  177. } else if (port->mapbase == 0xA4410000)
  178. __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
  179. }
  180. #elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
  181. static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
  182. {
  183. unsigned short data;
  184. if (cflag & CRTSCTS) {
  185. /* enable RTS/CTS */
  186. if (port->mapbase == 0xa4430000) { /* SCIF0 */
  187. /* Clear PTCR bit 9-2; enable all scif pins but sck */
  188. data = __raw_readw(PORT_PTCR);
  189. __raw_writew((data & 0xfc03), PORT_PTCR);
  190. } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
  191. /* Clear PVCR bit 9-2 */
  192. data = __raw_readw(PORT_PVCR);
  193. __raw_writew((data & 0xfc03), PORT_PVCR);
  194. }
  195. } else {
  196. if (port->mapbase == 0xa4430000) { /* SCIF0 */
  197. /* Clear PTCR bit 5-2; enable only tx and rx */
  198. data = __raw_readw(PORT_PTCR);
  199. __raw_writew((data & 0xffc3), PORT_PTCR);
  200. } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
  201. /* Clear PVCR bit 5-2 */
  202. data = __raw_readw(PORT_PVCR);
  203. __raw_writew((data & 0xffc3), PORT_PVCR);
  204. }
  205. }
  206. }
  207. #elif defined(CONFIG_CPU_SH3)
  208. /* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
  209. static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
  210. {
  211. unsigned short data;
  212. /* We need to set SCPCR to enable RTS/CTS */
  213. data = __raw_readw(SCPCR);
  214. /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
  215. __raw_writew(data & 0x0fcf, SCPCR);
  216. if (!(cflag & CRTSCTS)) {
  217. /* We need to set SCPCR to enable RTS/CTS */
  218. data = __raw_readw(SCPCR);
  219. /* Clear out SCP7MD1,0, SCP4MD1,0,
  220. Set SCP6MD1,0 = {01} (output) */
  221. __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
  222. data = __raw_readb(SCPDR);
  223. /* Set /RTS2 (bit6) = 0 */
  224. __raw_writeb(data & 0xbf, SCPDR);
  225. }
  226. }
  227. #elif defined(CONFIG_CPU_SUBTYPE_SH7722)
  228. static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
  229. {
  230. unsigned short data;
  231. if (port->mapbase == 0xffe00000) {
  232. data = __raw_readw(PSCR);
  233. data &= ~0x03cf;
  234. if (!(cflag & CRTSCTS))
  235. data |= 0x0340;
  236. __raw_writew(data, PSCR);
  237. }
  238. }
  239. #elif defined(CONFIG_CPU_SUBTYPE_SH7757) || \
  240. defined(CONFIG_CPU_SUBTYPE_SH7763) || \
  241. defined(CONFIG_CPU_SUBTYPE_SH7780) || \
  242. defined(CONFIG_CPU_SUBTYPE_SH7785) || \
  243. defined(CONFIG_CPU_SUBTYPE_SH7786) || \
  244. defined(CONFIG_CPU_SUBTYPE_SHX3)
  245. static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
  246. {
  247. if (!(cflag & CRTSCTS))
  248. __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
  249. }
  250. #elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
  251. static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
  252. {
  253. if (!(cflag & CRTSCTS))
  254. __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
  255. }
  256. #else
  257. static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
  258. {
  259. /* Nothing to do */
  260. }
  261. #endif
  262. #if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
  263. defined(CONFIG_CPU_SUBTYPE_SH7780) || \
  264. defined(CONFIG_CPU_SUBTYPE_SH7785) || \
  265. defined(CONFIG_CPU_SUBTYPE_SH7786)
  266. static int scif_txfill(struct uart_port *port)
  267. {
  268. return sci_in(port, SCTFDR) & 0xff;
  269. }
  270. static int scif_txroom(struct uart_port *port)
  271. {
  272. return SCIF_TXROOM_MAX - scif_txfill(port);
  273. }
  274. static int scif_rxfill(struct uart_port *port)
  275. {
  276. return sci_in(port, SCRFDR) & 0xff;
  277. }
  278. #elif defined(CONFIG_CPU_SUBTYPE_SH7763)
  279. static int scif_txfill(struct uart_port *port)
  280. {
  281. if (port->mapbase == 0xffe00000 ||
  282. port->mapbase == 0xffe08000)
  283. /* SCIF0/1*/
  284. return sci_in(port, SCTFDR) & 0xff;
  285. else
  286. /* SCIF2 */
  287. return sci_in(port, SCFDR) >> 8;
  288. }
  289. static int scif_txroom(struct uart_port *port)
  290. {
  291. if (port->mapbase == 0xffe00000 ||
  292. port->mapbase == 0xffe08000)
  293. /* SCIF0/1*/
  294. return SCIF_TXROOM_MAX - scif_txfill(port);
  295. else
  296. /* SCIF2 */
  297. return SCIF2_TXROOM_MAX - scif_txfill(port);
  298. }
  299. static int scif_rxfill(struct uart_port *port)
  300. {
  301. if ((port->mapbase == 0xffe00000) ||
  302. (port->mapbase == 0xffe08000)) {
  303. /* SCIF0/1*/
  304. return sci_in(port, SCRFDR) & 0xff;
  305. } else {
  306. /* SCIF2 */
  307. return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
  308. }
  309. }
  310. #elif defined(CONFIG_ARCH_SH7372)
  311. static int scif_txfill(struct uart_port *port)
  312. {
  313. if (port->type == PORT_SCIFA)
  314. return sci_in(port, SCFDR) >> 8;
  315. else
  316. return sci_in(port, SCTFDR);
  317. }
  318. static int scif_txroom(struct uart_port *port)
  319. {
  320. return port->fifosize - scif_txfill(port);
  321. }
  322. static int scif_rxfill(struct uart_port *port)
  323. {
  324. if (port->type == PORT_SCIFA)
  325. return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
  326. else
  327. return sci_in(port, SCRFDR);
  328. }
  329. #else
  330. static int scif_txfill(struct uart_port *port)
  331. {
  332. return sci_in(port, SCFDR) >> 8;
  333. }
  334. static int scif_txroom(struct uart_port *port)
  335. {
  336. return SCIF_TXROOM_MAX - scif_txfill(port);
  337. }
  338. static int scif_rxfill(struct uart_port *port)
  339. {
  340. return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
  341. }
  342. #endif
  343. static int sci_txfill(struct uart_port *port)
  344. {
  345. return !(sci_in(port, SCxSR) & SCI_TDRE);
  346. }
  347. static int sci_txroom(struct uart_port *port)
  348. {
  349. return !sci_txfill(port);
  350. }
  351. static int sci_rxfill(struct uart_port *port)
  352. {
  353. return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
  354. }
  355. /* ********************************************************************** *
  356. * the interrupt related routines *
  357. * ********************************************************************** */
  358. static void sci_transmit_chars(struct uart_port *port)
  359. {
  360. struct circ_buf *xmit = &port->state->xmit;
  361. unsigned int stopped = uart_tx_stopped(port);
  362. unsigned short status;
  363. unsigned short ctrl;
  364. int count;
  365. status = sci_in(port, SCxSR);
  366. if (!(status & SCxSR_TDxE(port))) {
  367. ctrl = sci_in(port, SCSCR);
  368. if (uart_circ_empty(xmit))
  369. ctrl &= ~SCSCR_TIE;
  370. else
  371. ctrl |= SCSCR_TIE;
  372. sci_out(port, SCSCR, ctrl);
  373. return;
  374. }
  375. if (port->type == PORT_SCI)
  376. count = sci_txroom(port);
  377. else
  378. count = scif_txroom(port);
  379. do {
  380. unsigned char c;
  381. if (port->x_char) {
  382. c = port->x_char;
  383. port->x_char = 0;
  384. } else if (!uart_circ_empty(xmit) && !stopped) {
  385. c = xmit->buf[xmit->tail];
  386. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  387. } else {
  388. break;
  389. }
  390. sci_out(port, SCxTDR, c);
  391. port->icount.tx++;
  392. } while (--count > 0);
  393. sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
  394. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  395. uart_write_wakeup(port);
  396. if (uart_circ_empty(xmit)) {
  397. sci_stop_tx(port);
  398. } else {
  399. ctrl = sci_in(port, SCSCR);
  400. if (port->type != PORT_SCI) {
  401. sci_in(port, SCxSR); /* Dummy read */
  402. sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
  403. }
  404. ctrl |= SCSCR_TIE;
  405. sci_out(port, SCSCR, ctrl);
  406. }
  407. }
  408. /* On SH3, SCIF may read end-of-break as a space->mark char */
  409. #define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
  410. static inline void sci_receive_chars(struct uart_port *port)
  411. {
  412. struct sci_port *sci_port = to_sci_port(port);
  413. struct tty_struct *tty = port->state->port.tty;
  414. int i, count, copied = 0;
  415. unsigned short status;
  416. unsigned char flag;
  417. status = sci_in(port, SCxSR);
  418. if (!(status & SCxSR_RDxF(port)))
  419. return;
  420. while (1) {
  421. if (port->type == PORT_SCI)
  422. count = sci_rxfill(port);
  423. else
  424. count = scif_rxfill(port);
  425. /* Don't copy more bytes than there is room for in the buffer */
  426. count = tty_buffer_request_room(tty, count);
  427. /* If for any reason we can't copy more data, we're done! */
  428. if (count == 0)
  429. break;
  430. if (port->type == PORT_SCI) {
  431. char c = sci_in(port, SCxRDR);
  432. if (uart_handle_sysrq_char(port, c) ||
  433. sci_port->break_flag)
  434. count = 0;
  435. else
  436. tty_insert_flip_char(tty, c, TTY_NORMAL);
  437. } else {
  438. for (i = 0; i < count; i++) {
  439. char c = sci_in(port, SCxRDR);
  440. status = sci_in(port, SCxSR);
  441. #if defined(CONFIG_CPU_SH3)
  442. /* Skip "chars" during break */
  443. if (sci_port->break_flag) {
  444. if ((c == 0) &&
  445. (status & SCxSR_FER(port))) {
  446. count--; i--;
  447. continue;
  448. }
  449. /* Nonzero => end-of-break */
  450. dev_dbg(port->dev, "debounce<%02x>\n", c);
  451. sci_port->break_flag = 0;
  452. if (STEPFN(c)) {
  453. count--; i--;
  454. continue;
  455. }
  456. }
  457. #endif /* CONFIG_CPU_SH3 */
  458. if (uart_handle_sysrq_char(port, c)) {
  459. count--; i--;
  460. continue;
  461. }
  462. /* Store data and status */
  463. if (status & SCxSR_FER(port)) {
  464. flag = TTY_FRAME;
  465. dev_notice(port->dev, "frame error\n");
  466. } else if (status & SCxSR_PER(port)) {
  467. flag = TTY_PARITY;
  468. dev_notice(port->dev, "parity error\n");
  469. } else
  470. flag = TTY_NORMAL;
  471. tty_insert_flip_char(tty, c, flag);
  472. }
  473. }
  474. sci_in(port, SCxSR); /* dummy read */
  475. sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  476. copied += count;
  477. port->icount.rx += count;
  478. }
  479. if (copied) {
  480. /* Tell the rest of the system the news. New characters! */
  481. tty_flip_buffer_push(tty);
  482. } else {
  483. sci_in(port, SCxSR); /* dummy read */
  484. sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  485. }
  486. }
  487. #define SCI_BREAK_JIFFIES (HZ/20)
  488. /* The sci generates interrupts during the break,
  489. * 1 per millisecond or so during the break period, for 9600 baud.
  490. * So dont bother disabling interrupts.
  491. * But dont want more than 1 break event.
  492. * Use a kernel timer to periodically poll the rx line until
  493. * the break is finished.
  494. */
  495. static void sci_schedule_break_timer(struct sci_port *port)
  496. {
  497. port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
  498. add_timer(&port->break_timer);
  499. }
  500. /* Ensure that two consecutive samples find the break over. */
  501. static void sci_break_timer(unsigned long data)
  502. {
  503. struct sci_port *port = (struct sci_port *)data;
  504. if (sci_rxd_in(&port->port) == 0) {
  505. port->break_flag = 1;
  506. sci_schedule_break_timer(port);
  507. } else if (port->break_flag == 1) {
  508. /* break is over. */
  509. port->break_flag = 2;
  510. sci_schedule_break_timer(port);
  511. } else
  512. port->break_flag = 0;
  513. }
  514. static inline int sci_handle_errors(struct uart_port *port)
  515. {
  516. int copied = 0;
  517. unsigned short status = sci_in(port, SCxSR);
  518. struct tty_struct *tty = port->state->port.tty;
  519. if (status & SCxSR_ORER(port)) {
  520. /* overrun error */
  521. if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
  522. copied++;
  523. dev_notice(port->dev, "overrun error");
  524. }
  525. if (status & SCxSR_FER(port)) {
  526. if (sci_rxd_in(port) == 0) {
  527. /* Notify of BREAK */
  528. struct sci_port *sci_port = to_sci_port(port);
  529. if (!sci_port->break_flag) {
  530. sci_port->break_flag = 1;
  531. sci_schedule_break_timer(sci_port);
  532. /* Do sysrq handling. */
  533. if (uart_handle_break(port))
  534. return 0;
  535. dev_dbg(port->dev, "BREAK detected\n");
  536. if (tty_insert_flip_char(tty, 0, TTY_BREAK))
  537. copied++;
  538. }
  539. } else {
  540. /* frame error */
  541. if (tty_insert_flip_char(tty, 0, TTY_FRAME))
  542. copied++;
  543. dev_notice(port->dev, "frame error\n");
  544. }
  545. }
  546. if (status & SCxSR_PER(port)) {
  547. /* parity error */
  548. if (tty_insert_flip_char(tty, 0, TTY_PARITY))
  549. copied++;
  550. dev_notice(port->dev, "parity error");
  551. }
  552. if (copied)
  553. tty_flip_buffer_push(tty);
  554. return copied;
  555. }
  556. static inline int sci_handle_fifo_overrun(struct uart_port *port)
  557. {
  558. struct tty_struct *tty = port->state->port.tty;
  559. int copied = 0;
  560. if (port->type != PORT_SCIF)
  561. return 0;
  562. if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
  563. sci_out(port, SCLSR, 0);
  564. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  565. tty_flip_buffer_push(tty);
  566. dev_notice(port->dev, "overrun error\n");
  567. copied++;
  568. }
  569. return copied;
  570. }
  571. static inline int sci_handle_breaks(struct uart_port *port)
  572. {
  573. int copied = 0;
  574. unsigned short status = sci_in(port, SCxSR);
  575. struct tty_struct *tty = port->state->port.tty;
  576. struct sci_port *s = to_sci_port(port);
  577. if (uart_handle_break(port))
  578. return 0;
  579. if (!s->break_flag && status & SCxSR_BRK(port)) {
  580. #if defined(CONFIG_CPU_SH3)
  581. /* Debounce break */
  582. s->break_flag = 1;
  583. #endif
  584. /* Notify of BREAK */
  585. if (tty_insert_flip_char(tty, 0, TTY_BREAK))
  586. copied++;
  587. dev_dbg(port->dev, "BREAK detected\n");
  588. }
  589. if (copied)
  590. tty_flip_buffer_push(tty);
  591. copied += sci_handle_fifo_overrun(port);
  592. return copied;
  593. }
  594. static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
  595. {
  596. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  597. struct uart_port *port = ptr;
  598. struct sci_port *s = to_sci_port(port);
  599. if (s->chan_rx) {
  600. u16 scr = sci_in(port, SCSCR);
  601. u16 ssr = sci_in(port, SCxSR);
  602. /* Disable future Rx interrupts */
  603. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
  604. disable_irq_nosync(irq);
  605. scr |= 0x4000;
  606. } else {
  607. scr &= ~SCSCR_RIE;
  608. }
  609. sci_out(port, SCSCR, scr);
  610. /* Clear current interrupt */
  611. sci_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
  612. dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
  613. jiffies, s->rx_timeout);
  614. mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
  615. return IRQ_HANDLED;
  616. }
  617. #endif
  618. /* I think sci_receive_chars has to be called irrespective
  619. * of whether the I_IXOFF is set, otherwise, how is the interrupt
  620. * to be disabled?
  621. */
  622. sci_receive_chars(ptr);
  623. return IRQ_HANDLED;
  624. }
  625. static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
  626. {
  627. struct uart_port *port = ptr;
  628. unsigned long flags;
  629. spin_lock_irqsave(&port->lock, flags);
  630. sci_transmit_chars(port);
  631. spin_unlock_irqrestore(&port->lock, flags);
  632. return IRQ_HANDLED;
  633. }
  634. static irqreturn_t sci_er_interrupt(int irq, void *ptr)
  635. {
  636. struct uart_port *port = ptr;
  637. /* Handle errors */
  638. if (port->type == PORT_SCI) {
  639. if (sci_handle_errors(port)) {
  640. /* discard character in rx buffer */
  641. sci_in(port, SCxSR);
  642. sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  643. }
  644. } else {
  645. sci_handle_fifo_overrun(port);
  646. sci_rx_interrupt(irq, ptr);
  647. }
  648. sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
  649. /* Kick the transmission */
  650. sci_tx_interrupt(irq, ptr);
  651. return IRQ_HANDLED;
  652. }
  653. static irqreturn_t sci_br_interrupt(int irq, void *ptr)
  654. {
  655. struct uart_port *port = ptr;
  656. /* Handle BREAKs */
  657. sci_handle_breaks(port);
  658. sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
  659. return IRQ_HANDLED;
  660. }
  661. static inline unsigned long port_rx_irq_mask(struct uart_port *port)
  662. {
  663. /*
  664. * Not all ports (such as SCIFA) will support REIE. Rather than
  665. * special-casing the port type, we check the port initialization
  666. * IRQ enable mask to see whether the IRQ is desired at all. If
  667. * it's unset, it's logically inferred that there's no point in
  668. * testing for it.
  669. */
  670. return SCSCR_RIE | (to_sci_port(port)->scscr & SCSCR_REIE);
  671. }
  672. static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
  673. {
  674. unsigned short ssr_status, scr_status, err_enabled;
  675. struct uart_port *port = ptr;
  676. struct sci_port *s = to_sci_port(port);
  677. irqreturn_t ret = IRQ_NONE;
  678. ssr_status = sci_in(port, SCxSR);
  679. scr_status = sci_in(port, SCSCR);
  680. err_enabled = scr_status & port_rx_irq_mask(port);
  681. /* Tx Interrupt */
  682. if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
  683. !s->chan_tx)
  684. ret = sci_tx_interrupt(irq, ptr);
  685. /*
  686. * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
  687. * DR flags
  688. */
  689. if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
  690. (scr_status & SCSCR_RIE))
  691. ret = sci_rx_interrupt(irq, ptr);
  692. /* Error Interrupt */
  693. if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
  694. ret = sci_er_interrupt(irq, ptr);
  695. /* Break Interrupt */
  696. if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
  697. ret = sci_br_interrupt(irq, ptr);
  698. return ret;
  699. }
  700. /*
  701. * Here we define a transistion notifier so that we can update all of our
  702. * ports' baud rate when the peripheral clock changes.
  703. */
  704. static int sci_notifier(struct notifier_block *self,
  705. unsigned long phase, void *p)
  706. {
  707. struct sh_sci_priv *priv = container_of(self,
  708. struct sh_sci_priv, clk_nb);
  709. struct sci_port *sci_port;
  710. unsigned long flags;
  711. if ((phase == CPUFREQ_POSTCHANGE) ||
  712. (phase == CPUFREQ_RESUMECHANGE)) {
  713. spin_lock_irqsave(&priv->lock, flags);
  714. list_for_each_entry(sci_port, &priv->ports, node)
  715. sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
  716. spin_unlock_irqrestore(&priv->lock, flags);
  717. }
  718. return NOTIFY_OK;
  719. }
  720. static void sci_clk_enable(struct uart_port *port)
  721. {
  722. struct sci_port *sci_port = to_sci_port(port);
  723. clk_enable(sci_port->iclk);
  724. sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
  725. clk_enable(sci_port->fclk);
  726. }
  727. static void sci_clk_disable(struct uart_port *port)
  728. {
  729. struct sci_port *sci_port = to_sci_port(port);
  730. clk_disable(sci_port->fclk);
  731. clk_disable(sci_port->iclk);
  732. }
  733. static int sci_request_irq(struct sci_port *port)
  734. {
  735. int i;
  736. irqreturn_t (*handlers[4])(int irq, void *ptr) = {
  737. sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
  738. sci_br_interrupt,
  739. };
  740. const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
  741. "SCI Transmit Data Empty", "SCI Break" };
  742. if (port->irqs[0] == port->irqs[1]) {
  743. if (unlikely(!port->irqs[0]))
  744. return -ENODEV;
  745. if (request_irq(port->irqs[0], sci_mpxed_interrupt,
  746. IRQF_DISABLED, "sci", port)) {
  747. dev_err(port->port.dev, "Can't allocate IRQ\n");
  748. return -ENODEV;
  749. }
  750. } else {
  751. for (i = 0; i < ARRAY_SIZE(handlers); i++) {
  752. if (unlikely(!port->irqs[i]))
  753. continue;
  754. if (request_irq(port->irqs[i], handlers[i],
  755. IRQF_DISABLED, desc[i], port)) {
  756. dev_err(port->port.dev, "Can't allocate IRQ\n");
  757. return -ENODEV;
  758. }
  759. }
  760. }
  761. return 0;
  762. }
  763. static void sci_free_irq(struct sci_port *port)
  764. {
  765. int i;
  766. if (port->irqs[0] == port->irqs[1])
  767. free_irq(port->irqs[0], port);
  768. else {
  769. for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
  770. if (!port->irqs[i])
  771. continue;
  772. free_irq(port->irqs[i], port);
  773. }
  774. }
  775. }
  776. static unsigned int sci_tx_empty(struct uart_port *port)
  777. {
  778. unsigned short status = sci_in(port, SCxSR);
  779. unsigned short in_tx_fifo = scif_txfill(port);
  780. return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
  781. }
  782. static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
  783. {
  784. /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
  785. /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
  786. /* If you have signals for DTR and DCD, please implement here. */
  787. }
  788. static unsigned int sci_get_mctrl(struct uart_port *port)
  789. {
  790. /* This routine is used for getting signals of: DTR, DCD, DSR, RI,
  791. and CTS/RTS */
  792. return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
  793. }
  794. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  795. static void sci_dma_tx_complete(void *arg)
  796. {
  797. struct sci_port *s = arg;
  798. struct uart_port *port = &s->port;
  799. struct circ_buf *xmit = &port->state->xmit;
  800. unsigned long flags;
  801. dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
  802. spin_lock_irqsave(&port->lock, flags);
  803. xmit->tail += sg_dma_len(&s->sg_tx);
  804. xmit->tail &= UART_XMIT_SIZE - 1;
  805. port->icount.tx += sg_dma_len(&s->sg_tx);
  806. async_tx_ack(s->desc_tx);
  807. s->cookie_tx = -EINVAL;
  808. s->desc_tx = NULL;
  809. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  810. uart_write_wakeup(port);
  811. if (!uart_circ_empty(xmit)) {
  812. schedule_work(&s->work_tx);
  813. } else if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
  814. u16 ctrl = sci_in(port, SCSCR);
  815. sci_out(port, SCSCR, ctrl & ~SCSCR_TIE);
  816. }
  817. spin_unlock_irqrestore(&port->lock, flags);
  818. }
  819. /* Locking: called with port lock held */
  820. static int sci_dma_rx_push(struct sci_port *s, struct tty_struct *tty,
  821. size_t count)
  822. {
  823. struct uart_port *port = &s->port;
  824. int i, active, room;
  825. room = tty_buffer_request_room(tty, count);
  826. if (s->active_rx == s->cookie_rx[0]) {
  827. active = 0;
  828. } else if (s->active_rx == s->cookie_rx[1]) {
  829. active = 1;
  830. } else {
  831. dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
  832. return 0;
  833. }
  834. if (room < count)
  835. dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
  836. count - room);
  837. if (!room)
  838. return room;
  839. for (i = 0; i < room; i++)
  840. tty_insert_flip_char(tty, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
  841. TTY_NORMAL);
  842. port->icount.rx += room;
  843. return room;
  844. }
  845. static void sci_dma_rx_complete(void *arg)
  846. {
  847. struct sci_port *s = arg;
  848. struct uart_port *port = &s->port;
  849. struct tty_struct *tty = port->state->port.tty;
  850. unsigned long flags;
  851. int count;
  852. dev_dbg(port->dev, "%s(%d) active #%d\n", __func__, port->line, s->active_rx);
  853. spin_lock_irqsave(&port->lock, flags);
  854. count = sci_dma_rx_push(s, tty, s->buf_len_rx);
  855. mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
  856. spin_unlock_irqrestore(&port->lock, flags);
  857. if (count)
  858. tty_flip_buffer_push(tty);
  859. schedule_work(&s->work_rx);
  860. }
  861. static void sci_start_rx(struct uart_port *port);
  862. static void sci_start_tx(struct uart_port *port);
  863. static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
  864. {
  865. struct dma_chan *chan = s->chan_rx;
  866. struct uart_port *port = &s->port;
  867. s->chan_rx = NULL;
  868. s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
  869. dma_release_channel(chan);
  870. if (sg_dma_address(&s->sg_rx[0]))
  871. dma_free_coherent(port->dev, s->buf_len_rx * 2,
  872. sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
  873. if (enable_pio)
  874. sci_start_rx(port);
  875. }
  876. static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
  877. {
  878. struct dma_chan *chan = s->chan_tx;
  879. struct uart_port *port = &s->port;
  880. s->chan_tx = NULL;
  881. s->cookie_tx = -EINVAL;
  882. dma_release_channel(chan);
  883. if (enable_pio)
  884. sci_start_tx(port);
  885. }
  886. static void sci_submit_rx(struct sci_port *s)
  887. {
  888. struct dma_chan *chan = s->chan_rx;
  889. int i;
  890. for (i = 0; i < 2; i++) {
  891. struct scatterlist *sg = &s->sg_rx[i];
  892. struct dma_async_tx_descriptor *desc;
  893. desc = chan->device->device_prep_slave_sg(chan,
  894. sg, 1, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT);
  895. if (desc) {
  896. s->desc_rx[i] = desc;
  897. desc->callback = sci_dma_rx_complete;
  898. desc->callback_param = s;
  899. s->cookie_rx[i] = desc->tx_submit(desc);
  900. }
  901. if (!desc || s->cookie_rx[i] < 0) {
  902. if (i) {
  903. async_tx_ack(s->desc_rx[0]);
  904. s->cookie_rx[0] = -EINVAL;
  905. }
  906. if (desc) {
  907. async_tx_ack(desc);
  908. s->cookie_rx[i] = -EINVAL;
  909. }
  910. dev_warn(s->port.dev,
  911. "failed to re-start DMA, using PIO\n");
  912. sci_rx_dma_release(s, true);
  913. return;
  914. }
  915. dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
  916. s->cookie_rx[i], i);
  917. }
  918. s->active_rx = s->cookie_rx[0];
  919. dma_async_issue_pending(chan);
  920. }
  921. static void work_fn_rx(struct work_struct *work)
  922. {
  923. struct sci_port *s = container_of(work, struct sci_port, work_rx);
  924. struct uart_port *port = &s->port;
  925. struct dma_async_tx_descriptor *desc;
  926. int new;
  927. if (s->active_rx == s->cookie_rx[0]) {
  928. new = 0;
  929. } else if (s->active_rx == s->cookie_rx[1]) {
  930. new = 1;
  931. } else {
  932. dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
  933. return;
  934. }
  935. desc = s->desc_rx[new];
  936. if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
  937. DMA_SUCCESS) {
  938. /* Handle incomplete DMA receive */
  939. struct tty_struct *tty = port->state->port.tty;
  940. struct dma_chan *chan = s->chan_rx;
  941. struct sh_desc *sh_desc = container_of(desc, struct sh_desc,
  942. async_tx);
  943. unsigned long flags;
  944. int count;
  945. chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
  946. dev_dbg(port->dev, "Read %u bytes with cookie %d\n",
  947. sh_desc->partial, sh_desc->cookie);
  948. spin_lock_irqsave(&port->lock, flags);
  949. count = sci_dma_rx_push(s, tty, sh_desc->partial);
  950. spin_unlock_irqrestore(&port->lock, flags);
  951. if (count)
  952. tty_flip_buffer_push(tty);
  953. sci_submit_rx(s);
  954. return;
  955. }
  956. s->cookie_rx[new] = desc->tx_submit(desc);
  957. if (s->cookie_rx[new] < 0) {
  958. dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
  959. sci_rx_dma_release(s, true);
  960. return;
  961. }
  962. s->active_rx = s->cookie_rx[!new];
  963. dev_dbg(port->dev, "%s: cookie %d #%d, new active #%d\n", __func__,
  964. s->cookie_rx[new], new, s->active_rx);
  965. }
  966. static void work_fn_tx(struct work_struct *work)
  967. {
  968. struct sci_port *s = container_of(work, struct sci_port, work_tx);
  969. struct dma_async_tx_descriptor *desc;
  970. struct dma_chan *chan = s->chan_tx;
  971. struct uart_port *port = &s->port;
  972. struct circ_buf *xmit = &port->state->xmit;
  973. struct scatterlist *sg = &s->sg_tx;
  974. /*
  975. * DMA is idle now.
  976. * Port xmit buffer is already mapped, and it is one page... Just adjust
  977. * offsets and lengths. Since it is a circular buffer, we have to
  978. * transmit till the end, and then the rest. Take the port lock to get a
  979. * consistent xmit buffer state.
  980. */
  981. spin_lock_irq(&port->lock);
  982. sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
  983. sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
  984. sg->offset;
  985. sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
  986. CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
  987. spin_unlock_irq(&port->lock);
  988. BUG_ON(!sg_dma_len(sg));
  989. desc = chan->device->device_prep_slave_sg(chan,
  990. sg, s->sg_len_tx, DMA_TO_DEVICE,
  991. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  992. if (!desc) {
  993. /* switch to PIO */
  994. sci_tx_dma_release(s, true);
  995. return;
  996. }
  997. dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
  998. spin_lock_irq(&port->lock);
  999. s->desc_tx = desc;
  1000. desc->callback = sci_dma_tx_complete;
  1001. desc->callback_param = s;
  1002. spin_unlock_irq(&port->lock);
  1003. s->cookie_tx = desc->tx_submit(desc);
  1004. if (s->cookie_tx < 0) {
  1005. dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
  1006. /* switch to PIO */
  1007. sci_tx_dma_release(s, true);
  1008. return;
  1009. }
  1010. dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
  1011. xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
  1012. dma_async_issue_pending(chan);
  1013. }
  1014. #endif
  1015. static void sci_start_tx(struct uart_port *port)
  1016. {
  1017. struct sci_port *s = to_sci_port(port);
  1018. unsigned short ctrl;
  1019. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  1020. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
  1021. u16 new, scr = sci_in(port, SCSCR);
  1022. if (s->chan_tx)
  1023. new = scr | 0x8000;
  1024. else
  1025. new = scr & ~0x8000;
  1026. if (new != scr)
  1027. sci_out(port, SCSCR, new);
  1028. }
  1029. if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
  1030. s->cookie_tx < 0)
  1031. schedule_work(&s->work_tx);
  1032. #endif
  1033. if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
  1034. /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
  1035. ctrl = sci_in(port, SCSCR);
  1036. sci_out(port, SCSCR, ctrl | SCSCR_TIE);
  1037. }
  1038. }
  1039. static void sci_stop_tx(struct uart_port *port)
  1040. {
  1041. unsigned short ctrl;
  1042. /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
  1043. ctrl = sci_in(port, SCSCR);
  1044. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
  1045. ctrl &= ~0x8000;
  1046. ctrl &= ~SCSCR_TIE;
  1047. sci_out(port, SCSCR, ctrl);
  1048. }
  1049. static void sci_start_rx(struct uart_port *port)
  1050. {
  1051. unsigned short ctrl;
  1052. ctrl = sci_in(port, SCSCR) | port_rx_irq_mask(port);
  1053. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
  1054. ctrl &= ~0x4000;
  1055. sci_out(port, SCSCR, ctrl);
  1056. }
  1057. static void sci_stop_rx(struct uart_port *port)
  1058. {
  1059. unsigned short ctrl;
  1060. ctrl = sci_in(port, SCSCR);
  1061. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
  1062. ctrl &= ~0x4000;
  1063. ctrl &= ~port_rx_irq_mask(port);
  1064. sci_out(port, SCSCR, ctrl);
  1065. }
  1066. static void sci_enable_ms(struct uart_port *port)
  1067. {
  1068. /* Nothing here yet .. */
  1069. }
  1070. static void sci_break_ctl(struct uart_port *port, int break_state)
  1071. {
  1072. /* Nothing here yet .. */
  1073. }
  1074. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  1075. static bool filter(struct dma_chan *chan, void *slave)
  1076. {
  1077. struct sh_dmae_slave *param = slave;
  1078. dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
  1079. param->slave_id);
  1080. if (param->dma_dev == chan->device->dev) {
  1081. chan->private = param;
  1082. return true;
  1083. } else {
  1084. return false;
  1085. }
  1086. }
  1087. static void rx_timer_fn(unsigned long arg)
  1088. {
  1089. struct sci_port *s = (struct sci_port *)arg;
  1090. struct uart_port *port = &s->port;
  1091. u16 scr = sci_in(port, SCSCR);
  1092. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
  1093. scr &= ~0x4000;
  1094. enable_irq(s->irqs[1]);
  1095. }
  1096. sci_out(port, SCSCR, scr | SCSCR_RIE);
  1097. dev_dbg(port->dev, "DMA Rx timed out\n");
  1098. schedule_work(&s->work_rx);
  1099. }
  1100. static void sci_request_dma(struct uart_port *port)
  1101. {
  1102. struct sci_port *s = to_sci_port(port);
  1103. struct sh_dmae_slave *param;
  1104. struct dma_chan *chan;
  1105. dma_cap_mask_t mask;
  1106. int nent;
  1107. dev_dbg(port->dev, "%s: port %d DMA %p\n", __func__,
  1108. port->line, s->dma_dev);
  1109. if (!s->dma_dev)
  1110. return;
  1111. dma_cap_zero(mask);
  1112. dma_cap_set(DMA_SLAVE, mask);
  1113. param = &s->param_tx;
  1114. /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
  1115. param->slave_id = s->slave_tx;
  1116. param->dma_dev = s->dma_dev;
  1117. s->cookie_tx = -EINVAL;
  1118. chan = dma_request_channel(mask, filter, param);
  1119. dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
  1120. if (chan) {
  1121. s->chan_tx = chan;
  1122. sg_init_table(&s->sg_tx, 1);
  1123. /* UART circular tx buffer is an aligned page. */
  1124. BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
  1125. sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
  1126. UART_XMIT_SIZE, (int)port->state->xmit.buf & ~PAGE_MASK);
  1127. nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
  1128. if (!nent)
  1129. sci_tx_dma_release(s, false);
  1130. else
  1131. dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
  1132. sg_dma_len(&s->sg_tx),
  1133. port->state->xmit.buf, sg_dma_address(&s->sg_tx));
  1134. s->sg_len_tx = nent;
  1135. INIT_WORK(&s->work_tx, work_fn_tx);
  1136. }
  1137. param = &s->param_rx;
  1138. /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
  1139. param->slave_id = s->slave_rx;
  1140. param->dma_dev = s->dma_dev;
  1141. chan = dma_request_channel(mask, filter, param);
  1142. dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
  1143. if (chan) {
  1144. dma_addr_t dma[2];
  1145. void *buf[2];
  1146. int i;
  1147. s->chan_rx = chan;
  1148. s->buf_len_rx = 2 * max(16, (int)port->fifosize);
  1149. buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
  1150. &dma[0], GFP_KERNEL);
  1151. if (!buf[0]) {
  1152. dev_warn(port->dev,
  1153. "failed to allocate dma buffer, using PIO\n");
  1154. sci_rx_dma_release(s, true);
  1155. return;
  1156. }
  1157. buf[1] = buf[0] + s->buf_len_rx;
  1158. dma[1] = dma[0] + s->buf_len_rx;
  1159. for (i = 0; i < 2; i++) {
  1160. struct scatterlist *sg = &s->sg_rx[i];
  1161. sg_init_table(sg, 1);
  1162. sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
  1163. (int)buf[i] & ~PAGE_MASK);
  1164. sg_dma_address(sg) = dma[i];
  1165. }
  1166. INIT_WORK(&s->work_rx, work_fn_rx);
  1167. setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
  1168. sci_submit_rx(s);
  1169. }
  1170. }
  1171. static void sci_free_dma(struct uart_port *port)
  1172. {
  1173. struct sci_port *s = to_sci_port(port);
  1174. if (!s->dma_dev)
  1175. return;
  1176. if (s->chan_tx)
  1177. sci_tx_dma_release(s, false);
  1178. if (s->chan_rx)
  1179. sci_rx_dma_release(s, false);
  1180. }
  1181. #endif
  1182. static int sci_startup(struct uart_port *port)
  1183. {
  1184. struct sci_port *s = to_sci_port(port);
  1185. dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
  1186. if (s->enable)
  1187. s->enable(port);
  1188. sci_request_irq(s);
  1189. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  1190. sci_request_dma(port);
  1191. #endif
  1192. sci_start_tx(port);
  1193. sci_start_rx(port);
  1194. return 0;
  1195. }
  1196. static void sci_shutdown(struct uart_port *port)
  1197. {
  1198. struct sci_port *s = to_sci_port(port);
  1199. dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
  1200. sci_stop_rx(port);
  1201. sci_stop_tx(port);
  1202. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  1203. sci_free_dma(port);
  1204. #endif
  1205. sci_free_irq(s);
  1206. if (s->disable)
  1207. s->disable(port);
  1208. }
  1209. static unsigned int sci_scbrr_calc(unsigned int algo_id, unsigned int bps,
  1210. unsigned long freq)
  1211. {
  1212. switch (algo_id) {
  1213. case SCBRR_ALGO_1:
  1214. return ((freq + 16 * bps) / (16 * bps) - 1);
  1215. case SCBRR_ALGO_2:
  1216. return ((freq + 16 * bps) / (32 * bps) - 1);
  1217. case SCBRR_ALGO_3:
  1218. return (((freq * 2) + 16 * bps) / (16 * bps) - 1);
  1219. case SCBRR_ALGO_4:
  1220. return (((freq * 2) + 16 * bps) / (32 * bps) - 1);
  1221. case SCBRR_ALGO_5:
  1222. return (((freq * 1000 / 32) / bps) - 1);
  1223. }
  1224. /* Warn, but use a safe default */
  1225. WARN_ON(1);
  1226. return ((freq + 16 * bps) / (32 * bps) - 1);
  1227. }
  1228. static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
  1229. struct ktermios *old)
  1230. {
  1231. struct sci_port *s = to_sci_port(port);
  1232. unsigned int status, baud, smr_val, max_baud;
  1233. int t = -1;
  1234. u16 scfcr = 0;
  1235. /*
  1236. * earlyprintk comes here early on with port->uartclk set to zero.
  1237. * the clock framework is not up and running at this point so here
  1238. * we assume that 115200 is the maximum baud rate. please note that
  1239. * the baud rate is not programmed during earlyprintk - it is assumed
  1240. * that the previous boot loader has enabled required clocks and
  1241. * setup the baud rate generator hardware for us already.
  1242. */
  1243. max_baud = port->uartclk ? port->uartclk / 16 : 115200;
  1244. baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
  1245. if (likely(baud && port->uartclk))
  1246. t = sci_scbrr_calc(s->scbrr_algo_id, baud, port->uartclk);
  1247. do {
  1248. status = sci_in(port, SCxSR);
  1249. } while (!(status & SCxSR_TEND(port)));
  1250. sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
  1251. if (port->type != PORT_SCI)
  1252. sci_out(port, SCFCR, scfcr | SCFCR_RFRST | SCFCR_TFRST);
  1253. smr_val = sci_in(port, SCSMR) & 3;
  1254. if ((termios->c_cflag & CSIZE) == CS7)
  1255. smr_val |= 0x40;
  1256. if (termios->c_cflag & PARENB)
  1257. smr_val |= 0x20;
  1258. if (termios->c_cflag & PARODD)
  1259. smr_val |= 0x30;
  1260. if (termios->c_cflag & CSTOPB)
  1261. smr_val |= 0x08;
  1262. uart_update_timeout(port, termios->c_cflag, baud);
  1263. sci_out(port, SCSMR, smr_val);
  1264. dev_dbg(port->dev, "%s: SMR %x, t %x, SCSCR %x\n", __func__, smr_val, t,
  1265. s->scscr);
  1266. if (t > 0) {
  1267. if (t >= 256) {
  1268. sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
  1269. t >>= 2;
  1270. } else
  1271. sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
  1272. sci_out(port, SCBRR, t);
  1273. udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
  1274. }
  1275. sci_init_pins(port, termios->c_cflag);
  1276. sci_out(port, SCFCR, scfcr | ((termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0));
  1277. sci_out(port, SCSCR, s->scscr);
  1278. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  1279. /*
  1280. * Calculate delay for 1.5 DMA buffers: see
  1281. * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
  1282. * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
  1283. * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
  1284. * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
  1285. * sizes), but it has been found out experimentally, that this is not
  1286. * enough: the driver too often needlessly runs on a DMA timeout. 20ms
  1287. * as a minimum seem to work perfectly.
  1288. */
  1289. if (s->chan_rx) {
  1290. s->rx_timeout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
  1291. port->fifosize / 2;
  1292. dev_dbg(port->dev,
  1293. "DMA Rx t-out %ums, tty t-out %u jiffies\n",
  1294. s->rx_timeout * 1000 / HZ, port->timeout);
  1295. if (s->rx_timeout < msecs_to_jiffies(20))
  1296. s->rx_timeout = msecs_to_jiffies(20);
  1297. }
  1298. #endif
  1299. if ((termios->c_cflag & CREAD) != 0)
  1300. sci_start_rx(port);
  1301. }
  1302. static const char *sci_type(struct uart_port *port)
  1303. {
  1304. switch (port->type) {
  1305. case PORT_IRDA:
  1306. return "irda";
  1307. case PORT_SCI:
  1308. return "sci";
  1309. case PORT_SCIF:
  1310. return "scif";
  1311. case PORT_SCIFA:
  1312. return "scifa";
  1313. case PORT_SCIFB:
  1314. return "scifb";
  1315. }
  1316. return NULL;
  1317. }
  1318. static void sci_release_port(struct uart_port *port)
  1319. {
  1320. /* Nothing here yet .. */
  1321. }
  1322. static int sci_request_port(struct uart_port *port)
  1323. {
  1324. /* Nothing here yet .. */
  1325. return 0;
  1326. }
  1327. static void sci_config_port(struct uart_port *port, int flags)
  1328. {
  1329. struct sci_port *s = to_sci_port(port);
  1330. port->type = s->type;
  1331. if (port->membase)
  1332. return;
  1333. if (port->flags & UPF_IOREMAP) {
  1334. port->membase = ioremap_nocache(port->mapbase, 0x40);
  1335. if (IS_ERR(port->membase))
  1336. dev_err(port->dev, "can't remap port#%d\n", port->line);
  1337. } else {
  1338. /*
  1339. * For the simple (and majority of) cases where we don't
  1340. * need to do any remapping, just cast the cookie
  1341. * directly.
  1342. */
  1343. port->membase = (void __iomem *)port->mapbase;
  1344. }
  1345. }
  1346. static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
  1347. {
  1348. struct sci_port *s = to_sci_port(port);
  1349. if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
  1350. return -EINVAL;
  1351. if (ser->baud_base < 2400)
  1352. /* No paper tape reader for Mitch.. */
  1353. return -EINVAL;
  1354. return 0;
  1355. }
  1356. static struct uart_ops sci_uart_ops = {
  1357. .tx_empty = sci_tx_empty,
  1358. .set_mctrl = sci_set_mctrl,
  1359. .get_mctrl = sci_get_mctrl,
  1360. .start_tx = sci_start_tx,
  1361. .stop_tx = sci_stop_tx,
  1362. .stop_rx = sci_stop_rx,
  1363. .enable_ms = sci_enable_ms,
  1364. .break_ctl = sci_break_ctl,
  1365. .startup = sci_startup,
  1366. .shutdown = sci_shutdown,
  1367. .set_termios = sci_set_termios,
  1368. .type = sci_type,
  1369. .release_port = sci_release_port,
  1370. .request_port = sci_request_port,
  1371. .config_port = sci_config_port,
  1372. .verify_port = sci_verify_port,
  1373. #ifdef CONFIG_CONSOLE_POLL
  1374. .poll_get_char = sci_poll_get_char,
  1375. .poll_put_char = sci_poll_put_char,
  1376. #endif
  1377. };
  1378. static int __devinit sci_init_single(struct platform_device *dev,
  1379. struct sci_port *sci_port,
  1380. unsigned int index,
  1381. struct plat_sci_port *p)
  1382. {
  1383. struct uart_port *port = &sci_port->port;
  1384. port->ops = &sci_uart_ops;
  1385. port->iotype = UPIO_MEM;
  1386. port->line = index;
  1387. switch (p->type) {
  1388. case PORT_SCIFB:
  1389. port->fifosize = 256;
  1390. break;
  1391. case PORT_SCIFA:
  1392. port->fifosize = 64;
  1393. break;
  1394. case PORT_SCIF:
  1395. port->fifosize = 16;
  1396. break;
  1397. default:
  1398. port->fifosize = 1;
  1399. break;
  1400. }
  1401. if (dev) {
  1402. sci_port->iclk = clk_get(&dev->dev, "sci_ick");
  1403. if (IS_ERR(sci_port->iclk)) {
  1404. sci_port->iclk = clk_get(&dev->dev, "peripheral_clk");
  1405. if (IS_ERR(sci_port->iclk)) {
  1406. dev_err(&dev->dev, "can't get iclk\n");
  1407. return PTR_ERR(sci_port->iclk);
  1408. }
  1409. }
  1410. /*
  1411. * The function clock is optional, ignore it if we can't
  1412. * find it.
  1413. */
  1414. sci_port->fclk = clk_get(&dev->dev, "sci_fck");
  1415. if (IS_ERR(sci_port->fclk))
  1416. sci_port->fclk = NULL;
  1417. sci_port->enable = sci_clk_enable;
  1418. sci_port->disable = sci_clk_disable;
  1419. port->dev = &dev->dev;
  1420. }
  1421. sci_port->break_timer.data = (unsigned long)sci_port;
  1422. sci_port->break_timer.function = sci_break_timer;
  1423. init_timer(&sci_port->break_timer);
  1424. port->mapbase = p->mapbase;
  1425. port->membase = p->membase;
  1426. port->irq = p->irqs[SCIx_TXI_IRQ];
  1427. port->flags = p->flags;
  1428. sci_port->type = port->type = p->type;
  1429. sci_port->scscr = p->scscr;
  1430. sci_port->scbrr_algo_id = p->scbrr_algo_id;
  1431. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  1432. sci_port->dma_dev = p->dma_dev;
  1433. sci_port->slave_tx = p->dma_slave_tx;
  1434. sci_port->slave_rx = p->dma_slave_rx;
  1435. dev_dbg(port->dev, "%s: DMA device %p, tx %d, rx %d\n", __func__,
  1436. p->dma_dev, p->dma_slave_tx, p->dma_slave_rx);
  1437. #endif
  1438. memcpy(&sci_port->irqs, &p->irqs, sizeof(p->irqs));
  1439. return 0;
  1440. }
  1441. #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
  1442. static struct tty_driver *serial_console_device(struct console *co, int *index)
  1443. {
  1444. struct uart_driver *p = &sci_uart_driver;
  1445. *index = co->index;
  1446. return p->tty_driver;
  1447. }
  1448. static void serial_console_putchar(struct uart_port *port, int ch)
  1449. {
  1450. sci_poll_put_char(port, ch);
  1451. }
  1452. /*
  1453. * Print a string to the serial port trying not to disturb
  1454. * any possible real use of the port...
  1455. */
  1456. static void serial_console_write(struct console *co, const char *s,
  1457. unsigned count)
  1458. {
  1459. struct uart_port *port = co->data;
  1460. struct sci_port *sci_port = to_sci_port(port);
  1461. unsigned short bits;
  1462. if (sci_port->enable)
  1463. sci_port->enable(port);
  1464. uart_console_write(port, s, count, serial_console_putchar);
  1465. /* wait until fifo is empty and last bit has been transmitted */
  1466. bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
  1467. while ((sci_in(port, SCxSR) & bits) != bits)
  1468. cpu_relax();
  1469. if (sci_port->disable)
  1470. sci_port->disable(port);
  1471. }
  1472. static int __devinit serial_console_setup(struct console *co, char *options)
  1473. {
  1474. struct sci_port *sci_port;
  1475. struct uart_port *port;
  1476. int baud = 115200;
  1477. int bits = 8;
  1478. int parity = 'n';
  1479. int flow = 'n';
  1480. int ret;
  1481. /*
  1482. * Check whether an invalid uart number has been specified, and
  1483. * if so, search for the first available port that does have
  1484. * console support.
  1485. */
  1486. if (co->index >= SCI_NPORTS)
  1487. co->index = 0;
  1488. if (co->data) {
  1489. port = co->data;
  1490. sci_port = to_sci_port(port);
  1491. } else {
  1492. sci_port = &sci_ports[co->index];
  1493. port = &sci_port->port;
  1494. co->data = port;
  1495. }
  1496. /*
  1497. * Also need to check port->type, we don't actually have any
  1498. * UPIO_PORT ports, but uart_report_port() handily misreports
  1499. * it anyways if we don't have a port available by the time this is
  1500. * called.
  1501. */
  1502. if (!port->type)
  1503. return -ENODEV;
  1504. sci_config_port(port, 0);
  1505. if (sci_port->enable)
  1506. sci_port->enable(port);
  1507. if (options)
  1508. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1509. ret = uart_set_options(port, co, baud, parity, bits, flow);
  1510. #if defined(__H8300H__) || defined(__H8300S__)
  1511. /* disable rx interrupt */
  1512. if (ret == 0)
  1513. sci_stop_rx(port);
  1514. #endif
  1515. /* TODO: disable clock */
  1516. return ret;
  1517. }
  1518. static struct console serial_console = {
  1519. .name = "ttySC",
  1520. .device = serial_console_device,
  1521. .write = serial_console_write,
  1522. .setup = serial_console_setup,
  1523. .flags = CON_PRINTBUFFER,
  1524. .index = -1,
  1525. };
  1526. static int __init sci_console_init(void)
  1527. {
  1528. register_console(&serial_console);
  1529. return 0;
  1530. }
  1531. console_initcall(sci_console_init);
  1532. static struct sci_port early_serial_port;
  1533. static struct console early_serial_console = {
  1534. .name = "early_ttySC",
  1535. .write = serial_console_write,
  1536. .flags = CON_PRINTBUFFER,
  1537. };
  1538. static char early_serial_buf[32];
  1539. #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
  1540. #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
  1541. #define SCI_CONSOLE (&serial_console)
  1542. #else
  1543. #define SCI_CONSOLE 0
  1544. #endif
  1545. static char banner[] __initdata =
  1546. KERN_INFO "SuperH SCI(F) driver initialized\n";
  1547. static struct uart_driver sci_uart_driver = {
  1548. .owner = THIS_MODULE,
  1549. .driver_name = "sci",
  1550. .dev_name = "ttySC",
  1551. .major = SCI_MAJOR,
  1552. .minor = SCI_MINOR_START,
  1553. .nr = SCI_NPORTS,
  1554. .cons = SCI_CONSOLE,
  1555. };
  1556. static int sci_remove(struct platform_device *dev)
  1557. {
  1558. struct sh_sci_priv *priv = platform_get_drvdata(dev);
  1559. struct sci_port *p;
  1560. unsigned long flags;
  1561. cpufreq_unregister_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
  1562. spin_lock_irqsave(&priv->lock, flags);
  1563. list_for_each_entry(p, &priv->ports, node) {
  1564. uart_remove_one_port(&sci_uart_driver, &p->port);
  1565. clk_put(p->iclk);
  1566. clk_put(p->fclk);
  1567. }
  1568. spin_unlock_irqrestore(&priv->lock, flags);
  1569. kfree(priv);
  1570. return 0;
  1571. }
  1572. static int __devinit sci_probe_single(struct platform_device *dev,
  1573. unsigned int index,
  1574. struct plat_sci_port *p,
  1575. struct sci_port *sciport)
  1576. {
  1577. struct sh_sci_priv *priv = platform_get_drvdata(dev);
  1578. unsigned long flags;
  1579. int ret;
  1580. /* Sanity check */
  1581. if (unlikely(index >= SCI_NPORTS)) {
  1582. dev_notice(&dev->dev, "Attempting to register port "
  1583. "%d when only %d are available.\n",
  1584. index+1, SCI_NPORTS);
  1585. dev_notice(&dev->dev, "Consider bumping "
  1586. "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
  1587. return 0;
  1588. }
  1589. ret = sci_init_single(dev, sciport, index, p);
  1590. if (ret)
  1591. return ret;
  1592. ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
  1593. if (ret)
  1594. return ret;
  1595. INIT_LIST_HEAD(&sciport->node);
  1596. spin_lock_irqsave(&priv->lock, flags);
  1597. list_add(&sciport->node, &priv->ports);
  1598. spin_unlock_irqrestore(&priv->lock, flags);
  1599. return 0;
  1600. }
  1601. /*
  1602. * Register a set of serial devices attached to a platform device. The
  1603. * list is terminated with a zero flags entry, which means we expect
  1604. * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
  1605. * remapping (such as sh64) should also set UPF_IOREMAP.
  1606. */
  1607. static int __devinit sci_probe(struct platform_device *dev)
  1608. {
  1609. struct plat_sci_port *p = dev->dev.platform_data;
  1610. struct sh_sci_priv *priv;
  1611. int i, ret = -EINVAL;
  1612. #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
  1613. if (is_early_platform_device(dev)) {
  1614. if (dev->id == -1)
  1615. return -ENOTSUPP;
  1616. early_serial_console.index = dev->id;
  1617. early_serial_console.data = &early_serial_port.port;
  1618. sci_init_single(NULL, &early_serial_port, dev->id, p);
  1619. serial_console_setup(&early_serial_console, early_serial_buf);
  1620. if (!strstr(early_serial_buf, "keep"))
  1621. early_serial_console.flags |= CON_BOOT;
  1622. register_console(&early_serial_console);
  1623. return 0;
  1624. }
  1625. #endif
  1626. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  1627. if (!priv)
  1628. return -ENOMEM;
  1629. INIT_LIST_HEAD(&priv->ports);
  1630. spin_lock_init(&priv->lock);
  1631. platform_set_drvdata(dev, priv);
  1632. priv->clk_nb.notifier_call = sci_notifier;
  1633. cpufreq_register_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
  1634. if (dev->id != -1) {
  1635. ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]);
  1636. if (ret)
  1637. goto err_unreg;
  1638. } else {
  1639. for (i = 0; p && p->flags != 0; p++, i++) {
  1640. ret = sci_probe_single(dev, i, p, &sci_ports[i]);
  1641. if (ret)
  1642. goto err_unreg;
  1643. }
  1644. }
  1645. #ifdef CONFIG_SH_STANDARD_BIOS
  1646. sh_bios_gdb_detach();
  1647. #endif
  1648. return 0;
  1649. err_unreg:
  1650. sci_remove(dev);
  1651. return ret;
  1652. }
  1653. static int sci_suspend(struct device *dev)
  1654. {
  1655. struct sh_sci_priv *priv = dev_get_drvdata(dev);
  1656. struct sci_port *p;
  1657. unsigned long flags;
  1658. spin_lock_irqsave(&priv->lock, flags);
  1659. list_for_each_entry(p, &priv->ports, node)
  1660. uart_suspend_port(&sci_uart_driver, &p->port);
  1661. spin_unlock_irqrestore(&priv->lock, flags);
  1662. return 0;
  1663. }
  1664. static int sci_resume(struct device *dev)
  1665. {
  1666. struct sh_sci_priv *priv = dev_get_drvdata(dev);
  1667. struct sci_port *p;
  1668. unsigned long flags;
  1669. spin_lock_irqsave(&priv->lock, flags);
  1670. list_for_each_entry(p, &priv->ports, node)
  1671. uart_resume_port(&sci_uart_driver, &p->port);
  1672. spin_unlock_irqrestore(&priv->lock, flags);
  1673. return 0;
  1674. }
  1675. static const struct dev_pm_ops sci_dev_pm_ops = {
  1676. .suspend = sci_suspend,
  1677. .resume = sci_resume,
  1678. };
  1679. static struct platform_driver sci_driver = {
  1680. .probe = sci_probe,
  1681. .remove = sci_remove,
  1682. .driver = {
  1683. .name = "sh-sci",
  1684. .owner = THIS_MODULE,
  1685. .pm = &sci_dev_pm_ops,
  1686. },
  1687. };
  1688. static int __init sci_init(void)
  1689. {
  1690. int ret;
  1691. printk(banner);
  1692. ret = uart_register_driver(&sci_uart_driver);
  1693. if (likely(ret == 0)) {
  1694. ret = platform_driver_register(&sci_driver);
  1695. if (unlikely(ret))
  1696. uart_unregister_driver(&sci_uart_driver);
  1697. }
  1698. return ret;
  1699. }
  1700. static void __exit sci_exit(void)
  1701. {
  1702. platform_driver_unregister(&sci_driver);
  1703. uart_unregister_driver(&sci_uart_driver);
  1704. }
  1705. #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
  1706. early_platform_init_buffer("earlyprintk", &sci_driver,
  1707. early_serial_buf, ARRAY_SIZE(early_serial_buf));
  1708. #endif
  1709. module_init(sci_init);
  1710. module_exit(sci_exit);
  1711. MODULE_LICENSE("GPL");
  1712. MODULE_ALIAS("platform:sh-sci");