amba-pl011.c 71 KB

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