amba-pl011.c 50 KB

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