atmel_serial.c 76 KB

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