amba-pl011.c 52 KB

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