sh-sci.c 48 KB

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