sh-sci.c 53 KB

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