amba-pl011.c 51 KB

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