amba-pl011.c 57 KB

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