amba-pl011.c 66 KB

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