amba-pl011.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067
  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. 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. /* restore RTS and DTR */
  1193. cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
  1194. cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
  1195. writew(cr, uap->port.membase + UART011_CR);
  1196. /* Clear pending error interrupts */
  1197. writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
  1198. uap->port.membase + UART011_ICR);
  1199. /*
  1200. * initialise the old status of the modem signals
  1201. */
  1202. uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
  1203. /* Startup DMA */
  1204. pl011_dma_startup(uap);
  1205. /*
  1206. * Finally, enable interrupts, only timeouts when using DMA
  1207. * if initial RX DMA job failed, start in interrupt mode
  1208. * as well.
  1209. */
  1210. spin_lock_irq(&uap->port.lock);
  1211. uap->im = UART011_RTIM;
  1212. if (!pl011_dma_rx_running(uap))
  1213. uap->im |= UART011_RXIM;
  1214. writew(uap->im, uap->port.membase + UART011_IMSC);
  1215. spin_unlock_irq(&uap->port.lock);
  1216. if (uap->port.dev->platform_data) {
  1217. struct amba_pl011_data *plat;
  1218. plat = uap->port.dev->platform_data;
  1219. if (plat->init)
  1220. plat->init();
  1221. }
  1222. return 0;
  1223. clk_dis:
  1224. clk_disable(uap->clk);
  1225. clk_unprep:
  1226. clk_unprepare(uap->clk);
  1227. out:
  1228. return retval;
  1229. }
  1230. static void pl011_shutdown_channel(struct uart_amba_port *uap,
  1231. unsigned int lcrh)
  1232. {
  1233. unsigned long val;
  1234. val = readw(uap->port.membase + lcrh);
  1235. val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
  1236. writew(val, uap->port.membase + lcrh);
  1237. }
  1238. static void pl011_shutdown(struct uart_port *port)
  1239. {
  1240. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1241. unsigned int cr;
  1242. /*
  1243. * disable all interrupts
  1244. */
  1245. spin_lock_irq(&uap->port.lock);
  1246. uap->im = 0;
  1247. writew(uap->im, uap->port.membase + UART011_IMSC);
  1248. writew(0xffff, uap->port.membase + UART011_ICR);
  1249. spin_unlock_irq(&uap->port.lock);
  1250. pl011_dma_shutdown(uap);
  1251. /*
  1252. * Free the interrupt
  1253. */
  1254. free_irq(uap->port.irq, uap);
  1255. /*
  1256. * disable the port
  1257. * disable the port. It should not disable RTS and DTR.
  1258. * Also RTS and DTR state should be preserved to restore
  1259. * it during startup().
  1260. */
  1261. uap->autorts = false;
  1262. cr = readw(uap->port.membase + UART011_CR);
  1263. uap->old_cr = cr;
  1264. cr &= UART011_CR_RTS | UART011_CR_DTR;
  1265. cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
  1266. writew(cr, uap->port.membase + UART011_CR);
  1267. /*
  1268. * disable break condition and fifos
  1269. */
  1270. pl011_shutdown_channel(uap, uap->lcrh_rx);
  1271. if (uap->lcrh_rx != uap->lcrh_tx)
  1272. pl011_shutdown_channel(uap, uap->lcrh_tx);
  1273. /*
  1274. * Shut down the clock producer
  1275. */
  1276. clk_disable(uap->clk);
  1277. clk_unprepare(uap->clk);
  1278. if (uap->port.dev->platform_data) {
  1279. struct amba_pl011_data *plat;
  1280. plat = uap->port.dev->platform_data;
  1281. if (plat->exit)
  1282. plat->exit();
  1283. }
  1284. }
  1285. static void
  1286. pl011_set_termios(struct uart_port *port, struct ktermios *termios,
  1287. struct ktermios *old)
  1288. {
  1289. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1290. unsigned int lcr_h, old_cr;
  1291. unsigned long flags;
  1292. unsigned int baud, quot, clkdiv;
  1293. if (uap->vendor->oversampling)
  1294. clkdiv = 8;
  1295. else
  1296. clkdiv = 16;
  1297. /*
  1298. * Ask the core to calculate the divisor for us.
  1299. */
  1300. baud = uart_get_baud_rate(port, termios, old, 0,
  1301. port->uartclk / clkdiv);
  1302. if (baud > port->uartclk/16)
  1303. quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
  1304. else
  1305. quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
  1306. switch (termios->c_cflag & CSIZE) {
  1307. case CS5:
  1308. lcr_h = UART01x_LCRH_WLEN_5;
  1309. break;
  1310. case CS6:
  1311. lcr_h = UART01x_LCRH_WLEN_6;
  1312. break;
  1313. case CS7:
  1314. lcr_h = UART01x_LCRH_WLEN_7;
  1315. break;
  1316. default: // CS8
  1317. lcr_h = UART01x_LCRH_WLEN_8;
  1318. break;
  1319. }
  1320. if (termios->c_cflag & CSTOPB)
  1321. lcr_h |= UART01x_LCRH_STP2;
  1322. if (termios->c_cflag & PARENB) {
  1323. lcr_h |= UART01x_LCRH_PEN;
  1324. if (!(termios->c_cflag & PARODD))
  1325. lcr_h |= UART01x_LCRH_EPS;
  1326. }
  1327. if (uap->fifosize > 1)
  1328. lcr_h |= UART01x_LCRH_FEN;
  1329. spin_lock_irqsave(&port->lock, flags);
  1330. /*
  1331. * Update the per-port timeout.
  1332. */
  1333. uart_update_timeout(port, termios->c_cflag, baud);
  1334. port->read_status_mask = UART011_DR_OE | 255;
  1335. if (termios->c_iflag & INPCK)
  1336. port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
  1337. if (termios->c_iflag & (BRKINT | PARMRK))
  1338. port->read_status_mask |= UART011_DR_BE;
  1339. /*
  1340. * Characters to ignore
  1341. */
  1342. port->ignore_status_mask = 0;
  1343. if (termios->c_iflag & IGNPAR)
  1344. port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
  1345. if (termios->c_iflag & IGNBRK) {
  1346. port->ignore_status_mask |= UART011_DR_BE;
  1347. /*
  1348. * If we're ignoring parity and break indicators,
  1349. * ignore overruns too (for real raw support).
  1350. */
  1351. if (termios->c_iflag & IGNPAR)
  1352. port->ignore_status_mask |= UART011_DR_OE;
  1353. }
  1354. /*
  1355. * Ignore all characters if CREAD is not set.
  1356. */
  1357. if ((termios->c_cflag & CREAD) == 0)
  1358. port->ignore_status_mask |= UART_DUMMY_DR_RX;
  1359. if (UART_ENABLE_MS(port, termios->c_cflag))
  1360. pl011_enable_ms(port);
  1361. /* first, disable everything */
  1362. old_cr = readw(port->membase + UART011_CR);
  1363. writew(0, port->membase + UART011_CR);
  1364. if (termios->c_cflag & CRTSCTS) {
  1365. if (old_cr & UART011_CR_RTS)
  1366. old_cr |= UART011_CR_RTSEN;
  1367. old_cr |= UART011_CR_CTSEN;
  1368. uap->autorts = true;
  1369. } else {
  1370. old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
  1371. uap->autorts = false;
  1372. }
  1373. if (uap->vendor->oversampling) {
  1374. if (baud > port->uartclk / 16)
  1375. old_cr |= ST_UART011_CR_OVSFACT;
  1376. else
  1377. old_cr &= ~ST_UART011_CR_OVSFACT;
  1378. }
  1379. /* Set baud rate */
  1380. writew(quot & 0x3f, port->membase + UART011_FBRD);
  1381. writew(quot >> 6, port->membase + UART011_IBRD);
  1382. /*
  1383. * ----------v----------v----------v----------v-----
  1384. * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
  1385. * ----------^----------^----------^----------^-----
  1386. */
  1387. writew(lcr_h, port->membase + uap->lcrh_rx);
  1388. if (uap->lcrh_rx != uap->lcrh_tx) {
  1389. int i;
  1390. /*
  1391. * Wait 10 PCLKs before writing LCRH_TX register,
  1392. * to get this delay write read only register 10 times
  1393. */
  1394. for (i = 0; i < 10; ++i)
  1395. writew(0xff, uap->port.membase + UART011_MIS);
  1396. writew(lcr_h, port->membase + uap->lcrh_tx);
  1397. }
  1398. writew(old_cr, port->membase + UART011_CR);
  1399. spin_unlock_irqrestore(&port->lock, flags);
  1400. }
  1401. static const char *pl011_type(struct uart_port *port)
  1402. {
  1403. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1404. return uap->port.type == PORT_AMBA ? uap->type : NULL;
  1405. }
  1406. /*
  1407. * Release the memory region(s) being used by 'port'
  1408. */
  1409. static void pl010_release_port(struct uart_port *port)
  1410. {
  1411. release_mem_region(port->mapbase, SZ_4K);
  1412. }
  1413. /*
  1414. * Request the memory region(s) being used by 'port'
  1415. */
  1416. static int pl010_request_port(struct uart_port *port)
  1417. {
  1418. return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
  1419. != NULL ? 0 : -EBUSY;
  1420. }
  1421. /*
  1422. * Configure/autoconfigure the port.
  1423. */
  1424. static void pl010_config_port(struct uart_port *port, int flags)
  1425. {
  1426. if (flags & UART_CONFIG_TYPE) {
  1427. port->type = PORT_AMBA;
  1428. pl010_request_port(port);
  1429. }
  1430. }
  1431. /*
  1432. * verify the new serial_struct (for TIOCSSERIAL).
  1433. */
  1434. static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
  1435. {
  1436. int ret = 0;
  1437. if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
  1438. ret = -EINVAL;
  1439. if (ser->irq < 0 || ser->irq >= nr_irqs)
  1440. ret = -EINVAL;
  1441. if (ser->baud_base < 9600)
  1442. ret = -EINVAL;
  1443. return ret;
  1444. }
  1445. static struct uart_ops amba_pl011_pops = {
  1446. .tx_empty = pl01x_tx_empty,
  1447. .set_mctrl = pl011_set_mctrl,
  1448. .get_mctrl = pl01x_get_mctrl,
  1449. .stop_tx = pl011_stop_tx,
  1450. .start_tx = pl011_start_tx,
  1451. .stop_rx = pl011_stop_rx,
  1452. .enable_ms = pl011_enable_ms,
  1453. .break_ctl = pl011_break_ctl,
  1454. .startup = pl011_startup,
  1455. .shutdown = pl011_shutdown,
  1456. .flush_buffer = pl011_dma_flush_buffer,
  1457. .set_termios = pl011_set_termios,
  1458. .type = pl011_type,
  1459. .release_port = pl010_release_port,
  1460. .request_port = pl010_request_port,
  1461. .config_port = pl010_config_port,
  1462. .verify_port = pl010_verify_port,
  1463. #ifdef CONFIG_CONSOLE_POLL
  1464. .poll_get_char = pl010_get_poll_char,
  1465. .poll_put_char = pl010_put_poll_char,
  1466. #endif
  1467. };
  1468. static struct uart_amba_port *amba_ports[UART_NR];
  1469. #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
  1470. static void pl011_console_putchar(struct uart_port *port, int ch)
  1471. {
  1472. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1473. while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
  1474. barrier();
  1475. writew(ch, uap->port.membase + UART01x_DR);
  1476. }
  1477. static void
  1478. pl011_console_write(struct console *co, const char *s, unsigned int count)
  1479. {
  1480. struct uart_amba_port *uap = amba_ports[co->index];
  1481. unsigned int status, old_cr, new_cr;
  1482. unsigned long flags;
  1483. int locked = 1;
  1484. clk_enable(uap->clk);
  1485. local_irq_save(flags);
  1486. if (uap->port.sysrq)
  1487. locked = 0;
  1488. else if (oops_in_progress)
  1489. locked = spin_trylock(&uap->port.lock);
  1490. else
  1491. spin_lock(&uap->port.lock);
  1492. /*
  1493. * First save the CR then disable the interrupts
  1494. */
  1495. old_cr = readw(uap->port.membase + UART011_CR);
  1496. new_cr = old_cr & ~UART011_CR_CTSEN;
  1497. new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
  1498. writew(new_cr, uap->port.membase + UART011_CR);
  1499. uart_console_write(&uap->port, s, count, pl011_console_putchar);
  1500. /*
  1501. * Finally, wait for transmitter to become empty
  1502. * and restore the TCR
  1503. */
  1504. do {
  1505. status = readw(uap->port.membase + UART01x_FR);
  1506. } while (status & UART01x_FR_BUSY);
  1507. writew(old_cr, uap->port.membase + UART011_CR);
  1508. if (locked)
  1509. spin_unlock(&uap->port.lock);
  1510. local_irq_restore(flags);
  1511. clk_disable(uap->clk);
  1512. }
  1513. static void __init
  1514. pl011_console_get_options(struct uart_amba_port *uap, int *baud,
  1515. int *parity, int *bits)
  1516. {
  1517. if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
  1518. unsigned int lcr_h, ibrd, fbrd;
  1519. lcr_h = readw(uap->port.membase + uap->lcrh_tx);
  1520. *parity = 'n';
  1521. if (lcr_h & UART01x_LCRH_PEN) {
  1522. if (lcr_h & UART01x_LCRH_EPS)
  1523. *parity = 'e';
  1524. else
  1525. *parity = 'o';
  1526. }
  1527. if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
  1528. *bits = 7;
  1529. else
  1530. *bits = 8;
  1531. ibrd = readw(uap->port.membase + UART011_IBRD);
  1532. fbrd = readw(uap->port.membase + UART011_FBRD);
  1533. *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
  1534. if (uap->vendor->oversampling) {
  1535. if (readw(uap->port.membase + UART011_CR)
  1536. & ST_UART011_CR_OVSFACT)
  1537. *baud *= 2;
  1538. }
  1539. }
  1540. }
  1541. static int __init pl011_console_setup(struct console *co, char *options)
  1542. {
  1543. struct uart_amba_port *uap;
  1544. int baud = 38400;
  1545. int bits = 8;
  1546. int parity = 'n';
  1547. int flow = 'n';
  1548. int ret;
  1549. /*
  1550. * Check whether an invalid uart number has been specified, and
  1551. * if so, search for the first available port that does have
  1552. * console support.
  1553. */
  1554. if (co->index >= UART_NR)
  1555. co->index = 0;
  1556. uap = amba_ports[co->index];
  1557. if (!uap)
  1558. return -ENODEV;
  1559. ret = clk_prepare(uap->clk);
  1560. if (ret)
  1561. return ret;
  1562. if (uap->port.dev->platform_data) {
  1563. struct amba_pl011_data *plat;
  1564. plat = uap->port.dev->platform_data;
  1565. if (plat->init)
  1566. plat->init();
  1567. }
  1568. uap->port.uartclk = clk_get_rate(uap->clk);
  1569. if (options)
  1570. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1571. else
  1572. pl011_console_get_options(uap, &baud, &parity, &bits);
  1573. return uart_set_options(&uap->port, co, baud, parity, bits, flow);
  1574. }
  1575. static struct uart_driver amba_reg;
  1576. static struct console amba_console = {
  1577. .name = "ttyAMA",
  1578. .write = pl011_console_write,
  1579. .device = uart_console_device,
  1580. .setup = pl011_console_setup,
  1581. .flags = CON_PRINTBUFFER,
  1582. .index = -1,
  1583. .data = &amba_reg,
  1584. };
  1585. #define AMBA_CONSOLE (&amba_console)
  1586. #else
  1587. #define AMBA_CONSOLE NULL
  1588. #endif
  1589. static struct uart_driver amba_reg = {
  1590. .owner = THIS_MODULE,
  1591. .driver_name = "ttyAMA",
  1592. .dev_name = "ttyAMA",
  1593. .major = SERIAL_AMBA_MAJOR,
  1594. .minor = SERIAL_AMBA_MINOR,
  1595. .nr = UART_NR,
  1596. .cons = AMBA_CONSOLE,
  1597. };
  1598. static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
  1599. {
  1600. struct uart_amba_port *uap;
  1601. struct vendor_data *vendor = id->data;
  1602. void __iomem *base;
  1603. int i, ret;
  1604. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  1605. if (amba_ports[i] == NULL)
  1606. break;
  1607. if (i == ARRAY_SIZE(amba_ports)) {
  1608. ret = -EBUSY;
  1609. goto out;
  1610. }
  1611. uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
  1612. if (uap == NULL) {
  1613. ret = -ENOMEM;
  1614. goto out;
  1615. }
  1616. base = ioremap(dev->res.start, resource_size(&dev->res));
  1617. if (!base) {
  1618. ret = -ENOMEM;
  1619. goto free;
  1620. }
  1621. uap->clk = clk_get(&dev->dev, NULL);
  1622. if (IS_ERR(uap->clk)) {
  1623. ret = PTR_ERR(uap->clk);
  1624. goto unmap;
  1625. }
  1626. uap->vendor = vendor;
  1627. uap->lcrh_rx = vendor->lcrh_rx;
  1628. uap->lcrh_tx = vendor->lcrh_tx;
  1629. uap->old_cr = 0;
  1630. uap->fifosize = vendor->fifosize;
  1631. uap->interrupt_may_hang = vendor->interrupt_may_hang;
  1632. uap->port.dev = &dev->dev;
  1633. uap->port.mapbase = dev->res.start;
  1634. uap->port.membase = base;
  1635. uap->port.iotype = UPIO_MEM;
  1636. uap->port.irq = dev->irq[0];
  1637. uap->port.fifosize = uap->fifosize;
  1638. uap->port.ops = &amba_pl011_pops;
  1639. uap->port.flags = UPF_BOOT_AUTOCONF;
  1640. uap->port.line = i;
  1641. pl011_dma_probe(uap);
  1642. snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
  1643. amba_ports[i] = uap;
  1644. amba_set_drvdata(dev, uap);
  1645. ret = uart_add_one_port(&amba_reg, &uap->port);
  1646. if (ret) {
  1647. amba_set_drvdata(dev, NULL);
  1648. amba_ports[i] = NULL;
  1649. pl011_dma_remove(uap);
  1650. clk_put(uap->clk);
  1651. unmap:
  1652. iounmap(base);
  1653. free:
  1654. kfree(uap);
  1655. }
  1656. out:
  1657. return ret;
  1658. }
  1659. static int pl011_remove(struct amba_device *dev)
  1660. {
  1661. struct uart_amba_port *uap = amba_get_drvdata(dev);
  1662. int i;
  1663. amba_set_drvdata(dev, NULL);
  1664. uart_remove_one_port(&amba_reg, &uap->port);
  1665. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  1666. if (amba_ports[i] == uap)
  1667. amba_ports[i] = NULL;
  1668. pl011_dma_remove(uap);
  1669. iounmap(uap->port.membase);
  1670. clk_put(uap->clk);
  1671. kfree(uap);
  1672. return 0;
  1673. }
  1674. #ifdef CONFIG_PM
  1675. static int pl011_suspend(struct amba_device *dev, pm_message_t state)
  1676. {
  1677. struct uart_amba_port *uap = amba_get_drvdata(dev);
  1678. if (!uap)
  1679. return -EINVAL;
  1680. return uart_suspend_port(&amba_reg, &uap->port);
  1681. }
  1682. static int pl011_resume(struct amba_device *dev)
  1683. {
  1684. struct uart_amba_port *uap = amba_get_drvdata(dev);
  1685. if (!uap)
  1686. return -EINVAL;
  1687. return uart_resume_port(&amba_reg, &uap->port);
  1688. }
  1689. #endif
  1690. static struct amba_id pl011_ids[] = {
  1691. {
  1692. .id = 0x00041011,
  1693. .mask = 0x000fffff,
  1694. .data = &vendor_arm,
  1695. },
  1696. {
  1697. .id = 0x00380802,
  1698. .mask = 0x00ffffff,
  1699. .data = &vendor_st,
  1700. },
  1701. { 0, 0 },
  1702. };
  1703. MODULE_DEVICE_TABLE(amba, pl011_ids);
  1704. static struct amba_driver pl011_driver = {
  1705. .drv = {
  1706. .name = "uart-pl011",
  1707. },
  1708. .id_table = pl011_ids,
  1709. .probe = pl011_probe,
  1710. .remove = pl011_remove,
  1711. #ifdef CONFIG_PM
  1712. .suspend = pl011_suspend,
  1713. .resume = pl011_resume,
  1714. #endif
  1715. };
  1716. static int __init pl011_init(void)
  1717. {
  1718. int ret;
  1719. printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
  1720. ret = uart_register_driver(&amba_reg);
  1721. if (ret == 0) {
  1722. ret = amba_driver_register(&pl011_driver);
  1723. if (ret)
  1724. uart_unregister_driver(&amba_reg);
  1725. }
  1726. return ret;
  1727. }
  1728. static void __exit pl011_exit(void)
  1729. {
  1730. amba_driver_unregister(&pl011_driver);
  1731. uart_unregister_driver(&amba_reg);
  1732. }
  1733. /*
  1734. * While this can be a module, if builtin it's most likely the console
  1735. * So let's leave module_exit but move module_init to an earlier place
  1736. */
  1737. arch_initcall(pl011_init);
  1738. module_exit(pl011_exit);
  1739. MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
  1740. MODULE_DESCRIPTION("ARM AMBA serial port driver");
  1741. MODULE_LICENSE("GPL");