sh-sci.c 58 KB

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