amba-pl011.c 47 KB

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