amba-pl011.c 75 KB

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