amba-pl011.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844
  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 bool 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. if (pl011_tx_chars(uap, false)) {
  1160. uap->im |= UART011_TXIM;
  1161. pl011_write(uap->im, uap, REG_IMSC);
  1162. }
  1163. }
  1164. static void pl011_start_tx(struct uart_port *port)
  1165. {
  1166. struct uart_amba_port *uap =
  1167. container_of(port, struct uart_amba_port, port);
  1168. if (!pl011_dma_tx_start(uap))
  1169. pl011_start_tx_pio(uap);
  1170. }
  1171. static void pl011_stop_rx(struct uart_port *port)
  1172. {
  1173. struct uart_amba_port *uap =
  1174. container_of(port, struct uart_amba_port, port);
  1175. uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
  1176. UART011_PEIM|UART011_BEIM|UART011_OEIM);
  1177. pl011_write(uap->im, uap, REG_IMSC);
  1178. pl011_dma_rx_stop(uap);
  1179. }
  1180. static void pl011_enable_ms(struct uart_port *port)
  1181. {
  1182. struct uart_amba_port *uap =
  1183. container_of(port, struct uart_amba_port, port);
  1184. uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
  1185. pl011_write(uap->im, uap, REG_IMSC);
  1186. }
  1187. static void pl011_rx_chars(struct uart_amba_port *uap)
  1188. __releases(&uap->port.lock)
  1189. __acquires(&uap->port.lock)
  1190. {
  1191. pl011_fifo_to_tty(uap);
  1192. spin_unlock(&uap->port.lock);
  1193. tty_flip_buffer_push(&uap->port.state->port);
  1194. /*
  1195. * If we were temporarily out of DMA mode for a while,
  1196. * attempt to switch back to DMA mode again.
  1197. */
  1198. if (pl011_dma_rx_available(uap)) {
  1199. if (pl011_dma_rx_trigger_dma(uap)) {
  1200. dev_dbg(uap->port.dev, "could not trigger RX DMA job "
  1201. "fall back to interrupt mode again\n");
  1202. uap->im |= UART011_RXIM;
  1203. pl011_write(uap->im, uap, REG_IMSC);
  1204. } else {
  1205. #ifdef CONFIG_DMA_ENGINE
  1206. /* Start Rx DMA poll */
  1207. if (uap->dmarx.poll_rate) {
  1208. uap->dmarx.last_jiffies = jiffies;
  1209. uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
  1210. mod_timer(&uap->dmarx.timer,
  1211. jiffies +
  1212. msecs_to_jiffies(uap->dmarx.poll_rate));
  1213. }
  1214. #endif
  1215. }
  1216. }
  1217. spin_lock(&uap->port.lock);
  1218. }
  1219. static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c,
  1220. bool from_irq)
  1221. {
  1222. if (unlikely(!from_irq) &&
  1223. pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
  1224. return false; /* unable to transmit character */
  1225. pl011_write(c, uap, REG_DR);
  1226. uap->port.icount.tx++;
  1227. return true;
  1228. }
  1229. /* Returns true if tx interrupts have to be (kept) enabled */
  1230. static bool pl011_tx_chars(struct uart_amba_port *uap, bool from_irq)
  1231. {
  1232. struct circ_buf *xmit = &uap->port.state->xmit;
  1233. int count = uap->fifosize >> 1;
  1234. if (uap->port.x_char) {
  1235. if (!pl011_tx_char(uap, uap->port.x_char, from_irq))
  1236. return true;
  1237. uap->port.x_char = 0;
  1238. --count;
  1239. }
  1240. if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
  1241. pl011_stop_tx(&uap->port);
  1242. return false;
  1243. }
  1244. /* If we are using DMA mode, try to send some characters. */
  1245. if (pl011_dma_tx_irq(uap))
  1246. return true;
  1247. do {
  1248. if (likely(from_irq) && count-- == 0)
  1249. break;
  1250. if (!pl011_tx_char(uap, xmit->buf[xmit->tail], from_irq))
  1251. break;
  1252. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  1253. } while (!uart_circ_empty(xmit));
  1254. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  1255. uart_write_wakeup(&uap->port);
  1256. if (uart_circ_empty(xmit)) {
  1257. pl011_stop_tx(&uap->port);
  1258. return false;
  1259. }
  1260. return true;
  1261. }
  1262. static void pl011_modem_status(struct uart_amba_port *uap)
  1263. {
  1264. unsigned int status, delta;
  1265. status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
  1266. delta = status ^ uap->old_status;
  1267. uap->old_status = status;
  1268. if (!delta)
  1269. return;
  1270. if (delta & UART01x_FR_DCD)
  1271. uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
  1272. if (delta & uap->vendor->fr_dsr)
  1273. uap->port.icount.dsr++;
  1274. if (delta & uap->vendor->fr_cts)
  1275. uart_handle_cts_change(&uap->port,
  1276. status & uap->vendor->fr_cts);
  1277. wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
  1278. }
  1279. static void check_apply_cts_event_workaround(struct uart_amba_port *uap)
  1280. {
  1281. unsigned int dummy_read;
  1282. if (!uap->vendor->cts_event_workaround)
  1283. return;
  1284. /* workaround to make sure that all bits are unlocked.. */
  1285. pl011_write(0x00, uap, REG_ICR);
  1286. /*
  1287. * WA: introduce 26ns(1 uart clk) delay before W1C;
  1288. * single apb access will incur 2 pclk(133.12Mhz) delay,
  1289. * so add 2 dummy reads
  1290. */
  1291. dummy_read = pl011_read(uap, REG_ICR);
  1292. dummy_read = pl011_read(uap, REG_ICR);
  1293. }
  1294. static irqreturn_t pl011_int(int irq, void *dev_id)
  1295. {
  1296. struct uart_amba_port *uap = dev_id;
  1297. unsigned long flags;
  1298. unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
  1299. u16 imsc;
  1300. int handled = 0;
  1301. spin_lock_irqsave(&uap->port.lock, flags);
  1302. imsc = pl011_read(uap, REG_IMSC);
  1303. status = pl011_read(uap, REG_RIS) & imsc;
  1304. if (status) {
  1305. do {
  1306. check_apply_cts_event_workaround(uap);
  1307. pl011_write(status & ~(UART011_TXIS|UART011_RTIS|
  1308. UART011_RXIS),
  1309. uap, REG_ICR);
  1310. if (status & (UART011_RTIS|UART011_RXIS)) {
  1311. if (pl011_dma_rx_running(uap))
  1312. pl011_dma_rx_irq(uap);
  1313. else
  1314. pl011_rx_chars(uap);
  1315. }
  1316. if (status & (UART011_DSRMIS|UART011_DCDMIS|
  1317. UART011_CTSMIS|UART011_RIMIS))
  1318. pl011_modem_status(uap);
  1319. if (status & UART011_TXIS)
  1320. pl011_tx_chars(uap, true);
  1321. if (pass_counter-- == 0)
  1322. break;
  1323. status = pl011_read(uap, REG_RIS) & imsc;
  1324. } while (status != 0);
  1325. handled = 1;
  1326. }
  1327. spin_unlock_irqrestore(&uap->port.lock, flags);
  1328. return IRQ_RETVAL(handled);
  1329. }
  1330. static unsigned int pl011_tx_empty(struct uart_port *port)
  1331. {
  1332. struct uart_amba_port *uap =
  1333. container_of(port, struct uart_amba_port, port);
  1334. /* Allow feature register bits to be inverted to work around errata */
  1335. unsigned int status = pl011_read(uap, REG_FR) ^ uap->vendor->inv_fr;
  1336. return status & (uap->vendor->fr_busy | UART01x_FR_TXFF) ?
  1337. 0 : TIOCSER_TEMT;
  1338. }
  1339. static unsigned int pl011_get_mctrl(struct uart_port *port)
  1340. {
  1341. struct uart_amba_port *uap =
  1342. container_of(port, struct uart_amba_port, port);
  1343. unsigned int result = 0;
  1344. unsigned int status = pl011_read(uap, REG_FR);
  1345. #define TIOCMBIT(uartbit, tiocmbit) \
  1346. if (status & uartbit) \
  1347. result |= tiocmbit
  1348. TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
  1349. TIOCMBIT(uap->vendor->fr_dsr, TIOCM_DSR);
  1350. TIOCMBIT(uap->vendor->fr_cts, TIOCM_CTS);
  1351. TIOCMBIT(uap->vendor->fr_ri, TIOCM_RNG);
  1352. #undef TIOCMBIT
  1353. return result;
  1354. }
  1355. static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1356. {
  1357. struct uart_amba_port *uap =
  1358. container_of(port, struct uart_amba_port, port);
  1359. unsigned int cr;
  1360. cr = pl011_read(uap, REG_CR);
  1361. #define TIOCMBIT(tiocmbit, uartbit) \
  1362. if (mctrl & tiocmbit) \
  1363. cr |= uartbit; \
  1364. else \
  1365. cr &= ~uartbit
  1366. TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
  1367. TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
  1368. TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
  1369. TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
  1370. TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
  1371. if (uap->autorts) {
  1372. /* We need to disable auto-RTS if we want to turn RTS off */
  1373. TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
  1374. }
  1375. #undef TIOCMBIT
  1376. pl011_write(cr, uap, REG_CR);
  1377. }
  1378. static void pl011_break_ctl(struct uart_port *port, int break_state)
  1379. {
  1380. struct uart_amba_port *uap =
  1381. container_of(port, struct uart_amba_port, port);
  1382. unsigned long flags;
  1383. unsigned int lcr_h;
  1384. spin_lock_irqsave(&uap->port.lock, flags);
  1385. lcr_h = pl011_read(uap, REG_LCRH_TX);
  1386. if (break_state == -1)
  1387. lcr_h |= UART01x_LCRH_BRK;
  1388. else
  1389. lcr_h &= ~UART01x_LCRH_BRK;
  1390. pl011_write(lcr_h, uap, REG_LCRH_TX);
  1391. spin_unlock_irqrestore(&uap->port.lock, flags);
  1392. }
  1393. #ifdef CONFIG_CONSOLE_POLL
  1394. static void pl011_quiesce_irqs(struct uart_port *port)
  1395. {
  1396. struct uart_amba_port *uap =
  1397. container_of(port, struct uart_amba_port, port);
  1398. pl011_write(pl011_read(uap, REG_MIS), uap, REG_ICR);
  1399. /*
  1400. * There is no way to clear TXIM as this is "ready to transmit IRQ", so
  1401. * we simply mask it. start_tx() will unmask it.
  1402. *
  1403. * Note we can race with start_tx(), and if the race happens, the
  1404. * polling user might get another interrupt just after we clear it.
  1405. * But it should be OK and can happen even w/o the race, e.g.
  1406. * controller immediately got some new data and raised the IRQ.
  1407. *
  1408. * And whoever uses polling routines assumes that it manages the device
  1409. * (including tx queue), so we're also fine with start_tx()'s caller
  1410. * side.
  1411. */
  1412. pl011_write(pl011_read(uap, REG_IMSC) & ~UART011_TXIM, uap,
  1413. REG_IMSC);
  1414. }
  1415. static int pl011_get_poll_char(struct uart_port *port)
  1416. {
  1417. struct uart_amba_port *uap =
  1418. container_of(port, struct uart_amba_port, port);
  1419. unsigned int status;
  1420. /*
  1421. * The caller might need IRQs lowered, e.g. if used with KDB NMI
  1422. * debugger.
  1423. */
  1424. pl011_quiesce_irqs(port);
  1425. status = pl011_read(uap, REG_FR);
  1426. if (status & UART01x_FR_RXFE)
  1427. return NO_POLL_CHAR;
  1428. return pl011_read(uap, REG_DR);
  1429. }
  1430. static void pl011_put_poll_char(struct uart_port *port,
  1431. unsigned char ch)
  1432. {
  1433. struct uart_amba_port *uap =
  1434. container_of(port, struct uart_amba_port, port);
  1435. while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
  1436. cpu_relax();
  1437. pl011_write(ch, uap, REG_DR);
  1438. }
  1439. #endif /* CONFIG_CONSOLE_POLL */
  1440. static int pl011_hwinit(struct uart_port *port)
  1441. {
  1442. struct uart_amba_port *uap =
  1443. container_of(port, struct uart_amba_port, port);
  1444. int retval;
  1445. /* Optionaly enable pins to be muxed in and configured */
  1446. pinctrl_pm_select_default_state(port->dev);
  1447. /*
  1448. * Try to enable the clock producer.
  1449. */
  1450. retval = clk_prepare_enable(uap->clk);
  1451. if (retval)
  1452. return retval;
  1453. uap->port.uartclk = clk_get_rate(uap->clk);
  1454. /* Clear pending error and receive interrupts */
  1455. pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
  1456. UART011_FEIS | UART011_RTIS | UART011_RXIS,
  1457. uap, REG_ICR);
  1458. /*
  1459. * Save interrupts enable mask, and enable RX interrupts in case if
  1460. * the interrupt is used for NMI entry.
  1461. */
  1462. uap->im = pl011_read(uap, REG_IMSC);
  1463. pl011_write(UART011_RTIM | UART011_RXIM, uap, REG_IMSC);
  1464. if (dev_get_platdata(uap->port.dev)) {
  1465. struct amba_pl011_data *plat;
  1466. plat = dev_get_platdata(uap->port.dev);
  1467. if (plat->init)
  1468. plat->init();
  1469. }
  1470. return 0;
  1471. }
  1472. static bool pl011_split_lcrh(const struct uart_amba_port *uap)
  1473. {
  1474. return pl011_reg_to_offset(uap, REG_LCRH_RX) !=
  1475. pl011_reg_to_offset(uap, REG_LCRH_TX);
  1476. }
  1477. static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
  1478. {
  1479. pl011_write(lcr_h, uap, REG_LCRH_RX);
  1480. if (pl011_split_lcrh(uap)) {
  1481. int i;
  1482. /*
  1483. * Wait 10 PCLKs before writing LCRH_TX register,
  1484. * to get this delay write read only register 10 times
  1485. */
  1486. for (i = 0; i < 10; ++i)
  1487. pl011_write(0xff, uap, REG_MIS);
  1488. pl011_write(lcr_h, uap, REG_LCRH_TX);
  1489. }
  1490. }
  1491. static int pl011_allocate_irq(struct uart_amba_port *uap)
  1492. {
  1493. pl011_write(uap->im, uap, REG_IMSC);
  1494. return request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
  1495. }
  1496. /*
  1497. * Enable interrupts, only timeouts when using DMA
  1498. * if initial RX DMA job failed, start in interrupt mode
  1499. * as well.
  1500. */
  1501. static void pl011_enable_interrupts(struct uart_amba_port *uap)
  1502. {
  1503. spin_lock_irq(&uap->port.lock);
  1504. /* Clear out any spuriously appearing RX interrupts */
  1505. pl011_write(UART011_RTIS | UART011_RXIS, uap, REG_ICR);
  1506. uap->im = UART011_RTIM;
  1507. if (!pl011_dma_rx_running(uap))
  1508. uap->im |= UART011_RXIM;
  1509. pl011_write(uap->im, uap, REG_IMSC);
  1510. spin_unlock_irq(&uap->port.lock);
  1511. }
  1512. static int pl011_startup(struct uart_port *port)
  1513. {
  1514. struct uart_amba_port *uap =
  1515. container_of(port, struct uart_amba_port, port);
  1516. unsigned int cr;
  1517. int retval;
  1518. retval = pl011_hwinit(port);
  1519. if (retval)
  1520. goto clk_dis;
  1521. retval = pl011_allocate_irq(uap);
  1522. if (retval)
  1523. goto clk_dis;
  1524. pl011_write(uap->vendor->ifls, uap, REG_IFLS);
  1525. spin_lock_irq(&uap->port.lock);
  1526. /* restore RTS and DTR */
  1527. cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
  1528. cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
  1529. pl011_write(cr, uap, REG_CR);
  1530. spin_unlock_irq(&uap->port.lock);
  1531. /*
  1532. * initialise the old status of the modem signals
  1533. */
  1534. uap->old_status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
  1535. /* Startup DMA */
  1536. pl011_dma_startup(uap);
  1537. pl011_enable_interrupts(uap);
  1538. return 0;
  1539. clk_dis:
  1540. clk_disable_unprepare(uap->clk);
  1541. return retval;
  1542. }
  1543. static int sbsa_uart_startup(struct uart_port *port)
  1544. {
  1545. struct uart_amba_port *uap =
  1546. container_of(port, struct uart_amba_port, port);
  1547. int retval;
  1548. retval = pl011_hwinit(port);
  1549. if (retval)
  1550. return retval;
  1551. retval = pl011_allocate_irq(uap);
  1552. if (retval)
  1553. return retval;
  1554. /* The SBSA UART does not support any modem status lines. */
  1555. uap->old_status = 0;
  1556. pl011_enable_interrupts(uap);
  1557. return 0;
  1558. }
  1559. static void pl011_shutdown_channel(struct uart_amba_port *uap,
  1560. unsigned int lcrh)
  1561. {
  1562. unsigned long val;
  1563. val = pl011_read(uap, lcrh);
  1564. val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
  1565. pl011_write(val, uap, lcrh);
  1566. }
  1567. /*
  1568. * disable the port. It should not disable RTS and DTR.
  1569. * Also RTS and DTR state should be preserved to restore
  1570. * it during startup().
  1571. */
  1572. static void pl011_disable_uart(struct uart_amba_port *uap)
  1573. {
  1574. unsigned int cr;
  1575. uap->autorts = false;
  1576. spin_lock_irq(&uap->port.lock);
  1577. cr = pl011_read(uap, REG_CR);
  1578. uap->old_cr = cr;
  1579. cr &= UART011_CR_RTS | UART011_CR_DTR;
  1580. cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
  1581. pl011_write(cr, uap, REG_CR);
  1582. spin_unlock_irq(&uap->port.lock);
  1583. /*
  1584. * disable break condition and fifos
  1585. */
  1586. pl011_shutdown_channel(uap, REG_LCRH_RX);
  1587. if (pl011_split_lcrh(uap))
  1588. pl011_shutdown_channel(uap, REG_LCRH_TX);
  1589. }
  1590. static void pl011_disable_interrupts(struct uart_amba_port *uap)
  1591. {
  1592. spin_lock_irq(&uap->port.lock);
  1593. /* mask all interrupts and clear all pending ones */
  1594. uap->im = 0;
  1595. pl011_write(uap->im, uap, REG_IMSC);
  1596. pl011_write(0xffff, uap, REG_ICR);
  1597. spin_unlock_irq(&uap->port.lock);
  1598. }
  1599. static void pl011_shutdown(struct uart_port *port)
  1600. {
  1601. struct uart_amba_port *uap =
  1602. container_of(port, struct uart_amba_port, port);
  1603. pl011_disable_interrupts(uap);
  1604. pl011_dma_shutdown(uap);
  1605. free_irq(uap->port.irq, uap);
  1606. pl011_disable_uart(uap);
  1607. /*
  1608. * Shut down the clock producer
  1609. */
  1610. clk_disable_unprepare(uap->clk);
  1611. /* Optionally let pins go into sleep states */
  1612. pinctrl_pm_select_sleep_state(port->dev);
  1613. if (dev_get_platdata(uap->port.dev)) {
  1614. struct amba_pl011_data *plat;
  1615. plat = dev_get_platdata(uap->port.dev);
  1616. if (plat->exit)
  1617. plat->exit();
  1618. }
  1619. if (uap->port.ops->flush_buffer)
  1620. uap->port.ops->flush_buffer(port);
  1621. }
  1622. static void sbsa_uart_shutdown(struct uart_port *port)
  1623. {
  1624. struct uart_amba_port *uap =
  1625. container_of(port, struct uart_amba_port, port);
  1626. pl011_disable_interrupts(uap);
  1627. free_irq(uap->port.irq, uap);
  1628. if (uap->port.ops->flush_buffer)
  1629. uap->port.ops->flush_buffer(port);
  1630. }
  1631. static void
  1632. pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios)
  1633. {
  1634. port->read_status_mask = UART011_DR_OE | 255;
  1635. if (termios->c_iflag & INPCK)
  1636. port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
  1637. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  1638. port->read_status_mask |= UART011_DR_BE;
  1639. /*
  1640. * Characters to ignore
  1641. */
  1642. port->ignore_status_mask = 0;
  1643. if (termios->c_iflag & IGNPAR)
  1644. port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
  1645. if (termios->c_iflag & IGNBRK) {
  1646. port->ignore_status_mask |= UART011_DR_BE;
  1647. /*
  1648. * If we're ignoring parity and break indicators,
  1649. * ignore overruns too (for real raw support).
  1650. */
  1651. if (termios->c_iflag & IGNPAR)
  1652. port->ignore_status_mask |= UART011_DR_OE;
  1653. }
  1654. /*
  1655. * Ignore all characters if CREAD is not set.
  1656. */
  1657. if ((termios->c_cflag & CREAD) == 0)
  1658. port->ignore_status_mask |= UART_DUMMY_DR_RX;
  1659. }
  1660. static void
  1661. pl011_set_termios(struct uart_port *port, struct ktermios *termios,
  1662. struct ktermios *old)
  1663. {
  1664. struct uart_amba_port *uap =
  1665. container_of(port, struct uart_amba_port, port);
  1666. unsigned int lcr_h, old_cr;
  1667. unsigned long flags;
  1668. unsigned int baud, quot, clkdiv;
  1669. if (uap->vendor->oversampling)
  1670. clkdiv = 8;
  1671. else
  1672. clkdiv = 16;
  1673. /*
  1674. * Ask the core to calculate the divisor for us.
  1675. */
  1676. baud = uart_get_baud_rate(port, termios, old, 0,
  1677. port->uartclk / clkdiv);
  1678. #ifdef CONFIG_DMA_ENGINE
  1679. /*
  1680. * Adjust RX DMA polling rate with baud rate if not specified.
  1681. */
  1682. if (uap->dmarx.auto_poll_rate)
  1683. uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
  1684. #endif
  1685. if (baud > port->uartclk/16)
  1686. quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
  1687. else
  1688. quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
  1689. switch (termios->c_cflag & CSIZE) {
  1690. case CS5:
  1691. lcr_h = UART01x_LCRH_WLEN_5;
  1692. break;
  1693. case CS6:
  1694. lcr_h = UART01x_LCRH_WLEN_6;
  1695. break;
  1696. case CS7:
  1697. lcr_h = UART01x_LCRH_WLEN_7;
  1698. break;
  1699. default: // CS8
  1700. lcr_h = UART01x_LCRH_WLEN_8;
  1701. break;
  1702. }
  1703. if (termios->c_cflag & CSTOPB)
  1704. lcr_h |= UART01x_LCRH_STP2;
  1705. if (termios->c_cflag & PARENB) {
  1706. lcr_h |= UART01x_LCRH_PEN;
  1707. if (!(termios->c_cflag & PARODD))
  1708. lcr_h |= UART01x_LCRH_EPS;
  1709. if (termios->c_cflag & CMSPAR)
  1710. lcr_h |= UART011_LCRH_SPS;
  1711. }
  1712. if (uap->fifosize > 1)
  1713. lcr_h |= UART01x_LCRH_FEN;
  1714. spin_lock_irqsave(&port->lock, flags);
  1715. /*
  1716. * Update the per-port timeout.
  1717. */
  1718. uart_update_timeout(port, termios->c_cflag, baud);
  1719. pl011_setup_status_masks(port, termios);
  1720. if (UART_ENABLE_MS(port, termios->c_cflag))
  1721. pl011_enable_ms(port);
  1722. /* first, disable everything */
  1723. old_cr = pl011_read(uap, REG_CR);
  1724. pl011_write(0, uap, REG_CR);
  1725. if (termios->c_cflag & CRTSCTS) {
  1726. if (old_cr & UART011_CR_RTS)
  1727. old_cr |= UART011_CR_RTSEN;
  1728. old_cr |= UART011_CR_CTSEN;
  1729. uap->autorts = true;
  1730. } else {
  1731. old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
  1732. uap->autorts = false;
  1733. }
  1734. if (uap->vendor->oversampling) {
  1735. if (baud > port->uartclk / 16)
  1736. old_cr |= ST_UART011_CR_OVSFACT;
  1737. else
  1738. old_cr &= ~ST_UART011_CR_OVSFACT;
  1739. }
  1740. /*
  1741. * Workaround for the ST Micro oversampling variants to
  1742. * increase the bitrate slightly, by lowering the divisor,
  1743. * to avoid delayed sampling of start bit at high speeds,
  1744. * else we see data corruption.
  1745. */
  1746. if (uap->vendor->oversampling) {
  1747. if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
  1748. quot -= 1;
  1749. else if ((baud > 3250000) && (quot > 2))
  1750. quot -= 2;
  1751. }
  1752. /* Set baud rate */
  1753. pl011_write(quot & 0x3f, uap, REG_FBRD);
  1754. pl011_write(quot >> 6, uap, REG_IBRD);
  1755. /*
  1756. * ----------v----------v----------v----------v-----
  1757. * NOTE: REG_LCRH_TX and REG_LCRH_RX MUST BE WRITTEN AFTER
  1758. * REG_FBRD & REG_IBRD.
  1759. * ----------^----------^----------^----------^-----
  1760. */
  1761. pl011_write_lcr_h(uap, lcr_h);
  1762. pl011_write(old_cr, uap, REG_CR);
  1763. spin_unlock_irqrestore(&port->lock, flags);
  1764. }
  1765. static void
  1766. sbsa_uart_set_termios(struct uart_port *port, struct ktermios *termios,
  1767. struct ktermios *old)
  1768. {
  1769. struct uart_amba_port *uap =
  1770. container_of(port, struct uart_amba_port, port);
  1771. unsigned long flags;
  1772. tty_termios_encode_baud_rate(termios, uap->fixed_baud, uap->fixed_baud);
  1773. /* The SBSA UART only supports 8n1 without hardware flow control. */
  1774. termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
  1775. termios->c_cflag &= ~(CMSPAR | CRTSCTS);
  1776. termios->c_cflag |= CS8 | CLOCAL;
  1777. spin_lock_irqsave(&port->lock, flags);
  1778. uart_update_timeout(port, CS8, uap->fixed_baud);
  1779. pl011_setup_status_masks(port, termios);
  1780. spin_unlock_irqrestore(&port->lock, flags);
  1781. }
  1782. static const char *pl011_type(struct uart_port *port)
  1783. {
  1784. struct uart_amba_port *uap =
  1785. container_of(port, struct uart_amba_port, port);
  1786. return uap->port.type == PORT_AMBA ? uap->type : NULL;
  1787. }
  1788. /*
  1789. * Release the memory region(s) being used by 'port'
  1790. */
  1791. static void pl011_release_port(struct uart_port *port)
  1792. {
  1793. release_mem_region(port->mapbase, SZ_4K);
  1794. }
  1795. /*
  1796. * Request the memory region(s) being used by 'port'
  1797. */
  1798. static int pl011_request_port(struct uart_port *port)
  1799. {
  1800. return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
  1801. != NULL ? 0 : -EBUSY;
  1802. }
  1803. /*
  1804. * Configure/autoconfigure the port.
  1805. */
  1806. static void pl011_config_port(struct uart_port *port, int flags)
  1807. {
  1808. if (flags & UART_CONFIG_TYPE) {
  1809. port->type = PORT_AMBA;
  1810. pl011_request_port(port);
  1811. }
  1812. }
  1813. /*
  1814. * verify the new serial_struct (for TIOCSSERIAL).
  1815. */
  1816. static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
  1817. {
  1818. int ret = 0;
  1819. if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
  1820. ret = -EINVAL;
  1821. if (ser->irq < 0 || ser->irq >= nr_irqs)
  1822. ret = -EINVAL;
  1823. if (ser->baud_base < 9600)
  1824. ret = -EINVAL;
  1825. return ret;
  1826. }
  1827. static const struct uart_ops amba_pl011_pops = {
  1828. .tx_empty = pl011_tx_empty,
  1829. .set_mctrl = pl011_set_mctrl,
  1830. .get_mctrl = pl011_get_mctrl,
  1831. .stop_tx = pl011_stop_tx,
  1832. .start_tx = pl011_start_tx,
  1833. .stop_rx = pl011_stop_rx,
  1834. .enable_ms = pl011_enable_ms,
  1835. .break_ctl = pl011_break_ctl,
  1836. .startup = pl011_startup,
  1837. .shutdown = pl011_shutdown,
  1838. .flush_buffer = pl011_dma_flush_buffer,
  1839. .set_termios = pl011_set_termios,
  1840. .type = pl011_type,
  1841. .release_port = pl011_release_port,
  1842. .request_port = pl011_request_port,
  1843. .config_port = pl011_config_port,
  1844. .verify_port = pl011_verify_port,
  1845. #ifdef CONFIG_CONSOLE_POLL
  1846. .poll_init = pl011_hwinit,
  1847. .poll_get_char = pl011_get_poll_char,
  1848. .poll_put_char = pl011_put_poll_char,
  1849. #endif
  1850. };
  1851. static void sbsa_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1852. {
  1853. }
  1854. static unsigned int sbsa_uart_get_mctrl(struct uart_port *port)
  1855. {
  1856. return 0;
  1857. }
  1858. static const struct uart_ops sbsa_uart_pops = {
  1859. .tx_empty = pl011_tx_empty,
  1860. .set_mctrl = sbsa_uart_set_mctrl,
  1861. .get_mctrl = sbsa_uart_get_mctrl,
  1862. .stop_tx = pl011_stop_tx,
  1863. .start_tx = pl011_start_tx,
  1864. .stop_rx = pl011_stop_rx,
  1865. .startup = sbsa_uart_startup,
  1866. .shutdown = sbsa_uart_shutdown,
  1867. .set_termios = sbsa_uart_set_termios,
  1868. .type = pl011_type,
  1869. .release_port = pl011_release_port,
  1870. .request_port = pl011_request_port,
  1871. .config_port = pl011_config_port,
  1872. .verify_port = pl011_verify_port,
  1873. #ifdef CONFIG_CONSOLE_POLL
  1874. .poll_init = pl011_hwinit,
  1875. .poll_get_char = pl011_get_poll_char,
  1876. .poll_put_char = pl011_put_poll_char,
  1877. #endif
  1878. };
  1879. static struct uart_amba_port *amba_ports[UART_NR];
  1880. #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
  1881. static void pl011_console_putchar(struct uart_port *port, int ch)
  1882. {
  1883. struct uart_amba_port *uap =
  1884. container_of(port, struct uart_amba_port, port);
  1885. while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
  1886. cpu_relax();
  1887. pl011_write(ch, uap, REG_DR);
  1888. }
  1889. static void
  1890. pl011_console_write(struct console *co, const char *s, unsigned int count)
  1891. {
  1892. struct uart_amba_port *uap = amba_ports[co->index];
  1893. unsigned int old_cr = 0, new_cr;
  1894. unsigned long flags;
  1895. int locked = 1;
  1896. clk_enable(uap->clk);
  1897. local_irq_save(flags);
  1898. if (uap->port.sysrq)
  1899. locked = 0;
  1900. else if (oops_in_progress)
  1901. locked = spin_trylock(&uap->port.lock);
  1902. else
  1903. spin_lock(&uap->port.lock);
  1904. /*
  1905. * First save the CR then disable the interrupts
  1906. */
  1907. if (!uap->vendor->always_enabled) {
  1908. old_cr = pl011_read(uap, REG_CR);
  1909. new_cr = old_cr & ~UART011_CR_CTSEN;
  1910. new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
  1911. pl011_write(new_cr, uap, REG_CR);
  1912. }
  1913. uart_console_write(&uap->port, s, count, pl011_console_putchar);
  1914. /*
  1915. * Finally, wait for transmitter to become empty and restore the
  1916. * TCR. Allow feature register bits to be inverted to work around
  1917. * errata.
  1918. */
  1919. while ((pl011_read(uap, REG_FR) ^ uap->vendor->inv_fr)
  1920. & uap->vendor->fr_busy)
  1921. cpu_relax();
  1922. if (!uap->vendor->always_enabled)
  1923. pl011_write(old_cr, uap, REG_CR);
  1924. if (locked)
  1925. spin_unlock(&uap->port.lock);
  1926. local_irq_restore(flags);
  1927. clk_disable(uap->clk);
  1928. }
  1929. static void __init
  1930. pl011_console_get_options(struct uart_amba_port *uap, int *baud,
  1931. int *parity, int *bits)
  1932. {
  1933. if (pl011_read(uap, REG_CR) & UART01x_CR_UARTEN) {
  1934. unsigned int lcr_h, ibrd, fbrd;
  1935. lcr_h = pl011_read(uap, REG_LCRH_TX);
  1936. *parity = 'n';
  1937. if (lcr_h & UART01x_LCRH_PEN) {
  1938. if (lcr_h & UART01x_LCRH_EPS)
  1939. *parity = 'e';
  1940. else
  1941. *parity = 'o';
  1942. }
  1943. if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
  1944. *bits = 7;
  1945. else
  1946. *bits = 8;
  1947. ibrd = pl011_read(uap, REG_IBRD);
  1948. fbrd = pl011_read(uap, REG_FBRD);
  1949. *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
  1950. if (uap->vendor->oversampling) {
  1951. if (pl011_read(uap, REG_CR)
  1952. & ST_UART011_CR_OVSFACT)
  1953. *baud *= 2;
  1954. }
  1955. }
  1956. }
  1957. static int __init pl011_console_setup(struct console *co, char *options)
  1958. {
  1959. struct uart_amba_port *uap;
  1960. int baud = 38400;
  1961. int bits = 8;
  1962. int parity = 'n';
  1963. int flow = 'n';
  1964. int ret;
  1965. /*
  1966. * Check whether an invalid uart number has been specified, and
  1967. * if so, search for the first available port that does have
  1968. * console support.
  1969. */
  1970. if (co->index >= UART_NR)
  1971. co->index = 0;
  1972. uap = amba_ports[co->index];
  1973. if (!uap)
  1974. return -ENODEV;
  1975. /* Allow pins to be muxed in and configured */
  1976. pinctrl_pm_select_default_state(uap->port.dev);
  1977. ret = clk_prepare(uap->clk);
  1978. if (ret)
  1979. return ret;
  1980. if (dev_get_platdata(uap->port.dev)) {
  1981. struct amba_pl011_data *plat;
  1982. plat = dev_get_platdata(uap->port.dev);
  1983. if (plat->init)
  1984. plat->init();
  1985. }
  1986. uap->port.uartclk = clk_get_rate(uap->clk);
  1987. if (uap->vendor->fixed_options) {
  1988. baud = uap->fixed_baud;
  1989. } else {
  1990. if (options)
  1991. uart_parse_options(options,
  1992. &baud, &parity, &bits, &flow);
  1993. else
  1994. pl011_console_get_options(uap, &baud, &parity, &bits);
  1995. }
  1996. return uart_set_options(&uap->port, co, baud, parity, bits, flow);
  1997. }
  1998. /**
  1999. * pl011_console_match - non-standard console matching
  2000. * @co: registering console
  2001. * @name: name from console command line
  2002. * @idx: index from console command line
  2003. * @options: ptr to option string from console command line
  2004. *
  2005. * Only attempts to match console command lines of the form:
  2006. * console=pl011,mmio|mmio32,<addr>[,<options>]
  2007. * console=pl011,0x<addr>[,<options>]
  2008. * This form is used to register an initial earlycon boot console and
  2009. * replace it with the amba_console at pl011 driver init.
  2010. *
  2011. * Performs console setup for a match (as required by interface)
  2012. * If no <options> are specified, then assume the h/w is already setup.
  2013. *
  2014. * Returns 0 if console matches; otherwise non-zero to use default matching
  2015. */
  2016. static int __init pl011_console_match(struct console *co, char *name, int idx,
  2017. char *options)
  2018. {
  2019. unsigned char iotype;
  2020. resource_size_t addr;
  2021. int i;
  2022. if (strcmp(name, "qdf2400_e44") == 0) {
  2023. pr_info_once("UART: Working around QDF2400 SoC erratum 44");
  2024. qdf2400_e44_present = true;
  2025. } else if (strcmp(name, "pl011") != 0) {
  2026. return -ENODEV;
  2027. }
  2028. if (uart_parse_earlycon(options, &iotype, &addr, &options))
  2029. return -ENODEV;
  2030. if (iotype != UPIO_MEM && iotype != UPIO_MEM32)
  2031. return -ENODEV;
  2032. /* try to match the port specified on the command line */
  2033. for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
  2034. struct uart_port *port;
  2035. if (!amba_ports[i])
  2036. continue;
  2037. port = &amba_ports[i]->port;
  2038. if (port->mapbase != addr)
  2039. continue;
  2040. co->index = i;
  2041. port->cons = co;
  2042. return pl011_console_setup(co, options);
  2043. }
  2044. return -ENODEV;
  2045. }
  2046. static struct uart_driver amba_reg;
  2047. static struct console amba_console = {
  2048. .name = "ttyAMA",
  2049. .write = pl011_console_write,
  2050. .device = uart_console_device,
  2051. .setup = pl011_console_setup,
  2052. .match = pl011_console_match,
  2053. .flags = CON_PRINTBUFFER | CON_ANYTIME,
  2054. .index = -1,
  2055. .data = &amba_reg,
  2056. };
  2057. #define AMBA_CONSOLE (&amba_console)
  2058. static void qdf2400_e44_putc(struct uart_port *port, int c)
  2059. {
  2060. while (readl(port->membase + UART01x_FR) & UART01x_FR_TXFF)
  2061. cpu_relax();
  2062. writel(c, port->membase + UART01x_DR);
  2063. while (!(readl(port->membase + UART01x_FR) & UART011_FR_TXFE))
  2064. cpu_relax();
  2065. }
  2066. static void qdf2400_e44_early_write(struct console *con, const char *s, unsigned n)
  2067. {
  2068. struct earlycon_device *dev = con->data;
  2069. uart_console_write(&dev->port, s, n, qdf2400_e44_putc);
  2070. }
  2071. static void pl011_putc(struct uart_port *port, int c)
  2072. {
  2073. while (readl(port->membase + UART01x_FR) & UART01x_FR_TXFF)
  2074. cpu_relax();
  2075. if (port->iotype == UPIO_MEM32)
  2076. writel(c, port->membase + UART01x_DR);
  2077. else
  2078. writeb(c, port->membase + UART01x_DR);
  2079. while (readl(port->membase + UART01x_FR) & UART01x_FR_BUSY)
  2080. cpu_relax();
  2081. }
  2082. static void pl011_early_write(struct console *con, const char *s, unsigned n)
  2083. {
  2084. struct earlycon_device *dev = con->data;
  2085. uart_console_write(&dev->port, s, n, pl011_putc);
  2086. }
  2087. /*
  2088. * On non-ACPI systems, earlycon is enabled by specifying
  2089. * "earlycon=pl011,<address>" on the kernel command line.
  2090. *
  2091. * On ACPI ARM64 systems, an "early" console is enabled via the SPCR table,
  2092. * by specifying only "earlycon" on the command line. Because it requires
  2093. * SPCR, the console starts after ACPI is parsed, which is later than a
  2094. * traditional early console.
  2095. *
  2096. * To get the traditional early console that starts before ACPI is parsed,
  2097. * specify the full "earlycon=pl011,<address>" option.
  2098. */
  2099. static int __init pl011_early_console_setup(struct earlycon_device *device,
  2100. const char *opt)
  2101. {
  2102. if (!device->port.membase)
  2103. return -ENODEV;
  2104. device->con->write = pl011_early_write;
  2105. return 0;
  2106. }
  2107. OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup);
  2108. OF_EARLYCON_DECLARE(pl011, "arm,sbsa-uart", pl011_early_console_setup);
  2109. /*
  2110. * On Qualcomm Datacenter Technologies QDF2400 SOCs affected by
  2111. * Erratum 44, traditional earlycon can be enabled by specifying
  2112. * "earlycon=qdf2400_e44,<address>". Any options are ignored.
  2113. *
  2114. * Alternatively, you can just specify "earlycon", and the early console
  2115. * will be enabled with the information from the SPCR table. In this
  2116. * case, the SPCR code will detect the need for the E44 work-around,
  2117. * and set the console name to "qdf2400_e44".
  2118. */
  2119. static int __init
  2120. qdf2400_e44_early_console_setup(struct earlycon_device *device,
  2121. const char *opt)
  2122. {
  2123. if (!device->port.membase)
  2124. return -ENODEV;
  2125. device->con->write = qdf2400_e44_early_write;
  2126. return 0;
  2127. }
  2128. EARLYCON_DECLARE(qdf2400_e44, qdf2400_e44_early_console_setup);
  2129. #else
  2130. #define AMBA_CONSOLE NULL
  2131. #endif
  2132. static struct uart_driver amba_reg = {
  2133. .owner = THIS_MODULE,
  2134. .driver_name = "ttyAMA",
  2135. .dev_name = "ttyAMA",
  2136. .major = SERIAL_AMBA_MAJOR,
  2137. .minor = SERIAL_AMBA_MINOR,
  2138. .nr = UART_NR,
  2139. .cons = AMBA_CONSOLE,
  2140. };
  2141. static int pl011_probe_dt_alias(int index, struct device *dev)
  2142. {
  2143. struct device_node *np;
  2144. static bool seen_dev_with_alias = false;
  2145. static bool seen_dev_without_alias = false;
  2146. int ret = index;
  2147. if (!IS_ENABLED(CONFIG_OF))
  2148. return ret;
  2149. np = dev->of_node;
  2150. if (!np)
  2151. return ret;
  2152. ret = of_alias_get_id(np, "serial");
  2153. if (ret < 0) {
  2154. seen_dev_without_alias = true;
  2155. ret = index;
  2156. } else {
  2157. seen_dev_with_alias = true;
  2158. if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
  2159. dev_warn(dev, "requested serial port %d not available.\n", ret);
  2160. ret = index;
  2161. }
  2162. }
  2163. if (seen_dev_with_alias && seen_dev_without_alias)
  2164. dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
  2165. return ret;
  2166. }
  2167. /* unregisters the driver also if no more ports are left */
  2168. static void pl011_unregister_port(struct uart_amba_port *uap)
  2169. {
  2170. int i;
  2171. bool busy = false;
  2172. for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
  2173. if (amba_ports[i] == uap)
  2174. amba_ports[i] = NULL;
  2175. else if (amba_ports[i])
  2176. busy = true;
  2177. }
  2178. pl011_dma_remove(uap);
  2179. if (!busy)
  2180. uart_unregister_driver(&amba_reg);
  2181. }
  2182. static int pl011_find_free_port(void)
  2183. {
  2184. int i;
  2185. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  2186. if (amba_ports[i] == NULL)
  2187. return i;
  2188. return -EBUSY;
  2189. }
  2190. static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap,
  2191. struct resource *mmiobase, int index)
  2192. {
  2193. void __iomem *base;
  2194. base = devm_ioremap_resource(dev, mmiobase);
  2195. if (IS_ERR(base))
  2196. return PTR_ERR(base);
  2197. index = pl011_probe_dt_alias(index, dev);
  2198. uap->old_cr = 0;
  2199. uap->port.dev = dev;
  2200. uap->port.mapbase = mmiobase->start;
  2201. uap->port.membase = base;
  2202. uap->port.fifosize = uap->fifosize;
  2203. uap->port.flags = UPF_BOOT_AUTOCONF;
  2204. uap->port.line = index;
  2205. amba_ports[index] = uap;
  2206. return 0;
  2207. }
  2208. static int pl011_register_port(struct uart_amba_port *uap)
  2209. {
  2210. int ret;
  2211. /* Ensure interrupts from this UART are masked and cleared */
  2212. pl011_write(0, uap, REG_IMSC);
  2213. pl011_write(0xffff, uap, REG_ICR);
  2214. if (!amba_reg.state) {
  2215. ret = uart_register_driver(&amba_reg);
  2216. if (ret < 0) {
  2217. dev_err(uap->port.dev,
  2218. "Failed to register AMBA-PL011 driver\n");
  2219. return ret;
  2220. }
  2221. }
  2222. ret = uart_add_one_port(&amba_reg, &uap->port);
  2223. if (ret)
  2224. pl011_unregister_port(uap);
  2225. return ret;
  2226. }
  2227. static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
  2228. {
  2229. struct uart_amba_port *uap;
  2230. struct vendor_data *vendor = id->data;
  2231. int portnr, ret;
  2232. portnr = pl011_find_free_port();
  2233. if (portnr < 0)
  2234. return portnr;
  2235. uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
  2236. GFP_KERNEL);
  2237. if (!uap)
  2238. return -ENOMEM;
  2239. uap->clk = devm_clk_get(&dev->dev, NULL);
  2240. if (IS_ERR(uap->clk))
  2241. return PTR_ERR(uap->clk);
  2242. uap->reg_offset = vendor->reg_offset;
  2243. uap->vendor = vendor;
  2244. uap->fifosize = vendor->get_fifosize(dev);
  2245. uap->port.iotype = vendor->access_32b ? UPIO_MEM32 : UPIO_MEM;
  2246. uap->port.irq = dev->irq[0];
  2247. uap->port.ops = &amba_pl011_pops;
  2248. snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
  2249. ret = pl011_setup_port(&dev->dev, uap, &dev->res, portnr);
  2250. if (ret)
  2251. return ret;
  2252. amba_set_drvdata(dev, uap);
  2253. return pl011_register_port(uap);
  2254. }
  2255. static int pl011_remove(struct amba_device *dev)
  2256. {
  2257. struct uart_amba_port *uap = amba_get_drvdata(dev);
  2258. uart_remove_one_port(&amba_reg, &uap->port);
  2259. pl011_unregister_port(uap);
  2260. return 0;
  2261. }
  2262. #ifdef CONFIG_PM_SLEEP
  2263. static int pl011_suspend(struct device *dev)
  2264. {
  2265. struct uart_amba_port *uap = dev_get_drvdata(dev);
  2266. if (!uap)
  2267. return -EINVAL;
  2268. return uart_suspend_port(&amba_reg, &uap->port);
  2269. }
  2270. static int pl011_resume(struct device *dev)
  2271. {
  2272. struct uart_amba_port *uap = dev_get_drvdata(dev);
  2273. if (!uap)
  2274. return -EINVAL;
  2275. return uart_resume_port(&amba_reg, &uap->port);
  2276. }
  2277. #endif
  2278. static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
  2279. static int sbsa_uart_probe(struct platform_device *pdev)
  2280. {
  2281. struct uart_amba_port *uap;
  2282. struct resource *r;
  2283. int portnr, ret;
  2284. int baudrate;
  2285. /*
  2286. * Check the mandatory baud rate parameter in the DT node early
  2287. * so that we can easily exit with the error.
  2288. */
  2289. if (pdev->dev.of_node) {
  2290. struct device_node *np = pdev->dev.of_node;
  2291. ret = of_property_read_u32(np, "current-speed", &baudrate);
  2292. if (ret)
  2293. return ret;
  2294. } else {
  2295. baudrate = 115200;
  2296. }
  2297. portnr = pl011_find_free_port();
  2298. if (portnr < 0)
  2299. return portnr;
  2300. uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port),
  2301. GFP_KERNEL);
  2302. if (!uap)
  2303. return -ENOMEM;
  2304. ret = platform_get_irq(pdev, 0);
  2305. if (ret < 0) {
  2306. if (ret != -EPROBE_DEFER)
  2307. dev_err(&pdev->dev, "cannot obtain irq\n");
  2308. return ret;
  2309. }
  2310. uap->port.irq = ret;
  2311. uap->reg_offset = vendor_sbsa.reg_offset;
  2312. uap->vendor = qdf2400_e44_present ?
  2313. &vendor_qdt_qdf2400_e44 : &vendor_sbsa;
  2314. uap->fifosize = 32;
  2315. uap->port.iotype = vendor_sbsa.access_32b ? UPIO_MEM32 : UPIO_MEM;
  2316. uap->port.ops = &sbsa_uart_pops;
  2317. uap->fixed_baud = baudrate;
  2318. snprintf(uap->type, sizeof(uap->type), "SBSA");
  2319. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2320. ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
  2321. if (ret)
  2322. return ret;
  2323. platform_set_drvdata(pdev, uap);
  2324. return pl011_register_port(uap);
  2325. }
  2326. static int sbsa_uart_remove(struct platform_device *pdev)
  2327. {
  2328. struct uart_amba_port *uap = platform_get_drvdata(pdev);
  2329. uart_remove_one_port(&amba_reg, &uap->port);
  2330. pl011_unregister_port(uap);
  2331. return 0;
  2332. }
  2333. static const struct of_device_id sbsa_uart_of_match[] = {
  2334. { .compatible = "arm,sbsa-uart", },
  2335. {},
  2336. };
  2337. MODULE_DEVICE_TABLE(of, sbsa_uart_of_match);
  2338. static const struct acpi_device_id sbsa_uart_acpi_match[] = {
  2339. { "ARMH0011", 0 },
  2340. {},
  2341. };
  2342. MODULE_DEVICE_TABLE(acpi, sbsa_uart_acpi_match);
  2343. static struct platform_driver arm_sbsa_uart_platform_driver = {
  2344. .probe = sbsa_uart_probe,
  2345. .remove = sbsa_uart_remove,
  2346. .driver = {
  2347. .name = "sbsa-uart",
  2348. .of_match_table = of_match_ptr(sbsa_uart_of_match),
  2349. .acpi_match_table = ACPI_PTR(sbsa_uart_acpi_match),
  2350. },
  2351. };
  2352. static struct amba_id pl011_ids[] = {
  2353. {
  2354. .id = 0x00041011,
  2355. .mask = 0x000fffff,
  2356. .data = &vendor_arm,
  2357. },
  2358. {
  2359. .id = 0x00380802,
  2360. .mask = 0x00ffffff,
  2361. .data = &vendor_st,
  2362. },
  2363. {
  2364. .id = AMBA_LINUX_ID(0x00, 0x1, 0xffe),
  2365. .mask = 0x00ffffff,
  2366. .data = &vendor_zte,
  2367. },
  2368. { 0, 0 },
  2369. };
  2370. MODULE_DEVICE_TABLE(amba, pl011_ids);
  2371. static struct amba_driver pl011_driver = {
  2372. .drv = {
  2373. .name = "uart-pl011",
  2374. .pm = &pl011_dev_pm_ops,
  2375. },
  2376. .id_table = pl011_ids,
  2377. .probe = pl011_probe,
  2378. .remove = pl011_remove,
  2379. };
  2380. static int __init pl011_init(void)
  2381. {
  2382. printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
  2383. if (platform_driver_register(&arm_sbsa_uart_platform_driver))
  2384. pr_warn("could not register SBSA UART platform driver\n");
  2385. return amba_driver_register(&pl011_driver);
  2386. }
  2387. static void __exit pl011_exit(void)
  2388. {
  2389. platform_driver_unregister(&arm_sbsa_uart_platform_driver);
  2390. amba_driver_unregister(&pl011_driver);
  2391. }
  2392. /*
  2393. * While this can be a module, if builtin it's most likely the console
  2394. * So let's leave module_exit but move module_init to an earlier place
  2395. */
  2396. arch_initcall(pl011_init);
  2397. module_exit(pl011_exit);
  2398. MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
  2399. MODULE_DESCRIPTION("ARM AMBA serial port driver");
  2400. MODULE_LICENSE("GPL");