amba-pl011.c 52 KB

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