amba-pl011.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255
  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 = dev_get_platdata(uap->port.dev);
  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. __releases(&uap->port.lock)
  588. __acquires(&uap->port.lock)
  589. {
  590. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  591. if (!uap->using_tx_dma)
  592. return;
  593. /* Avoid deadlock with the DMA engine callback */
  594. spin_unlock(&uap->port.lock);
  595. dmaengine_terminate_all(uap->dmatx.chan);
  596. spin_lock(&uap->port.lock);
  597. if (uap->dmatx.queued) {
  598. dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
  599. DMA_TO_DEVICE);
  600. uap->dmatx.queued = false;
  601. uap->dmacr &= ~UART011_TXDMAE;
  602. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  603. }
  604. }
  605. static void pl011_dma_rx_callback(void *data);
  606. static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
  607. {
  608. struct dma_chan *rxchan = uap->dmarx.chan;
  609. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  610. struct dma_async_tx_descriptor *desc;
  611. struct pl011_sgbuf *sgbuf;
  612. if (!rxchan)
  613. return -EIO;
  614. /* Start the RX DMA job */
  615. sgbuf = uap->dmarx.use_buf_b ?
  616. &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
  617. desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
  618. DMA_DEV_TO_MEM,
  619. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  620. /*
  621. * If the DMA engine is busy and cannot prepare a
  622. * channel, no big deal, the driver will fall back
  623. * to interrupt mode as a result of this error code.
  624. */
  625. if (!desc) {
  626. uap->dmarx.running = false;
  627. dmaengine_terminate_all(rxchan);
  628. return -EBUSY;
  629. }
  630. /* Some data to go along to the callback */
  631. desc->callback = pl011_dma_rx_callback;
  632. desc->callback_param = uap;
  633. dmarx->cookie = dmaengine_submit(desc);
  634. dma_async_issue_pending(rxchan);
  635. uap->dmacr |= UART011_RXDMAE;
  636. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  637. uap->dmarx.running = true;
  638. uap->im &= ~UART011_RXIM;
  639. writew(uap->im, uap->port.membase + UART011_IMSC);
  640. return 0;
  641. }
  642. /*
  643. * This is called when either the DMA job is complete, or
  644. * the FIFO timeout interrupt occurred. This must be called
  645. * with the port spinlock uap->port.lock held.
  646. */
  647. static void pl011_dma_rx_chars(struct uart_amba_port *uap,
  648. u32 pending, bool use_buf_b,
  649. bool readfifo)
  650. {
  651. struct tty_port *port = &uap->port.state->port;
  652. struct pl011_sgbuf *sgbuf = use_buf_b ?
  653. &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
  654. int dma_count = 0;
  655. u32 fifotaken = 0; /* only used for vdbg() */
  656. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  657. int dmataken = 0;
  658. if (uap->dmarx.poll_rate) {
  659. /* The data can be taken by polling */
  660. dmataken = sgbuf->sg.length - dmarx->last_residue;
  661. /* Recalculate the pending size */
  662. if (pending >= dmataken)
  663. pending -= dmataken;
  664. }
  665. /* Pick the remain data from the DMA */
  666. if (pending) {
  667. /*
  668. * First take all chars in the DMA pipe, then look in the FIFO.
  669. * Note that tty_insert_flip_buf() tries to take as many chars
  670. * as it can.
  671. */
  672. dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
  673. pending);
  674. uap->port.icount.rx += dma_count;
  675. if (dma_count < pending)
  676. dev_warn(uap->port.dev,
  677. "couldn't insert all characters (TTY is full?)\n");
  678. }
  679. /* Reset the last_residue for Rx DMA poll */
  680. if (uap->dmarx.poll_rate)
  681. dmarx->last_residue = sgbuf->sg.length;
  682. /*
  683. * Only continue with trying to read the FIFO if all DMA chars have
  684. * been taken first.
  685. */
  686. if (dma_count == pending && readfifo) {
  687. /* Clear any error flags */
  688. writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
  689. uap->port.membase + UART011_ICR);
  690. /*
  691. * If we read all the DMA'd characters, and we had an
  692. * incomplete buffer, that could be due to an rx error, or
  693. * maybe we just timed out. Read any pending chars and check
  694. * the error status.
  695. *
  696. * Error conditions will only occur in the FIFO, these will
  697. * trigger an immediate interrupt and stop the DMA job, so we
  698. * will always find the error in the FIFO, never in the DMA
  699. * buffer.
  700. */
  701. fifotaken = pl011_fifo_to_tty(uap);
  702. }
  703. spin_unlock(&uap->port.lock);
  704. dev_vdbg(uap->port.dev,
  705. "Took %d chars from DMA buffer and %d chars from the FIFO\n",
  706. dma_count, fifotaken);
  707. tty_flip_buffer_push(port);
  708. spin_lock(&uap->port.lock);
  709. }
  710. static void pl011_dma_rx_irq(struct uart_amba_port *uap)
  711. {
  712. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  713. struct dma_chan *rxchan = dmarx->chan;
  714. struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
  715. &dmarx->sgbuf_b : &dmarx->sgbuf_a;
  716. size_t pending;
  717. struct dma_tx_state state;
  718. enum dma_status dmastat;
  719. /*
  720. * Pause the transfer so we can trust the current counter,
  721. * do this before we pause the PL011 block, else we may
  722. * overflow the FIFO.
  723. */
  724. if (dmaengine_pause(rxchan))
  725. dev_err(uap->port.dev, "unable to pause DMA transfer\n");
  726. dmastat = rxchan->device->device_tx_status(rxchan,
  727. dmarx->cookie, &state);
  728. if (dmastat != DMA_PAUSED)
  729. dev_err(uap->port.dev, "unable to pause DMA transfer\n");
  730. /* Disable RX DMA - incoming data will wait in the FIFO */
  731. uap->dmacr &= ~UART011_RXDMAE;
  732. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  733. uap->dmarx.running = false;
  734. pending = sgbuf->sg.length - state.residue;
  735. BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
  736. /* Then we terminate the transfer - we now know our residue */
  737. dmaengine_terminate_all(rxchan);
  738. /*
  739. * This will take the chars we have so far and insert
  740. * into the framework.
  741. */
  742. pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
  743. /* Switch buffer & re-trigger DMA job */
  744. dmarx->use_buf_b = !dmarx->use_buf_b;
  745. if (pl011_dma_rx_trigger_dma(uap)) {
  746. dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
  747. "fall back to interrupt mode\n");
  748. uap->im |= UART011_RXIM;
  749. writew(uap->im, uap->port.membase + UART011_IMSC);
  750. }
  751. }
  752. static void pl011_dma_rx_callback(void *data)
  753. {
  754. struct uart_amba_port *uap = data;
  755. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  756. struct dma_chan *rxchan = dmarx->chan;
  757. bool lastbuf = dmarx->use_buf_b;
  758. struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
  759. &dmarx->sgbuf_b : &dmarx->sgbuf_a;
  760. size_t pending;
  761. struct dma_tx_state state;
  762. int ret;
  763. /*
  764. * This completion interrupt occurs typically when the
  765. * RX buffer is totally stuffed but no timeout has yet
  766. * occurred. When that happens, we just want the RX
  767. * routine to flush out the secondary DMA buffer while
  768. * we immediately trigger the next DMA job.
  769. */
  770. spin_lock_irq(&uap->port.lock);
  771. /*
  772. * Rx data can be taken by the UART interrupts during
  773. * the DMA irq handler. So we check the residue here.
  774. */
  775. rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
  776. pending = sgbuf->sg.length - state.residue;
  777. BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
  778. /* Then we terminate the transfer - we now know our residue */
  779. dmaengine_terminate_all(rxchan);
  780. uap->dmarx.running = false;
  781. dmarx->use_buf_b = !lastbuf;
  782. ret = pl011_dma_rx_trigger_dma(uap);
  783. pl011_dma_rx_chars(uap, pending, lastbuf, false);
  784. spin_unlock_irq(&uap->port.lock);
  785. /*
  786. * Do this check after we picked the DMA chars so we don't
  787. * get some IRQ immediately from RX.
  788. */
  789. if (ret) {
  790. dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
  791. "fall back to interrupt mode\n");
  792. uap->im |= UART011_RXIM;
  793. writew(uap->im, uap->port.membase + UART011_IMSC);
  794. }
  795. }
  796. /*
  797. * Stop accepting received characters, when we're shutting down or
  798. * suspending this port.
  799. * Locking: called with port lock held and IRQs disabled.
  800. */
  801. static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
  802. {
  803. /* FIXME. Just disable the DMA enable */
  804. uap->dmacr &= ~UART011_RXDMAE;
  805. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  806. }
  807. /*
  808. * Timer handler for Rx DMA polling.
  809. * Every polling, It checks the residue in the dma buffer and transfer
  810. * data to the tty. Also, last_residue is updated for the next polling.
  811. */
  812. static void pl011_dma_rx_poll(unsigned long args)
  813. {
  814. struct uart_amba_port *uap = (struct uart_amba_port *)args;
  815. struct tty_port *port = &uap->port.state->port;
  816. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  817. struct dma_chan *rxchan = uap->dmarx.chan;
  818. unsigned long flags = 0;
  819. unsigned int dmataken = 0;
  820. unsigned int size = 0;
  821. struct pl011_sgbuf *sgbuf;
  822. int dma_count;
  823. struct dma_tx_state state;
  824. sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
  825. rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
  826. if (likely(state.residue < dmarx->last_residue)) {
  827. dmataken = sgbuf->sg.length - dmarx->last_residue;
  828. size = dmarx->last_residue - state.residue;
  829. dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
  830. size);
  831. if (dma_count == size)
  832. dmarx->last_residue = state.residue;
  833. dmarx->last_jiffies = jiffies;
  834. }
  835. tty_flip_buffer_push(port);
  836. /*
  837. * If no data is received in poll_timeout, the driver will fall back
  838. * to interrupt mode. We will retrigger DMA at the first interrupt.
  839. */
  840. if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
  841. > uap->dmarx.poll_timeout) {
  842. spin_lock_irqsave(&uap->port.lock, flags);
  843. pl011_dma_rx_stop(uap);
  844. spin_unlock_irqrestore(&uap->port.lock, flags);
  845. uap->dmarx.running = false;
  846. dmaengine_terminate_all(rxchan);
  847. del_timer(&uap->dmarx.timer);
  848. } else {
  849. mod_timer(&uap->dmarx.timer,
  850. jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
  851. }
  852. }
  853. static void pl011_dma_startup(struct uart_amba_port *uap)
  854. {
  855. int ret;
  856. if (!uap->dmatx.chan)
  857. return;
  858. uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
  859. if (!uap->dmatx.buf) {
  860. dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
  861. uap->port.fifosize = uap->fifosize;
  862. return;
  863. }
  864. sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
  865. /* The DMA buffer is now the FIFO the TTY subsystem can use */
  866. uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
  867. uap->using_tx_dma = true;
  868. if (!uap->dmarx.chan)
  869. goto skip_rx;
  870. /* Allocate and map DMA RX buffers */
  871. ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
  872. DMA_FROM_DEVICE);
  873. if (ret) {
  874. dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
  875. "RX buffer A", ret);
  876. goto skip_rx;
  877. }
  878. ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
  879. DMA_FROM_DEVICE);
  880. if (ret) {
  881. dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
  882. "RX buffer B", ret);
  883. pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
  884. DMA_FROM_DEVICE);
  885. goto skip_rx;
  886. }
  887. uap->using_rx_dma = true;
  888. skip_rx:
  889. /* Turn on DMA error (RX/TX will be enabled on demand) */
  890. uap->dmacr |= UART011_DMAONERR;
  891. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  892. /*
  893. * ST Micro variants has some specific dma burst threshold
  894. * compensation. Set this to 16 bytes, so burst will only
  895. * be issued above/below 16 bytes.
  896. */
  897. if (uap->vendor->dma_threshold)
  898. writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
  899. uap->port.membase + ST_UART011_DMAWM);
  900. if (uap->using_rx_dma) {
  901. if (pl011_dma_rx_trigger_dma(uap))
  902. dev_dbg(uap->port.dev, "could not trigger initial "
  903. "RX DMA job, fall back to interrupt mode\n");
  904. if (uap->dmarx.poll_rate) {
  905. init_timer(&(uap->dmarx.timer));
  906. uap->dmarx.timer.function = pl011_dma_rx_poll;
  907. uap->dmarx.timer.data = (unsigned long)uap;
  908. mod_timer(&uap->dmarx.timer,
  909. jiffies +
  910. msecs_to_jiffies(uap->dmarx.poll_rate));
  911. uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
  912. uap->dmarx.last_jiffies = jiffies;
  913. }
  914. }
  915. }
  916. static void pl011_dma_shutdown(struct uart_amba_port *uap)
  917. {
  918. if (!(uap->using_tx_dma || uap->using_rx_dma))
  919. return;
  920. /* Disable RX and TX DMA */
  921. while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
  922. barrier();
  923. spin_lock_irq(&uap->port.lock);
  924. uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
  925. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  926. spin_unlock_irq(&uap->port.lock);
  927. if (uap->using_tx_dma) {
  928. /* In theory, this should already be done by pl011_dma_flush_buffer */
  929. dmaengine_terminate_all(uap->dmatx.chan);
  930. if (uap->dmatx.queued) {
  931. dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
  932. DMA_TO_DEVICE);
  933. uap->dmatx.queued = false;
  934. }
  935. kfree(uap->dmatx.buf);
  936. uap->using_tx_dma = false;
  937. }
  938. if (uap->using_rx_dma) {
  939. dmaengine_terminate_all(uap->dmarx.chan);
  940. /* Clean up the RX DMA */
  941. pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
  942. pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
  943. if (uap->dmarx.poll_rate)
  944. del_timer_sync(&uap->dmarx.timer);
  945. uap->using_rx_dma = false;
  946. }
  947. }
  948. static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
  949. {
  950. return uap->using_rx_dma;
  951. }
  952. static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
  953. {
  954. return uap->using_rx_dma && uap->dmarx.running;
  955. }
  956. #else
  957. /* Blank functions if the DMA engine is not available */
  958. static inline void pl011_dma_probe(struct device *dev, struct uart_amba_port *uap)
  959. {
  960. }
  961. static inline void pl011_dma_remove(struct uart_amba_port *uap)
  962. {
  963. }
  964. static inline void pl011_dma_startup(struct uart_amba_port *uap)
  965. {
  966. }
  967. static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
  968. {
  969. }
  970. static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
  971. {
  972. return false;
  973. }
  974. static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
  975. {
  976. }
  977. static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
  978. {
  979. return false;
  980. }
  981. static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
  982. {
  983. }
  984. static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
  985. {
  986. }
  987. static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
  988. {
  989. return -EIO;
  990. }
  991. static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
  992. {
  993. return false;
  994. }
  995. static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
  996. {
  997. return false;
  998. }
  999. #define pl011_dma_flush_buffer NULL
  1000. #endif
  1001. static void pl011_stop_tx(struct uart_port *port)
  1002. {
  1003. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1004. uap->im &= ~UART011_TXIM;
  1005. writew(uap->im, uap->port.membase + UART011_IMSC);
  1006. pl011_dma_tx_stop(uap);
  1007. }
  1008. static void pl011_start_tx(struct uart_port *port)
  1009. {
  1010. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1011. if (!pl011_dma_tx_start(uap)) {
  1012. uap->im |= UART011_TXIM;
  1013. writew(uap->im, uap->port.membase + UART011_IMSC);
  1014. }
  1015. }
  1016. static void pl011_stop_rx(struct uart_port *port)
  1017. {
  1018. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1019. uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
  1020. UART011_PEIM|UART011_BEIM|UART011_OEIM);
  1021. writew(uap->im, uap->port.membase + UART011_IMSC);
  1022. pl011_dma_rx_stop(uap);
  1023. }
  1024. static void pl011_enable_ms(struct uart_port *port)
  1025. {
  1026. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1027. uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
  1028. writew(uap->im, uap->port.membase + UART011_IMSC);
  1029. }
  1030. static void pl011_rx_chars(struct uart_amba_port *uap)
  1031. __releases(&uap->port.lock)
  1032. __acquires(&uap->port.lock)
  1033. {
  1034. pl011_fifo_to_tty(uap);
  1035. spin_unlock(&uap->port.lock);
  1036. tty_flip_buffer_push(&uap->port.state->port);
  1037. /*
  1038. * If we were temporarily out of DMA mode for a while,
  1039. * attempt to switch back to DMA mode again.
  1040. */
  1041. if (pl011_dma_rx_available(uap)) {
  1042. if (pl011_dma_rx_trigger_dma(uap)) {
  1043. dev_dbg(uap->port.dev, "could not trigger RX DMA job "
  1044. "fall back to interrupt mode again\n");
  1045. uap->im |= UART011_RXIM;
  1046. } else {
  1047. uap->im &= ~UART011_RXIM;
  1048. #ifdef CONFIG_DMA_ENGINE
  1049. /* Start Rx DMA poll */
  1050. if (uap->dmarx.poll_rate) {
  1051. uap->dmarx.last_jiffies = jiffies;
  1052. uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
  1053. mod_timer(&uap->dmarx.timer,
  1054. jiffies +
  1055. msecs_to_jiffies(uap->dmarx.poll_rate));
  1056. }
  1057. #endif
  1058. }
  1059. writew(uap->im, uap->port.membase + UART011_IMSC);
  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 int pl011_startup(struct uart_port *port)
  1288. {
  1289. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1290. unsigned int cr;
  1291. int retval;
  1292. retval = pl011_hwinit(port);
  1293. if (retval)
  1294. goto clk_dis;
  1295. writew(uap->im, uap->port.membase + UART011_IMSC);
  1296. /*
  1297. * Allocate the IRQ
  1298. */
  1299. retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
  1300. if (retval)
  1301. goto clk_dis;
  1302. writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
  1303. /*
  1304. * Provoke TX FIFO interrupt into asserting.
  1305. */
  1306. cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
  1307. writew(cr, uap->port.membase + UART011_CR);
  1308. writew(0, uap->port.membase + UART011_FBRD);
  1309. writew(1, uap->port.membase + UART011_IBRD);
  1310. writew(0, uap->port.membase + uap->lcrh_rx);
  1311. if (uap->lcrh_tx != uap->lcrh_rx) {
  1312. int i;
  1313. /*
  1314. * Wait 10 PCLKs before writing LCRH_TX register,
  1315. * to get this delay write read only register 10 times
  1316. */
  1317. for (i = 0; i < 10; ++i)
  1318. writew(0xff, uap->port.membase + UART011_MIS);
  1319. writew(0, uap->port.membase + uap->lcrh_tx);
  1320. }
  1321. writew(0, uap->port.membase + UART01x_DR);
  1322. while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
  1323. barrier();
  1324. /* restore RTS and DTR */
  1325. cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
  1326. cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
  1327. writew(cr, uap->port.membase + UART011_CR);
  1328. /*
  1329. * initialise the old status of the modem signals
  1330. */
  1331. uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
  1332. /* Startup DMA */
  1333. pl011_dma_startup(uap);
  1334. /*
  1335. * Finally, enable interrupts, only timeouts when using DMA
  1336. * if initial RX DMA job failed, start in interrupt mode
  1337. * as well.
  1338. */
  1339. spin_lock_irq(&uap->port.lock);
  1340. /* Clear out any spuriously appearing RX interrupts */
  1341. writew(UART011_RTIS | UART011_RXIS,
  1342. uap->port.membase + UART011_ICR);
  1343. uap->im = UART011_RTIM;
  1344. if (!pl011_dma_rx_running(uap))
  1345. uap->im |= UART011_RXIM;
  1346. writew(uap->im, uap->port.membase + UART011_IMSC);
  1347. spin_unlock_irq(&uap->port.lock);
  1348. return 0;
  1349. clk_dis:
  1350. clk_disable_unprepare(uap->clk);
  1351. return retval;
  1352. }
  1353. static void pl011_shutdown_channel(struct uart_amba_port *uap,
  1354. unsigned int lcrh)
  1355. {
  1356. unsigned long val;
  1357. val = readw(uap->port.membase + lcrh);
  1358. val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
  1359. writew(val, uap->port.membase + lcrh);
  1360. }
  1361. static void pl011_shutdown(struct uart_port *port)
  1362. {
  1363. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1364. unsigned int cr;
  1365. /*
  1366. * disable all interrupts
  1367. */
  1368. spin_lock_irq(&uap->port.lock);
  1369. uap->im = 0;
  1370. writew(uap->im, uap->port.membase + UART011_IMSC);
  1371. writew(0xffff, uap->port.membase + UART011_ICR);
  1372. spin_unlock_irq(&uap->port.lock);
  1373. pl011_dma_shutdown(uap);
  1374. /*
  1375. * Free the interrupt
  1376. */
  1377. free_irq(uap->port.irq, uap);
  1378. /*
  1379. * disable the port
  1380. * disable the port. It should not disable RTS and DTR.
  1381. * Also RTS and DTR state should be preserved to restore
  1382. * it during startup().
  1383. */
  1384. uap->autorts = false;
  1385. cr = readw(uap->port.membase + UART011_CR);
  1386. uap->old_cr = cr;
  1387. cr &= UART011_CR_RTS | UART011_CR_DTR;
  1388. cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
  1389. writew(cr, uap->port.membase + UART011_CR);
  1390. /*
  1391. * disable break condition and fifos
  1392. */
  1393. pl011_shutdown_channel(uap, uap->lcrh_rx);
  1394. if (uap->lcrh_rx != uap->lcrh_tx)
  1395. pl011_shutdown_channel(uap, uap->lcrh_tx);
  1396. /*
  1397. * Shut down the clock producer
  1398. */
  1399. clk_disable_unprepare(uap->clk);
  1400. /* Optionally let pins go into sleep states */
  1401. pinctrl_pm_select_sleep_state(port->dev);
  1402. if (dev_get_platdata(uap->port.dev)) {
  1403. struct amba_pl011_data *plat;
  1404. plat = dev_get_platdata(uap->port.dev);
  1405. if (plat->exit)
  1406. plat->exit();
  1407. }
  1408. }
  1409. static void
  1410. pl011_set_termios(struct uart_port *port, struct ktermios *termios,
  1411. struct ktermios *old)
  1412. {
  1413. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1414. unsigned int lcr_h, old_cr;
  1415. unsigned long flags;
  1416. unsigned int baud, quot, clkdiv;
  1417. if (uap->vendor->oversampling)
  1418. clkdiv = 8;
  1419. else
  1420. clkdiv = 16;
  1421. /*
  1422. * Ask the core to calculate the divisor for us.
  1423. */
  1424. baud = uart_get_baud_rate(port, termios, old, 0,
  1425. port->uartclk / clkdiv);
  1426. #ifdef CONFIG_DMA_ENGINE
  1427. /*
  1428. * Adjust RX DMA polling rate with baud rate if not specified.
  1429. */
  1430. if (uap->dmarx.auto_poll_rate)
  1431. uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
  1432. #endif
  1433. if (baud > port->uartclk/16)
  1434. quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
  1435. else
  1436. quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
  1437. switch (termios->c_cflag & CSIZE) {
  1438. case CS5:
  1439. lcr_h = UART01x_LCRH_WLEN_5;
  1440. break;
  1441. case CS6:
  1442. lcr_h = UART01x_LCRH_WLEN_6;
  1443. break;
  1444. case CS7:
  1445. lcr_h = UART01x_LCRH_WLEN_7;
  1446. break;
  1447. default: // CS8
  1448. lcr_h = UART01x_LCRH_WLEN_8;
  1449. break;
  1450. }
  1451. if (termios->c_cflag & CSTOPB)
  1452. lcr_h |= UART01x_LCRH_STP2;
  1453. if (termios->c_cflag & PARENB) {
  1454. lcr_h |= UART01x_LCRH_PEN;
  1455. if (!(termios->c_cflag & PARODD))
  1456. lcr_h |= UART01x_LCRH_EPS;
  1457. }
  1458. if (uap->fifosize > 1)
  1459. lcr_h |= UART01x_LCRH_FEN;
  1460. spin_lock_irqsave(&port->lock, flags);
  1461. /*
  1462. * Update the per-port timeout.
  1463. */
  1464. uart_update_timeout(port, termios->c_cflag, baud);
  1465. port->read_status_mask = UART011_DR_OE | 255;
  1466. if (termios->c_iflag & INPCK)
  1467. port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
  1468. if (termios->c_iflag & (BRKINT | PARMRK))
  1469. port->read_status_mask |= UART011_DR_BE;
  1470. /*
  1471. * Characters to ignore
  1472. */
  1473. port->ignore_status_mask = 0;
  1474. if (termios->c_iflag & IGNPAR)
  1475. port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
  1476. if (termios->c_iflag & IGNBRK) {
  1477. port->ignore_status_mask |= UART011_DR_BE;
  1478. /*
  1479. * If we're ignoring parity and break indicators,
  1480. * ignore overruns too (for real raw support).
  1481. */
  1482. if (termios->c_iflag & IGNPAR)
  1483. port->ignore_status_mask |= UART011_DR_OE;
  1484. }
  1485. /*
  1486. * Ignore all characters if CREAD is not set.
  1487. */
  1488. if ((termios->c_cflag & CREAD) == 0)
  1489. port->ignore_status_mask |= UART_DUMMY_DR_RX;
  1490. if (UART_ENABLE_MS(port, termios->c_cflag))
  1491. pl011_enable_ms(port);
  1492. /* first, disable everything */
  1493. old_cr = readw(port->membase + UART011_CR);
  1494. writew(0, port->membase + UART011_CR);
  1495. if (termios->c_cflag & CRTSCTS) {
  1496. if (old_cr & UART011_CR_RTS)
  1497. old_cr |= UART011_CR_RTSEN;
  1498. old_cr |= UART011_CR_CTSEN;
  1499. uap->autorts = true;
  1500. } else {
  1501. old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
  1502. uap->autorts = false;
  1503. }
  1504. if (uap->vendor->oversampling) {
  1505. if (baud > port->uartclk / 16)
  1506. old_cr |= ST_UART011_CR_OVSFACT;
  1507. else
  1508. old_cr &= ~ST_UART011_CR_OVSFACT;
  1509. }
  1510. /*
  1511. * Workaround for the ST Micro oversampling variants to
  1512. * increase the bitrate slightly, by lowering the divisor,
  1513. * to avoid delayed sampling of start bit at high speeds,
  1514. * else we see data corruption.
  1515. */
  1516. if (uap->vendor->oversampling) {
  1517. if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
  1518. quot -= 1;
  1519. else if ((baud > 3250000) && (quot > 2))
  1520. quot -= 2;
  1521. }
  1522. /* Set baud rate */
  1523. writew(quot & 0x3f, port->membase + UART011_FBRD);
  1524. writew(quot >> 6, port->membase + UART011_IBRD);
  1525. /*
  1526. * ----------v----------v----------v----------v-----
  1527. * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
  1528. * UART011_FBRD & UART011_IBRD.
  1529. * ----------^----------^----------^----------^-----
  1530. */
  1531. writew(lcr_h, port->membase + uap->lcrh_rx);
  1532. if (uap->lcrh_rx != uap->lcrh_tx) {
  1533. int i;
  1534. /*
  1535. * Wait 10 PCLKs before writing LCRH_TX register,
  1536. * to get this delay write read only register 10 times
  1537. */
  1538. for (i = 0; i < 10; ++i)
  1539. writew(0xff, uap->port.membase + UART011_MIS);
  1540. writew(lcr_h, port->membase + uap->lcrh_tx);
  1541. }
  1542. writew(old_cr, port->membase + UART011_CR);
  1543. spin_unlock_irqrestore(&port->lock, flags);
  1544. }
  1545. static const char *pl011_type(struct uart_port *port)
  1546. {
  1547. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1548. return uap->port.type == PORT_AMBA ? uap->type : NULL;
  1549. }
  1550. /*
  1551. * Release the memory region(s) being used by 'port'
  1552. */
  1553. static void pl011_release_port(struct uart_port *port)
  1554. {
  1555. release_mem_region(port->mapbase, SZ_4K);
  1556. }
  1557. /*
  1558. * Request the memory region(s) being used by 'port'
  1559. */
  1560. static int pl011_request_port(struct uart_port *port)
  1561. {
  1562. return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
  1563. != NULL ? 0 : -EBUSY;
  1564. }
  1565. /*
  1566. * Configure/autoconfigure the port.
  1567. */
  1568. static void pl011_config_port(struct uart_port *port, int flags)
  1569. {
  1570. if (flags & UART_CONFIG_TYPE) {
  1571. port->type = PORT_AMBA;
  1572. pl011_request_port(port);
  1573. }
  1574. }
  1575. /*
  1576. * verify the new serial_struct (for TIOCSSERIAL).
  1577. */
  1578. static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
  1579. {
  1580. int ret = 0;
  1581. if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
  1582. ret = -EINVAL;
  1583. if (ser->irq < 0 || ser->irq >= nr_irqs)
  1584. ret = -EINVAL;
  1585. if (ser->baud_base < 9600)
  1586. ret = -EINVAL;
  1587. return ret;
  1588. }
  1589. static struct uart_ops amba_pl011_pops = {
  1590. .tx_empty = pl011_tx_empty,
  1591. .set_mctrl = pl011_set_mctrl,
  1592. .get_mctrl = pl011_get_mctrl,
  1593. .stop_tx = pl011_stop_tx,
  1594. .start_tx = pl011_start_tx,
  1595. .stop_rx = pl011_stop_rx,
  1596. .enable_ms = pl011_enable_ms,
  1597. .break_ctl = pl011_break_ctl,
  1598. .startup = pl011_startup,
  1599. .shutdown = pl011_shutdown,
  1600. .flush_buffer = pl011_dma_flush_buffer,
  1601. .set_termios = pl011_set_termios,
  1602. .type = pl011_type,
  1603. .release_port = pl011_release_port,
  1604. .request_port = pl011_request_port,
  1605. .config_port = pl011_config_port,
  1606. .verify_port = pl011_verify_port,
  1607. #ifdef CONFIG_CONSOLE_POLL
  1608. .poll_init = pl011_hwinit,
  1609. .poll_get_char = pl011_get_poll_char,
  1610. .poll_put_char = pl011_put_poll_char,
  1611. #endif
  1612. };
  1613. static struct uart_amba_port *amba_ports[UART_NR];
  1614. #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
  1615. static void pl011_console_putchar(struct uart_port *port, int ch)
  1616. {
  1617. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1618. while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
  1619. barrier();
  1620. writew(ch, uap->port.membase + UART01x_DR);
  1621. }
  1622. static void
  1623. pl011_console_write(struct console *co, const char *s, unsigned int count)
  1624. {
  1625. struct uart_amba_port *uap = amba_ports[co->index];
  1626. unsigned int status, old_cr, new_cr;
  1627. unsigned long flags;
  1628. int locked = 1;
  1629. clk_enable(uap->clk);
  1630. local_irq_save(flags);
  1631. if (uap->port.sysrq)
  1632. locked = 0;
  1633. else if (oops_in_progress)
  1634. locked = spin_trylock(&uap->port.lock);
  1635. else
  1636. spin_lock(&uap->port.lock);
  1637. /*
  1638. * First save the CR then disable the interrupts
  1639. */
  1640. old_cr = readw(uap->port.membase + UART011_CR);
  1641. new_cr = old_cr & ~UART011_CR_CTSEN;
  1642. new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
  1643. writew(new_cr, uap->port.membase + UART011_CR);
  1644. uart_console_write(&uap->port, s, count, pl011_console_putchar);
  1645. /*
  1646. * Finally, wait for transmitter to become empty
  1647. * and restore the TCR
  1648. */
  1649. do {
  1650. status = readw(uap->port.membase + UART01x_FR);
  1651. } while (status & UART01x_FR_BUSY);
  1652. writew(old_cr, uap->port.membase + UART011_CR);
  1653. if (locked)
  1654. spin_unlock(&uap->port.lock);
  1655. local_irq_restore(flags);
  1656. clk_disable(uap->clk);
  1657. }
  1658. static void __init
  1659. pl011_console_get_options(struct uart_amba_port *uap, int *baud,
  1660. int *parity, int *bits)
  1661. {
  1662. if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
  1663. unsigned int lcr_h, ibrd, fbrd;
  1664. lcr_h = readw(uap->port.membase + uap->lcrh_tx);
  1665. *parity = 'n';
  1666. if (lcr_h & UART01x_LCRH_PEN) {
  1667. if (lcr_h & UART01x_LCRH_EPS)
  1668. *parity = 'e';
  1669. else
  1670. *parity = 'o';
  1671. }
  1672. if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
  1673. *bits = 7;
  1674. else
  1675. *bits = 8;
  1676. ibrd = readw(uap->port.membase + UART011_IBRD);
  1677. fbrd = readw(uap->port.membase + UART011_FBRD);
  1678. *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
  1679. if (uap->vendor->oversampling) {
  1680. if (readw(uap->port.membase + UART011_CR)
  1681. & ST_UART011_CR_OVSFACT)
  1682. *baud *= 2;
  1683. }
  1684. }
  1685. }
  1686. static int __init pl011_console_setup(struct console *co, char *options)
  1687. {
  1688. struct uart_amba_port *uap;
  1689. int baud = 38400;
  1690. int bits = 8;
  1691. int parity = 'n';
  1692. int flow = 'n';
  1693. int ret;
  1694. /*
  1695. * Check whether an invalid uart number has been specified, and
  1696. * if so, search for the first available port that does have
  1697. * console support.
  1698. */
  1699. if (co->index >= UART_NR)
  1700. co->index = 0;
  1701. uap = amba_ports[co->index];
  1702. if (!uap)
  1703. return -ENODEV;
  1704. /* Allow pins to be muxed in and configured */
  1705. pinctrl_pm_select_default_state(uap->port.dev);
  1706. ret = clk_prepare(uap->clk);
  1707. if (ret)
  1708. return ret;
  1709. if (dev_get_platdata(uap->port.dev)) {
  1710. struct amba_pl011_data *plat;
  1711. plat = dev_get_platdata(uap->port.dev);
  1712. if (plat->init)
  1713. plat->init();
  1714. }
  1715. uap->port.uartclk = clk_get_rate(uap->clk);
  1716. if (options)
  1717. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1718. else
  1719. pl011_console_get_options(uap, &baud, &parity, &bits);
  1720. return uart_set_options(&uap->port, co, baud, parity, bits, flow);
  1721. }
  1722. static struct uart_driver amba_reg;
  1723. static struct console amba_console = {
  1724. .name = "ttyAMA",
  1725. .write = pl011_console_write,
  1726. .device = uart_console_device,
  1727. .setup = pl011_console_setup,
  1728. .flags = CON_PRINTBUFFER,
  1729. .index = -1,
  1730. .data = &amba_reg,
  1731. };
  1732. #define AMBA_CONSOLE (&amba_console)
  1733. #else
  1734. #define AMBA_CONSOLE NULL
  1735. #endif
  1736. static struct uart_driver amba_reg = {
  1737. .owner = THIS_MODULE,
  1738. .driver_name = "ttyAMA",
  1739. .dev_name = "ttyAMA",
  1740. .major = SERIAL_AMBA_MAJOR,
  1741. .minor = SERIAL_AMBA_MINOR,
  1742. .nr = UART_NR,
  1743. .cons = AMBA_CONSOLE,
  1744. };
  1745. static int pl011_probe_dt_alias(int index, struct device *dev)
  1746. {
  1747. struct device_node *np;
  1748. static bool seen_dev_with_alias = false;
  1749. static bool seen_dev_without_alias = false;
  1750. int ret = index;
  1751. if (!IS_ENABLED(CONFIG_OF))
  1752. return ret;
  1753. np = dev->of_node;
  1754. if (!np)
  1755. return ret;
  1756. ret = of_alias_get_id(np, "serial");
  1757. if (IS_ERR_VALUE(ret)) {
  1758. seen_dev_without_alias = true;
  1759. ret = index;
  1760. } else {
  1761. seen_dev_with_alias = true;
  1762. if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
  1763. dev_warn(dev, "requested serial port %d not available.\n", ret);
  1764. ret = index;
  1765. }
  1766. }
  1767. if (seen_dev_with_alias && seen_dev_without_alias)
  1768. dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
  1769. return ret;
  1770. }
  1771. static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
  1772. {
  1773. struct uart_amba_port *uap;
  1774. struct vendor_data *vendor = id->data;
  1775. void __iomem *base;
  1776. int i, ret;
  1777. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  1778. if (amba_ports[i] == NULL)
  1779. break;
  1780. if (i == ARRAY_SIZE(amba_ports)) {
  1781. ret = -EBUSY;
  1782. goto out;
  1783. }
  1784. uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
  1785. GFP_KERNEL);
  1786. if (uap == NULL) {
  1787. ret = -ENOMEM;
  1788. goto out;
  1789. }
  1790. i = pl011_probe_dt_alias(i, &dev->dev);
  1791. base = devm_ioremap(&dev->dev, dev->res.start,
  1792. resource_size(&dev->res));
  1793. if (!base) {
  1794. ret = -ENOMEM;
  1795. goto out;
  1796. }
  1797. uap->clk = devm_clk_get(&dev->dev, NULL);
  1798. if (IS_ERR(uap->clk)) {
  1799. ret = PTR_ERR(uap->clk);
  1800. goto out;
  1801. }
  1802. uap->vendor = vendor;
  1803. uap->lcrh_rx = vendor->lcrh_rx;
  1804. uap->lcrh_tx = vendor->lcrh_tx;
  1805. uap->old_cr = 0;
  1806. uap->fifosize = vendor->get_fifosize(dev);
  1807. uap->port.dev = &dev->dev;
  1808. uap->port.mapbase = dev->res.start;
  1809. uap->port.membase = base;
  1810. uap->port.iotype = UPIO_MEM;
  1811. uap->port.irq = dev->irq[0];
  1812. uap->port.fifosize = uap->fifosize;
  1813. uap->port.ops = &amba_pl011_pops;
  1814. uap->port.flags = UPF_BOOT_AUTOCONF;
  1815. uap->port.line = i;
  1816. pl011_dma_probe(&dev->dev, uap);
  1817. /* Ensure interrupts from this UART are masked and cleared */
  1818. writew(0, uap->port.membase + UART011_IMSC);
  1819. writew(0xffff, uap->port.membase + UART011_ICR);
  1820. snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
  1821. amba_ports[i] = uap;
  1822. amba_set_drvdata(dev, uap);
  1823. ret = uart_add_one_port(&amba_reg, &uap->port);
  1824. if (ret) {
  1825. amba_set_drvdata(dev, NULL);
  1826. amba_ports[i] = NULL;
  1827. pl011_dma_remove(uap);
  1828. }
  1829. out:
  1830. return ret;
  1831. }
  1832. static int pl011_remove(struct amba_device *dev)
  1833. {
  1834. struct uart_amba_port *uap = amba_get_drvdata(dev);
  1835. int i;
  1836. amba_set_drvdata(dev, NULL);
  1837. uart_remove_one_port(&amba_reg, &uap->port);
  1838. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  1839. if (amba_ports[i] == uap)
  1840. amba_ports[i] = NULL;
  1841. pl011_dma_remove(uap);
  1842. return 0;
  1843. }
  1844. #ifdef CONFIG_PM
  1845. static int pl011_suspend(struct amba_device *dev, pm_message_t state)
  1846. {
  1847. struct uart_amba_port *uap = amba_get_drvdata(dev);
  1848. if (!uap)
  1849. return -EINVAL;
  1850. return uart_suspend_port(&amba_reg, &uap->port);
  1851. }
  1852. static int pl011_resume(struct amba_device *dev)
  1853. {
  1854. struct uart_amba_port *uap = amba_get_drvdata(dev);
  1855. if (!uap)
  1856. return -EINVAL;
  1857. return uart_resume_port(&amba_reg, &uap->port);
  1858. }
  1859. #endif
  1860. static struct amba_id pl011_ids[] = {
  1861. {
  1862. .id = 0x00041011,
  1863. .mask = 0x000fffff,
  1864. .data = &vendor_arm,
  1865. },
  1866. {
  1867. .id = 0x00380802,
  1868. .mask = 0x00ffffff,
  1869. .data = &vendor_st,
  1870. },
  1871. { 0, 0 },
  1872. };
  1873. MODULE_DEVICE_TABLE(amba, pl011_ids);
  1874. static struct amba_driver pl011_driver = {
  1875. .drv = {
  1876. .name = "uart-pl011",
  1877. },
  1878. .id_table = pl011_ids,
  1879. .probe = pl011_probe,
  1880. .remove = pl011_remove,
  1881. #ifdef CONFIG_PM
  1882. .suspend = pl011_suspend,
  1883. .resume = pl011_resume,
  1884. #endif
  1885. };
  1886. static int __init pl011_init(void)
  1887. {
  1888. int ret;
  1889. printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
  1890. ret = uart_register_driver(&amba_reg);
  1891. if (ret == 0) {
  1892. ret = amba_driver_register(&pl011_driver);
  1893. if (ret)
  1894. uart_unregister_driver(&amba_reg);
  1895. }
  1896. return ret;
  1897. }
  1898. static void __exit pl011_exit(void)
  1899. {
  1900. amba_driver_unregister(&pl011_driver);
  1901. uart_unregister_driver(&amba_reg);
  1902. }
  1903. /*
  1904. * While this can be a module, if builtin it's most likely the console
  1905. * So let's leave module_exit but move module_init to an earlier place
  1906. */
  1907. arch_initcall(pl011_init);
  1908. module_exit(pl011_exit);
  1909. MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
  1910. MODULE_DESCRIPTION("ARM AMBA serial port driver");
  1911. MODULE_LICENSE("GPL");