sh-sci.c 53 KB

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