sh-sci.c 80 KB

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