sh-sci.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211
  1. /*
  2. * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
  3. *
  4. * Copyright (C) 2002 - 2011 Paul Mundt
  5. * Copyright (C) 2015 Glider bvba
  6. * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
  7. *
  8. * based off of the old drivers/char/sh-sci.c by:
  9. *
  10. * Copyright (C) 1999, 2000 Niibe Yutaka
  11. * Copyright (C) 2000 Sugioka Toshinobu
  12. * Modified to support multiple serial ports. Stuart Menefy (May 2000).
  13. * Modified to support SecureEdge. David McCullough (2002)
  14. * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
  15. * Removed SH7300 support (Jul 2007).
  16. *
  17. * This file is subject to the terms and conditions of the GNU General Public
  18. * License. See the file "COPYING" in the main directory of this archive
  19. * for more details.
  20. */
  21. #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  22. #define SUPPORT_SYSRQ
  23. #endif
  24. #undef DEBUG
  25. #include <linux/clk.h>
  26. #include <linux/console.h>
  27. #include <linux/ctype.h>
  28. #include <linux/cpufreq.h>
  29. #include <linux/delay.h>
  30. #include <linux/dmaengine.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/err.h>
  33. #include <linux/errno.h>
  34. #include <linux/init.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/ioport.h>
  37. #include <linux/major.h>
  38. #include <linux/module.h>
  39. #include <linux/mm.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 "serial_mctrl_gpio.h"
  57. #include "sh-sci.h"
  58. /* Offsets into the sci_port->irqs array */
  59. enum {
  60. SCIx_ERI_IRQ,
  61. SCIx_RXI_IRQ,
  62. SCIx_TXI_IRQ,
  63. SCIx_BRI_IRQ,
  64. SCIx_NR_IRQS,
  65. SCIx_MUX_IRQ = SCIx_NR_IRQS, /* special case */
  66. };
  67. #define SCIx_IRQ_IS_MUXED(port) \
  68. ((port)->irqs[SCIx_ERI_IRQ] == \
  69. (port)->irqs[SCIx_RXI_IRQ]) || \
  70. ((port)->irqs[SCIx_ERI_IRQ] && \
  71. ((port)->irqs[SCIx_RXI_IRQ] < 0))
  72. enum SCI_CLKS {
  73. SCI_FCK, /* Functional Clock */
  74. SCI_SCK, /* Optional External Clock */
  75. SCI_BRG_INT, /* Optional BRG Internal Clock Source */
  76. SCI_SCIF_CLK, /* Optional BRG External Clock Source */
  77. SCI_NUM_CLKS
  78. };
  79. /* Bit x set means sampling rate x + 1 is supported */
  80. #define SCI_SR(x) BIT((x) - 1)
  81. #define SCI_SR_RANGE(x, y) GENMASK((y) - 1, (x) - 1)
  82. #define SCI_SR_SCIFAB SCI_SR(5) | SCI_SR(7) | SCI_SR(11) | \
  83. SCI_SR(13) | SCI_SR(16) | SCI_SR(17) | \
  84. SCI_SR(19) | SCI_SR(27)
  85. #define min_sr(_port) ffs((_port)->sampling_rate_mask)
  86. #define max_sr(_port) fls((_port)->sampling_rate_mask)
  87. /* Iterate over all supported sampling rates, from high to low */
  88. #define for_each_sr(_sr, _port) \
  89. for ((_sr) = max_sr(_port); (_sr) >= min_sr(_port); (_sr)--) \
  90. if ((_port)->sampling_rate_mask & SCI_SR((_sr)))
  91. struct sci_port {
  92. struct uart_port port;
  93. /* Platform configuration */
  94. struct plat_sci_port *cfg;
  95. unsigned int overrun_reg;
  96. unsigned int overrun_mask;
  97. unsigned int error_mask;
  98. unsigned int error_clear;
  99. unsigned int sampling_rate_mask;
  100. resource_size_t reg_size;
  101. struct mctrl_gpios *gpios;
  102. /* Break timer */
  103. struct timer_list break_timer;
  104. int break_flag;
  105. /* Clocks */
  106. struct clk *clks[SCI_NUM_CLKS];
  107. unsigned long clk_rates[SCI_NUM_CLKS];
  108. int irqs[SCIx_NR_IRQS];
  109. char *irqstr[SCIx_NR_IRQS];
  110. struct dma_chan *chan_tx;
  111. struct dma_chan *chan_rx;
  112. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  113. dma_cookie_t cookie_tx;
  114. dma_cookie_t cookie_rx[2];
  115. dma_cookie_t active_rx;
  116. dma_addr_t tx_dma_addr;
  117. unsigned int tx_dma_len;
  118. struct scatterlist sg_rx[2];
  119. void *rx_buf[2];
  120. size_t buf_len_rx;
  121. struct work_struct work_tx;
  122. struct timer_list rx_timer;
  123. unsigned int rx_timeout;
  124. #endif
  125. bool autorts;
  126. };
  127. #define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
  128. static struct sci_port sci_ports[SCI_NPORTS];
  129. static struct uart_driver sci_uart_driver;
  130. static inline struct sci_port *
  131. to_sci_port(struct uart_port *uart)
  132. {
  133. return container_of(uart, struct sci_port, port);
  134. }
  135. struct plat_sci_reg {
  136. u8 offset, size;
  137. };
  138. /* Helper for invalidating specific entries of an inherited map. */
  139. #define sci_reg_invalid { .offset = 0, .size = 0 }
  140. static const struct plat_sci_reg sci_regmap[SCIx_NR_REGTYPES][SCIx_NR_REGS] = {
  141. [SCIx_PROBE_REGTYPE] = {
  142. [0 ... SCIx_NR_REGS - 1] = sci_reg_invalid,
  143. },
  144. /*
  145. * Common SCI definitions, dependent on the port's regshift
  146. * value.
  147. */
  148. [SCIx_SCI_REGTYPE] = {
  149. [SCSMR] = { 0x00, 8 },
  150. [SCBRR] = { 0x01, 8 },
  151. [SCSCR] = { 0x02, 8 },
  152. [SCxTDR] = { 0x03, 8 },
  153. [SCxSR] = { 0x04, 8 },
  154. [SCxRDR] = { 0x05, 8 },
  155. [SCFCR] = sci_reg_invalid,
  156. [SCFDR] = sci_reg_invalid,
  157. [SCTFDR] = sci_reg_invalid,
  158. [SCRFDR] = sci_reg_invalid,
  159. [SCSPTR] = sci_reg_invalid,
  160. [SCLSR] = sci_reg_invalid,
  161. [HSSRR] = sci_reg_invalid,
  162. [SCPCR] = sci_reg_invalid,
  163. [SCPDR] = sci_reg_invalid,
  164. [SCDL] = sci_reg_invalid,
  165. [SCCKS] = sci_reg_invalid,
  166. },
  167. /*
  168. * Common definitions for legacy IrDA ports, dependent on
  169. * regshift value.
  170. */
  171. [SCIx_IRDA_REGTYPE] = {
  172. [SCSMR] = { 0x00, 8 },
  173. [SCBRR] = { 0x01, 8 },
  174. [SCSCR] = { 0x02, 8 },
  175. [SCxTDR] = { 0x03, 8 },
  176. [SCxSR] = { 0x04, 8 },
  177. [SCxRDR] = { 0x05, 8 },
  178. [SCFCR] = { 0x06, 8 },
  179. [SCFDR] = { 0x07, 16 },
  180. [SCTFDR] = sci_reg_invalid,
  181. [SCRFDR] = sci_reg_invalid,
  182. [SCSPTR] = sci_reg_invalid,
  183. [SCLSR] = sci_reg_invalid,
  184. [HSSRR] = sci_reg_invalid,
  185. [SCPCR] = sci_reg_invalid,
  186. [SCPDR] = sci_reg_invalid,
  187. [SCDL] = sci_reg_invalid,
  188. [SCCKS] = sci_reg_invalid,
  189. },
  190. /*
  191. * Common SCIFA definitions.
  192. */
  193. [SCIx_SCIFA_REGTYPE] = {
  194. [SCSMR] = { 0x00, 16 },
  195. [SCBRR] = { 0x04, 8 },
  196. [SCSCR] = { 0x08, 16 },
  197. [SCxTDR] = { 0x20, 8 },
  198. [SCxSR] = { 0x14, 16 },
  199. [SCxRDR] = { 0x24, 8 },
  200. [SCFCR] = { 0x18, 16 },
  201. [SCFDR] = { 0x1c, 16 },
  202. [SCTFDR] = sci_reg_invalid,
  203. [SCRFDR] = sci_reg_invalid,
  204. [SCSPTR] = sci_reg_invalid,
  205. [SCLSR] = sci_reg_invalid,
  206. [HSSRR] = sci_reg_invalid,
  207. [SCPCR] = { 0x30, 16 },
  208. [SCPDR] = { 0x34, 16 },
  209. [SCDL] = sci_reg_invalid,
  210. [SCCKS] = sci_reg_invalid,
  211. },
  212. /*
  213. * Common SCIFB definitions.
  214. */
  215. [SCIx_SCIFB_REGTYPE] = {
  216. [SCSMR] = { 0x00, 16 },
  217. [SCBRR] = { 0x04, 8 },
  218. [SCSCR] = { 0x08, 16 },
  219. [SCxTDR] = { 0x40, 8 },
  220. [SCxSR] = { 0x14, 16 },
  221. [SCxRDR] = { 0x60, 8 },
  222. [SCFCR] = { 0x18, 16 },
  223. [SCFDR] = sci_reg_invalid,
  224. [SCTFDR] = { 0x38, 16 },
  225. [SCRFDR] = { 0x3c, 16 },
  226. [SCSPTR] = sci_reg_invalid,
  227. [SCLSR] = sci_reg_invalid,
  228. [HSSRR] = sci_reg_invalid,
  229. [SCPCR] = { 0x30, 16 },
  230. [SCPDR] = { 0x34, 16 },
  231. [SCDL] = sci_reg_invalid,
  232. [SCCKS] = sci_reg_invalid,
  233. },
  234. /*
  235. * Common SH-2(A) SCIF definitions for ports with FIFO data
  236. * count registers.
  237. */
  238. [SCIx_SH2_SCIF_FIFODATA_REGTYPE] = {
  239. [SCSMR] = { 0x00, 16 },
  240. [SCBRR] = { 0x04, 8 },
  241. [SCSCR] = { 0x08, 16 },
  242. [SCxTDR] = { 0x0c, 8 },
  243. [SCxSR] = { 0x10, 16 },
  244. [SCxRDR] = { 0x14, 8 },
  245. [SCFCR] = { 0x18, 16 },
  246. [SCFDR] = { 0x1c, 16 },
  247. [SCTFDR] = sci_reg_invalid,
  248. [SCRFDR] = sci_reg_invalid,
  249. [SCSPTR] = { 0x20, 16 },
  250. [SCLSR] = { 0x24, 16 },
  251. [HSSRR] = sci_reg_invalid,
  252. [SCPCR] = sci_reg_invalid,
  253. [SCPDR] = sci_reg_invalid,
  254. [SCDL] = sci_reg_invalid,
  255. [SCCKS] = sci_reg_invalid,
  256. },
  257. /*
  258. * Common SH-3 SCIF definitions.
  259. */
  260. [SCIx_SH3_SCIF_REGTYPE] = {
  261. [SCSMR] = { 0x00, 8 },
  262. [SCBRR] = { 0x02, 8 },
  263. [SCSCR] = { 0x04, 8 },
  264. [SCxTDR] = { 0x06, 8 },
  265. [SCxSR] = { 0x08, 16 },
  266. [SCxRDR] = { 0x0a, 8 },
  267. [SCFCR] = { 0x0c, 8 },
  268. [SCFDR] = { 0x0e, 16 },
  269. [SCTFDR] = sci_reg_invalid,
  270. [SCRFDR] = sci_reg_invalid,
  271. [SCSPTR] = sci_reg_invalid,
  272. [SCLSR] = sci_reg_invalid,
  273. [HSSRR] = sci_reg_invalid,
  274. [SCPCR] = sci_reg_invalid,
  275. [SCPDR] = sci_reg_invalid,
  276. [SCDL] = sci_reg_invalid,
  277. [SCCKS] = sci_reg_invalid,
  278. },
  279. /*
  280. * Common SH-4(A) SCIF(B) definitions.
  281. */
  282. [SCIx_SH4_SCIF_REGTYPE] = {
  283. [SCSMR] = { 0x00, 16 },
  284. [SCBRR] = { 0x04, 8 },
  285. [SCSCR] = { 0x08, 16 },
  286. [SCxTDR] = { 0x0c, 8 },
  287. [SCxSR] = { 0x10, 16 },
  288. [SCxRDR] = { 0x14, 8 },
  289. [SCFCR] = { 0x18, 16 },
  290. [SCFDR] = { 0x1c, 16 },
  291. [SCTFDR] = sci_reg_invalid,
  292. [SCRFDR] = sci_reg_invalid,
  293. [SCSPTR] = { 0x20, 16 },
  294. [SCLSR] = { 0x24, 16 },
  295. [HSSRR] = sci_reg_invalid,
  296. [SCPCR] = sci_reg_invalid,
  297. [SCPDR] = sci_reg_invalid,
  298. [SCDL] = sci_reg_invalid,
  299. [SCCKS] = sci_reg_invalid,
  300. },
  301. /*
  302. * Common SCIF definitions for ports with a Baud Rate Generator for
  303. * External Clock (BRG).
  304. */
  305. [SCIx_SH4_SCIF_BRG_REGTYPE] = {
  306. [SCSMR] = { 0x00, 16 },
  307. [SCBRR] = { 0x04, 8 },
  308. [SCSCR] = { 0x08, 16 },
  309. [SCxTDR] = { 0x0c, 8 },
  310. [SCxSR] = { 0x10, 16 },
  311. [SCxRDR] = { 0x14, 8 },
  312. [SCFCR] = { 0x18, 16 },
  313. [SCFDR] = { 0x1c, 16 },
  314. [SCTFDR] = sci_reg_invalid,
  315. [SCRFDR] = sci_reg_invalid,
  316. [SCSPTR] = { 0x20, 16 },
  317. [SCLSR] = { 0x24, 16 },
  318. [HSSRR] = sci_reg_invalid,
  319. [SCPCR] = sci_reg_invalid,
  320. [SCPDR] = sci_reg_invalid,
  321. [SCDL] = { 0x30, 16 },
  322. [SCCKS] = { 0x34, 16 },
  323. },
  324. /*
  325. * Common HSCIF definitions.
  326. */
  327. [SCIx_HSCIF_REGTYPE] = {
  328. [SCSMR] = { 0x00, 16 },
  329. [SCBRR] = { 0x04, 8 },
  330. [SCSCR] = { 0x08, 16 },
  331. [SCxTDR] = { 0x0c, 8 },
  332. [SCxSR] = { 0x10, 16 },
  333. [SCxRDR] = { 0x14, 8 },
  334. [SCFCR] = { 0x18, 16 },
  335. [SCFDR] = { 0x1c, 16 },
  336. [SCTFDR] = sci_reg_invalid,
  337. [SCRFDR] = sci_reg_invalid,
  338. [SCSPTR] = { 0x20, 16 },
  339. [SCLSR] = { 0x24, 16 },
  340. [HSSRR] = { 0x40, 16 },
  341. [SCPCR] = sci_reg_invalid,
  342. [SCPDR] = sci_reg_invalid,
  343. [SCDL] = { 0x30, 16 },
  344. [SCCKS] = { 0x34, 16 },
  345. },
  346. /*
  347. * Common SH-4(A) SCIF(B) definitions for ports without an SCSPTR
  348. * register.
  349. */
  350. [SCIx_SH4_SCIF_NO_SCSPTR_REGTYPE] = {
  351. [SCSMR] = { 0x00, 16 },
  352. [SCBRR] = { 0x04, 8 },
  353. [SCSCR] = { 0x08, 16 },
  354. [SCxTDR] = { 0x0c, 8 },
  355. [SCxSR] = { 0x10, 16 },
  356. [SCxRDR] = { 0x14, 8 },
  357. [SCFCR] = { 0x18, 16 },
  358. [SCFDR] = { 0x1c, 16 },
  359. [SCTFDR] = sci_reg_invalid,
  360. [SCRFDR] = sci_reg_invalid,
  361. [SCSPTR] = sci_reg_invalid,
  362. [SCLSR] = { 0x24, 16 },
  363. [HSSRR] = sci_reg_invalid,
  364. [SCPCR] = sci_reg_invalid,
  365. [SCPDR] = sci_reg_invalid,
  366. [SCDL] = sci_reg_invalid,
  367. [SCCKS] = sci_reg_invalid,
  368. },
  369. /*
  370. * Common SH-4(A) SCIF(B) definitions for ports with FIFO data
  371. * count registers.
  372. */
  373. [SCIx_SH4_SCIF_FIFODATA_REGTYPE] = {
  374. [SCSMR] = { 0x00, 16 },
  375. [SCBRR] = { 0x04, 8 },
  376. [SCSCR] = { 0x08, 16 },
  377. [SCxTDR] = { 0x0c, 8 },
  378. [SCxSR] = { 0x10, 16 },
  379. [SCxRDR] = { 0x14, 8 },
  380. [SCFCR] = { 0x18, 16 },
  381. [SCFDR] = { 0x1c, 16 },
  382. [SCTFDR] = { 0x1c, 16 }, /* aliased to SCFDR */
  383. [SCRFDR] = { 0x20, 16 },
  384. [SCSPTR] = { 0x24, 16 },
  385. [SCLSR] = { 0x28, 16 },
  386. [HSSRR] = sci_reg_invalid,
  387. [SCPCR] = sci_reg_invalid,
  388. [SCPDR] = sci_reg_invalid,
  389. [SCDL] = sci_reg_invalid,
  390. [SCCKS] = sci_reg_invalid,
  391. },
  392. /*
  393. * SH7705-style SCIF(B) ports, lacking both SCSPTR and SCLSR
  394. * registers.
  395. */
  396. [SCIx_SH7705_SCIF_REGTYPE] = {
  397. [SCSMR] = { 0x00, 16 },
  398. [SCBRR] = { 0x04, 8 },
  399. [SCSCR] = { 0x08, 16 },
  400. [SCxTDR] = { 0x20, 8 },
  401. [SCxSR] = { 0x14, 16 },
  402. [SCxRDR] = { 0x24, 8 },
  403. [SCFCR] = { 0x18, 16 },
  404. [SCFDR] = { 0x1c, 16 },
  405. [SCTFDR] = sci_reg_invalid,
  406. [SCRFDR] = sci_reg_invalid,
  407. [SCSPTR] = sci_reg_invalid,
  408. [SCLSR] = sci_reg_invalid,
  409. [HSSRR] = sci_reg_invalid,
  410. [SCPCR] = sci_reg_invalid,
  411. [SCPDR] = sci_reg_invalid,
  412. [SCDL] = sci_reg_invalid,
  413. [SCCKS] = sci_reg_invalid,
  414. },
  415. };
  416. #define sci_getreg(up, offset) (sci_regmap[to_sci_port(up)->cfg->regtype] + offset)
  417. /*
  418. * The "offset" here is rather misleading, in that it refers to an enum
  419. * value relative to the port mapping rather than the fixed offset
  420. * itself, which needs to be manually retrieved from the platform's
  421. * register map for the given port.
  422. */
  423. static unsigned int sci_serial_in(struct uart_port *p, int offset)
  424. {
  425. const struct plat_sci_reg *reg = sci_getreg(p, offset);
  426. if (reg->size == 8)
  427. return ioread8(p->membase + (reg->offset << p->regshift));
  428. else if (reg->size == 16)
  429. return ioread16(p->membase + (reg->offset << p->regshift));
  430. else
  431. WARN(1, "Invalid register access\n");
  432. return 0;
  433. }
  434. static void sci_serial_out(struct uart_port *p, int offset, int value)
  435. {
  436. const struct plat_sci_reg *reg = sci_getreg(p, offset);
  437. if (reg->size == 8)
  438. iowrite8(value, p->membase + (reg->offset << p->regshift));
  439. else if (reg->size == 16)
  440. iowrite16(value, p->membase + (reg->offset << p->regshift));
  441. else
  442. WARN(1, "Invalid register access\n");
  443. }
  444. static int sci_probe_regmap(struct plat_sci_port *cfg)
  445. {
  446. switch (cfg->type) {
  447. case PORT_SCI:
  448. cfg->regtype = SCIx_SCI_REGTYPE;
  449. break;
  450. case PORT_IRDA:
  451. cfg->regtype = SCIx_IRDA_REGTYPE;
  452. break;
  453. case PORT_SCIFA:
  454. cfg->regtype = SCIx_SCIFA_REGTYPE;
  455. break;
  456. case PORT_SCIFB:
  457. cfg->regtype = SCIx_SCIFB_REGTYPE;
  458. break;
  459. case PORT_SCIF:
  460. /*
  461. * The SH-4 is a bit of a misnomer here, although that's
  462. * where this particular port layout originated. This
  463. * configuration (or some slight variation thereof)
  464. * remains the dominant model for all SCIFs.
  465. */
  466. cfg->regtype = SCIx_SH4_SCIF_REGTYPE;
  467. break;
  468. case PORT_HSCIF:
  469. cfg->regtype = SCIx_HSCIF_REGTYPE;
  470. break;
  471. default:
  472. pr_err("Can't probe register map for given port\n");
  473. return -EINVAL;
  474. }
  475. return 0;
  476. }
  477. static void sci_port_enable(struct sci_port *sci_port)
  478. {
  479. unsigned int i;
  480. if (!sci_port->port.dev)
  481. return;
  482. pm_runtime_get_sync(sci_port->port.dev);
  483. for (i = 0; i < SCI_NUM_CLKS; i++) {
  484. clk_prepare_enable(sci_port->clks[i]);
  485. sci_port->clk_rates[i] = clk_get_rate(sci_port->clks[i]);
  486. }
  487. sci_port->port.uartclk = sci_port->clk_rates[SCI_FCK];
  488. }
  489. static void sci_port_disable(struct sci_port *sci_port)
  490. {
  491. unsigned int i;
  492. if (!sci_port->port.dev)
  493. return;
  494. /* Cancel the break timer to ensure that the timer handler will not try
  495. * to access the hardware with clocks and power disabled. Reset the
  496. * break flag to make the break debouncing state machine ready for the
  497. * next break.
  498. */
  499. del_timer_sync(&sci_port->break_timer);
  500. sci_port->break_flag = 0;
  501. for (i = SCI_NUM_CLKS; i-- > 0; )
  502. clk_disable_unprepare(sci_port->clks[i]);
  503. pm_runtime_put_sync(sci_port->port.dev);
  504. }
  505. static inline unsigned long port_rx_irq_mask(struct uart_port *port)
  506. {
  507. /*
  508. * Not all ports (such as SCIFA) will support REIE. Rather than
  509. * special-casing the port type, we check the port initialization
  510. * IRQ enable mask to see whether the IRQ is desired at all. If
  511. * it's unset, it's logically inferred that there's no point in
  512. * testing for it.
  513. */
  514. return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
  515. }
  516. static void sci_start_tx(struct uart_port *port)
  517. {
  518. struct sci_port *s = to_sci_port(port);
  519. unsigned short ctrl;
  520. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  521. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
  522. u16 new, scr = serial_port_in(port, SCSCR);
  523. if (s->chan_tx)
  524. new = scr | SCSCR_TDRQE;
  525. else
  526. new = scr & ~SCSCR_TDRQE;
  527. if (new != scr)
  528. serial_port_out(port, SCSCR, new);
  529. }
  530. if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
  531. dma_submit_error(s->cookie_tx)) {
  532. s->cookie_tx = 0;
  533. schedule_work(&s->work_tx);
  534. }
  535. #endif
  536. if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
  537. /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
  538. ctrl = serial_port_in(port, SCSCR);
  539. serial_port_out(port, SCSCR, ctrl | SCSCR_TIE);
  540. }
  541. }
  542. static void sci_stop_tx(struct uart_port *port)
  543. {
  544. unsigned short ctrl;
  545. /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
  546. ctrl = serial_port_in(port, SCSCR);
  547. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
  548. ctrl &= ~SCSCR_TDRQE;
  549. ctrl &= ~SCSCR_TIE;
  550. serial_port_out(port, SCSCR, ctrl);
  551. }
  552. static void sci_start_rx(struct uart_port *port)
  553. {
  554. unsigned short ctrl;
  555. ctrl = serial_port_in(port, SCSCR) | port_rx_irq_mask(port);
  556. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
  557. ctrl &= ~SCSCR_RDRQE;
  558. serial_port_out(port, SCSCR, ctrl);
  559. }
  560. static void sci_stop_rx(struct uart_port *port)
  561. {
  562. unsigned short ctrl;
  563. ctrl = serial_port_in(port, SCSCR);
  564. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
  565. ctrl &= ~SCSCR_RDRQE;
  566. ctrl &= ~port_rx_irq_mask(port);
  567. serial_port_out(port, SCSCR, ctrl);
  568. }
  569. static void sci_clear_SCxSR(struct uart_port *port, unsigned int mask)
  570. {
  571. if (port->type == PORT_SCI) {
  572. /* Just store the mask */
  573. serial_port_out(port, SCxSR, mask);
  574. } else if (to_sci_port(port)->overrun_mask == SCIFA_ORER) {
  575. /* SCIFA/SCIFB and SCIF on SH7705/SH7720/SH7721 */
  576. /* Only clear the status bits we want to clear */
  577. serial_port_out(port, SCxSR,
  578. serial_port_in(port, SCxSR) & mask);
  579. } else {
  580. /* Store the mask, clear parity/framing errors */
  581. serial_port_out(port, SCxSR, mask & ~(SCIF_FERC | SCIF_PERC));
  582. }
  583. }
  584. #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || \
  585. defined(CONFIG_SERIAL_SH_SCI_EARLYCON)
  586. #ifdef CONFIG_CONSOLE_POLL
  587. static int sci_poll_get_char(struct uart_port *port)
  588. {
  589. unsigned short status;
  590. int c;
  591. do {
  592. status = serial_port_in(port, SCxSR);
  593. if (status & SCxSR_ERRORS(port)) {
  594. sci_clear_SCxSR(port, SCxSR_ERROR_CLEAR(port));
  595. continue;
  596. }
  597. break;
  598. } while (1);
  599. if (!(status & SCxSR_RDxF(port)))
  600. return NO_POLL_CHAR;
  601. c = serial_port_in(port, SCxRDR);
  602. /* Dummy read */
  603. serial_port_in(port, SCxSR);
  604. sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
  605. return c;
  606. }
  607. #endif
  608. static void sci_poll_put_char(struct uart_port *port, unsigned char c)
  609. {
  610. unsigned short status;
  611. do {
  612. status = serial_port_in(port, SCxSR);
  613. } while (!(status & SCxSR_TDxE(port)));
  614. serial_port_out(port, SCxTDR, c);
  615. sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
  616. }
  617. #endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE ||
  618. CONFIG_SERIAL_SH_SCI_EARLYCON */
  619. static void sci_init_pins(struct uart_port *port, unsigned int cflag)
  620. {
  621. struct sci_port *s = to_sci_port(port);
  622. /*
  623. * Use port-specific handler if provided.
  624. */
  625. if (s->cfg->ops && s->cfg->ops->init_pins) {
  626. s->cfg->ops->init_pins(port, cflag);
  627. return;
  628. }
  629. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
  630. u16 ctrl = serial_port_in(port, SCPCR);
  631. /* Enable RXD and TXD pin functions */
  632. ctrl &= ~(SCPCR_RXDC | SCPCR_TXDC);
  633. if (to_sci_port(port)->cfg->capabilities & SCIx_HAVE_RTSCTS) {
  634. /* RTS# is output, driven 1 */
  635. ctrl |= SCPCR_RTSC;
  636. serial_port_out(port, SCPDR,
  637. serial_port_in(port, SCPDR) | SCPDR_RTSD);
  638. /* Enable CTS# pin function */
  639. ctrl &= ~SCPCR_CTSC;
  640. }
  641. serial_port_out(port, SCPCR, ctrl);
  642. } else if (sci_getreg(port, SCSPTR)->size) {
  643. u16 status = serial_port_in(port, SCSPTR);
  644. /* RTS# is output, driven 1 */
  645. status |= SCSPTR_RTSIO | SCSPTR_RTSDT;
  646. /* CTS# and SCK are inputs */
  647. status &= ~(SCSPTR_CTSIO | SCSPTR_SCKIO);
  648. serial_port_out(port, SCSPTR, status);
  649. }
  650. }
  651. static int sci_txfill(struct uart_port *port)
  652. {
  653. const struct plat_sci_reg *reg;
  654. reg = sci_getreg(port, SCTFDR);
  655. if (reg->size)
  656. return serial_port_in(port, SCTFDR) & ((port->fifosize << 1) - 1);
  657. reg = sci_getreg(port, SCFDR);
  658. if (reg->size)
  659. return serial_port_in(port, SCFDR) >> 8;
  660. return !(serial_port_in(port, SCxSR) & SCI_TDRE);
  661. }
  662. static int sci_txroom(struct uart_port *port)
  663. {
  664. return port->fifosize - sci_txfill(port);
  665. }
  666. static int sci_rxfill(struct uart_port *port)
  667. {
  668. const struct plat_sci_reg *reg;
  669. reg = sci_getreg(port, SCRFDR);
  670. if (reg->size)
  671. return serial_port_in(port, SCRFDR) & ((port->fifosize << 1) - 1);
  672. reg = sci_getreg(port, SCFDR);
  673. if (reg->size)
  674. return serial_port_in(port, SCFDR) & ((port->fifosize << 1) - 1);
  675. return (serial_port_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
  676. }
  677. /*
  678. * SCI helper for checking the state of the muxed port/RXD pins.
  679. */
  680. static inline int sci_rxd_in(struct uart_port *port)
  681. {
  682. struct sci_port *s = to_sci_port(port);
  683. if (s->cfg->port_reg <= 0)
  684. return 1;
  685. /* Cast for ARM damage */
  686. return !!__raw_readb((void __iomem *)(uintptr_t)s->cfg->port_reg);
  687. }
  688. /* ********************************************************************** *
  689. * the interrupt related routines *
  690. * ********************************************************************** */
  691. static void sci_transmit_chars(struct uart_port *port)
  692. {
  693. struct circ_buf *xmit = &port->state->xmit;
  694. unsigned int stopped = uart_tx_stopped(port);
  695. unsigned short status;
  696. unsigned short ctrl;
  697. int count;
  698. status = serial_port_in(port, SCxSR);
  699. if (!(status & SCxSR_TDxE(port))) {
  700. ctrl = serial_port_in(port, SCSCR);
  701. if (uart_circ_empty(xmit))
  702. ctrl &= ~SCSCR_TIE;
  703. else
  704. ctrl |= SCSCR_TIE;
  705. serial_port_out(port, SCSCR, ctrl);
  706. return;
  707. }
  708. count = sci_txroom(port);
  709. do {
  710. unsigned char c;
  711. if (port->x_char) {
  712. c = port->x_char;
  713. port->x_char = 0;
  714. } else if (!uart_circ_empty(xmit) && !stopped) {
  715. c = xmit->buf[xmit->tail];
  716. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  717. } else {
  718. break;
  719. }
  720. serial_port_out(port, SCxTDR, c);
  721. port->icount.tx++;
  722. } while (--count > 0);
  723. sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port));
  724. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  725. uart_write_wakeup(port);
  726. if (uart_circ_empty(xmit)) {
  727. sci_stop_tx(port);
  728. } else {
  729. ctrl = serial_port_in(port, SCSCR);
  730. if (port->type != PORT_SCI) {
  731. serial_port_in(port, SCxSR); /* Dummy read */
  732. sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port));
  733. }
  734. ctrl |= SCSCR_TIE;
  735. serial_port_out(port, SCSCR, ctrl);
  736. }
  737. }
  738. /* On SH3, SCIF may read end-of-break as a space->mark char */
  739. #define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
  740. static void sci_receive_chars(struct uart_port *port)
  741. {
  742. struct sci_port *sci_port = to_sci_port(port);
  743. struct tty_port *tport = &port->state->port;
  744. int i, count, copied = 0;
  745. unsigned short status;
  746. unsigned char flag;
  747. status = serial_port_in(port, SCxSR);
  748. if (!(status & SCxSR_RDxF(port)))
  749. return;
  750. while (1) {
  751. /* Don't copy more bytes than there is room for in the buffer */
  752. count = tty_buffer_request_room(tport, sci_rxfill(port));
  753. /* If for any reason we can't copy more data, we're done! */
  754. if (count == 0)
  755. break;
  756. if (port->type == PORT_SCI) {
  757. char c = serial_port_in(port, SCxRDR);
  758. if (uart_handle_sysrq_char(port, c) ||
  759. sci_port->break_flag)
  760. count = 0;
  761. else
  762. tty_insert_flip_char(tport, c, TTY_NORMAL);
  763. } else {
  764. for (i = 0; i < count; i++) {
  765. char c = serial_port_in(port, SCxRDR);
  766. status = serial_port_in(port, SCxSR);
  767. #if defined(CONFIG_CPU_SH3)
  768. /* Skip "chars" during break */
  769. if (sci_port->break_flag) {
  770. if ((c == 0) &&
  771. (status & SCxSR_FER(port))) {
  772. count--; i--;
  773. continue;
  774. }
  775. /* Nonzero => end-of-break */
  776. dev_dbg(port->dev, "debounce<%02x>\n", c);
  777. sci_port->break_flag = 0;
  778. if (STEPFN(c)) {
  779. count--; i--;
  780. continue;
  781. }
  782. }
  783. #endif /* CONFIG_CPU_SH3 */
  784. if (uart_handle_sysrq_char(port, c)) {
  785. count--; i--;
  786. continue;
  787. }
  788. /* Store data and status */
  789. if (status & SCxSR_FER(port)) {
  790. flag = TTY_FRAME;
  791. port->icount.frame++;
  792. dev_notice(port->dev, "frame error\n");
  793. } else if (status & SCxSR_PER(port)) {
  794. flag = TTY_PARITY;
  795. port->icount.parity++;
  796. dev_notice(port->dev, "parity error\n");
  797. } else
  798. flag = TTY_NORMAL;
  799. tty_insert_flip_char(tport, c, flag);
  800. }
  801. }
  802. serial_port_in(port, SCxSR); /* dummy read */
  803. sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
  804. copied += count;
  805. port->icount.rx += count;
  806. }
  807. if (copied) {
  808. /* Tell the rest of the system the news. New characters! */
  809. tty_flip_buffer_push(tport);
  810. } else {
  811. serial_port_in(port, SCxSR); /* dummy read */
  812. sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
  813. }
  814. }
  815. #define SCI_BREAK_JIFFIES (HZ/20)
  816. /*
  817. * The sci generates interrupts during the break,
  818. * 1 per millisecond or so during the break period, for 9600 baud.
  819. * So dont bother disabling interrupts.
  820. * But dont want more than 1 break event.
  821. * Use a kernel timer to periodically poll the rx line until
  822. * the break is finished.
  823. */
  824. static inline void sci_schedule_break_timer(struct sci_port *port)
  825. {
  826. mod_timer(&port->break_timer, jiffies + SCI_BREAK_JIFFIES);
  827. }
  828. /* Ensure that two consecutive samples find the break over. */
  829. static void sci_break_timer(unsigned long data)
  830. {
  831. struct sci_port *port = (struct sci_port *)data;
  832. if (sci_rxd_in(&port->port) == 0) {
  833. port->break_flag = 1;
  834. sci_schedule_break_timer(port);
  835. } else if (port->break_flag == 1) {
  836. /* break is over. */
  837. port->break_flag = 2;
  838. sci_schedule_break_timer(port);
  839. } else
  840. port->break_flag = 0;
  841. }
  842. static int sci_handle_errors(struct uart_port *port)
  843. {
  844. int copied = 0;
  845. unsigned short status = serial_port_in(port, SCxSR);
  846. struct tty_port *tport = &port->state->port;
  847. struct sci_port *s = to_sci_port(port);
  848. /* Handle overruns */
  849. if (status & s->overrun_mask) {
  850. port->icount.overrun++;
  851. /* overrun error */
  852. if (tty_insert_flip_char(tport, 0, TTY_OVERRUN))
  853. copied++;
  854. dev_notice(port->dev, "overrun error\n");
  855. }
  856. if (status & SCxSR_FER(port)) {
  857. if (sci_rxd_in(port) == 0) {
  858. /* Notify of BREAK */
  859. struct sci_port *sci_port = to_sci_port(port);
  860. if (!sci_port->break_flag) {
  861. port->icount.brk++;
  862. sci_port->break_flag = 1;
  863. sci_schedule_break_timer(sci_port);
  864. /* Do sysrq handling. */
  865. if (uart_handle_break(port))
  866. return 0;
  867. dev_dbg(port->dev, "BREAK detected\n");
  868. if (tty_insert_flip_char(tport, 0, TTY_BREAK))
  869. copied++;
  870. }
  871. } else {
  872. /* frame error */
  873. port->icount.frame++;
  874. if (tty_insert_flip_char(tport, 0, TTY_FRAME))
  875. copied++;
  876. dev_notice(port->dev, "frame error\n");
  877. }
  878. }
  879. if (status & SCxSR_PER(port)) {
  880. /* parity error */
  881. port->icount.parity++;
  882. if (tty_insert_flip_char(tport, 0, TTY_PARITY))
  883. copied++;
  884. dev_notice(port->dev, "parity error\n");
  885. }
  886. if (copied)
  887. tty_flip_buffer_push(tport);
  888. return copied;
  889. }
  890. static int sci_handle_fifo_overrun(struct uart_port *port)
  891. {
  892. struct tty_port *tport = &port->state->port;
  893. struct sci_port *s = to_sci_port(port);
  894. const struct plat_sci_reg *reg;
  895. int copied = 0;
  896. u16 status;
  897. reg = sci_getreg(port, s->overrun_reg);
  898. if (!reg->size)
  899. return 0;
  900. status = serial_port_in(port, s->overrun_reg);
  901. if (status & s->overrun_mask) {
  902. status &= ~s->overrun_mask;
  903. serial_port_out(port, s->overrun_reg, status);
  904. port->icount.overrun++;
  905. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  906. tty_flip_buffer_push(tport);
  907. dev_dbg(port->dev, "overrun error\n");
  908. copied++;
  909. }
  910. return copied;
  911. }
  912. static int sci_handle_breaks(struct uart_port *port)
  913. {
  914. int copied = 0;
  915. unsigned short status = serial_port_in(port, SCxSR);
  916. struct tty_port *tport = &port->state->port;
  917. struct sci_port *s = to_sci_port(port);
  918. if (uart_handle_break(port))
  919. return 0;
  920. if (!s->break_flag && status & SCxSR_BRK(port)) {
  921. #if defined(CONFIG_CPU_SH3)
  922. /* Debounce break */
  923. s->break_flag = 1;
  924. #endif
  925. port->icount.brk++;
  926. /* Notify of BREAK */
  927. if (tty_insert_flip_char(tport, 0, TTY_BREAK))
  928. copied++;
  929. dev_dbg(port->dev, "BREAK detected\n");
  930. }
  931. if (copied)
  932. tty_flip_buffer_push(tport);
  933. copied += sci_handle_fifo_overrun(port);
  934. return copied;
  935. }
  936. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  937. static void sci_dma_tx_complete(void *arg)
  938. {
  939. struct sci_port *s = arg;
  940. struct uart_port *port = &s->port;
  941. struct circ_buf *xmit = &port->state->xmit;
  942. unsigned long flags;
  943. dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
  944. spin_lock_irqsave(&port->lock, flags);
  945. xmit->tail += s->tx_dma_len;
  946. xmit->tail &= UART_XMIT_SIZE - 1;
  947. port->icount.tx += s->tx_dma_len;
  948. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  949. uart_write_wakeup(port);
  950. if (!uart_circ_empty(xmit)) {
  951. s->cookie_tx = 0;
  952. schedule_work(&s->work_tx);
  953. } else {
  954. s->cookie_tx = -EINVAL;
  955. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
  956. u16 ctrl = serial_port_in(port, SCSCR);
  957. serial_port_out(port, SCSCR, ctrl & ~SCSCR_TIE);
  958. }
  959. }
  960. spin_unlock_irqrestore(&port->lock, flags);
  961. }
  962. /* Locking: called with port lock held */
  963. static int sci_dma_rx_push(struct sci_port *s, void *buf, size_t count)
  964. {
  965. struct uart_port *port = &s->port;
  966. struct tty_port *tport = &port->state->port;
  967. int copied;
  968. copied = tty_insert_flip_string(tport, buf, count);
  969. if (copied < count)
  970. port->icount.buf_overrun++;
  971. port->icount.rx += copied;
  972. return copied;
  973. }
  974. static int sci_dma_rx_find_active(struct sci_port *s)
  975. {
  976. unsigned int i;
  977. for (i = 0; i < ARRAY_SIZE(s->cookie_rx); i++)
  978. if (s->active_rx == s->cookie_rx[i])
  979. return i;
  980. return -1;
  981. }
  982. static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
  983. {
  984. struct dma_chan *chan = s->chan_rx;
  985. struct uart_port *port = &s->port;
  986. unsigned long flags;
  987. spin_lock_irqsave(&port->lock, flags);
  988. s->chan_rx = NULL;
  989. s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
  990. spin_unlock_irqrestore(&port->lock, flags);
  991. dmaengine_terminate_all(chan);
  992. dma_free_coherent(chan->device->dev, s->buf_len_rx * 2, s->rx_buf[0],
  993. sg_dma_address(&s->sg_rx[0]));
  994. dma_release_channel(chan);
  995. if (enable_pio)
  996. sci_start_rx(port);
  997. }
  998. static void sci_dma_rx_complete(void *arg)
  999. {
  1000. struct sci_port *s = arg;
  1001. struct dma_chan *chan = s->chan_rx;
  1002. struct uart_port *port = &s->port;
  1003. struct dma_async_tx_descriptor *desc;
  1004. unsigned long flags;
  1005. int active, count = 0;
  1006. dev_dbg(port->dev, "%s(%d) active cookie %d\n", __func__, port->line,
  1007. s->active_rx);
  1008. spin_lock_irqsave(&port->lock, flags);
  1009. active = sci_dma_rx_find_active(s);
  1010. if (active >= 0)
  1011. count = sci_dma_rx_push(s, s->rx_buf[active], s->buf_len_rx);
  1012. mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
  1013. if (count)
  1014. tty_flip_buffer_push(&port->state->port);
  1015. desc = dmaengine_prep_slave_sg(s->chan_rx, &s->sg_rx[active], 1,
  1016. DMA_DEV_TO_MEM,
  1017. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1018. if (!desc)
  1019. goto fail;
  1020. desc->callback = sci_dma_rx_complete;
  1021. desc->callback_param = s;
  1022. s->cookie_rx[active] = dmaengine_submit(desc);
  1023. if (dma_submit_error(s->cookie_rx[active]))
  1024. goto fail;
  1025. s->active_rx = s->cookie_rx[!active];
  1026. dma_async_issue_pending(chan);
  1027. spin_unlock_irqrestore(&port->lock, flags);
  1028. dev_dbg(port->dev, "%s: cookie %d #%d, new active cookie %d\n",
  1029. __func__, s->cookie_rx[active], active, s->active_rx);
  1030. return;
  1031. fail:
  1032. spin_unlock_irqrestore(&port->lock, flags);
  1033. dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
  1034. sci_rx_dma_release(s, true);
  1035. }
  1036. static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
  1037. {
  1038. struct dma_chan *chan = s->chan_tx;
  1039. struct uart_port *port = &s->port;
  1040. unsigned long flags;
  1041. spin_lock_irqsave(&port->lock, flags);
  1042. s->chan_tx = NULL;
  1043. s->cookie_tx = -EINVAL;
  1044. spin_unlock_irqrestore(&port->lock, flags);
  1045. dmaengine_terminate_all(chan);
  1046. dma_unmap_single(chan->device->dev, s->tx_dma_addr, UART_XMIT_SIZE,
  1047. DMA_TO_DEVICE);
  1048. dma_release_channel(chan);
  1049. if (enable_pio)
  1050. sci_start_tx(port);
  1051. }
  1052. static void sci_submit_rx(struct sci_port *s)
  1053. {
  1054. struct dma_chan *chan = s->chan_rx;
  1055. int i;
  1056. for (i = 0; i < 2; i++) {
  1057. struct scatterlist *sg = &s->sg_rx[i];
  1058. struct dma_async_tx_descriptor *desc;
  1059. desc = dmaengine_prep_slave_sg(chan,
  1060. sg, 1, DMA_DEV_TO_MEM,
  1061. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1062. if (!desc)
  1063. goto fail;
  1064. desc->callback = sci_dma_rx_complete;
  1065. desc->callback_param = s;
  1066. s->cookie_rx[i] = dmaengine_submit(desc);
  1067. if (dma_submit_error(s->cookie_rx[i]))
  1068. goto fail;
  1069. }
  1070. s->active_rx = s->cookie_rx[0];
  1071. dma_async_issue_pending(chan);
  1072. return;
  1073. fail:
  1074. if (i)
  1075. dmaengine_terminate_all(chan);
  1076. for (i = 0; i < 2; i++)
  1077. s->cookie_rx[i] = -EINVAL;
  1078. s->active_rx = -EINVAL;
  1079. sci_rx_dma_release(s, true);
  1080. }
  1081. static void work_fn_tx(struct work_struct *work)
  1082. {
  1083. struct sci_port *s = container_of(work, struct sci_port, work_tx);
  1084. struct dma_async_tx_descriptor *desc;
  1085. struct dma_chan *chan = s->chan_tx;
  1086. struct uart_port *port = &s->port;
  1087. struct circ_buf *xmit = &port->state->xmit;
  1088. dma_addr_t buf;
  1089. /*
  1090. * DMA is idle now.
  1091. * Port xmit buffer is already mapped, and it is one page... Just adjust
  1092. * offsets and lengths. Since it is a circular buffer, we have to
  1093. * transmit till the end, and then the rest. Take the port lock to get a
  1094. * consistent xmit buffer state.
  1095. */
  1096. spin_lock_irq(&port->lock);
  1097. buf = s->tx_dma_addr + (xmit->tail & (UART_XMIT_SIZE - 1));
  1098. s->tx_dma_len = min_t(unsigned int,
  1099. CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
  1100. CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
  1101. spin_unlock_irq(&port->lock);
  1102. desc = dmaengine_prep_slave_single(chan, buf, s->tx_dma_len,
  1103. DMA_MEM_TO_DEV,
  1104. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1105. if (!desc) {
  1106. dev_warn(port->dev, "Failed preparing Tx DMA descriptor\n");
  1107. /* switch to PIO */
  1108. sci_tx_dma_release(s, true);
  1109. return;
  1110. }
  1111. dma_sync_single_for_device(chan->device->dev, buf, s->tx_dma_len,
  1112. DMA_TO_DEVICE);
  1113. spin_lock_irq(&port->lock);
  1114. desc->callback = sci_dma_tx_complete;
  1115. desc->callback_param = s;
  1116. spin_unlock_irq(&port->lock);
  1117. s->cookie_tx = dmaengine_submit(desc);
  1118. if (dma_submit_error(s->cookie_tx)) {
  1119. dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
  1120. /* switch to PIO */
  1121. sci_tx_dma_release(s, true);
  1122. return;
  1123. }
  1124. dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n",
  1125. __func__, xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
  1126. dma_async_issue_pending(chan);
  1127. }
  1128. static void rx_timer_fn(unsigned long arg)
  1129. {
  1130. struct sci_port *s = (struct sci_port *)arg;
  1131. struct dma_chan *chan = s->chan_rx;
  1132. struct uart_port *port = &s->port;
  1133. struct dma_tx_state state;
  1134. enum dma_status status;
  1135. unsigned long flags;
  1136. unsigned int read;
  1137. int active, count;
  1138. u16 scr;
  1139. dev_dbg(port->dev, "DMA Rx timed out\n");
  1140. spin_lock_irqsave(&port->lock, flags);
  1141. active = sci_dma_rx_find_active(s);
  1142. if (active < 0) {
  1143. spin_unlock_irqrestore(&port->lock, flags);
  1144. return;
  1145. }
  1146. status = dmaengine_tx_status(s->chan_rx, s->active_rx, &state);
  1147. if (status == DMA_COMPLETE) {
  1148. spin_unlock_irqrestore(&port->lock, flags);
  1149. dev_dbg(port->dev, "Cookie %d #%d has already completed\n",
  1150. s->active_rx, active);
  1151. /* Let packet complete handler take care of the packet */
  1152. return;
  1153. }
  1154. dmaengine_pause(chan);
  1155. /*
  1156. * sometimes DMA transfer doesn't stop even if it is stopped and
  1157. * data keeps on coming until transaction is complete so check
  1158. * for DMA_COMPLETE again
  1159. * Let packet complete handler take care of the packet
  1160. */
  1161. status = dmaengine_tx_status(s->chan_rx, s->active_rx, &state);
  1162. if (status == DMA_COMPLETE) {
  1163. spin_unlock_irqrestore(&port->lock, flags);
  1164. dev_dbg(port->dev, "Transaction complete after DMA engine was stopped");
  1165. return;
  1166. }
  1167. /* Handle incomplete DMA receive */
  1168. dmaengine_terminate_all(s->chan_rx);
  1169. read = sg_dma_len(&s->sg_rx[active]) - state.residue;
  1170. if (read) {
  1171. count = sci_dma_rx_push(s, s->rx_buf[active], read);
  1172. if (count)
  1173. tty_flip_buffer_push(&port->state->port);
  1174. }
  1175. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
  1176. sci_submit_rx(s);
  1177. /* Direct new serial port interrupts back to CPU */
  1178. scr = serial_port_in(port, SCSCR);
  1179. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
  1180. scr &= ~SCSCR_RDRQE;
  1181. enable_irq(s->irqs[SCIx_RXI_IRQ]);
  1182. }
  1183. serial_port_out(port, SCSCR, scr | SCSCR_RIE);
  1184. spin_unlock_irqrestore(&port->lock, flags);
  1185. }
  1186. static struct dma_chan *sci_request_dma_chan(struct uart_port *port,
  1187. enum dma_transfer_direction dir,
  1188. unsigned int id)
  1189. {
  1190. dma_cap_mask_t mask;
  1191. struct dma_chan *chan;
  1192. struct dma_slave_config cfg;
  1193. int ret;
  1194. dma_cap_zero(mask);
  1195. dma_cap_set(DMA_SLAVE, mask);
  1196. chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
  1197. (void *)(unsigned long)id, port->dev,
  1198. dir == DMA_MEM_TO_DEV ? "tx" : "rx");
  1199. if (!chan) {
  1200. dev_warn(port->dev,
  1201. "dma_request_slave_channel_compat failed\n");
  1202. return NULL;
  1203. }
  1204. memset(&cfg, 0, sizeof(cfg));
  1205. cfg.direction = dir;
  1206. if (dir == DMA_MEM_TO_DEV) {
  1207. cfg.dst_addr = port->mapbase +
  1208. (sci_getreg(port, SCxTDR)->offset << port->regshift);
  1209. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  1210. } else {
  1211. cfg.src_addr = port->mapbase +
  1212. (sci_getreg(port, SCxRDR)->offset << port->regshift);
  1213. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  1214. }
  1215. ret = dmaengine_slave_config(chan, &cfg);
  1216. if (ret) {
  1217. dev_warn(port->dev, "dmaengine_slave_config failed %d\n", ret);
  1218. dma_release_channel(chan);
  1219. return NULL;
  1220. }
  1221. return chan;
  1222. }
  1223. static void sci_request_dma(struct uart_port *port)
  1224. {
  1225. struct sci_port *s = to_sci_port(port);
  1226. struct dma_chan *chan;
  1227. dev_dbg(port->dev, "%s: port %d\n", __func__, port->line);
  1228. if (!port->dev->of_node &&
  1229. (s->cfg->dma_slave_tx <= 0 || s->cfg->dma_slave_rx <= 0))
  1230. return;
  1231. s->cookie_tx = -EINVAL;
  1232. chan = sci_request_dma_chan(port, DMA_MEM_TO_DEV, s->cfg->dma_slave_tx);
  1233. dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
  1234. if (chan) {
  1235. s->chan_tx = chan;
  1236. /* UART circular tx buffer is an aligned page. */
  1237. s->tx_dma_addr = dma_map_single(chan->device->dev,
  1238. port->state->xmit.buf,
  1239. UART_XMIT_SIZE,
  1240. DMA_TO_DEVICE);
  1241. if (dma_mapping_error(chan->device->dev, s->tx_dma_addr)) {
  1242. dev_warn(port->dev, "Failed mapping Tx DMA descriptor\n");
  1243. dma_release_channel(chan);
  1244. s->chan_tx = NULL;
  1245. } else {
  1246. dev_dbg(port->dev, "%s: mapped %lu@%p to %pad\n",
  1247. __func__, UART_XMIT_SIZE,
  1248. port->state->xmit.buf, &s->tx_dma_addr);
  1249. }
  1250. INIT_WORK(&s->work_tx, work_fn_tx);
  1251. }
  1252. chan = sci_request_dma_chan(port, DMA_DEV_TO_MEM, s->cfg->dma_slave_rx);
  1253. dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
  1254. if (chan) {
  1255. unsigned int i;
  1256. dma_addr_t dma;
  1257. void *buf;
  1258. s->chan_rx = chan;
  1259. s->buf_len_rx = 2 * max_t(size_t, 16, port->fifosize);
  1260. buf = dma_alloc_coherent(chan->device->dev, s->buf_len_rx * 2,
  1261. &dma, GFP_KERNEL);
  1262. if (!buf) {
  1263. dev_warn(port->dev,
  1264. "Failed to allocate Rx dma buffer, using PIO\n");
  1265. dma_release_channel(chan);
  1266. s->chan_rx = NULL;
  1267. return;
  1268. }
  1269. for (i = 0; i < 2; i++) {
  1270. struct scatterlist *sg = &s->sg_rx[i];
  1271. sg_init_table(sg, 1);
  1272. s->rx_buf[i] = buf;
  1273. sg_dma_address(sg) = dma;
  1274. sg_dma_len(sg) = s->buf_len_rx;
  1275. buf += s->buf_len_rx;
  1276. dma += s->buf_len_rx;
  1277. }
  1278. setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
  1279. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
  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->chan_tx)
  1287. sci_tx_dma_release(s, false);
  1288. if (s->chan_rx)
  1289. sci_rx_dma_release(s, false);
  1290. }
  1291. #else
  1292. static inline void sci_request_dma(struct uart_port *port)
  1293. {
  1294. }
  1295. static inline void sci_free_dma(struct uart_port *port)
  1296. {
  1297. }
  1298. #endif
  1299. static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
  1300. {
  1301. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  1302. struct uart_port *port = ptr;
  1303. struct sci_port *s = to_sci_port(port);
  1304. if (s->chan_rx) {
  1305. u16 scr = serial_port_in(port, SCSCR);
  1306. u16 ssr = serial_port_in(port, SCxSR);
  1307. /* Disable future Rx interrupts */
  1308. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
  1309. disable_irq_nosync(irq);
  1310. scr |= SCSCR_RDRQE;
  1311. } else {
  1312. scr &= ~SCSCR_RIE;
  1313. sci_submit_rx(s);
  1314. }
  1315. serial_port_out(port, SCSCR, scr);
  1316. /* Clear current interrupt */
  1317. serial_port_out(port, SCxSR,
  1318. ssr & ~(SCIF_DR | SCxSR_RDxF(port)));
  1319. dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
  1320. jiffies, s->rx_timeout);
  1321. mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
  1322. return IRQ_HANDLED;
  1323. }
  1324. #endif
  1325. /* I think sci_receive_chars has to be called irrespective
  1326. * of whether the I_IXOFF is set, otherwise, how is the interrupt
  1327. * to be disabled?
  1328. */
  1329. sci_receive_chars(ptr);
  1330. return IRQ_HANDLED;
  1331. }
  1332. static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
  1333. {
  1334. struct uart_port *port = ptr;
  1335. unsigned long flags;
  1336. spin_lock_irqsave(&port->lock, flags);
  1337. sci_transmit_chars(port);
  1338. spin_unlock_irqrestore(&port->lock, flags);
  1339. return IRQ_HANDLED;
  1340. }
  1341. static irqreturn_t sci_er_interrupt(int irq, void *ptr)
  1342. {
  1343. struct uart_port *port = ptr;
  1344. struct sci_port *s = to_sci_port(port);
  1345. /* Handle errors */
  1346. if (port->type == PORT_SCI) {
  1347. if (sci_handle_errors(port)) {
  1348. /* discard character in rx buffer */
  1349. serial_port_in(port, SCxSR);
  1350. sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
  1351. }
  1352. } else {
  1353. sci_handle_fifo_overrun(port);
  1354. if (!s->chan_rx)
  1355. sci_receive_chars(ptr);
  1356. }
  1357. sci_clear_SCxSR(port, SCxSR_ERROR_CLEAR(port));
  1358. /* Kick the transmission */
  1359. if (!s->chan_tx)
  1360. sci_tx_interrupt(irq, ptr);
  1361. return IRQ_HANDLED;
  1362. }
  1363. static irqreturn_t sci_br_interrupt(int irq, void *ptr)
  1364. {
  1365. struct uart_port *port = ptr;
  1366. /* Handle BREAKs */
  1367. sci_handle_breaks(port);
  1368. sci_clear_SCxSR(port, SCxSR_BREAK_CLEAR(port));
  1369. return IRQ_HANDLED;
  1370. }
  1371. static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
  1372. {
  1373. unsigned short ssr_status, scr_status, err_enabled, orer_status = 0;
  1374. struct uart_port *port = ptr;
  1375. struct sci_port *s = to_sci_port(port);
  1376. irqreturn_t ret = IRQ_NONE;
  1377. ssr_status = serial_port_in(port, SCxSR);
  1378. scr_status = serial_port_in(port, SCSCR);
  1379. if (s->overrun_reg == SCxSR)
  1380. orer_status = ssr_status;
  1381. else {
  1382. if (sci_getreg(port, s->overrun_reg)->size)
  1383. orer_status = serial_port_in(port, s->overrun_reg);
  1384. }
  1385. err_enabled = scr_status & port_rx_irq_mask(port);
  1386. /* Tx Interrupt */
  1387. if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
  1388. !s->chan_tx)
  1389. ret = sci_tx_interrupt(irq, ptr);
  1390. /*
  1391. * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
  1392. * DR flags
  1393. */
  1394. if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
  1395. (scr_status & SCSCR_RIE))
  1396. ret = sci_rx_interrupt(irq, ptr);
  1397. /* Error Interrupt */
  1398. if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
  1399. ret = sci_er_interrupt(irq, ptr);
  1400. /* Break Interrupt */
  1401. if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
  1402. ret = sci_br_interrupt(irq, ptr);
  1403. /* Overrun Interrupt */
  1404. if (orer_status & s->overrun_mask) {
  1405. sci_handle_fifo_overrun(port);
  1406. ret = IRQ_HANDLED;
  1407. }
  1408. return ret;
  1409. }
  1410. static const struct sci_irq_desc {
  1411. const char *desc;
  1412. irq_handler_t handler;
  1413. } sci_irq_desc[] = {
  1414. /*
  1415. * Split out handlers, the default case.
  1416. */
  1417. [SCIx_ERI_IRQ] = {
  1418. .desc = "rx err",
  1419. .handler = sci_er_interrupt,
  1420. },
  1421. [SCIx_RXI_IRQ] = {
  1422. .desc = "rx full",
  1423. .handler = sci_rx_interrupt,
  1424. },
  1425. [SCIx_TXI_IRQ] = {
  1426. .desc = "tx empty",
  1427. .handler = sci_tx_interrupt,
  1428. },
  1429. [SCIx_BRI_IRQ] = {
  1430. .desc = "break",
  1431. .handler = sci_br_interrupt,
  1432. },
  1433. /*
  1434. * Special muxed handler.
  1435. */
  1436. [SCIx_MUX_IRQ] = {
  1437. .desc = "mux",
  1438. .handler = sci_mpxed_interrupt,
  1439. },
  1440. };
  1441. static int sci_request_irq(struct sci_port *port)
  1442. {
  1443. struct uart_port *up = &port->port;
  1444. int i, j, ret = 0;
  1445. for (i = j = 0; i < SCIx_NR_IRQS; i++, j++) {
  1446. const struct sci_irq_desc *desc;
  1447. int irq;
  1448. if (SCIx_IRQ_IS_MUXED(port)) {
  1449. i = SCIx_MUX_IRQ;
  1450. irq = up->irq;
  1451. } else {
  1452. irq = port->irqs[i];
  1453. /*
  1454. * Certain port types won't support all of the
  1455. * available interrupt sources.
  1456. */
  1457. if (unlikely(irq < 0))
  1458. continue;
  1459. }
  1460. desc = sci_irq_desc + i;
  1461. port->irqstr[j] = kasprintf(GFP_KERNEL, "%s:%s",
  1462. dev_name(up->dev), desc->desc);
  1463. if (!port->irqstr[j])
  1464. goto out_nomem;
  1465. ret = request_irq(irq, desc->handler, up->irqflags,
  1466. port->irqstr[j], port);
  1467. if (unlikely(ret)) {
  1468. dev_err(up->dev, "Can't allocate %s IRQ\n", desc->desc);
  1469. goto out_noirq;
  1470. }
  1471. }
  1472. return 0;
  1473. out_noirq:
  1474. while (--i >= 0)
  1475. free_irq(port->irqs[i], port);
  1476. out_nomem:
  1477. while (--j >= 0)
  1478. kfree(port->irqstr[j]);
  1479. return ret;
  1480. }
  1481. static void sci_free_irq(struct sci_port *port)
  1482. {
  1483. int i;
  1484. /*
  1485. * Intentionally in reverse order so we iterate over the muxed
  1486. * IRQ first.
  1487. */
  1488. for (i = 0; i < SCIx_NR_IRQS; i++) {
  1489. int irq = port->irqs[i];
  1490. /*
  1491. * Certain port types won't support all of the available
  1492. * interrupt sources.
  1493. */
  1494. if (unlikely(irq < 0))
  1495. continue;
  1496. free_irq(port->irqs[i], port);
  1497. kfree(port->irqstr[i]);
  1498. if (SCIx_IRQ_IS_MUXED(port)) {
  1499. /* If there's only one IRQ, we're done. */
  1500. return;
  1501. }
  1502. }
  1503. }
  1504. static unsigned int sci_tx_empty(struct uart_port *port)
  1505. {
  1506. unsigned short status = serial_port_in(port, SCxSR);
  1507. unsigned short in_tx_fifo = sci_txfill(port);
  1508. return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
  1509. }
  1510. static void sci_set_rts(struct uart_port *port, bool state)
  1511. {
  1512. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
  1513. u16 data = serial_port_in(port, SCPDR);
  1514. /* Active low */
  1515. if (state)
  1516. data &= ~SCPDR_RTSD;
  1517. else
  1518. data |= SCPDR_RTSD;
  1519. serial_port_out(port, SCPDR, data);
  1520. /* RTS# is output */
  1521. serial_port_out(port, SCPCR,
  1522. serial_port_in(port, SCPCR) | SCPCR_RTSC);
  1523. } else if (sci_getreg(port, SCSPTR)->size) {
  1524. u16 ctrl = serial_port_in(port, SCSPTR);
  1525. /* Active low */
  1526. if (state)
  1527. ctrl &= ~SCSPTR_RTSDT;
  1528. else
  1529. ctrl |= SCSPTR_RTSDT;
  1530. serial_port_out(port, SCSPTR, ctrl);
  1531. }
  1532. }
  1533. static bool sci_get_cts(struct uart_port *port)
  1534. {
  1535. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
  1536. /* Active low */
  1537. return !(serial_port_in(port, SCPDR) & SCPDR_CTSD);
  1538. } else if (sci_getreg(port, SCSPTR)->size) {
  1539. /* Active low */
  1540. return !(serial_port_in(port, SCSPTR) & SCSPTR_CTSDT);
  1541. }
  1542. return true;
  1543. }
  1544. /*
  1545. * Modem control is a bit of a mixed bag for SCI(F) ports. Generally
  1546. * CTS/RTS is supported in hardware by at least one port and controlled
  1547. * via SCSPTR (SCxPCR for SCIFA/B parts), or external pins (presently
  1548. * handled via the ->init_pins() op, which is a bit of a one-way street,
  1549. * lacking any ability to defer pin control -- this will later be
  1550. * converted over to the GPIO framework).
  1551. *
  1552. * Other modes (such as loopback) are supported generically on certain
  1553. * port types, but not others. For these it's sufficient to test for the
  1554. * existence of the support register and simply ignore the port type.
  1555. */
  1556. static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1557. {
  1558. struct sci_port *s = to_sci_port(port);
  1559. if (mctrl & TIOCM_LOOP) {
  1560. const struct plat_sci_reg *reg;
  1561. /*
  1562. * Standard loopback mode for SCFCR ports.
  1563. */
  1564. reg = sci_getreg(port, SCFCR);
  1565. if (reg->size)
  1566. serial_port_out(port, SCFCR,
  1567. serial_port_in(port, SCFCR) |
  1568. SCFCR_LOOP);
  1569. }
  1570. mctrl_gpio_set(s->gpios, mctrl);
  1571. if (!(s->cfg->capabilities & SCIx_HAVE_RTSCTS))
  1572. return;
  1573. if (!(mctrl & TIOCM_RTS)) {
  1574. /* Disable Auto RTS */
  1575. serial_port_out(port, SCFCR,
  1576. serial_port_in(port, SCFCR) & ~SCFCR_MCE);
  1577. /* Clear RTS */
  1578. sci_set_rts(port, 0);
  1579. } else if (s->autorts) {
  1580. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
  1581. /* Enable RTS# pin function */
  1582. serial_port_out(port, SCPCR,
  1583. serial_port_in(port, SCPCR) & ~SCPCR_RTSC);
  1584. }
  1585. /* Enable Auto RTS */
  1586. serial_port_out(port, SCFCR,
  1587. serial_port_in(port, SCFCR) | SCFCR_MCE);
  1588. } else {
  1589. /* Set RTS */
  1590. sci_set_rts(port, 1);
  1591. }
  1592. }
  1593. static unsigned int sci_get_mctrl(struct uart_port *port)
  1594. {
  1595. struct sci_port *s = to_sci_port(port);
  1596. struct mctrl_gpios *gpios = s->gpios;
  1597. unsigned int mctrl = 0;
  1598. mctrl_gpio_get(gpios, &mctrl);
  1599. /*
  1600. * CTS/RTS is handled in hardware when supported, while nothing
  1601. * else is wired up.
  1602. */
  1603. if (s->autorts) {
  1604. if (sci_get_cts(port))
  1605. mctrl |= TIOCM_CTS;
  1606. } else if (IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(gpios, UART_GPIO_CTS))) {
  1607. mctrl |= TIOCM_CTS;
  1608. }
  1609. if (IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(gpios, UART_GPIO_DSR)))
  1610. mctrl |= TIOCM_DSR;
  1611. if (IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(gpios, UART_GPIO_DCD)))
  1612. mctrl |= TIOCM_CAR;
  1613. return mctrl;
  1614. }
  1615. static void sci_enable_ms(struct uart_port *port)
  1616. {
  1617. mctrl_gpio_enable_ms(to_sci_port(port)->gpios);
  1618. }
  1619. static void sci_break_ctl(struct uart_port *port, int break_state)
  1620. {
  1621. unsigned short scscr, scsptr;
  1622. /* check wheter the port has SCSPTR */
  1623. if (!sci_getreg(port, SCSPTR)->size) {
  1624. /*
  1625. * Not supported by hardware. Most parts couple break and rx
  1626. * interrupts together, with break detection always enabled.
  1627. */
  1628. return;
  1629. }
  1630. scsptr = serial_port_in(port, SCSPTR);
  1631. scscr = serial_port_in(port, SCSCR);
  1632. if (break_state == -1) {
  1633. scsptr = (scsptr | SCSPTR_SPB2IO) & ~SCSPTR_SPB2DT;
  1634. scscr &= ~SCSCR_TE;
  1635. } else {
  1636. scsptr = (scsptr | SCSPTR_SPB2DT) & ~SCSPTR_SPB2IO;
  1637. scscr |= SCSCR_TE;
  1638. }
  1639. serial_port_out(port, SCSPTR, scsptr);
  1640. serial_port_out(port, SCSCR, scscr);
  1641. }
  1642. static int sci_startup(struct uart_port *port)
  1643. {
  1644. struct sci_port *s = to_sci_port(port);
  1645. int ret;
  1646. dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
  1647. ret = sci_request_irq(s);
  1648. if (unlikely(ret < 0))
  1649. return ret;
  1650. sci_request_dma(port);
  1651. return 0;
  1652. }
  1653. static void sci_shutdown(struct uart_port *port)
  1654. {
  1655. struct sci_port *s = to_sci_port(port);
  1656. unsigned long flags;
  1657. u16 scr;
  1658. dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
  1659. s->autorts = false;
  1660. mctrl_gpio_disable_ms(to_sci_port(port)->gpios);
  1661. spin_lock_irqsave(&port->lock, flags);
  1662. sci_stop_rx(port);
  1663. sci_stop_tx(port);
  1664. /* Stop RX and TX, disable related interrupts, keep clock source */
  1665. scr = serial_port_in(port, SCSCR);
  1666. serial_port_out(port, SCSCR, scr & (SCSCR_CKE1 | SCSCR_CKE0));
  1667. spin_unlock_irqrestore(&port->lock, flags);
  1668. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  1669. if (s->chan_rx) {
  1670. dev_dbg(port->dev, "%s(%d) deleting rx_timer\n", __func__,
  1671. port->line);
  1672. del_timer_sync(&s->rx_timer);
  1673. }
  1674. #endif
  1675. sci_free_dma(port);
  1676. sci_free_irq(s);
  1677. }
  1678. static int sci_sck_calc(struct sci_port *s, unsigned int bps,
  1679. unsigned int *srr)
  1680. {
  1681. unsigned long freq = s->clk_rates[SCI_SCK];
  1682. int err, min_err = INT_MAX;
  1683. unsigned int sr;
  1684. if (s->port.type != PORT_HSCIF)
  1685. freq *= 2;
  1686. for_each_sr(sr, s) {
  1687. err = DIV_ROUND_CLOSEST(freq, sr) - bps;
  1688. if (abs(err) >= abs(min_err))
  1689. continue;
  1690. min_err = err;
  1691. *srr = sr - 1;
  1692. if (!err)
  1693. break;
  1694. }
  1695. dev_dbg(s->port.dev, "SCK: %u%+d bps using SR %u\n", bps, min_err,
  1696. *srr + 1);
  1697. return min_err;
  1698. }
  1699. static int sci_brg_calc(struct sci_port *s, unsigned int bps,
  1700. unsigned long freq, unsigned int *dlr,
  1701. unsigned int *srr)
  1702. {
  1703. int err, min_err = INT_MAX;
  1704. unsigned int sr, dl;
  1705. if (s->port.type != PORT_HSCIF)
  1706. freq *= 2;
  1707. for_each_sr(sr, s) {
  1708. dl = DIV_ROUND_CLOSEST(freq, sr * bps);
  1709. dl = clamp(dl, 1U, 65535U);
  1710. err = DIV_ROUND_CLOSEST(freq, sr * dl) - bps;
  1711. if (abs(err) >= abs(min_err))
  1712. continue;
  1713. min_err = err;
  1714. *dlr = dl;
  1715. *srr = sr - 1;
  1716. if (!err)
  1717. break;
  1718. }
  1719. dev_dbg(s->port.dev, "BRG: %u%+d bps using DL %u SR %u\n", bps,
  1720. min_err, *dlr, *srr + 1);
  1721. return min_err;
  1722. }
  1723. /* calculate sample rate, BRR, and clock select */
  1724. static int sci_scbrr_calc(struct sci_port *s, unsigned int bps,
  1725. unsigned int *brr, unsigned int *srr,
  1726. unsigned int *cks)
  1727. {
  1728. unsigned long freq = s->clk_rates[SCI_FCK];
  1729. unsigned int sr, br, prediv, scrate, c;
  1730. int err, min_err = INT_MAX;
  1731. if (s->port.type != PORT_HSCIF)
  1732. freq *= 2;
  1733. /*
  1734. * Find the combination of sample rate and clock select with the
  1735. * smallest deviation from the desired baud rate.
  1736. * Prefer high sample rates to maximise the receive margin.
  1737. *
  1738. * M: Receive margin (%)
  1739. * N: Ratio of bit rate to clock (N = sampling rate)
  1740. * D: Clock duty (D = 0 to 1.0)
  1741. * L: Frame length (L = 9 to 12)
  1742. * F: Absolute value of clock frequency deviation
  1743. *
  1744. * M = |(0.5 - 1 / 2 * N) - ((L - 0.5) * F) -
  1745. * (|D - 0.5| / N * (1 + F))|
  1746. * NOTE: Usually, treat D for 0.5, F is 0 by this calculation.
  1747. */
  1748. for_each_sr(sr, s) {
  1749. for (c = 0; c <= 3; c++) {
  1750. /* integerized formulas from HSCIF documentation */
  1751. prediv = sr * (1 << (2 * c + 1));
  1752. /*
  1753. * We need to calculate:
  1754. *
  1755. * br = freq / (prediv * bps) clamped to [1..256]
  1756. * err = freq / (br * prediv) - bps
  1757. *
  1758. * Watch out for overflow when calculating the desired
  1759. * sampling clock rate!
  1760. */
  1761. if (bps > UINT_MAX / prediv)
  1762. break;
  1763. scrate = prediv * bps;
  1764. br = DIV_ROUND_CLOSEST(freq, scrate);
  1765. br = clamp(br, 1U, 256U);
  1766. err = DIV_ROUND_CLOSEST(freq, br * prediv) - bps;
  1767. if (abs(err) >= abs(min_err))
  1768. continue;
  1769. min_err = err;
  1770. *brr = br - 1;
  1771. *srr = sr - 1;
  1772. *cks = c;
  1773. if (!err)
  1774. goto found;
  1775. }
  1776. }
  1777. found:
  1778. dev_dbg(s->port.dev, "BRR: %u%+d bps using N %u SR %u cks %u\n", bps,
  1779. min_err, *brr, *srr + 1, *cks);
  1780. return min_err;
  1781. }
  1782. static void sci_reset(struct uart_port *port)
  1783. {
  1784. const struct plat_sci_reg *reg;
  1785. unsigned int status;
  1786. do {
  1787. status = serial_port_in(port, SCxSR);
  1788. } while (!(status & SCxSR_TEND(port)));
  1789. serial_port_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
  1790. reg = sci_getreg(port, SCFCR);
  1791. if (reg->size)
  1792. serial_port_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
  1793. sci_clear_SCxSR(port,
  1794. SCxSR_RDxF_CLEAR(port) & SCxSR_ERROR_CLEAR(port) &
  1795. SCxSR_BREAK_CLEAR(port));
  1796. if (sci_getreg(port, SCLSR)->size) {
  1797. status = serial_port_in(port, SCLSR);
  1798. status &= ~(SCLSR_TO | SCLSR_ORER);
  1799. serial_port_out(port, SCLSR, status);
  1800. }
  1801. }
  1802. static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
  1803. struct ktermios *old)
  1804. {
  1805. unsigned int baud, smr_val = SCSMR_ASYNC, scr_val = 0, i;
  1806. unsigned int brr = 255, cks = 0, srr = 15, dl = 0, sccks = 0;
  1807. unsigned int brr1 = 255, cks1 = 0, srr1 = 15, dl1 = 0;
  1808. struct sci_port *s = to_sci_port(port);
  1809. const struct plat_sci_reg *reg;
  1810. int min_err = INT_MAX, err;
  1811. unsigned long max_freq = 0;
  1812. int best_clk = -1;
  1813. if ((termios->c_cflag & CSIZE) == CS7)
  1814. smr_val |= SCSMR_CHR;
  1815. if (termios->c_cflag & PARENB)
  1816. smr_val |= SCSMR_PE;
  1817. if (termios->c_cflag & PARODD)
  1818. smr_val |= SCSMR_PE | SCSMR_ODD;
  1819. if (termios->c_cflag & CSTOPB)
  1820. smr_val |= SCSMR_STOP;
  1821. /*
  1822. * earlyprintk comes here early on with port->uartclk set to zero.
  1823. * the clock framework is not up and running at this point so here
  1824. * we assume that 115200 is the maximum baud rate. please note that
  1825. * the baud rate is not programmed during earlyprintk - it is assumed
  1826. * that the previous boot loader has enabled required clocks and
  1827. * setup the baud rate generator hardware for us already.
  1828. */
  1829. if (!port->uartclk) {
  1830. baud = uart_get_baud_rate(port, termios, old, 0, 115200);
  1831. goto done;
  1832. }
  1833. for (i = 0; i < SCI_NUM_CLKS; i++)
  1834. max_freq = max(max_freq, s->clk_rates[i]);
  1835. baud = uart_get_baud_rate(port, termios, old, 0, max_freq / min_sr(s));
  1836. if (!baud)
  1837. goto done;
  1838. /*
  1839. * There can be multiple sources for the sampling clock. Find the one
  1840. * that gives us the smallest deviation from the desired baud rate.
  1841. */
  1842. /* Optional Undivided External Clock */
  1843. if (s->clk_rates[SCI_SCK] && port->type != PORT_SCIFA &&
  1844. port->type != PORT_SCIFB) {
  1845. err = sci_sck_calc(s, baud, &srr1);
  1846. if (abs(err) < abs(min_err)) {
  1847. best_clk = SCI_SCK;
  1848. scr_val = SCSCR_CKE1;
  1849. sccks = SCCKS_CKS;
  1850. min_err = err;
  1851. srr = srr1;
  1852. if (!err)
  1853. goto done;
  1854. }
  1855. }
  1856. /* Optional BRG Frequency Divided External Clock */
  1857. if (s->clk_rates[SCI_SCIF_CLK] && sci_getreg(port, SCDL)->size) {
  1858. err = sci_brg_calc(s, baud, s->clk_rates[SCI_SCIF_CLK], &dl1,
  1859. &srr1);
  1860. if (abs(err) < abs(min_err)) {
  1861. best_clk = SCI_SCIF_CLK;
  1862. scr_val = SCSCR_CKE1;
  1863. sccks = 0;
  1864. min_err = err;
  1865. dl = dl1;
  1866. srr = srr1;
  1867. if (!err)
  1868. goto done;
  1869. }
  1870. }
  1871. /* Optional BRG Frequency Divided Internal Clock */
  1872. if (s->clk_rates[SCI_BRG_INT] && sci_getreg(port, SCDL)->size) {
  1873. err = sci_brg_calc(s, baud, s->clk_rates[SCI_BRG_INT], &dl1,
  1874. &srr1);
  1875. if (abs(err) < abs(min_err)) {
  1876. best_clk = SCI_BRG_INT;
  1877. scr_val = SCSCR_CKE1;
  1878. sccks = SCCKS_XIN;
  1879. min_err = err;
  1880. dl = dl1;
  1881. srr = srr1;
  1882. if (!min_err)
  1883. goto done;
  1884. }
  1885. }
  1886. /* Divided Functional Clock using standard Bit Rate Register */
  1887. err = sci_scbrr_calc(s, baud, &brr1, &srr1, &cks1);
  1888. if (abs(err) < abs(min_err)) {
  1889. best_clk = SCI_FCK;
  1890. scr_val = 0;
  1891. min_err = err;
  1892. brr = brr1;
  1893. srr = srr1;
  1894. cks = cks1;
  1895. }
  1896. done:
  1897. if (best_clk >= 0)
  1898. dev_dbg(port->dev, "Using clk %pC for %u%+d bps\n",
  1899. s->clks[best_clk], baud, min_err);
  1900. sci_port_enable(s);
  1901. /*
  1902. * Program the optional External Baud Rate Generator (BRG) first.
  1903. * It controls the mux to select (H)SCK or frequency divided clock.
  1904. */
  1905. if (best_clk >= 0 && sci_getreg(port, SCCKS)->size) {
  1906. serial_port_out(port, SCDL, dl);
  1907. serial_port_out(port, SCCKS, sccks);
  1908. }
  1909. sci_reset(port);
  1910. uart_update_timeout(port, termios->c_cflag, baud);
  1911. if (best_clk >= 0) {
  1912. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
  1913. switch (srr + 1) {
  1914. case 5: smr_val |= SCSMR_SRC_5; break;
  1915. case 7: smr_val |= SCSMR_SRC_7; break;
  1916. case 11: smr_val |= SCSMR_SRC_11; break;
  1917. case 13: smr_val |= SCSMR_SRC_13; break;
  1918. case 16: smr_val |= SCSMR_SRC_16; break;
  1919. case 17: smr_val |= SCSMR_SRC_17; break;
  1920. case 19: smr_val |= SCSMR_SRC_19; break;
  1921. case 27: smr_val |= SCSMR_SRC_27; break;
  1922. }
  1923. smr_val |= cks;
  1924. dev_dbg(port->dev,
  1925. "SCR 0x%x SMR 0x%x BRR %u CKS 0x%x DL %u SRR %u\n",
  1926. scr_val, smr_val, brr, sccks, dl, srr);
  1927. serial_port_out(port, SCSCR, scr_val);
  1928. serial_port_out(port, SCSMR, smr_val);
  1929. serial_port_out(port, SCBRR, brr);
  1930. if (sci_getreg(port, HSSRR)->size)
  1931. serial_port_out(port, HSSRR, srr | HSCIF_SRE);
  1932. /* Wait one bit interval */
  1933. udelay((1000000 + (baud - 1)) / baud);
  1934. } else {
  1935. /* Don't touch the bit rate configuration */
  1936. scr_val = s->cfg->scscr & (SCSCR_CKE1 | SCSCR_CKE0);
  1937. smr_val |= serial_port_in(port, SCSMR) &
  1938. (SCSMR_CKEDG | SCSMR_SRC_MASK | SCSMR_CKS);
  1939. dev_dbg(port->dev, "SCR 0x%x SMR 0x%x\n", scr_val, smr_val);
  1940. serial_port_out(port, SCSCR, scr_val);
  1941. serial_port_out(port, SCSMR, smr_val);
  1942. }
  1943. sci_init_pins(port, termios->c_cflag);
  1944. port->status &= ~UPSTAT_AUTOCTS;
  1945. s->autorts = false;
  1946. reg = sci_getreg(port, SCFCR);
  1947. if (reg->size) {
  1948. unsigned short ctrl = serial_port_in(port, SCFCR);
  1949. if ((port->flags & UPF_HARD_FLOW) &&
  1950. (termios->c_cflag & CRTSCTS)) {
  1951. /* There is no CTS interrupt to restart the hardware */
  1952. port->status |= UPSTAT_AUTOCTS;
  1953. /* MCE is enabled when RTS is raised */
  1954. s->autorts = true;
  1955. }
  1956. /*
  1957. * As we've done a sci_reset() above, ensure we don't
  1958. * interfere with the FIFOs while toggling MCE. As the
  1959. * reset values could still be set, simply mask them out.
  1960. */
  1961. ctrl &= ~(SCFCR_RFRST | SCFCR_TFRST);
  1962. serial_port_out(port, SCFCR, ctrl);
  1963. }
  1964. scr_val |= s->cfg->scscr & ~(SCSCR_CKE1 | SCSCR_CKE0);
  1965. dev_dbg(port->dev, "SCSCR 0x%x\n", scr_val);
  1966. serial_port_out(port, SCSCR, scr_val);
  1967. if ((srr + 1 == 5) &&
  1968. (port->type == PORT_SCIFA || port->type == PORT_SCIFB)) {
  1969. /*
  1970. * In asynchronous mode, when the sampling rate is 1/5, first
  1971. * received data may become invalid on some SCIFA and SCIFB.
  1972. * To avoid this problem wait more than 1 serial data time (1
  1973. * bit time x serial data number) after setting SCSCR.RE = 1.
  1974. */
  1975. udelay(DIV_ROUND_UP(10 * 1000000, baud));
  1976. }
  1977. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  1978. /*
  1979. * Calculate delay for 2 DMA buffers (4 FIFO).
  1980. * See serial_core.c::uart_update_timeout().
  1981. * With 10 bits (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above
  1982. * function calculates 1 jiffie for the data plus 5 jiffies for the
  1983. * "slop(e)." Then below we calculate 5 jiffies (20ms) for 2 DMA
  1984. * buffers (4 FIFO sizes), but when performing a faster transfer, the
  1985. * value obtained by this formula is too small. Therefore, if the value
  1986. * is smaller than 20ms, use 20ms as the timeout value for DMA.
  1987. */
  1988. if (s->chan_rx) {
  1989. unsigned int bits;
  1990. /* byte size and parity */
  1991. switch (termios->c_cflag & CSIZE) {
  1992. case CS5:
  1993. bits = 7;
  1994. break;
  1995. case CS6:
  1996. bits = 8;
  1997. break;
  1998. case CS7:
  1999. bits = 9;
  2000. break;
  2001. default:
  2002. bits = 10;
  2003. break;
  2004. }
  2005. if (termios->c_cflag & CSTOPB)
  2006. bits++;
  2007. if (termios->c_cflag & PARENB)
  2008. bits++;
  2009. s->rx_timeout = DIV_ROUND_UP((s->buf_len_rx * 2 * bits * HZ) /
  2010. (baud / 10), 10);
  2011. dev_dbg(port->dev, "DMA Rx t-out %ums, tty t-out %u jiffies\n",
  2012. s->rx_timeout * 1000 / HZ, port->timeout);
  2013. if (s->rx_timeout < msecs_to_jiffies(20))
  2014. s->rx_timeout = msecs_to_jiffies(20);
  2015. }
  2016. #endif
  2017. if ((termios->c_cflag & CREAD) != 0)
  2018. sci_start_rx(port);
  2019. sci_port_disable(s);
  2020. if (UART_ENABLE_MS(port, termios->c_cflag))
  2021. sci_enable_ms(port);
  2022. }
  2023. static void sci_pm(struct uart_port *port, unsigned int state,
  2024. unsigned int oldstate)
  2025. {
  2026. struct sci_port *sci_port = to_sci_port(port);
  2027. switch (state) {
  2028. case UART_PM_STATE_OFF:
  2029. sci_port_disable(sci_port);
  2030. break;
  2031. default:
  2032. sci_port_enable(sci_port);
  2033. break;
  2034. }
  2035. }
  2036. static const char *sci_type(struct uart_port *port)
  2037. {
  2038. switch (port->type) {
  2039. case PORT_IRDA:
  2040. return "irda";
  2041. case PORT_SCI:
  2042. return "sci";
  2043. case PORT_SCIF:
  2044. return "scif";
  2045. case PORT_SCIFA:
  2046. return "scifa";
  2047. case PORT_SCIFB:
  2048. return "scifb";
  2049. case PORT_HSCIF:
  2050. return "hscif";
  2051. }
  2052. return NULL;
  2053. }
  2054. static int sci_remap_port(struct uart_port *port)
  2055. {
  2056. struct sci_port *sport = to_sci_port(port);
  2057. /*
  2058. * Nothing to do if there's already an established membase.
  2059. */
  2060. if (port->membase)
  2061. return 0;
  2062. if (port->flags & UPF_IOREMAP) {
  2063. port->membase = ioremap_nocache(port->mapbase, sport->reg_size);
  2064. if (unlikely(!port->membase)) {
  2065. dev_err(port->dev, "can't remap port#%d\n", port->line);
  2066. return -ENXIO;
  2067. }
  2068. } else {
  2069. /*
  2070. * For the simple (and majority of) cases where we don't
  2071. * need to do any remapping, just cast the cookie
  2072. * directly.
  2073. */
  2074. port->membase = (void __iomem *)(uintptr_t)port->mapbase;
  2075. }
  2076. return 0;
  2077. }
  2078. static void sci_release_port(struct uart_port *port)
  2079. {
  2080. struct sci_port *sport = to_sci_port(port);
  2081. if (port->flags & UPF_IOREMAP) {
  2082. iounmap(port->membase);
  2083. port->membase = NULL;
  2084. }
  2085. release_mem_region(port->mapbase, sport->reg_size);
  2086. }
  2087. static int sci_request_port(struct uart_port *port)
  2088. {
  2089. struct resource *res;
  2090. struct sci_port *sport = to_sci_port(port);
  2091. int ret;
  2092. res = request_mem_region(port->mapbase, sport->reg_size,
  2093. dev_name(port->dev));
  2094. if (unlikely(res == NULL)) {
  2095. dev_err(port->dev, "request_mem_region failed.");
  2096. return -EBUSY;
  2097. }
  2098. ret = sci_remap_port(port);
  2099. if (unlikely(ret != 0)) {
  2100. release_resource(res);
  2101. return ret;
  2102. }
  2103. return 0;
  2104. }
  2105. static void sci_config_port(struct uart_port *port, int flags)
  2106. {
  2107. if (flags & UART_CONFIG_TYPE) {
  2108. struct sci_port *sport = to_sci_port(port);
  2109. port->type = sport->cfg->type;
  2110. sci_request_port(port);
  2111. }
  2112. }
  2113. static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
  2114. {
  2115. if (ser->baud_base < 2400)
  2116. /* No paper tape reader for Mitch.. */
  2117. return -EINVAL;
  2118. return 0;
  2119. }
  2120. static const struct uart_ops sci_uart_ops = {
  2121. .tx_empty = sci_tx_empty,
  2122. .set_mctrl = sci_set_mctrl,
  2123. .get_mctrl = sci_get_mctrl,
  2124. .start_tx = sci_start_tx,
  2125. .stop_tx = sci_stop_tx,
  2126. .stop_rx = sci_stop_rx,
  2127. .enable_ms = sci_enable_ms,
  2128. .break_ctl = sci_break_ctl,
  2129. .startup = sci_startup,
  2130. .shutdown = sci_shutdown,
  2131. .set_termios = sci_set_termios,
  2132. .pm = sci_pm,
  2133. .type = sci_type,
  2134. .release_port = sci_release_port,
  2135. .request_port = sci_request_port,
  2136. .config_port = sci_config_port,
  2137. .verify_port = sci_verify_port,
  2138. #ifdef CONFIG_CONSOLE_POLL
  2139. .poll_get_char = sci_poll_get_char,
  2140. .poll_put_char = sci_poll_put_char,
  2141. #endif
  2142. };
  2143. static int sci_init_clocks(struct sci_port *sci_port, struct device *dev)
  2144. {
  2145. const char *clk_names[] = {
  2146. [SCI_FCK] = "fck",
  2147. [SCI_SCK] = "sck",
  2148. [SCI_BRG_INT] = "brg_int",
  2149. [SCI_SCIF_CLK] = "scif_clk",
  2150. };
  2151. struct clk *clk;
  2152. unsigned int i;
  2153. if (sci_port->cfg->type == PORT_HSCIF)
  2154. clk_names[SCI_SCK] = "hsck";
  2155. for (i = 0; i < SCI_NUM_CLKS; i++) {
  2156. clk = devm_clk_get(dev, clk_names[i]);
  2157. if (PTR_ERR(clk) == -EPROBE_DEFER)
  2158. return -EPROBE_DEFER;
  2159. if (IS_ERR(clk) && i == SCI_FCK) {
  2160. /*
  2161. * "fck" used to be called "sci_ick", and we need to
  2162. * maintain DT backward compatibility.
  2163. */
  2164. clk = devm_clk_get(dev, "sci_ick");
  2165. if (PTR_ERR(clk) == -EPROBE_DEFER)
  2166. return -EPROBE_DEFER;
  2167. if (!IS_ERR(clk))
  2168. goto found;
  2169. /*
  2170. * Not all SH platforms declare a clock lookup entry
  2171. * for SCI devices, in which case we need to get the
  2172. * global "peripheral_clk" clock.
  2173. */
  2174. clk = devm_clk_get(dev, "peripheral_clk");
  2175. if (!IS_ERR(clk))
  2176. goto found;
  2177. dev_err(dev, "failed to get %s (%ld)\n", clk_names[i],
  2178. PTR_ERR(clk));
  2179. return PTR_ERR(clk);
  2180. }
  2181. found:
  2182. if (IS_ERR(clk))
  2183. dev_dbg(dev, "failed to get %s (%ld)\n", clk_names[i],
  2184. PTR_ERR(clk));
  2185. else
  2186. dev_dbg(dev, "clk %s is %pC rate %pCr\n", clk_names[i],
  2187. clk, clk);
  2188. sci_port->clks[i] = IS_ERR(clk) ? NULL : clk;
  2189. }
  2190. return 0;
  2191. }
  2192. static int sci_init_single(struct platform_device *dev,
  2193. struct sci_port *sci_port, unsigned int index,
  2194. struct plat_sci_port *p, bool early)
  2195. {
  2196. struct uart_port *port = &sci_port->port;
  2197. const struct resource *res;
  2198. unsigned int i;
  2199. int ret;
  2200. sci_port->cfg = p;
  2201. port->ops = &sci_uart_ops;
  2202. port->iotype = UPIO_MEM;
  2203. port->line = index;
  2204. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  2205. if (res == NULL)
  2206. return -ENOMEM;
  2207. port->mapbase = res->start;
  2208. sci_port->reg_size = resource_size(res);
  2209. for (i = 0; i < ARRAY_SIZE(sci_port->irqs); ++i)
  2210. sci_port->irqs[i] = platform_get_irq(dev, i);
  2211. /* The SCI generates several interrupts. They can be muxed together or
  2212. * connected to different interrupt lines. In the muxed case only one
  2213. * interrupt resource is specified. In the non-muxed case three or four
  2214. * interrupt resources are specified, as the BRI interrupt is optional.
  2215. */
  2216. if (sci_port->irqs[0] < 0)
  2217. return -ENXIO;
  2218. if (sci_port->irqs[1] < 0) {
  2219. sci_port->irqs[1] = sci_port->irqs[0];
  2220. sci_port->irqs[2] = sci_port->irqs[0];
  2221. sci_port->irqs[3] = sci_port->irqs[0];
  2222. }
  2223. if (p->regtype == SCIx_PROBE_REGTYPE) {
  2224. ret = sci_probe_regmap(p);
  2225. if (unlikely(ret))
  2226. return ret;
  2227. }
  2228. switch (p->type) {
  2229. case PORT_SCIFB:
  2230. port->fifosize = 256;
  2231. sci_port->overrun_reg = SCxSR;
  2232. sci_port->overrun_mask = SCIFA_ORER;
  2233. sci_port->sampling_rate_mask = SCI_SR_SCIFAB;
  2234. break;
  2235. case PORT_HSCIF:
  2236. port->fifosize = 128;
  2237. sci_port->overrun_reg = SCLSR;
  2238. sci_port->overrun_mask = SCLSR_ORER;
  2239. sci_port->sampling_rate_mask = SCI_SR_RANGE(8, 32);
  2240. break;
  2241. case PORT_SCIFA:
  2242. port->fifosize = 64;
  2243. sci_port->overrun_reg = SCxSR;
  2244. sci_port->overrun_mask = SCIFA_ORER;
  2245. sci_port->sampling_rate_mask = SCI_SR_SCIFAB;
  2246. break;
  2247. case PORT_SCIF:
  2248. port->fifosize = 16;
  2249. if (p->regtype == SCIx_SH7705_SCIF_REGTYPE) {
  2250. sci_port->overrun_reg = SCxSR;
  2251. sci_port->overrun_mask = SCIFA_ORER;
  2252. sci_port->sampling_rate_mask = SCI_SR(16);
  2253. } else {
  2254. sci_port->overrun_reg = SCLSR;
  2255. sci_port->overrun_mask = SCLSR_ORER;
  2256. sci_port->sampling_rate_mask = SCI_SR(32);
  2257. }
  2258. break;
  2259. default:
  2260. port->fifosize = 1;
  2261. sci_port->overrun_reg = SCxSR;
  2262. sci_port->overrun_mask = SCI_ORER;
  2263. sci_port->sampling_rate_mask = SCI_SR(32);
  2264. break;
  2265. }
  2266. /* SCIFA on sh7723 and sh7724 need a custom sampling rate that doesn't
  2267. * match the SoC datasheet, this should be investigated. Let platform
  2268. * data override the sampling rate for now.
  2269. */
  2270. if (p->sampling_rate)
  2271. sci_port->sampling_rate_mask = SCI_SR(p->sampling_rate);
  2272. if (!early) {
  2273. ret = sci_init_clocks(sci_port, &dev->dev);
  2274. if (ret < 0)
  2275. return ret;
  2276. port->dev = &dev->dev;
  2277. pm_runtime_enable(&dev->dev);
  2278. }
  2279. sci_port->break_timer.data = (unsigned long)sci_port;
  2280. sci_port->break_timer.function = sci_break_timer;
  2281. init_timer(&sci_port->break_timer);
  2282. /*
  2283. * Establish some sensible defaults for the error detection.
  2284. */
  2285. if (p->type == PORT_SCI) {
  2286. sci_port->error_mask = SCI_DEFAULT_ERROR_MASK;
  2287. sci_port->error_clear = SCI_ERROR_CLEAR;
  2288. } else {
  2289. sci_port->error_mask = SCIF_DEFAULT_ERROR_MASK;
  2290. sci_port->error_clear = SCIF_ERROR_CLEAR;
  2291. }
  2292. /*
  2293. * Make the error mask inclusive of overrun detection, if
  2294. * supported.
  2295. */
  2296. if (sci_port->overrun_reg == SCxSR) {
  2297. sci_port->error_mask |= sci_port->overrun_mask;
  2298. sci_port->error_clear &= ~sci_port->overrun_mask;
  2299. }
  2300. port->type = p->type;
  2301. port->flags = UPF_FIXED_PORT | p->flags;
  2302. port->regshift = p->regshift;
  2303. /*
  2304. * The UART port needs an IRQ value, so we peg this to the RX IRQ
  2305. * for the multi-IRQ ports, which is where we are primarily
  2306. * concerned with the shutdown path synchronization.
  2307. *
  2308. * For the muxed case there's nothing more to do.
  2309. */
  2310. port->irq = sci_port->irqs[SCIx_RXI_IRQ];
  2311. port->irqflags = 0;
  2312. port->serial_in = sci_serial_in;
  2313. port->serial_out = sci_serial_out;
  2314. if (p->dma_slave_tx > 0 && p->dma_slave_rx > 0)
  2315. dev_dbg(port->dev, "DMA tx %d, rx %d\n",
  2316. p->dma_slave_tx, p->dma_slave_rx);
  2317. return 0;
  2318. }
  2319. static void sci_cleanup_single(struct sci_port *port)
  2320. {
  2321. pm_runtime_disable(port->port.dev);
  2322. }
  2323. #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || \
  2324. defined(CONFIG_SERIAL_SH_SCI_EARLYCON)
  2325. static void serial_console_putchar(struct uart_port *port, int ch)
  2326. {
  2327. sci_poll_put_char(port, ch);
  2328. }
  2329. /*
  2330. * Print a string to the serial port trying not to disturb
  2331. * any possible real use of the port...
  2332. */
  2333. static void serial_console_write(struct console *co, const char *s,
  2334. unsigned count)
  2335. {
  2336. struct sci_port *sci_port = &sci_ports[co->index];
  2337. struct uart_port *port = &sci_port->port;
  2338. unsigned short bits, ctrl, ctrl_temp;
  2339. unsigned long flags;
  2340. int locked = 1;
  2341. local_irq_save(flags);
  2342. #if defined(SUPPORT_SYSRQ)
  2343. if (port->sysrq)
  2344. locked = 0;
  2345. else
  2346. #endif
  2347. if (oops_in_progress)
  2348. locked = spin_trylock(&port->lock);
  2349. else
  2350. spin_lock(&port->lock);
  2351. /* first save SCSCR then disable interrupts, keep clock source */
  2352. ctrl = serial_port_in(port, SCSCR);
  2353. ctrl_temp = (sci_port->cfg->scscr & ~(SCSCR_CKE1 | SCSCR_CKE0)) |
  2354. (ctrl & (SCSCR_CKE1 | SCSCR_CKE0));
  2355. serial_port_out(port, SCSCR, ctrl_temp);
  2356. uart_console_write(port, s, count, serial_console_putchar);
  2357. /* wait until fifo is empty and last bit has been transmitted */
  2358. bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
  2359. while ((serial_port_in(port, SCxSR) & bits) != bits)
  2360. cpu_relax();
  2361. /* restore the SCSCR */
  2362. serial_port_out(port, SCSCR, ctrl);
  2363. if (locked)
  2364. spin_unlock(&port->lock);
  2365. local_irq_restore(flags);
  2366. }
  2367. static int serial_console_setup(struct console *co, char *options)
  2368. {
  2369. struct sci_port *sci_port;
  2370. struct uart_port *port;
  2371. int baud = 115200;
  2372. int bits = 8;
  2373. int parity = 'n';
  2374. int flow = 'n';
  2375. int ret;
  2376. /*
  2377. * Refuse to handle any bogus ports.
  2378. */
  2379. if (co->index < 0 || co->index >= SCI_NPORTS)
  2380. return -ENODEV;
  2381. sci_port = &sci_ports[co->index];
  2382. port = &sci_port->port;
  2383. /*
  2384. * Refuse to handle uninitialized ports.
  2385. */
  2386. if (!port->ops)
  2387. return -ENODEV;
  2388. ret = sci_remap_port(port);
  2389. if (unlikely(ret != 0))
  2390. return ret;
  2391. if (options)
  2392. uart_parse_options(options, &baud, &parity, &bits, &flow);
  2393. return uart_set_options(port, co, baud, parity, bits, flow);
  2394. }
  2395. static struct console serial_console = {
  2396. .name = "ttySC",
  2397. .device = uart_console_device,
  2398. .write = serial_console_write,
  2399. .setup = serial_console_setup,
  2400. .flags = CON_PRINTBUFFER,
  2401. .index = -1,
  2402. .data = &sci_uart_driver,
  2403. };
  2404. static struct console early_serial_console = {
  2405. .name = "early_ttySC",
  2406. .write = serial_console_write,
  2407. .flags = CON_PRINTBUFFER,
  2408. .index = -1,
  2409. };
  2410. static char early_serial_buf[32];
  2411. static int sci_probe_earlyprintk(struct platform_device *pdev)
  2412. {
  2413. struct plat_sci_port *cfg = dev_get_platdata(&pdev->dev);
  2414. if (early_serial_console.data)
  2415. return -EEXIST;
  2416. early_serial_console.index = pdev->id;
  2417. sci_init_single(pdev, &sci_ports[pdev->id], pdev->id, cfg, true);
  2418. serial_console_setup(&early_serial_console, early_serial_buf);
  2419. if (!strstr(early_serial_buf, "keep"))
  2420. early_serial_console.flags |= CON_BOOT;
  2421. register_console(&early_serial_console);
  2422. return 0;
  2423. }
  2424. #define SCI_CONSOLE (&serial_console)
  2425. #else
  2426. static inline int sci_probe_earlyprintk(struct platform_device *pdev)
  2427. {
  2428. return -EINVAL;
  2429. }
  2430. #define SCI_CONSOLE NULL
  2431. #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE || CONFIG_SERIAL_SH_SCI_EARLYCON */
  2432. static const char banner[] __initconst = "SuperH (H)SCI(F) driver initialized";
  2433. static struct uart_driver sci_uart_driver = {
  2434. .owner = THIS_MODULE,
  2435. .driver_name = "sci",
  2436. .dev_name = "ttySC",
  2437. .major = SCI_MAJOR,
  2438. .minor = SCI_MINOR_START,
  2439. .nr = SCI_NPORTS,
  2440. .cons = SCI_CONSOLE,
  2441. };
  2442. static int sci_remove(struct platform_device *dev)
  2443. {
  2444. struct sci_port *port = platform_get_drvdata(dev);
  2445. uart_remove_one_port(&sci_uart_driver, &port->port);
  2446. sci_cleanup_single(port);
  2447. return 0;
  2448. }
  2449. #define SCI_OF_DATA(type, regtype) (void *)((type) << 16 | (regtype))
  2450. #define SCI_OF_TYPE(data) ((unsigned long)(data) >> 16)
  2451. #define SCI_OF_REGTYPE(data) ((unsigned long)(data) & 0xffff)
  2452. static const struct of_device_id of_sci_match[] = {
  2453. /* SoC-specific types */
  2454. {
  2455. .compatible = "renesas,scif-r7s72100",
  2456. .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH2_SCIF_FIFODATA_REGTYPE),
  2457. },
  2458. /* Family-specific types */
  2459. {
  2460. .compatible = "renesas,rcar-gen1-scif",
  2461. .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
  2462. }, {
  2463. .compatible = "renesas,rcar-gen2-scif",
  2464. .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
  2465. }, {
  2466. .compatible = "renesas,rcar-gen3-scif",
  2467. .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
  2468. },
  2469. /* Generic types */
  2470. {
  2471. .compatible = "renesas,scif",
  2472. .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_REGTYPE),
  2473. }, {
  2474. .compatible = "renesas,scifa",
  2475. .data = SCI_OF_DATA(PORT_SCIFA, SCIx_SCIFA_REGTYPE),
  2476. }, {
  2477. .compatible = "renesas,scifb",
  2478. .data = SCI_OF_DATA(PORT_SCIFB, SCIx_SCIFB_REGTYPE),
  2479. }, {
  2480. .compatible = "renesas,hscif",
  2481. .data = SCI_OF_DATA(PORT_HSCIF, SCIx_HSCIF_REGTYPE),
  2482. }, {
  2483. .compatible = "renesas,sci",
  2484. .data = SCI_OF_DATA(PORT_SCI, SCIx_SCI_REGTYPE),
  2485. }, {
  2486. /* Terminator */
  2487. },
  2488. };
  2489. MODULE_DEVICE_TABLE(of, of_sci_match);
  2490. static struct plat_sci_port *
  2491. sci_parse_dt(struct platform_device *pdev, unsigned int *dev_id)
  2492. {
  2493. struct device_node *np = pdev->dev.of_node;
  2494. const struct of_device_id *match;
  2495. struct plat_sci_port *p;
  2496. int id;
  2497. if (!IS_ENABLED(CONFIG_OF) || !np)
  2498. return NULL;
  2499. match = of_match_node(of_sci_match, np);
  2500. if (!match)
  2501. return NULL;
  2502. p = devm_kzalloc(&pdev->dev, sizeof(struct plat_sci_port), GFP_KERNEL);
  2503. if (!p)
  2504. return NULL;
  2505. /* Get the line number from the aliases node. */
  2506. id = of_alias_get_id(np, "serial");
  2507. if (id < 0) {
  2508. dev_err(&pdev->dev, "failed to get alias id (%d)\n", id);
  2509. return NULL;
  2510. }
  2511. *dev_id = id;
  2512. p->flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
  2513. p->type = SCI_OF_TYPE(match->data);
  2514. p->regtype = SCI_OF_REGTYPE(match->data);
  2515. p->scscr = SCSCR_RE | SCSCR_TE;
  2516. if (of_find_property(np, "uart-has-rtscts", NULL))
  2517. p->capabilities |= SCIx_HAVE_RTSCTS;
  2518. return p;
  2519. }
  2520. static int sci_probe_single(struct platform_device *dev,
  2521. unsigned int index,
  2522. struct plat_sci_port *p,
  2523. struct sci_port *sciport)
  2524. {
  2525. int ret;
  2526. /* Sanity check */
  2527. if (unlikely(index >= SCI_NPORTS)) {
  2528. dev_notice(&dev->dev, "Attempting to register port %d when only %d are available\n",
  2529. index+1, SCI_NPORTS);
  2530. dev_notice(&dev->dev, "Consider bumping CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
  2531. return -EINVAL;
  2532. }
  2533. ret = sci_init_single(dev, sciport, index, p, false);
  2534. if (ret)
  2535. return ret;
  2536. sciport->gpios = mctrl_gpio_init(&sciport->port, 0);
  2537. if (IS_ERR(sciport->gpios) && PTR_ERR(sciport->gpios) != -ENOSYS)
  2538. return PTR_ERR(sciport->gpios);
  2539. if (p->capabilities & SCIx_HAVE_RTSCTS) {
  2540. if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(sciport->gpios,
  2541. UART_GPIO_CTS)) ||
  2542. !IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(sciport->gpios,
  2543. UART_GPIO_RTS))) {
  2544. dev_err(&dev->dev, "Conflicting RTS/CTS config\n");
  2545. return -EINVAL;
  2546. }
  2547. sciport->port.flags |= UPF_HARD_FLOW;
  2548. }
  2549. ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
  2550. if (ret) {
  2551. sci_cleanup_single(sciport);
  2552. return ret;
  2553. }
  2554. return 0;
  2555. }
  2556. static int sci_probe(struct platform_device *dev)
  2557. {
  2558. struct plat_sci_port *p;
  2559. struct sci_port *sp;
  2560. unsigned int dev_id;
  2561. int ret;
  2562. /*
  2563. * If we've come here via earlyprintk initialization, head off to
  2564. * the special early probe. We don't have sufficient device state
  2565. * to make it beyond this yet.
  2566. */
  2567. if (is_early_platform_device(dev))
  2568. return sci_probe_earlyprintk(dev);
  2569. if (dev->dev.of_node) {
  2570. p = sci_parse_dt(dev, &dev_id);
  2571. if (p == NULL)
  2572. return -EINVAL;
  2573. } else {
  2574. p = dev->dev.platform_data;
  2575. if (p == NULL) {
  2576. dev_err(&dev->dev, "no platform data supplied\n");
  2577. return -EINVAL;
  2578. }
  2579. dev_id = dev->id;
  2580. }
  2581. sp = &sci_ports[dev_id];
  2582. platform_set_drvdata(dev, sp);
  2583. ret = sci_probe_single(dev, dev_id, p, sp);
  2584. if (ret)
  2585. return ret;
  2586. #ifdef CONFIG_SH_STANDARD_BIOS
  2587. sh_bios_gdb_detach();
  2588. #endif
  2589. return 0;
  2590. }
  2591. static __maybe_unused int sci_suspend(struct device *dev)
  2592. {
  2593. struct sci_port *sport = dev_get_drvdata(dev);
  2594. if (sport)
  2595. uart_suspend_port(&sci_uart_driver, &sport->port);
  2596. return 0;
  2597. }
  2598. static __maybe_unused int sci_resume(struct device *dev)
  2599. {
  2600. struct sci_port *sport = dev_get_drvdata(dev);
  2601. if (sport)
  2602. uart_resume_port(&sci_uart_driver, &sport->port);
  2603. return 0;
  2604. }
  2605. static SIMPLE_DEV_PM_OPS(sci_dev_pm_ops, sci_suspend, sci_resume);
  2606. static struct platform_driver sci_driver = {
  2607. .probe = sci_probe,
  2608. .remove = sci_remove,
  2609. .driver = {
  2610. .name = "sh-sci",
  2611. .pm = &sci_dev_pm_ops,
  2612. .of_match_table = of_match_ptr(of_sci_match),
  2613. },
  2614. };
  2615. static int __init sci_init(void)
  2616. {
  2617. int ret;
  2618. pr_info("%s\n", banner);
  2619. ret = uart_register_driver(&sci_uart_driver);
  2620. if (likely(ret == 0)) {
  2621. ret = platform_driver_register(&sci_driver);
  2622. if (unlikely(ret))
  2623. uart_unregister_driver(&sci_uart_driver);
  2624. }
  2625. return ret;
  2626. }
  2627. static void __exit sci_exit(void)
  2628. {
  2629. platform_driver_unregister(&sci_driver);
  2630. uart_unregister_driver(&sci_uart_driver);
  2631. }
  2632. #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
  2633. early_platform_init_buffer("earlyprintk", &sci_driver,
  2634. early_serial_buf, ARRAY_SIZE(early_serial_buf));
  2635. #endif
  2636. #ifdef CONFIG_SERIAL_SH_SCI_EARLYCON
  2637. static struct __init plat_sci_port port_cfg;
  2638. static int __init early_console_setup(struct earlycon_device *device,
  2639. int type)
  2640. {
  2641. if (!device->port.membase)
  2642. return -ENODEV;
  2643. device->port.serial_in = sci_serial_in;
  2644. device->port.serial_out = sci_serial_out;
  2645. device->port.type = type;
  2646. memcpy(&sci_ports[0].port, &device->port, sizeof(struct uart_port));
  2647. sci_ports[0].cfg = &port_cfg;
  2648. sci_ports[0].cfg->type = type;
  2649. sci_probe_regmap(sci_ports[0].cfg);
  2650. port_cfg.scscr = sci_serial_in(&sci_ports[0].port, SCSCR) |
  2651. SCSCR_RE | SCSCR_TE;
  2652. sci_serial_out(&sci_ports[0].port, SCSCR, port_cfg.scscr);
  2653. device->con->write = serial_console_write;
  2654. return 0;
  2655. }
  2656. static int __init sci_early_console_setup(struct earlycon_device *device,
  2657. const char *opt)
  2658. {
  2659. return early_console_setup(device, PORT_SCI);
  2660. }
  2661. static int __init scif_early_console_setup(struct earlycon_device *device,
  2662. const char *opt)
  2663. {
  2664. return early_console_setup(device, PORT_SCIF);
  2665. }
  2666. static int __init scifa_early_console_setup(struct earlycon_device *device,
  2667. const char *opt)
  2668. {
  2669. return early_console_setup(device, PORT_SCIFA);
  2670. }
  2671. static int __init scifb_early_console_setup(struct earlycon_device *device,
  2672. const char *opt)
  2673. {
  2674. return early_console_setup(device, PORT_SCIFB);
  2675. }
  2676. static int __init hscif_early_console_setup(struct earlycon_device *device,
  2677. const char *opt)
  2678. {
  2679. return early_console_setup(device, PORT_HSCIF);
  2680. }
  2681. OF_EARLYCON_DECLARE(sci, "renesas,sci", sci_early_console_setup);
  2682. OF_EARLYCON_DECLARE(scif, "renesas,scif", scif_early_console_setup);
  2683. OF_EARLYCON_DECLARE(scifa, "renesas,scifa", scifa_early_console_setup);
  2684. OF_EARLYCON_DECLARE(scifb, "renesas,scifb", scifb_early_console_setup);
  2685. OF_EARLYCON_DECLARE(hscif, "renesas,hscif", hscif_early_console_setup);
  2686. #endif /* CONFIG_SERIAL_SH_SCI_EARLYCON */
  2687. module_init(sci_init);
  2688. module_exit(sci_exit);
  2689. MODULE_LICENSE("GPL");
  2690. MODULE_ALIAS("platform:sh-sci");
  2691. MODULE_AUTHOR("Paul Mundt");
  2692. MODULE_DESCRIPTION("SuperH (H)SCI(F) serial driver");