amba-pl011.c 53 KB

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