sh-sci.c 84 KB

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