ark_uart.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733
  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_ARKMICRO_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. #include <linux/acpi.h>
  59. #include "amba-pl011.h"
  60. #define UART_NR 4
  61. #define SERIAL_ARK_MAJOR 4
  62. #define SERIAL_ARK_MINOR 64
  63. #define SERIAL_ARK_NR UART_NR
  64. #define AMBA_ISR_PASS_LIMIT 256
  65. #define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
  66. #define UART_DUMMY_DR_RX (1 << 16)
  67. const char uartx_name[][16]={
  68. "ark-uart0",
  69. "ark-uart1",
  70. "ark-uart2",
  71. "ark-uart3",
  72. };
  73. static u16 pl011_std_offsets[REG_ARRAY_SIZE] = {
  74. [REG_DR] = UART01x_DR,
  75. [REG_FR] = UART01x_FR,
  76. [REG_LCRH_RX] = UART011_LCRH,
  77. [REG_LCRH_TX] = UART011_LCRH,
  78. [REG_IBRD] = UART011_IBRD,
  79. [REG_FBRD] = UART011_FBRD,
  80. [REG_CR] = UART011_CR,
  81. [REG_IFLS] = UART011_IFLS,
  82. [REG_IMSC] = UART011_IMSC,
  83. [REG_RIS] = UART011_RIS,
  84. [REG_MIS] = UART011_MIS,
  85. [REG_ICR] = UART011_ICR,
  86. [REG_DMACR] = UART011_DMACR,
  87. };
  88. /* There is by now at least one vendor with differing details, so handle it */
  89. struct vendor_data {
  90. const u16 *reg_offset;
  91. unsigned int ifls;
  92. unsigned int fr_busy;
  93. unsigned int fr_dsr;
  94. unsigned int fr_cts;
  95. unsigned int fr_ri;
  96. unsigned int inv_fr;
  97. bool access_32b;
  98. bool oversampling;
  99. bool dma_threshold;
  100. bool cts_event_workaround;
  101. bool always_enabled;
  102. bool fixed_options;
  103. unsigned int (*get_fifosize)(struct amba_device *dev);
  104. };
  105. static struct vendor_data vendor_ark = {
  106. .reg_offset = pl011_std_offsets,
  107. .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
  108. .fr_busy = UART01x_FR_BUSY,
  109. .fr_dsr = UART01x_FR_DSR,
  110. .fr_cts = UART01x_FR_CTS,
  111. .fr_ri = UART011_FR_RI,
  112. .oversampling = false,
  113. .dma_threshold = false,
  114. .cts_event_workaround = false,
  115. .always_enabled = false,
  116. .fixed_options = false,
  117. };
  118. /* Deals with DMA transactions */
  119. struct pl011_sgbuf {
  120. struct scatterlist sg;
  121. char *buf;
  122. };
  123. struct pl011_dmarx_data {
  124. struct dma_chan *chan;
  125. struct completion complete;
  126. bool use_buf_b;
  127. struct pl011_sgbuf sgbuf_a;
  128. struct pl011_sgbuf sgbuf_b;
  129. dma_cookie_t cookie;
  130. bool running;
  131. struct timer_list timer;
  132. unsigned int last_residue;
  133. unsigned long last_jiffies;
  134. bool auto_poll_rate;
  135. unsigned int poll_rate;
  136. unsigned int poll_timeout;
  137. };
  138. struct pl011_dmatx_data {
  139. struct dma_chan *chan;
  140. struct scatterlist sg;
  141. char *buf;
  142. bool queued;
  143. };
  144. /*
  145. * We wrap our port structure around the generic uart_port.
  146. */
  147. struct uart_amba_port {
  148. struct uart_port port;
  149. const u16 *reg_offset;
  150. struct clk *clk;
  151. const struct vendor_data *vendor;
  152. unsigned int dmacr; /* dma control reg */
  153. unsigned int im; /* interrupt mask */
  154. unsigned int old_status;
  155. unsigned int fifosize; /* vendor-specific */
  156. unsigned int old_cr; /* state during shutdown */
  157. bool autorts;
  158. unsigned int fixed_baud; /* vendor-set fixed baud rate */
  159. char type[12];
  160. bool use_in_kernel;
  161. bool use_for_mcu;
  162. bool use_for_tool;
  163. #ifdef CONFIG_DMA_ENGINE
  164. /* DMA stuff */
  165. bool using_tx_dma;
  166. bool using_rx_dma;
  167. struct pl011_dmarx_data dmarx;
  168. struct pl011_dmatx_data dmatx;
  169. bool dma_probed;
  170. #endif
  171. };
  172. static bool enable_console = false;//false;
  173. static void (*mcu_serial_rev_handler)(unsigned char ch);
  174. static struct work_struct *mcu_serial_rev_task;
  175. static bool (*tool_serial_rev_handler)(char ch);
  176. static bool (*tool_serial_rev_enable_check)(char ch);
  177. static int __init enable_console_setup(char *str)
  178. {
  179. enable_console = true;
  180. printk(KERN_INFO "enable uart0 rx to open console.\n");
  181. return 0;
  182. }
  183. early_param("enable_console", enable_console_setup);
  184. static unsigned int pl011_reg_to_offset(const struct uart_amba_port *uap,
  185. unsigned int reg)
  186. {
  187. return uap->reg_offset[reg];
  188. }
  189. static unsigned int pl011_read(const struct uart_amba_port *uap,
  190. unsigned int reg)
  191. {
  192. void __iomem *addr = uap->port.membase + pl011_reg_to_offset(uap, reg);
  193. return (uap->port.iotype == UPIO_MEM32) ?
  194. readl_relaxed(addr) : readw_relaxed(addr);
  195. }
  196. static void pl011_write(unsigned int val, const struct uart_amba_port *uap,
  197. unsigned int reg)
  198. {
  199. void __iomem *addr = uap->port.membase + pl011_reg_to_offset(uap, reg);
  200. if (uap->port.iotype == UPIO_MEM32)
  201. writel_relaxed(val, addr);
  202. else
  203. writew_relaxed(val, addr);
  204. }
  205. /*
  206. * Reads up to 256 characters from the FIFO or until it's empty and
  207. * inserts them into the TTY layer. Returns the number of characters
  208. * read from the FIFO.
  209. */
  210. static int pl011_fifo_to_tty(struct uart_amba_port *uap)
  211. {
  212. u16 status;
  213. unsigned int ch, flag, max_count = 256;
  214. int fifotaken = 0;
  215. while (max_count--) {
  216. status = pl011_read(uap, REG_FR);
  217. if (status & UART01x_FR_RXFE)
  218. break;
  219. /* Take chars from the FIFO and update status */
  220. ch = pl011_read(uap, REG_DR) | UART_DUMMY_DR_RX;
  221. flag = TTY_NORMAL;
  222. uap->port.icount.rx++;
  223. fifotaken++;
  224. if (unlikely(ch & UART_DR_ERROR)) {
  225. if (ch & UART011_DR_BE) {
  226. ch &= ~(UART011_DR_FE | UART011_DR_PE);
  227. uap->port.icount.brk++;
  228. if (uart_handle_break(&uap->port))
  229. continue;
  230. } else if (ch & UART011_DR_PE)
  231. uap->port.icount.parity++;
  232. else if (ch & UART011_DR_FE)
  233. uap->port.icount.frame++;
  234. if (ch & UART011_DR_OE)
  235. uap->port.icount.overrun++;
  236. ch &= uap->port.read_status_mask;
  237. if (ch & UART011_DR_BE)
  238. flag = TTY_BREAK;
  239. else if (ch & UART011_DR_PE)
  240. flag = TTY_PARITY;
  241. else if (ch & UART011_DR_FE)
  242. flag = TTY_FRAME;
  243. }
  244. if (uart_handle_sysrq_char(&uap->port, ch & 255))
  245. continue;
  246. if (uap->use_for_mcu && mcu_serial_rev_handler) {
  247. mcu_serial_rev_handler(ch);
  248. }
  249. if (uap->use_for_tool && tool_serial_rev_handler && tool_serial_rev_enable_check){
  250. if(tool_serial_rev_enable_check(ch) && tool_serial_rev_handler(ch)){
  251. continue;
  252. }
  253. }
  254. uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
  255. }
  256. if (uap->use_for_mcu && mcu_serial_rev_task)
  257. schedule_work(mcu_serial_rev_task);
  258. return fifotaken;
  259. }
  260. /*
  261. * All the DMA operation mode stuff goes inside this ifdef.
  262. * This assumes that you have a generic DMA device interface,
  263. * no custom DMA interfaces are supported.
  264. */
  265. #ifdef CONFIG_DMA_ENGINE
  266. #define PL011_DMA_BUFFER_SIZE PAGE_SIZE
  267. static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
  268. enum dma_data_direction dir)
  269. {
  270. dma_addr_t dma_addr;
  271. sg->buf = dma_alloc_coherent(chan->device->dev,
  272. PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
  273. if (!sg->buf)
  274. return -ENOMEM;
  275. sg_init_table(&sg->sg, 1);
  276. sg_set_page(&sg->sg, phys_to_page(dma_addr),
  277. PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
  278. sg_dma_address(&sg->sg) = dma_addr;
  279. sg_dma_len(&sg->sg) = PL011_DMA_BUFFER_SIZE;
  280. return 0;
  281. }
  282. static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
  283. enum dma_data_direction dir)
  284. {
  285. if (sg->buf) {
  286. dma_free_coherent(chan->device->dev,
  287. PL011_DMA_BUFFER_SIZE, sg->buf,
  288. sg_dma_address(&sg->sg));
  289. }
  290. }
  291. static void pl011_dma_probe(struct uart_amba_port *uap)
  292. {
  293. /* DMA is the sole user of the platform data right now */
  294. struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
  295. struct device *dev = uap->port.dev;
  296. struct dma_slave_config tx_conf = {
  297. .dst_addr = uap->port.mapbase +
  298. pl011_reg_to_offset(uap, REG_DR),
  299. .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  300. .direction = DMA_MEM_TO_DEV,
  301. .dst_maxburst = uap->fifosize >> 1,
  302. .device_fc = false,
  303. };
  304. struct dma_chan *chan;
  305. dma_cap_mask_t mask;
  306. uap->dma_probed = true;
  307. chan = dma_request_slave_channel_reason(dev, "tx");
  308. if (IS_ERR(chan)) {
  309. if (PTR_ERR(chan) == -EPROBE_DEFER) {
  310. uap->dma_probed = false;
  311. return;
  312. }
  313. /* We need platform data */
  314. if (!plat || !plat->dma_filter) {
  315. dev_info(uap->port.dev, "no TX platform data\n");
  316. } else {
  317. /* Try to acquire a generic DMA engine slave TX channel */
  318. dma_cap_zero(mask);
  319. dma_cap_set(DMA_SLAVE, mask);
  320. chan = dma_request_channel(mask, plat->dma_filter,
  321. plat->dma_tx_param);
  322. if (!chan) {
  323. dev_err(uap->port.dev, "no TX DMA channel!\n");
  324. } else {
  325. dmaengine_slave_config(chan, &tx_conf);
  326. uap->dmatx.chan = chan;
  327. dev_info(uap->port.dev, "DMA channel TX %s\n",
  328. dma_chan_name(uap->dmatx.chan));
  329. }
  330. }
  331. } else {
  332. dmaengine_slave_config(chan, &tx_conf);
  333. uap->dmatx.chan = chan;
  334. dev_info(uap->port.dev, "DMA channel TX %s\n",
  335. dma_chan_name(uap->dmatx.chan));
  336. }
  337. /* Optionally make use of an RX channel as well */
  338. chan = dma_request_slave_channel(dev, "rx");
  339. if (!chan && plat && plat->dma_rx_param) {
  340. chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
  341. if (!chan) {
  342. dev_err(uap->port.dev, "no RX DMA channel!\n");
  343. return;
  344. }
  345. }
  346. if (chan) {
  347. struct dma_slave_config rx_conf = {
  348. .src_addr = uap->port.mapbase +
  349. pl011_reg_to_offset(uap, REG_DR),
  350. .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  351. .direction = DMA_DEV_TO_MEM,
  352. .src_maxburst = uap->fifosize >> 2,
  353. .device_fc = false,
  354. };
  355. struct dma_slave_caps caps;
  356. /*
  357. * Some DMA controllers provide information on their capabilities.
  358. * If the controller does, check for suitable residue processing
  359. * otherwise assime all is well.
  360. */
  361. if (0 == dma_get_slave_caps(chan, &caps)) {
  362. if (caps.residue_granularity ==
  363. DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
  364. dma_release_channel(chan);
  365. dev_info(uap->port.dev,
  366. "RX DMA disabled - no residue processing\n");
  367. return;
  368. }
  369. }
  370. dmaengine_slave_config(chan, &rx_conf);
  371. uap->dmarx.chan = chan;
  372. uap->dmarx.auto_poll_rate = false;
  373. if (plat && plat->dma_rx_poll_enable) {
  374. /* Set poll rate if specified. */
  375. if (plat->dma_rx_poll_rate) {
  376. uap->dmarx.auto_poll_rate = false;
  377. uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
  378. } else {
  379. /*
  380. * 100 ms defaults to poll rate if not
  381. * specified. This will be adjusted with
  382. * the baud rate at set_termios.
  383. */
  384. uap->dmarx.auto_poll_rate = true;
  385. uap->dmarx.poll_rate = 100;
  386. }
  387. /* 3 secs defaults poll_timeout if not specified. */
  388. if (plat->dma_rx_poll_timeout)
  389. uap->dmarx.poll_timeout =
  390. plat->dma_rx_poll_timeout;
  391. else
  392. uap->dmarx.poll_timeout = 3000;
  393. } else if (!plat && dev->of_node) {
  394. uap->dmarx.auto_poll_rate = of_property_read_bool(
  395. dev->of_node, "auto-poll");
  396. if (uap->dmarx.auto_poll_rate) {
  397. u32 x;
  398. if (0 == of_property_read_u32(dev->of_node,
  399. "poll-rate-ms", &x))
  400. uap->dmarx.poll_rate = x;
  401. else
  402. uap->dmarx.poll_rate = 100;
  403. if (0 == of_property_read_u32(dev->of_node,
  404. "poll-timeout-ms", &x))
  405. uap->dmarx.poll_timeout = x;
  406. else
  407. uap->dmarx.poll_timeout = 3000;
  408. }
  409. }
  410. dev_info(uap->port.dev, "DMA channel RX %s\n",
  411. dma_chan_name(uap->dmarx.chan));
  412. }
  413. }
  414. static void pl011_dma_remove(struct uart_amba_port *uap)
  415. {
  416. if (uap->dmatx.chan)
  417. dma_release_channel(uap->dmatx.chan);
  418. if (uap->dmarx.chan)
  419. dma_release_channel(uap->dmarx.chan);
  420. }
  421. /* Forward declare these for the refill routine */
  422. static int pl011_dma_tx_refill(struct uart_amba_port *uap);
  423. static void pl011_start_tx_pio(struct uart_amba_port *uap);
  424. /*
  425. * The current DMA TX buffer has been sent.
  426. * Try to queue up another DMA buffer.
  427. */
  428. static void pl011_dma_tx_callback(void *data)
  429. {
  430. struct uart_amba_port *uap = data;
  431. struct pl011_dmatx_data *dmatx = &uap->dmatx;
  432. unsigned long flags;
  433. u16 dmacr;
  434. printk("pl011_dma_tx_callback\n");
  435. spin_lock_irqsave(&uap->port.lock, flags);
  436. if (uap->dmatx.queued)
  437. dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
  438. DMA_TO_DEVICE);
  439. dmacr = uap->dmacr;
  440. uap->dmacr = dmacr & ~UART011_TXDMAE;
  441. pl011_write(uap->dmacr, uap, REG_DMACR);
  442. /*
  443. * If TX DMA was disabled, it means that we've stopped the DMA for
  444. * some reason (eg, XOFF received, or we want to send an X-char.)
  445. *
  446. * Note: we need to be careful here of a potential race between DMA
  447. * and the rest of the driver - if the driver disables TX DMA while
  448. * a TX buffer completing, we must update the tx queued status to
  449. * get further refills (hence we check dmacr).
  450. */
  451. if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
  452. uart_circ_empty(&uap->port.state->xmit)) {
  453. uap->dmatx.queued = false;
  454. spin_unlock_irqrestore(&uap->port.lock, flags);
  455. return;
  456. }
  457. if (pl011_dma_tx_refill(uap) <= 0)
  458. /*
  459. * We didn't queue a DMA buffer for some reason, but we
  460. * have data pending to be sent. Re-enable the TX IRQ.
  461. */
  462. pl011_start_tx_pio(uap);
  463. spin_unlock_irqrestore(&uap->port.lock, flags);
  464. }
  465. /*
  466. * Try to refill the TX DMA buffer.
  467. * Locking: called with port lock held and IRQs disabled.
  468. * Returns:
  469. * 1 if we queued up a TX DMA buffer.
  470. * 0 if we didn't want to handle this by DMA
  471. * <0 on error
  472. */
  473. static int pl011_dma_tx_refill(struct uart_amba_port *uap)
  474. {
  475. struct pl011_dmatx_data *dmatx = &uap->dmatx;
  476. struct dma_chan *chan = dmatx->chan;
  477. struct dma_device *dma_dev = chan->device;
  478. struct dma_async_tx_descriptor *desc;
  479. struct circ_buf *xmit = &uap->port.state->xmit;
  480. unsigned int count;
  481. /*
  482. * Try to avoid the overhead involved in using DMA if the
  483. * transaction fits in the first half of the FIFO, by using
  484. * the standard interrupt handling. This ensures that we
  485. * issue a uart_write_wakeup() at the appropriate time.
  486. */
  487. count = uart_circ_chars_pending(xmit);
  488. if (count < (uap->fifosize >> 1)) {
  489. uap->dmatx.queued = false;
  490. return 0;
  491. }
  492. /*
  493. * Bodge: don't send the last character by DMA, as this
  494. * will prevent XON from notifying us to restart DMA.
  495. */
  496. count -= 1;
  497. /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
  498. if (count > PL011_DMA_BUFFER_SIZE)
  499. count = PL011_DMA_BUFFER_SIZE;
  500. if (xmit->tail < xmit->head)
  501. memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
  502. else {
  503. size_t first = UART_XMIT_SIZE - xmit->tail;
  504. size_t second;
  505. if (first > count)
  506. first = count;
  507. second = count - first;
  508. memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
  509. if (second)
  510. memcpy(&dmatx->buf[first], &xmit->buf[0], second);
  511. }
  512. dmatx->sg.length = count;
  513. printk("dmatx->sg.length=%d.\n", count);
  514. if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
  515. uap->dmatx.queued = false;
  516. dev_dbg(uap->port.dev, "unable to map TX DMA\n");
  517. return -EBUSY;
  518. }
  519. desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
  520. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  521. if (!desc) {
  522. dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
  523. uap->dmatx.queued = false;
  524. /*
  525. * If DMA cannot be used right now, we complete this
  526. * transaction via IRQ and let the TTY layer retry.
  527. */
  528. dev_dbg(uap->port.dev, "TX DMA busy\n");
  529. return -EBUSY;
  530. }
  531. /* Some data to go along to the callback */
  532. desc->callback = pl011_dma_tx_callback;
  533. desc->callback_param = uap;
  534. /* All errors should happen at prepare time */
  535. dmaengine_submit(desc);
  536. /* Fire the DMA transaction */
  537. dma_dev->device_issue_pending(chan);
  538. uap->dmacr |= UART011_TXDMAE;
  539. pl011_write(uap->dmacr, uap, REG_DMACR);
  540. uap->dmatx.queued = true;
  541. /*
  542. * Now we know that DMA will fire, so advance the ring buffer
  543. * with the stuff we just dispatched.
  544. */
  545. xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
  546. uap->port.icount.tx += count;
  547. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  548. uart_write_wakeup(&uap->port);
  549. return 1;
  550. }
  551. /*
  552. * We received a transmit interrupt without a pending X-char but with
  553. * pending characters.
  554. * Locking: called with port lock held and IRQs disabled.
  555. * Returns:
  556. * false if we want to use PIO to transmit
  557. * true if we queued a DMA buffer
  558. */
  559. static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
  560. {
  561. if (!uap->using_tx_dma)
  562. return false;
  563. /*
  564. * If we already have a TX buffer queued, but received a
  565. * TX interrupt, it will be because we've just sent an X-char.
  566. * Ensure the TX DMA is enabled and the TX IRQ is disabled.
  567. */
  568. if (uap->dmatx.queued) {
  569. uap->dmacr |= UART011_TXDMAE;
  570. pl011_write(uap->dmacr, uap, REG_DMACR);
  571. uap->im &= ~UART011_TXIM;
  572. pl011_write(uap->im, uap, REG_IMSC);
  573. return true;
  574. }
  575. /*
  576. * We don't have a TX buffer queued, so try to queue one.
  577. * If we successfully queued a buffer, mask the TX IRQ.
  578. */
  579. if (pl011_dma_tx_refill(uap) > 0) {
  580. uap->im &= ~UART011_TXIM;
  581. pl011_write(uap->im, uap, REG_IMSC);
  582. return true;
  583. }
  584. return false;
  585. }
  586. /*
  587. * Stop the DMA transmit (eg, due to received XOFF).
  588. * Locking: called with port lock held and IRQs disabled.
  589. */
  590. static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
  591. {
  592. if (uap->dmatx.queued) {
  593. uap->dmacr &= ~UART011_TXDMAE;
  594. pl011_write(uap->dmacr, uap, REG_DMACR);
  595. }
  596. }
  597. /*
  598. * Try to start a DMA transmit, or in the case of an XON/OFF
  599. * character queued for send, try to get that character out ASAP.
  600. * Locking: called with port lock held and IRQs disabled.
  601. * Returns:
  602. * false if we want the TX IRQ to be enabled
  603. * true if we have a buffer queued
  604. */
  605. static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
  606. {
  607. u16 dmacr;
  608. if (!uap->using_tx_dma)
  609. return false;
  610. if (!uap->port.x_char) {
  611. /* no X-char, try to push chars out in DMA mode */
  612. bool ret = true;
  613. if (!uap->dmatx.queued) {
  614. if (pl011_dma_tx_refill(uap) > 0) {
  615. uap->im &= ~UART011_TXIM;
  616. pl011_write(uap->im, uap, REG_IMSC);
  617. } else
  618. ret = false;
  619. } else if (!(uap->dmacr & UART011_TXDMAE)) {
  620. uap->dmacr |= UART011_TXDMAE;
  621. pl011_write(uap->dmacr, uap, REG_DMACR);
  622. }
  623. return ret;
  624. }
  625. /*
  626. * We have an X-char to send. Disable DMA to prevent it loading
  627. * the TX fifo, and then see if we can stuff it into the FIFO.
  628. */
  629. dmacr = uap->dmacr;
  630. uap->dmacr &= ~UART011_TXDMAE;
  631. pl011_write(uap->dmacr, uap, REG_DMACR);
  632. if (pl011_read(uap, REG_FR) & UART01x_FR_TXFF) {
  633. /*
  634. * No space in the FIFO, so enable the transmit interrupt
  635. * so we know when there is space. Note that once we've
  636. * loaded the character, we should just re-enable DMA.
  637. */
  638. return false;
  639. }
  640. pl011_write(uap->port.x_char, uap, REG_DR);
  641. uap->port.icount.tx++;
  642. uap->port.x_char = 0;
  643. /* Success - restore the DMA state */
  644. uap->dmacr = dmacr;
  645. pl011_write(dmacr, uap, REG_DMACR);
  646. return true;
  647. }
  648. /*
  649. * Flush the transmit buffer.
  650. * Locking: called with port lock held and IRQs disabled.
  651. */
  652. static void pl011_dma_flush_buffer(struct uart_port *port)
  653. __releases(&uap->port.lock)
  654. __acquires(&uap->port.lock)
  655. {
  656. struct uart_amba_port *uap =
  657. container_of(port, struct uart_amba_port, port);
  658. if (!uap->using_tx_dma)
  659. return;
  660. /* Avoid deadlock with the DMA engine callback */
  661. spin_unlock(&uap->port.lock);
  662. dmaengine_terminate_all(uap->dmatx.chan);
  663. spin_lock(&uap->port.lock);
  664. if (uap->dmatx.queued) {
  665. dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
  666. DMA_TO_DEVICE);
  667. uap->dmatx.queued = false;
  668. uap->dmacr &= ~UART011_TXDMAE;
  669. pl011_write(uap->dmacr, uap, REG_DMACR);
  670. }
  671. }
  672. static void pl011_dma_rx_callback(void *data);
  673. static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
  674. {
  675. struct dma_chan *rxchan = uap->dmarx.chan;
  676. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  677. struct dma_async_tx_descriptor *desc;
  678. struct pl011_sgbuf *sgbuf;
  679. if (!rxchan)
  680. return -EIO;
  681. /* Start the RX DMA job */
  682. sgbuf = uap->dmarx.use_buf_b ?
  683. &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
  684. desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
  685. DMA_DEV_TO_MEM,
  686. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  687. /*
  688. * If the DMA engine is busy and cannot prepare a
  689. * channel, no big deal, the driver will fall back
  690. * to interrupt mode as a result of this error code.
  691. */
  692. if (!desc) {
  693. uap->dmarx.running = false;
  694. dmaengine_terminate_all(rxchan);
  695. return -EBUSY;
  696. }
  697. /* Some data to go along to the callback */
  698. desc->callback = pl011_dma_rx_callback;
  699. desc->callback_param = uap;
  700. dmarx->cookie = dmaengine_submit(desc);
  701. dma_async_issue_pending(rxchan);
  702. uap->dmacr |= UART011_RXDMAE;
  703. pl011_write(uap->dmacr, uap, REG_DMACR);
  704. uap->dmarx.running = true;
  705. uap->im &= ~UART011_RXIM;
  706. pl011_write(uap->im, uap, REG_IMSC);
  707. return 0;
  708. }
  709. /*
  710. * This is called when either the DMA job is complete, or
  711. * the FIFO timeout interrupt occurred. This must be called
  712. * with the port spinlock uap->port.lock held.
  713. */
  714. static void pl011_dma_rx_chars(struct uart_amba_port *uap,
  715. u32 pending, bool use_buf_b,
  716. bool readfifo)
  717. {
  718. struct tty_port *port = &uap->port.state->port;
  719. struct pl011_sgbuf *sgbuf = use_buf_b ?
  720. &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
  721. int dma_count = 0;
  722. u32 fifotaken = 0; /* only used for vdbg() */
  723. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  724. int dmataken = 0;
  725. if (uap->dmarx.poll_rate) {
  726. /* The data can be taken by polling */
  727. dmataken = sgbuf->sg.length - dmarx->last_residue;
  728. /* Recalculate the pending size */
  729. if (pending >= dmataken)
  730. pending -= dmataken;
  731. }
  732. /* Pick the remain data from the DMA */
  733. if (pending) {
  734. /*
  735. * First take all chars in the DMA pipe, then look in the FIFO.
  736. * Note that tty_insert_flip_buf() tries to take as many chars
  737. * as it can.
  738. */
  739. dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
  740. pending);
  741. uap->port.icount.rx += dma_count;
  742. if (dma_count < pending)
  743. dev_warn(uap->port.dev,
  744. "couldn't insert all characters (TTY is full?)\n");
  745. }
  746. /* Reset the last_residue for Rx DMA poll */
  747. if (uap->dmarx.poll_rate)
  748. dmarx->last_residue = sgbuf->sg.length;
  749. /*
  750. * Only continue with trying to read the FIFO if all DMA chars have
  751. * been taken first.
  752. */
  753. if (dma_count == pending && readfifo) {
  754. /* Clear any error flags */
  755. pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
  756. UART011_FEIS, uap, REG_ICR);
  757. /*
  758. * If we read all the DMA'd characters, and we had an
  759. * incomplete buffer, that could be due to an rx error, or
  760. * maybe we just timed out. Read any pending chars and check
  761. * the error status.
  762. *
  763. * Error conditions will only occur in the FIFO, these will
  764. * trigger an immediate interrupt and stop the DMA job, so we
  765. * will always find the error in the FIFO, never in the DMA
  766. * buffer.
  767. */
  768. fifotaken = pl011_fifo_to_tty(uap);
  769. }
  770. spin_unlock(&uap->port.lock);
  771. dev_vdbg(uap->port.dev,
  772. "Took %d chars from DMA buffer and %d chars from the FIFO\n",
  773. dma_count, fifotaken);
  774. tty_flip_buffer_push(port);
  775. spin_lock(&uap->port.lock);
  776. }
  777. static void pl011_dma_rx_irq(struct uart_amba_port *uap)
  778. {
  779. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  780. struct dma_chan *rxchan = dmarx->chan;
  781. struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
  782. &dmarx->sgbuf_b : &dmarx->sgbuf_a;
  783. size_t pending;
  784. struct dma_tx_state state;
  785. enum dma_status dmastat;
  786. /*
  787. * Pause the transfer so we can trust the current counter,
  788. * do this before we pause the PL011 block, else we may
  789. * overflow the FIFO.
  790. */
  791. if (dmaengine_pause(rxchan))
  792. dev_err(uap->port.dev, "unable to pause DMA transfer\n");
  793. dmastat = rxchan->device->device_tx_status(rxchan,
  794. dmarx->cookie, &state);
  795. if (dmastat != DMA_PAUSED)
  796. dev_err(uap->port.dev, "unable to pause DMA transfer\n");
  797. /* Disable RX DMA - incoming data will wait in the FIFO */
  798. uap->dmacr &= ~UART011_RXDMAE;
  799. pl011_write(uap->dmacr, uap, REG_DMACR);
  800. uap->dmarx.running = false;
  801. pending = sgbuf->sg.length - state.residue;
  802. BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
  803. /* Then we terminate the transfer - we now know our residue */
  804. dmaengine_terminate_all(rxchan);
  805. /*
  806. * This will take the chars we have so far and insert
  807. * into the framework.
  808. */
  809. pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
  810. /* Switch buffer & re-trigger DMA job */
  811. dmarx->use_buf_b = !dmarx->use_buf_b;
  812. if (pl011_dma_rx_trigger_dma(uap)) {
  813. dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
  814. "fall back to interrupt mode\n");
  815. uap->im |= UART011_RXIM;
  816. pl011_write(uap->im, uap, REG_IMSC);
  817. }
  818. }
  819. static void pl011_dma_rx_callback(void *data)
  820. {
  821. struct uart_amba_port *uap = data;
  822. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  823. struct dma_chan *rxchan = dmarx->chan;
  824. bool lastbuf = dmarx->use_buf_b;
  825. struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
  826. &dmarx->sgbuf_b : &dmarx->sgbuf_a;
  827. size_t pending;
  828. struct dma_tx_state state;
  829. int ret;
  830. /*
  831. * This completion interrupt occurs typically when the
  832. * RX buffer is totally stuffed but no timeout has yet
  833. * occurred. When that happens, we just want the RX
  834. * routine to flush out the secondary DMA buffer while
  835. * we immediately trigger the next DMA job.
  836. */
  837. spin_lock_irq(&uap->port.lock);
  838. /*
  839. * Rx data can be taken by the UART interrupts during
  840. * the DMA irq handler. So we check the residue here.
  841. */
  842. rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
  843. pending = sgbuf->sg.length - state.residue;
  844. BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
  845. /* Then we terminate the transfer - we now know our residue */
  846. dmaengine_terminate_all(rxchan);
  847. uap->dmarx.running = false;
  848. dmarx->use_buf_b = !lastbuf;
  849. ret = pl011_dma_rx_trigger_dma(uap);
  850. pl011_dma_rx_chars(uap, pending, lastbuf, false);
  851. spin_unlock_irq(&uap->port.lock);
  852. /*
  853. * Do this check after we picked the DMA chars so we don't
  854. * get some IRQ immediately from RX.
  855. */
  856. if (ret) {
  857. dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
  858. "fall back to interrupt mode\n");
  859. uap->im |= UART011_RXIM;
  860. pl011_write(uap->im, uap, REG_IMSC);
  861. }
  862. }
  863. /*
  864. * Stop accepting received characters, when we're shutting down or
  865. * suspending this port.
  866. * Locking: called with port lock held and IRQs disabled.
  867. */
  868. static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
  869. {
  870. /* FIXME. Just disable the DMA enable */
  871. uap->dmacr &= ~UART011_RXDMAE;
  872. pl011_write(uap->dmacr, uap, REG_DMACR);
  873. }
  874. /*
  875. * Timer handler for Rx DMA polling.
  876. * Every polling, It checks the residue in the dma buffer and transfer
  877. * data to the tty. Also, last_residue is updated for the next polling.
  878. */
  879. static void pl011_dma_rx_poll(struct timer_list *t)
  880. {
  881. struct uart_amba_port *uap = from_timer(uap, t, dmarx.timer);
  882. struct tty_port *port = &uap->port.state->port;
  883. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  884. struct dma_chan *rxchan = uap->dmarx.chan;
  885. unsigned long flags = 0;
  886. unsigned int dmataken = 0;
  887. unsigned int size = 0;
  888. struct pl011_sgbuf *sgbuf;
  889. int dma_count;
  890. struct dma_tx_state state;
  891. sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
  892. rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
  893. if (likely(state.residue < dmarx->last_residue)) {
  894. dmataken = sgbuf->sg.length - dmarx->last_residue;
  895. size = dmarx->last_residue - state.residue;
  896. dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
  897. size);
  898. if (dma_count == size)
  899. dmarx->last_residue = state.residue;
  900. dmarx->last_jiffies = jiffies;
  901. }
  902. tty_flip_buffer_push(port);
  903. /*
  904. * If no data is received in poll_timeout, the driver will fall back
  905. * to interrupt mode. We will retrigger DMA at the first interrupt.
  906. */
  907. if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
  908. > uap->dmarx.poll_timeout) {
  909. spin_lock_irqsave(&uap->port.lock, flags);
  910. pl011_dma_rx_stop(uap);
  911. uap->im |= UART011_RXIM;
  912. pl011_write(uap->im, uap, REG_IMSC);
  913. spin_unlock_irqrestore(&uap->port.lock, flags);
  914. uap->dmarx.running = false;
  915. dmaengine_terminate_all(rxchan);
  916. del_timer(&uap->dmarx.timer);
  917. } else {
  918. mod_timer(&uap->dmarx.timer,
  919. jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
  920. }
  921. }
  922. static void pl011_dma_startup(struct uart_amba_port *uap)
  923. {
  924. int ret;
  925. if (!uap->dma_probed)
  926. pl011_dma_probe(uap);
  927. if (uap->dmatx.chan) {
  928. uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
  929. if (!uap->dmatx.buf) {
  930. dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
  931. uap->port.fifosize = uap->fifosize;
  932. } else {
  933. sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
  934. /* The DMA buffer is now the FIFO the TTY subsystem can use */
  935. uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
  936. uap->using_tx_dma = true;
  937. }
  938. }
  939. if (!uap->dmarx.chan)
  940. goto skip_rx;
  941. /* Allocate and map DMA RX buffers */
  942. ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
  943. DMA_FROM_DEVICE);
  944. if (ret) {
  945. dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
  946. "RX buffer A", ret);
  947. goto skip_rx;
  948. }
  949. ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
  950. DMA_FROM_DEVICE);
  951. if (ret) {
  952. dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
  953. "RX buffer B", ret);
  954. pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
  955. DMA_FROM_DEVICE);
  956. goto skip_rx;
  957. }
  958. uap->using_rx_dma = true;
  959. skip_rx:
  960. /* Turn on DMA error (RX/TX will be enabled on demand) */
  961. uap->dmacr |= UART011_DMAONERR;
  962. pl011_write(uap->dmacr, uap, REG_DMACR);
  963. /*
  964. * ST Micro variants has some specific dma burst threshold
  965. * compensation. Set this to 16 bytes, so burst will only
  966. * be issued above/below 16 bytes.
  967. */
  968. if (uap->vendor->dma_threshold)
  969. pl011_write(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
  970. uap, REG_ST_DMAWM);
  971. if (uap->using_rx_dma) {
  972. if (pl011_dma_rx_trigger_dma(uap))
  973. dev_dbg(uap->port.dev, "could not trigger initial "
  974. "RX DMA job, fall back to interrupt mode\n");
  975. if (uap->dmarx.poll_rate) {
  976. timer_setup(&uap->dmarx.timer, pl011_dma_rx_poll, 0);
  977. mod_timer(&uap->dmarx.timer,
  978. jiffies +
  979. msecs_to_jiffies(uap->dmarx.poll_rate));
  980. uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
  981. uap->dmarx.last_jiffies = jiffies;
  982. }
  983. }
  984. }
  985. static void pl011_dma_shutdown(struct uart_amba_port *uap)
  986. {
  987. if (!(uap->using_tx_dma || uap->using_rx_dma))
  988. return;
  989. /* Disable RX and TX DMA */
  990. while (pl011_read(uap, REG_FR) & uap->vendor->fr_busy)
  991. cpu_relax();
  992. spin_lock_irq(&uap->port.lock);
  993. uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
  994. pl011_write(uap->dmacr, uap, REG_DMACR);
  995. spin_unlock_irq(&uap->port.lock);
  996. if (uap->using_tx_dma) {
  997. /* In theory, this should already be done by pl011_dma_flush_buffer */
  998. dmaengine_terminate_all(uap->dmatx.chan);
  999. if (uap->dmatx.queued) {
  1000. dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
  1001. DMA_TO_DEVICE);
  1002. uap->dmatx.queued = false;
  1003. }
  1004. kfree(uap->dmatx.buf);
  1005. uap->using_tx_dma = false;
  1006. }
  1007. if (uap->using_rx_dma) {
  1008. dmaengine_terminate_all(uap->dmarx.chan);
  1009. /* Clean up the RX DMA */
  1010. pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
  1011. pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
  1012. if (uap->dmarx.poll_rate)
  1013. del_timer_sync(&uap->dmarx.timer);
  1014. uap->using_rx_dma = false;
  1015. }
  1016. }
  1017. static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
  1018. {
  1019. return uap->using_rx_dma;
  1020. }
  1021. static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
  1022. {
  1023. return uap->using_rx_dma && uap->dmarx.running;
  1024. }
  1025. #else
  1026. /* Blank functions if the DMA engine is not available */
  1027. static inline void pl011_dma_probe(struct uart_amba_port *uap)
  1028. {
  1029. }
  1030. static inline void pl011_dma_remove(struct uart_amba_port *uap)
  1031. {
  1032. }
  1033. static inline void pl011_dma_startup(struct uart_amba_port *uap)
  1034. {
  1035. }
  1036. static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
  1037. {
  1038. }
  1039. static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
  1040. {
  1041. return false;
  1042. }
  1043. static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
  1044. {
  1045. }
  1046. static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
  1047. {
  1048. return false;
  1049. }
  1050. static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
  1051. {
  1052. }
  1053. static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
  1054. {
  1055. }
  1056. static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
  1057. {
  1058. return -EIO;
  1059. }
  1060. static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
  1061. {
  1062. return false;
  1063. }
  1064. static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
  1065. {
  1066. return false;
  1067. }
  1068. #define pl011_dma_flush_buffer NULL
  1069. #endif
  1070. static void pl011_stop_tx(struct uart_port *port)
  1071. {
  1072. struct uart_amba_port *uap =
  1073. container_of(port, struct uart_amba_port, port);
  1074. uap->im &= ~UART011_TXIM;
  1075. pl011_write(uap->im, uap, REG_IMSC);
  1076. pl011_dma_tx_stop(uap);
  1077. }
  1078. static bool pl011_tx_chars(struct uart_amba_port *uap, bool from_irq);
  1079. /* Start TX with programmed I/O only (no DMA) */
  1080. static void pl011_start_tx_pio(struct uart_amba_port *uap)
  1081. {
  1082. if (pl011_tx_chars(uap, false)) {
  1083. uap->im |= UART011_TXIM;
  1084. pl011_write(uap->im, uap, REG_IMSC);
  1085. }
  1086. }
  1087. static void pl011_start_tx(struct uart_port *port)
  1088. {
  1089. struct uart_amba_port *uap =
  1090. container_of(port, struct uart_amba_port, port);
  1091. if (!pl011_dma_tx_start(uap))
  1092. pl011_start_tx_pio(uap);
  1093. }
  1094. static void pl011_stop_rx(struct uart_port *port)
  1095. {
  1096. struct uart_amba_port *uap =
  1097. container_of(port, struct uart_amba_port, port);
  1098. if (uap->use_in_kernel)
  1099. return;
  1100. uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
  1101. UART011_PEIM|UART011_BEIM|UART011_OEIM);
  1102. pl011_write(uap->im, uap, REG_IMSC);
  1103. pl011_dma_rx_stop(uap);
  1104. }
  1105. static void pl011_enable_ms(struct uart_port *port)
  1106. {
  1107. struct uart_amba_port *uap =
  1108. container_of(port, struct uart_amba_port, port);
  1109. uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
  1110. pl011_write(uap->im, uap, REG_IMSC);
  1111. }
  1112. static void pl011_rx_chars(struct uart_amba_port *uap)
  1113. __releases(&uap->port.lock)
  1114. __acquires(&uap->port.lock)
  1115. {
  1116. pl011_fifo_to_tty(uap);
  1117. spin_unlock(&uap->port.lock);
  1118. tty_flip_buffer_push(&uap->port.state->port);
  1119. /*
  1120. * If we were temporarily out of DMA mode for a while,
  1121. * attempt to switch back to DMA mode again.
  1122. */
  1123. if (pl011_dma_rx_available(uap)) {
  1124. if (pl011_dma_rx_trigger_dma(uap)) {
  1125. dev_dbg(uap->port.dev, "could not trigger RX DMA job "
  1126. "fall back to interrupt mode again\n");
  1127. uap->im |= UART011_RXIM;
  1128. pl011_write(uap->im, uap, REG_IMSC);
  1129. } else {
  1130. #ifdef CONFIG_DMA_ENGINE
  1131. /* Start Rx DMA poll */
  1132. if (uap->dmarx.poll_rate) {
  1133. uap->dmarx.last_jiffies = jiffies;
  1134. uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
  1135. mod_timer(&uap->dmarx.timer,
  1136. jiffies +
  1137. msecs_to_jiffies(uap->dmarx.poll_rate));
  1138. }
  1139. #endif
  1140. }
  1141. }
  1142. spin_lock(&uap->port.lock);
  1143. }
  1144. static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c,
  1145. bool from_irq)
  1146. {
  1147. if (unlikely(!from_irq) &&
  1148. pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
  1149. return false; /* unable to transmit character */
  1150. pl011_write(c, uap, REG_DR);
  1151. uap->port.icount.tx++;
  1152. return true;
  1153. }
  1154. /* Returns true if tx interrupts have to be (kept) enabled */
  1155. static bool pl011_tx_chars(struct uart_amba_port *uap, bool from_irq)
  1156. {
  1157. struct circ_buf *xmit = &uap->port.state->xmit;
  1158. int count = uap->fifosize >> 1;
  1159. if (uap->port.x_char) {
  1160. if (!pl011_tx_char(uap, uap->port.x_char, from_irq))
  1161. return true;
  1162. uap->port.x_char = 0;
  1163. --count;
  1164. }
  1165. if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
  1166. pl011_stop_tx(&uap->port);
  1167. return false;
  1168. }
  1169. /* If we are using DMA mode, try to send some characters. */
  1170. if (pl011_dma_tx_irq(uap))
  1171. return true;
  1172. do {
  1173. if (likely(from_irq) && count-- == 0)
  1174. break;
  1175. if (!pl011_tx_char(uap, xmit->buf[xmit->tail], from_irq))
  1176. break;
  1177. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  1178. } while (!uart_circ_empty(xmit));
  1179. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  1180. uart_write_wakeup(&uap->port);
  1181. if (uart_circ_empty(xmit)) {
  1182. pl011_stop_tx(&uap->port);
  1183. return false;
  1184. }
  1185. return true;
  1186. }
  1187. static void pl011_modem_status(struct uart_amba_port *uap)
  1188. {
  1189. unsigned int status, delta;
  1190. status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
  1191. delta = status ^ uap->old_status;
  1192. uap->old_status = status;
  1193. if (!delta)
  1194. return;
  1195. if (delta & UART01x_FR_DCD)
  1196. uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
  1197. if (delta & uap->vendor->fr_dsr)
  1198. uap->port.icount.dsr++;
  1199. if (delta & uap->vendor->fr_cts)
  1200. uart_handle_cts_change(&uap->port,
  1201. status & uap->vendor->fr_cts);
  1202. wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
  1203. }
  1204. static void check_apply_cts_event_workaround(struct uart_amba_port *uap)
  1205. {
  1206. unsigned int dummy_read;
  1207. if (!uap->vendor->cts_event_workaround)
  1208. return;
  1209. /* workaround to make sure that all bits are unlocked.. */
  1210. pl011_write(0x00, uap, REG_ICR);
  1211. /*
  1212. * WA: introduce 26ns(1 uart clk) delay before W1C;
  1213. * single apb access will incur 2 pclk(133.12Mhz) delay,
  1214. * so add 2 dummy reads
  1215. */
  1216. dummy_read = pl011_read(uap, REG_ICR);
  1217. dummy_read = pl011_read(uap, REG_ICR);
  1218. }
  1219. static irqreturn_t pl011_int(int irq, void *dev_id)
  1220. {
  1221. struct uart_amba_port *uap = dev_id;
  1222. unsigned long flags;
  1223. unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
  1224. u16 imsc;
  1225. int handled = 0;
  1226. spin_lock_irqsave(&uap->port.lock, flags);
  1227. imsc = pl011_read(uap, REG_IMSC);
  1228. status = pl011_read(uap, REG_RIS) & imsc;
  1229. if (status) {
  1230. do {
  1231. check_apply_cts_event_workaround(uap);
  1232. pl011_write(status & ~(UART011_TXIS|UART011_RTIS|
  1233. UART011_RXIS),
  1234. uap, REG_ICR);
  1235. if (status & (UART011_RTIS|UART011_RXIS)) {
  1236. if (pl011_dma_rx_running(uap)) {
  1237. pl011_dma_rx_irq(uap);
  1238. }
  1239. else
  1240. pl011_rx_chars(uap);
  1241. }
  1242. if (status & (UART011_DSRMIS|UART011_DCDMIS|
  1243. UART011_CTSMIS|UART011_RIMIS))
  1244. pl011_modem_status(uap);
  1245. if (status & UART011_TXIS)
  1246. pl011_tx_chars(uap, true);
  1247. if (pass_counter-- == 0)
  1248. break;
  1249. status = pl011_read(uap, REG_RIS) & imsc;
  1250. } while (status != 0);
  1251. handled = 1;
  1252. }
  1253. spin_unlock_irqrestore(&uap->port.lock, flags);
  1254. return IRQ_RETVAL(handled);
  1255. }
  1256. static unsigned int pl011_tx_empty(struct uart_port *port)
  1257. {
  1258. struct uart_amba_port *uap =
  1259. container_of(port, struct uart_amba_port, port);
  1260. /* Allow feature register bits to be inverted to work around errata */
  1261. unsigned int status = pl011_read(uap, REG_FR) ^ uap->vendor->inv_fr;
  1262. return status & (uap->vendor->fr_busy | UART01x_FR_TXFF) ?
  1263. 0 : TIOCSER_TEMT;
  1264. }
  1265. static unsigned int pl011_get_mctrl(struct uart_port *port)
  1266. {
  1267. struct uart_amba_port *uap =
  1268. container_of(port, struct uart_amba_port, port);
  1269. unsigned int result = 0;
  1270. unsigned int status = pl011_read(uap, REG_FR);
  1271. #define TIOCMBIT(uartbit, tiocmbit) \
  1272. if (status & uartbit) \
  1273. result |= tiocmbit
  1274. TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
  1275. TIOCMBIT(uap->vendor->fr_dsr, TIOCM_DSR);
  1276. TIOCMBIT(uap->vendor->fr_cts, TIOCM_CTS);
  1277. TIOCMBIT(uap->vendor->fr_ri, TIOCM_RNG);
  1278. #undef TIOCMBIT
  1279. return result;
  1280. }
  1281. static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1282. {
  1283. struct uart_amba_port *uap =
  1284. container_of(port, struct uart_amba_port, port);
  1285. unsigned int cr;
  1286. if (uap->use_in_kernel)
  1287. return;
  1288. cr = pl011_read(uap, REG_CR);
  1289. #define TIOCMBIT(tiocmbit, uartbit) \
  1290. if (mctrl & tiocmbit) \
  1291. cr |= uartbit; \
  1292. else \
  1293. cr &= ~uartbit
  1294. TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
  1295. TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
  1296. TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
  1297. TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
  1298. TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
  1299. if (uap->autorts) {
  1300. /* We need to disable auto-RTS if we want to turn RTS off */
  1301. TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
  1302. }
  1303. #undef TIOCMBIT
  1304. pl011_write(cr, uap, REG_CR);
  1305. }
  1306. static void pl011_break_ctl(struct uart_port *port, int break_state)
  1307. {
  1308. struct uart_amba_port *uap =
  1309. container_of(port, struct uart_amba_port, port);
  1310. unsigned long flags;
  1311. unsigned int lcr_h;
  1312. spin_lock_irqsave(&uap->port.lock, flags);
  1313. lcr_h = pl011_read(uap, REG_LCRH_TX);
  1314. if (break_state == -1)
  1315. lcr_h |= UART01x_LCRH_BRK;
  1316. else
  1317. lcr_h &= ~UART01x_LCRH_BRK;
  1318. pl011_write(lcr_h, uap, REG_LCRH_TX);
  1319. spin_unlock_irqrestore(&uap->port.lock, flags);
  1320. }
  1321. #ifdef CONFIG_CONSOLE_POLL
  1322. static void pl011_quiesce_irqs(struct uart_port *port)
  1323. {
  1324. struct uart_amba_port *uap =
  1325. container_of(port, struct uart_amba_port, port);
  1326. pl011_write(pl011_read(uap, REG_MIS), uap, REG_ICR);
  1327. /*
  1328. * There is no way to clear TXIM as this is "ready to transmit IRQ", so
  1329. * we simply mask it. start_tx() will unmask it.
  1330. *
  1331. * Note we can race with start_tx(), and if the race happens, the
  1332. * polling user might get another interrupt just after we clear it.
  1333. * But it should be OK and can happen even w/o the race, e.g.
  1334. * controller immediately got some new data and raised the IRQ.
  1335. *
  1336. * And whoever uses polling routines assumes that it manages the device
  1337. * (including tx queue), so we're also fine with start_tx()'s caller
  1338. * side.
  1339. */
  1340. pl011_write(pl011_read(uap, REG_IMSC) & ~UART011_TXIM, uap,
  1341. REG_IMSC);
  1342. }
  1343. static int pl011_get_poll_char(struct uart_port *port)
  1344. {
  1345. struct uart_amba_port *uap =
  1346. container_of(port, struct uart_amba_port, port);
  1347. unsigned int status;
  1348. /*
  1349. * The caller might need IRQs lowered, e.g. if used with KDB NMI
  1350. * debugger.
  1351. */
  1352. pl011_quiesce_irqs(port);
  1353. status = pl011_read(uap, REG_FR);
  1354. if (status & UART01x_FR_RXFE)
  1355. return NO_POLL_CHAR;
  1356. return pl011_read(uap, REG_DR);
  1357. }
  1358. static void pl011_put_poll_char(struct uart_port *port,
  1359. unsigned char ch)
  1360. {
  1361. struct uart_amba_port *uap =
  1362. container_of(port, struct uart_amba_port, port);
  1363. while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
  1364. cpu_relax();
  1365. pl011_write(ch, uap, REG_DR);
  1366. }
  1367. #endif /* CONFIG_CONSOLE_POLL */
  1368. static int pl011_hwinit(struct uart_port *port)
  1369. {
  1370. struct uart_amba_port *uap =
  1371. container_of(port, struct uart_amba_port, port);
  1372. int retval;
  1373. /* Optionaly enable pins to be muxed in and configured */
  1374. pinctrl_pm_select_default_state(port->dev);
  1375. /*
  1376. * Try to enable the clock producer.
  1377. */
  1378. retval = clk_prepare_enable(uap->clk);
  1379. if (retval)
  1380. return retval;
  1381. uap->port.uartclk = clk_get_rate(uap->clk);
  1382. /* Clear pending error and receive interrupts */
  1383. pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
  1384. UART011_FEIS | UART011_RTIS | UART011_RXIS,
  1385. uap, REG_ICR);
  1386. /*
  1387. * Save interrupts enable mask, and enable RX interrupts in case if
  1388. * the interrupt is used for NMI entry.
  1389. */
  1390. uap->im = pl011_read(uap, REG_IMSC);
  1391. pl011_write(UART011_RTIM | UART011_RXIM, uap, REG_IMSC);
  1392. if (dev_get_platdata(uap->port.dev)) {
  1393. struct amba_pl011_data *plat;
  1394. plat = dev_get_platdata(uap->port.dev);
  1395. if (plat->init)
  1396. plat->init();
  1397. }
  1398. return 0;
  1399. }
  1400. static bool pl011_split_lcrh(const struct uart_amba_port *uap)
  1401. {
  1402. return pl011_reg_to_offset(uap, REG_LCRH_RX) !=
  1403. pl011_reg_to_offset(uap, REG_LCRH_TX);
  1404. }
  1405. static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
  1406. {
  1407. pl011_write(lcr_h, uap, REG_LCRH_RX);
  1408. if (pl011_split_lcrh(uap)) {
  1409. int i;
  1410. /*
  1411. * Wait 10 PCLKs before writing LCRH_TX register,
  1412. * to get this delay write read only register 10 times
  1413. */
  1414. for (i = 0; i < 10; ++i)
  1415. pl011_write(0xff, uap, REG_MIS);
  1416. pl011_write(lcr_h, uap, REG_LCRH_TX);
  1417. }
  1418. }
  1419. static int pl011_allocate_irq(struct uart_amba_port *uap)
  1420. {
  1421. pl011_write(uap->im, uap, REG_IMSC);
  1422. // return request_irq(uap->port.irq, pl011_int, 0, uartx_name[uap->port.line], uap);
  1423. return request_irq(uap->port.irq, pl011_int, IRQF_SHARED, uartx_name[uap->port.line], uap);
  1424. }
  1425. /*
  1426. * Enable interrupts, only timeouts when using DMA
  1427. * if initial RX DMA job failed, start in interrupt mode
  1428. * as well.
  1429. */
  1430. static void pl011_enable_interrupts(struct uart_amba_port *uap)
  1431. {
  1432. unsigned int i;
  1433. spin_lock_irq(&uap->port.lock);
  1434. /* Clear out any spuriously appearing RX interrupts */
  1435. pl011_write(UART011_RTIS | UART011_RXIS, uap, REG_ICR);
  1436. /*
  1437. * RXIS is asserted only when the RX FIFO transitions from below
  1438. * to above the trigger threshold. If the RX FIFO is already
  1439. * full to the threshold this can't happen and RXIS will now be
  1440. * stuck off. Drain the RX FIFO explicitly to fix this:
  1441. */
  1442. for (i = 0; i < uap->fifosize * 2; ++i) {
  1443. if (pl011_read(uap, REG_FR) & UART01x_FR_RXFE)
  1444. break;
  1445. pl011_read(uap, REG_DR);
  1446. }
  1447. if (uap->port.line > 0 || enable_console) {
  1448. uap->im = UART011_RTIM;
  1449. if (!pl011_dma_rx_running(uap))
  1450. uap->im |= UART011_RXIM;
  1451. }
  1452. pl011_write(uap->im, uap, REG_IMSC);
  1453. spin_unlock_irq(&uap->port.lock);
  1454. }
  1455. static int pl011_startup(struct uart_port *port)
  1456. {
  1457. struct uart_amba_port *uap =
  1458. container_of(port, struct uart_amba_port, port);
  1459. unsigned int cr;
  1460. int retval;
  1461. if (uap->use_in_kernel) {
  1462. if (pl011_read(uap, REG_CR) & UART01x_CR_UARTEN) {
  1463. dev_dbg(uap->port.dev, "already startup in early init.\n");
  1464. return 0;
  1465. }
  1466. }
  1467. retval = pl011_hwinit(port);
  1468. if (retval)
  1469. goto clk_dis;
  1470. retval = pl011_allocate_irq(uap);
  1471. if (retval)
  1472. goto clk_dis;
  1473. pl011_write(uap->vendor->ifls, uap, REG_IFLS);
  1474. spin_lock_irq(&uap->port.lock);
  1475. /* restore RTS and DTR */
  1476. cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
  1477. cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
  1478. pl011_write(cr, uap, REG_CR);
  1479. spin_unlock_irq(&uap->port.lock);
  1480. /*
  1481. * initialise the old status of the modem signals
  1482. */
  1483. uap->old_status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
  1484. /* Startup DMA */
  1485. if (!uap->use_in_kernel)
  1486. pl011_dma_startup(uap);
  1487. pl011_enable_interrupts(uap);
  1488. return 0;
  1489. clk_dis:
  1490. clk_disable_unprepare(uap->clk);
  1491. return retval;
  1492. }
  1493. static void pl011_shutdown_channel(struct uart_amba_port *uap,
  1494. unsigned int lcrh)
  1495. {
  1496. unsigned long val;
  1497. val = pl011_read(uap, lcrh);
  1498. val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
  1499. pl011_write(val, uap, lcrh);
  1500. }
  1501. /*
  1502. * disable the port. It should not disable RTS and DTR.
  1503. * Also RTS and DTR state should be preserved to restore
  1504. * it during startup().
  1505. */
  1506. static void pl011_disable_uart(struct uart_amba_port *uap)
  1507. {
  1508. unsigned int cr;
  1509. uap->autorts = false;
  1510. spin_lock_irq(&uap->port.lock);
  1511. cr = pl011_read(uap, REG_CR);
  1512. uap->old_cr = cr;
  1513. cr &= UART011_CR_RTS | UART011_CR_DTR;
  1514. cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
  1515. pl011_write(cr, uap, REG_CR);
  1516. spin_unlock_irq(&uap->port.lock);
  1517. /*
  1518. * disable break condition and fifos
  1519. */
  1520. pl011_shutdown_channel(uap, REG_LCRH_RX);
  1521. if (pl011_split_lcrh(uap))
  1522. pl011_shutdown_channel(uap, REG_LCRH_TX);
  1523. }
  1524. static void pl011_disable_interrupts(struct uart_amba_port *uap)
  1525. {
  1526. spin_lock_irq(&uap->port.lock);
  1527. /* mask all interrupts and clear all pending ones */
  1528. uap->im = 0;
  1529. pl011_write(uap->im, uap, REG_IMSC);
  1530. pl011_write(0xffff, uap, REG_ICR);
  1531. spin_unlock_irq(&uap->port.lock);
  1532. }
  1533. static void pl011_shutdown(struct uart_port *port)
  1534. {
  1535. struct uart_amba_port *uap =
  1536. container_of(port, struct uart_amba_port, port);
  1537. if (uap->use_in_kernel)
  1538. return;
  1539. pl011_disable_interrupts(uap);
  1540. pl011_dma_shutdown(uap);
  1541. free_irq(uap->port.irq, uap);
  1542. pl011_disable_uart(uap);
  1543. /*
  1544. * Shut down the clock producer
  1545. */
  1546. clk_disable_unprepare(uap->clk);
  1547. /* Optionally let pins go into sleep states */
  1548. pinctrl_pm_select_sleep_state(port->dev);
  1549. if (dev_get_platdata(uap->port.dev)) {
  1550. struct amba_pl011_data *plat;
  1551. plat = dev_get_platdata(uap->port.dev);
  1552. if (plat->exit)
  1553. plat->exit();
  1554. }
  1555. if (uap->port.ops->flush_buffer)
  1556. uap->port.ops->flush_buffer(port);
  1557. }
  1558. static void
  1559. pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios)
  1560. {
  1561. port->read_status_mask = UART011_DR_OE | 255;
  1562. if (termios->c_iflag & INPCK)
  1563. port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
  1564. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  1565. port->read_status_mask |= UART011_DR_BE;
  1566. /*
  1567. * Characters to ignore
  1568. */
  1569. port->ignore_status_mask = 0;
  1570. if (termios->c_iflag & IGNPAR)
  1571. port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
  1572. if (termios->c_iflag & IGNBRK) {
  1573. port->ignore_status_mask |= UART011_DR_BE;
  1574. /*
  1575. * If we're ignoring parity and break indicators,
  1576. * ignore overruns too (for real raw support).
  1577. */
  1578. if (termios->c_iflag & IGNPAR)
  1579. port->ignore_status_mask |= UART011_DR_OE;
  1580. }
  1581. /*
  1582. * Ignore all characters if CREAD is not set.
  1583. */
  1584. if ((termios->c_cflag & CREAD) == 0)
  1585. port->ignore_status_mask |= UART_DUMMY_DR_RX;
  1586. }
  1587. static void
  1588. pl011_set_termios(struct uart_port *port, struct ktermios *termios,
  1589. struct ktermios *old)
  1590. {
  1591. struct uart_amba_port *uap =
  1592. container_of(port, struct uart_amba_port, port);
  1593. unsigned int lcr_h, old_cr;
  1594. unsigned long flags;
  1595. unsigned int baud, quot, clkdiv;
  1596. if (uap->use_in_kernel)
  1597. return;
  1598. if (uap->vendor->oversampling)
  1599. clkdiv = 8;
  1600. else
  1601. clkdiv = 16;
  1602. /*
  1603. * Ask the core to calculate the divisor for us.
  1604. */
  1605. baud = uart_get_baud_rate(port, termios, old, 0,
  1606. port->uartclk / clkdiv);
  1607. #ifdef CONFIG_DMA_ENGINE
  1608. /*
  1609. * Adjust RX DMA polling rate with baud rate if not specified.
  1610. */
  1611. if (uap->dmarx.auto_poll_rate)
  1612. uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
  1613. #endif
  1614. if (baud > port->uartclk/16)
  1615. quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
  1616. else
  1617. quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
  1618. switch (termios->c_cflag & CSIZE) {
  1619. case CS5:
  1620. lcr_h = UART01x_LCRH_WLEN_5;
  1621. break;
  1622. case CS6:
  1623. lcr_h = UART01x_LCRH_WLEN_6;
  1624. break;
  1625. case CS7:
  1626. lcr_h = UART01x_LCRH_WLEN_7;
  1627. break;
  1628. default: // CS8
  1629. lcr_h = UART01x_LCRH_WLEN_8;
  1630. break;
  1631. }
  1632. if (termios->c_cflag & CSTOPB)
  1633. lcr_h |= UART01x_LCRH_STP2;
  1634. if (termios->c_cflag & PARENB) {
  1635. lcr_h |= UART01x_LCRH_PEN;
  1636. if (!(termios->c_cflag & PARODD))
  1637. lcr_h |= UART01x_LCRH_EPS;
  1638. if (termios->c_cflag & CMSPAR)
  1639. lcr_h |= UART011_LCRH_SPS;
  1640. }
  1641. if (uap->fifosize > 1)
  1642. lcr_h |= UART01x_LCRH_FEN;
  1643. spin_lock_irqsave(&port->lock, flags);
  1644. /*
  1645. * Update the per-port timeout.
  1646. */
  1647. uart_update_timeout(port, termios->c_cflag, baud);
  1648. pl011_setup_status_masks(port, termios);
  1649. if (UART_ENABLE_MS(port, termios->c_cflag))
  1650. pl011_enable_ms(port);
  1651. /* first, disable everything */
  1652. old_cr = pl011_read(uap, REG_CR);
  1653. pl011_write(0, uap, REG_CR);
  1654. if (termios->c_cflag & CRTSCTS) {
  1655. if (old_cr & UART011_CR_RTS)
  1656. old_cr |= UART011_CR_RTSEN;
  1657. old_cr |= UART011_CR_CTSEN;
  1658. uap->autorts = true;
  1659. } else {
  1660. old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
  1661. uap->autorts = false;
  1662. }
  1663. if (uap->vendor->oversampling) {
  1664. if (baud > port->uartclk / 16)
  1665. old_cr |= ST_UART011_CR_OVSFACT;
  1666. else
  1667. old_cr &= ~ST_UART011_CR_OVSFACT;
  1668. }
  1669. /*
  1670. * Workaround for the ST Micro oversampling variants to
  1671. * increase the bitrate slightly, by lowering the divisor,
  1672. * to avoid delayed sampling of start bit at high speeds,
  1673. * else we see data corruption.
  1674. */
  1675. if (uap->vendor->oversampling) {
  1676. if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
  1677. quot -= 1;
  1678. else if ((baud > 3250000) && (quot > 2))
  1679. quot -= 2;
  1680. }
  1681. /* Set baud rate */
  1682. pl011_write(quot & 0x3f, uap, REG_FBRD);
  1683. pl011_write(quot >> 6, uap, REG_IBRD);
  1684. /*
  1685. * ----------v----------v----------v----------v-----
  1686. * NOTE: REG_LCRH_TX and REG_LCRH_RX MUST BE WRITTEN AFTER
  1687. * REG_FBRD & REG_IBRD.
  1688. * ----------^----------^----------^----------^-----
  1689. */
  1690. pl011_write_lcr_h(uap, lcr_h);
  1691. pl011_write(old_cr, uap, REG_CR);
  1692. spin_unlock_irqrestore(&port->lock, flags);
  1693. }
  1694. static const char *pl011_type(struct uart_port *port)
  1695. {
  1696. struct uart_amba_port *uap =
  1697. container_of(port, struct uart_amba_port, port);
  1698. return uap->port.type == PORT_AMBA ? uap->type : NULL;
  1699. }
  1700. /*
  1701. * Release the memory region(s) being used by 'port'
  1702. */
  1703. static void pl011_release_port(struct uart_port *port)
  1704. {
  1705. release_mem_region(port->mapbase, SZ_4K);
  1706. }
  1707. /*
  1708. * Request the memory region(s) being used by 'port'
  1709. */
  1710. static int pl011_request_port(struct uart_port *port)
  1711. {
  1712. return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
  1713. != NULL ? 0 : -EBUSY;
  1714. }
  1715. /*
  1716. * Configure/autoconfigure the port.
  1717. */
  1718. static void pl011_config_port(struct uart_port *port, int flags)
  1719. {
  1720. if (flags & UART_CONFIG_TYPE) {
  1721. port->type = PORT_AMBA;
  1722. pl011_request_port(port);
  1723. }
  1724. }
  1725. /*
  1726. * verify the new serial_struct (for TIOCSSERIAL).
  1727. */
  1728. static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
  1729. {
  1730. int ret = 0;
  1731. if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
  1732. ret = -EINVAL;
  1733. if (ser->irq < 0 || ser->irq >= nr_irqs)
  1734. ret = -EINVAL;
  1735. if (ser->baud_base < 9600)
  1736. ret = -EINVAL;
  1737. return ret;
  1738. }
  1739. static const struct uart_ops amba_pl011_pops = {
  1740. .tx_empty = pl011_tx_empty,
  1741. .set_mctrl = pl011_set_mctrl,
  1742. .get_mctrl = pl011_get_mctrl,
  1743. .stop_tx = pl011_stop_tx,
  1744. .start_tx = pl011_start_tx,
  1745. .stop_rx = pl011_stop_rx,
  1746. .enable_ms = pl011_enable_ms,
  1747. .break_ctl = pl011_break_ctl,
  1748. .startup = pl011_startup,
  1749. .shutdown = pl011_shutdown,
  1750. .flush_buffer = pl011_dma_flush_buffer,
  1751. .set_termios = pl011_set_termios,
  1752. .type = pl011_type,
  1753. .release_port = pl011_release_port,
  1754. .request_port = pl011_request_port,
  1755. .config_port = pl011_config_port,
  1756. .verify_port = pl011_verify_port,
  1757. #ifdef CONFIG_CONSOLE_POLL
  1758. .poll_init = pl011_hwinit,
  1759. .poll_get_char = pl011_get_poll_char,
  1760. .poll_put_char = pl011_put_poll_char,
  1761. #endif
  1762. };
  1763. static struct uart_amba_port *amba_ports[UART_NR];
  1764. #ifdef CONFIG_SERIAL_ARKMICRO_CONSOLE
  1765. static void pl011_console_putchar(struct uart_port *port, int ch)
  1766. {
  1767. struct uart_amba_port *uap =
  1768. container_of(port, struct uart_amba_port, port);
  1769. while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
  1770. cpu_relax();
  1771. pl011_write(ch, uap, REG_DR);
  1772. }
  1773. static void
  1774. pl011_console_write(struct console *co, const char *s, unsigned int count)
  1775. {
  1776. struct uart_amba_port *uap = amba_ports[co->index];
  1777. unsigned int old_cr = 0, new_cr;
  1778. unsigned long flags;
  1779. int locked = 1;
  1780. clk_enable(uap->clk);
  1781. local_irq_save(flags);
  1782. if (uap->port.sysrq)
  1783. locked = 0;
  1784. else if (oops_in_progress)
  1785. locked = spin_trylock(&uap->port.lock);
  1786. else
  1787. spin_lock(&uap->port.lock);
  1788. /*
  1789. * First save the CR then disable the interrupts
  1790. */
  1791. if (!uap->vendor->always_enabled) {
  1792. old_cr = pl011_read(uap, REG_CR);
  1793. new_cr = old_cr & ~UART011_CR_CTSEN;
  1794. new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
  1795. pl011_write(new_cr, uap, REG_CR);
  1796. }
  1797. uart_console_write(&uap->port, s, count, pl011_console_putchar);
  1798. /*
  1799. * Finally, wait for transmitter to become empty and restore the
  1800. * TCR. Allow feature register bits to be inverted to work around
  1801. * errata.
  1802. */
  1803. while ((pl011_read(uap, REG_FR) ^ uap->vendor->inv_fr)
  1804. & uap->vendor->fr_busy)
  1805. cpu_relax();
  1806. if (!uap->vendor->always_enabled)
  1807. pl011_write(old_cr, uap, REG_CR);
  1808. if (locked)
  1809. spin_unlock(&uap->port.lock);
  1810. local_irq_restore(flags);
  1811. clk_disable(uap->clk);
  1812. }
  1813. static void __init
  1814. pl011_console_get_options(struct uart_amba_port *uap, int *baud,
  1815. int *parity, int *bits)
  1816. {
  1817. if (pl011_read(uap, REG_CR) & UART01x_CR_UARTEN) {
  1818. unsigned int lcr_h, ibrd, fbrd;
  1819. lcr_h = pl011_read(uap, REG_LCRH_TX);
  1820. *parity = 'n';
  1821. if (lcr_h & UART01x_LCRH_PEN) {
  1822. if (lcr_h & UART01x_LCRH_EPS)
  1823. *parity = 'e';
  1824. else
  1825. *parity = 'o';
  1826. }
  1827. if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
  1828. *bits = 7;
  1829. else
  1830. *bits = 8;
  1831. ibrd = pl011_read(uap, REG_IBRD);
  1832. fbrd = pl011_read(uap, REG_FBRD);
  1833. *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
  1834. if (uap->vendor->oversampling) {
  1835. if (pl011_read(uap, REG_CR)
  1836. & ST_UART011_CR_OVSFACT)
  1837. *baud *= 2;
  1838. }
  1839. }
  1840. }
  1841. static int __init pl011_console_setup(struct console *co, char *options)
  1842. {
  1843. struct uart_amba_port *uap;
  1844. int baud = 38400;
  1845. int bits = 8;
  1846. int parity = 'n';
  1847. int flow = 'n';
  1848. int ret;
  1849. /*
  1850. * Check whether an invalid uart number has been specified, and
  1851. * if so, search for the first available port that does have
  1852. * console support.
  1853. */
  1854. if (co->index >= UART_NR)
  1855. co->index = 0;
  1856. uap = amba_ports[co->index];
  1857. if (!uap)
  1858. return -ENODEV;
  1859. /* Allow pins to be muxed in and configured */
  1860. pinctrl_pm_select_default_state(uap->port.dev);
  1861. ret = clk_prepare(uap->clk);
  1862. if (ret)
  1863. return ret;
  1864. if (dev_get_platdata(uap->port.dev)) {
  1865. struct amba_pl011_data *plat;
  1866. plat = dev_get_platdata(uap->port.dev);
  1867. if (plat->init)
  1868. plat->init();
  1869. }
  1870. uap->port.uartclk = clk_get_rate(uap->clk);
  1871. if (uap->vendor->fixed_options) {
  1872. baud = uap->fixed_baud;
  1873. } else {
  1874. if (options)
  1875. uart_parse_options(options,
  1876. &baud, &parity, &bits, &flow);
  1877. else
  1878. pl011_console_get_options(uap, &baud, &parity, &bits);
  1879. }
  1880. return uart_set_options(&uap->port, co, baud, parity, bits, flow);
  1881. }
  1882. /**
  1883. * pl011_console_match - non-standard console matching
  1884. * @co: registering console
  1885. * @name: name from console command line
  1886. * @idx: index from console command line
  1887. * @options: ptr to option string from console command line
  1888. *
  1889. * Only attempts to match console command lines of the form:
  1890. * console=pl011,mmio|mmio32,<addr>[,<options>]
  1891. * console=pl011,0x<addr>[,<options>]
  1892. * This form is used to register an initial earlycon boot console and
  1893. * replace it with the amba_console at pl011 driver init.
  1894. *
  1895. * Performs console setup for a match (as required by interface)
  1896. * If no <options> are specified, then assume the h/w is already setup.
  1897. *
  1898. * Returns 0 if console matches; otherwise non-zero to use default matching
  1899. */
  1900. static int __init pl011_console_match(struct console *co, char *name, int idx,
  1901. char *options)
  1902. {
  1903. unsigned char iotype;
  1904. resource_size_t addr;
  1905. int i;
  1906. /*
  1907. * Systems affected by the Qualcomm Technologies QDF2400 E44 erratum
  1908. * have a distinct console name, so make sure we check for that.
  1909. * The actual implementation of the erratum occurs in the probe
  1910. * function.
  1911. */
  1912. if ((strcmp(name, "qdf2400_e44") != 0) && (strcmp(name, "pl011") != 0))
  1913. return -ENODEV;
  1914. if (uart_parse_earlycon(options, &iotype, &addr, &options))
  1915. return -ENODEV;
  1916. if (iotype != UPIO_MEM && iotype != UPIO_MEM32)
  1917. return -ENODEV;
  1918. /* try to match the port specified on the command line */
  1919. for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
  1920. struct uart_port *port;
  1921. if (!amba_ports[i])
  1922. continue;
  1923. port = &amba_ports[i]->port;
  1924. if (port->mapbase != addr)
  1925. continue;
  1926. co->index = i;
  1927. port->cons = co;
  1928. return pl011_console_setup(co, options);
  1929. }
  1930. return -ENODEV;
  1931. }
  1932. static struct uart_driver ark_uart_reg;
  1933. static struct console ark_console = {
  1934. .name = "ttyS",
  1935. .write = pl011_console_write,
  1936. .device = uart_console_device,
  1937. .setup = pl011_console_setup,
  1938. .match = pl011_console_match,
  1939. .flags = CON_PRINTBUFFER | CON_ANYTIME,
  1940. .index = -1,
  1941. .data = &ark_uart_reg,
  1942. };
  1943. #define ARK_CONSOLE (&ark_console)
  1944. static void pl011_putc(struct uart_port *port, int c)
  1945. {
  1946. while (readl(port->membase + UART01x_FR) & UART01x_FR_TXFF)
  1947. cpu_relax();
  1948. if (port->iotype == UPIO_MEM32)
  1949. writel(c, port->membase + UART01x_DR);
  1950. else
  1951. writeb(c, port->membase + UART01x_DR);
  1952. while (readl(port->membase + UART01x_FR) & UART01x_FR_BUSY)
  1953. cpu_relax();
  1954. }
  1955. static void pl011_early_write(struct console *con, const char *s, unsigned n)
  1956. {
  1957. struct earlycon_device *dev = con->data;
  1958. uart_console_write(&dev->port, s, n, pl011_putc);
  1959. }
  1960. /*
  1961. * On non-ACPI systems, earlycon is enabled by specifying
  1962. * "earlycon=pl011,<address>" on the kernel command line.
  1963. *
  1964. * On ACPI ARM64 systems, an "early" console is enabled via the SPCR table,
  1965. * by specifying only "earlycon" on the command line. Because it requires
  1966. * SPCR, the console starts after ACPI is parsed, which is later than a
  1967. * traditional early console.
  1968. *
  1969. * To get the traditional early console that starts before ACPI is parsed,
  1970. * specify the full "earlycon=pl011,<address>" option.
  1971. */
  1972. static int __init pl011_early_console_setup(struct earlycon_device *device,
  1973. const char *opt)
  1974. {
  1975. if (!device->port.membase)
  1976. return -ENODEV;
  1977. device->con->write = pl011_early_write;
  1978. return 0;
  1979. }
  1980. OF_EARLYCON_DECLARE(pl011, "arkmicro,ark-uart", pl011_early_console_setup);
  1981. #else
  1982. #define ARK_CONSOLE NULL
  1983. #endif
  1984. static struct uart_driver ark_uart_reg = {
  1985. .owner = THIS_MODULE,
  1986. .driver_name = "ttyS",
  1987. .dev_name = "ttyS",
  1988. .major = SERIAL_ARK_MAJOR,
  1989. .minor = SERIAL_ARK_MINOR,
  1990. .nr = UART_NR,
  1991. .cons = ARK_CONSOLE,
  1992. };
  1993. static int pl011_probe_dt_alias(int index, struct device *dev)
  1994. {
  1995. struct device_node *np;
  1996. static bool seen_dev_with_alias = false;
  1997. static bool seen_dev_without_alias = false;
  1998. int ret = index;
  1999. if (!IS_ENABLED(CONFIG_OF))
  2000. return ret;
  2001. np = dev->of_node;
  2002. if (!np)
  2003. return ret;
  2004. ret = of_alias_get_id(np, "serial");
  2005. if (ret < 0) {
  2006. seen_dev_without_alias = true;
  2007. ret = index;
  2008. } else {
  2009. seen_dev_with_alias = true;
  2010. if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
  2011. dev_warn(dev, "requested serial port %d not available.\n", ret);
  2012. ret = index;
  2013. }
  2014. }
  2015. if (seen_dev_with_alias && seen_dev_without_alias)
  2016. dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
  2017. return ret;
  2018. }
  2019. /* unregisters the driver also if no more ports are left */
  2020. static void pl011_unregister_port(struct uart_amba_port *uap)
  2021. {
  2022. int i;
  2023. bool busy = false;
  2024. for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
  2025. if (amba_ports[i] == uap)
  2026. amba_ports[i] = NULL;
  2027. else if (amba_ports[i])
  2028. busy = true;
  2029. }
  2030. pl011_dma_remove(uap);
  2031. if (!busy)
  2032. uart_unregister_driver(&ark_uart_reg);
  2033. }
  2034. static int pl011_find_free_port(void)
  2035. {
  2036. int i;
  2037. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  2038. if (amba_ports[i] == NULL)
  2039. return i;
  2040. return -EBUSY;
  2041. }
  2042. static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap,
  2043. struct resource *mmiobase, int index)
  2044. {
  2045. void __iomem *base;
  2046. base = devm_ioremap_resource(dev, mmiobase);
  2047. if (IS_ERR(base))
  2048. return PTR_ERR(base);
  2049. index = pl011_probe_dt_alias(index, dev);
  2050. uap->old_cr = 0;
  2051. uap->port.dev = dev;
  2052. uap->port.mapbase = mmiobase->start;
  2053. uap->port.membase = base;
  2054. uap->port.fifosize = uap->fifosize;
  2055. uap->port.flags = UPF_BOOT_AUTOCONF;
  2056. uap->port.line = index;
  2057. uap->port.uartclk = clk_get_rate(uap->clk);
  2058. spin_lock_init(&uap->port.lock);
  2059. amba_ports[index] = uap;
  2060. return 0;
  2061. }
  2062. static int pl011_register_port(struct uart_amba_port *uap)
  2063. {
  2064. int ret;
  2065. /* Ensure interrupts from this UART are masked and cleared */
  2066. pl011_write(0, uap, REG_IMSC);
  2067. pl011_write(0xffff, uap, REG_ICR);
  2068. if (!ark_uart_reg.state) {
  2069. ret = uart_register_driver(&ark_uart_reg);
  2070. if (ret < 0) {
  2071. dev_err(uap->port.dev,
  2072. "Failed to register AMBA-PL011 driver\n");
  2073. return ret;
  2074. }
  2075. }
  2076. ret = uart_add_one_port(&ark_uart_reg, &uap->port);
  2077. if (ret)
  2078. pl011_unregister_port(uap);
  2079. return ret;
  2080. }
  2081. #ifdef CONFIG_PM_SLEEP
  2082. static int pl011_suspend(struct device *dev)
  2083. {
  2084. struct uart_amba_port *uap = dev_get_drvdata(dev);
  2085. if (!uap)
  2086. return -EINVAL;
  2087. return uart_suspend_port(&ark_uart_reg, &uap->port);
  2088. }
  2089. static int pl011_resume(struct device *dev)
  2090. {
  2091. struct uart_amba_port *uap = dev_get_drvdata(dev);
  2092. if (!uap)
  2093. return -EINVAL;
  2094. return uart_resume_port(&ark_uart_reg, &uap->port);
  2095. }
  2096. #endif
  2097. static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
  2098. static void pl011_early_init(struct uart_amba_port *uap, int baudrate)
  2099. {
  2100. unsigned int clock = uap->port.uartclk;
  2101. unsigned int temp;
  2102. unsigned int divider;
  2103. unsigned int remainder;
  2104. unsigned int fraction;
  2105. unsigned int lcr;
  2106. /* first, disable everything */
  2107. pl011_write(0, uap, REG_CR);
  2108. /*
  2109. * Set baud rate
  2110. *
  2111. * IBRD = UART_CLK / (16 * BAUD_RATE)
  2112. * FBRD = RND((64 * MOD(UART_CLK,(16 * BAUD_RATE)))
  2113. * / (16 * BAUD_RATE))
  2114. */
  2115. temp = 16 * baudrate;
  2116. divider = clock / temp;
  2117. remainder = clock % temp;
  2118. temp = (8 * remainder) / baudrate;
  2119. fraction = (temp >> 1) + (temp & 1);
  2120. pl011_write(divider, uap, REG_IBRD);
  2121. pl011_write(fraction, uap, REG_FBRD);
  2122. /*
  2123. * Internal update of baud rate register require line
  2124. * control register write
  2125. */
  2126. lcr = UART01x_LCRH_WLEN_8 | UART01x_LCRH_FEN;
  2127. pl011_write_lcr_h(uap, lcr);
  2128. /* Finally, enable the UART */
  2129. pl011_startup(&uap->port);
  2130. }
  2131. static int pl011_tx_data(struct uart_amba_port *uap,
  2132. const unsigned char *buf, int count)
  2133. {
  2134. struct circ_buf *circ = &uap->port.state->xmit;
  2135. unsigned long flags;
  2136. int c, ret = 0;
  2137. spin_lock_irqsave(&uap->port.lock, flags);
  2138. if (!circ->buf) {
  2139. for (c = 0; c < count; c++) {
  2140. while(pl011_read(uap, REG_FR) & UART01x_FR_TXFF);
  2141. pl011_write(buf[c], uap, REG_DR);
  2142. }
  2143. return 0;
  2144. }
  2145. while (1) {
  2146. c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
  2147. if (count < c)
  2148. c = count;
  2149. if (c <= 0)
  2150. break;
  2151. memcpy(circ->buf + circ->head, buf, c);
  2152. circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
  2153. buf += c;
  2154. count -= c;
  2155. ret += c;
  2156. }
  2157. pl011_start_tx(&uap->port);
  2158. spin_unlock_irqrestore(&uap->port.lock, flags);
  2159. return ret;
  2160. }
  2161. int mcu_serial_send(const unsigned char *buf, int len)
  2162. {
  2163. int i;
  2164. for (i = 0; i < UART_NR; i++) {
  2165. if (amba_ports[i] && amba_ports[i]->use_for_mcu) {
  2166. return pl011_tx_data(amba_ports[i], buf, len);
  2167. }
  2168. }
  2169. return -ENODEV;
  2170. }
  2171. EXPORT_SYMBOL(mcu_serial_send);
  2172. void mcu_serial_register_rev_handler(void (*handler)(unsigned char ch), struct work_struct *task)
  2173. {
  2174. mcu_serial_rev_handler = handler;
  2175. mcu_serial_rev_task = task;
  2176. }
  2177. EXPORT_SYMBOL(mcu_serial_register_rev_handler);
  2178. void mcu_serial_unregister_rev_handler(void)
  2179. {
  2180. mcu_serial_rev_handler = NULL;
  2181. mcu_serial_rev_task = NULL;
  2182. }
  2183. EXPORT_SYMBOL(mcu_serial_unregister_rev_handler);
  2184. int tool_serial_send(const unsigned char *buf, int len)
  2185. {
  2186. int i;
  2187. for (i = 0; i < UART_NR; i++) {
  2188. if (amba_ports[i] && amba_ports[i]->use_for_tool) {
  2189. return pl011_tx_data(amba_ports[i], buf, len);
  2190. }
  2191. }
  2192. return -ENODEV;
  2193. }
  2194. EXPORT_SYMBOL(tool_serial_send);
  2195. void tool_serial_register_rev_handler(bool (*handler)(char ch), bool (*enable_check)(char ch))
  2196. {
  2197. tool_serial_rev_handler = handler;
  2198. tool_serial_rev_enable_check = enable_check;
  2199. }
  2200. EXPORT_SYMBOL(tool_serial_register_rev_handler);
  2201. void tool_serial_unregister_rev_handler(void)
  2202. {
  2203. tool_serial_rev_handler = NULL;
  2204. tool_serial_rev_enable_check = NULL;
  2205. }
  2206. EXPORT_SYMBOL(tool_serial_unregister_rev_handler);
  2207. static int ark_uart_probe(struct platform_device *pdev)
  2208. {
  2209. struct uart_amba_port *uap;
  2210. struct resource *r;
  2211. int portnr, ret;
  2212. int baudrate;
  2213. /*
  2214. * Check the mandatory baud rate parameter in the DT node early
  2215. * so that we can easily exit with the error.
  2216. */
  2217. if (pdev->dev.of_node) {
  2218. struct device_node *np = pdev->dev.of_node;
  2219. ret = of_property_read_u32(np, "current-speed", &baudrate);
  2220. if (ret)
  2221. return ret;
  2222. } else {
  2223. baudrate = 115200;
  2224. }
  2225. portnr = pl011_find_free_port();
  2226. if (portnr < 0)
  2227. return portnr;
  2228. uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port),
  2229. GFP_KERNEL);
  2230. if (!uap)
  2231. return -ENOMEM;
  2232. uap->clk = devm_clk_get(&pdev->dev, NULL);
  2233. if (IS_ERR(uap->clk))
  2234. return PTR_ERR(uap->clk);
  2235. ret = platform_get_irq(pdev, 0);
  2236. if (ret < 0) {
  2237. if (ret != -EPROBE_DEFER)
  2238. dev_err(&pdev->dev, "cannot obtain irq\n");
  2239. return ret;
  2240. }
  2241. uap->port.irq = ret;
  2242. uap->vendor = &vendor_ark;
  2243. uap->reg_offset = uap->vendor->reg_offset;
  2244. uap->fifosize = 16;
  2245. uap->port.iotype = uap->vendor->access_32b ? UPIO_MEM32 : UPIO_MEM;
  2246. uap->port.ops = &amba_pl011_pops;
  2247. snprintf(uap->type, sizeof(uap->type), "ARK");
  2248. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2249. ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
  2250. if (ret)
  2251. return ret;
  2252. platform_set_drvdata(pdev, uap);
  2253. ret = pl011_register_port(uap);
  2254. if (ret)
  2255. return ret;
  2256. uap->use_in_kernel = of_property_read_bool(pdev->dev.of_node, "use-in-kernel");
  2257. if (uap->use_in_kernel) {
  2258. uap->use_for_mcu = of_property_read_bool(pdev->dev.of_node, "use-for-mcu");
  2259. uap->use_for_tool = of_property_read_bool(pdev->dev.of_node, "use-for-tool");
  2260. pl011_early_init(uap, baudrate);
  2261. }
  2262. return 0;
  2263. }
  2264. static int ark_uart_remove(struct platform_device *pdev)
  2265. {
  2266. struct uart_amba_port *uap = platform_get_drvdata(pdev);
  2267. uart_remove_one_port(&ark_uart_reg, &uap->port);
  2268. pl011_unregister_port(uap);
  2269. return 0;
  2270. }
  2271. static const struct of_device_id ark_uart_of_match[] = {
  2272. { .compatible = "arkmicro,ark-uart", },
  2273. {},
  2274. };
  2275. static struct platform_driver ark_uart_platform_driver = {
  2276. .probe = ark_uart_probe,
  2277. .remove = ark_uart_remove,
  2278. .driver = {
  2279. .name = "ark-uart",
  2280. .of_match_table = of_match_ptr(ark_uart_of_match),
  2281. },
  2282. };
  2283. static int __init ark_uart_init(void)
  2284. {
  2285. printk(KERN_INFO "Serial: Arkmicro UART driver\n");
  2286. return platform_driver_register(&ark_uart_platform_driver);
  2287. }
  2288. static void __exit ark_uart_exit(void)
  2289. {
  2290. platform_driver_unregister(&ark_uart_platform_driver);
  2291. }
  2292. /*
  2293. * While this can be a module, if builtin it's most likely the console
  2294. * So let's leave module_exit but move module_init to an earlier place
  2295. */
  2296. arch_initcall(ark_uart_init);
  2297. module_exit(ark_uart_exit);
  2298. MODULE_AUTHOR("Sim");
  2299. MODULE_DESCRIPTION("Arkmicro serial port driver");
  2300. MODULE_LICENSE("GPL v2");