atmel_serial.c 62 KB

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