atmel_serial.c 66 KB

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