amba-pl011.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for AMBA serial ports
  4. *
  5. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  6. *
  7. * Copyright 1999 ARM Limited
  8. * Copyright (C) 2000 Deep Blue Solutions Ltd.
  9. * Copyright (C) 2010 ST-Ericsson SA
  10. *
  11. * This is a generic driver for ARM AMBA-type serial ports. They
  12. * have a lot of 16550-like features, but are not register compatible.
  13. * Note that although they do have CTS, DCD and DSR inputs, they do
  14. * not have an RI input, nor do they have DTR or RTS outputs. If
  15. * required, these have to be supplied via some other means (eg, GPIO)
  16. * and hooked into this driver.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/ioport.h>
  20. #include <linux/init.h>
  21. #include <linux/console.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/sysrq.h>
  24. #include <linux/device.h>
  25. #include <linux/tty.h>
  26. #include <linux/tty_flip.h>
  27. #include <linux/serial_core.h>
  28. #include <linux/serial.h>
  29. #include <linux/amba/bus.h>
  30. #include <linux/amba/serial.h>
  31. #include <linux/clk.h>
  32. #include <linux/slab.h>
  33. #include <linux/dmaengine.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/scatterlist.h>
  36. #include <linux/delay.h>
  37. #include <linux/types.h>
  38. #include <linux/of.h>
  39. #include <linux/pinctrl/consumer.h>
  40. #include <linux/sizes.h>
  41. #include <linux/io.h>
  42. #include <linux/acpi.h>
  43. #define UART_NR 14
  44. #define SERIAL_AMBA_MAJOR 204
  45. #define SERIAL_AMBA_MINOR 64
  46. #define SERIAL_AMBA_NR UART_NR
  47. #define AMBA_ISR_PASS_LIMIT 256
  48. #define UART_DR_ERROR (UART011_DR_OE | UART011_DR_BE | UART011_DR_PE | UART011_DR_FE)
  49. #define UART_DUMMY_DR_RX BIT(16)
  50. enum {
  51. REG_DR,
  52. REG_ST_DMAWM,
  53. REG_ST_TIMEOUT,
  54. REG_FR,
  55. REG_LCRH_RX,
  56. REG_LCRH_TX,
  57. REG_IBRD,
  58. REG_FBRD,
  59. REG_CR,
  60. REG_IFLS,
  61. REG_IMSC,
  62. REG_RIS,
  63. REG_MIS,
  64. REG_ICR,
  65. REG_DMACR,
  66. REG_ST_XFCR,
  67. REG_ST_XON1,
  68. REG_ST_XON2,
  69. REG_ST_XOFF1,
  70. REG_ST_XOFF2,
  71. REG_ST_ITCR,
  72. REG_ST_ITIP,
  73. REG_ST_ABCR,
  74. REG_ST_ABIMSC,
  75. /* The size of the array - must be last */
  76. REG_ARRAY_SIZE,
  77. };
  78. static u16 pl011_std_offsets[REG_ARRAY_SIZE] = {
  79. [REG_DR] = UART01x_DR,
  80. [REG_FR] = UART01x_FR,
  81. [REG_LCRH_RX] = UART011_LCRH,
  82. [REG_LCRH_TX] = UART011_LCRH,
  83. [REG_IBRD] = UART011_IBRD,
  84. [REG_FBRD] = UART011_FBRD,
  85. [REG_CR] = UART011_CR,
  86. [REG_IFLS] = UART011_IFLS,
  87. [REG_IMSC] = UART011_IMSC,
  88. [REG_RIS] = UART011_RIS,
  89. [REG_MIS] = UART011_MIS,
  90. [REG_ICR] = UART011_ICR,
  91. [REG_DMACR] = UART011_DMACR,
  92. };
  93. /* There is by now at least one vendor with differing details, so handle it */
  94. struct vendor_data {
  95. const u16 *reg_offset;
  96. unsigned int ifls;
  97. unsigned int fr_busy;
  98. unsigned int fr_dsr;
  99. unsigned int fr_cts;
  100. unsigned int fr_ri;
  101. unsigned int inv_fr;
  102. bool access_32b;
  103. bool oversampling;
  104. bool dma_threshold;
  105. bool cts_event_workaround;
  106. bool always_enabled;
  107. bool fixed_options;
  108. unsigned int (*get_fifosize)(struct amba_device *dev);
  109. };
  110. static unsigned int get_fifosize_arm(struct amba_device *dev)
  111. {
  112. return amba_rev(dev) < 3 ? 16 : 32;
  113. }
  114. static struct vendor_data vendor_arm = {
  115. .reg_offset = pl011_std_offsets,
  116. .ifls = UART011_IFLS_RX4_8 | UART011_IFLS_TX4_8,
  117. .fr_busy = UART01x_FR_BUSY,
  118. .fr_dsr = UART01x_FR_DSR,
  119. .fr_cts = UART01x_FR_CTS,
  120. .fr_ri = UART011_FR_RI,
  121. .oversampling = false,
  122. .dma_threshold = false,
  123. .cts_event_workaround = false,
  124. .always_enabled = false,
  125. .fixed_options = false,
  126. .get_fifosize = get_fifosize_arm,
  127. };
  128. static const struct vendor_data vendor_sbsa = {
  129. .reg_offset = pl011_std_offsets,
  130. .fr_busy = UART01x_FR_BUSY,
  131. .fr_dsr = UART01x_FR_DSR,
  132. .fr_cts = UART01x_FR_CTS,
  133. .fr_ri = UART011_FR_RI,
  134. .access_32b = true,
  135. .oversampling = false,
  136. .dma_threshold = false,
  137. .cts_event_workaround = false,
  138. .always_enabled = true,
  139. .fixed_options = true,
  140. };
  141. #ifdef CONFIG_ACPI_SPCR_TABLE
  142. static const struct vendor_data vendor_qdt_qdf2400_e44 = {
  143. .reg_offset = pl011_std_offsets,
  144. .fr_busy = UART011_FR_TXFE,
  145. .fr_dsr = UART01x_FR_DSR,
  146. .fr_cts = UART01x_FR_CTS,
  147. .fr_ri = UART011_FR_RI,
  148. .inv_fr = UART011_FR_TXFE,
  149. .access_32b = true,
  150. .oversampling = false,
  151. .dma_threshold = false,
  152. .cts_event_workaround = false,
  153. .always_enabled = true,
  154. .fixed_options = true,
  155. };
  156. #endif
  157. static u16 pl011_st_offsets[REG_ARRAY_SIZE] = {
  158. [REG_DR] = UART01x_DR,
  159. [REG_ST_DMAWM] = ST_UART011_DMAWM,
  160. [REG_ST_TIMEOUT] = ST_UART011_TIMEOUT,
  161. [REG_FR] = UART01x_FR,
  162. [REG_LCRH_RX] = ST_UART011_LCRH_RX,
  163. [REG_LCRH_TX] = ST_UART011_LCRH_TX,
  164. [REG_IBRD] = UART011_IBRD,
  165. [REG_FBRD] = UART011_FBRD,
  166. [REG_CR] = UART011_CR,
  167. [REG_IFLS] = UART011_IFLS,
  168. [REG_IMSC] = UART011_IMSC,
  169. [REG_RIS] = UART011_RIS,
  170. [REG_MIS] = UART011_MIS,
  171. [REG_ICR] = UART011_ICR,
  172. [REG_DMACR] = UART011_DMACR,
  173. [REG_ST_XFCR] = ST_UART011_XFCR,
  174. [REG_ST_XON1] = ST_UART011_XON1,
  175. [REG_ST_XON2] = ST_UART011_XON2,
  176. [REG_ST_XOFF1] = ST_UART011_XOFF1,
  177. [REG_ST_XOFF2] = ST_UART011_XOFF2,
  178. [REG_ST_ITCR] = ST_UART011_ITCR,
  179. [REG_ST_ITIP] = ST_UART011_ITIP,
  180. [REG_ST_ABCR] = ST_UART011_ABCR,
  181. [REG_ST_ABIMSC] = ST_UART011_ABIMSC,
  182. };
  183. static unsigned int get_fifosize_st(struct amba_device *dev)
  184. {
  185. return 64;
  186. }
  187. static struct vendor_data vendor_st = {
  188. .reg_offset = pl011_st_offsets,
  189. .ifls = UART011_IFLS_RX_HALF | UART011_IFLS_TX_HALF,
  190. .fr_busy = UART01x_FR_BUSY,
  191. .fr_dsr = UART01x_FR_DSR,
  192. .fr_cts = UART01x_FR_CTS,
  193. .fr_ri = UART011_FR_RI,
  194. .oversampling = true,
  195. .dma_threshold = true,
  196. .cts_event_workaround = true,
  197. .always_enabled = false,
  198. .fixed_options = false,
  199. .get_fifosize = get_fifosize_st,
  200. };
  201. /* Deals with DMA transactions */
  202. struct pl011_dmabuf {
  203. dma_addr_t dma;
  204. size_t len;
  205. char *buf;
  206. };
  207. struct pl011_dmarx_data {
  208. struct dma_chan *chan;
  209. struct completion complete;
  210. bool use_buf_b;
  211. struct pl011_dmabuf dbuf_a;
  212. struct pl011_dmabuf dbuf_b;
  213. dma_cookie_t cookie;
  214. bool running;
  215. struct timer_list timer;
  216. unsigned int last_residue;
  217. unsigned long last_jiffies;
  218. bool auto_poll_rate;
  219. unsigned int poll_rate;
  220. unsigned int poll_timeout;
  221. };
  222. struct pl011_dmatx_data {
  223. struct dma_chan *chan;
  224. dma_addr_t dma;
  225. size_t len;
  226. char *buf;
  227. bool queued;
  228. };
  229. enum pl011_rs485_tx_state {
  230. OFF,
  231. WAIT_AFTER_RTS,
  232. SEND,
  233. WAIT_AFTER_SEND,
  234. };
  235. /*
  236. * We wrap our port structure around the generic uart_port.
  237. */
  238. struct uart_amba_port {
  239. struct uart_port port;
  240. const u16 *reg_offset;
  241. struct clk *clk;
  242. const struct vendor_data *vendor;
  243. unsigned int im; /* interrupt mask */
  244. unsigned int old_status;
  245. unsigned int fifosize; /* vendor-specific */
  246. unsigned int fixed_baud; /* vendor-set fixed baud rate */
  247. char type[12];
  248. ktime_t rs485_tx_drain_interval; /* nano */
  249. enum pl011_rs485_tx_state rs485_tx_state;
  250. struct hrtimer trigger_start_tx;
  251. struct hrtimer trigger_stop_tx;
  252. bool console_line_ended;
  253. #ifdef CONFIG_DMA_ENGINE
  254. /* DMA stuff */
  255. unsigned int dmacr; /* dma control reg */
  256. bool using_tx_dma;
  257. bool using_rx_dma;
  258. struct pl011_dmarx_data dmarx;
  259. struct pl011_dmatx_data dmatx;
  260. bool dma_probed;
  261. #endif
  262. };
  263. static unsigned int pl011_tx_empty(struct uart_port *port);
  264. static unsigned int pl011_reg_to_offset(const struct uart_amba_port *uap,
  265. unsigned int reg)
  266. {
  267. return uap->reg_offset[reg];
  268. }
  269. static unsigned int pl011_read(const struct uart_amba_port *uap,
  270. unsigned int reg)
  271. {
  272. void __iomem *addr = uap->port.membase + pl011_reg_to_offset(uap, reg);
  273. return (uap->port.iotype == UPIO_MEM32) ?
  274. readl_relaxed(addr) : readw_relaxed(addr);
  275. }
  276. static void pl011_write(unsigned int val, const struct uart_amba_port *uap,
  277. unsigned int reg)
  278. {
  279. void __iomem *addr = uap->port.membase + pl011_reg_to_offset(uap, reg);
  280. if (uap->port.iotype == UPIO_MEM32)
  281. writel_relaxed(val, addr);
  282. else
  283. writew_relaxed(val, addr);
  284. }
  285. /*
  286. * Reads up to 256 characters from the FIFO or until it's empty and
  287. * inserts them into the TTY layer. Returns the number of characters
  288. * read from the FIFO.
  289. */
  290. static int pl011_fifo_to_tty(struct uart_amba_port *uap)
  291. {
  292. unsigned int ch, fifotaken;
  293. int sysrq;
  294. u16 status;
  295. u8 flag;
  296. for (fifotaken = 0; fifotaken != 256; fifotaken++) {
  297. status = pl011_read(uap, REG_FR);
  298. if (status & UART01x_FR_RXFE)
  299. break;
  300. /* Take chars from the FIFO and update status */
  301. ch = pl011_read(uap, REG_DR) | UART_DUMMY_DR_RX;
  302. flag = TTY_NORMAL;
  303. uap->port.icount.rx++;
  304. if (unlikely(ch & UART_DR_ERROR)) {
  305. if (ch & UART011_DR_BE) {
  306. ch &= ~(UART011_DR_FE | UART011_DR_PE);
  307. uap->port.icount.brk++;
  308. if (uart_handle_break(&uap->port))
  309. continue;
  310. } else if (ch & UART011_DR_PE) {
  311. uap->port.icount.parity++;
  312. } else if (ch & UART011_DR_FE) {
  313. uap->port.icount.frame++;
  314. }
  315. if (ch & UART011_DR_OE)
  316. uap->port.icount.overrun++;
  317. ch &= uap->port.read_status_mask;
  318. if (ch & UART011_DR_BE)
  319. flag = TTY_BREAK;
  320. else if (ch & UART011_DR_PE)
  321. flag = TTY_PARITY;
  322. else if (ch & UART011_DR_FE)
  323. flag = TTY_FRAME;
  324. }
  325. sysrq = uart_prepare_sysrq_char(&uap->port, ch & 255);
  326. if (!sysrq)
  327. uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
  328. }
  329. return fifotaken;
  330. }
  331. /*
  332. * All the DMA operation mode stuff goes inside this ifdef.
  333. * This assumes that you have a generic DMA device interface,
  334. * no custom DMA interfaces are supported.
  335. */
  336. #ifdef CONFIG_DMA_ENGINE
  337. #define PL011_DMA_BUFFER_SIZE PAGE_SIZE
  338. static int pl011_dmabuf_init(struct dma_chan *chan, struct pl011_dmabuf *db,
  339. enum dma_data_direction dir)
  340. {
  341. db->buf = dma_alloc_coherent(chan->device->dev, PL011_DMA_BUFFER_SIZE,
  342. &db->dma, GFP_KERNEL);
  343. if (!db->buf)
  344. return -ENOMEM;
  345. db->len = PL011_DMA_BUFFER_SIZE;
  346. return 0;
  347. }
  348. static void pl011_dmabuf_free(struct dma_chan *chan, struct pl011_dmabuf *db,
  349. enum dma_data_direction dir)
  350. {
  351. if (db->buf) {
  352. dma_free_coherent(chan->device->dev,
  353. PL011_DMA_BUFFER_SIZE, db->buf, db->dma);
  354. }
  355. }
  356. static void pl011_dma_probe(struct uart_amba_port *uap)
  357. {
  358. /* DMA is the sole user of the platform data right now */
  359. struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
  360. struct device *dev = uap->port.dev;
  361. struct dma_slave_config tx_conf = {
  362. .dst_addr = uap->port.mapbase +
  363. pl011_reg_to_offset(uap, REG_DR),
  364. .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  365. .direction = DMA_MEM_TO_DEV,
  366. .dst_maxburst = uap->fifosize >> 1,
  367. .device_fc = false,
  368. };
  369. struct dma_chan *chan;
  370. dma_cap_mask_t mask;
  371. uap->dma_probed = true;
  372. chan = dma_request_chan(dev, "tx");
  373. if (IS_ERR(chan)) {
  374. if (PTR_ERR(chan) == -EPROBE_DEFER) {
  375. uap->dma_probed = false;
  376. return;
  377. }
  378. /* We need platform data */
  379. if (!plat || !plat->dma_filter) {
  380. dev_dbg(uap->port.dev, "no DMA platform data\n");
  381. return;
  382. }
  383. /* Try to acquire a generic DMA engine slave TX channel */
  384. dma_cap_zero(mask);
  385. dma_cap_set(DMA_SLAVE, mask);
  386. chan = dma_request_channel(mask, plat->dma_filter,
  387. plat->dma_tx_param);
  388. if (!chan) {
  389. dev_err(uap->port.dev, "no TX DMA channel!\n");
  390. return;
  391. }
  392. }
  393. dmaengine_slave_config(chan, &tx_conf);
  394. uap->dmatx.chan = chan;
  395. dev_info(uap->port.dev, "DMA channel TX %s\n",
  396. dma_chan_name(uap->dmatx.chan));
  397. /* Optionally make use of an RX channel as well */
  398. chan = dma_request_chan(dev, "rx");
  399. if (IS_ERR(chan) && plat && plat->dma_rx_param) {
  400. chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
  401. if (!chan) {
  402. dev_err(uap->port.dev, "no RX DMA channel!\n");
  403. return;
  404. }
  405. }
  406. if (!IS_ERR(chan)) {
  407. struct dma_slave_config rx_conf = {
  408. .src_addr = uap->port.mapbase +
  409. pl011_reg_to_offset(uap, REG_DR),
  410. .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  411. .direction = DMA_DEV_TO_MEM,
  412. .src_maxburst = uap->fifosize >> 2,
  413. .device_fc = false,
  414. };
  415. struct dma_slave_caps caps;
  416. /*
  417. * Some DMA controllers provide information on their capabilities.
  418. * If the controller does, check for suitable residue processing
  419. * otherwise assime all is well.
  420. */
  421. if (dma_get_slave_caps(chan, &caps) == 0) {
  422. if (caps.residue_granularity ==
  423. DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
  424. dma_release_channel(chan);
  425. dev_info(uap->port.dev,
  426. "RX DMA disabled - no residue processing\n");
  427. return;
  428. }
  429. }
  430. dmaengine_slave_config(chan, &rx_conf);
  431. uap->dmarx.chan = chan;
  432. uap->dmarx.auto_poll_rate = false;
  433. if (plat && plat->dma_rx_poll_enable) {
  434. /* Set poll rate if specified. */
  435. if (plat->dma_rx_poll_rate) {
  436. uap->dmarx.auto_poll_rate = false;
  437. uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
  438. } else {
  439. /*
  440. * 100 ms defaults to poll rate if not
  441. * specified. This will be adjusted with
  442. * the baud rate at set_termios.
  443. */
  444. uap->dmarx.auto_poll_rate = true;
  445. uap->dmarx.poll_rate = 100;
  446. }
  447. /* 3 secs defaults poll_timeout if not specified. */
  448. if (plat->dma_rx_poll_timeout)
  449. uap->dmarx.poll_timeout =
  450. plat->dma_rx_poll_timeout;
  451. else
  452. uap->dmarx.poll_timeout = 3000;
  453. } else if (!plat && dev->of_node) {
  454. uap->dmarx.auto_poll_rate =
  455. of_property_read_bool(dev->of_node, "auto-poll");
  456. if (uap->dmarx.auto_poll_rate) {
  457. u32 x;
  458. if (of_property_read_u32(dev->of_node, "poll-rate-ms", &x) == 0)
  459. uap->dmarx.poll_rate = x;
  460. else
  461. uap->dmarx.poll_rate = 100;
  462. if (of_property_read_u32(dev->of_node, "poll-timeout-ms", &x) == 0)
  463. uap->dmarx.poll_timeout = x;
  464. else
  465. uap->dmarx.poll_timeout = 3000;
  466. }
  467. }
  468. dev_info(uap->port.dev, "DMA channel RX %s\n",
  469. dma_chan_name(uap->dmarx.chan));
  470. }
  471. }
  472. static void pl011_dma_remove(struct uart_amba_port *uap)
  473. {
  474. if (uap->dmatx.chan)
  475. dma_release_channel(uap->dmatx.chan);
  476. if (uap->dmarx.chan)
  477. dma_release_channel(uap->dmarx.chan);
  478. }
  479. /* Forward declare these for the refill routine */
  480. static int pl011_dma_tx_refill(struct uart_amba_port *uap);
  481. static void pl011_start_tx_pio(struct uart_amba_port *uap);
  482. /*
  483. * The current DMA TX buffer has been sent.
  484. * Try to queue up another DMA buffer.
  485. */
  486. static void pl011_dma_tx_callback(void *data)
  487. {
  488. struct uart_amba_port *uap = data;
  489. struct tty_port *tport = &uap->port.state->port;
  490. struct pl011_dmatx_data *dmatx = &uap->dmatx;
  491. unsigned long flags;
  492. u16 dmacr;
  493. uart_port_lock_irqsave(&uap->port, &flags);
  494. if (uap->dmatx.queued)
  495. dma_unmap_single(dmatx->chan->device->dev, dmatx->dma,
  496. dmatx->len, DMA_TO_DEVICE);
  497. dmacr = uap->dmacr;
  498. uap->dmacr = dmacr & ~UART011_TXDMAE;
  499. pl011_write(uap->dmacr, uap, REG_DMACR);
  500. /*
  501. * If TX DMA was disabled, it means that we've stopped the DMA for
  502. * some reason (eg, XOFF received, or we want to send an X-char.)
  503. *
  504. * Note: we need to be careful here of a potential race between DMA
  505. * and the rest of the driver - if the driver disables TX DMA while
  506. * a TX buffer completing, we must update the tx queued status to
  507. * get further refills (hence we check dmacr).
  508. */
  509. if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
  510. kfifo_is_empty(&tport->xmit_fifo)) {
  511. uap->dmatx.queued = false;
  512. uart_port_unlock_irqrestore(&uap->port, flags);
  513. return;
  514. }
  515. if (pl011_dma_tx_refill(uap) <= 0)
  516. /*
  517. * We didn't queue a DMA buffer for some reason, but we
  518. * have data pending to be sent. Re-enable the TX IRQ.
  519. */
  520. pl011_start_tx_pio(uap);
  521. uart_port_unlock_irqrestore(&uap->port, flags);
  522. }
  523. /*
  524. * Try to refill the TX DMA buffer.
  525. * Locking: called with port lock held and IRQs disabled.
  526. * Returns:
  527. * 1 if we queued up a TX DMA buffer.
  528. * 0 if we didn't want to handle this by DMA
  529. * <0 on error
  530. */
  531. static int pl011_dma_tx_refill(struct uart_amba_port *uap)
  532. {
  533. struct pl011_dmatx_data *dmatx = &uap->dmatx;
  534. struct dma_chan *chan = dmatx->chan;
  535. struct dma_device *dma_dev = chan->device;
  536. struct dma_async_tx_descriptor *desc;
  537. struct tty_port *tport = &uap->port.state->port;
  538. unsigned int count;
  539. /*
  540. * Try to avoid the overhead involved in using DMA if the
  541. * transaction fits in the first half of the FIFO, by using
  542. * the standard interrupt handling. This ensures that we
  543. * issue a uart_write_wakeup() at the appropriate time.
  544. */
  545. count = kfifo_len(&tport->xmit_fifo);
  546. if (count < (uap->fifosize >> 1)) {
  547. uap->dmatx.queued = false;
  548. return 0;
  549. }
  550. /*
  551. * Bodge: don't send the last character by DMA, as this
  552. * will prevent XON from notifying us to restart DMA.
  553. */
  554. count -= 1;
  555. /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
  556. if (count > PL011_DMA_BUFFER_SIZE)
  557. count = PL011_DMA_BUFFER_SIZE;
  558. count = kfifo_out_peek(&tport->xmit_fifo, dmatx->buf, count);
  559. dmatx->len = count;
  560. dmatx->dma = dma_map_single(dma_dev->dev, dmatx->buf, count,
  561. DMA_TO_DEVICE);
  562. if (dma_mapping_error(dma_dev->dev, dmatx->dma)) {
  563. uap->dmatx.queued = false;
  564. dev_dbg(uap->port.dev, "unable to map TX DMA\n");
  565. return -EBUSY;
  566. }
  567. desc = dmaengine_prep_slave_single(chan, dmatx->dma, dmatx->len, DMA_MEM_TO_DEV,
  568. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  569. if (!desc) {
  570. dma_unmap_single(dma_dev->dev, dmatx->dma, dmatx->len, DMA_TO_DEVICE);
  571. uap->dmatx.queued = false;
  572. /*
  573. * If DMA cannot be used right now, we complete this
  574. * transaction via IRQ and let the TTY layer retry.
  575. */
  576. dev_dbg(uap->port.dev, "TX DMA busy\n");
  577. return -EBUSY;
  578. }
  579. /* Some data to go along to the callback */
  580. desc->callback = pl011_dma_tx_callback;
  581. desc->callback_param = uap;
  582. /* All errors should happen at prepare time */
  583. dmaengine_submit(desc);
  584. /* Fire the DMA transaction */
  585. dma_dev->device_issue_pending(chan);
  586. uap->dmacr |= UART011_TXDMAE;
  587. pl011_write(uap->dmacr, uap, REG_DMACR);
  588. uap->dmatx.queued = true;
  589. /*
  590. * Now we know that DMA will fire, so advance the ring buffer
  591. * with the stuff we just dispatched.
  592. */
  593. uart_xmit_advance(&uap->port, count);
  594. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  595. uart_write_wakeup(&uap->port);
  596. return 1;
  597. }
  598. /*
  599. * We received a transmit interrupt without a pending X-char but with
  600. * pending characters.
  601. * Locking: called with port lock held and IRQs disabled.
  602. * Returns:
  603. * false if we want to use PIO to transmit
  604. * true if we queued a DMA buffer
  605. */
  606. static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
  607. {
  608. if (!uap->using_tx_dma)
  609. return false;
  610. /*
  611. * If we already have a TX buffer queued, but received a
  612. * TX interrupt, it will be because we've just sent an X-char.
  613. * Ensure the TX DMA is enabled and the TX IRQ is disabled.
  614. */
  615. if (uap->dmatx.queued) {
  616. uap->dmacr |= UART011_TXDMAE;
  617. pl011_write(uap->dmacr, uap, REG_DMACR);
  618. uap->im &= ~UART011_TXIM;
  619. pl011_write(uap->im, uap, REG_IMSC);
  620. return true;
  621. }
  622. /*
  623. * We don't have a TX buffer queued, so try to queue one.
  624. * If we successfully queued a buffer, mask the TX IRQ.
  625. */
  626. if (pl011_dma_tx_refill(uap) > 0) {
  627. uap->im &= ~UART011_TXIM;
  628. pl011_write(uap->im, uap, REG_IMSC);
  629. return true;
  630. }
  631. return false;
  632. }
  633. /*
  634. * Stop the DMA transmit (eg, due to received XOFF).
  635. * Locking: called with port lock held and IRQs disabled.
  636. */
  637. static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
  638. {
  639. if (uap->dmatx.queued) {
  640. uap->dmacr &= ~UART011_TXDMAE;
  641. pl011_write(uap->dmacr, uap, REG_DMACR);
  642. }
  643. }
  644. /*
  645. * Try to start a DMA transmit, or in the case of an XON/OFF
  646. * character queued for send, try to get that character out ASAP.
  647. * Locking: called with port lock held and IRQs disabled.
  648. * Returns:
  649. * false if we want the TX IRQ to be enabled
  650. * true if we have a buffer queued
  651. */
  652. static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
  653. {
  654. u16 dmacr;
  655. if (!uap->using_tx_dma)
  656. return false;
  657. if (!uap->port.x_char) {
  658. /* no X-char, try to push chars out in DMA mode */
  659. bool ret = true;
  660. if (!uap->dmatx.queued) {
  661. if (pl011_dma_tx_refill(uap) > 0) {
  662. uap->im &= ~UART011_TXIM;
  663. pl011_write(uap->im, uap, REG_IMSC);
  664. } else {
  665. ret = false;
  666. }
  667. } else if (!(uap->dmacr & UART011_TXDMAE)) {
  668. uap->dmacr |= UART011_TXDMAE;
  669. pl011_write(uap->dmacr, uap, REG_DMACR);
  670. }
  671. return ret;
  672. }
  673. /*
  674. * We have an X-char to send. Disable DMA to prevent it loading
  675. * the TX fifo, and then see if we can stuff it into the FIFO.
  676. */
  677. dmacr = uap->dmacr;
  678. uap->dmacr &= ~UART011_TXDMAE;
  679. pl011_write(uap->dmacr, uap, REG_DMACR);
  680. if (pl011_read(uap, REG_FR) & UART01x_FR_TXFF) {
  681. /*
  682. * No space in the FIFO, so enable the transmit interrupt
  683. * so we know when there is space. Note that once we've
  684. * loaded the character, we should just re-enable DMA.
  685. */
  686. return false;
  687. }
  688. pl011_write(uap->port.x_char, uap, REG_DR);
  689. uap->port.icount.tx++;
  690. uap->port.x_char = 0;
  691. /* Success - restore the DMA state */
  692. uap->dmacr = dmacr;
  693. pl011_write(dmacr, uap, REG_DMACR);
  694. return true;
  695. }
  696. /*
  697. * Flush the transmit buffer.
  698. * Locking: called with port lock held and IRQs disabled.
  699. */
  700. static void pl011_dma_flush_buffer(struct uart_port *port)
  701. __releases(&uap->port.lock)
  702. __acquires(&uap->port.lock)
  703. {
  704. struct uart_amba_port *uap =
  705. container_of(port, struct uart_amba_port, port);
  706. if (!uap->using_tx_dma)
  707. return;
  708. dmaengine_terminate_async(uap->dmatx.chan);
  709. if (uap->dmatx.queued) {
  710. dma_unmap_single(uap->dmatx.chan->device->dev, uap->dmatx.dma,
  711. uap->dmatx.len, DMA_TO_DEVICE);
  712. uap->dmatx.queued = false;
  713. uap->dmacr &= ~UART011_TXDMAE;
  714. pl011_write(uap->dmacr, uap, REG_DMACR);
  715. }
  716. }
  717. static void pl011_dma_rx_callback(void *data);
  718. static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
  719. {
  720. struct dma_chan *rxchan = uap->dmarx.chan;
  721. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  722. struct dma_async_tx_descriptor *desc;
  723. struct pl011_dmabuf *dbuf;
  724. if (!rxchan)
  725. return -EIO;
  726. /* Start the RX DMA job */
  727. dbuf = uap->dmarx.use_buf_b ?
  728. &uap->dmarx.dbuf_b : &uap->dmarx.dbuf_a;
  729. desc = dmaengine_prep_slave_single(rxchan, dbuf->dma, dbuf->len,
  730. DMA_DEV_TO_MEM,
  731. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  732. /*
  733. * If the DMA engine is busy and cannot prepare a
  734. * channel, no big deal, the driver will fall back
  735. * to interrupt mode as a result of this error code.
  736. */
  737. if (!desc) {
  738. uap->dmarx.running = false;
  739. dmaengine_terminate_all(rxchan);
  740. return -EBUSY;
  741. }
  742. /* Some data to go along to the callback */
  743. desc->callback = pl011_dma_rx_callback;
  744. desc->callback_param = uap;
  745. dmarx->cookie = dmaengine_submit(desc);
  746. dma_async_issue_pending(rxchan);
  747. uap->dmacr |= UART011_RXDMAE;
  748. pl011_write(uap->dmacr, uap, REG_DMACR);
  749. uap->dmarx.running = true;
  750. uap->im &= ~UART011_RXIM;
  751. pl011_write(uap->im, uap, REG_IMSC);
  752. return 0;
  753. }
  754. /*
  755. * This is called when either the DMA job is complete, or
  756. * the FIFO timeout interrupt occurred. This must be called
  757. * with the port spinlock uap->port.lock held.
  758. */
  759. static void pl011_dma_rx_chars(struct uart_amba_port *uap,
  760. u32 pending, bool use_buf_b,
  761. bool readfifo)
  762. {
  763. struct tty_port *port = &uap->port.state->port;
  764. struct pl011_dmabuf *dbuf = use_buf_b ?
  765. &uap->dmarx.dbuf_b : &uap->dmarx.dbuf_a;
  766. int dma_count = 0;
  767. u32 fifotaken = 0; /* only used for vdbg() */
  768. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  769. int dmataken = 0;
  770. if (uap->dmarx.poll_rate) {
  771. /* The data can be taken by polling */
  772. dmataken = dbuf->len - dmarx->last_residue;
  773. /* Recalculate the pending size */
  774. if (pending >= dmataken)
  775. pending -= dmataken;
  776. }
  777. /* Pick the remain data from the DMA */
  778. if (pending) {
  779. /*
  780. * First take all chars in the DMA pipe, then look in the FIFO.
  781. * Note that tty_insert_flip_buf() tries to take as many chars
  782. * as it can.
  783. */
  784. dma_count = tty_insert_flip_string(port, dbuf->buf + dmataken, pending);
  785. uap->port.icount.rx += dma_count;
  786. if (dma_count < pending)
  787. dev_warn(uap->port.dev,
  788. "couldn't insert all characters (TTY is full?)\n");
  789. }
  790. /* Reset the last_residue for Rx DMA poll */
  791. if (uap->dmarx.poll_rate)
  792. dmarx->last_residue = dbuf->len;
  793. /*
  794. * Only continue with trying to read the FIFO if all DMA chars have
  795. * been taken first.
  796. */
  797. if (dma_count == pending && readfifo) {
  798. /* Clear any error flags */
  799. pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
  800. UART011_FEIS, uap, REG_ICR);
  801. /*
  802. * If we read all the DMA'd characters, and we had an
  803. * incomplete buffer, that could be due to an rx error, or
  804. * maybe we just timed out. Read any pending chars and check
  805. * the error status.
  806. *
  807. * Error conditions will only occur in the FIFO, these will
  808. * trigger an immediate interrupt and stop the DMA job, so we
  809. * will always find the error in the FIFO, never in the DMA
  810. * buffer.
  811. */
  812. fifotaken = pl011_fifo_to_tty(uap);
  813. }
  814. dev_vdbg(uap->port.dev,
  815. "Took %d chars from DMA buffer and %d chars from the FIFO\n",
  816. dma_count, fifotaken);
  817. tty_flip_buffer_push(port);
  818. }
  819. static void pl011_dma_rx_irq(struct uart_amba_port *uap)
  820. {
  821. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  822. struct dma_chan *rxchan = dmarx->chan;
  823. struct pl011_dmabuf *dbuf = dmarx->use_buf_b ?
  824. &dmarx->dbuf_b : &dmarx->dbuf_a;
  825. size_t pending;
  826. struct dma_tx_state state;
  827. enum dma_status dmastat;
  828. /*
  829. * Pause the transfer so we can trust the current counter,
  830. * do this before we pause the PL011 block, else we may
  831. * overflow the FIFO.
  832. */
  833. if (dmaengine_pause(rxchan))
  834. dev_err(uap->port.dev, "unable to pause DMA transfer\n");
  835. dmastat = rxchan->device->device_tx_status(rxchan,
  836. dmarx->cookie, &state);
  837. if (dmastat != DMA_PAUSED)
  838. dev_err(uap->port.dev, "unable to pause DMA transfer\n");
  839. /* Disable RX DMA - incoming data will wait in the FIFO */
  840. uap->dmacr &= ~UART011_RXDMAE;
  841. pl011_write(uap->dmacr, uap, REG_DMACR);
  842. uap->dmarx.running = false;
  843. pending = dbuf->len - state.residue;
  844. BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
  845. /* Then we terminate the transfer - we now know our residue */
  846. dmaengine_terminate_all(rxchan);
  847. /*
  848. * This will take the chars we have so far and insert
  849. * into the framework.
  850. */
  851. pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
  852. /* Switch buffer & re-trigger DMA job */
  853. dmarx->use_buf_b = !dmarx->use_buf_b;
  854. if (pl011_dma_rx_trigger_dma(uap)) {
  855. dev_dbg(uap->port.dev,
  856. "could not retrigger RX DMA job fall back to interrupt mode\n");
  857. uap->im |= UART011_RXIM;
  858. pl011_write(uap->im, uap, REG_IMSC);
  859. }
  860. }
  861. static void pl011_dma_rx_callback(void *data)
  862. {
  863. struct uart_amba_port *uap = data;
  864. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  865. struct dma_chan *rxchan = dmarx->chan;
  866. bool lastbuf = dmarx->use_buf_b;
  867. struct pl011_dmabuf *dbuf = dmarx->use_buf_b ?
  868. &dmarx->dbuf_b : &dmarx->dbuf_a;
  869. size_t pending;
  870. struct dma_tx_state state;
  871. int ret;
  872. /*
  873. * This completion interrupt occurs typically when the
  874. * RX buffer is totally stuffed but no timeout has yet
  875. * occurred. When that happens, we just want the RX
  876. * routine to flush out the secondary DMA buffer while
  877. * we immediately trigger the next DMA job.
  878. */
  879. uart_port_lock_irq(&uap->port);
  880. /*
  881. * Rx data can be taken by the UART interrupts during
  882. * the DMA irq handler. So we check the residue here.
  883. */
  884. rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
  885. pending = dbuf->len - state.residue;
  886. BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
  887. /* Then we terminate the transfer - we now know our residue */
  888. dmaengine_terminate_all(rxchan);
  889. uap->dmarx.running = false;
  890. dmarx->use_buf_b = !lastbuf;
  891. ret = pl011_dma_rx_trigger_dma(uap);
  892. pl011_dma_rx_chars(uap, pending, lastbuf, false);
  893. uart_unlock_and_check_sysrq(&uap->port);
  894. /*
  895. * Do this check after we picked the DMA chars so we don't
  896. * get some IRQ immediately from RX.
  897. */
  898. if (ret) {
  899. dev_dbg(uap->port.dev,
  900. "could not retrigger RX DMA job fall back to interrupt mode\n");
  901. uap->im |= UART011_RXIM;
  902. pl011_write(uap->im, uap, REG_IMSC);
  903. }
  904. }
  905. /*
  906. * Stop accepting received characters, when we're shutting down or
  907. * suspending this port.
  908. * Locking: called with port lock held and IRQs disabled.
  909. */
  910. static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
  911. {
  912. if (!uap->using_rx_dma)
  913. return;
  914. /* FIXME. Just disable the DMA enable */
  915. uap->dmacr &= ~UART011_RXDMAE;
  916. pl011_write(uap->dmacr, uap, REG_DMACR);
  917. }
  918. /*
  919. * Timer handler for Rx DMA polling.
  920. * Every polling, It checks the residue in the dma buffer and transfer
  921. * data to the tty. Also, last_residue is updated for the next polling.
  922. */
  923. static void pl011_dma_rx_poll(struct timer_list *t)
  924. {
  925. struct uart_amba_port *uap = timer_container_of(uap, t, dmarx.timer);
  926. struct tty_port *port = &uap->port.state->port;
  927. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  928. struct dma_chan *rxchan = uap->dmarx.chan;
  929. unsigned long flags;
  930. unsigned int dmataken = 0;
  931. unsigned int size = 0;
  932. struct pl011_dmabuf *dbuf;
  933. int dma_count;
  934. struct dma_tx_state state;
  935. dbuf = dmarx->use_buf_b ? &uap->dmarx.dbuf_b : &uap->dmarx.dbuf_a;
  936. rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
  937. if (likely(state.residue < dmarx->last_residue)) {
  938. dmataken = dbuf->len - dmarx->last_residue;
  939. size = dmarx->last_residue - state.residue;
  940. dma_count = tty_insert_flip_string(port, dbuf->buf + dmataken,
  941. size);
  942. if (dma_count == size)
  943. dmarx->last_residue = state.residue;
  944. dmarx->last_jiffies = jiffies;
  945. }
  946. tty_flip_buffer_push(port);
  947. /*
  948. * If no data is received in poll_timeout, the driver will fall back
  949. * to interrupt mode. We will retrigger DMA at the first interrupt.
  950. */
  951. if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
  952. > uap->dmarx.poll_timeout) {
  953. uart_port_lock_irqsave(&uap->port, &flags);
  954. pl011_dma_rx_stop(uap);
  955. uap->im |= UART011_RXIM;
  956. pl011_write(uap->im, uap, REG_IMSC);
  957. uart_port_unlock_irqrestore(&uap->port, flags);
  958. uap->dmarx.running = false;
  959. dmaengine_terminate_all(rxchan);
  960. timer_delete(&uap->dmarx.timer);
  961. } else {
  962. mod_timer(&uap->dmarx.timer,
  963. jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
  964. }
  965. }
  966. static void pl011_dma_startup(struct uart_amba_port *uap)
  967. {
  968. int ret;
  969. if (!uap->dma_probed)
  970. pl011_dma_probe(uap);
  971. if (!uap->dmatx.chan)
  972. return;
  973. uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
  974. if (!uap->dmatx.buf) {
  975. uap->port.fifosize = uap->fifosize;
  976. return;
  977. }
  978. uap->dmatx.len = PL011_DMA_BUFFER_SIZE;
  979. /* The DMA buffer is now the FIFO the TTY subsystem can use */
  980. uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
  981. uap->using_tx_dma = true;
  982. if (!uap->dmarx.chan)
  983. goto skip_rx;
  984. /* Allocate and map DMA RX buffers */
  985. ret = pl011_dmabuf_init(uap->dmarx.chan, &uap->dmarx.dbuf_a,
  986. DMA_FROM_DEVICE);
  987. if (ret) {
  988. dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
  989. "RX buffer A", ret);
  990. goto skip_rx;
  991. }
  992. ret = pl011_dmabuf_init(uap->dmarx.chan, &uap->dmarx.dbuf_b,
  993. DMA_FROM_DEVICE);
  994. if (ret) {
  995. dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
  996. "RX buffer B", ret);
  997. pl011_dmabuf_free(uap->dmarx.chan, &uap->dmarx.dbuf_a,
  998. DMA_FROM_DEVICE);
  999. goto skip_rx;
  1000. }
  1001. uap->using_rx_dma = true;
  1002. skip_rx:
  1003. /* Turn on DMA error (RX/TX will be enabled on demand) */
  1004. uap->dmacr |= UART011_DMAONERR;
  1005. pl011_write(uap->dmacr, uap, REG_DMACR);
  1006. /*
  1007. * ST Micro variants has some specific dma burst threshold
  1008. * compensation. Set this to 16 bytes, so burst will only
  1009. * be issued above/below 16 bytes.
  1010. */
  1011. if (uap->vendor->dma_threshold)
  1012. pl011_write(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
  1013. uap, REG_ST_DMAWM);
  1014. if (uap->using_rx_dma) {
  1015. if (pl011_dma_rx_trigger_dma(uap))
  1016. dev_dbg(uap->port.dev,
  1017. "could not trigger initial RX DMA job, fall back to interrupt mode\n");
  1018. if (uap->dmarx.poll_rate) {
  1019. timer_setup(&uap->dmarx.timer, pl011_dma_rx_poll, 0);
  1020. mod_timer(&uap->dmarx.timer,
  1021. jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
  1022. uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
  1023. uap->dmarx.last_jiffies = jiffies;
  1024. }
  1025. }
  1026. }
  1027. static void pl011_dma_shutdown(struct uart_amba_port *uap)
  1028. {
  1029. if (!(uap->using_tx_dma || uap->using_rx_dma))
  1030. return;
  1031. /* Disable RX and TX DMA */
  1032. while (pl011_read(uap, REG_FR) & uap->vendor->fr_busy)
  1033. cpu_relax();
  1034. uart_port_lock_irq(&uap->port);
  1035. uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
  1036. pl011_write(uap->dmacr, uap, REG_DMACR);
  1037. uart_port_unlock_irq(&uap->port);
  1038. if (uap->using_tx_dma) {
  1039. /* In theory, this should already be done by pl011_dma_flush_buffer */
  1040. dmaengine_terminate_all(uap->dmatx.chan);
  1041. if (uap->dmatx.queued) {
  1042. dma_unmap_single(uap->dmatx.chan->device->dev,
  1043. uap->dmatx.dma, uap->dmatx.len,
  1044. DMA_TO_DEVICE);
  1045. uap->dmatx.queued = false;
  1046. }
  1047. kfree(uap->dmatx.buf);
  1048. uap->using_tx_dma = false;
  1049. }
  1050. if (uap->using_rx_dma) {
  1051. dmaengine_terminate_all(uap->dmarx.chan);
  1052. /* Clean up the RX DMA */
  1053. pl011_dmabuf_free(uap->dmarx.chan, &uap->dmarx.dbuf_a, DMA_FROM_DEVICE);
  1054. pl011_dmabuf_free(uap->dmarx.chan, &uap->dmarx.dbuf_b, DMA_FROM_DEVICE);
  1055. if (uap->dmarx.poll_rate)
  1056. timer_delete_sync(&uap->dmarx.timer);
  1057. uap->using_rx_dma = false;
  1058. }
  1059. }
  1060. static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
  1061. {
  1062. return uap->using_rx_dma;
  1063. }
  1064. static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
  1065. {
  1066. return uap->using_rx_dma && uap->dmarx.running;
  1067. }
  1068. #else
  1069. /* Blank functions if the DMA engine is not available */
  1070. static inline void pl011_dma_remove(struct uart_amba_port *uap)
  1071. {
  1072. }
  1073. static inline void pl011_dma_startup(struct uart_amba_port *uap)
  1074. {
  1075. }
  1076. static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
  1077. {
  1078. }
  1079. static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
  1080. {
  1081. return false;
  1082. }
  1083. static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
  1084. {
  1085. }
  1086. static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
  1087. {
  1088. return false;
  1089. }
  1090. static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
  1091. {
  1092. }
  1093. static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
  1094. {
  1095. }
  1096. static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
  1097. {
  1098. return -EIO;
  1099. }
  1100. static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
  1101. {
  1102. return false;
  1103. }
  1104. static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
  1105. {
  1106. return false;
  1107. }
  1108. #define pl011_dma_flush_buffer NULL
  1109. #endif
  1110. static void pl011_rs485_tx_stop(struct uart_amba_port *uap)
  1111. {
  1112. struct uart_port *port = &uap->port;
  1113. u32 cr;
  1114. if (uap->rs485_tx_state == SEND)
  1115. uap->rs485_tx_state = WAIT_AFTER_SEND;
  1116. if (uap->rs485_tx_state == WAIT_AFTER_SEND) {
  1117. /* Schedule hrtimer if tx queue not empty */
  1118. if (!pl011_tx_empty(port)) {
  1119. hrtimer_start(&uap->trigger_stop_tx,
  1120. uap->rs485_tx_drain_interval,
  1121. HRTIMER_MODE_REL);
  1122. return;
  1123. }
  1124. if (port->rs485.delay_rts_after_send > 0) {
  1125. hrtimer_start(&uap->trigger_stop_tx,
  1126. ms_to_ktime(port->rs485.delay_rts_after_send),
  1127. HRTIMER_MODE_REL);
  1128. return;
  1129. }
  1130. /* Continue without any delay */
  1131. } else if (uap->rs485_tx_state == WAIT_AFTER_RTS) {
  1132. hrtimer_try_to_cancel(&uap->trigger_start_tx);
  1133. }
  1134. cr = pl011_read(uap, REG_CR);
  1135. if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
  1136. cr &= ~UART011_CR_RTS;
  1137. else
  1138. cr |= UART011_CR_RTS;
  1139. /* Disable the transmitter and reenable the transceiver */
  1140. cr &= ~UART011_CR_TXE;
  1141. cr |= UART011_CR_RXE;
  1142. pl011_write(cr, uap, REG_CR);
  1143. uap->rs485_tx_state = OFF;
  1144. }
  1145. static void pl011_stop_tx(struct uart_port *port)
  1146. {
  1147. struct uart_amba_port *uap =
  1148. container_of(port, struct uart_amba_port, port);
  1149. if (port->rs485.flags & SER_RS485_ENABLED &&
  1150. uap->rs485_tx_state == WAIT_AFTER_RTS) {
  1151. pl011_rs485_tx_stop(uap);
  1152. return;
  1153. }
  1154. uap->im &= ~UART011_TXIM;
  1155. pl011_write(uap->im, uap, REG_IMSC);
  1156. pl011_dma_tx_stop(uap);
  1157. if (port->rs485.flags & SER_RS485_ENABLED &&
  1158. uap->rs485_tx_state != OFF)
  1159. pl011_rs485_tx_stop(uap);
  1160. }
  1161. static bool pl011_tx_chars(struct uart_amba_port *uap, bool from_irq);
  1162. /* Start TX with programmed I/O only (no DMA) */
  1163. static void pl011_start_tx_pio(struct uart_amba_port *uap)
  1164. {
  1165. if (pl011_tx_chars(uap, false)) {
  1166. uap->im |= UART011_TXIM;
  1167. pl011_write(uap->im, uap, REG_IMSC);
  1168. }
  1169. }
  1170. static void pl011_rs485_tx_start(struct uart_amba_port *uap)
  1171. {
  1172. struct uart_port *port = &uap->port;
  1173. u32 cr;
  1174. if (uap->rs485_tx_state == WAIT_AFTER_RTS) {
  1175. uap->rs485_tx_state = SEND;
  1176. return;
  1177. }
  1178. if (uap->rs485_tx_state == WAIT_AFTER_SEND) {
  1179. hrtimer_try_to_cancel(&uap->trigger_stop_tx);
  1180. uap->rs485_tx_state = SEND;
  1181. return;
  1182. }
  1183. /* uap->rs485_tx_state == OFF */
  1184. /* Enable transmitter */
  1185. cr = pl011_read(uap, REG_CR);
  1186. cr |= UART011_CR_TXE;
  1187. /* Disable receiver if half-duplex */
  1188. if (!(port->rs485.flags & SER_RS485_RX_DURING_TX))
  1189. cr &= ~UART011_CR_RXE;
  1190. if (port->rs485.flags & SER_RS485_RTS_ON_SEND)
  1191. cr &= ~UART011_CR_RTS;
  1192. else
  1193. cr |= UART011_CR_RTS;
  1194. pl011_write(cr, uap, REG_CR);
  1195. if (port->rs485.delay_rts_before_send > 0) {
  1196. uap->rs485_tx_state = WAIT_AFTER_RTS;
  1197. hrtimer_start(&uap->trigger_start_tx,
  1198. ms_to_ktime(port->rs485.delay_rts_before_send),
  1199. HRTIMER_MODE_REL);
  1200. } else {
  1201. uap->rs485_tx_state = SEND;
  1202. }
  1203. }
  1204. static void pl011_start_tx(struct uart_port *port)
  1205. {
  1206. struct uart_amba_port *uap =
  1207. container_of(port, struct uart_amba_port, port);
  1208. if ((uap->port.rs485.flags & SER_RS485_ENABLED) &&
  1209. uap->rs485_tx_state != SEND) {
  1210. pl011_rs485_tx_start(uap);
  1211. if (uap->rs485_tx_state == WAIT_AFTER_RTS)
  1212. return;
  1213. }
  1214. if (!pl011_dma_tx_start(uap))
  1215. pl011_start_tx_pio(uap);
  1216. }
  1217. static enum hrtimer_restart pl011_trigger_start_tx(struct hrtimer *t)
  1218. {
  1219. struct uart_amba_port *uap =
  1220. container_of(t, struct uart_amba_port, trigger_start_tx);
  1221. unsigned long flags;
  1222. uart_port_lock_irqsave(&uap->port, &flags);
  1223. if (uap->rs485_tx_state == WAIT_AFTER_RTS)
  1224. pl011_start_tx(&uap->port);
  1225. uart_port_unlock_irqrestore(&uap->port, flags);
  1226. return HRTIMER_NORESTART;
  1227. }
  1228. static enum hrtimer_restart pl011_trigger_stop_tx(struct hrtimer *t)
  1229. {
  1230. struct uart_amba_port *uap =
  1231. container_of(t, struct uart_amba_port, trigger_stop_tx);
  1232. unsigned long flags;
  1233. uart_port_lock_irqsave(&uap->port, &flags);
  1234. if (uap->rs485_tx_state == WAIT_AFTER_SEND)
  1235. pl011_rs485_tx_stop(uap);
  1236. uart_port_unlock_irqrestore(&uap->port, flags);
  1237. return HRTIMER_NORESTART;
  1238. }
  1239. static void pl011_stop_rx(struct uart_port *port)
  1240. {
  1241. struct uart_amba_port *uap =
  1242. container_of(port, struct uart_amba_port, port);
  1243. uap->im &= ~(UART011_RXIM | UART011_RTIM | UART011_FEIM |
  1244. UART011_PEIM | UART011_BEIM | UART011_OEIM);
  1245. pl011_write(uap->im, uap, REG_IMSC);
  1246. pl011_dma_rx_stop(uap);
  1247. }
  1248. static void pl011_throttle_rx(struct uart_port *port)
  1249. {
  1250. unsigned long flags;
  1251. uart_port_lock_irqsave(port, &flags);
  1252. pl011_stop_rx(port);
  1253. uart_port_unlock_irqrestore(port, flags);
  1254. }
  1255. static void pl011_enable_ms(struct uart_port *port)
  1256. {
  1257. struct uart_amba_port *uap =
  1258. container_of(port, struct uart_amba_port, port);
  1259. uap->im |= UART011_RIMIM | UART011_CTSMIM | UART011_DCDMIM | UART011_DSRMIM;
  1260. pl011_write(uap->im, uap, REG_IMSC);
  1261. }
  1262. static void pl011_rx_chars(struct uart_amba_port *uap)
  1263. __releases(&uap->port.lock)
  1264. __acquires(&uap->port.lock)
  1265. {
  1266. pl011_fifo_to_tty(uap);
  1267. uart_port_unlock(&uap->port);
  1268. tty_flip_buffer_push(&uap->port.state->port);
  1269. /*
  1270. * If we were temporarily out of DMA mode for a while,
  1271. * attempt to switch back to DMA mode again.
  1272. */
  1273. if (pl011_dma_rx_available(uap)) {
  1274. if (pl011_dma_rx_trigger_dma(uap)) {
  1275. dev_dbg(uap->port.dev,
  1276. "could not trigger RX DMA job fall back to interrupt mode again\n");
  1277. uap->im |= UART011_RXIM;
  1278. pl011_write(uap->im, uap, REG_IMSC);
  1279. } else {
  1280. #ifdef CONFIG_DMA_ENGINE
  1281. /* Start Rx DMA poll */
  1282. if (uap->dmarx.poll_rate) {
  1283. uap->dmarx.last_jiffies = jiffies;
  1284. uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
  1285. mod_timer(&uap->dmarx.timer,
  1286. jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
  1287. }
  1288. #endif
  1289. }
  1290. }
  1291. uart_port_lock(&uap->port);
  1292. }
  1293. static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c,
  1294. bool from_irq)
  1295. {
  1296. if (unlikely(!from_irq) &&
  1297. pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
  1298. return false; /* unable to transmit character */
  1299. pl011_write(c, uap, REG_DR);
  1300. uap->port.icount.tx++;
  1301. return true;
  1302. }
  1303. /* Returns true if tx interrupts have to be (kept) enabled */
  1304. static bool pl011_tx_chars(struct uart_amba_port *uap, bool from_irq)
  1305. {
  1306. struct tty_port *tport = &uap->port.state->port;
  1307. int count = uap->fifosize >> 1;
  1308. if (uap->port.x_char) {
  1309. if (!pl011_tx_char(uap, uap->port.x_char, from_irq))
  1310. return true;
  1311. uap->port.x_char = 0;
  1312. --count;
  1313. }
  1314. if (kfifo_is_empty(&tport->xmit_fifo) || uart_tx_stopped(&uap->port)) {
  1315. pl011_stop_tx(&uap->port);
  1316. return false;
  1317. }
  1318. /* If we are using DMA mode, try to send some characters. */
  1319. if (pl011_dma_tx_irq(uap))
  1320. return true;
  1321. while (1) {
  1322. unsigned char c;
  1323. if (likely(from_irq) && count-- == 0)
  1324. break;
  1325. if (!kfifo_peek(&tport->xmit_fifo, &c))
  1326. break;
  1327. if (!pl011_tx_char(uap, c, from_irq))
  1328. break;
  1329. kfifo_skip(&tport->xmit_fifo);
  1330. }
  1331. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  1332. uart_write_wakeup(&uap->port);
  1333. if (kfifo_is_empty(&tport->xmit_fifo)) {
  1334. pl011_stop_tx(&uap->port);
  1335. return false;
  1336. }
  1337. return true;
  1338. }
  1339. static void pl011_modem_status(struct uart_amba_port *uap)
  1340. {
  1341. unsigned int status, delta;
  1342. status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
  1343. delta = status ^ uap->old_status;
  1344. uap->old_status = status;
  1345. if (!delta)
  1346. return;
  1347. if (delta & UART01x_FR_DCD)
  1348. uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
  1349. if (delta & uap->vendor->fr_dsr)
  1350. uap->port.icount.dsr++;
  1351. if (delta & uap->vendor->fr_cts)
  1352. uart_handle_cts_change(&uap->port,
  1353. status & uap->vendor->fr_cts);
  1354. wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
  1355. }
  1356. static void check_apply_cts_event_workaround(struct uart_amba_port *uap)
  1357. {
  1358. if (!uap->vendor->cts_event_workaround)
  1359. return;
  1360. /* workaround to make sure that all bits are unlocked.. */
  1361. pl011_write(0x00, uap, REG_ICR);
  1362. /*
  1363. * WA: introduce 26ns(1 uart clk) delay before W1C;
  1364. * single apb access will incur 2 pclk(133.12Mhz) delay,
  1365. * so add 2 dummy reads
  1366. */
  1367. pl011_read(uap, REG_ICR);
  1368. pl011_read(uap, REG_ICR);
  1369. }
  1370. static irqreturn_t pl011_int(int irq, void *dev_id)
  1371. {
  1372. struct uart_amba_port *uap = dev_id;
  1373. unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
  1374. int handled = 0;
  1375. uart_port_lock(&uap->port);
  1376. status = pl011_read(uap, REG_RIS) & uap->im;
  1377. if (status) {
  1378. do {
  1379. check_apply_cts_event_workaround(uap);
  1380. pl011_write(status & ~(UART011_TXIS | UART011_RTIS | UART011_RXIS),
  1381. uap, REG_ICR);
  1382. if (status & (UART011_RTIS | UART011_RXIS)) {
  1383. if (pl011_dma_rx_running(uap))
  1384. pl011_dma_rx_irq(uap);
  1385. else
  1386. pl011_rx_chars(uap);
  1387. }
  1388. if (status & (UART011_DSRMIS | UART011_DCDMIS |
  1389. UART011_CTSMIS | UART011_RIMIS))
  1390. pl011_modem_status(uap);
  1391. if (status & UART011_TXIS)
  1392. pl011_tx_chars(uap, true);
  1393. if (pass_counter-- == 0)
  1394. break;
  1395. status = pl011_read(uap, REG_RIS) & uap->im;
  1396. } while (status != 0);
  1397. handled = 1;
  1398. }
  1399. uart_unlock_and_check_sysrq(&uap->port);
  1400. return IRQ_RETVAL(handled);
  1401. }
  1402. static unsigned int pl011_tx_empty(struct uart_port *port)
  1403. {
  1404. struct uart_amba_port *uap =
  1405. container_of(port, struct uart_amba_port, port);
  1406. /* Allow feature register bits to be inverted to work around errata */
  1407. unsigned int status = pl011_read(uap, REG_FR) ^ uap->vendor->inv_fr;
  1408. return status & (uap->vendor->fr_busy | UART01x_FR_TXFF) ?
  1409. 0 : TIOCSER_TEMT;
  1410. }
  1411. static void pl011_maybe_set_bit(bool cond, unsigned int *ptr, unsigned int mask)
  1412. {
  1413. if (cond)
  1414. *ptr |= mask;
  1415. }
  1416. static unsigned int pl011_get_mctrl(struct uart_port *port)
  1417. {
  1418. struct uart_amba_port *uap =
  1419. container_of(port, struct uart_amba_port, port);
  1420. unsigned int result = 0;
  1421. unsigned int status = pl011_read(uap, REG_FR);
  1422. pl011_maybe_set_bit(status & UART01x_FR_DCD, &result, TIOCM_CAR);
  1423. pl011_maybe_set_bit(status & uap->vendor->fr_dsr, &result, TIOCM_DSR);
  1424. pl011_maybe_set_bit(status & uap->vendor->fr_cts, &result, TIOCM_CTS);
  1425. pl011_maybe_set_bit(status & uap->vendor->fr_ri, &result, TIOCM_RNG);
  1426. return result;
  1427. }
  1428. static void pl011_assign_bit(bool cond, unsigned int *ptr, unsigned int mask)
  1429. {
  1430. if (cond)
  1431. *ptr |= mask;
  1432. else
  1433. *ptr &= ~mask;
  1434. }
  1435. static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1436. {
  1437. struct uart_amba_port *uap =
  1438. container_of(port, struct uart_amba_port, port);
  1439. unsigned int cr;
  1440. cr = pl011_read(uap, REG_CR);
  1441. pl011_assign_bit(mctrl & TIOCM_RTS, &cr, UART011_CR_RTS);
  1442. pl011_assign_bit(mctrl & TIOCM_DTR, &cr, UART011_CR_DTR);
  1443. pl011_assign_bit(mctrl & TIOCM_OUT1, &cr, UART011_CR_OUT1);
  1444. pl011_assign_bit(mctrl & TIOCM_OUT2, &cr, UART011_CR_OUT2);
  1445. pl011_assign_bit(mctrl & TIOCM_LOOP, &cr, UART011_CR_LBE);
  1446. if (port->status & UPSTAT_AUTORTS) {
  1447. /* We need to disable auto-RTS if we want to turn RTS off */
  1448. pl011_assign_bit(mctrl & TIOCM_RTS, &cr, UART011_CR_RTSEN);
  1449. }
  1450. pl011_write(cr, uap, REG_CR);
  1451. }
  1452. static void pl011_break_ctl(struct uart_port *port, int break_state)
  1453. {
  1454. struct uart_amba_port *uap =
  1455. container_of(port, struct uart_amba_port, port);
  1456. unsigned long flags;
  1457. unsigned int lcr_h;
  1458. uart_port_lock_irqsave(&uap->port, &flags);
  1459. lcr_h = pl011_read(uap, REG_LCRH_TX);
  1460. if (break_state == -1)
  1461. lcr_h |= UART01x_LCRH_BRK;
  1462. else
  1463. lcr_h &= ~UART01x_LCRH_BRK;
  1464. pl011_write(lcr_h, uap, REG_LCRH_TX);
  1465. uart_port_unlock_irqrestore(&uap->port, flags);
  1466. }
  1467. #ifdef CONFIG_CONSOLE_POLL
  1468. static void pl011_quiesce_irqs(struct uart_port *port)
  1469. {
  1470. struct uart_amba_port *uap =
  1471. container_of(port, struct uart_amba_port, port);
  1472. pl011_write(pl011_read(uap, REG_MIS), uap, REG_ICR);
  1473. /*
  1474. * There is no way to clear TXIM as this is "ready to transmit IRQ", so
  1475. * we simply mask it. start_tx() will unmask it.
  1476. *
  1477. * Note we can race with start_tx(), and if the race happens, the
  1478. * polling user might get another interrupt just after we clear it.
  1479. * But it should be OK and can happen even w/o the race, e.g.
  1480. * controller immediately got some new data and raised the IRQ.
  1481. *
  1482. * And whoever uses polling routines assumes that it manages the device
  1483. * (including tx queue), so we're also fine with start_tx()'s caller
  1484. * side.
  1485. */
  1486. pl011_write(pl011_read(uap, REG_IMSC) & ~UART011_TXIM, uap,
  1487. REG_IMSC);
  1488. }
  1489. static int pl011_get_poll_char(struct uart_port *port)
  1490. {
  1491. struct uart_amba_port *uap =
  1492. container_of(port, struct uart_amba_port, port);
  1493. unsigned int status;
  1494. /*
  1495. * The caller might need IRQs lowered, e.g. if used with KDB NMI
  1496. * debugger.
  1497. */
  1498. pl011_quiesce_irqs(port);
  1499. status = pl011_read(uap, REG_FR);
  1500. if (status & UART01x_FR_RXFE)
  1501. return NO_POLL_CHAR;
  1502. return pl011_read(uap, REG_DR);
  1503. }
  1504. static void pl011_put_poll_char(struct uart_port *port, unsigned char ch)
  1505. {
  1506. struct uart_amba_port *uap =
  1507. container_of(port, struct uart_amba_port, port);
  1508. while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
  1509. cpu_relax();
  1510. pl011_write(ch, uap, REG_DR);
  1511. }
  1512. #endif /* CONFIG_CONSOLE_POLL */
  1513. static int pl011_hwinit(struct uart_port *port)
  1514. {
  1515. struct uart_amba_port *uap =
  1516. container_of(port, struct uart_amba_port, port);
  1517. int retval;
  1518. /* Optionaly enable pins to be muxed in and configured */
  1519. pinctrl_pm_select_default_state(port->dev);
  1520. /*
  1521. * Try to enable the clock producer.
  1522. */
  1523. retval = clk_prepare_enable(uap->clk);
  1524. if (retval)
  1525. return retval;
  1526. uap->port.uartclk = clk_get_rate(uap->clk);
  1527. /* Clear pending error and receive interrupts */
  1528. pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
  1529. UART011_FEIS | UART011_RTIS | UART011_RXIS,
  1530. uap, REG_ICR);
  1531. /*
  1532. * Save interrupts enable mask, and enable RX interrupts in case if
  1533. * the interrupt is used for NMI entry.
  1534. */
  1535. uap->im = pl011_read(uap, REG_IMSC);
  1536. pl011_write(UART011_RTIM | UART011_RXIM, uap, REG_IMSC);
  1537. if (dev_get_platdata(uap->port.dev)) {
  1538. struct amba_pl011_data *plat;
  1539. plat = dev_get_platdata(uap->port.dev);
  1540. if (plat->init)
  1541. plat->init();
  1542. }
  1543. return 0;
  1544. }
  1545. static bool pl011_split_lcrh(const struct uart_amba_port *uap)
  1546. {
  1547. return pl011_reg_to_offset(uap, REG_LCRH_RX) !=
  1548. pl011_reg_to_offset(uap, REG_LCRH_TX);
  1549. }
  1550. static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
  1551. {
  1552. pl011_write(lcr_h, uap, REG_LCRH_RX);
  1553. if (pl011_split_lcrh(uap)) {
  1554. int i;
  1555. /*
  1556. * Wait 10 PCLKs before writing LCRH_TX register,
  1557. * to get this delay write read only register 10 times
  1558. */
  1559. for (i = 0; i < 10; ++i)
  1560. pl011_write(0xff, uap, REG_MIS);
  1561. pl011_write(lcr_h, uap, REG_LCRH_TX);
  1562. }
  1563. }
  1564. static int pl011_allocate_irq(struct uart_amba_port *uap)
  1565. {
  1566. pl011_write(uap->im, uap, REG_IMSC);
  1567. return request_irq(uap->port.irq, pl011_int, IRQF_SHARED, "uart-pl011", uap);
  1568. }
  1569. /*
  1570. * Enable interrupts, only timeouts when using DMA
  1571. * if initial RX DMA job failed, start in interrupt mode
  1572. * as well.
  1573. */
  1574. static void pl011_enable_interrupts(struct uart_amba_port *uap)
  1575. {
  1576. unsigned long flags;
  1577. unsigned int i;
  1578. uart_port_lock_irqsave(&uap->port, &flags);
  1579. /* Clear out any spuriously appearing RX interrupts */
  1580. pl011_write(UART011_RTIS | UART011_RXIS, uap, REG_ICR);
  1581. /*
  1582. * RXIS is asserted only when the RX FIFO transitions from below
  1583. * to above the trigger threshold. If the RX FIFO is already
  1584. * full to the threshold this can't happen and RXIS will now be
  1585. * stuck off. Drain the RX FIFO explicitly to fix this:
  1586. */
  1587. for (i = 0; i < uap->fifosize * 2; ++i) {
  1588. if (pl011_read(uap, REG_FR) & UART01x_FR_RXFE)
  1589. break;
  1590. pl011_read(uap, REG_DR);
  1591. }
  1592. uap->im = UART011_RTIM;
  1593. if (!pl011_dma_rx_running(uap))
  1594. uap->im |= UART011_RXIM;
  1595. pl011_write(uap->im, uap, REG_IMSC);
  1596. uart_port_unlock_irqrestore(&uap->port, flags);
  1597. }
  1598. static void pl011_unthrottle_rx(struct uart_port *port)
  1599. {
  1600. struct uart_amba_port *uap = container_of(port, struct uart_amba_port, port);
  1601. unsigned long flags;
  1602. uart_port_lock_irqsave(&uap->port, &flags);
  1603. uap->im = UART011_RTIM;
  1604. if (!pl011_dma_rx_running(uap))
  1605. uap->im |= UART011_RXIM;
  1606. pl011_write(uap->im, uap, REG_IMSC);
  1607. #ifdef CONFIG_DMA_ENGINE
  1608. if (uap->using_rx_dma) {
  1609. uap->dmacr |= UART011_RXDMAE;
  1610. pl011_write(uap->dmacr, uap, REG_DMACR);
  1611. }
  1612. #endif
  1613. uart_port_unlock_irqrestore(&uap->port, flags);
  1614. }
  1615. static int pl011_startup(struct uart_port *port)
  1616. {
  1617. struct uart_amba_port *uap =
  1618. container_of(port, struct uart_amba_port, port);
  1619. unsigned int cr;
  1620. int retval;
  1621. retval = pl011_hwinit(port);
  1622. if (retval)
  1623. goto clk_dis;
  1624. retval = pl011_allocate_irq(uap);
  1625. if (retval)
  1626. goto clk_dis;
  1627. pl011_write(uap->vendor->ifls, uap, REG_IFLS);
  1628. uart_port_lock_irq(&uap->port);
  1629. cr = pl011_read(uap, REG_CR);
  1630. cr &= UART011_CR_RTS | UART011_CR_DTR;
  1631. cr |= UART01x_CR_UARTEN | UART011_CR_RXE;
  1632. if (!(port->rs485.flags & SER_RS485_ENABLED))
  1633. cr |= UART011_CR_TXE;
  1634. pl011_write(cr, uap, REG_CR);
  1635. uart_port_unlock_irq(&uap->port);
  1636. /*
  1637. * initialise the old status of the modem signals
  1638. */
  1639. uap->old_status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
  1640. /* Startup DMA */
  1641. pl011_dma_startup(uap);
  1642. pl011_enable_interrupts(uap);
  1643. return 0;
  1644. clk_dis:
  1645. clk_disable_unprepare(uap->clk);
  1646. return retval;
  1647. }
  1648. static int sbsa_uart_startup(struct uart_port *port)
  1649. {
  1650. struct uart_amba_port *uap =
  1651. container_of(port, struct uart_amba_port, port);
  1652. int retval;
  1653. retval = pl011_hwinit(port);
  1654. if (retval)
  1655. return retval;
  1656. retval = pl011_allocate_irq(uap);
  1657. if (retval)
  1658. return retval;
  1659. /* The SBSA UART does not support any modem status lines. */
  1660. uap->old_status = 0;
  1661. pl011_enable_interrupts(uap);
  1662. return 0;
  1663. }
  1664. static void pl011_shutdown_channel(struct uart_amba_port *uap, unsigned int lcrh)
  1665. {
  1666. unsigned long val;
  1667. val = pl011_read(uap, lcrh);
  1668. val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
  1669. pl011_write(val, uap, lcrh);
  1670. }
  1671. /*
  1672. * disable the port. It should not disable RTS and DTR.
  1673. * Also RTS and DTR state should be preserved to restore
  1674. * it during startup().
  1675. */
  1676. static void pl011_disable_uart(struct uart_amba_port *uap)
  1677. {
  1678. unsigned int cr;
  1679. uap->port.status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
  1680. uart_port_lock_irq(&uap->port);
  1681. cr = pl011_read(uap, REG_CR);
  1682. cr &= UART011_CR_RTS | UART011_CR_DTR;
  1683. cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
  1684. pl011_write(cr, uap, REG_CR);
  1685. uart_port_unlock_irq(&uap->port);
  1686. /*
  1687. * disable break condition and fifos
  1688. */
  1689. pl011_shutdown_channel(uap, REG_LCRH_RX);
  1690. if (pl011_split_lcrh(uap))
  1691. pl011_shutdown_channel(uap, REG_LCRH_TX);
  1692. }
  1693. static void pl011_disable_interrupts(struct uart_amba_port *uap)
  1694. {
  1695. uart_port_lock_irq(&uap->port);
  1696. /* mask all interrupts and clear all pending ones */
  1697. uap->im = 0;
  1698. pl011_write(uap->im, uap, REG_IMSC);
  1699. pl011_write(0xffff, uap, REG_ICR);
  1700. uart_port_unlock_irq(&uap->port);
  1701. }
  1702. static void pl011_shutdown(struct uart_port *port)
  1703. {
  1704. struct uart_amba_port *uap =
  1705. container_of(port, struct uart_amba_port, port);
  1706. pl011_disable_interrupts(uap);
  1707. pl011_dma_shutdown(uap);
  1708. if ((port->rs485.flags & SER_RS485_ENABLED && uap->rs485_tx_state != OFF))
  1709. pl011_rs485_tx_stop(uap);
  1710. free_irq(uap->port.irq, uap);
  1711. pl011_disable_uart(uap);
  1712. /*
  1713. * Shut down the clock producer
  1714. */
  1715. clk_disable_unprepare(uap->clk);
  1716. /* Optionally let pins go into sleep states */
  1717. pinctrl_pm_select_sleep_state(port->dev);
  1718. if (dev_get_platdata(uap->port.dev)) {
  1719. struct amba_pl011_data *plat;
  1720. plat = dev_get_platdata(uap->port.dev);
  1721. if (plat->exit)
  1722. plat->exit();
  1723. }
  1724. if (uap->port.ops->flush_buffer)
  1725. uap->port.ops->flush_buffer(port);
  1726. }
  1727. static void sbsa_uart_shutdown(struct uart_port *port)
  1728. {
  1729. struct uart_amba_port *uap =
  1730. container_of(port, struct uart_amba_port, port);
  1731. pl011_disable_interrupts(uap);
  1732. free_irq(uap->port.irq, uap);
  1733. if (uap->port.ops->flush_buffer)
  1734. uap->port.ops->flush_buffer(port);
  1735. }
  1736. static void
  1737. pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios)
  1738. {
  1739. port->read_status_mask = UART011_DR_OE | 255;
  1740. if (termios->c_iflag & INPCK)
  1741. port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
  1742. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  1743. port->read_status_mask |= UART011_DR_BE;
  1744. /*
  1745. * Characters to ignore
  1746. */
  1747. port->ignore_status_mask = 0;
  1748. if (termios->c_iflag & IGNPAR)
  1749. port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
  1750. if (termios->c_iflag & IGNBRK) {
  1751. port->ignore_status_mask |= UART011_DR_BE;
  1752. /*
  1753. * If we're ignoring parity and break indicators,
  1754. * ignore overruns too (for real raw support).
  1755. */
  1756. if (termios->c_iflag & IGNPAR)
  1757. port->ignore_status_mask |= UART011_DR_OE;
  1758. }
  1759. /*
  1760. * Ignore all characters if CREAD is not set.
  1761. */
  1762. if ((termios->c_cflag & CREAD) == 0)
  1763. port->ignore_status_mask |= UART_DUMMY_DR_RX;
  1764. }
  1765. static void
  1766. pl011_set_termios(struct uart_port *port, struct ktermios *termios,
  1767. const struct ktermios *old)
  1768. {
  1769. struct uart_amba_port *uap =
  1770. container_of(port, struct uart_amba_port, port);
  1771. unsigned int lcr_h, old_cr;
  1772. unsigned long flags;
  1773. unsigned int baud, quot, clkdiv;
  1774. unsigned int bits;
  1775. if (uap->vendor->oversampling)
  1776. clkdiv = 8;
  1777. else
  1778. clkdiv = 16;
  1779. /*
  1780. * Ask the core to calculate the divisor for us.
  1781. */
  1782. baud = uart_get_baud_rate(port, termios, old, 0,
  1783. port->uartclk / clkdiv);
  1784. #ifdef CONFIG_DMA_ENGINE
  1785. /*
  1786. * Adjust RX DMA polling rate with baud rate if not specified.
  1787. */
  1788. if (uap->dmarx.auto_poll_rate)
  1789. uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
  1790. #endif
  1791. if (baud > port->uartclk / 16)
  1792. quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
  1793. else
  1794. quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
  1795. switch (termios->c_cflag & CSIZE) {
  1796. case CS5:
  1797. lcr_h = UART01x_LCRH_WLEN_5;
  1798. break;
  1799. case CS6:
  1800. lcr_h = UART01x_LCRH_WLEN_6;
  1801. break;
  1802. case CS7:
  1803. lcr_h = UART01x_LCRH_WLEN_7;
  1804. break;
  1805. default: // CS8
  1806. lcr_h = UART01x_LCRH_WLEN_8;
  1807. break;
  1808. }
  1809. if (termios->c_cflag & CSTOPB)
  1810. lcr_h |= UART01x_LCRH_STP2;
  1811. if (termios->c_cflag & PARENB) {
  1812. lcr_h |= UART01x_LCRH_PEN;
  1813. if (!(termios->c_cflag & PARODD))
  1814. lcr_h |= UART01x_LCRH_EPS;
  1815. if (termios->c_cflag & CMSPAR)
  1816. lcr_h |= UART011_LCRH_SPS;
  1817. }
  1818. if (uap->fifosize > 1)
  1819. lcr_h |= UART01x_LCRH_FEN;
  1820. bits = tty_get_frame_size(termios->c_cflag);
  1821. uart_port_lock_irqsave(port, &flags);
  1822. /*
  1823. * Update the per-port timeout.
  1824. */
  1825. uart_update_timeout(port, termios->c_cflag, baud);
  1826. /*
  1827. * Calculate the approximated time it takes to transmit one character
  1828. * with the given baud rate. We use this as the poll interval when we
  1829. * wait for the tx queue to empty.
  1830. */
  1831. uap->rs485_tx_drain_interval = ns_to_ktime(DIV_ROUND_UP(bits * NSEC_PER_SEC, baud));
  1832. pl011_setup_status_masks(port, termios);
  1833. if (UART_ENABLE_MS(port, termios->c_cflag))
  1834. pl011_enable_ms(port);
  1835. if (port->rs485.flags & SER_RS485_ENABLED)
  1836. termios->c_cflag &= ~CRTSCTS;
  1837. old_cr = pl011_read(uap, REG_CR);
  1838. if (termios->c_cflag & CRTSCTS) {
  1839. if (old_cr & UART011_CR_RTS)
  1840. old_cr |= UART011_CR_RTSEN;
  1841. old_cr |= UART011_CR_CTSEN;
  1842. port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
  1843. } else {
  1844. old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
  1845. port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
  1846. }
  1847. if (uap->vendor->oversampling) {
  1848. if (baud > port->uartclk / 16)
  1849. old_cr |= ST_UART011_CR_OVSFACT;
  1850. else
  1851. old_cr &= ~ST_UART011_CR_OVSFACT;
  1852. }
  1853. /*
  1854. * Workaround for the ST Micro oversampling variants to
  1855. * increase the bitrate slightly, by lowering the divisor,
  1856. * to avoid delayed sampling of start bit at high speeds,
  1857. * else we see data corruption.
  1858. */
  1859. if (uap->vendor->oversampling) {
  1860. if (baud >= 3000000 && baud < 3250000 && quot > 1)
  1861. quot -= 1;
  1862. else if (baud > 3250000 && quot > 2)
  1863. quot -= 2;
  1864. }
  1865. /* Set baud rate */
  1866. pl011_write(quot & 0x3f, uap, REG_FBRD);
  1867. pl011_write(quot >> 6, uap, REG_IBRD);
  1868. /*
  1869. * ----------v----------v----------v----------v-----
  1870. * NOTE: REG_LCRH_TX and REG_LCRH_RX MUST BE WRITTEN AFTER
  1871. * REG_FBRD & REG_IBRD.
  1872. * ----------^----------^----------^----------^-----
  1873. */
  1874. pl011_write_lcr_h(uap, lcr_h);
  1875. /*
  1876. * Receive was disabled by pl011_disable_uart during shutdown.
  1877. * Need to reenable receive if you need to use a tty_driver
  1878. * returns from tty_find_polling_driver() after a port shutdown.
  1879. */
  1880. old_cr |= UART011_CR_RXE;
  1881. pl011_write(old_cr, uap, REG_CR);
  1882. uart_port_unlock_irqrestore(port, flags);
  1883. }
  1884. static void
  1885. sbsa_uart_set_termios(struct uart_port *port, struct ktermios *termios,
  1886. const struct ktermios *old)
  1887. {
  1888. struct uart_amba_port *uap =
  1889. container_of(port, struct uart_amba_port, port);
  1890. unsigned long flags;
  1891. tty_termios_encode_baud_rate(termios, uap->fixed_baud, uap->fixed_baud);
  1892. /* The SBSA UART only supports 8n1 without hardware flow control. */
  1893. termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
  1894. termios->c_cflag &= ~(CMSPAR | CRTSCTS);
  1895. termios->c_cflag |= CS8 | CLOCAL;
  1896. uart_port_lock_irqsave(port, &flags);
  1897. uart_update_timeout(port, CS8, uap->fixed_baud);
  1898. pl011_setup_status_masks(port, termios);
  1899. uart_port_unlock_irqrestore(port, flags);
  1900. }
  1901. static const char *pl011_type(struct uart_port *port)
  1902. {
  1903. struct uart_amba_port *uap =
  1904. container_of(port, struct uart_amba_port, port);
  1905. return uap->port.type == PORT_AMBA ? uap->type : NULL;
  1906. }
  1907. /*
  1908. * Configure/autoconfigure the port.
  1909. */
  1910. static void pl011_config_port(struct uart_port *port, int flags)
  1911. {
  1912. if (flags & UART_CONFIG_TYPE)
  1913. port->type = PORT_AMBA;
  1914. }
  1915. /*
  1916. * verify the new serial_struct (for TIOCSSERIAL).
  1917. */
  1918. static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
  1919. {
  1920. int ret = 0;
  1921. if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
  1922. ret = -EINVAL;
  1923. if (ser->irq < 0 || ser->irq >= irq_get_nr_irqs())
  1924. ret = -EINVAL;
  1925. if (ser->baud_base < 9600)
  1926. ret = -EINVAL;
  1927. if (port->mapbase != (unsigned long)ser->iomem_base)
  1928. ret = -EINVAL;
  1929. return ret;
  1930. }
  1931. static int pl011_rs485_config(struct uart_port *port, struct ktermios *termios,
  1932. struct serial_rs485 *rs485)
  1933. {
  1934. struct uart_amba_port *uap =
  1935. container_of(port, struct uart_amba_port, port);
  1936. if (port->rs485.flags & SER_RS485_ENABLED)
  1937. pl011_rs485_tx_stop(uap);
  1938. /* Make sure auto RTS is disabled */
  1939. if (rs485->flags & SER_RS485_ENABLED) {
  1940. u32 cr = pl011_read(uap, REG_CR);
  1941. cr &= ~UART011_CR_RTSEN;
  1942. pl011_write(cr, uap, REG_CR);
  1943. port->status &= ~UPSTAT_AUTORTS;
  1944. }
  1945. return 0;
  1946. }
  1947. static const struct uart_ops amba_pl011_pops = {
  1948. .tx_empty = pl011_tx_empty,
  1949. .set_mctrl = pl011_set_mctrl,
  1950. .get_mctrl = pl011_get_mctrl,
  1951. .stop_tx = pl011_stop_tx,
  1952. .start_tx = pl011_start_tx,
  1953. .stop_rx = pl011_stop_rx,
  1954. .throttle = pl011_throttle_rx,
  1955. .unthrottle = pl011_unthrottle_rx,
  1956. .enable_ms = pl011_enable_ms,
  1957. .break_ctl = pl011_break_ctl,
  1958. .startup = pl011_startup,
  1959. .shutdown = pl011_shutdown,
  1960. .flush_buffer = pl011_dma_flush_buffer,
  1961. .set_termios = pl011_set_termios,
  1962. .type = pl011_type,
  1963. .config_port = pl011_config_port,
  1964. .verify_port = pl011_verify_port,
  1965. #ifdef CONFIG_CONSOLE_POLL
  1966. .poll_init = pl011_hwinit,
  1967. .poll_get_char = pl011_get_poll_char,
  1968. .poll_put_char = pl011_put_poll_char,
  1969. #endif
  1970. };
  1971. static void sbsa_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1972. {
  1973. }
  1974. static unsigned int sbsa_uart_get_mctrl(struct uart_port *port)
  1975. {
  1976. return 0;
  1977. }
  1978. static const struct uart_ops sbsa_uart_pops = {
  1979. .tx_empty = pl011_tx_empty,
  1980. .set_mctrl = sbsa_uart_set_mctrl,
  1981. .get_mctrl = sbsa_uart_get_mctrl,
  1982. .stop_tx = pl011_stop_tx,
  1983. .start_tx = pl011_start_tx,
  1984. .stop_rx = pl011_stop_rx,
  1985. .startup = sbsa_uart_startup,
  1986. .shutdown = sbsa_uart_shutdown,
  1987. .set_termios = sbsa_uart_set_termios,
  1988. .type = pl011_type,
  1989. .config_port = pl011_config_port,
  1990. .verify_port = pl011_verify_port,
  1991. #ifdef CONFIG_CONSOLE_POLL
  1992. .poll_init = pl011_hwinit,
  1993. .poll_get_char = pl011_get_poll_char,
  1994. .poll_put_char = pl011_put_poll_char,
  1995. #endif
  1996. };
  1997. static struct uart_amba_port *amba_ports[UART_NR];
  1998. #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
  1999. static void pl011_console_putchar(struct uart_port *port, unsigned char ch)
  2000. {
  2001. struct uart_amba_port *uap =
  2002. container_of(port, struct uart_amba_port, port);
  2003. while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
  2004. cpu_relax();
  2005. pl011_write(ch, uap, REG_DR);
  2006. uap->console_line_ended = (ch == '\n');
  2007. }
  2008. static void pl011_console_get_options(struct uart_amba_port *uap, int *baud,
  2009. int *parity, int *bits)
  2010. {
  2011. unsigned int lcr_h, ibrd, fbrd;
  2012. if (!(pl011_read(uap, REG_CR) & UART01x_CR_UARTEN))
  2013. return;
  2014. lcr_h = pl011_read(uap, REG_LCRH_TX);
  2015. *parity = 'n';
  2016. if (lcr_h & UART01x_LCRH_PEN) {
  2017. if (lcr_h & UART01x_LCRH_EPS)
  2018. *parity = 'e';
  2019. else
  2020. *parity = 'o';
  2021. }
  2022. if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
  2023. *bits = 7;
  2024. else
  2025. *bits = 8;
  2026. ibrd = pl011_read(uap, REG_IBRD);
  2027. fbrd = pl011_read(uap, REG_FBRD);
  2028. *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
  2029. if (uap->vendor->oversampling &&
  2030. (pl011_read(uap, REG_CR) & ST_UART011_CR_OVSFACT))
  2031. *baud *= 2;
  2032. }
  2033. static int pl011_console_setup(struct console *co, char *options)
  2034. {
  2035. struct uart_amba_port *uap;
  2036. int baud = 38400;
  2037. int bits = 8;
  2038. int parity = 'n';
  2039. int flow = 'n';
  2040. int ret;
  2041. /*
  2042. * Check whether an invalid uart number has been specified, and
  2043. * if so, search for the first available port that does have
  2044. * console support.
  2045. */
  2046. if (co->index >= UART_NR)
  2047. co->index = 0;
  2048. uap = amba_ports[co->index];
  2049. if (!uap)
  2050. return -ENODEV;
  2051. /* Allow pins to be muxed in and configured */
  2052. pinctrl_pm_select_default_state(uap->port.dev);
  2053. ret = clk_prepare(uap->clk);
  2054. if (ret)
  2055. return ret;
  2056. uap->console_line_ended = true;
  2057. if (dev_get_platdata(uap->port.dev)) {
  2058. struct amba_pl011_data *plat;
  2059. plat = dev_get_platdata(uap->port.dev);
  2060. if (plat->init)
  2061. plat->init();
  2062. }
  2063. uap->port.uartclk = clk_get_rate(uap->clk);
  2064. if (uap->vendor->fixed_options) {
  2065. baud = uap->fixed_baud;
  2066. } else {
  2067. if (options)
  2068. uart_parse_options(options,
  2069. &baud, &parity, &bits, &flow);
  2070. else
  2071. pl011_console_get_options(uap, &baud, &parity, &bits);
  2072. }
  2073. return uart_set_options(&uap->port, co, baud, parity, bits, flow);
  2074. }
  2075. /**
  2076. * pl011_console_match - non-standard console matching
  2077. * @co: registering console
  2078. * @name: name from console command line
  2079. * @idx: index from console command line
  2080. * @options: ptr to option string from console command line
  2081. *
  2082. * Only attempts to match console command lines of the form:
  2083. * console=pl011,mmio|mmio32,<addr>[,<options>]
  2084. * console=pl011,0x<addr>[,<options>]
  2085. * This form is used to register an initial earlycon boot console and
  2086. * replace it with the amba_console at pl011 driver init.
  2087. *
  2088. * Performs console setup for a match (as required by interface)
  2089. * If no <options> are specified, then assume the h/w is already setup.
  2090. *
  2091. * Returns 0 if console matches; otherwise non-zero to use default matching
  2092. */
  2093. static int pl011_console_match(struct console *co, char *name, int idx,
  2094. char *options)
  2095. {
  2096. enum uart_iotype iotype;
  2097. resource_size_t addr;
  2098. int i;
  2099. /*
  2100. * Systems affected by the Qualcomm Technologies QDF2400 E44 erratum
  2101. * have a distinct console name, so make sure we check for that.
  2102. * The actual implementation of the erratum occurs in the probe
  2103. * function.
  2104. */
  2105. if ((strcmp(name, "qdf2400_e44") != 0) && (strcmp(name, "pl011") != 0))
  2106. return -ENODEV;
  2107. if (uart_parse_earlycon(options, &iotype, &addr, &options))
  2108. return -ENODEV;
  2109. if (iotype != UPIO_MEM && iotype != UPIO_MEM32)
  2110. return -ENODEV;
  2111. /* try to match the port specified on the command line */
  2112. for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
  2113. struct uart_port *port;
  2114. if (!amba_ports[i])
  2115. continue;
  2116. port = &amba_ports[i]->port;
  2117. if (port->mapbase != addr)
  2118. continue;
  2119. co->index = i;
  2120. uart_port_set_cons(port, co);
  2121. return pl011_console_setup(co, options);
  2122. }
  2123. return -ENODEV;
  2124. }
  2125. static void
  2126. pl011_console_write_atomic(struct console *co, struct nbcon_write_context *wctxt)
  2127. {
  2128. struct uart_amba_port *uap = amba_ports[co->index];
  2129. unsigned int old_cr = 0;
  2130. if (!nbcon_enter_unsafe(wctxt))
  2131. return;
  2132. clk_enable(uap->clk);
  2133. if (!uap->vendor->always_enabled) {
  2134. old_cr = pl011_read(uap, REG_CR);
  2135. pl011_write((old_cr & ~UART011_CR_CTSEN) | (UART01x_CR_UARTEN | UART011_CR_TXE),
  2136. uap, REG_CR);
  2137. }
  2138. if (!uap->console_line_ended)
  2139. uart_console_write(&uap->port, "\n", 1, pl011_console_putchar);
  2140. uart_console_write(&uap->port, wctxt->outbuf, wctxt->len, pl011_console_putchar);
  2141. while ((pl011_read(uap, REG_FR) ^ uap->vendor->inv_fr) & uap->vendor->fr_busy)
  2142. cpu_relax();
  2143. if (!uap->vendor->always_enabled)
  2144. pl011_write(old_cr, uap, REG_CR);
  2145. clk_disable(uap->clk);
  2146. nbcon_exit_unsafe(wctxt);
  2147. }
  2148. static void
  2149. pl011_console_write_thread(struct console *co, struct nbcon_write_context *wctxt)
  2150. {
  2151. struct uart_amba_port *uap = amba_ports[co->index];
  2152. unsigned int old_cr = 0;
  2153. if (!nbcon_enter_unsafe(wctxt))
  2154. return;
  2155. clk_enable(uap->clk);
  2156. if (!uap->vendor->always_enabled) {
  2157. old_cr = pl011_read(uap, REG_CR);
  2158. pl011_write((old_cr & ~UART011_CR_CTSEN) | (UART01x_CR_UARTEN | UART011_CR_TXE),
  2159. uap, REG_CR);
  2160. }
  2161. if (nbcon_exit_unsafe(wctxt)) {
  2162. int i;
  2163. unsigned int len = READ_ONCE(wctxt->len);
  2164. for (i = 0; i < len; i++) {
  2165. if (!nbcon_enter_unsafe(wctxt))
  2166. break;
  2167. uart_console_write(&uap->port, wctxt->outbuf + i, 1, pl011_console_putchar);
  2168. if (!nbcon_exit_unsafe(wctxt))
  2169. break;
  2170. }
  2171. }
  2172. while (!nbcon_enter_unsafe(wctxt))
  2173. nbcon_reacquire_nobuf(wctxt);
  2174. while ((pl011_read(uap, REG_FR) ^ uap->vendor->inv_fr) & uap->vendor->fr_busy)
  2175. cpu_relax();
  2176. if (!uap->vendor->always_enabled)
  2177. pl011_write(old_cr, uap, REG_CR);
  2178. clk_disable(uap->clk);
  2179. nbcon_exit_unsafe(wctxt);
  2180. }
  2181. static void
  2182. pl011_console_device_lock(struct console *co, unsigned long *flags)
  2183. {
  2184. __uart_port_lock_irqsave(&amba_ports[co->index]->port, flags);
  2185. }
  2186. static void
  2187. pl011_console_device_unlock(struct console *co, unsigned long flags)
  2188. {
  2189. __uart_port_unlock_irqrestore(&amba_ports[co->index]->port, flags);
  2190. }
  2191. static struct uart_driver amba_reg;
  2192. static struct console amba_console = {
  2193. .name = "ttyAMA",
  2194. .device = uart_console_device,
  2195. .setup = pl011_console_setup,
  2196. .match = pl011_console_match,
  2197. .write_atomic = pl011_console_write_atomic,
  2198. .write_thread = pl011_console_write_thread,
  2199. .device_lock = pl011_console_device_lock,
  2200. .device_unlock = pl011_console_device_unlock,
  2201. .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_NBCON,
  2202. .index = -1,
  2203. .data = &amba_reg,
  2204. };
  2205. #define AMBA_CONSOLE (&amba_console)
  2206. static void qdf2400_e44_putc(struct uart_port *port, unsigned char c)
  2207. {
  2208. while (readl(port->membase + UART01x_FR) & UART01x_FR_TXFF)
  2209. cpu_relax();
  2210. writel(c, port->membase + UART01x_DR);
  2211. while (!(readl(port->membase + UART01x_FR) & UART011_FR_TXFE))
  2212. cpu_relax();
  2213. }
  2214. static void qdf2400_e44_early_write(struct console *con, const char *s, unsigned int n)
  2215. {
  2216. struct earlycon_device *dev = con->data;
  2217. uart_console_write(&dev->port, s, n, qdf2400_e44_putc);
  2218. }
  2219. static void pl011_putc(struct uart_port *port, unsigned char c)
  2220. {
  2221. while (readl(port->membase + UART01x_FR) & UART01x_FR_TXFF)
  2222. cpu_relax();
  2223. if (port->iotype == UPIO_MEM32)
  2224. writel(c, port->membase + UART01x_DR);
  2225. else
  2226. writeb(c, port->membase + UART01x_DR);
  2227. while (readl(port->membase + UART01x_FR) & UART01x_FR_BUSY)
  2228. cpu_relax();
  2229. }
  2230. static void pl011_early_write(struct console *con, const char *s, unsigned int n)
  2231. {
  2232. struct earlycon_device *dev = con->data;
  2233. uart_console_write(&dev->port, s, n, pl011_putc);
  2234. }
  2235. #ifdef CONFIG_CONSOLE_POLL
  2236. static int pl011_getc(struct uart_port *port)
  2237. {
  2238. if (readl(port->membase + UART01x_FR) & UART01x_FR_RXFE)
  2239. return NO_POLL_CHAR;
  2240. if (port->iotype == UPIO_MEM32)
  2241. return readl(port->membase + UART01x_DR);
  2242. else
  2243. return readb(port->membase + UART01x_DR);
  2244. }
  2245. static int pl011_early_read(struct console *con, char *s, unsigned int n)
  2246. {
  2247. struct earlycon_device *dev = con->data;
  2248. int ch, num_read = 0;
  2249. while (num_read < n) {
  2250. ch = pl011_getc(&dev->port);
  2251. if (ch == NO_POLL_CHAR)
  2252. break;
  2253. s[num_read++] = ch;
  2254. }
  2255. return num_read;
  2256. }
  2257. #else
  2258. #define pl011_early_read NULL
  2259. #endif
  2260. /*
  2261. * On non-ACPI systems, earlycon is enabled by specifying
  2262. * "earlycon=pl011,<address>" on the kernel command line.
  2263. *
  2264. * On ACPI ARM64 systems, an "early" console is enabled via the SPCR table,
  2265. * by specifying only "earlycon" on the command line. Because it requires
  2266. * SPCR, the console starts after ACPI is parsed, which is later than a
  2267. * traditional early console.
  2268. *
  2269. * To get the traditional early console that starts before ACPI is parsed,
  2270. * specify the full "earlycon=pl011,<address>" option.
  2271. */
  2272. static int __init pl011_early_console_setup(struct earlycon_device *device,
  2273. const char *opt)
  2274. {
  2275. if (!device->port.membase)
  2276. return -ENODEV;
  2277. device->con->write = pl011_early_write;
  2278. device->con->read = pl011_early_read;
  2279. return 0;
  2280. }
  2281. OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup);
  2282. OF_EARLYCON_DECLARE(pl011, "arm,sbsa-uart", pl011_early_console_setup);
  2283. /*
  2284. * On Qualcomm Datacenter Technologies QDF2400 SOCs affected by
  2285. * Erratum 44, traditional earlycon can be enabled by specifying
  2286. * "earlycon=qdf2400_e44,<address>". Any options are ignored.
  2287. *
  2288. * Alternatively, you can just specify "earlycon", and the early console
  2289. * will be enabled with the information from the SPCR table. In this
  2290. * case, the SPCR code will detect the need for the E44 work-around,
  2291. * and set the console name to "qdf2400_e44".
  2292. */
  2293. static int __init
  2294. qdf2400_e44_early_console_setup(struct earlycon_device *device,
  2295. const char *opt)
  2296. {
  2297. if (!device->port.membase)
  2298. return -ENODEV;
  2299. device->con->write = qdf2400_e44_early_write;
  2300. return 0;
  2301. }
  2302. EARLYCON_DECLARE(qdf2400_e44, qdf2400_e44_early_console_setup);
  2303. #else
  2304. #define AMBA_CONSOLE NULL
  2305. #endif
  2306. static struct uart_driver amba_reg = {
  2307. .owner = THIS_MODULE,
  2308. .driver_name = "ttyAMA",
  2309. .dev_name = "ttyAMA",
  2310. .major = SERIAL_AMBA_MAJOR,
  2311. .minor = SERIAL_AMBA_MINOR,
  2312. .nr = UART_NR,
  2313. .cons = AMBA_CONSOLE,
  2314. };
  2315. static int pl011_probe_dt_alias(int index, struct device *dev)
  2316. {
  2317. struct device_node *np;
  2318. static bool seen_dev_with_alias;
  2319. static bool seen_dev_without_alias;
  2320. int ret = index;
  2321. if (!IS_ENABLED(CONFIG_OF))
  2322. return ret;
  2323. np = dev->of_node;
  2324. if (!np)
  2325. return ret;
  2326. ret = of_alias_get_id(np, "serial");
  2327. if (ret < 0) {
  2328. seen_dev_without_alias = true;
  2329. ret = index;
  2330. } else {
  2331. seen_dev_with_alias = true;
  2332. if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret]) {
  2333. dev_warn(dev, "requested serial port %d not available.\n", ret);
  2334. ret = index;
  2335. }
  2336. }
  2337. if (seen_dev_with_alias && seen_dev_without_alias)
  2338. dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
  2339. return ret;
  2340. }
  2341. /* unregisters the driver also if no more ports are left */
  2342. static void pl011_unregister_port(struct uart_amba_port *uap)
  2343. {
  2344. int i;
  2345. bool busy = false;
  2346. for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
  2347. if (amba_ports[i] == uap)
  2348. amba_ports[i] = NULL;
  2349. else if (amba_ports[i])
  2350. busy = true;
  2351. }
  2352. pl011_dma_remove(uap);
  2353. if (!busy)
  2354. uart_unregister_driver(&amba_reg);
  2355. }
  2356. static int pl011_find_free_port(void)
  2357. {
  2358. int i;
  2359. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  2360. if (!amba_ports[i])
  2361. return i;
  2362. return -EBUSY;
  2363. }
  2364. static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap,
  2365. struct resource *mmiobase, int index)
  2366. {
  2367. void __iomem *base;
  2368. int ret;
  2369. base = devm_ioremap_resource(dev, mmiobase);
  2370. if (IS_ERR(base))
  2371. return PTR_ERR(base);
  2372. index = pl011_probe_dt_alias(index, dev);
  2373. uap->port.dev = dev;
  2374. uap->port.mapbase = mmiobase->start;
  2375. uap->port.membase = base;
  2376. uap->port.fifosize = uap->fifosize;
  2377. uap->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_AMBA_PL011_CONSOLE);
  2378. uap->port.flags = UPF_BOOT_AUTOCONF;
  2379. uap->port.line = index;
  2380. ret = uart_get_rs485_mode(&uap->port);
  2381. if (ret)
  2382. return ret;
  2383. amba_ports[index] = uap;
  2384. return 0;
  2385. }
  2386. static int pl011_register_port(struct uart_amba_port *uap)
  2387. {
  2388. int ret, i;
  2389. /* Ensure interrupts from this UART are masked and cleared */
  2390. pl011_write(0, uap, REG_IMSC);
  2391. pl011_write(0xffff, uap, REG_ICR);
  2392. if (!amba_reg.state) {
  2393. ret = uart_register_driver(&amba_reg);
  2394. if (ret < 0) {
  2395. dev_err(uap->port.dev,
  2396. "Failed to register AMBA-PL011 driver\n");
  2397. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  2398. if (amba_ports[i] == uap)
  2399. amba_ports[i] = NULL;
  2400. return ret;
  2401. }
  2402. }
  2403. ret = uart_add_one_port(&amba_reg, &uap->port);
  2404. if (ret)
  2405. pl011_unregister_port(uap);
  2406. return ret;
  2407. }
  2408. static const struct serial_rs485 pl011_rs485_supported = {
  2409. .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND |
  2410. SER_RS485_RX_DURING_TX,
  2411. .delay_rts_before_send = 1,
  2412. .delay_rts_after_send = 1,
  2413. };
  2414. static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
  2415. {
  2416. struct uart_amba_port *uap;
  2417. struct vendor_data *vendor = id->data;
  2418. int portnr, ret;
  2419. u32 val;
  2420. portnr = pl011_find_free_port();
  2421. if (portnr < 0)
  2422. return portnr;
  2423. uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
  2424. GFP_KERNEL);
  2425. if (!uap)
  2426. return -ENOMEM;
  2427. uap->clk = devm_clk_get(&dev->dev, NULL);
  2428. if (IS_ERR(uap->clk))
  2429. return PTR_ERR(uap->clk);
  2430. uap->reg_offset = vendor->reg_offset;
  2431. uap->vendor = vendor;
  2432. uap->fifosize = vendor->get_fifosize(dev);
  2433. uap->port.iotype = vendor->access_32b ? UPIO_MEM32 : UPIO_MEM;
  2434. uap->port.irq = dev->irq[0];
  2435. uap->port.ops = &amba_pl011_pops;
  2436. uap->port.rs485_config = pl011_rs485_config;
  2437. uap->port.rs485_supported = pl011_rs485_supported;
  2438. snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
  2439. if (device_property_read_u32(&dev->dev, "reg-io-width", &val) == 0) {
  2440. switch (val) {
  2441. case 1:
  2442. uap->port.iotype = UPIO_MEM;
  2443. break;
  2444. case 4:
  2445. uap->port.iotype = UPIO_MEM32;
  2446. break;
  2447. default:
  2448. dev_warn(&dev->dev, "unsupported reg-io-width (%d)\n",
  2449. val);
  2450. return -EINVAL;
  2451. }
  2452. }
  2453. hrtimer_setup(&uap->trigger_start_tx, pl011_trigger_start_tx, CLOCK_MONOTONIC,
  2454. HRTIMER_MODE_REL);
  2455. hrtimer_setup(&uap->trigger_stop_tx, pl011_trigger_stop_tx, CLOCK_MONOTONIC,
  2456. HRTIMER_MODE_REL);
  2457. ret = pl011_setup_port(&dev->dev, uap, &dev->res, portnr);
  2458. if (ret)
  2459. return ret;
  2460. amba_set_drvdata(dev, uap);
  2461. return pl011_register_port(uap);
  2462. }
  2463. static void pl011_remove(struct amba_device *dev)
  2464. {
  2465. struct uart_amba_port *uap = amba_get_drvdata(dev);
  2466. uart_remove_one_port(&amba_reg, &uap->port);
  2467. pl011_unregister_port(uap);
  2468. }
  2469. #ifdef CONFIG_PM_SLEEP
  2470. static int pl011_suspend(struct device *dev)
  2471. {
  2472. struct uart_amba_port *uap = dev_get_drvdata(dev);
  2473. if (!uap)
  2474. return -EINVAL;
  2475. return uart_suspend_port(&amba_reg, &uap->port);
  2476. }
  2477. static int pl011_resume(struct device *dev)
  2478. {
  2479. struct uart_amba_port *uap = dev_get_drvdata(dev);
  2480. if (!uap)
  2481. return -EINVAL;
  2482. return uart_resume_port(&amba_reg, &uap->port);
  2483. }
  2484. #endif
  2485. static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
  2486. #ifdef CONFIG_ACPI_SPCR_TABLE
  2487. static void qpdf2400_erratum44_workaround(struct device *dev,
  2488. struct uart_amba_port *uap)
  2489. {
  2490. if (!qdf2400_e44_present)
  2491. return;
  2492. dev_info(dev, "working around QDF2400 SoC erratum 44\n");
  2493. uap->vendor = &vendor_qdt_qdf2400_e44;
  2494. }
  2495. #else
  2496. static void qpdf2400_erratum44_workaround(struct device *dev,
  2497. struct uart_amba_port *uap)
  2498. { /* empty */ }
  2499. #endif
  2500. static int sbsa_uart_probe(struct platform_device *pdev)
  2501. {
  2502. struct uart_amba_port *uap;
  2503. struct resource *r;
  2504. int portnr, ret;
  2505. int baudrate;
  2506. /*
  2507. * Check the mandatory baud rate parameter in the DT node early
  2508. * so that we can easily exit with the error.
  2509. */
  2510. if (pdev->dev.of_node) {
  2511. struct device_node *np = pdev->dev.of_node;
  2512. ret = of_property_read_u32(np, "current-speed", &baudrate);
  2513. if (ret)
  2514. return ret;
  2515. } else {
  2516. baudrate = 115200;
  2517. }
  2518. portnr = pl011_find_free_port();
  2519. if (portnr < 0)
  2520. return portnr;
  2521. uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port),
  2522. GFP_KERNEL);
  2523. if (!uap)
  2524. return -ENOMEM;
  2525. ret = platform_get_irq(pdev, 0);
  2526. if (ret < 0)
  2527. return ret;
  2528. uap->port.irq = ret;
  2529. uap->vendor = &vendor_sbsa;
  2530. qpdf2400_erratum44_workaround(&pdev->dev, uap);
  2531. uap->reg_offset = uap->vendor->reg_offset;
  2532. uap->fifosize = 32;
  2533. uap->port.iotype = uap->vendor->access_32b ? UPIO_MEM32 : UPIO_MEM;
  2534. uap->port.ops = &sbsa_uart_pops;
  2535. uap->fixed_baud = baudrate;
  2536. snprintf(uap->type, sizeof(uap->type), "SBSA");
  2537. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2538. ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
  2539. if (ret)
  2540. return ret;
  2541. platform_set_drvdata(pdev, uap);
  2542. return pl011_register_port(uap);
  2543. }
  2544. static void sbsa_uart_remove(struct platform_device *pdev)
  2545. {
  2546. struct uart_amba_port *uap = platform_get_drvdata(pdev);
  2547. uart_remove_one_port(&amba_reg, &uap->port);
  2548. pl011_unregister_port(uap);
  2549. }
  2550. static const struct of_device_id sbsa_uart_of_match[] = {
  2551. { .compatible = "arm,sbsa-uart", },
  2552. {},
  2553. };
  2554. MODULE_DEVICE_TABLE(of, sbsa_uart_of_match);
  2555. static const struct acpi_device_id sbsa_uart_acpi_match[] = {
  2556. { "ARMH0011", 0 },
  2557. { "ARMHB000", 0 },
  2558. {},
  2559. };
  2560. MODULE_DEVICE_TABLE(acpi, sbsa_uart_acpi_match);
  2561. static struct platform_driver arm_sbsa_uart_platform_driver = {
  2562. .probe = sbsa_uart_probe,
  2563. .remove = sbsa_uart_remove,
  2564. .driver = {
  2565. .name = "sbsa-uart",
  2566. .pm = &pl011_dev_pm_ops,
  2567. .of_match_table = sbsa_uart_of_match,
  2568. .acpi_match_table = sbsa_uart_acpi_match,
  2569. .suppress_bind_attrs = IS_BUILTIN(CONFIG_SERIAL_AMBA_PL011),
  2570. },
  2571. };
  2572. static const struct amba_id pl011_ids[] = {
  2573. {
  2574. .id = 0x00041011,
  2575. .mask = 0x000fffff,
  2576. .data = &vendor_arm,
  2577. },
  2578. {
  2579. .id = 0x00380802,
  2580. .mask = 0x00ffffff,
  2581. .data = &vendor_st,
  2582. },
  2583. { 0, 0 },
  2584. };
  2585. MODULE_DEVICE_TABLE(amba, pl011_ids);
  2586. static struct amba_driver pl011_driver = {
  2587. .drv = {
  2588. .name = "uart-pl011",
  2589. .pm = &pl011_dev_pm_ops,
  2590. .suppress_bind_attrs = IS_BUILTIN(CONFIG_SERIAL_AMBA_PL011),
  2591. },
  2592. .id_table = pl011_ids,
  2593. .probe = pl011_probe,
  2594. .remove = pl011_remove,
  2595. };
  2596. static int __init pl011_init(void)
  2597. {
  2598. pr_info("Serial: AMBA PL011 UART driver\n");
  2599. if (platform_driver_register(&arm_sbsa_uart_platform_driver))
  2600. pr_warn("could not register SBSA UART platform driver\n");
  2601. return amba_driver_register(&pl011_driver);
  2602. }
  2603. static void __exit pl011_exit(void)
  2604. {
  2605. platform_driver_unregister(&arm_sbsa_uart_platform_driver);
  2606. amba_driver_unregister(&pl011_driver);
  2607. }
  2608. /*
  2609. * While this can be a module, if builtin it's most likely the console
  2610. * So let's leave module_exit but move module_init to an earlier place
  2611. */
  2612. arch_initcall(pl011_init);
  2613. module_exit(pl011_exit);
  2614. MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
  2615. MODULE_DESCRIPTION("ARM AMBA serial port driver");
  2616. MODULE_LICENSE("GPL");