amba-pl011.c 58 KB

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