amba-pl011.c 59 KB

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