amba-pl011.c 59 KB

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