atmel_serial.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695
  1. /*
  2. * Driver for Atmel AT91 / AT32 Serial ports
  3. * Copyright (C) 2003 Rick Bronson
  4. *
  5. * Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
  6. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  7. *
  8. * DMA support added by Chip Coldwell.
  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. */
  25. #include <linux/module.h>
  26. #include <linux/tty.h>
  27. #include <linux/ioport.h>
  28. #include <linux/slab.h>
  29. #include <linux/init.h>
  30. #include <linux/serial.h>
  31. #include <linux/clk.h>
  32. #include <linux/console.h>
  33. #include <linux/sysrq.h>
  34. #include <linux/tty_flip.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/of.h>
  37. #include <linux/of_device.h>
  38. #include <linux/of_gpio.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/dmaengine.h>
  41. #include <linux/atmel_pdc.h>
  42. #include <linux/atmel_serial.h>
  43. #include <linux/uaccess.h>
  44. #include <linux/platform_data/atmel.h>
  45. #include <linux/timer.h>
  46. #include <linux/gpio.h>
  47. #include <linux/gpio/consumer.h>
  48. #include <linux/err.h>
  49. #include <linux/irq.h>
  50. #include <asm/io.h>
  51. #include <asm/ioctls.h>
  52. #define PDC_BUFFER_SIZE 512
  53. /* Revisit: We should calculate this based on the actual port settings */
  54. #define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */
  55. #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  56. #define SUPPORT_SYSRQ
  57. #endif
  58. #include <linux/serial_core.h>
  59. #include "serial_mctrl_gpio.h"
  60. static void atmel_start_rx(struct uart_port *port);
  61. static void atmel_stop_rx(struct uart_port *port);
  62. #ifdef CONFIG_SERIAL_ATMEL_TTYAT
  63. /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
  64. * should coexist with the 8250 driver, such as if we have an external 16C550
  65. * UART. */
  66. #define SERIAL_ATMEL_MAJOR 204
  67. #define MINOR_START 154
  68. #define ATMEL_DEVICENAME "ttyAT"
  69. #else
  70. /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
  71. * name, but it is legally reserved for the 8250 driver. */
  72. #define SERIAL_ATMEL_MAJOR TTY_MAJOR
  73. #define MINOR_START 64
  74. #define ATMEL_DEVICENAME "ttyS"
  75. #endif
  76. #define ATMEL_ISR_PASS_LIMIT 256
  77. /* UART registers. CR is write-only, hence no GET macro */
  78. #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
  79. #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
  80. #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
  81. #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
  82. #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
  83. #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
  84. #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
  85. #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
  86. #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
  87. #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
  88. #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
  89. #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
  90. #define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR)
  91. #define UART_GET_IP_NAME(port) __raw_readl((port)->membase + ATMEL_US_NAME)
  92. #define UART_GET_IP_VERSION(port) __raw_readl((port)->membase + ATMEL_US_VERSION)
  93. /* PDC registers */
  94. #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
  95. #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
  96. #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
  97. #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
  98. #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
  99. #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
  100. #define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
  101. #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
  102. #define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
  103. #define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR)
  104. struct atmel_dma_buffer {
  105. unsigned char *buf;
  106. dma_addr_t dma_addr;
  107. unsigned int dma_size;
  108. unsigned int ofs;
  109. };
  110. struct atmel_uart_char {
  111. u16 status;
  112. u16 ch;
  113. };
  114. #define ATMEL_SERIAL_RINGSIZE 1024
  115. /*
  116. * We wrap our port structure around the generic uart_port.
  117. */
  118. struct atmel_uart_port {
  119. struct uart_port uart; /* uart */
  120. struct clk *clk; /* uart clock */
  121. int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */
  122. u32 backup_imr; /* IMR saved during suspend */
  123. int break_active; /* break being received */
  124. bool use_dma_rx; /* enable DMA receiver */
  125. bool use_pdc_rx; /* enable PDC receiver */
  126. short pdc_rx_idx; /* current PDC RX buffer */
  127. struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
  128. bool use_dma_tx; /* enable DMA transmitter */
  129. bool use_pdc_tx; /* enable PDC transmitter */
  130. struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
  131. spinlock_t lock_tx; /* port lock */
  132. spinlock_t lock_rx; /* port lock */
  133. struct dma_chan *chan_tx;
  134. struct dma_chan *chan_rx;
  135. struct dma_async_tx_descriptor *desc_tx;
  136. struct dma_async_tx_descriptor *desc_rx;
  137. dma_cookie_t cookie_tx;
  138. dma_cookie_t cookie_rx;
  139. struct scatterlist sg_tx;
  140. struct scatterlist sg_rx;
  141. struct tasklet_struct tasklet;
  142. unsigned int irq_status;
  143. unsigned int irq_status_prev;
  144. struct circ_buf rx_ring;
  145. struct serial_rs485 rs485; /* rs485 settings */
  146. struct mctrl_gpios *gpios;
  147. int gpio_irq[UART_GPIO_MAX];
  148. unsigned int tx_done_mask;
  149. bool ms_irq_enabled;
  150. bool is_usart; /* usart or uart */
  151. struct timer_list uart_timer; /* uart timer */
  152. int (*prepare_rx)(struct uart_port *port);
  153. int (*prepare_tx)(struct uart_port *port);
  154. void (*schedule_rx)(struct uart_port *port);
  155. void (*schedule_tx)(struct uart_port *port);
  156. void (*release_rx)(struct uart_port *port);
  157. void (*release_tx)(struct uart_port *port);
  158. };
  159. static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
  160. static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
  161. #ifdef SUPPORT_SYSRQ
  162. static struct console atmel_console;
  163. #endif
  164. #if defined(CONFIG_OF)
  165. static const struct of_device_id atmel_serial_dt_ids[] = {
  166. { .compatible = "atmel,at91rm9200-usart" },
  167. { .compatible = "atmel,at91sam9260-usart" },
  168. { /* sentinel */ }
  169. };
  170. MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids);
  171. #endif
  172. static inline struct atmel_uart_port *
  173. to_atmel_uart_port(struct uart_port *uart)
  174. {
  175. return container_of(uart, struct atmel_uart_port, uart);
  176. }
  177. #ifdef CONFIG_SERIAL_ATMEL_PDC
  178. static bool atmel_use_pdc_rx(struct uart_port *port)
  179. {
  180. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  181. return atmel_port->use_pdc_rx;
  182. }
  183. static bool atmel_use_pdc_tx(struct uart_port *port)
  184. {
  185. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  186. return atmel_port->use_pdc_tx;
  187. }
  188. #else
  189. static bool atmel_use_pdc_rx(struct uart_port *port)
  190. {
  191. return false;
  192. }
  193. static bool atmel_use_pdc_tx(struct uart_port *port)
  194. {
  195. return false;
  196. }
  197. #endif
  198. static bool atmel_use_dma_tx(struct uart_port *port)
  199. {
  200. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  201. return atmel_port->use_dma_tx;
  202. }
  203. static bool atmel_use_dma_rx(struct uart_port *port)
  204. {
  205. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  206. return atmel_port->use_dma_rx;
  207. }
  208. static unsigned int atmel_get_lines_status(struct uart_port *port)
  209. {
  210. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  211. unsigned int status, ret = 0;
  212. status = UART_GET_CSR(port);
  213. mctrl_gpio_get(atmel_port->gpios, &ret);
  214. if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
  215. UART_GPIO_CTS))) {
  216. if (ret & TIOCM_CTS)
  217. status &= ~ATMEL_US_CTS;
  218. else
  219. status |= ATMEL_US_CTS;
  220. }
  221. if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
  222. UART_GPIO_DSR))) {
  223. if (ret & TIOCM_DSR)
  224. status &= ~ATMEL_US_DSR;
  225. else
  226. status |= ATMEL_US_DSR;
  227. }
  228. if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
  229. UART_GPIO_RI))) {
  230. if (ret & TIOCM_RI)
  231. status &= ~ATMEL_US_RI;
  232. else
  233. status |= ATMEL_US_RI;
  234. }
  235. if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
  236. UART_GPIO_DCD))) {
  237. if (ret & TIOCM_CD)
  238. status &= ~ATMEL_US_DCD;
  239. else
  240. status |= ATMEL_US_DCD;
  241. }
  242. return status;
  243. }
  244. /* Enable or disable the rs485 support */
  245. void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
  246. {
  247. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  248. unsigned int mode;
  249. unsigned long flags;
  250. spin_lock_irqsave(&port->lock, flags);
  251. /* Disable interrupts */
  252. UART_PUT_IDR(port, atmel_port->tx_done_mask);
  253. mode = UART_GET_MR(port);
  254. /* Resetting serial mode to RS232 (0x0) */
  255. mode &= ~ATMEL_US_USMODE;
  256. atmel_port->rs485 = *rs485conf;
  257. if (rs485conf->flags & SER_RS485_ENABLED) {
  258. dev_dbg(port->dev, "Setting UART to RS485\n");
  259. atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
  260. if ((rs485conf->delay_rts_after_send) > 0)
  261. UART_PUT_TTGR(port, rs485conf->delay_rts_after_send);
  262. mode |= ATMEL_US_USMODE_RS485;
  263. } else {
  264. dev_dbg(port->dev, "Setting UART to RS232\n");
  265. if (atmel_use_pdc_tx(port))
  266. atmel_port->tx_done_mask = ATMEL_US_ENDTX |
  267. ATMEL_US_TXBUFE;
  268. else
  269. atmel_port->tx_done_mask = ATMEL_US_TXRDY;
  270. }
  271. UART_PUT_MR(port, mode);
  272. /* Enable interrupts */
  273. UART_PUT_IER(port, atmel_port->tx_done_mask);
  274. spin_unlock_irqrestore(&port->lock, flags);
  275. }
  276. /*
  277. * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
  278. */
  279. static u_int atmel_tx_empty(struct uart_port *port)
  280. {
  281. return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
  282. }
  283. /*
  284. * Set state of the modem control output lines
  285. */
  286. static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
  287. {
  288. unsigned int control = 0;
  289. unsigned int mode;
  290. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  291. if (mctrl & TIOCM_RTS)
  292. control |= ATMEL_US_RTSEN;
  293. else
  294. control |= ATMEL_US_RTSDIS;
  295. if (mctrl & TIOCM_DTR)
  296. control |= ATMEL_US_DTREN;
  297. else
  298. control |= ATMEL_US_DTRDIS;
  299. UART_PUT_CR(port, control);
  300. mctrl_gpio_set(atmel_port->gpios, mctrl);
  301. /* Local loopback mode? */
  302. mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
  303. if (mctrl & TIOCM_LOOP)
  304. mode |= ATMEL_US_CHMODE_LOC_LOOP;
  305. else
  306. mode |= ATMEL_US_CHMODE_NORMAL;
  307. /* Resetting serial mode to RS232 (0x0) */
  308. mode &= ~ATMEL_US_USMODE;
  309. if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
  310. dev_dbg(port->dev, "Setting UART to RS485\n");
  311. if ((atmel_port->rs485.delay_rts_after_send) > 0)
  312. UART_PUT_TTGR(port,
  313. atmel_port->rs485.delay_rts_after_send);
  314. mode |= ATMEL_US_USMODE_RS485;
  315. } else {
  316. dev_dbg(port->dev, "Setting UART to RS232\n");
  317. }
  318. UART_PUT_MR(port, mode);
  319. }
  320. /*
  321. * Get state of the modem control input lines
  322. */
  323. static u_int atmel_get_mctrl(struct uart_port *port)
  324. {
  325. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  326. unsigned int ret = 0, status;
  327. status = UART_GET_CSR(port);
  328. /*
  329. * The control signals are active low.
  330. */
  331. if (!(status & ATMEL_US_DCD))
  332. ret |= TIOCM_CD;
  333. if (!(status & ATMEL_US_CTS))
  334. ret |= TIOCM_CTS;
  335. if (!(status & ATMEL_US_DSR))
  336. ret |= TIOCM_DSR;
  337. if (!(status & ATMEL_US_RI))
  338. ret |= TIOCM_RI;
  339. return mctrl_gpio_get(atmel_port->gpios, &ret);
  340. }
  341. /*
  342. * Stop transmitting.
  343. */
  344. static void atmel_stop_tx(struct uart_port *port)
  345. {
  346. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  347. if (atmel_use_pdc_tx(port)) {
  348. /* disable PDC transmit */
  349. UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
  350. }
  351. /* Disable interrupts */
  352. UART_PUT_IDR(port, atmel_port->tx_done_mask);
  353. if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
  354. !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
  355. atmel_start_rx(port);
  356. }
  357. /*
  358. * Start transmitting.
  359. */
  360. static void atmel_start_tx(struct uart_port *port)
  361. {
  362. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  363. if (atmel_use_pdc_tx(port)) {
  364. if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
  365. /* The transmitter is already running. Yes, we
  366. really need this.*/
  367. return;
  368. if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
  369. !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
  370. atmel_stop_rx(port);
  371. /* re-enable PDC transmit */
  372. UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
  373. }
  374. /* Enable interrupts */
  375. UART_PUT_IER(port, atmel_port->tx_done_mask);
  376. }
  377. /*
  378. * start receiving - port is in process of being opened.
  379. */
  380. static void atmel_start_rx(struct uart_port *port)
  381. {
  382. UART_PUT_CR(port, ATMEL_US_RSTSTA); /* reset status and receiver */
  383. UART_PUT_CR(port, ATMEL_US_RXEN);
  384. if (atmel_use_pdc_rx(port)) {
  385. /* enable PDC controller */
  386. UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
  387. port->read_status_mask);
  388. UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
  389. } else {
  390. UART_PUT_IER(port, ATMEL_US_RXRDY);
  391. }
  392. }
  393. /*
  394. * Stop receiving - port is in process of being closed.
  395. */
  396. static void atmel_stop_rx(struct uart_port *port)
  397. {
  398. UART_PUT_CR(port, ATMEL_US_RXDIS);
  399. if (atmel_use_pdc_rx(port)) {
  400. /* disable PDC receive */
  401. UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
  402. UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
  403. port->read_status_mask);
  404. } else {
  405. UART_PUT_IDR(port, ATMEL_US_RXRDY);
  406. }
  407. }
  408. /*
  409. * Enable modem status interrupts
  410. */
  411. static void atmel_enable_ms(struct uart_port *port)
  412. {
  413. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  414. uint32_t ier = 0;
  415. /*
  416. * Interrupt should not be enabled twice
  417. */
  418. if (atmel_port->ms_irq_enabled)
  419. return;
  420. atmel_port->ms_irq_enabled = true;
  421. if (atmel_port->gpio_irq[UART_GPIO_CTS] >= 0)
  422. enable_irq(atmel_port->gpio_irq[UART_GPIO_CTS]);
  423. else
  424. ier |= ATMEL_US_CTSIC;
  425. if (atmel_port->gpio_irq[UART_GPIO_DSR] >= 0)
  426. enable_irq(atmel_port->gpio_irq[UART_GPIO_DSR]);
  427. else
  428. ier |= ATMEL_US_DSRIC;
  429. if (atmel_port->gpio_irq[UART_GPIO_RI] >= 0)
  430. enable_irq(atmel_port->gpio_irq[UART_GPIO_RI]);
  431. else
  432. ier |= ATMEL_US_RIIC;
  433. if (atmel_port->gpio_irq[UART_GPIO_DCD] >= 0)
  434. enable_irq(atmel_port->gpio_irq[UART_GPIO_DCD]);
  435. else
  436. ier |= ATMEL_US_DCDIC;
  437. UART_PUT_IER(port, ier);
  438. }
  439. /*
  440. * Disable modem status interrupts
  441. */
  442. static void atmel_disable_ms(struct uart_port *port)
  443. {
  444. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  445. uint32_t idr = 0;
  446. /*
  447. * Interrupt should not be disabled twice
  448. */
  449. if (!atmel_port->ms_irq_enabled)
  450. return;
  451. atmel_port->ms_irq_enabled = false;
  452. if (atmel_port->gpio_irq[UART_GPIO_CTS] >= 0)
  453. disable_irq(atmel_port->gpio_irq[UART_GPIO_CTS]);
  454. else
  455. idr |= ATMEL_US_CTSIC;
  456. if (atmel_port->gpio_irq[UART_GPIO_DSR] >= 0)
  457. disable_irq(atmel_port->gpio_irq[UART_GPIO_DSR]);
  458. else
  459. idr |= ATMEL_US_DSRIC;
  460. if (atmel_port->gpio_irq[UART_GPIO_RI] >= 0)
  461. disable_irq(atmel_port->gpio_irq[UART_GPIO_RI]);
  462. else
  463. idr |= ATMEL_US_RIIC;
  464. if (atmel_port->gpio_irq[UART_GPIO_DCD] >= 0)
  465. disable_irq(atmel_port->gpio_irq[UART_GPIO_DCD]);
  466. else
  467. idr |= ATMEL_US_DCDIC;
  468. UART_PUT_IDR(port, idr);
  469. }
  470. /*
  471. * Control the transmission of a break signal
  472. */
  473. static void atmel_break_ctl(struct uart_port *port, int break_state)
  474. {
  475. if (break_state != 0)
  476. UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
  477. else
  478. UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
  479. }
  480. /*
  481. * Stores the incoming character in the ring buffer
  482. */
  483. static void
  484. atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
  485. unsigned int ch)
  486. {
  487. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  488. struct circ_buf *ring = &atmel_port->rx_ring;
  489. struct atmel_uart_char *c;
  490. if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
  491. /* Buffer overflow, ignore char */
  492. return;
  493. c = &((struct atmel_uart_char *)ring->buf)[ring->head];
  494. c->status = status;
  495. c->ch = ch;
  496. /* Make sure the character is stored before we update head. */
  497. smp_wmb();
  498. ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
  499. }
  500. /*
  501. * Deal with parity, framing and overrun errors.
  502. */
  503. static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
  504. {
  505. /* clear error */
  506. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  507. if (status & ATMEL_US_RXBRK) {
  508. /* ignore side-effect */
  509. status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
  510. port->icount.brk++;
  511. }
  512. if (status & ATMEL_US_PARE)
  513. port->icount.parity++;
  514. if (status & ATMEL_US_FRAME)
  515. port->icount.frame++;
  516. if (status & ATMEL_US_OVRE)
  517. port->icount.overrun++;
  518. }
  519. /*
  520. * Characters received (called from interrupt handler)
  521. */
  522. static void atmel_rx_chars(struct uart_port *port)
  523. {
  524. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  525. unsigned int status, ch;
  526. status = UART_GET_CSR(port);
  527. while (status & ATMEL_US_RXRDY) {
  528. ch = UART_GET_CHAR(port);
  529. /*
  530. * note that the error handling code is
  531. * out of the main execution path
  532. */
  533. if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
  534. | ATMEL_US_OVRE | ATMEL_US_RXBRK)
  535. || atmel_port->break_active)) {
  536. /* clear error */
  537. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  538. if (status & ATMEL_US_RXBRK
  539. && !atmel_port->break_active) {
  540. atmel_port->break_active = 1;
  541. UART_PUT_IER(port, ATMEL_US_RXBRK);
  542. } else {
  543. /*
  544. * This is either the end-of-break
  545. * condition or we've received at
  546. * least one character without RXBRK
  547. * being set. In both cases, the next
  548. * RXBRK will indicate start-of-break.
  549. */
  550. UART_PUT_IDR(port, ATMEL_US_RXBRK);
  551. status &= ~ATMEL_US_RXBRK;
  552. atmel_port->break_active = 0;
  553. }
  554. }
  555. atmel_buffer_rx_char(port, status, ch);
  556. status = UART_GET_CSR(port);
  557. }
  558. tasklet_schedule(&atmel_port->tasklet);
  559. }
  560. /*
  561. * Transmit characters (called from tasklet with TXRDY interrupt
  562. * disabled)
  563. */
  564. static void atmel_tx_chars(struct uart_port *port)
  565. {
  566. struct circ_buf *xmit = &port->state->xmit;
  567. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  568. if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
  569. UART_PUT_CHAR(port, port->x_char);
  570. port->icount.tx++;
  571. port->x_char = 0;
  572. }
  573. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  574. return;
  575. while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
  576. UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
  577. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  578. port->icount.tx++;
  579. if (uart_circ_empty(xmit))
  580. break;
  581. }
  582. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  583. uart_write_wakeup(port);
  584. if (!uart_circ_empty(xmit))
  585. /* Enable interrupts */
  586. UART_PUT_IER(port, atmel_port->tx_done_mask);
  587. }
  588. static void atmel_complete_tx_dma(void *arg)
  589. {
  590. struct atmel_uart_port *atmel_port = arg;
  591. struct uart_port *port = &atmel_port->uart;
  592. struct circ_buf *xmit = &port->state->xmit;
  593. struct dma_chan *chan = atmel_port->chan_tx;
  594. unsigned long flags;
  595. spin_lock_irqsave(&port->lock, flags);
  596. if (chan)
  597. dmaengine_terminate_all(chan);
  598. xmit->tail += sg_dma_len(&atmel_port->sg_tx);
  599. xmit->tail &= UART_XMIT_SIZE - 1;
  600. port->icount.tx += sg_dma_len(&atmel_port->sg_tx);
  601. spin_lock_irq(&atmel_port->lock_tx);
  602. async_tx_ack(atmel_port->desc_tx);
  603. atmel_port->cookie_tx = -EINVAL;
  604. atmel_port->desc_tx = NULL;
  605. spin_unlock_irq(&atmel_port->lock_tx);
  606. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  607. uart_write_wakeup(port);
  608. /* Do we really need this? */
  609. if (!uart_circ_empty(xmit))
  610. tasklet_schedule(&atmel_port->tasklet);
  611. spin_unlock_irqrestore(&port->lock, flags);
  612. }
  613. static void atmel_release_tx_dma(struct uart_port *port)
  614. {
  615. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  616. struct dma_chan *chan = atmel_port->chan_tx;
  617. if (chan) {
  618. dmaengine_terminate_all(chan);
  619. dma_release_channel(chan);
  620. dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
  621. DMA_TO_DEVICE);
  622. }
  623. atmel_port->desc_tx = NULL;
  624. atmel_port->chan_tx = NULL;
  625. atmel_port->cookie_tx = -EINVAL;
  626. }
  627. /*
  628. * Called from tasklet with TXRDY interrupt is disabled.
  629. */
  630. static void atmel_tx_dma(struct uart_port *port)
  631. {
  632. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  633. struct circ_buf *xmit = &port->state->xmit;
  634. struct dma_chan *chan = atmel_port->chan_tx;
  635. struct dma_async_tx_descriptor *desc;
  636. struct scatterlist *sg = &atmel_port->sg_tx;
  637. /* Make sure we have an idle channel */
  638. if (atmel_port->desc_tx != NULL)
  639. return;
  640. if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
  641. /*
  642. * DMA is idle now.
  643. * Port xmit buffer is already mapped,
  644. * and it is one page... Just adjust
  645. * offsets and lengths. Since it is a circular buffer,
  646. * we have to transmit till the end, and then the rest.
  647. * Take the port lock to get a
  648. * consistent xmit buffer state.
  649. */
  650. sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
  651. sg_dma_address(sg) = (sg_dma_address(sg) &
  652. ~(UART_XMIT_SIZE - 1))
  653. + sg->offset;
  654. sg_dma_len(sg) = CIRC_CNT_TO_END(xmit->head,
  655. xmit->tail,
  656. UART_XMIT_SIZE);
  657. BUG_ON(!sg_dma_len(sg));
  658. desc = dmaengine_prep_slave_sg(chan,
  659. sg,
  660. 1,
  661. DMA_MEM_TO_DEV,
  662. DMA_PREP_INTERRUPT |
  663. DMA_CTRL_ACK);
  664. if (!desc) {
  665. dev_err(port->dev, "Failed to send via dma!\n");
  666. return;
  667. }
  668. dma_sync_sg_for_device(port->dev, sg, 1, DMA_MEM_TO_DEV);
  669. atmel_port->desc_tx = desc;
  670. desc->callback = atmel_complete_tx_dma;
  671. desc->callback_param = atmel_port;
  672. atmel_port->cookie_tx = dmaengine_submit(desc);
  673. } else {
  674. if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
  675. /* DMA done, stop TX, start RX for RS485 */
  676. atmel_start_rx(port);
  677. }
  678. }
  679. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  680. uart_write_wakeup(port);
  681. }
  682. static int atmel_prepare_tx_dma(struct uart_port *port)
  683. {
  684. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  685. dma_cap_mask_t mask;
  686. struct dma_slave_config config;
  687. int ret, nent;
  688. dma_cap_zero(mask);
  689. dma_cap_set(DMA_SLAVE, mask);
  690. atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
  691. if (atmel_port->chan_tx == NULL)
  692. goto chan_err;
  693. dev_info(port->dev, "using %s for tx DMA transfers\n",
  694. dma_chan_name(atmel_port->chan_tx));
  695. spin_lock_init(&atmel_port->lock_tx);
  696. sg_init_table(&atmel_port->sg_tx, 1);
  697. /* UART circular tx buffer is an aligned page. */
  698. BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
  699. sg_set_page(&atmel_port->sg_tx,
  700. virt_to_page(port->state->xmit.buf),
  701. UART_XMIT_SIZE,
  702. (int)port->state->xmit.buf & ~PAGE_MASK);
  703. nent = dma_map_sg(port->dev,
  704. &atmel_port->sg_tx,
  705. 1,
  706. DMA_TO_DEVICE);
  707. if (!nent) {
  708. dev_dbg(port->dev, "need to release resource of dma\n");
  709. goto chan_err;
  710. } else {
  711. dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
  712. sg_dma_len(&atmel_port->sg_tx),
  713. port->state->xmit.buf,
  714. sg_dma_address(&atmel_port->sg_tx));
  715. }
  716. /* Configure the slave DMA */
  717. memset(&config, 0, sizeof(config));
  718. config.direction = DMA_MEM_TO_DEV;
  719. config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  720. config.dst_addr = port->mapbase + ATMEL_US_THR;
  721. ret = dmaengine_device_control(atmel_port->chan_tx,
  722. DMA_SLAVE_CONFIG,
  723. (unsigned long)&config);
  724. if (ret) {
  725. dev_err(port->dev, "DMA tx slave configuration failed\n");
  726. goto chan_err;
  727. }
  728. return 0;
  729. chan_err:
  730. dev_err(port->dev, "TX channel not available, switch to pio\n");
  731. atmel_port->use_dma_tx = 0;
  732. if (atmel_port->chan_tx)
  733. atmel_release_tx_dma(port);
  734. return -EINVAL;
  735. }
  736. static void atmel_flip_buffer_rx_dma(struct uart_port *port,
  737. char *buf, size_t count)
  738. {
  739. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  740. struct tty_port *tport = &port->state->port;
  741. dma_sync_sg_for_cpu(port->dev,
  742. &atmel_port->sg_rx,
  743. 1,
  744. DMA_DEV_TO_MEM);
  745. tty_insert_flip_string(tport, buf, count);
  746. dma_sync_sg_for_device(port->dev,
  747. &atmel_port->sg_rx,
  748. 1,
  749. DMA_DEV_TO_MEM);
  750. /*
  751. * Drop the lock here since it might end up calling
  752. * uart_start(), which takes the lock.
  753. */
  754. spin_unlock(&port->lock);
  755. tty_flip_buffer_push(tport);
  756. spin_lock(&port->lock);
  757. }
  758. static void atmel_complete_rx_dma(void *arg)
  759. {
  760. struct uart_port *port = arg;
  761. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  762. tasklet_schedule(&atmel_port->tasklet);
  763. }
  764. static void atmel_release_rx_dma(struct uart_port *port)
  765. {
  766. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  767. struct dma_chan *chan = atmel_port->chan_rx;
  768. if (chan) {
  769. dmaengine_terminate_all(chan);
  770. dma_release_channel(chan);
  771. dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
  772. DMA_FROM_DEVICE);
  773. }
  774. atmel_port->desc_rx = NULL;
  775. atmel_port->chan_rx = NULL;
  776. atmel_port->cookie_rx = -EINVAL;
  777. }
  778. static void atmel_rx_from_dma(struct uart_port *port)
  779. {
  780. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  781. struct circ_buf *ring = &atmel_port->rx_ring;
  782. struct dma_chan *chan = atmel_port->chan_rx;
  783. struct dma_tx_state state;
  784. enum dma_status dmastat;
  785. size_t pending, count;
  786. /* Reset the UART timeout early so that we don't miss one */
  787. UART_PUT_CR(port, ATMEL_US_STTTO);
  788. dmastat = dmaengine_tx_status(chan,
  789. atmel_port->cookie_rx,
  790. &state);
  791. /* Restart a new tasklet if DMA status is error */
  792. if (dmastat == DMA_ERROR) {
  793. dev_dbg(port->dev, "Get residue error, restart tasklet\n");
  794. UART_PUT_IER(port, ATMEL_US_TIMEOUT);
  795. tasklet_schedule(&atmel_port->tasklet);
  796. return;
  797. }
  798. /* current transfer size should no larger than dma buffer */
  799. pending = sg_dma_len(&atmel_port->sg_rx) - state.residue;
  800. BUG_ON(pending > sg_dma_len(&atmel_port->sg_rx));
  801. /*
  802. * This will take the chars we have so far,
  803. * ring->head will record the transfer size, only new bytes come
  804. * will insert into the framework.
  805. */
  806. if (pending > ring->head) {
  807. count = pending - ring->head;
  808. atmel_flip_buffer_rx_dma(port, ring->buf + ring->head, count);
  809. ring->head += count;
  810. if (ring->head == sg_dma_len(&atmel_port->sg_rx))
  811. ring->head = 0;
  812. port->icount.rx += count;
  813. }
  814. UART_PUT_IER(port, ATMEL_US_TIMEOUT);
  815. }
  816. static int atmel_prepare_rx_dma(struct uart_port *port)
  817. {
  818. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  819. struct dma_async_tx_descriptor *desc;
  820. dma_cap_mask_t mask;
  821. struct dma_slave_config config;
  822. struct circ_buf *ring;
  823. int ret, nent;
  824. ring = &atmel_port->rx_ring;
  825. dma_cap_zero(mask);
  826. dma_cap_set(DMA_CYCLIC, mask);
  827. atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
  828. if (atmel_port->chan_rx == NULL)
  829. goto chan_err;
  830. dev_info(port->dev, "using %s for rx DMA transfers\n",
  831. dma_chan_name(atmel_port->chan_rx));
  832. spin_lock_init(&atmel_port->lock_rx);
  833. sg_init_table(&atmel_port->sg_rx, 1);
  834. /* UART circular rx buffer is an aligned page. */
  835. BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
  836. sg_set_page(&atmel_port->sg_rx,
  837. virt_to_page(ring->buf),
  838. ATMEL_SERIAL_RINGSIZE,
  839. (int)ring->buf & ~PAGE_MASK);
  840. nent = dma_map_sg(port->dev,
  841. &atmel_port->sg_rx,
  842. 1,
  843. DMA_FROM_DEVICE);
  844. if (!nent) {
  845. dev_dbg(port->dev, "need to release resource of dma\n");
  846. goto chan_err;
  847. } else {
  848. dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
  849. sg_dma_len(&atmel_port->sg_rx),
  850. ring->buf,
  851. sg_dma_address(&atmel_port->sg_rx));
  852. }
  853. /* Configure the slave DMA */
  854. memset(&config, 0, sizeof(config));
  855. config.direction = DMA_DEV_TO_MEM;
  856. config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  857. config.src_addr = port->mapbase + ATMEL_US_RHR;
  858. ret = dmaengine_device_control(atmel_port->chan_rx,
  859. DMA_SLAVE_CONFIG,
  860. (unsigned long)&config);
  861. if (ret) {
  862. dev_err(port->dev, "DMA rx slave configuration failed\n");
  863. goto chan_err;
  864. }
  865. /*
  866. * Prepare a cyclic dma transfer, assign 2 descriptors,
  867. * each one is half ring buffer size
  868. */
  869. desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
  870. sg_dma_address(&atmel_port->sg_rx),
  871. sg_dma_len(&atmel_port->sg_rx),
  872. sg_dma_len(&atmel_port->sg_rx)/2,
  873. DMA_DEV_TO_MEM,
  874. DMA_PREP_INTERRUPT);
  875. desc->callback = atmel_complete_rx_dma;
  876. desc->callback_param = port;
  877. atmel_port->desc_rx = desc;
  878. atmel_port->cookie_rx = dmaengine_submit(desc);
  879. return 0;
  880. chan_err:
  881. dev_err(port->dev, "RX channel not available, switch to pio\n");
  882. atmel_port->use_dma_rx = 0;
  883. if (atmel_port->chan_rx)
  884. atmel_release_rx_dma(port);
  885. return -EINVAL;
  886. }
  887. static void atmel_uart_timer_callback(unsigned long data)
  888. {
  889. struct uart_port *port = (void *)data;
  890. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  891. tasklet_schedule(&atmel_port->tasklet);
  892. mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
  893. }
  894. /*
  895. * receive interrupt handler.
  896. */
  897. static void
  898. atmel_handle_receive(struct uart_port *port, unsigned int pending)
  899. {
  900. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  901. if (atmel_use_pdc_rx(port)) {
  902. /*
  903. * PDC receive. Just schedule the tasklet and let it
  904. * figure out the details.
  905. *
  906. * TODO: We're not handling error flags correctly at
  907. * the moment.
  908. */
  909. if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
  910. UART_PUT_IDR(port, (ATMEL_US_ENDRX
  911. | ATMEL_US_TIMEOUT));
  912. tasklet_schedule(&atmel_port->tasklet);
  913. }
  914. if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
  915. ATMEL_US_FRAME | ATMEL_US_PARE))
  916. atmel_pdc_rxerr(port, pending);
  917. }
  918. if (atmel_use_dma_rx(port)) {
  919. if (pending & ATMEL_US_TIMEOUT) {
  920. UART_PUT_IDR(port, ATMEL_US_TIMEOUT);
  921. tasklet_schedule(&atmel_port->tasklet);
  922. }
  923. }
  924. /* Interrupt receive */
  925. if (pending & ATMEL_US_RXRDY)
  926. atmel_rx_chars(port);
  927. else if (pending & ATMEL_US_RXBRK) {
  928. /*
  929. * End of break detected. If it came along with a
  930. * character, atmel_rx_chars will handle it.
  931. */
  932. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  933. UART_PUT_IDR(port, ATMEL_US_RXBRK);
  934. atmel_port->break_active = 0;
  935. }
  936. }
  937. /*
  938. * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
  939. */
  940. static void
  941. atmel_handle_transmit(struct uart_port *port, unsigned int pending)
  942. {
  943. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  944. if (pending & atmel_port->tx_done_mask) {
  945. /* Either PDC or interrupt transmission */
  946. UART_PUT_IDR(port, atmel_port->tx_done_mask);
  947. tasklet_schedule(&atmel_port->tasklet);
  948. }
  949. }
  950. /*
  951. * status flags interrupt handler.
  952. */
  953. static void
  954. atmel_handle_status(struct uart_port *port, unsigned int pending,
  955. unsigned int status)
  956. {
  957. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  958. if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
  959. | ATMEL_US_CTSIC)) {
  960. atmel_port->irq_status = status;
  961. tasklet_schedule(&atmel_port->tasklet);
  962. }
  963. }
  964. /*
  965. * Interrupt handler
  966. */
  967. static irqreturn_t atmel_interrupt(int irq, void *dev_id)
  968. {
  969. struct uart_port *port = dev_id;
  970. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  971. unsigned int status, pending, pass_counter = 0;
  972. bool gpio_handled = false;
  973. do {
  974. status = atmel_get_lines_status(port);
  975. pending = status & UART_GET_IMR(port);
  976. if (!gpio_handled) {
  977. /*
  978. * Dealing with GPIO interrupt
  979. */
  980. if (irq == atmel_port->gpio_irq[UART_GPIO_CTS])
  981. pending |= ATMEL_US_CTSIC;
  982. if (irq == atmel_port->gpio_irq[UART_GPIO_DSR])
  983. pending |= ATMEL_US_DSRIC;
  984. if (irq == atmel_port->gpio_irq[UART_GPIO_RI])
  985. pending |= ATMEL_US_RIIC;
  986. if (irq == atmel_port->gpio_irq[UART_GPIO_DCD])
  987. pending |= ATMEL_US_DCDIC;
  988. gpio_handled = true;
  989. }
  990. if (!pending)
  991. break;
  992. atmel_handle_receive(port, pending);
  993. atmel_handle_status(port, pending, status);
  994. atmel_handle_transmit(port, pending);
  995. } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
  996. return pass_counter ? IRQ_HANDLED : IRQ_NONE;
  997. }
  998. static void atmel_release_tx_pdc(struct uart_port *port)
  999. {
  1000. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1001. struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
  1002. dma_unmap_single(port->dev,
  1003. pdc->dma_addr,
  1004. pdc->dma_size,
  1005. DMA_TO_DEVICE);
  1006. }
  1007. /*
  1008. * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
  1009. */
  1010. static void atmel_tx_pdc(struct uart_port *port)
  1011. {
  1012. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1013. struct circ_buf *xmit = &port->state->xmit;
  1014. struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
  1015. int count;
  1016. /* nothing left to transmit? */
  1017. if (UART_GET_TCR(port))
  1018. return;
  1019. xmit->tail += pdc->ofs;
  1020. xmit->tail &= UART_XMIT_SIZE - 1;
  1021. port->icount.tx += pdc->ofs;
  1022. pdc->ofs = 0;
  1023. /* more to transmit - setup next transfer */
  1024. /* disable PDC transmit */
  1025. UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
  1026. if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
  1027. dma_sync_single_for_device(port->dev,
  1028. pdc->dma_addr,
  1029. pdc->dma_size,
  1030. DMA_TO_DEVICE);
  1031. count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  1032. pdc->ofs = count;
  1033. UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
  1034. UART_PUT_TCR(port, count);
  1035. /* re-enable PDC transmit */
  1036. UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
  1037. /* Enable interrupts */
  1038. UART_PUT_IER(port, atmel_port->tx_done_mask);
  1039. } else {
  1040. if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
  1041. !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX)) {
  1042. /* DMA done, stop TX, start RX for RS485 */
  1043. atmel_start_rx(port);
  1044. }
  1045. }
  1046. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  1047. uart_write_wakeup(port);
  1048. }
  1049. static int atmel_prepare_tx_pdc(struct uart_port *port)
  1050. {
  1051. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1052. struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
  1053. struct circ_buf *xmit = &port->state->xmit;
  1054. pdc->buf = xmit->buf;
  1055. pdc->dma_addr = dma_map_single(port->dev,
  1056. pdc->buf,
  1057. UART_XMIT_SIZE,
  1058. DMA_TO_DEVICE);
  1059. pdc->dma_size = UART_XMIT_SIZE;
  1060. pdc->ofs = 0;
  1061. return 0;
  1062. }
  1063. static void atmel_rx_from_ring(struct uart_port *port)
  1064. {
  1065. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1066. struct circ_buf *ring = &atmel_port->rx_ring;
  1067. unsigned int flg;
  1068. unsigned int status;
  1069. while (ring->head != ring->tail) {
  1070. struct atmel_uart_char c;
  1071. /* Make sure c is loaded after head. */
  1072. smp_rmb();
  1073. c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
  1074. ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
  1075. port->icount.rx++;
  1076. status = c.status;
  1077. flg = TTY_NORMAL;
  1078. /*
  1079. * note that the error handling code is
  1080. * out of the main execution path
  1081. */
  1082. if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
  1083. | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
  1084. if (status & ATMEL_US_RXBRK) {
  1085. /* ignore side-effect */
  1086. status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
  1087. port->icount.brk++;
  1088. if (uart_handle_break(port))
  1089. continue;
  1090. }
  1091. if (status & ATMEL_US_PARE)
  1092. port->icount.parity++;
  1093. if (status & ATMEL_US_FRAME)
  1094. port->icount.frame++;
  1095. if (status & ATMEL_US_OVRE)
  1096. port->icount.overrun++;
  1097. status &= port->read_status_mask;
  1098. if (status & ATMEL_US_RXBRK)
  1099. flg = TTY_BREAK;
  1100. else if (status & ATMEL_US_PARE)
  1101. flg = TTY_PARITY;
  1102. else if (status & ATMEL_US_FRAME)
  1103. flg = TTY_FRAME;
  1104. }
  1105. if (uart_handle_sysrq_char(port, c.ch))
  1106. continue;
  1107. uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
  1108. }
  1109. /*
  1110. * Drop the lock here since it might end up calling
  1111. * uart_start(), which takes the lock.
  1112. */
  1113. spin_unlock(&port->lock);
  1114. tty_flip_buffer_push(&port->state->port);
  1115. spin_lock(&port->lock);
  1116. }
  1117. static void atmel_release_rx_pdc(struct uart_port *port)
  1118. {
  1119. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1120. int i;
  1121. for (i = 0; i < 2; i++) {
  1122. struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
  1123. dma_unmap_single(port->dev,
  1124. pdc->dma_addr,
  1125. pdc->dma_size,
  1126. DMA_FROM_DEVICE);
  1127. kfree(pdc->buf);
  1128. }
  1129. }
  1130. static void atmel_rx_from_pdc(struct uart_port *port)
  1131. {
  1132. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1133. struct tty_port *tport = &port->state->port;
  1134. struct atmel_dma_buffer *pdc;
  1135. int rx_idx = atmel_port->pdc_rx_idx;
  1136. unsigned int head;
  1137. unsigned int tail;
  1138. unsigned int count;
  1139. do {
  1140. /* Reset the UART timeout early so that we don't miss one */
  1141. UART_PUT_CR(port, ATMEL_US_STTTO);
  1142. pdc = &atmel_port->pdc_rx[rx_idx];
  1143. head = UART_GET_RPR(port) - pdc->dma_addr;
  1144. tail = pdc->ofs;
  1145. /* If the PDC has switched buffers, RPR won't contain
  1146. * any address within the current buffer. Since head
  1147. * is unsigned, we just need a one-way comparison to
  1148. * find out.
  1149. *
  1150. * In this case, we just need to consume the entire
  1151. * buffer and resubmit it for DMA. This will clear the
  1152. * ENDRX bit as well, so that we can safely re-enable
  1153. * all interrupts below.
  1154. */
  1155. head = min(head, pdc->dma_size);
  1156. if (likely(head != tail)) {
  1157. dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
  1158. pdc->dma_size, DMA_FROM_DEVICE);
  1159. /*
  1160. * head will only wrap around when we recycle
  1161. * the DMA buffer, and when that happens, we
  1162. * explicitly set tail to 0. So head will
  1163. * always be greater than tail.
  1164. */
  1165. count = head - tail;
  1166. tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
  1167. count);
  1168. dma_sync_single_for_device(port->dev, pdc->dma_addr,
  1169. pdc->dma_size, DMA_FROM_DEVICE);
  1170. port->icount.rx += count;
  1171. pdc->ofs = head;
  1172. }
  1173. /*
  1174. * If the current buffer is full, we need to check if
  1175. * the next one contains any additional data.
  1176. */
  1177. if (head >= pdc->dma_size) {
  1178. pdc->ofs = 0;
  1179. UART_PUT_RNPR(port, pdc->dma_addr);
  1180. UART_PUT_RNCR(port, pdc->dma_size);
  1181. rx_idx = !rx_idx;
  1182. atmel_port->pdc_rx_idx = rx_idx;
  1183. }
  1184. } while (head >= pdc->dma_size);
  1185. /*
  1186. * Drop the lock here since it might end up calling
  1187. * uart_start(), which takes the lock.
  1188. */
  1189. spin_unlock(&port->lock);
  1190. tty_flip_buffer_push(tport);
  1191. spin_lock(&port->lock);
  1192. UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
  1193. }
  1194. static int atmel_prepare_rx_pdc(struct uart_port *port)
  1195. {
  1196. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1197. int i;
  1198. for (i = 0; i < 2; i++) {
  1199. struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
  1200. pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
  1201. if (pdc->buf == NULL) {
  1202. if (i != 0) {
  1203. dma_unmap_single(port->dev,
  1204. atmel_port->pdc_rx[0].dma_addr,
  1205. PDC_BUFFER_SIZE,
  1206. DMA_FROM_DEVICE);
  1207. kfree(atmel_port->pdc_rx[0].buf);
  1208. }
  1209. atmel_port->use_pdc_rx = 0;
  1210. return -ENOMEM;
  1211. }
  1212. pdc->dma_addr = dma_map_single(port->dev,
  1213. pdc->buf,
  1214. PDC_BUFFER_SIZE,
  1215. DMA_FROM_DEVICE);
  1216. pdc->dma_size = PDC_BUFFER_SIZE;
  1217. pdc->ofs = 0;
  1218. }
  1219. atmel_port->pdc_rx_idx = 0;
  1220. UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
  1221. UART_PUT_RCR(port, PDC_BUFFER_SIZE);
  1222. UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
  1223. UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
  1224. return 0;
  1225. }
  1226. /*
  1227. * tasklet handling tty stuff outside the interrupt handler.
  1228. */
  1229. static void atmel_tasklet_func(unsigned long data)
  1230. {
  1231. struct uart_port *port = (struct uart_port *)data;
  1232. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1233. unsigned int status;
  1234. unsigned int status_change;
  1235. /* The interrupt handler does not take the lock */
  1236. spin_lock(&port->lock);
  1237. atmel_port->schedule_tx(port);
  1238. status = atmel_port->irq_status;
  1239. status_change = status ^ atmel_port->irq_status_prev;
  1240. if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
  1241. | ATMEL_US_DCD | ATMEL_US_CTS)) {
  1242. /* TODO: All reads to CSR will clear these interrupts! */
  1243. if (status_change & ATMEL_US_RI)
  1244. port->icount.rng++;
  1245. if (status_change & ATMEL_US_DSR)
  1246. port->icount.dsr++;
  1247. if (status_change & ATMEL_US_DCD)
  1248. uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
  1249. if (status_change & ATMEL_US_CTS)
  1250. uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
  1251. wake_up_interruptible(&port->state->port.delta_msr_wait);
  1252. atmel_port->irq_status_prev = status;
  1253. }
  1254. atmel_port->schedule_rx(port);
  1255. spin_unlock(&port->lock);
  1256. }
  1257. static int atmel_init_property(struct atmel_uart_port *atmel_port,
  1258. struct platform_device *pdev)
  1259. {
  1260. struct device_node *np = pdev->dev.of_node;
  1261. struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
  1262. if (np) {
  1263. /* DMA/PDC usage specification */
  1264. if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
  1265. if (of_get_property(np, "dmas", NULL)) {
  1266. atmel_port->use_dma_rx = true;
  1267. atmel_port->use_pdc_rx = false;
  1268. } else {
  1269. atmel_port->use_dma_rx = false;
  1270. atmel_port->use_pdc_rx = true;
  1271. }
  1272. } else {
  1273. atmel_port->use_dma_rx = false;
  1274. atmel_port->use_pdc_rx = false;
  1275. }
  1276. if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
  1277. if (of_get_property(np, "dmas", NULL)) {
  1278. atmel_port->use_dma_tx = true;
  1279. atmel_port->use_pdc_tx = false;
  1280. } else {
  1281. atmel_port->use_dma_tx = false;
  1282. atmel_port->use_pdc_tx = true;
  1283. }
  1284. } else {
  1285. atmel_port->use_dma_tx = false;
  1286. atmel_port->use_pdc_tx = false;
  1287. }
  1288. } else {
  1289. atmel_port->use_pdc_rx = pdata->use_dma_rx;
  1290. atmel_port->use_pdc_tx = pdata->use_dma_tx;
  1291. atmel_port->use_dma_rx = false;
  1292. atmel_port->use_dma_tx = false;
  1293. }
  1294. return 0;
  1295. }
  1296. static void atmel_init_rs485(struct atmel_uart_port *atmel_port,
  1297. struct platform_device *pdev)
  1298. {
  1299. struct device_node *np = pdev->dev.of_node;
  1300. struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
  1301. if (np) {
  1302. u32 rs485_delay[2];
  1303. /* rs485 properties */
  1304. if (of_property_read_u32_array(np, "rs485-rts-delay",
  1305. rs485_delay, 2) == 0) {
  1306. struct serial_rs485 *rs485conf = &atmel_port->rs485;
  1307. rs485conf->delay_rts_before_send = rs485_delay[0];
  1308. rs485conf->delay_rts_after_send = rs485_delay[1];
  1309. rs485conf->flags = 0;
  1310. if (of_get_property(np, "rs485-rx-during-tx", NULL))
  1311. rs485conf->flags |= SER_RS485_RX_DURING_TX;
  1312. if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
  1313. NULL))
  1314. rs485conf->flags |= SER_RS485_ENABLED;
  1315. }
  1316. } else {
  1317. atmel_port->rs485 = pdata->rs485;
  1318. }
  1319. }
  1320. static void atmel_set_ops(struct uart_port *port)
  1321. {
  1322. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1323. if (atmel_use_dma_rx(port)) {
  1324. atmel_port->prepare_rx = &atmel_prepare_rx_dma;
  1325. atmel_port->schedule_rx = &atmel_rx_from_dma;
  1326. atmel_port->release_rx = &atmel_release_rx_dma;
  1327. } else if (atmel_use_pdc_rx(port)) {
  1328. atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
  1329. atmel_port->schedule_rx = &atmel_rx_from_pdc;
  1330. atmel_port->release_rx = &atmel_release_rx_pdc;
  1331. } else {
  1332. atmel_port->prepare_rx = NULL;
  1333. atmel_port->schedule_rx = &atmel_rx_from_ring;
  1334. atmel_port->release_rx = NULL;
  1335. }
  1336. if (atmel_use_dma_tx(port)) {
  1337. atmel_port->prepare_tx = &atmel_prepare_tx_dma;
  1338. atmel_port->schedule_tx = &atmel_tx_dma;
  1339. atmel_port->release_tx = &atmel_release_tx_dma;
  1340. } else if (atmel_use_pdc_tx(port)) {
  1341. atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
  1342. atmel_port->schedule_tx = &atmel_tx_pdc;
  1343. atmel_port->release_tx = &atmel_release_tx_pdc;
  1344. } else {
  1345. atmel_port->prepare_tx = NULL;
  1346. atmel_port->schedule_tx = &atmel_tx_chars;
  1347. atmel_port->release_tx = NULL;
  1348. }
  1349. }
  1350. /*
  1351. * Get ip name usart or uart
  1352. */
  1353. static void atmel_get_ip_name(struct uart_port *port)
  1354. {
  1355. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1356. int name = UART_GET_IP_NAME(port);
  1357. u32 version;
  1358. int usart, uart;
  1359. /* usart and uart ascii */
  1360. usart = 0x55534152;
  1361. uart = 0x44424755;
  1362. atmel_port->is_usart = false;
  1363. if (name == usart) {
  1364. dev_dbg(port->dev, "This is usart\n");
  1365. atmel_port->is_usart = true;
  1366. } else if (name == uart) {
  1367. dev_dbg(port->dev, "This is uart\n");
  1368. atmel_port->is_usart = false;
  1369. } else {
  1370. /* fallback for older SoCs: use version field */
  1371. version = UART_GET_IP_VERSION(port);
  1372. switch (version) {
  1373. case 0x302:
  1374. case 0x10213:
  1375. dev_dbg(port->dev, "This version is usart\n");
  1376. atmel_port->is_usart = true;
  1377. break;
  1378. case 0x203:
  1379. case 0x10202:
  1380. dev_dbg(port->dev, "This version is uart\n");
  1381. atmel_port->is_usart = false;
  1382. break;
  1383. default:
  1384. dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
  1385. }
  1386. }
  1387. }
  1388. static void atmel_free_gpio_irq(struct uart_port *port)
  1389. {
  1390. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1391. enum mctrl_gpio_idx i;
  1392. for (i = 0; i < UART_GPIO_MAX; i++)
  1393. if (atmel_port->gpio_irq[i] >= 0)
  1394. free_irq(atmel_port->gpio_irq[i], port);
  1395. }
  1396. static int atmel_request_gpio_irq(struct uart_port *port)
  1397. {
  1398. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1399. int *irq = atmel_port->gpio_irq;
  1400. enum mctrl_gpio_idx i;
  1401. int err = 0;
  1402. for (i = 0; (i < UART_GPIO_MAX) && !err; i++) {
  1403. if (irq[i] < 0)
  1404. continue;
  1405. irq_set_status_flags(irq[i], IRQ_NOAUTOEN);
  1406. err = request_irq(irq[i], atmel_interrupt, IRQ_TYPE_EDGE_BOTH,
  1407. "atmel_serial", port);
  1408. if (err)
  1409. dev_err(port->dev, "atmel_startup - Can't get %d irq\n",
  1410. irq[i]);
  1411. }
  1412. /*
  1413. * If something went wrong, rollback.
  1414. */
  1415. while (err && (--i >= 0))
  1416. if (irq[i] >= 0)
  1417. free_irq(irq[i], port);
  1418. return err;
  1419. }
  1420. /*
  1421. * Perform initialization and enable port for reception
  1422. */
  1423. static int atmel_startup(struct uart_port *port)
  1424. {
  1425. struct platform_device *pdev = to_platform_device(port->dev);
  1426. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1427. struct tty_struct *tty = port->state->port.tty;
  1428. int retval;
  1429. /*
  1430. * Ensure that no interrupts are enabled otherwise when
  1431. * request_irq() is called we could get stuck trying to
  1432. * handle an unexpected interrupt
  1433. */
  1434. UART_PUT_IDR(port, -1);
  1435. atmel_port->ms_irq_enabled = false;
  1436. /*
  1437. * Allocate the IRQ
  1438. */
  1439. retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
  1440. tty ? tty->name : "atmel_serial", port);
  1441. if (retval) {
  1442. dev_err(port->dev, "atmel_startup - Can't get irq\n");
  1443. return retval;
  1444. }
  1445. /*
  1446. * Get the GPIO lines IRQ
  1447. */
  1448. retval = atmel_request_gpio_irq(port);
  1449. if (retval)
  1450. goto free_irq;
  1451. /*
  1452. * Initialize DMA (if necessary)
  1453. */
  1454. atmel_init_property(atmel_port, pdev);
  1455. if (atmel_port->prepare_rx) {
  1456. retval = atmel_port->prepare_rx(port);
  1457. if (retval < 0)
  1458. atmel_set_ops(port);
  1459. }
  1460. if (atmel_port->prepare_tx) {
  1461. retval = atmel_port->prepare_tx(port);
  1462. if (retval < 0)
  1463. atmel_set_ops(port);
  1464. }
  1465. /* Save current CSR for comparison in atmel_tasklet_func() */
  1466. atmel_port->irq_status_prev = atmel_get_lines_status(port);
  1467. atmel_port->irq_status = atmel_port->irq_status_prev;
  1468. /*
  1469. * Finally, enable the serial port
  1470. */
  1471. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  1472. /* enable xmit & rcvr */
  1473. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  1474. setup_timer(&atmel_port->uart_timer,
  1475. atmel_uart_timer_callback,
  1476. (unsigned long)port);
  1477. if (atmel_use_pdc_rx(port)) {
  1478. /* set UART timeout */
  1479. if (!atmel_port->is_usart) {
  1480. mod_timer(&atmel_port->uart_timer,
  1481. jiffies + uart_poll_timeout(port));
  1482. /* set USART timeout */
  1483. } else {
  1484. UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
  1485. UART_PUT_CR(port, ATMEL_US_STTTO);
  1486. UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
  1487. }
  1488. /* enable PDC controller */
  1489. UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
  1490. } else if (atmel_use_dma_rx(port)) {
  1491. /* set UART timeout */
  1492. if (!atmel_port->is_usart) {
  1493. mod_timer(&atmel_port->uart_timer,
  1494. jiffies + uart_poll_timeout(port));
  1495. /* set USART timeout */
  1496. } else {
  1497. UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
  1498. UART_PUT_CR(port, ATMEL_US_STTTO);
  1499. UART_PUT_IER(port, ATMEL_US_TIMEOUT);
  1500. }
  1501. } else {
  1502. /* enable receive only */
  1503. UART_PUT_IER(port, ATMEL_US_RXRDY);
  1504. }
  1505. return 0;
  1506. free_irq:
  1507. free_irq(port->irq, port);
  1508. return retval;
  1509. }
  1510. /*
  1511. * Disable the port
  1512. */
  1513. static void atmel_shutdown(struct uart_port *port)
  1514. {
  1515. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1516. /*
  1517. * Prevent any tasklets being scheduled during
  1518. * cleanup
  1519. */
  1520. del_timer_sync(&atmel_port->uart_timer);
  1521. /*
  1522. * Clear out any scheduled tasklets before
  1523. * we destroy the buffers
  1524. */
  1525. tasklet_kill(&atmel_port->tasklet);
  1526. /*
  1527. * Ensure everything is stopped and
  1528. * disable all interrupts, port and break condition.
  1529. */
  1530. atmel_stop_rx(port);
  1531. atmel_stop_tx(port);
  1532. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  1533. UART_PUT_IDR(port, -1);
  1534. /*
  1535. * Shut-down the DMA.
  1536. */
  1537. if (atmel_port->release_rx)
  1538. atmel_port->release_rx(port);
  1539. if (atmel_port->release_tx)
  1540. atmel_port->release_tx(port);
  1541. /*
  1542. * Reset ring buffer pointers
  1543. */
  1544. atmel_port->rx_ring.head = 0;
  1545. atmel_port->rx_ring.tail = 0;
  1546. /*
  1547. * Free the interrupts
  1548. */
  1549. free_irq(port->irq, port);
  1550. atmel_free_gpio_irq(port);
  1551. atmel_port->ms_irq_enabled = false;
  1552. }
  1553. /*
  1554. * Flush any TX data submitted for DMA. Called when the TX circular
  1555. * buffer is reset.
  1556. */
  1557. static void atmel_flush_buffer(struct uart_port *port)
  1558. {
  1559. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1560. if (atmel_use_pdc_tx(port)) {
  1561. UART_PUT_TCR(port, 0);
  1562. atmel_port->pdc_tx.ofs = 0;
  1563. }
  1564. }
  1565. /*
  1566. * Power / Clock management.
  1567. */
  1568. static void atmel_serial_pm(struct uart_port *port, unsigned int state,
  1569. unsigned int oldstate)
  1570. {
  1571. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1572. switch (state) {
  1573. case 0:
  1574. /*
  1575. * Enable the peripheral clock for this serial port.
  1576. * This is called on uart_open() or a resume event.
  1577. */
  1578. clk_prepare_enable(atmel_port->clk);
  1579. /* re-enable interrupts if we disabled some on suspend */
  1580. UART_PUT_IER(port, atmel_port->backup_imr);
  1581. break;
  1582. case 3:
  1583. /* Back up the interrupt mask and disable all interrupts */
  1584. atmel_port->backup_imr = UART_GET_IMR(port);
  1585. UART_PUT_IDR(port, -1);
  1586. /*
  1587. * Disable the peripheral clock for this serial port.
  1588. * This is called on uart_close() or a suspend event.
  1589. */
  1590. clk_disable_unprepare(atmel_port->clk);
  1591. break;
  1592. default:
  1593. dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
  1594. }
  1595. }
  1596. /*
  1597. * Change the port parameters
  1598. */
  1599. static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
  1600. struct ktermios *old)
  1601. {
  1602. unsigned long flags;
  1603. unsigned int mode, imr, quot, baud;
  1604. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1605. /* Get current mode register */
  1606. mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
  1607. | ATMEL_US_NBSTOP | ATMEL_US_PAR
  1608. | ATMEL_US_USMODE);
  1609. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
  1610. quot = uart_get_divisor(port, baud);
  1611. if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
  1612. quot /= 8;
  1613. mode |= ATMEL_US_USCLKS_MCK_DIV8;
  1614. }
  1615. /* byte size */
  1616. switch (termios->c_cflag & CSIZE) {
  1617. case CS5:
  1618. mode |= ATMEL_US_CHRL_5;
  1619. break;
  1620. case CS6:
  1621. mode |= ATMEL_US_CHRL_6;
  1622. break;
  1623. case CS7:
  1624. mode |= ATMEL_US_CHRL_7;
  1625. break;
  1626. default:
  1627. mode |= ATMEL_US_CHRL_8;
  1628. break;
  1629. }
  1630. /* stop bits */
  1631. if (termios->c_cflag & CSTOPB)
  1632. mode |= ATMEL_US_NBSTOP_2;
  1633. /* parity */
  1634. if (termios->c_cflag & PARENB) {
  1635. /* Mark or Space parity */
  1636. if (termios->c_cflag & CMSPAR) {
  1637. if (termios->c_cflag & PARODD)
  1638. mode |= ATMEL_US_PAR_MARK;
  1639. else
  1640. mode |= ATMEL_US_PAR_SPACE;
  1641. } else if (termios->c_cflag & PARODD)
  1642. mode |= ATMEL_US_PAR_ODD;
  1643. else
  1644. mode |= ATMEL_US_PAR_EVEN;
  1645. } else
  1646. mode |= ATMEL_US_PAR_NONE;
  1647. /* hardware handshake (RTS/CTS) */
  1648. if (termios->c_cflag & CRTSCTS)
  1649. mode |= ATMEL_US_USMODE_HWHS;
  1650. else
  1651. mode |= ATMEL_US_USMODE_NORMAL;
  1652. spin_lock_irqsave(&port->lock, flags);
  1653. port->read_status_mask = ATMEL_US_OVRE;
  1654. if (termios->c_iflag & INPCK)
  1655. port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
  1656. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  1657. port->read_status_mask |= ATMEL_US_RXBRK;
  1658. if (atmel_use_pdc_rx(port))
  1659. /* need to enable error interrupts */
  1660. UART_PUT_IER(port, port->read_status_mask);
  1661. /*
  1662. * Characters to ignore
  1663. */
  1664. port->ignore_status_mask = 0;
  1665. if (termios->c_iflag & IGNPAR)
  1666. port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
  1667. if (termios->c_iflag & IGNBRK) {
  1668. port->ignore_status_mask |= ATMEL_US_RXBRK;
  1669. /*
  1670. * If we're ignoring parity and break indicators,
  1671. * ignore overruns too (for real raw support).
  1672. */
  1673. if (termios->c_iflag & IGNPAR)
  1674. port->ignore_status_mask |= ATMEL_US_OVRE;
  1675. }
  1676. /* TODO: Ignore all characters if CREAD is set.*/
  1677. /* update the per-port timeout */
  1678. uart_update_timeout(port, termios->c_cflag, baud);
  1679. /*
  1680. * save/disable interrupts. The tty layer will ensure that the
  1681. * transmitter is empty if requested by the caller, so there's
  1682. * no need to wait for it here.
  1683. */
  1684. imr = UART_GET_IMR(port);
  1685. UART_PUT_IDR(port, -1);
  1686. /* disable receiver and transmitter */
  1687. UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
  1688. /* Resetting serial mode to RS232 (0x0) */
  1689. mode &= ~ATMEL_US_USMODE;
  1690. if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
  1691. if ((atmel_port->rs485.delay_rts_after_send) > 0)
  1692. UART_PUT_TTGR(port,
  1693. atmel_port->rs485.delay_rts_after_send);
  1694. mode |= ATMEL_US_USMODE_RS485;
  1695. }
  1696. /* set the parity, stop bits and data size */
  1697. UART_PUT_MR(port, mode);
  1698. /* set the baud rate */
  1699. UART_PUT_BRGR(port, quot);
  1700. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  1701. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  1702. /* restore interrupts */
  1703. UART_PUT_IER(port, imr);
  1704. /* CTS flow-control and modem-status interrupts */
  1705. if (UART_ENABLE_MS(port, termios->c_cflag))
  1706. atmel_enable_ms(port);
  1707. else
  1708. atmel_disable_ms(port);
  1709. spin_unlock_irqrestore(&port->lock, flags);
  1710. }
  1711. static void atmel_set_ldisc(struct uart_port *port, int new)
  1712. {
  1713. if (new == N_PPS) {
  1714. port->flags |= UPF_HARDPPS_CD;
  1715. atmel_enable_ms(port);
  1716. } else {
  1717. port->flags &= ~UPF_HARDPPS_CD;
  1718. }
  1719. }
  1720. /*
  1721. * Return string describing the specified port
  1722. */
  1723. static const char *atmel_type(struct uart_port *port)
  1724. {
  1725. return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
  1726. }
  1727. /*
  1728. * Release the memory region(s) being used by 'port'.
  1729. */
  1730. static void atmel_release_port(struct uart_port *port)
  1731. {
  1732. struct platform_device *pdev = to_platform_device(port->dev);
  1733. int size = pdev->resource[0].end - pdev->resource[0].start + 1;
  1734. release_mem_region(port->mapbase, size);
  1735. if (port->flags & UPF_IOREMAP) {
  1736. iounmap(port->membase);
  1737. port->membase = NULL;
  1738. }
  1739. }
  1740. /*
  1741. * Request the memory region(s) being used by 'port'.
  1742. */
  1743. static int atmel_request_port(struct uart_port *port)
  1744. {
  1745. struct platform_device *pdev = to_platform_device(port->dev);
  1746. int size = pdev->resource[0].end - pdev->resource[0].start + 1;
  1747. if (!request_mem_region(port->mapbase, size, "atmel_serial"))
  1748. return -EBUSY;
  1749. if (port->flags & UPF_IOREMAP) {
  1750. port->membase = ioremap(port->mapbase, size);
  1751. if (port->membase == NULL) {
  1752. release_mem_region(port->mapbase, size);
  1753. return -ENOMEM;
  1754. }
  1755. }
  1756. return 0;
  1757. }
  1758. /*
  1759. * Configure/autoconfigure the port.
  1760. */
  1761. static void atmel_config_port(struct uart_port *port, int flags)
  1762. {
  1763. if (flags & UART_CONFIG_TYPE) {
  1764. port->type = PORT_ATMEL;
  1765. atmel_request_port(port);
  1766. }
  1767. }
  1768. /*
  1769. * Verify the new serial_struct (for TIOCSSERIAL).
  1770. */
  1771. static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
  1772. {
  1773. int ret = 0;
  1774. if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
  1775. ret = -EINVAL;
  1776. if (port->irq != ser->irq)
  1777. ret = -EINVAL;
  1778. if (ser->io_type != SERIAL_IO_MEM)
  1779. ret = -EINVAL;
  1780. if (port->uartclk / 16 != ser->baud_base)
  1781. ret = -EINVAL;
  1782. if ((void *)port->mapbase != ser->iomem_base)
  1783. ret = -EINVAL;
  1784. if (port->iobase != ser->port)
  1785. ret = -EINVAL;
  1786. if (ser->hub6 != 0)
  1787. ret = -EINVAL;
  1788. return ret;
  1789. }
  1790. #ifdef CONFIG_CONSOLE_POLL
  1791. static int atmel_poll_get_char(struct uart_port *port)
  1792. {
  1793. while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY))
  1794. cpu_relax();
  1795. return UART_GET_CHAR(port);
  1796. }
  1797. static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
  1798. {
  1799. while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
  1800. cpu_relax();
  1801. UART_PUT_CHAR(port, ch);
  1802. }
  1803. #endif
  1804. static int
  1805. atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
  1806. {
  1807. struct serial_rs485 rs485conf;
  1808. switch (cmd) {
  1809. case TIOCSRS485:
  1810. if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
  1811. sizeof(rs485conf)))
  1812. return -EFAULT;
  1813. atmel_config_rs485(port, &rs485conf);
  1814. break;
  1815. case TIOCGRS485:
  1816. if (copy_to_user((struct serial_rs485 *) arg,
  1817. &(to_atmel_uart_port(port)->rs485),
  1818. sizeof(rs485conf)))
  1819. return -EFAULT;
  1820. break;
  1821. default:
  1822. return -ENOIOCTLCMD;
  1823. }
  1824. return 0;
  1825. }
  1826. static struct uart_ops atmel_pops = {
  1827. .tx_empty = atmel_tx_empty,
  1828. .set_mctrl = atmel_set_mctrl,
  1829. .get_mctrl = atmel_get_mctrl,
  1830. .stop_tx = atmel_stop_tx,
  1831. .start_tx = atmel_start_tx,
  1832. .stop_rx = atmel_stop_rx,
  1833. .enable_ms = atmel_enable_ms,
  1834. .break_ctl = atmel_break_ctl,
  1835. .startup = atmel_startup,
  1836. .shutdown = atmel_shutdown,
  1837. .flush_buffer = atmel_flush_buffer,
  1838. .set_termios = atmel_set_termios,
  1839. .set_ldisc = atmel_set_ldisc,
  1840. .type = atmel_type,
  1841. .release_port = atmel_release_port,
  1842. .request_port = atmel_request_port,
  1843. .config_port = atmel_config_port,
  1844. .verify_port = atmel_verify_port,
  1845. .pm = atmel_serial_pm,
  1846. .ioctl = atmel_ioctl,
  1847. #ifdef CONFIG_CONSOLE_POLL
  1848. .poll_get_char = atmel_poll_get_char,
  1849. .poll_put_char = atmel_poll_put_char,
  1850. #endif
  1851. };
  1852. /*
  1853. * Configure the port from the platform device resource info.
  1854. */
  1855. static int atmel_init_port(struct atmel_uart_port *atmel_port,
  1856. struct platform_device *pdev)
  1857. {
  1858. int ret;
  1859. struct uart_port *port = &atmel_port->uart;
  1860. struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
  1861. if (!atmel_init_property(atmel_port, pdev))
  1862. atmel_set_ops(port);
  1863. atmel_init_rs485(atmel_port, pdev);
  1864. port->iotype = UPIO_MEM;
  1865. port->flags = UPF_BOOT_AUTOCONF;
  1866. port->ops = &atmel_pops;
  1867. port->fifosize = 1;
  1868. port->dev = &pdev->dev;
  1869. port->mapbase = pdev->resource[0].start;
  1870. port->irq = pdev->resource[1].start;
  1871. tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
  1872. (unsigned long)port);
  1873. memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
  1874. if (pdata && pdata->regs) {
  1875. /* Already mapped by setup code */
  1876. port->membase = pdata->regs;
  1877. } else {
  1878. port->flags |= UPF_IOREMAP;
  1879. port->membase = NULL;
  1880. }
  1881. /* for console, the clock could already be configured */
  1882. if (!atmel_port->clk) {
  1883. atmel_port->clk = clk_get(&pdev->dev, "usart");
  1884. if (IS_ERR(atmel_port->clk)) {
  1885. ret = PTR_ERR(atmel_port->clk);
  1886. atmel_port->clk = NULL;
  1887. return ret;
  1888. }
  1889. ret = clk_prepare_enable(atmel_port->clk);
  1890. if (ret) {
  1891. clk_put(atmel_port->clk);
  1892. atmel_port->clk = NULL;
  1893. return ret;
  1894. }
  1895. port->uartclk = clk_get_rate(atmel_port->clk);
  1896. clk_disable_unprepare(atmel_port->clk);
  1897. /* only enable clock when USART is in use */
  1898. }
  1899. /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
  1900. if (atmel_port->rs485.flags & SER_RS485_ENABLED)
  1901. atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
  1902. else if (atmel_use_pdc_tx(port)) {
  1903. port->fifosize = PDC_BUFFER_SIZE;
  1904. atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
  1905. } else {
  1906. atmel_port->tx_done_mask = ATMEL_US_TXRDY;
  1907. }
  1908. return 0;
  1909. }
  1910. struct platform_device *atmel_default_console_device; /* the serial console device */
  1911. #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
  1912. static void atmel_console_putchar(struct uart_port *port, int ch)
  1913. {
  1914. while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
  1915. cpu_relax();
  1916. UART_PUT_CHAR(port, ch);
  1917. }
  1918. /*
  1919. * Interrupts are disabled on entering
  1920. */
  1921. static void atmel_console_write(struct console *co, const char *s, u_int count)
  1922. {
  1923. struct uart_port *port = &atmel_ports[co->index].uart;
  1924. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1925. unsigned int status, imr;
  1926. unsigned int pdc_tx;
  1927. /*
  1928. * First, save IMR and then disable interrupts
  1929. */
  1930. imr = UART_GET_IMR(port);
  1931. UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);
  1932. /* Store PDC transmit status and disable it */
  1933. pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
  1934. UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
  1935. uart_console_write(port, s, count, atmel_console_putchar);
  1936. /*
  1937. * Finally, wait for transmitter to become empty
  1938. * and restore IMR
  1939. */
  1940. do {
  1941. status = UART_GET_CSR(port);
  1942. } while (!(status & ATMEL_US_TXRDY));
  1943. /* Restore PDC transmit status */
  1944. if (pdc_tx)
  1945. UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
  1946. /* set interrupts back the way they were */
  1947. UART_PUT_IER(port, imr);
  1948. }
  1949. /*
  1950. * If the port was already initialised (eg, by a boot loader),
  1951. * try to determine the current setup.
  1952. */
  1953. static void __init atmel_console_get_options(struct uart_port *port, int *baud,
  1954. int *parity, int *bits)
  1955. {
  1956. unsigned int mr, quot;
  1957. /*
  1958. * If the baud rate generator isn't running, the port wasn't
  1959. * initialized by the boot loader.
  1960. */
  1961. quot = UART_GET_BRGR(port) & ATMEL_US_CD;
  1962. if (!quot)
  1963. return;
  1964. mr = UART_GET_MR(port) & ATMEL_US_CHRL;
  1965. if (mr == ATMEL_US_CHRL_8)
  1966. *bits = 8;
  1967. else
  1968. *bits = 7;
  1969. mr = UART_GET_MR(port) & ATMEL_US_PAR;
  1970. if (mr == ATMEL_US_PAR_EVEN)
  1971. *parity = 'e';
  1972. else if (mr == ATMEL_US_PAR_ODD)
  1973. *parity = 'o';
  1974. /*
  1975. * The serial core only rounds down when matching this to a
  1976. * supported baud rate. Make sure we don't end up slightly
  1977. * lower than one of those, as it would make us fall through
  1978. * to a much lower baud rate than we really want.
  1979. */
  1980. *baud = port->uartclk / (16 * (quot - 1));
  1981. }
  1982. static int __init atmel_console_setup(struct console *co, char *options)
  1983. {
  1984. int ret;
  1985. struct uart_port *port = &atmel_ports[co->index].uart;
  1986. int baud = 115200;
  1987. int bits = 8;
  1988. int parity = 'n';
  1989. int flow = 'n';
  1990. if (port->membase == NULL) {
  1991. /* Port not initialized yet - delay setup */
  1992. return -ENODEV;
  1993. }
  1994. ret = clk_prepare_enable(atmel_ports[co->index].clk);
  1995. if (ret)
  1996. return ret;
  1997. UART_PUT_IDR(port, -1);
  1998. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  1999. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  2000. if (options)
  2001. uart_parse_options(options, &baud, &parity, &bits, &flow);
  2002. else
  2003. atmel_console_get_options(port, &baud, &parity, &bits);
  2004. return uart_set_options(port, co, baud, parity, bits, flow);
  2005. }
  2006. static struct uart_driver atmel_uart;
  2007. static struct console atmel_console = {
  2008. .name = ATMEL_DEVICENAME,
  2009. .write = atmel_console_write,
  2010. .device = uart_console_device,
  2011. .setup = atmel_console_setup,
  2012. .flags = CON_PRINTBUFFER,
  2013. .index = -1,
  2014. .data = &atmel_uart,
  2015. };
  2016. #define ATMEL_CONSOLE_DEVICE (&atmel_console)
  2017. /*
  2018. * Early console initialization (before VM subsystem initialized).
  2019. */
  2020. static int __init atmel_console_init(void)
  2021. {
  2022. int ret;
  2023. if (atmel_default_console_device) {
  2024. struct atmel_uart_data *pdata =
  2025. dev_get_platdata(&atmel_default_console_device->dev);
  2026. int id = pdata->num;
  2027. struct atmel_uart_port *port = &atmel_ports[id];
  2028. port->backup_imr = 0;
  2029. port->uart.line = id;
  2030. add_preferred_console(ATMEL_DEVICENAME, id, NULL);
  2031. ret = atmel_init_port(port, atmel_default_console_device);
  2032. if (ret)
  2033. return ret;
  2034. register_console(&atmel_console);
  2035. }
  2036. return 0;
  2037. }
  2038. console_initcall(atmel_console_init);
  2039. /*
  2040. * Late console initialization.
  2041. */
  2042. static int __init atmel_late_console_init(void)
  2043. {
  2044. if (atmel_default_console_device
  2045. && !(atmel_console.flags & CON_ENABLED))
  2046. register_console(&atmel_console);
  2047. return 0;
  2048. }
  2049. core_initcall(atmel_late_console_init);
  2050. static inline bool atmel_is_console_port(struct uart_port *port)
  2051. {
  2052. return port->cons && port->cons->index == port->line;
  2053. }
  2054. #else
  2055. #define ATMEL_CONSOLE_DEVICE NULL
  2056. static inline bool atmel_is_console_port(struct uart_port *port)
  2057. {
  2058. return false;
  2059. }
  2060. #endif
  2061. static struct uart_driver atmel_uart = {
  2062. .owner = THIS_MODULE,
  2063. .driver_name = "atmel_serial",
  2064. .dev_name = ATMEL_DEVICENAME,
  2065. .major = SERIAL_ATMEL_MAJOR,
  2066. .minor = MINOR_START,
  2067. .nr = ATMEL_MAX_UART,
  2068. .cons = ATMEL_CONSOLE_DEVICE,
  2069. };
  2070. #ifdef CONFIG_PM
  2071. static bool atmel_serial_clk_will_stop(void)
  2072. {
  2073. #ifdef CONFIG_ARCH_AT91
  2074. return at91_suspend_entering_slow_clock();
  2075. #else
  2076. return false;
  2077. #endif
  2078. }
  2079. static int atmel_serial_suspend(struct platform_device *pdev,
  2080. pm_message_t state)
  2081. {
  2082. struct uart_port *port = platform_get_drvdata(pdev);
  2083. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  2084. if (atmel_is_console_port(port) && console_suspend_enabled) {
  2085. /* Drain the TX shifter */
  2086. while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
  2087. cpu_relax();
  2088. }
  2089. /* we can not wake up if we're running on slow clock */
  2090. atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
  2091. if (atmel_serial_clk_will_stop())
  2092. device_set_wakeup_enable(&pdev->dev, 0);
  2093. uart_suspend_port(&atmel_uart, port);
  2094. return 0;
  2095. }
  2096. static int atmel_serial_resume(struct platform_device *pdev)
  2097. {
  2098. struct uart_port *port = platform_get_drvdata(pdev);
  2099. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  2100. uart_resume_port(&atmel_uart, port);
  2101. device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
  2102. return 0;
  2103. }
  2104. #else
  2105. #define atmel_serial_suspend NULL
  2106. #define atmel_serial_resume NULL
  2107. #endif
  2108. static int atmel_init_gpios(struct atmel_uart_port *p, struct device *dev)
  2109. {
  2110. enum mctrl_gpio_idx i;
  2111. struct gpio_desc *gpiod;
  2112. p->gpios = mctrl_gpio_init(dev, 0);
  2113. if (IS_ERR_OR_NULL(p->gpios))
  2114. return -1;
  2115. for (i = 0; i < UART_GPIO_MAX; i++) {
  2116. gpiod = mctrl_gpio_to_gpiod(p->gpios, i);
  2117. if (gpiod && (gpiod_get_direction(gpiod) == GPIOF_DIR_IN))
  2118. p->gpio_irq[i] = gpiod_to_irq(gpiod);
  2119. else
  2120. p->gpio_irq[i] = -EINVAL;
  2121. }
  2122. return 0;
  2123. }
  2124. static int atmel_serial_probe(struct platform_device *pdev)
  2125. {
  2126. struct atmel_uart_port *port;
  2127. struct device_node *np = pdev->dev.of_node;
  2128. struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
  2129. void *data;
  2130. int ret = -ENODEV;
  2131. BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
  2132. if (np)
  2133. ret = of_alias_get_id(np, "serial");
  2134. else
  2135. if (pdata)
  2136. ret = pdata->num;
  2137. if (ret < 0)
  2138. /* port id not found in platform data nor device-tree aliases:
  2139. * auto-enumerate it */
  2140. ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
  2141. if (ret >= ATMEL_MAX_UART) {
  2142. ret = -ENODEV;
  2143. goto err;
  2144. }
  2145. if (test_and_set_bit(ret, atmel_ports_in_use)) {
  2146. /* port already in use */
  2147. ret = -EBUSY;
  2148. goto err;
  2149. }
  2150. port = &atmel_ports[ret];
  2151. port->backup_imr = 0;
  2152. port->uart.line = ret;
  2153. ret = atmel_init_gpios(port, &pdev->dev);
  2154. if (ret < 0)
  2155. dev_err(&pdev->dev, "%s",
  2156. "Failed to initialize GPIOs. The serial port may not work as expected");
  2157. ret = atmel_init_port(port, pdev);
  2158. if (ret)
  2159. goto err;
  2160. if (!atmel_use_pdc_rx(&port->uart)) {
  2161. ret = -ENOMEM;
  2162. data = kmalloc(sizeof(struct atmel_uart_char)
  2163. * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
  2164. if (!data)
  2165. goto err_alloc_ring;
  2166. port->rx_ring.buf = data;
  2167. }
  2168. ret = uart_add_one_port(&atmel_uart, &port->uart);
  2169. if (ret)
  2170. goto err_add_port;
  2171. #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
  2172. if (atmel_is_console_port(&port->uart)
  2173. && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
  2174. /*
  2175. * The serial core enabled the clock for us, so undo
  2176. * the clk_prepare_enable() in atmel_console_setup()
  2177. */
  2178. clk_disable_unprepare(port->clk);
  2179. }
  2180. #endif
  2181. device_init_wakeup(&pdev->dev, 1);
  2182. platform_set_drvdata(pdev, port);
  2183. if (port->rs485.flags & SER_RS485_ENABLED) {
  2184. UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL);
  2185. UART_PUT_CR(&port->uart, ATMEL_US_RTSEN);
  2186. }
  2187. /*
  2188. * Get port name of usart or uart
  2189. */
  2190. atmel_get_ip_name(&port->uart);
  2191. return 0;
  2192. err_add_port:
  2193. kfree(port->rx_ring.buf);
  2194. port->rx_ring.buf = NULL;
  2195. err_alloc_ring:
  2196. if (!atmel_is_console_port(&port->uart)) {
  2197. clk_put(port->clk);
  2198. port->clk = NULL;
  2199. }
  2200. err:
  2201. return ret;
  2202. }
  2203. static int atmel_serial_remove(struct platform_device *pdev)
  2204. {
  2205. struct uart_port *port = platform_get_drvdata(pdev);
  2206. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  2207. int ret = 0;
  2208. tasklet_kill(&atmel_port->tasklet);
  2209. device_init_wakeup(&pdev->dev, 0);
  2210. ret = uart_remove_one_port(&atmel_uart, port);
  2211. kfree(atmel_port->rx_ring.buf);
  2212. /* "port" is allocated statically, so we shouldn't free it */
  2213. clear_bit(port->line, atmel_ports_in_use);
  2214. clk_put(atmel_port->clk);
  2215. return ret;
  2216. }
  2217. static struct platform_driver atmel_serial_driver = {
  2218. .probe = atmel_serial_probe,
  2219. .remove = atmel_serial_remove,
  2220. .suspend = atmel_serial_suspend,
  2221. .resume = atmel_serial_resume,
  2222. .driver = {
  2223. .name = "atmel_usart",
  2224. .owner = THIS_MODULE,
  2225. .of_match_table = of_match_ptr(atmel_serial_dt_ids),
  2226. },
  2227. };
  2228. static int __init atmel_serial_init(void)
  2229. {
  2230. int ret;
  2231. ret = uart_register_driver(&atmel_uart);
  2232. if (ret)
  2233. return ret;
  2234. ret = platform_driver_register(&atmel_serial_driver);
  2235. if (ret)
  2236. uart_unregister_driver(&atmel_uart);
  2237. return ret;
  2238. }
  2239. static void __exit atmel_serial_exit(void)
  2240. {
  2241. platform_driver_unregister(&atmel_serial_driver);
  2242. uart_unregister_driver(&atmel_uart);
  2243. }
  2244. module_init(atmel_serial_init);
  2245. module_exit(atmel_serial_exit);
  2246. MODULE_AUTHOR("Rick Bronson");
  2247. MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
  2248. MODULE_LICENSE("GPL");
  2249. MODULE_ALIAS("platform:atmel_usart");