sh-sci.c 84 KB

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