sh-sci.c 63 KB

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