amba-pl011.c 58 KB

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