amba-pl011.c 47 KB

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