sh-sci.c 48 KB

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