amba-pl011.c 67 KB

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