amba-pl011.c 69 KB

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