atmel_serial.c 49 KB

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