sh-sci.c 57 KB

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