amba-pl011.c 51 KB

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