atmel_serial.c 75 KB

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