amba-pl011.c 52 KB

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