amba-pl011.c 64 KB

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