amba-pl011.c 71 KB

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