amba-pl011.c 60 KB

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