amba-pl011.c 52 KB

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