sh-sci.c 48 KB

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