amba-pl011.c 53 KB

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