sh-sci.c 58 KB

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