amba-pl011.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  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_dmatx_data {
  89. struct dma_chan *chan;
  90. struct scatterlist sg;
  91. char *buf;
  92. bool queued;
  93. };
  94. /*
  95. * We wrap our port structure around the generic uart_port.
  96. */
  97. struct uart_amba_port {
  98. struct uart_port port;
  99. struct clk *clk;
  100. const struct vendor_data *vendor;
  101. unsigned int dmacr; /* dma control reg */
  102. unsigned int im; /* interrupt mask */
  103. unsigned int old_status;
  104. unsigned int fifosize; /* vendor-specific */
  105. unsigned int lcrh_tx; /* vendor-specific */
  106. unsigned int lcrh_rx; /* vendor-specific */
  107. bool autorts;
  108. char type[12];
  109. #ifdef CONFIG_DMA_ENGINE
  110. /* DMA stuff */
  111. bool using_dma;
  112. struct pl011_dmatx_data dmatx;
  113. #endif
  114. };
  115. /*
  116. * All the DMA operation mode stuff goes inside this ifdef.
  117. * This assumes that you have a generic DMA device interface,
  118. * no custom DMA interfaces are supported.
  119. */
  120. #ifdef CONFIG_DMA_ENGINE
  121. #define PL011_DMA_BUFFER_SIZE PAGE_SIZE
  122. static void pl011_dma_probe_initcall(struct uart_amba_port *uap)
  123. {
  124. /* DMA is the sole user of the platform data right now */
  125. struct amba_pl011_data *plat = uap->port.dev->platform_data;
  126. struct dma_slave_config tx_conf = {
  127. .dst_addr = uap->port.mapbase + UART01x_DR,
  128. .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  129. .direction = DMA_TO_DEVICE,
  130. .dst_maxburst = uap->fifosize >> 1,
  131. };
  132. struct dma_chan *chan;
  133. dma_cap_mask_t mask;
  134. /* We need platform data */
  135. if (!plat || !plat->dma_filter) {
  136. dev_info(uap->port.dev, "no DMA platform data\n");
  137. return;
  138. }
  139. /* Try to acquire a generic DMA engine slave channel */
  140. dma_cap_zero(mask);
  141. dma_cap_set(DMA_SLAVE, mask);
  142. chan = dma_request_channel(mask, plat->dma_filter, plat->dma_tx_param);
  143. if (!chan) {
  144. dev_err(uap->port.dev, "no TX DMA channel!\n");
  145. return;
  146. }
  147. dmaengine_slave_config(chan, &tx_conf);
  148. uap->dmatx.chan = chan;
  149. dev_info(uap->port.dev, "DMA channel TX %s\n",
  150. dma_chan_name(uap->dmatx.chan));
  151. }
  152. #ifndef MODULE
  153. /*
  154. * Stack up the UARTs and let the above initcall be done at device
  155. * initcall time, because the serial driver is called as an arch
  156. * initcall, and at this time the DMA subsystem is not yet registered.
  157. * At this point the driver will switch over to using DMA where desired.
  158. */
  159. struct dma_uap {
  160. struct list_head node;
  161. struct uart_amba_port *uap;
  162. };
  163. static LIST_HEAD(pl011_dma_uarts);
  164. static int __init pl011_dma_initcall(void)
  165. {
  166. struct list_head *node, *tmp;
  167. list_for_each_safe(node, tmp, &pl011_dma_uarts) {
  168. struct dma_uap *dmau = list_entry(node, struct dma_uap, node);
  169. pl011_dma_probe_initcall(dmau->uap);
  170. list_del(node);
  171. kfree(dmau);
  172. }
  173. return 0;
  174. }
  175. device_initcall(pl011_dma_initcall);
  176. static void pl011_dma_probe(struct uart_amba_port *uap)
  177. {
  178. struct dma_uap *dmau = kzalloc(sizeof(struct dma_uap), GFP_KERNEL);
  179. if (dmau) {
  180. dmau->uap = uap;
  181. list_add_tail(&dmau->node, &pl011_dma_uarts);
  182. }
  183. }
  184. #else
  185. static void pl011_dma_probe(struct uart_amba_port *uap)
  186. {
  187. pl011_dma_probe_initcall(uap);
  188. }
  189. #endif
  190. static void pl011_dma_remove(struct uart_amba_port *uap)
  191. {
  192. /* TODO: remove the initcall if it has not yet executed */
  193. if (uap->dmatx.chan)
  194. dma_release_channel(uap->dmatx.chan);
  195. }
  196. /* Forward declare this for the refill routine */
  197. static int pl011_dma_tx_refill(struct uart_amba_port *uap);
  198. /*
  199. * The current DMA TX buffer has been sent.
  200. * Try to queue up another DMA buffer.
  201. */
  202. static void pl011_dma_tx_callback(void *data)
  203. {
  204. struct uart_amba_port *uap = data;
  205. struct pl011_dmatx_data *dmatx = &uap->dmatx;
  206. unsigned long flags;
  207. u16 dmacr;
  208. spin_lock_irqsave(&uap->port.lock, flags);
  209. if (uap->dmatx.queued)
  210. dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
  211. DMA_TO_DEVICE);
  212. dmacr = uap->dmacr;
  213. uap->dmacr = dmacr & ~UART011_TXDMAE;
  214. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  215. /*
  216. * If TX DMA was disabled, it means that we've stopped the DMA for
  217. * some reason (eg, XOFF received, or we want to send an X-char.)
  218. *
  219. * Note: we need to be careful here of a potential race between DMA
  220. * and the rest of the driver - if the driver disables TX DMA while
  221. * a TX buffer completing, we must update the tx queued status to
  222. * get further refills (hence we check dmacr).
  223. */
  224. if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
  225. uart_circ_empty(&uap->port.state->xmit)) {
  226. uap->dmatx.queued = false;
  227. spin_unlock_irqrestore(&uap->port.lock, flags);
  228. return;
  229. }
  230. if (pl011_dma_tx_refill(uap) <= 0) {
  231. /*
  232. * We didn't queue a DMA buffer for some reason, but we
  233. * have data pending to be sent. Re-enable the TX IRQ.
  234. */
  235. uap->im |= UART011_TXIM;
  236. writew(uap->im, uap->port.membase + UART011_IMSC);
  237. }
  238. spin_unlock_irqrestore(&uap->port.lock, flags);
  239. }
  240. /*
  241. * Try to refill the TX DMA buffer.
  242. * Locking: called with port lock held and IRQs disabled.
  243. * Returns:
  244. * 1 if we queued up a TX DMA buffer.
  245. * 0 if we didn't want to handle this by DMA
  246. * <0 on error
  247. */
  248. static int pl011_dma_tx_refill(struct uart_amba_port *uap)
  249. {
  250. struct pl011_dmatx_data *dmatx = &uap->dmatx;
  251. struct dma_chan *chan = dmatx->chan;
  252. struct dma_device *dma_dev = chan->device;
  253. struct dma_async_tx_descriptor *desc;
  254. struct circ_buf *xmit = &uap->port.state->xmit;
  255. unsigned int count;
  256. /*
  257. * Try to avoid the overhead involved in using DMA if the
  258. * transaction fits in the first half of the FIFO, by using
  259. * the standard interrupt handling. This ensures that we
  260. * issue a uart_write_wakeup() at the appropriate time.
  261. */
  262. count = uart_circ_chars_pending(xmit);
  263. if (count < (uap->fifosize >> 1)) {
  264. uap->dmatx.queued = false;
  265. return 0;
  266. }
  267. /*
  268. * Bodge: don't send the last character by DMA, as this
  269. * will prevent XON from notifying us to restart DMA.
  270. */
  271. count -= 1;
  272. /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
  273. if (count > PL011_DMA_BUFFER_SIZE)
  274. count = PL011_DMA_BUFFER_SIZE;
  275. if (xmit->tail < xmit->head)
  276. memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
  277. else {
  278. size_t first = UART_XMIT_SIZE - xmit->tail;
  279. size_t second = xmit->head;
  280. memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
  281. if (second)
  282. memcpy(&dmatx->buf[first], &xmit->buf[0], second);
  283. }
  284. dmatx->sg.length = count;
  285. if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
  286. uap->dmatx.queued = false;
  287. dev_dbg(uap->port.dev, "unable to map TX DMA\n");
  288. return -EBUSY;
  289. }
  290. desc = dma_dev->device_prep_slave_sg(chan, &dmatx->sg, 1, DMA_TO_DEVICE,
  291. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  292. if (!desc) {
  293. dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
  294. uap->dmatx.queued = false;
  295. /*
  296. * If DMA cannot be used right now, we complete this
  297. * transaction via IRQ and let the TTY layer retry.
  298. */
  299. dev_dbg(uap->port.dev, "TX DMA busy\n");
  300. return -EBUSY;
  301. }
  302. /* Some data to go along to the callback */
  303. desc->callback = pl011_dma_tx_callback;
  304. desc->callback_param = uap;
  305. /* All errors should happen at prepare time */
  306. dmaengine_submit(desc);
  307. /* Fire the DMA transaction */
  308. dma_dev->device_issue_pending(chan);
  309. uap->dmacr |= UART011_TXDMAE;
  310. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  311. uap->dmatx.queued = true;
  312. /*
  313. * Now we know that DMA will fire, so advance the ring buffer
  314. * with the stuff we just dispatched.
  315. */
  316. xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
  317. uap->port.icount.tx += count;
  318. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  319. uart_write_wakeup(&uap->port);
  320. return 1;
  321. }
  322. /*
  323. * We received a transmit interrupt without a pending X-char but with
  324. * pending characters.
  325. * Locking: called with port lock held and IRQs disabled.
  326. * Returns:
  327. * false if we want to use PIO to transmit
  328. * true if we queued a DMA buffer
  329. */
  330. static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
  331. {
  332. if (!uap->using_dma)
  333. return false;
  334. /*
  335. * If we already have a TX buffer queued, but received a
  336. * TX interrupt, it will be because we've just sent an X-char.
  337. * Ensure the TX DMA is enabled and the TX IRQ is disabled.
  338. */
  339. if (uap->dmatx.queued) {
  340. uap->dmacr |= UART011_TXDMAE;
  341. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  342. uap->im &= ~UART011_TXIM;
  343. writew(uap->im, uap->port.membase + UART011_IMSC);
  344. return true;
  345. }
  346. /*
  347. * We don't have a TX buffer queued, so try to queue one.
  348. * If we succesfully queued a buffer, mask the TX IRQ.
  349. */
  350. if (pl011_dma_tx_refill(uap) > 0) {
  351. uap->im &= ~UART011_TXIM;
  352. writew(uap->im, uap->port.membase + UART011_IMSC);
  353. return true;
  354. }
  355. return false;
  356. }
  357. /*
  358. * Stop the DMA transmit (eg, due to received XOFF).
  359. * Locking: called with port lock held and IRQs disabled.
  360. */
  361. static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
  362. {
  363. if (uap->dmatx.queued) {
  364. uap->dmacr &= ~UART011_TXDMAE;
  365. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  366. }
  367. }
  368. /*
  369. * Try to start a DMA transmit, or in the case of an XON/OFF
  370. * character queued for send, try to get that character out ASAP.
  371. * Locking: called with port lock held and IRQs disabled.
  372. * Returns:
  373. * false if we want the TX IRQ to be enabled
  374. * true if we have a buffer queued
  375. */
  376. static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
  377. {
  378. u16 dmacr;
  379. if (!uap->using_dma)
  380. return false;
  381. if (!uap->port.x_char) {
  382. /* no X-char, try to push chars out in DMA mode */
  383. bool ret = true;
  384. if (!uap->dmatx.queued) {
  385. if (pl011_dma_tx_refill(uap) > 0) {
  386. uap->im &= ~UART011_TXIM;
  387. ret = true;
  388. } else {
  389. uap->im |= UART011_TXIM;
  390. ret = false;
  391. }
  392. writew(uap->im, uap->port.membase + UART011_IMSC);
  393. } else if (!(uap->dmacr & UART011_TXDMAE)) {
  394. uap->dmacr |= UART011_TXDMAE;
  395. writew(uap->dmacr,
  396. uap->port.membase + UART011_DMACR);
  397. }
  398. return ret;
  399. }
  400. /*
  401. * We have an X-char to send. Disable DMA to prevent it loading
  402. * the TX fifo, and then see if we can stuff it into the FIFO.
  403. */
  404. dmacr = uap->dmacr;
  405. uap->dmacr &= ~UART011_TXDMAE;
  406. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  407. if (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF) {
  408. /*
  409. * No space in the FIFO, so enable the transmit interrupt
  410. * so we know when there is space. Note that once we've
  411. * loaded the character, we should just re-enable DMA.
  412. */
  413. return false;
  414. }
  415. writew(uap->port.x_char, uap->port.membase + UART01x_DR);
  416. uap->port.icount.tx++;
  417. uap->port.x_char = 0;
  418. /* Success - restore the DMA state */
  419. uap->dmacr = dmacr;
  420. writew(dmacr, uap->port.membase + UART011_DMACR);
  421. return true;
  422. }
  423. /*
  424. * Flush the transmit buffer.
  425. * Locking: called with port lock held and IRQs disabled.
  426. */
  427. static void pl011_dma_flush_buffer(struct uart_port *port)
  428. {
  429. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  430. if (!uap->using_dma)
  431. return;
  432. /* Avoid deadlock with the DMA engine callback */
  433. spin_unlock(&uap->port.lock);
  434. dmaengine_terminate_all(uap->dmatx.chan);
  435. spin_lock(&uap->port.lock);
  436. if (uap->dmatx.queued) {
  437. dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
  438. DMA_TO_DEVICE);
  439. uap->dmatx.queued = false;
  440. uap->dmacr &= ~UART011_TXDMAE;
  441. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  442. }
  443. }
  444. static void pl011_dma_startup(struct uart_amba_port *uap)
  445. {
  446. if (!uap->dmatx.chan)
  447. return;
  448. uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
  449. if (!uap->dmatx.buf) {
  450. dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
  451. uap->port.fifosize = uap->fifosize;
  452. return;
  453. }
  454. sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
  455. /* The DMA buffer is now the FIFO the TTY subsystem can use */
  456. uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
  457. uap->using_dma = true;
  458. /* Turn on DMA error (RX/TX will be enabled on demand) */
  459. uap->dmacr |= UART011_DMAONERR;
  460. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  461. /*
  462. * ST Micro variants has some specific dma burst threshold
  463. * compensation. Set this to 16 bytes, so burst will only
  464. * be issued above/below 16 bytes.
  465. */
  466. if (uap->vendor->dma_threshold)
  467. writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
  468. uap->port.membase + ST_UART011_DMAWM);
  469. }
  470. static void pl011_dma_shutdown(struct uart_amba_port *uap)
  471. {
  472. if (!uap->using_dma)
  473. return;
  474. /* Disable RX and TX DMA */
  475. while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
  476. barrier();
  477. spin_lock_irq(&uap->port.lock);
  478. uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
  479. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  480. spin_unlock_irq(&uap->port.lock);
  481. /* In theory, this should already be done by pl011_dma_flush_buffer */
  482. dmaengine_terminate_all(uap->dmatx.chan);
  483. if (uap->dmatx.queued) {
  484. dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
  485. DMA_TO_DEVICE);
  486. uap->dmatx.queued = false;
  487. }
  488. kfree(uap->dmatx.buf);
  489. uap->using_dma = false;
  490. }
  491. #else
  492. /* Blank functions if the DMA engine is not available */
  493. static inline void pl011_dma_probe(struct uart_amba_port *uap)
  494. {
  495. }
  496. static inline void pl011_dma_remove(struct uart_amba_port *uap)
  497. {
  498. }
  499. static inline void pl011_dma_startup(struct uart_amba_port *uap)
  500. {
  501. }
  502. static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
  503. {
  504. }
  505. static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
  506. {
  507. return false;
  508. }
  509. static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
  510. {
  511. }
  512. static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
  513. {
  514. return false;
  515. }
  516. #define pl011_dma_flush_buffer NULL
  517. #endif
  518. static void pl011_stop_tx(struct uart_port *port)
  519. {
  520. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  521. uap->im &= ~UART011_TXIM;
  522. writew(uap->im, uap->port.membase + UART011_IMSC);
  523. pl011_dma_tx_stop(uap);
  524. }
  525. static void pl011_start_tx(struct uart_port *port)
  526. {
  527. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  528. if (!pl011_dma_tx_start(uap)) {
  529. uap->im |= UART011_TXIM;
  530. writew(uap->im, uap->port.membase + UART011_IMSC);
  531. }
  532. }
  533. static void pl011_stop_rx(struct uart_port *port)
  534. {
  535. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  536. uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
  537. UART011_PEIM|UART011_BEIM|UART011_OEIM);
  538. writew(uap->im, uap->port.membase + UART011_IMSC);
  539. }
  540. static void pl011_enable_ms(struct uart_port *port)
  541. {
  542. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  543. uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
  544. writew(uap->im, uap->port.membase + UART011_IMSC);
  545. }
  546. static void pl011_rx_chars(struct uart_amba_port *uap)
  547. {
  548. struct tty_struct *tty = uap->port.state->port.tty;
  549. unsigned int status, ch, flag, max_count = 256;
  550. status = readw(uap->port.membase + UART01x_FR);
  551. while ((status & UART01x_FR_RXFE) == 0 && max_count--) {
  552. ch = readw(uap->port.membase + UART01x_DR) | UART_DUMMY_DR_RX;
  553. flag = TTY_NORMAL;
  554. uap->port.icount.rx++;
  555. /*
  556. * Note that the error handling code is
  557. * out of the main execution path
  558. */
  559. if (unlikely(ch & UART_DR_ERROR)) {
  560. if (ch & UART011_DR_BE) {
  561. ch &= ~(UART011_DR_FE | UART011_DR_PE);
  562. uap->port.icount.brk++;
  563. if (uart_handle_break(&uap->port))
  564. goto ignore_char;
  565. } else if (ch & UART011_DR_PE)
  566. uap->port.icount.parity++;
  567. else if (ch & UART011_DR_FE)
  568. uap->port.icount.frame++;
  569. if (ch & UART011_DR_OE)
  570. uap->port.icount.overrun++;
  571. ch &= uap->port.read_status_mask;
  572. if (ch & UART011_DR_BE)
  573. flag = TTY_BREAK;
  574. else if (ch & UART011_DR_PE)
  575. flag = TTY_PARITY;
  576. else if (ch & UART011_DR_FE)
  577. flag = TTY_FRAME;
  578. }
  579. if (uart_handle_sysrq_char(&uap->port, ch & 255))
  580. goto ignore_char;
  581. uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
  582. ignore_char:
  583. status = readw(uap->port.membase + UART01x_FR);
  584. }
  585. spin_unlock(&uap->port.lock);
  586. tty_flip_buffer_push(tty);
  587. spin_lock(&uap->port.lock);
  588. }
  589. static void pl011_tx_chars(struct uart_amba_port *uap)
  590. {
  591. struct circ_buf *xmit = &uap->port.state->xmit;
  592. int count;
  593. if (uap->port.x_char) {
  594. writew(uap->port.x_char, uap->port.membase + UART01x_DR);
  595. uap->port.icount.tx++;
  596. uap->port.x_char = 0;
  597. return;
  598. }
  599. if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
  600. pl011_stop_tx(&uap->port);
  601. return;
  602. }
  603. /* If we are using DMA mode, try to send some characters. */
  604. if (pl011_dma_tx_irq(uap))
  605. return;
  606. count = uap->fifosize >> 1;
  607. do {
  608. writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
  609. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  610. uap->port.icount.tx++;
  611. if (uart_circ_empty(xmit))
  612. break;
  613. } while (--count > 0);
  614. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  615. uart_write_wakeup(&uap->port);
  616. if (uart_circ_empty(xmit))
  617. pl011_stop_tx(&uap->port);
  618. }
  619. static void pl011_modem_status(struct uart_amba_port *uap)
  620. {
  621. unsigned int status, delta;
  622. status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
  623. delta = status ^ uap->old_status;
  624. uap->old_status = status;
  625. if (!delta)
  626. return;
  627. if (delta & UART01x_FR_DCD)
  628. uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
  629. if (delta & UART01x_FR_DSR)
  630. uap->port.icount.dsr++;
  631. if (delta & UART01x_FR_CTS)
  632. uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
  633. wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
  634. }
  635. static irqreturn_t pl011_int(int irq, void *dev_id)
  636. {
  637. struct uart_amba_port *uap = dev_id;
  638. unsigned long flags;
  639. unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
  640. int handled = 0;
  641. spin_lock_irqsave(&uap->port.lock, flags);
  642. status = readw(uap->port.membase + UART011_MIS);
  643. if (status) {
  644. do {
  645. writew(status & ~(UART011_TXIS|UART011_RTIS|
  646. UART011_RXIS),
  647. uap->port.membase + UART011_ICR);
  648. if (status & (UART011_RTIS|UART011_RXIS))
  649. pl011_rx_chars(uap);
  650. if (status & (UART011_DSRMIS|UART011_DCDMIS|
  651. UART011_CTSMIS|UART011_RIMIS))
  652. pl011_modem_status(uap);
  653. if (status & UART011_TXIS)
  654. pl011_tx_chars(uap);
  655. if (pass_counter-- == 0)
  656. break;
  657. status = readw(uap->port.membase + UART011_MIS);
  658. } while (status != 0);
  659. handled = 1;
  660. }
  661. spin_unlock_irqrestore(&uap->port.lock, flags);
  662. return IRQ_RETVAL(handled);
  663. }
  664. static unsigned int pl01x_tx_empty(struct uart_port *port)
  665. {
  666. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  667. unsigned int status = readw(uap->port.membase + UART01x_FR);
  668. return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
  669. }
  670. static unsigned int pl01x_get_mctrl(struct uart_port *port)
  671. {
  672. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  673. unsigned int result = 0;
  674. unsigned int status = readw(uap->port.membase + UART01x_FR);
  675. #define TIOCMBIT(uartbit, tiocmbit) \
  676. if (status & uartbit) \
  677. result |= tiocmbit
  678. TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
  679. TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
  680. TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
  681. TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
  682. #undef TIOCMBIT
  683. return result;
  684. }
  685. static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
  686. {
  687. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  688. unsigned int cr;
  689. cr = readw(uap->port.membase + UART011_CR);
  690. #define TIOCMBIT(tiocmbit, uartbit) \
  691. if (mctrl & tiocmbit) \
  692. cr |= uartbit; \
  693. else \
  694. cr &= ~uartbit
  695. TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
  696. TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
  697. TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
  698. TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
  699. TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
  700. if (uap->autorts) {
  701. /* We need to disable auto-RTS if we want to turn RTS off */
  702. TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
  703. }
  704. #undef TIOCMBIT
  705. writew(cr, uap->port.membase + UART011_CR);
  706. }
  707. static void pl011_break_ctl(struct uart_port *port, int break_state)
  708. {
  709. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  710. unsigned long flags;
  711. unsigned int lcr_h;
  712. spin_lock_irqsave(&uap->port.lock, flags);
  713. lcr_h = readw(uap->port.membase + uap->lcrh_tx);
  714. if (break_state == -1)
  715. lcr_h |= UART01x_LCRH_BRK;
  716. else
  717. lcr_h &= ~UART01x_LCRH_BRK;
  718. writew(lcr_h, uap->port.membase + uap->lcrh_tx);
  719. spin_unlock_irqrestore(&uap->port.lock, flags);
  720. }
  721. #ifdef CONFIG_CONSOLE_POLL
  722. static int pl010_get_poll_char(struct uart_port *port)
  723. {
  724. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  725. unsigned int status;
  726. status = readw(uap->port.membase + UART01x_FR);
  727. if (status & UART01x_FR_RXFE)
  728. return NO_POLL_CHAR;
  729. return readw(uap->port.membase + UART01x_DR);
  730. }
  731. static void pl010_put_poll_char(struct uart_port *port,
  732. unsigned char ch)
  733. {
  734. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  735. while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
  736. barrier();
  737. writew(ch, uap->port.membase + UART01x_DR);
  738. }
  739. #endif /* CONFIG_CONSOLE_POLL */
  740. static int pl011_startup(struct uart_port *port)
  741. {
  742. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  743. unsigned int cr;
  744. int retval;
  745. /*
  746. * Try to enable the clock producer.
  747. */
  748. retval = clk_enable(uap->clk);
  749. if (retval)
  750. goto out;
  751. uap->port.uartclk = clk_get_rate(uap->clk);
  752. /*
  753. * Allocate the IRQ
  754. */
  755. retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
  756. if (retval)
  757. goto clk_dis;
  758. writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
  759. /*
  760. * Provoke TX FIFO interrupt into asserting.
  761. */
  762. cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
  763. writew(cr, uap->port.membase + UART011_CR);
  764. writew(0, uap->port.membase + UART011_FBRD);
  765. writew(1, uap->port.membase + UART011_IBRD);
  766. writew(0, uap->port.membase + uap->lcrh_rx);
  767. if (uap->lcrh_tx != uap->lcrh_rx) {
  768. int i;
  769. /*
  770. * Wait 10 PCLKs before writing LCRH_TX register,
  771. * to get this delay write read only register 10 times
  772. */
  773. for (i = 0; i < 10; ++i)
  774. writew(0xff, uap->port.membase + UART011_MIS);
  775. writew(0, uap->port.membase + uap->lcrh_tx);
  776. }
  777. writew(0, uap->port.membase + UART01x_DR);
  778. while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
  779. barrier();
  780. cr = UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
  781. writew(cr, uap->port.membase + UART011_CR);
  782. /* Clear pending error interrupts */
  783. writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
  784. uap->port.membase + UART011_ICR);
  785. /*
  786. * initialise the old status of the modem signals
  787. */
  788. uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
  789. /* Startup DMA */
  790. pl011_dma_startup(uap);
  791. /*
  792. * Finally, enable interrupts
  793. */
  794. spin_lock_irq(&uap->port.lock);
  795. uap->im = UART011_RXIM | UART011_RTIM;
  796. writew(uap->im, uap->port.membase + UART011_IMSC);
  797. spin_unlock_irq(&uap->port.lock);
  798. return 0;
  799. clk_dis:
  800. clk_disable(uap->clk);
  801. out:
  802. return retval;
  803. }
  804. static void pl011_shutdown_channel(struct uart_amba_port *uap,
  805. unsigned int lcrh)
  806. {
  807. unsigned long val;
  808. val = readw(uap->port.membase + lcrh);
  809. val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
  810. writew(val, uap->port.membase + lcrh);
  811. }
  812. static void pl011_shutdown(struct uart_port *port)
  813. {
  814. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  815. /*
  816. * disable all interrupts
  817. */
  818. spin_lock_irq(&uap->port.lock);
  819. uap->im = 0;
  820. writew(uap->im, uap->port.membase + UART011_IMSC);
  821. writew(0xffff, uap->port.membase + UART011_ICR);
  822. spin_unlock_irq(&uap->port.lock);
  823. pl011_dma_shutdown(uap);
  824. /*
  825. * Free the interrupt
  826. */
  827. free_irq(uap->port.irq, uap);
  828. /*
  829. * disable the port
  830. */
  831. uap->autorts = false;
  832. writew(UART01x_CR_UARTEN | UART011_CR_TXE, uap->port.membase + UART011_CR);
  833. /*
  834. * disable break condition and fifos
  835. */
  836. pl011_shutdown_channel(uap, uap->lcrh_rx);
  837. if (uap->lcrh_rx != uap->lcrh_tx)
  838. pl011_shutdown_channel(uap, uap->lcrh_tx);
  839. /*
  840. * Shut down the clock producer
  841. */
  842. clk_disable(uap->clk);
  843. }
  844. static void
  845. pl011_set_termios(struct uart_port *port, struct ktermios *termios,
  846. struct ktermios *old)
  847. {
  848. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  849. unsigned int lcr_h, old_cr;
  850. unsigned long flags;
  851. unsigned int baud, quot, clkdiv;
  852. if (uap->vendor->oversampling)
  853. clkdiv = 8;
  854. else
  855. clkdiv = 16;
  856. /*
  857. * Ask the core to calculate the divisor for us.
  858. */
  859. baud = uart_get_baud_rate(port, termios, old, 0,
  860. port->uartclk / clkdiv);
  861. if (baud > port->uartclk/16)
  862. quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
  863. else
  864. quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
  865. switch (termios->c_cflag & CSIZE) {
  866. case CS5:
  867. lcr_h = UART01x_LCRH_WLEN_5;
  868. break;
  869. case CS6:
  870. lcr_h = UART01x_LCRH_WLEN_6;
  871. break;
  872. case CS7:
  873. lcr_h = UART01x_LCRH_WLEN_7;
  874. break;
  875. default: // CS8
  876. lcr_h = UART01x_LCRH_WLEN_8;
  877. break;
  878. }
  879. if (termios->c_cflag & CSTOPB)
  880. lcr_h |= UART01x_LCRH_STP2;
  881. if (termios->c_cflag & PARENB) {
  882. lcr_h |= UART01x_LCRH_PEN;
  883. if (!(termios->c_cflag & PARODD))
  884. lcr_h |= UART01x_LCRH_EPS;
  885. }
  886. if (uap->fifosize > 1)
  887. lcr_h |= UART01x_LCRH_FEN;
  888. spin_lock_irqsave(&port->lock, flags);
  889. /*
  890. * Update the per-port timeout.
  891. */
  892. uart_update_timeout(port, termios->c_cflag, baud);
  893. port->read_status_mask = UART011_DR_OE | 255;
  894. if (termios->c_iflag & INPCK)
  895. port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
  896. if (termios->c_iflag & (BRKINT | PARMRK))
  897. port->read_status_mask |= UART011_DR_BE;
  898. /*
  899. * Characters to ignore
  900. */
  901. port->ignore_status_mask = 0;
  902. if (termios->c_iflag & IGNPAR)
  903. port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
  904. if (termios->c_iflag & IGNBRK) {
  905. port->ignore_status_mask |= UART011_DR_BE;
  906. /*
  907. * If we're ignoring parity and break indicators,
  908. * ignore overruns too (for real raw support).
  909. */
  910. if (termios->c_iflag & IGNPAR)
  911. port->ignore_status_mask |= UART011_DR_OE;
  912. }
  913. /*
  914. * Ignore all characters if CREAD is not set.
  915. */
  916. if ((termios->c_cflag & CREAD) == 0)
  917. port->ignore_status_mask |= UART_DUMMY_DR_RX;
  918. if (UART_ENABLE_MS(port, termios->c_cflag))
  919. pl011_enable_ms(port);
  920. /* first, disable everything */
  921. old_cr = readw(port->membase + UART011_CR);
  922. writew(0, port->membase + UART011_CR);
  923. if (termios->c_cflag & CRTSCTS) {
  924. if (old_cr & UART011_CR_RTS)
  925. old_cr |= UART011_CR_RTSEN;
  926. old_cr |= UART011_CR_CTSEN;
  927. uap->autorts = true;
  928. } else {
  929. old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
  930. uap->autorts = false;
  931. }
  932. if (uap->vendor->oversampling) {
  933. if (baud > port->uartclk / 16)
  934. old_cr |= ST_UART011_CR_OVSFACT;
  935. else
  936. old_cr &= ~ST_UART011_CR_OVSFACT;
  937. }
  938. /* Set baud rate */
  939. writew(quot & 0x3f, port->membase + UART011_FBRD);
  940. writew(quot >> 6, port->membase + UART011_IBRD);
  941. /*
  942. * ----------v----------v----------v----------v-----
  943. * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
  944. * ----------^----------^----------^----------^-----
  945. */
  946. writew(lcr_h, port->membase + uap->lcrh_rx);
  947. if (uap->lcrh_rx != uap->lcrh_tx) {
  948. int i;
  949. /*
  950. * Wait 10 PCLKs before writing LCRH_TX register,
  951. * to get this delay write read only register 10 times
  952. */
  953. for (i = 0; i < 10; ++i)
  954. writew(0xff, uap->port.membase + UART011_MIS);
  955. writew(lcr_h, port->membase + uap->lcrh_tx);
  956. }
  957. writew(old_cr, port->membase + UART011_CR);
  958. spin_unlock_irqrestore(&port->lock, flags);
  959. }
  960. static const char *pl011_type(struct uart_port *port)
  961. {
  962. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  963. return uap->port.type == PORT_AMBA ? uap->type : NULL;
  964. }
  965. /*
  966. * Release the memory region(s) being used by 'port'
  967. */
  968. static void pl010_release_port(struct uart_port *port)
  969. {
  970. release_mem_region(port->mapbase, SZ_4K);
  971. }
  972. /*
  973. * Request the memory region(s) being used by 'port'
  974. */
  975. static int pl010_request_port(struct uart_port *port)
  976. {
  977. return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
  978. != NULL ? 0 : -EBUSY;
  979. }
  980. /*
  981. * Configure/autoconfigure the port.
  982. */
  983. static void pl010_config_port(struct uart_port *port, int flags)
  984. {
  985. if (flags & UART_CONFIG_TYPE) {
  986. port->type = PORT_AMBA;
  987. pl010_request_port(port);
  988. }
  989. }
  990. /*
  991. * verify the new serial_struct (for TIOCSSERIAL).
  992. */
  993. static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
  994. {
  995. int ret = 0;
  996. if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
  997. ret = -EINVAL;
  998. if (ser->irq < 0 || ser->irq >= nr_irqs)
  999. ret = -EINVAL;
  1000. if (ser->baud_base < 9600)
  1001. ret = -EINVAL;
  1002. return ret;
  1003. }
  1004. static struct uart_ops amba_pl011_pops = {
  1005. .tx_empty = pl01x_tx_empty,
  1006. .set_mctrl = pl011_set_mctrl,
  1007. .get_mctrl = pl01x_get_mctrl,
  1008. .stop_tx = pl011_stop_tx,
  1009. .start_tx = pl011_start_tx,
  1010. .stop_rx = pl011_stop_rx,
  1011. .enable_ms = pl011_enable_ms,
  1012. .break_ctl = pl011_break_ctl,
  1013. .startup = pl011_startup,
  1014. .shutdown = pl011_shutdown,
  1015. .flush_buffer = pl011_dma_flush_buffer,
  1016. .set_termios = pl011_set_termios,
  1017. .type = pl011_type,
  1018. .release_port = pl010_release_port,
  1019. .request_port = pl010_request_port,
  1020. .config_port = pl010_config_port,
  1021. .verify_port = pl010_verify_port,
  1022. #ifdef CONFIG_CONSOLE_POLL
  1023. .poll_get_char = pl010_get_poll_char,
  1024. .poll_put_char = pl010_put_poll_char,
  1025. #endif
  1026. };
  1027. static struct uart_amba_port *amba_ports[UART_NR];
  1028. #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
  1029. static void pl011_console_putchar(struct uart_port *port, int ch)
  1030. {
  1031. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1032. while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
  1033. barrier();
  1034. writew(ch, uap->port.membase + UART01x_DR);
  1035. }
  1036. static void
  1037. pl011_console_write(struct console *co, const char *s, unsigned int count)
  1038. {
  1039. struct uart_amba_port *uap = amba_ports[co->index];
  1040. unsigned int status, old_cr, new_cr;
  1041. clk_enable(uap->clk);
  1042. /*
  1043. * First save the CR then disable the interrupts
  1044. */
  1045. old_cr = readw(uap->port.membase + UART011_CR);
  1046. new_cr = old_cr & ~UART011_CR_CTSEN;
  1047. new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
  1048. writew(new_cr, uap->port.membase + UART011_CR);
  1049. uart_console_write(&uap->port, s, count, pl011_console_putchar);
  1050. /*
  1051. * Finally, wait for transmitter to become empty
  1052. * and restore the TCR
  1053. */
  1054. do {
  1055. status = readw(uap->port.membase + UART01x_FR);
  1056. } while (status & UART01x_FR_BUSY);
  1057. writew(old_cr, uap->port.membase + UART011_CR);
  1058. clk_disable(uap->clk);
  1059. }
  1060. static void __init
  1061. pl011_console_get_options(struct uart_amba_port *uap, int *baud,
  1062. int *parity, int *bits)
  1063. {
  1064. if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
  1065. unsigned int lcr_h, ibrd, fbrd;
  1066. lcr_h = readw(uap->port.membase + uap->lcrh_tx);
  1067. *parity = 'n';
  1068. if (lcr_h & UART01x_LCRH_PEN) {
  1069. if (lcr_h & UART01x_LCRH_EPS)
  1070. *parity = 'e';
  1071. else
  1072. *parity = 'o';
  1073. }
  1074. if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
  1075. *bits = 7;
  1076. else
  1077. *bits = 8;
  1078. ibrd = readw(uap->port.membase + UART011_IBRD);
  1079. fbrd = readw(uap->port.membase + UART011_FBRD);
  1080. *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
  1081. if (uap->vendor->oversampling) {
  1082. if (readw(uap->port.membase + UART011_CR)
  1083. & ST_UART011_CR_OVSFACT)
  1084. *baud *= 2;
  1085. }
  1086. }
  1087. }
  1088. static int __init pl011_console_setup(struct console *co, char *options)
  1089. {
  1090. struct uart_amba_port *uap;
  1091. int baud = 38400;
  1092. int bits = 8;
  1093. int parity = 'n';
  1094. int flow = 'n';
  1095. /*
  1096. * Check whether an invalid uart number has been specified, and
  1097. * if so, search for the first available port that does have
  1098. * console support.
  1099. */
  1100. if (co->index >= UART_NR)
  1101. co->index = 0;
  1102. uap = amba_ports[co->index];
  1103. if (!uap)
  1104. return -ENODEV;
  1105. uap->port.uartclk = clk_get_rate(uap->clk);
  1106. if (options)
  1107. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1108. else
  1109. pl011_console_get_options(uap, &baud, &parity, &bits);
  1110. return uart_set_options(&uap->port, co, baud, parity, bits, flow);
  1111. }
  1112. static struct uart_driver amba_reg;
  1113. static struct console amba_console = {
  1114. .name = "ttyAMA",
  1115. .write = pl011_console_write,
  1116. .device = uart_console_device,
  1117. .setup = pl011_console_setup,
  1118. .flags = CON_PRINTBUFFER,
  1119. .index = -1,
  1120. .data = &amba_reg,
  1121. };
  1122. #define AMBA_CONSOLE (&amba_console)
  1123. #else
  1124. #define AMBA_CONSOLE NULL
  1125. #endif
  1126. static struct uart_driver amba_reg = {
  1127. .owner = THIS_MODULE,
  1128. .driver_name = "ttyAMA",
  1129. .dev_name = "ttyAMA",
  1130. .major = SERIAL_AMBA_MAJOR,
  1131. .minor = SERIAL_AMBA_MINOR,
  1132. .nr = UART_NR,
  1133. .cons = AMBA_CONSOLE,
  1134. };
  1135. static int pl011_probe(struct amba_device *dev, struct amba_id *id)
  1136. {
  1137. struct uart_amba_port *uap;
  1138. struct vendor_data *vendor = id->data;
  1139. void __iomem *base;
  1140. int i, ret;
  1141. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  1142. if (amba_ports[i] == NULL)
  1143. break;
  1144. if (i == ARRAY_SIZE(amba_ports)) {
  1145. ret = -EBUSY;
  1146. goto out;
  1147. }
  1148. uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
  1149. if (uap == NULL) {
  1150. ret = -ENOMEM;
  1151. goto out;
  1152. }
  1153. base = ioremap(dev->res.start, resource_size(&dev->res));
  1154. if (!base) {
  1155. ret = -ENOMEM;
  1156. goto free;
  1157. }
  1158. uap->clk = clk_get(&dev->dev, NULL);
  1159. if (IS_ERR(uap->clk)) {
  1160. ret = PTR_ERR(uap->clk);
  1161. goto unmap;
  1162. }
  1163. uap->vendor = vendor;
  1164. uap->lcrh_rx = vendor->lcrh_rx;
  1165. uap->lcrh_tx = vendor->lcrh_tx;
  1166. uap->fifosize = vendor->fifosize;
  1167. uap->port.dev = &dev->dev;
  1168. uap->port.mapbase = dev->res.start;
  1169. uap->port.membase = base;
  1170. uap->port.iotype = UPIO_MEM;
  1171. uap->port.irq = dev->irq[0];
  1172. uap->port.fifosize = uap->fifosize;
  1173. uap->port.ops = &amba_pl011_pops;
  1174. uap->port.flags = UPF_BOOT_AUTOCONF;
  1175. uap->port.line = i;
  1176. pl011_dma_probe(uap);
  1177. snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
  1178. amba_ports[i] = uap;
  1179. amba_set_drvdata(dev, uap);
  1180. ret = uart_add_one_port(&amba_reg, &uap->port);
  1181. if (ret) {
  1182. amba_set_drvdata(dev, NULL);
  1183. amba_ports[i] = NULL;
  1184. pl011_dma_remove(uap);
  1185. clk_put(uap->clk);
  1186. unmap:
  1187. iounmap(base);
  1188. free:
  1189. kfree(uap);
  1190. }
  1191. out:
  1192. return ret;
  1193. }
  1194. static int pl011_remove(struct amba_device *dev)
  1195. {
  1196. struct uart_amba_port *uap = amba_get_drvdata(dev);
  1197. int i;
  1198. amba_set_drvdata(dev, NULL);
  1199. uart_remove_one_port(&amba_reg, &uap->port);
  1200. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  1201. if (amba_ports[i] == uap)
  1202. amba_ports[i] = NULL;
  1203. pl011_dma_remove(uap);
  1204. iounmap(uap->port.membase);
  1205. clk_put(uap->clk);
  1206. kfree(uap);
  1207. return 0;
  1208. }
  1209. #ifdef CONFIG_PM
  1210. static int pl011_suspend(struct amba_device *dev, pm_message_t state)
  1211. {
  1212. struct uart_amba_port *uap = amba_get_drvdata(dev);
  1213. if (!uap)
  1214. return -EINVAL;
  1215. return uart_suspend_port(&amba_reg, &uap->port);
  1216. }
  1217. static int pl011_resume(struct amba_device *dev)
  1218. {
  1219. struct uart_amba_port *uap = amba_get_drvdata(dev);
  1220. if (!uap)
  1221. return -EINVAL;
  1222. return uart_resume_port(&amba_reg, &uap->port);
  1223. }
  1224. #endif
  1225. static struct amba_id pl011_ids[] = {
  1226. {
  1227. .id = 0x00041011,
  1228. .mask = 0x000fffff,
  1229. .data = &vendor_arm,
  1230. },
  1231. {
  1232. .id = 0x00380802,
  1233. .mask = 0x00ffffff,
  1234. .data = &vendor_st,
  1235. },
  1236. { 0, 0 },
  1237. };
  1238. static struct amba_driver pl011_driver = {
  1239. .drv = {
  1240. .name = "uart-pl011",
  1241. },
  1242. .id_table = pl011_ids,
  1243. .probe = pl011_probe,
  1244. .remove = pl011_remove,
  1245. #ifdef CONFIG_PM
  1246. .suspend = pl011_suspend,
  1247. .resume = pl011_resume,
  1248. #endif
  1249. };
  1250. static int __init pl011_init(void)
  1251. {
  1252. int ret;
  1253. printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
  1254. ret = uart_register_driver(&amba_reg);
  1255. if (ret == 0) {
  1256. ret = amba_driver_register(&pl011_driver);
  1257. if (ret)
  1258. uart_unregister_driver(&amba_reg);
  1259. }
  1260. return ret;
  1261. }
  1262. static void __exit pl011_exit(void)
  1263. {
  1264. amba_driver_unregister(&pl011_driver);
  1265. uart_unregister_driver(&amba_reg);
  1266. }
  1267. /*
  1268. * While this can be a module, if builtin it's most likely the console
  1269. * So let's leave module_exit but move module_init to an earlier place
  1270. */
  1271. arch_initcall(pl011_init);
  1272. module_exit(pl011_exit);
  1273. MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
  1274. MODULE_DESCRIPTION("ARM AMBA serial port driver");
  1275. MODULE_LICENSE("GPL");