ark_uart.c 69 KB

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