atmel_serial.c 79 KB

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