sh-sci.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467
  1. /*
  2. * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
  3. *
  4. * Copyright (C) 2002 - 2011 Paul Mundt
  5. * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
  6. *
  7. * based off of the old drivers/char/sh-sci.c by:
  8. *
  9. * Copyright (C) 1999, 2000 Niibe Yutaka
  10. * Copyright (C) 2000 Sugioka Toshinobu
  11. * Modified to support multiple serial ports. Stuart Menefy (May 2000).
  12. * Modified to support SecureEdge. David McCullough (2002)
  13. * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
  14. * Removed SH7300 support (Jul 2007).
  15. *
  16. * This file is subject to the terms and conditions of the GNU General Public
  17. * License. See the file "COPYING" in the main directory of this archive
  18. * for more details.
  19. */
  20. #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  21. #define SUPPORT_SYSRQ
  22. #endif
  23. #undef DEBUG
  24. #include <linux/module.h>
  25. #include <linux/errno.h>
  26. #include <linux/timer.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/tty.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/serial.h>
  31. #include <linux/major.h>
  32. #include <linux/string.h>
  33. #include <linux/sysrq.h>
  34. #include <linux/ioport.h>
  35. #include <linux/mm.h>
  36. #include <linux/init.h>
  37. #include <linux/delay.h>
  38. #include <linux/console.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/serial_sci.h>
  41. #include <linux/notifier.h>
  42. #include <linux/pm_runtime.h>
  43. #include <linux/cpufreq.h>
  44. #include <linux/clk.h>
  45. #include <linux/ctype.h>
  46. #include <linux/err.h>
  47. #include <linux/dmaengine.h>
  48. #include <linux/dma-mapping.h>
  49. #include <linux/scatterlist.h>
  50. #include <linux/slab.h>
  51. #include <linux/gpio.h>
  52. #ifdef CONFIG_SUPERH
  53. #include <asm/sh_bios.h>
  54. #endif
  55. #include "sh-sci.h"
  56. struct sci_port {
  57. struct uart_port port;
  58. /* Platform configuration */
  59. struct plat_sci_port *cfg;
  60. /* Break timer */
  61. struct timer_list break_timer;
  62. int break_flag;
  63. /* Interface clock */
  64. struct clk *iclk;
  65. /* Function clock */
  66. struct clk *fclk;
  67. char *irqstr[SCIx_NR_IRQS];
  68. char *gpiostr[SCIx_NR_FNS];
  69. struct dma_chan *chan_tx;
  70. struct dma_chan *chan_rx;
  71. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  72. struct dma_async_tx_descriptor *desc_tx;
  73. struct dma_async_tx_descriptor *desc_rx[2];
  74. dma_cookie_t cookie_tx;
  75. dma_cookie_t cookie_rx[2];
  76. dma_cookie_t active_rx;
  77. struct scatterlist sg_tx;
  78. unsigned int sg_len_tx;
  79. struct scatterlist sg_rx[2];
  80. size_t buf_len_rx;
  81. struct sh_dmae_slave param_tx;
  82. struct sh_dmae_slave param_rx;
  83. struct work_struct work_tx;
  84. struct work_struct work_rx;
  85. struct timer_list rx_timer;
  86. unsigned int rx_timeout;
  87. #endif
  88. struct notifier_block freq_transition;
  89. #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
  90. unsigned short saved_smr;
  91. unsigned short saved_fcr;
  92. unsigned char saved_brr;
  93. #endif
  94. };
  95. /* Function prototypes */
  96. static void sci_start_tx(struct uart_port *port);
  97. static void sci_stop_tx(struct uart_port *port);
  98. static void sci_start_rx(struct uart_port *port);
  99. #define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
  100. static struct sci_port sci_ports[SCI_NPORTS];
  101. static struct uart_driver sci_uart_driver;
  102. static inline struct sci_port *
  103. to_sci_port(struct uart_port *uart)
  104. {
  105. return container_of(uart, struct sci_port, port);
  106. }
  107. struct plat_sci_reg {
  108. u8 offset, size;
  109. };
  110. /* Helper for invalidating specific entries of an inherited map. */
  111. #define sci_reg_invalid { .offset = 0, .size = 0 }
  112. static struct plat_sci_reg sci_regmap[SCIx_NR_REGTYPES][SCIx_NR_REGS] = {
  113. [SCIx_PROBE_REGTYPE] = {
  114. [0 ... SCIx_NR_REGS - 1] = sci_reg_invalid,
  115. },
  116. /*
  117. * Common SCI definitions, dependent on the port's regshift
  118. * value.
  119. */
  120. [SCIx_SCI_REGTYPE] = {
  121. [SCSMR] = { 0x00, 8 },
  122. [SCBRR] = { 0x01, 8 },
  123. [SCSCR] = { 0x02, 8 },
  124. [SCxTDR] = { 0x03, 8 },
  125. [SCxSR] = { 0x04, 8 },
  126. [SCxRDR] = { 0x05, 8 },
  127. [SCFCR] = sci_reg_invalid,
  128. [SCFDR] = sci_reg_invalid,
  129. [SCTFDR] = sci_reg_invalid,
  130. [SCRFDR] = sci_reg_invalid,
  131. [SCSPTR] = sci_reg_invalid,
  132. [SCLSR] = sci_reg_invalid,
  133. },
  134. /*
  135. * Common definitions for legacy IrDA ports, dependent on
  136. * regshift value.
  137. */
  138. [SCIx_IRDA_REGTYPE] = {
  139. [SCSMR] = { 0x00, 8 },
  140. [SCBRR] = { 0x01, 8 },
  141. [SCSCR] = { 0x02, 8 },
  142. [SCxTDR] = { 0x03, 8 },
  143. [SCxSR] = { 0x04, 8 },
  144. [SCxRDR] = { 0x05, 8 },
  145. [SCFCR] = { 0x06, 8 },
  146. [SCFDR] = { 0x07, 16 },
  147. [SCTFDR] = sci_reg_invalid,
  148. [SCRFDR] = sci_reg_invalid,
  149. [SCSPTR] = sci_reg_invalid,
  150. [SCLSR] = sci_reg_invalid,
  151. },
  152. /*
  153. * Common SCIFA definitions.
  154. */
  155. [SCIx_SCIFA_REGTYPE] = {
  156. [SCSMR] = { 0x00, 16 },
  157. [SCBRR] = { 0x04, 8 },
  158. [SCSCR] = { 0x08, 16 },
  159. [SCxTDR] = { 0x20, 8 },
  160. [SCxSR] = { 0x14, 16 },
  161. [SCxRDR] = { 0x24, 8 },
  162. [SCFCR] = { 0x18, 16 },
  163. [SCFDR] = { 0x1c, 16 },
  164. [SCTFDR] = sci_reg_invalid,
  165. [SCRFDR] = sci_reg_invalid,
  166. [SCSPTR] = sci_reg_invalid,
  167. [SCLSR] = sci_reg_invalid,
  168. },
  169. /*
  170. * Common SCIFB definitions.
  171. */
  172. [SCIx_SCIFB_REGTYPE] = {
  173. [SCSMR] = { 0x00, 16 },
  174. [SCBRR] = { 0x04, 8 },
  175. [SCSCR] = { 0x08, 16 },
  176. [SCxTDR] = { 0x40, 8 },
  177. [SCxSR] = { 0x14, 16 },
  178. [SCxRDR] = { 0x60, 8 },
  179. [SCFCR] = { 0x18, 16 },
  180. [SCFDR] = { 0x1c, 16 },
  181. [SCTFDR] = sci_reg_invalid,
  182. [SCRFDR] = sci_reg_invalid,
  183. [SCSPTR] = sci_reg_invalid,
  184. [SCLSR] = sci_reg_invalid,
  185. },
  186. /*
  187. * Common SH-2(A) SCIF definitions for ports with FIFO data
  188. * count registers.
  189. */
  190. [SCIx_SH2_SCIF_FIFODATA_REGTYPE] = {
  191. [SCSMR] = { 0x00, 16 },
  192. [SCBRR] = { 0x04, 8 },
  193. [SCSCR] = { 0x08, 16 },
  194. [SCxTDR] = { 0x0c, 8 },
  195. [SCxSR] = { 0x10, 16 },
  196. [SCxRDR] = { 0x14, 8 },
  197. [SCFCR] = { 0x18, 16 },
  198. [SCFDR] = { 0x1c, 16 },
  199. [SCTFDR] = sci_reg_invalid,
  200. [SCRFDR] = sci_reg_invalid,
  201. [SCSPTR] = { 0x20, 16 },
  202. [SCLSR] = { 0x24, 16 },
  203. },
  204. /*
  205. * Common SH-3 SCIF definitions.
  206. */
  207. [SCIx_SH3_SCIF_REGTYPE] = {
  208. [SCSMR] = { 0x00, 8 },
  209. [SCBRR] = { 0x02, 8 },
  210. [SCSCR] = { 0x04, 8 },
  211. [SCxTDR] = { 0x06, 8 },
  212. [SCxSR] = { 0x08, 16 },
  213. [SCxRDR] = { 0x0a, 8 },
  214. [SCFCR] = { 0x0c, 8 },
  215. [SCFDR] = { 0x0e, 16 },
  216. [SCTFDR] = sci_reg_invalid,
  217. [SCRFDR] = sci_reg_invalid,
  218. [SCSPTR] = sci_reg_invalid,
  219. [SCLSR] = sci_reg_invalid,
  220. },
  221. /*
  222. * Common SH-4(A) SCIF(B) definitions.
  223. */
  224. [SCIx_SH4_SCIF_REGTYPE] = {
  225. [SCSMR] = { 0x00, 16 },
  226. [SCBRR] = { 0x04, 8 },
  227. [SCSCR] = { 0x08, 16 },
  228. [SCxTDR] = { 0x0c, 8 },
  229. [SCxSR] = { 0x10, 16 },
  230. [SCxRDR] = { 0x14, 8 },
  231. [SCFCR] = { 0x18, 16 },
  232. [SCFDR] = { 0x1c, 16 },
  233. [SCTFDR] = sci_reg_invalid,
  234. [SCRFDR] = sci_reg_invalid,
  235. [SCSPTR] = { 0x20, 16 },
  236. [SCLSR] = { 0x24, 16 },
  237. },
  238. /*
  239. * Common SH-4(A) SCIF(B) definitions for ports without an SCSPTR
  240. * register.
  241. */
  242. [SCIx_SH4_SCIF_NO_SCSPTR_REGTYPE] = {
  243. [SCSMR] = { 0x00, 16 },
  244. [SCBRR] = { 0x04, 8 },
  245. [SCSCR] = { 0x08, 16 },
  246. [SCxTDR] = { 0x0c, 8 },
  247. [SCxSR] = { 0x10, 16 },
  248. [SCxRDR] = { 0x14, 8 },
  249. [SCFCR] = { 0x18, 16 },
  250. [SCFDR] = { 0x1c, 16 },
  251. [SCTFDR] = sci_reg_invalid,
  252. [SCRFDR] = sci_reg_invalid,
  253. [SCSPTR] = sci_reg_invalid,
  254. [SCLSR] = { 0x24, 16 },
  255. },
  256. /*
  257. * Common SH-4(A) SCIF(B) definitions for ports with FIFO data
  258. * count registers.
  259. */
  260. [SCIx_SH4_SCIF_FIFODATA_REGTYPE] = {
  261. [SCSMR] = { 0x00, 16 },
  262. [SCBRR] = { 0x04, 8 },
  263. [SCSCR] = { 0x08, 16 },
  264. [SCxTDR] = { 0x0c, 8 },
  265. [SCxSR] = { 0x10, 16 },
  266. [SCxRDR] = { 0x14, 8 },
  267. [SCFCR] = { 0x18, 16 },
  268. [SCFDR] = { 0x1c, 16 },
  269. [SCTFDR] = { 0x1c, 16 }, /* aliased to SCFDR */
  270. [SCRFDR] = { 0x20, 16 },
  271. [SCSPTR] = { 0x24, 16 },
  272. [SCLSR] = { 0x28, 16 },
  273. },
  274. /*
  275. * SH7705-style SCIF(B) ports, lacking both SCSPTR and SCLSR
  276. * registers.
  277. */
  278. [SCIx_SH7705_SCIF_REGTYPE] = {
  279. [SCSMR] = { 0x00, 16 },
  280. [SCBRR] = { 0x04, 8 },
  281. [SCSCR] = { 0x08, 16 },
  282. [SCxTDR] = { 0x20, 8 },
  283. [SCxSR] = { 0x14, 16 },
  284. [SCxRDR] = { 0x24, 8 },
  285. [SCFCR] = { 0x18, 16 },
  286. [SCFDR] = { 0x1c, 16 },
  287. [SCTFDR] = sci_reg_invalid,
  288. [SCRFDR] = sci_reg_invalid,
  289. [SCSPTR] = sci_reg_invalid,
  290. [SCLSR] = sci_reg_invalid,
  291. },
  292. };
  293. #define sci_getreg(up, offset) (sci_regmap[to_sci_port(up)->cfg->regtype] + offset)
  294. /*
  295. * The "offset" here is rather misleading, in that it refers to an enum
  296. * value relative to the port mapping rather than the fixed offset
  297. * itself, which needs to be manually retrieved from the platform's
  298. * register map for the given port.
  299. */
  300. static unsigned int sci_serial_in(struct uart_port *p, int offset)
  301. {
  302. struct plat_sci_reg *reg = sci_getreg(p, offset);
  303. if (reg->size == 8)
  304. return ioread8(p->membase + (reg->offset << p->regshift));
  305. else if (reg->size == 16)
  306. return ioread16(p->membase + (reg->offset << p->regshift));
  307. else
  308. WARN(1, "Invalid register access\n");
  309. return 0;
  310. }
  311. static void sci_serial_out(struct uart_port *p, int offset, int value)
  312. {
  313. struct plat_sci_reg *reg = sci_getreg(p, offset);
  314. if (reg->size == 8)
  315. iowrite8(value, p->membase + (reg->offset << p->regshift));
  316. else if (reg->size == 16)
  317. iowrite16(value, p->membase + (reg->offset << p->regshift));
  318. else
  319. WARN(1, "Invalid register access\n");
  320. }
  321. static int sci_probe_regmap(struct plat_sci_port *cfg)
  322. {
  323. switch (cfg->type) {
  324. case PORT_SCI:
  325. cfg->regtype = SCIx_SCI_REGTYPE;
  326. break;
  327. case PORT_IRDA:
  328. cfg->regtype = SCIx_IRDA_REGTYPE;
  329. break;
  330. case PORT_SCIFA:
  331. cfg->regtype = SCIx_SCIFA_REGTYPE;
  332. break;
  333. case PORT_SCIFB:
  334. cfg->regtype = SCIx_SCIFB_REGTYPE;
  335. break;
  336. case PORT_SCIF:
  337. /*
  338. * The SH-4 is a bit of a misnomer here, although that's
  339. * where this particular port layout originated. This
  340. * configuration (or some slight variation thereof)
  341. * remains the dominant model for all SCIFs.
  342. */
  343. cfg->regtype = SCIx_SH4_SCIF_REGTYPE;
  344. break;
  345. default:
  346. printk(KERN_ERR "Can't probe register map for given port\n");
  347. return -EINVAL;
  348. }
  349. return 0;
  350. }
  351. static void sci_port_enable(struct sci_port *sci_port)
  352. {
  353. if (!sci_port->port.dev)
  354. return;
  355. pm_runtime_get_sync(sci_port->port.dev);
  356. clk_enable(sci_port->iclk);
  357. sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
  358. clk_enable(sci_port->fclk);
  359. }
  360. static void sci_port_disable(struct sci_port *sci_port)
  361. {
  362. if (!sci_port->port.dev)
  363. return;
  364. clk_disable(sci_port->fclk);
  365. clk_disable(sci_port->iclk);
  366. pm_runtime_put_sync(sci_port->port.dev);
  367. }
  368. #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
  369. #ifdef CONFIG_CONSOLE_POLL
  370. static int sci_poll_get_char(struct uart_port *port)
  371. {
  372. unsigned short status;
  373. int c;
  374. do {
  375. status = serial_port_in(port, SCxSR);
  376. if (status & SCxSR_ERRORS(port)) {
  377. serial_port_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
  378. continue;
  379. }
  380. break;
  381. } while (1);
  382. if (!(status & SCxSR_RDxF(port)))
  383. return NO_POLL_CHAR;
  384. c = serial_port_in(port, SCxRDR);
  385. /* Dummy read */
  386. serial_port_in(port, SCxSR);
  387. serial_port_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  388. return c;
  389. }
  390. #endif
  391. static void sci_poll_put_char(struct uart_port *port, unsigned char c)
  392. {
  393. unsigned short status;
  394. do {
  395. status = serial_port_in(port, SCxSR);
  396. } while (!(status & SCxSR_TDxE(port)));
  397. serial_port_out(port, SCxTDR, c);
  398. serial_port_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
  399. }
  400. #endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
  401. static void sci_init_pins(struct uart_port *port, unsigned int cflag)
  402. {
  403. struct sci_port *s = to_sci_port(port);
  404. struct plat_sci_reg *reg = sci_regmap[s->cfg->regtype] + SCSPTR;
  405. /*
  406. * Use port-specific handler if provided.
  407. */
  408. if (s->cfg->ops && s->cfg->ops->init_pins) {
  409. s->cfg->ops->init_pins(port, cflag);
  410. return;
  411. }
  412. /*
  413. * For the generic path SCSPTR is necessary. Bail out if that's
  414. * unavailable, too.
  415. */
  416. if (!reg->size)
  417. return;
  418. if ((s->cfg->capabilities & SCIx_HAVE_RTSCTS) &&
  419. ((!(cflag & CRTSCTS)))) {
  420. unsigned short status;
  421. status = serial_port_in(port, SCSPTR);
  422. status &= ~SCSPTR_CTSIO;
  423. status |= SCSPTR_RTSIO;
  424. serial_port_out(port, SCSPTR, status); /* Set RTS = 1 */
  425. }
  426. }
  427. static int sci_txfill(struct uart_port *port)
  428. {
  429. struct plat_sci_reg *reg;
  430. reg = sci_getreg(port, SCTFDR);
  431. if (reg->size)
  432. return serial_port_in(port, SCTFDR) & 0xff;
  433. reg = sci_getreg(port, SCFDR);
  434. if (reg->size)
  435. return serial_port_in(port, SCFDR) >> 8;
  436. return !(serial_port_in(port, SCxSR) & SCI_TDRE);
  437. }
  438. static int sci_txroom(struct uart_port *port)
  439. {
  440. return port->fifosize - sci_txfill(port);
  441. }
  442. static int sci_rxfill(struct uart_port *port)
  443. {
  444. struct plat_sci_reg *reg;
  445. reg = sci_getreg(port, SCRFDR);
  446. if (reg->size)
  447. return serial_port_in(port, SCRFDR) & 0xff;
  448. reg = sci_getreg(port, SCFDR);
  449. if (reg->size)
  450. return serial_port_in(port, SCFDR) & ((port->fifosize << 1) - 1);
  451. return (serial_port_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
  452. }
  453. /*
  454. * SCI helper for checking the state of the muxed port/RXD pins.
  455. */
  456. static inline int sci_rxd_in(struct uart_port *port)
  457. {
  458. struct sci_port *s = to_sci_port(port);
  459. if (s->cfg->port_reg <= 0)
  460. return 1;
  461. return !!__raw_readb(s->cfg->port_reg);
  462. }
  463. /* ********************************************************************** *
  464. * the interrupt related routines *
  465. * ********************************************************************** */
  466. static void sci_transmit_chars(struct uart_port *port)
  467. {
  468. struct circ_buf *xmit = &port->state->xmit;
  469. unsigned int stopped = uart_tx_stopped(port);
  470. unsigned short status;
  471. unsigned short ctrl;
  472. int count;
  473. status = serial_port_in(port, SCxSR);
  474. if (!(status & SCxSR_TDxE(port))) {
  475. ctrl = serial_port_in(port, SCSCR);
  476. if (uart_circ_empty(xmit))
  477. ctrl &= ~SCSCR_TIE;
  478. else
  479. ctrl |= SCSCR_TIE;
  480. serial_port_out(port, SCSCR, ctrl);
  481. return;
  482. }
  483. count = sci_txroom(port);
  484. do {
  485. unsigned char c;
  486. if (port->x_char) {
  487. c = port->x_char;
  488. port->x_char = 0;
  489. } else if (!uart_circ_empty(xmit) && !stopped) {
  490. c = xmit->buf[xmit->tail];
  491. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  492. } else {
  493. break;
  494. }
  495. serial_port_out(port, SCxTDR, c);
  496. port->icount.tx++;
  497. } while (--count > 0);
  498. serial_port_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
  499. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  500. uart_write_wakeup(port);
  501. if (uart_circ_empty(xmit)) {
  502. sci_stop_tx(port);
  503. } else {
  504. ctrl = serial_port_in(port, SCSCR);
  505. if (port->type != PORT_SCI) {
  506. serial_port_in(port, SCxSR); /* Dummy read */
  507. serial_port_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
  508. }
  509. ctrl |= SCSCR_TIE;
  510. serial_port_out(port, SCSCR, ctrl);
  511. }
  512. }
  513. /* On SH3, SCIF may read end-of-break as a space->mark char */
  514. #define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
  515. static void sci_receive_chars(struct uart_port *port)
  516. {
  517. struct sci_port *sci_port = to_sci_port(port);
  518. struct tty_struct *tty = port->state->port.tty;
  519. int i, count, copied = 0;
  520. unsigned short status;
  521. unsigned char flag;
  522. status = serial_port_in(port, SCxSR);
  523. if (!(status & SCxSR_RDxF(port)))
  524. return;
  525. while (1) {
  526. /* Don't copy more bytes than there is room for in the buffer */
  527. count = tty_buffer_request_room(tty, sci_rxfill(port));
  528. /* If for any reason we can't copy more data, we're done! */
  529. if (count == 0)
  530. break;
  531. if (port->type == PORT_SCI) {
  532. char c = serial_port_in(port, SCxRDR);
  533. if (uart_handle_sysrq_char(port, c) ||
  534. sci_port->break_flag)
  535. count = 0;
  536. else
  537. tty_insert_flip_char(tty, c, TTY_NORMAL);
  538. } else {
  539. for (i = 0; i < count; i++) {
  540. char c = serial_port_in(port, SCxRDR);
  541. status = serial_port_in(port, SCxSR);
  542. #if defined(CONFIG_CPU_SH3)
  543. /* Skip "chars" during break */
  544. if (sci_port->break_flag) {
  545. if ((c == 0) &&
  546. (status & SCxSR_FER(port))) {
  547. count--; i--;
  548. continue;
  549. }
  550. /* Nonzero => end-of-break */
  551. dev_dbg(port->dev, "debounce<%02x>\n", c);
  552. sci_port->break_flag = 0;
  553. if (STEPFN(c)) {
  554. count--; i--;
  555. continue;
  556. }
  557. }
  558. #endif /* CONFIG_CPU_SH3 */
  559. if (uart_handle_sysrq_char(port, c)) {
  560. count--; i--;
  561. continue;
  562. }
  563. /* Store data and status */
  564. if (status & SCxSR_FER(port)) {
  565. flag = TTY_FRAME;
  566. port->icount.frame++;
  567. dev_notice(port->dev, "frame error\n");
  568. } else if (status & SCxSR_PER(port)) {
  569. flag = TTY_PARITY;
  570. port->icount.parity++;
  571. dev_notice(port->dev, "parity error\n");
  572. } else
  573. flag = TTY_NORMAL;
  574. tty_insert_flip_char(tty, c, flag);
  575. }
  576. }
  577. serial_port_in(port, SCxSR); /* dummy read */
  578. serial_port_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  579. copied += count;
  580. port->icount.rx += count;
  581. }
  582. if (copied) {
  583. /* Tell the rest of the system the news. New characters! */
  584. tty_flip_buffer_push(tty);
  585. } else {
  586. serial_port_in(port, SCxSR); /* dummy read */
  587. serial_port_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  588. }
  589. }
  590. #define SCI_BREAK_JIFFIES (HZ/20)
  591. /*
  592. * The sci generates interrupts during the break,
  593. * 1 per millisecond or so during the break period, for 9600 baud.
  594. * So dont bother disabling interrupts.
  595. * But dont want more than 1 break event.
  596. * Use a kernel timer to periodically poll the rx line until
  597. * the break is finished.
  598. */
  599. static inline void sci_schedule_break_timer(struct sci_port *port)
  600. {
  601. mod_timer(&port->break_timer, jiffies + SCI_BREAK_JIFFIES);
  602. }
  603. /* Ensure that two consecutive samples find the break over. */
  604. static void sci_break_timer(unsigned long data)
  605. {
  606. struct sci_port *port = (struct sci_port *)data;
  607. sci_port_enable(port);
  608. if (sci_rxd_in(&port->port) == 0) {
  609. port->break_flag = 1;
  610. sci_schedule_break_timer(port);
  611. } else if (port->break_flag == 1) {
  612. /* break is over. */
  613. port->break_flag = 2;
  614. sci_schedule_break_timer(port);
  615. } else
  616. port->break_flag = 0;
  617. sci_port_disable(port);
  618. }
  619. static int sci_handle_errors(struct uart_port *port)
  620. {
  621. int copied = 0;
  622. unsigned short status = serial_port_in(port, SCxSR);
  623. struct tty_struct *tty = port->state->port.tty;
  624. struct sci_port *s = to_sci_port(port);
  625. /*
  626. * Handle overruns, if supported.
  627. */
  628. if (s->cfg->overrun_bit != SCIx_NOT_SUPPORTED) {
  629. if (status & (1 << s->cfg->overrun_bit)) {
  630. port->icount.overrun++;
  631. /* overrun error */
  632. if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
  633. copied++;
  634. dev_notice(port->dev, "overrun error");
  635. }
  636. }
  637. if (status & SCxSR_FER(port)) {
  638. if (sci_rxd_in(port) == 0) {
  639. /* Notify of BREAK */
  640. struct sci_port *sci_port = to_sci_port(port);
  641. if (!sci_port->break_flag) {
  642. port->icount.brk++;
  643. sci_port->break_flag = 1;
  644. sci_schedule_break_timer(sci_port);
  645. /* Do sysrq handling. */
  646. if (uart_handle_break(port))
  647. return 0;
  648. dev_dbg(port->dev, "BREAK detected\n");
  649. if (tty_insert_flip_char(tty, 0, TTY_BREAK))
  650. copied++;
  651. }
  652. } else {
  653. /* frame error */
  654. port->icount.frame++;
  655. if (tty_insert_flip_char(tty, 0, TTY_FRAME))
  656. copied++;
  657. dev_notice(port->dev, "frame error\n");
  658. }
  659. }
  660. if (status & SCxSR_PER(port)) {
  661. /* parity error */
  662. port->icount.parity++;
  663. if (tty_insert_flip_char(tty, 0, TTY_PARITY))
  664. copied++;
  665. dev_notice(port->dev, "parity error");
  666. }
  667. if (copied)
  668. tty_flip_buffer_push(tty);
  669. return copied;
  670. }
  671. static int sci_handle_fifo_overrun(struct uart_port *port)
  672. {
  673. struct tty_struct *tty = port->state->port.tty;
  674. struct sci_port *s = to_sci_port(port);
  675. struct plat_sci_reg *reg;
  676. int copied = 0;
  677. reg = sci_getreg(port, SCLSR);
  678. if (!reg->size)
  679. return 0;
  680. if ((serial_port_in(port, SCLSR) & (1 << s->cfg->overrun_bit))) {
  681. serial_port_out(port, SCLSR, 0);
  682. port->icount.overrun++;
  683. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  684. tty_flip_buffer_push(tty);
  685. dev_notice(port->dev, "overrun error\n");
  686. copied++;
  687. }
  688. return copied;
  689. }
  690. static int sci_handle_breaks(struct uart_port *port)
  691. {
  692. int copied = 0;
  693. unsigned short status = serial_port_in(port, SCxSR);
  694. struct tty_struct *tty = port->state->port.tty;
  695. struct sci_port *s = to_sci_port(port);
  696. if (uart_handle_break(port))
  697. return 0;
  698. if (!s->break_flag && status & SCxSR_BRK(port)) {
  699. #if defined(CONFIG_CPU_SH3)
  700. /* Debounce break */
  701. s->break_flag = 1;
  702. #endif
  703. port->icount.brk++;
  704. /* Notify of BREAK */
  705. if (tty_insert_flip_char(tty, 0, TTY_BREAK))
  706. copied++;
  707. dev_dbg(port->dev, "BREAK detected\n");
  708. }
  709. if (copied)
  710. tty_flip_buffer_push(tty);
  711. copied += sci_handle_fifo_overrun(port);
  712. return copied;
  713. }
  714. static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
  715. {
  716. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  717. struct uart_port *port = ptr;
  718. struct sci_port *s = to_sci_port(port);
  719. if (s->chan_rx) {
  720. u16 scr = serial_port_in(port, SCSCR);
  721. u16 ssr = serial_port_in(port, SCxSR);
  722. /* Disable future Rx interrupts */
  723. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
  724. disable_irq_nosync(irq);
  725. scr |= 0x4000;
  726. } else {
  727. scr &= ~SCSCR_RIE;
  728. }
  729. serial_port_out(port, SCSCR, scr);
  730. /* Clear current interrupt */
  731. serial_port_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
  732. dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
  733. jiffies, s->rx_timeout);
  734. mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
  735. return IRQ_HANDLED;
  736. }
  737. #endif
  738. /* I think sci_receive_chars has to be called irrespective
  739. * of whether the I_IXOFF is set, otherwise, how is the interrupt
  740. * to be disabled?
  741. */
  742. sci_receive_chars(ptr);
  743. return IRQ_HANDLED;
  744. }
  745. static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
  746. {
  747. struct uart_port *port = ptr;
  748. unsigned long flags;
  749. spin_lock_irqsave(&port->lock, flags);
  750. sci_transmit_chars(port);
  751. spin_unlock_irqrestore(&port->lock, flags);
  752. return IRQ_HANDLED;
  753. }
  754. static irqreturn_t sci_er_interrupt(int irq, void *ptr)
  755. {
  756. struct uart_port *port = ptr;
  757. /* Handle errors */
  758. if (port->type == PORT_SCI) {
  759. if (sci_handle_errors(port)) {
  760. /* discard character in rx buffer */
  761. serial_port_in(port, SCxSR);
  762. serial_port_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
  763. }
  764. } else {
  765. sci_handle_fifo_overrun(port);
  766. sci_rx_interrupt(irq, ptr);
  767. }
  768. serial_port_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
  769. /* Kick the transmission */
  770. sci_tx_interrupt(irq, ptr);
  771. return IRQ_HANDLED;
  772. }
  773. static irqreturn_t sci_br_interrupt(int irq, void *ptr)
  774. {
  775. struct uart_port *port = ptr;
  776. /* Handle BREAKs */
  777. sci_handle_breaks(port);
  778. serial_port_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
  779. return IRQ_HANDLED;
  780. }
  781. static inline unsigned long port_rx_irq_mask(struct uart_port *port)
  782. {
  783. /*
  784. * Not all ports (such as SCIFA) will support REIE. Rather than
  785. * special-casing the port type, we check the port initialization
  786. * IRQ enable mask to see whether the IRQ is desired at all. If
  787. * it's unset, it's logically inferred that there's no point in
  788. * testing for it.
  789. */
  790. return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
  791. }
  792. static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
  793. {
  794. unsigned short ssr_status, scr_status, err_enabled;
  795. struct uart_port *port = ptr;
  796. struct sci_port *s = to_sci_port(port);
  797. irqreturn_t ret = IRQ_NONE;
  798. ssr_status = serial_port_in(port, SCxSR);
  799. scr_status = serial_port_in(port, SCSCR);
  800. err_enabled = scr_status & port_rx_irq_mask(port);
  801. /* Tx Interrupt */
  802. if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
  803. !s->chan_tx)
  804. ret = sci_tx_interrupt(irq, ptr);
  805. /*
  806. * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
  807. * DR flags
  808. */
  809. if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
  810. (scr_status & SCSCR_RIE))
  811. ret = sci_rx_interrupt(irq, ptr);
  812. /* Error Interrupt */
  813. if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
  814. ret = sci_er_interrupt(irq, ptr);
  815. /* Break Interrupt */
  816. if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
  817. ret = sci_br_interrupt(irq, ptr);
  818. return ret;
  819. }
  820. /*
  821. * Here we define a transition notifier so that we can update all of our
  822. * ports' baud rate when the peripheral clock changes.
  823. */
  824. static int sci_notifier(struct notifier_block *self,
  825. unsigned long phase, void *p)
  826. {
  827. struct sci_port *sci_port;
  828. unsigned long flags;
  829. sci_port = container_of(self, struct sci_port, freq_transition);
  830. if ((phase == CPUFREQ_POSTCHANGE) ||
  831. (phase == CPUFREQ_RESUMECHANGE)) {
  832. struct uart_port *port = &sci_port->port;
  833. spin_lock_irqsave(&port->lock, flags);
  834. port->uartclk = clk_get_rate(sci_port->iclk);
  835. spin_unlock_irqrestore(&port->lock, flags);
  836. }
  837. return NOTIFY_OK;
  838. }
  839. static struct sci_irq_desc {
  840. const char *desc;
  841. irq_handler_t handler;
  842. } sci_irq_desc[] = {
  843. /*
  844. * Split out handlers, the default case.
  845. */
  846. [SCIx_ERI_IRQ] = {
  847. .desc = "rx err",
  848. .handler = sci_er_interrupt,
  849. },
  850. [SCIx_RXI_IRQ] = {
  851. .desc = "rx full",
  852. .handler = sci_rx_interrupt,
  853. },
  854. [SCIx_TXI_IRQ] = {
  855. .desc = "tx empty",
  856. .handler = sci_tx_interrupt,
  857. },
  858. [SCIx_BRI_IRQ] = {
  859. .desc = "break",
  860. .handler = sci_br_interrupt,
  861. },
  862. /*
  863. * Special muxed handler.
  864. */
  865. [SCIx_MUX_IRQ] = {
  866. .desc = "mux",
  867. .handler = sci_mpxed_interrupt,
  868. },
  869. };
  870. static int sci_request_irq(struct sci_port *port)
  871. {
  872. struct uart_port *up = &port->port;
  873. int i, j, ret = 0;
  874. for (i = j = 0; i < SCIx_NR_IRQS; i++, j++) {
  875. struct sci_irq_desc *desc;
  876. unsigned int irq;
  877. if (SCIx_IRQ_IS_MUXED(port)) {
  878. i = SCIx_MUX_IRQ;
  879. irq = up->irq;
  880. } else
  881. irq = port->cfg->irqs[i];
  882. desc = sci_irq_desc + i;
  883. port->irqstr[j] = kasprintf(GFP_KERNEL, "%s:%s",
  884. dev_name(up->dev), desc->desc);
  885. if (!port->irqstr[j]) {
  886. dev_err(up->dev, "Failed to allocate %s IRQ string\n",
  887. desc->desc);
  888. goto out_nomem;
  889. }
  890. ret = request_irq(irq, desc->handler, up->irqflags,
  891. port->irqstr[j], port);
  892. if (unlikely(ret)) {
  893. dev_err(up->dev, "Can't allocate %s IRQ\n", desc->desc);
  894. goto out_noirq;
  895. }
  896. }
  897. return 0;
  898. out_noirq:
  899. while (--i >= 0)
  900. free_irq(port->cfg->irqs[i], port);
  901. out_nomem:
  902. while (--j >= 0)
  903. kfree(port->irqstr[j]);
  904. return ret;
  905. }
  906. static void sci_free_irq(struct sci_port *port)
  907. {
  908. int i;
  909. /*
  910. * Intentionally in reverse order so we iterate over the muxed
  911. * IRQ first.
  912. */
  913. for (i = 0; i < SCIx_NR_IRQS; i++) {
  914. free_irq(port->cfg->irqs[i], port);
  915. kfree(port->irqstr[i]);
  916. if (SCIx_IRQ_IS_MUXED(port)) {
  917. /* If there's only one IRQ, we're done. */
  918. return;
  919. }
  920. }
  921. }
  922. static const char *sci_gpio_names[SCIx_NR_FNS] = {
  923. "sck", "rxd", "txd", "cts", "rts",
  924. };
  925. static const char *sci_gpio_str(unsigned int index)
  926. {
  927. return sci_gpio_names[index];
  928. }
  929. static void __devinit sci_init_gpios(struct sci_port *port)
  930. {
  931. struct uart_port *up = &port->port;
  932. int i;
  933. if (!port->cfg)
  934. return;
  935. for (i = 0; i < SCIx_NR_FNS; i++) {
  936. const char *desc;
  937. int ret;
  938. if (!port->cfg->gpios[i])
  939. continue;
  940. desc = sci_gpio_str(i);
  941. port->gpiostr[i] = kasprintf(GFP_KERNEL, "%s:%s",
  942. dev_name(up->dev), desc);
  943. /*
  944. * If we've failed the allocation, we can still continue
  945. * on with a NULL string.
  946. */
  947. if (!port->gpiostr[i])
  948. dev_notice(up->dev, "%s string allocation failure\n",
  949. desc);
  950. ret = gpio_request(port->cfg->gpios[i], port->gpiostr[i]);
  951. if (unlikely(ret != 0)) {
  952. dev_notice(up->dev, "failed %s gpio request\n", desc);
  953. /*
  954. * If we can't get the GPIO for whatever reason,
  955. * no point in keeping the verbose string around.
  956. */
  957. kfree(port->gpiostr[i]);
  958. }
  959. }
  960. }
  961. static void sci_free_gpios(struct sci_port *port)
  962. {
  963. int i;
  964. for (i = 0; i < SCIx_NR_FNS; i++)
  965. if (port->cfg->gpios[i]) {
  966. gpio_free(port->cfg->gpios[i]);
  967. kfree(port->gpiostr[i]);
  968. }
  969. }
  970. static unsigned int sci_tx_empty(struct uart_port *port)
  971. {
  972. unsigned short status = serial_port_in(port, SCxSR);
  973. unsigned short in_tx_fifo = sci_txfill(port);
  974. return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
  975. }
  976. /*
  977. * Modem control is a bit of a mixed bag for SCI(F) ports. Generally
  978. * CTS/RTS is supported in hardware by at least one port and controlled
  979. * via SCSPTR (SCxPCR for SCIFA/B parts), or external pins (presently
  980. * handled via the ->init_pins() op, which is a bit of a one-way street,
  981. * lacking any ability to defer pin control -- this will later be
  982. * converted over to the GPIO framework).
  983. *
  984. * Other modes (such as loopback) are supported generically on certain
  985. * port types, but not others. For these it's sufficient to test for the
  986. * existence of the support register and simply ignore the port type.
  987. */
  988. static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
  989. {
  990. if (mctrl & TIOCM_LOOP) {
  991. struct plat_sci_reg *reg;
  992. /*
  993. * Standard loopback mode for SCFCR ports.
  994. */
  995. reg = sci_getreg(port, SCFCR);
  996. if (reg->size)
  997. serial_port_out(port, SCFCR, serial_port_in(port, SCFCR) | 1);
  998. }
  999. }
  1000. static unsigned int sci_get_mctrl(struct uart_port *port)
  1001. {
  1002. /*
  1003. * CTS/RTS is handled in hardware when supported, while nothing
  1004. * else is wired up. Keep it simple and simply assert DSR/CAR.
  1005. */
  1006. return TIOCM_DSR | TIOCM_CAR;
  1007. }
  1008. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  1009. static void sci_dma_tx_complete(void *arg)
  1010. {
  1011. struct sci_port *s = arg;
  1012. struct uart_port *port = &s->port;
  1013. struct circ_buf *xmit = &port->state->xmit;
  1014. unsigned long flags;
  1015. dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
  1016. spin_lock_irqsave(&port->lock, flags);
  1017. xmit->tail += sg_dma_len(&s->sg_tx);
  1018. xmit->tail &= UART_XMIT_SIZE - 1;
  1019. port->icount.tx += sg_dma_len(&s->sg_tx);
  1020. async_tx_ack(s->desc_tx);
  1021. s->desc_tx = NULL;
  1022. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  1023. uart_write_wakeup(port);
  1024. if (!uart_circ_empty(xmit)) {
  1025. s->cookie_tx = 0;
  1026. schedule_work(&s->work_tx);
  1027. } else {
  1028. s->cookie_tx = -EINVAL;
  1029. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
  1030. u16 ctrl = serial_port_in(port, SCSCR);
  1031. serial_port_out(port, SCSCR, ctrl & ~SCSCR_TIE);
  1032. }
  1033. }
  1034. spin_unlock_irqrestore(&port->lock, flags);
  1035. }
  1036. /* Locking: called with port lock held */
  1037. static int sci_dma_rx_push(struct sci_port *s, struct tty_struct *tty,
  1038. size_t count)
  1039. {
  1040. struct uart_port *port = &s->port;
  1041. int i, active, room;
  1042. room = tty_buffer_request_room(tty, count);
  1043. if (s->active_rx == s->cookie_rx[0]) {
  1044. active = 0;
  1045. } else if (s->active_rx == s->cookie_rx[1]) {
  1046. active = 1;
  1047. } else {
  1048. dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
  1049. return 0;
  1050. }
  1051. if (room < count)
  1052. dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
  1053. count - room);
  1054. if (!room)
  1055. return room;
  1056. for (i = 0; i < room; i++)
  1057. tty_insert_flip_char(tty, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
  1058. TTY_NORMAL);
  1059. port->icount.rx += room;
  1060. return room;
  1061. }
  1062. static void sci_dma_rx_complete(void *arg)
  1063. {
  1064. struct sci_port *s = arg;
  1065. struct uart_port *port = &s->port;
  1066. struct tty_struct *tty = port->state->port.tty;
  1067. unsigned long flags;
  1068. int count;
  1069. dev_dbg(port->dev, "%s(%d) active #%d\n", __func__, port->line, s->active_rx);
  1070. spin_lock_irqsave(&port->lock, flags);
  1071. count = sci_dma_rx_push(s, tty, s->buf_len_rx);
  1072. mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
  1073. spin_unlock_irqrestore(&port->lock, flags);
  1074. if (count)
  1075. tty_flip_buffer_push(tty);
  1076. schedule_work(&s->work_rx);
  1077. }
  1078. static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
  1079. {
  1080. struct dma_chan *chan = s->chan_rx;
  1081. struct uart_port *port = &s->port;
  1082. s->chan_rx = NULL;
  1083. s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
  1084. dma_release_channel(chan);
  1085. if (sg_dma_address(&s->sg_rx[0]))
  1086. dma_free_coherent(port->dev, s->buf_len_rx * 2,
  1087. sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
  1088. if (enable_pio)
  1089. sci_start_rx(port);
  1090. }
  1091. static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
  1092. {
  1093. struct dma_chan *chan = s->chan_tx;
  1094. struct uart_port *port = &s->port;
  1095. s->chan_tx = NULL;
  1096. s->cookie_tx = -EINVAL;
  1097. dma_release_channel(chan);
  1098. if (enable_pio)
  1099. sci_start_tx(port);
  1100. }
  1101. static void sci_submit_rx(struct sci_port *s)
  1102. {
  1103. struct dma_chan *chan = s->chan_rx;
  1104. int i;
  1105. for (i = 0; i < 2; i++) {
  1106. struct scatterlist *sg = &s->sg_rx[i];
  1107. struct dma_async_tx_descriptor *desc;
  1108. desc = dmaengine_prep_slave_sg(chan,
  1109. sg, 1, DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
  1110. if (desc) {
  1111. s->desc_rx[i] = desc;
  1112. desc->callback = sci_dma_rx_complete;
  1113. desc->callback_param = s;
  1114. s->cookie_rx[i] = desc->tx_submit(desc);
  1115. }
  1116. if (!desc || s->cookie_rx[i] < 0) {
  1117. if (i) {
  1118. async_tx_ack(s->desc_rx[0]);
  1119. s->cookie_rx[0] = -EINVAL;
  1120. }
  1121. if (desc) {
  1122. async_tx_ack(desc);
  1123. s->cookie_rx[i] = -EINVAL;
  1124. }
  1125. dev_warn(s->port.dev,
  1126. "failed to re-start DMA, using PIO\n");
  1127. sci_rx_dma_release(s, true);
  1128. return;
  1129. }
  1130. dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
  1131. s->cookie_rx[i], i);
  1132. }
  1133. s->active_rx = s->cookie_rx[0];
  1134. dma_async_issue_pending(chan);
  1135. }
  1136. static void work_fn_rx(struct work_struct *work)
  1137. {
  1138. struct sci_port *s = container_of(work, struct sci_port, work_rx);
  1139. struct uart_port *port = &s->port;
  1140. struct dma_async_tx_descriptor *desc;
  1141. int new;
  1142. if (s->active_rx == s->cookie_rx[0]) {
  1143. new = 0;
  1144. } else if (s->active_rx == s->cookie_rx[1]) {
  1145. new = 1;
  1146. } else {
  1147. dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
  1148. return;
  1149. }
  1150. desc = s->desc_rx[new];
  1151. if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
  1152. DMA_SUCCESS) {
  1153. /* Handle incomplete DMA receive */
  1154. struct tty_struct *tty = port->state->port.tty;
  1155. struct dma_chan *chan = s->chan_rx;
  1156. struct sh_desc *sh_desc = container_of(desc, struct sh_desc,
  1157. async_tx);
  1158. unsigned long flags;
  1159. int count;
  1160. chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
  1161. dev_dbg(port->dev, "Read %u bytes with cookie %d\n",
  1162. sh_desc->partial, sh_desc->cookie);
  1163. spin_lock_irqsave(&port->lock, flags);
  1164. count = sci_dma_rx_push(s, tty, sh_desc->partial);
  1165. spin_unlock_irqrestore(&port->lock, flags);
  1166. if (count)
  1167. tty_flip_buffer_push(tty);
  1168. sci_submit_rx(s);
  1169. return;
  1170. }
  1171. s->cookie_rx[new] = desc->tx_submit(desc);
  1172. if (s->cookie_rx[new] < 0) {
  1173. dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
  1174. sci_rx_dma_release(s, true);
  1175. return;
  1176. }
  1177. s->active_rx = s->cookie_rx[!new];
  1178. dev_dbg(port->dev, "%s: cookie %d #%d, new active #%d\n", __func__,
  1179. s->cookie_rx[new], new, s->active_rx);
  1180. }
  1181. static void work_fn_tx(struct work_struct *work)
  1182. {
  1183. struct sci_port *s = container_of(work, struct sci_port, work_tx);
  1184. struct dma_async_tx_descriptor *desc;
  1185. struct dma_chan *chan = s->chan_tx;
  1186. struct uart_port *port = &s->port;
  1187. struct circ_buf *xmit = &port->state->xmit;
  1188. struct scatterlist *sg = &s->sg_tx;
  1189. /*
  1190. * DMA is idle now.
  1191. * Port xmit buffer is already mapped, and it is one page... Just adjust
  1192. * offsets and lengths. Since it is a circular buffer, we have to
  1193. * transmit till the end, and then the rest. Take the port lock to get a
  1194. * consistent xmit buffer state.
  1195. */
  1196. spin_lock_irq(&port->lock);
  1197. sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
  1198. sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
  1199. sg->offset;
  1200. sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
  1201. CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
  1202. spin_unlock_irq(&port->lock);
  1203. BUG_ON(!sg_dma_len(sg));
  1204. desc = dmaengine_prep_slave_sg(chan,
  1205. sg, s->sg_len_tx, DMA_MEM_TO_DEV,
  1206. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1207. if (!desc) {
  1208. /* switch to PIO */
  1209. sci_tx_dma_release(s, true);
  1210. return;
  1211. }
  1212. dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
  1213. spin_lock_irq(&port->lock);
  1214. s->desc_tx = desc;
  1215. desc->callback = sci_dma_tx_complete;
  1216. desc->callback_param = s;
  1217. spin_unlock_irq(&port->lock);
  1218. s->cookie_tx = desc->tx_submit(desc);
  1219. if (s->cookie_tx < 0) {
  1220. dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
  1221. /* switch to PIO */
  1222. sci_tx_dma_release(s, true);
  1223. return;
  1224. }
  1225. dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
  1226. xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
  1227. dma_async_issue_pending(chan);
  1228. }
  1229. #endif
  1230. static void sci_start_tx(struct uart_port *port)
  1231. {
  1232. struct sci_port *s = to_sci_port(port);
  1233. unsigned short ctrl;
  1234. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  1235. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
  1236. u16 new, scr = serial_port_in(port, SCSCR);
  1237. if (s->chan_tx)
  1238. new = scr | 0x8000;
  1239. else
  1240. new = scr & ~0x8000;
  1241. if (new != scr)
  1242. serial_port_out(port, SCSCR, new);
  1243. }
  1244. if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
  1245. s->cookie_tx < 0) {
  1246. s->cookie_tx = 0;
  1247. schedule_work(&s->work_tx);
  1248. }
  1249. #endif
  1250. if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
  1251. /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
  1252. ctrl = serial_port_in(port, SCSCR);
  1253. serial_port_out(port, SCSCR, ctrl | SCSCR_TIE);
  1254. }
  1255. }
  1256. static void sci_stop_tx(struct uart_port *port)
  1257. {
  1258. unsigned short ctrl;
  1259. /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
  1260. ctrl = serial_port_in(port, SCSCR);
  1261. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
  1262. ctrl &= ~0x8000;
  1263. ctrl &= ~SCSCR_TIE;
  1264. serial_port_out(port, SCSCR, ctrl);
  1265. }
  1266. static void sci_start_rx(struct uart_port *port)
  1267. {
  1268. unsigned short ctrl;
  1269. ctrl = serial_port_in(port, SCSCR) | port_rx_irq_mask(port);
  1270. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
  1271. ctrl &= ~0x4000;
  1272. serial_port_out(port, SCSCR, ctrl);
  1273. }
  1274. static void sci_stop_rx(struct uart_port *port)
  1275. {
  1276. unsigned short ctrl;
  1277. ctrl = serial_port_in(port, SCSCR);
  1278. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
  1279. ctrl &= ~0x4000;
  1280. ctrl &= ~port_rx_irq_mask(port);
  1281. serial_port_out(port, SCSCR, ctrl);
  1282. }
  1283. static void sci_enable_ms(struct uart_port *port)
  1284. {
  1285. /*
  1286. * Not supported by hardware, always a nop.
  1287. */
  1288. }
  1289. static void sci_break_ctl(struct uart_port *port, int break_state)
  1290. {
  1291. /*
  1292. * Not supported by hardware. Most parts couple break and rx
  1293. * interrupts together, with break detection always enabled.
  1294. */
  1295. }
  1296. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  1297. static bool filter(struct dma_chan *chan, void *slave)
  1298. {
  1299. struct sh_dmae_slave *param = slave;
  1300. dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
  1301. param->slave_id);
  1302. chan->private = param;
  1303. return true;
  1304. }
  1305. static void rx_timer_fn(unsigned long arg)
  1306. {
  1307. struct sci_port *s = (struct sci_port *)arg;
  1308. struct uart_port *port = &s->port;
  1309. u16 scr = serial_port_in(port, SCSCR);
  1310. if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
  1311. scr &= ~0x4000;
  1312. enable_irq(s->cfg->irqs[1]);
  1313. }
  1314. serial_port_out(port, SCSCR, scr | SCSCR_RIE);
  1315. dev_dbg(port->dev, "DMA Rx timed out\n");
  1316. schedule_work(&s->work_rx);
  1317. }
  1318. static void sci_request_dma(struct uart_port *port)
  1319. {
  1320. struct sci_port *s = to_sci_port(port);
  1321. struct sh_dmae_slave *param;
  1322. struct dma_chan *chan;
  1323. dma_cap_mask_t mask;
  1324. int nent;
  1325. dev_dbg(port->dev, "%s: port %d\n", __func__,
  1326. port->line);
  1327. if (s->cfg->dma_slave_tx <= 0 || s->cfg->dma_slave_rx <= 0)
  1328. return;
  1329. dma_cap_zero(mask);
  1330. dma_cap_set(DMA_SLAVE, mask);
  1331. param = &s->param_tx;
  1332. /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
  1333. param->slave_id = s->cfg->dma_slave_tx;
  1334. s->cookie_tx = -EINVAL;
  1335. chan = dma_request_channel(mask, filter, param);
  1336. dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
  1337. if (chan) {
  1338. s->chan_tx = chan;
  1339. sg_init_table(&s->sg_tx, 1);
  1340. /* UART circular tx buffer is an aligned page. */
  1341. BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
  1342. sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
  1343. UART_XMIT_SIZE, (int)port->state->xmit.buf & ~PAGE_MASK);
  1344. nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
  1345. if (!nent)
  1346. sci_tx_dma_release(s, false);
  1347. else
  1348. dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
  1349. sg_dma_len(&s->sg_tx),
  1350. port->state->xmit.buf, sg_dma_address(&s->sg_tx));
  1351. s->sg_len_tx = nent;
  1352. INIT_WORK(&s->work_tx, work_fn_tx);
  1353. }
  1354. param = &s->param_rx;
  1355. /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
  1356. param->slave_id = s->cfg->dma_slave_rx;
  1357. chan = dma_request_channel(mask, filter, param);
  1358. dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
  1359. if (chan) {
  1360. dma_addr_t dma[2];
  1361. void *buf[2];
  1362. int i;
  1363. s->chan_rx = chan;
  1364. s->buf_len_rx = 2 * max(16, (int)port->fifosize);
  1365. buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
  1366. &dma[0], GFP_KERNEL);
  1367. if (!buf[0]) {
  1368. dev_warn(port->dev,
  1369. "failed to allocate dma buffer, using PIO\n");
  1370. sci_rx_dma_release(s, true);
  1371. return;
  1372. }
  1373. buf[1] = buf[0] + s->buf_len_rx;
  1374. dma[1] = dma[0] + s->buf_len_rx;
  1375. for (i = 0; i < 2; i++) {
  1376. struct scatterlist *sg = &s->sg_rx[i];
  1377. sg_init_table(sg, 1);
  1378. sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
  1379. (int)buf[i] & ~PAGE_MASK);
  1380. sg_dma_address(sg) = dma[i];
  1381. }
  1382. INIT_WORK(&s->work_rx, work_fn_rx);
  1383. setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
  1384. sci_submit_rx(s);
  1385. }
  1386. }
  1387. static void sci_free_dma(struct uart_port *port)
  1388. {
  1389. struct sci_port *s = to_sci_port(port);
  1390. if (s->chan_tx)
  1391. sci_tx_dma_release(s, false);
  1392. if (s->chan_rx)
  1393. sci_rx_dma_release(s, false);
  1394. }
  1395. #else
  1396. static inline void sci_request_dma(struct uart_port *port)
  1397. {
  1398. }
  1399. static inline void sci_free_dma(struct uart_port *port)
  1400. {
  1401. }
  1402. #endif
  1403. static int sci_startup(struct uart_port *port)
  1404. {
  1405. struct sci_port *s = to_sci_port(port);
  1406. int ret;
  1407. dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
  1408. pm_runtime_put_noidle(port->dev);
  1409. sci_port_enable(s);
  1410. ret = sci_request_irq(s);
  1411. if (unlikely(ret < 0))
  1412. return ret;
  1413. sci_request_dma(port);
  1414. sci_start_tx(port);
  1415. sci_start_rx(port);
  1416. return 0;
  1417. }
  1418. static void sci_shutdown(struct uart_port *port)
  1419. {
  1420. struct sci_port *s = to_sci_port(port);
  1421. dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
  1422. sci_stop_rx(port);
  1423. sci_stop_tx(port);
  1424. sci_free_dma(port);
  1425. sci_free_irq(s);
  1426. sci_port_disable(s);
  1427. pm_runtime_get_noresume(port->dev);
  1428. }
  1429. static unsigned int sci_scbrr_calc(unsigned int algo_id, unsigned int bps,
  1430. unsigned long freq)
  1431. {
  1432. switch (algo_id) {
  1433. case SCBRR_ALGO_1:
  1434. return ((freq + 16 * bps) / (16 * bps) - 1);
  1435. case SCBRR_ALGO_2:
  1436. return ((freq + 16 * bps) / (32 * bps) - 1);
  1437. case SCBRR_ALGO_3:
  1438. return (((freq * 2) + 16 * bps) / (16 * bps) - 1);
  1439. case SCBRR_ALGO_4:
  1440. return (((freq * 2) + 16 * bps) / (32 * bps) - 1);
  1441. case SCBRR_ALGO_5:
  1442. return (((freq * 1000 / 32) / bps) - 1);
  1443. }
  1444. /* Warn, but use a safe default */
  1445. WARN_ON(1);
  1446. return ((freq + 16 * bps) / (32 * bps) - 1);
  1447. }
  1448. static void sci_reset(struct uart_port *port)
  1449. {
  1450. struct plat_sci_reg *reg;
  1451. unsigned int status;
  1452. do {
  1453. status = serial_port_in(port, SCxSR);
  1454. } while (!(status & SCxSR_TEND(port)));
  1455. serial_port_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
  1456. reg = sci_getreg(port, SCFCR);
  1457. if (reg->size)
  1458. serial_port_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
  1459. }
  1460. static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
  1461. struct ktermios *old)
  1462. {
  1463. struct sci_port *s = to_sci_port(port);
  1464. struct plat_sci_reg *reg;
  1465. unsigned int baud, smr_val, max_baud;
  1466. int t = -1;
  1467. /*
  1468. * earlyprintk comes here early on with port->uartclk set to zero.
  1469. * the clock framework is not up and running at this point so here
  1470. * we assume that 115200 is the maximum baud rate. please note that
  1471. * the baud rate is not programmed during earlyprintk - it is assumed
  1472. * that the previous boot loader has enabled required clocks and
  1473. * setup the baud rate generator hardware for us already.
  1474. */
  1475. max_baud = port->uartclk ? port->uartclk / 16 : 115200;
  1476. baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
  1477. if (likely(baud && port->uartclk))
  1478. t = sci_scbrr_calc(s->cfg->scbrr_algo_id, baud, port->uartclk);
  1479. sci_port_enable(s);
  1480. sci_reset(port);
  1481. smr_val = serial_port_in(port, SCSMR) & 3;
  1482. if ((termios->c_cflag & CSIZE) == CS7)
  1483. smr_val |= 0x40;
  1484. if (termios->c_cflag & PARENB)
  1485. smr_val |= 0x20;
  1486. if (termios->c_cflag & PARODD)
  1487. smr_val |= 0x30;
  1488. if (termios->c_cflag & CSTOPB)
  1489. smr_val |= 0x08;
  1490. uart_update_timeout(port, termios->c_cflag, baud);
  1491. serial_port_out(port, SCSMR, smr_val);
  1492. dev_dbg(port->dev, "%s: SMR %x, t %x, SCSCR %x\n", __func__, smr_val, t,
  1493. s->cfg->scscr);
  1494. if (t > 0) {
  1495. if (t >= 256) {
  1496. serial_port_out(port, SCSMR, (serial_port_in(port, SCSMR) & ~3) | 1);
  1497. t >>= 2;
  1498. } else
  1499. serial_port_out(port, SCSMR, serial_port_in(port, SCSMR) & ~3);
  1500. serial_port_out(port, SCBRR, t);
  1501. udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
  1502. }
  1503. sci_init_pins(port, termios->c_cflag);
  1504. reg = sci_getreg(port, SCFCR);
  1505. if (reg->size) {
  1506. unsigned short ctrl = serial_port_in(port, SCFCR);
  1507. if (s->cfg->capabilities & SCIx_HAVE_RTSCTS) {
  1508. if (termios->c_cflag & CRTSCTS)
  1509. ctrl |= SCFCR_MCE;
  1510. else
  1511. ctrl &= ~SCFCR_MCE;
  1512. }
  1513. /*
  1514. * As we've done a sci_reset() above, ensure we don't
  1515. * interfere with the FIFOs while toggling MCE. As the
  1516. * reset values could still be set, simply mask them out.
  1517. */
  1518. ctrl &= ~(SCFCR_RFRST | SCFCR_TFRST);
  1519. serial_port_out(port, SCFCR, ctrl);
  1520. }
  1521. serial_port_out(port, SCSCR, s->cfg->scscr);
  1522. #ifdef CONFIG_SERIAL_SH_SCI_DMA
  1523. /*
  1524. * Calculate delay for 1.5 DMA buffers: see
  1525. * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
  1526. * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
  1527. * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
  1528. * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
  1529. * sizes), but it has been found out experimentally, that this is not
  1530. * enough: the driver too often needlessly runs on a DMA timeout. 20ms
  1531. * as a minimum seem to work perfectly.
  1532. */
  1533. if (s->chan_rx) {
  1534. s->rx_timeout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
  1535. port->fifosize / 2;
  1536. dev_dbg(port->dev,
  1537. "DMA Rx t-out %ums, tty t-out %u jiffies\n",
  1538. s->rx_timeout * 1000 / HZ, port->timeout);
  1539. if (s->rx_timeout < msecs_to_jiffies(20))
  1540. s->rx_timeout = msecs_to_jiffies(20);
  1541. }
  1542. #endif
  1543. if ((termios->c_cflag & CREAD) != 0)
  1544. sci_start_rx(port);
  1545. sci_port_disable(s);
  1546. }
  1547. static const char *sci_type(struct uart_port *port)
  1548. {
  1549. switch (port->type) {
  1550. case PORT_IRDA:
  1551. return "irda";
  1552. case PORT_SCI:
  1553. return "sci";
  1554. case PORT_SCIF:
  1555. return "scif";
  1556. case PORT_SCIFA:
  1557. return "scifa";
  1558. case PORT_SCIFB:
  1559. return "scifb";
  1560. }
  1561. return NULL;
  1562. }
  1563. static inline unsigned long sci_port_size(struct uart_port *port)
  1564. {
  1565. /*
  1566. * Pick an arbitrary size that encapsulates all of the base
  1567. * registers by default. This can be optimized later, or derived
  1568. * from platform resource data at such a time that ports begin to
  1569. * behave more erratically.
  1570. */
  1571. return 64;
  1572. }
  1573. static int sci_remap_port(struct uart_port *port)
  1574. {
  1575. unsigned long size = sci_port_size(port);
  1576. /*
  1577. * Nothing to do if there's already an established membase.
  1578. */
  1579. if (port->membase)
  1580. return 0;
  1581. if (port->flags & UPF_IOREMAP) {
  1582. port->membase = ioremap_nocache(port->mapbase, size);
  1583. if (unlikely(!port->membase)) {
  1584. dev_err(port->dev, "can't remap port#%d\n", port->line);
  1585. return -ENXIO;
  1586. }
  1587. } else {
  1588. /*
  1589. * For the simple (and majority of) cases where we don't
  1590. * need to do any remapping, just cast the cookie
  1591. * directly.
  1592. */
  1593. port->membase = (void __iomem *)port->mapbase;
  1594. }
  1595. return 0;
  1596. }
  1597. static void sci_release_port(struct uart_port *port)
  1598. {
  1599. if (port->flags & UPF_IOREMAP) {
  1600. iounmap(port->membase);
  1601. port->membase = NULL;
  1602. }
  1603. release_mem_region(port->mapbase, sci_port_size(port));
  1604. }
  1605. static int sci_request_port(struct uart_port *port)
  1606. {
  1607. unsigned long size = sci_port_size(port);
  1608. struct resource *res;
  1609. int ret;
  1610. res = request_mem_region(port->mapbase, size, dev_name(port->dev));
  1611. if (unlikely(res == NULL))
  1612. return -EBUSY;
  1613. ret = sci_remap_port(port);
  1614. if (unlikely(ret != 0)) {
  1615. release_resource(res);
  1616. return ret;
  1617. }
  1618. return 0;
  1619. }
  1620. static void sci_config_port(struct uart_port *port, int flags)
  1621. {
  1622. if (flags & UART_CONFIG_TYPE) {
  1623. struct sci_port *sport = to_sci_port(port);
  1624. port->type = sport->cfg->type;
  1625. sci_request_port(port);
  1626. }
  1627. }
  1628. static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
  1629. {
  1630. struct sci_port *s = to_sci_port(port);
  1631. if (ser->irq != s->cfg->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
  1632. return -EINVAL;
  1633. if (ser->baud_base < 2400)
  1634. /* No paper tape reader for Mitch.. */
  1635. return -EINVAL;
  1636. return 0;
  1637. }
  1638. static struct uart_ops sci_uart_ops = {
  1639. .tx_empty = sci_tx_empty,
  1640. .set_mctrl = sci_set_mctrl,
  1641. .get_mctrl = sci_get_mctrl,
  1642. .start_tx = sci_start_tx,
  1643. .stop_tx = sci_stop_tx,
  1644. .stop_rx = sci_stop_rx,
  1645. .enable_ms = sci_enable_ms,
  1646. .break_ctl = sci_break_ctl,
  1647. .startup = sci_startup,
  1648. .shutdown = sci_shutdown,
  1649. .set_termios = sci_set_termios,
  1650. .type = sci_type,
  1651. .release_port = sci_release_port,
  1652. .request_port = sci_request_port,
  1653. .config_port = sci_config_port,
  1654. .verify_port = sci_verify_port,
  1655. #ifdef CONFIG_CONSOLE_POLL
  1656. .poll_get_char = sci_poll_get_char,
  1657. .poll_put_char = sci_poll_put_char,
  1658. #endif
  1659. };
  1660. static int __devinit sci_init_single(struct platform_device *dev,
  1661. struct sci_port *sci_port,
  1662. unsigned int index,
  1663. struct plat_sci_port *p)
  1664. {
  1665. struct uart_port *port = &sci_port->port;
  1666. int ret;
  1667. sci_port->cfg = p;
  1668. port->ops = &sci_uart_ops;
  1669. port->iotype = UPIO_MEM;
  1670. port->line = index;
  1671. switch (p->type) {
  1672. case PORT_SCIFB:
  1673. port->fifosize = 256;
  1674. break;
  1675. case PORT_SCIFA:
  1676. port->fifosize = 64;
  1677. break;
  1678. case PORT_SCIF:
  1679. port->fifosize = 16;
  1680. break;
  1681. default:
  1682. port->fifosize = 1;
  1683. break;
  1684. }
  1685. if (p->regtype == SCIx_PROBE_REGTYPE) {
  1686. ret = sci_probe_regmap(p);
  1687. if (unlikely(ret))
  1688. return ret;
  1689. }
  1690. if (dev) {
  1691. sci_port->iclk = clk_get(&dev->dev, "sci_ick");
  1692. if (IS_ERR(sci_port->iclk)) {
  1693. sci_port->iclk = clk_get(&dev->dev, "peripheral_clk");
  1694. if (IS_ERR(sci_port->iclk)) {
  1695. dev_err(&dev->dev, "can't get iclk\n");
  1696. return PTR_ERR(sci_port->iclk);
  1697. }
  1698. }
  1699. /*
  1700. * The function clock is optional, ignore it if we can't
  1701. * find it.
  1702. */
  1703. sci_port->fclk = clk_get(&dev->dev, "sci_fck");
  1704. if (IS_ERR(sci_port->fclk))
  1705. sci_port->fclk = NULL;
  1706. port->dev = &dev->dev;
  1707. sci_init_gpios(sci_port);
  1708. pm_runtime_irq_safe(&dev->dev);
  1709. pm_runtime_get_noresume(&dev->dev);
  1710. pm_runtime_enable(&dev->dev);
  1711. }
  1712. sci_port->break_timer.data = (unsigned long)sci_port;
  1713. sci_port->break_timer.function = sci_break_timer;
  1714. init_timer(&sci_port->break_timer);
  1715. /*
  1716. * Establish some sensible defaults for the error detection.
  1717. */
  1718. if (!p->error_mask)
  1719. p->error_mask = (p->type == PORT_SCI) ?
  1720. SCI_DEFAULT_ERROR_MASK : SCIF_DEFAULT_ERROR_MASK;
  1721. /*
  1722. * Establish sensible defaults for the overrun detection, unless
  1723. * the part has explicitly disabled support for it.
  1724. */
  1725. if (p->overrun_bit != SCIx_NOT_SUPPORTED) {
  1726. if (p->type == PORT_SCI)
  1727. p->overrun_bit = 5;
  1728. else if (p->scbrr_algo_id == SCBRR_ALGO_4)
  1729. p->overrun_bit = 9;
  1730. else
  1731. p->overrun_bit = 0;
  1732. /*
  1733. * Make the error mask inclusive of overrun detection, if
  1734. * supported.
  1735. */
  1736. p->error_mask |= (1 << p->overrun_bit);
  1737. }
  1738. port->mapbase = p->mapbase;
  1739. port->type = p->type;
  1740. port->flags = p->flags;
  1741. port->regshift = p->regshift;
  1742. /*
  1743. * The UART port needs an IRQ value, so we peg this to the RX IRQ
  1744. * for the multi-IRQ ports, which is where we are primarily
  1745. * concerned with the shutdown path synchronization.
  1746. *
  1747. * For the muxed case there's nothing more to do.
  1748. */
  1749. port->irq = p->irqs[SCIx_RXI_IRQ];
  1750. port->irqflags = 0;
  1751. port->serial_in = sci_serial_in;
  1752. port->serial_out = sci_serial_out;
  1753. if (p->dma_slave_tx > 0 && p->dma_slave_rx > 0)
  1754. dev_dbg(port->dev, "DMA tx %d, rx %d\n",
  1755. p->dma_slave_tx, p->dma_slave_rx);
  1756. return 0;
  1757. }
  1758. #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
  1759. static void serial_console_putchar(struct uart_port *port, int ch)
  1760. {
  1761. sci_poll_put_char(port, ch);
  1762. }
  1763. /*
  1764. * Print a string to the serial port trying not to disturb
  1765. * any possible real use of the port...
  1766. */
  1767. static void serial_console_write(struct console *co, const char *s,
  1768. unsigned count)
  1769. {
  1770. struct sci_port *sci_port = &sci_ports[co->index];
  1771. struct uart_port *port = &sci_port->port;
  1772. unsigned short bits;
  1773. sci_port_enable(sci_port);
  1774. uart_console_write(port, s, count, serial_console_putchar);
  1775. /* wait until fifo is empty and last bit has been transmitted */
  1776. bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
  1777. while ((serial_port_in(port, SCxSR) & bits) != bits)
  1778. cpu_relax();
  1779. sci_port_disable(sci_port);
  1780. }
  1781. static int __devinit serial_console_setup(struct console *co, char *options)
  1782. {
  1783. struct sci_port *sci_port;
  1784. struct uart_port *port;
  1785. int baud = 115200;
  1786. int bits = 8;
  1787. int parity = 'n';
  1788. int flow = 'n';
  1789. int ret;
  1790. /*
  1791. * Refuse to handle any bogus ports.
  1792. */
  1793. if (co->index < 0 || co->index >= SCI_NPORTS)
  1794. return -ENODEV;
  1795. sci_port = &sci_ports[co->index];
  1796. port = &sci_port->port;
  1797. /*
  1798. * Refuse to handle uninitialized ports.
  1799. */
  1800. if (!port->ops)
  1801. return -ENODEV;
  1802. ret = sci_remap_port(port);
  1803. if (unlikely(ret != 0))
  1804. return ret;
  1805. sci_port_enable(sci_port);
  1806. if (options)
  1807. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1808. sci_port_disable(sci_port);
  1809. return uart_set_options(port, co, baud, parity, bits, flow);
  1810. }
  1811. static struct console serial_console = {
  1812. .name = "ttySC",
  1813. .device = uart_console_device,
  1814. .write = serial_console_write,
  1815. .setup = serial_console_setup,
  1816. .flags = CON_PRINTBUFFER,
  1817. .index = -1,
  1818. .data = &sci_uart_driver,
  1819. };
  1820. static struct console early_serial_console = {
  1821. .name = "early_ttySC",
  1822. .write = serial_console_write,
  1823. .flags = CON_PRINTBUFFER,
  1824. .index = -1,
  1825. };
  1826. static char early_serial_buf[32];
  1827. static int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
  1828. {
  1829. struct plat_sci_port *cfg = pdev->dev.platform_data;
  1830. if (early_serial_console.data)
  1831. return -EEXIST;
  1832. early_serial_console.index = pdev->id;
  1833. sci_init_single(NULL, &sci_ports[pdev->id], pdev->id, cfg);
  1834. serial_console_setup(&early_serial_console, early_serial_buf);
  1835. if (!strstr(early_serial_buf, "keep"))
  1836. early_serial_console.flags |= CON_BOOT;
  1837. register_console(&early_serial_console);
  1838. return 0;
  1839. }
  1840. #define uart_console(port) ((port)->cons->index == (port)->line)
  1841. static int sci_runtime_suspend(struct device *dev)
  1842. {
  1843. struct sci_port *sci_port = dev_get_drvdata(dev);
  1844. struct uart_port *port = &sci_port->port;
  1845. if (uart_console(port)) {
  1846. struct plat_sci_reg *reg;
  1847. sci_port->saved_smr = serial_port_in(port, SCSMR);
  1848. sci_port->saved_brr = serial_port_in(port, SCBRR);
  1849. reg = sci_getreg(port, SCFCR);
  1850. if (reg->size)
  1851. sci_port->saved_fcr = serial_port_in(port, SCFCR);
  1852. else
  1853. sci_port->saved_fcr = 0;
  1854. }
  1855. return 0;
  1856. }
  1857. static int sci_runtime_resume(struct device *dev)
  1858. {
  1859. struct sci_port *sci_port = dev_get_drvdata(dev);
  1860. struct uart_port *port = &sci_port->port;
  1861. if (uart_console(port)) {
  1862. sci_reset(port);
  1863. serial_port_out(port, SCSMR, sci_port->saved_smr);
  1864. serial_port_out(port, SCBRR, sci_port->saved_brr);
  1865. if (sci_port->saved_fcr)
  1866. serial_port_out(port, SCFCR, sci_port->saved_fcr);
  1867. serial_port_out(port, SCSCR, sci_port->cfg->scscr);
  1868. }
  1869. return 0;
  1870. }
  1871. #define SCI_CONSOLE (&serial_console)
  1872. #else
  1873. static inline int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
  1874. {
  1875. return -EINVAL;
  1876. }
  1877. #define SCI_CONSOLE NULL
  1878. #define sci_runtime_suspend NULL
  1879. #define sci_runtime_resume NULL
  1880. #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
  1881. static char banner[] __initdata =
  1882. KERN_INFO "SuperH SCI(F) driver initialized\n";
  1883. static struct uart_driver sci_uart_driver = {
  1884. .owner = THIS_MODULE,
  1885. .driver_name = "sci",
  1886. .dev_name = "ttySC",
  1887. .major = SCI_MAJOR,
  1888. .minor = SCI_MINOR_START,
  1889. .nr = SCI_NPORTS,
  1890. .cons = SCI_CONSOLE,
  1891. };
  1892. static int sci_remove(struct platform_device *dev)
  1893. {
  1894. struct sci_port *port = platform_get_drvdata(dev);
  1895. cpufreq_unregister_notifier(&port->freq_transition,
  1896. CPUFREQ_TRANSITION_NOTIFIER);
  1897. sci_free_gpios(port);
  1898. uart_remove_one_port(&sci_uart_driver, &port->port);
  1899. clk_put(port->iclk);
  1900. clk_put(port->fclk);
  1901. pm_runtime_disable(&dev->dev);
  1902. return 0;
  1903. }
  1904. static int __devinit sci_probe_single(struct platform_device *dev,
  1905. unsigned int index,
  1906. struct plat_sci_port *p,
  1907. struct sci_port *sciport)
  1908. {
  1909. int ret;
  1910. /* Sanity check */
  1911. if (unlikely(index >= SCI_NPORTS)) {
  1912. dev_notice(&dev->dev, "Attempting to register port "
  1913. "%d when only %d are available.\n",
  1914. index+1, SCI_NPORTS);
  1915. dev_notice(&dev->dev, "Consider bumping "
  1916. "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
  1917. return 0;
  1918. }
  1919. ret = sci_init_single(dev, sciport, index, p);
  1920. if (ret)
  1921. return ret;
  1922. return uart_add_one_port(&sci_uart_driver, &sciport->port);
  1923. }
  1924. static int __devinit sci_probe(struct platform_device *dev)
  1925. {
  1926. struct plat_sci_port *p = dev->dev.platform_data;
  1927. struct sci_port *sp = &sci_ports[dev->id];
  1928. int ret;
  1929. /*
  1930. * If we've come here via earlyprintk initialization, head off to
  1931. * the special early probe. We don't have sufficient device state
  1932. * to make it beyond this yet.
  1933. */
  1934. if (is_early_platform_device(dev))
  1935. return sci_probe_earlyprintk(dev);
  1936. platform_set_drvdata(dev, sp);
  1937. ret = sci_probe_single(dev, dev->id, p, sp);
  1938. if (ret)
  1939. goto err_unreg;
  1940. sp->freq_transition.notifier_call = sci_notifier;
  1941. ret = cpufreq_register_notifier(&sp->freq_transition,
  1942. CPUFREQ_TRANSITION_NOTIFIER);
  1943. if (unlikely(ret < 0))
  1944. goto err_unreg;
  1945. #ifdef CONFIG_SH_STANDARD_BIOS
  1946. sh_bios_gdb_detach();
  1947. #endif
  1948. return 0;
  1949. err_unreg:
  1950. sci_remove(dev);
  1951. return ret;
  1952. }
  1953. static int sci_suspend(struct device *dev)
  1954. {
  1955. struct sci_port *sport = dev_get_drvdata(dev);
  1956. if (sport)
  1957. uart_suspend_port(&sci_uart_driver, &sport->port);
  1958. return 0;
  1959. }
  1960. static int sci_resume(struct device *dev)
  1961. {
  1962. struct sci_port *sport = dev_get_drvdata(dev);
  1963. if (sport)
  1964. uart_resume_port(&sci_uart_driver, &sport->port);
  1965. return 0;
  1966. }
  1967. static const struct dev_pm_ops sci_dev_pm_ops = {
  1968. .runtime_suspend = sci_runtime_suspend,
  1969. .runtime_resume = sci_runtime_resume,
  1970. .suspend = sci_suspend,
  1971. .resume = sci_resume,
  1972. };
  1973. static struct platform_driver sci_driver = {
  1974. .probe = sci_probe,
  1975. .remove = sci_remove,
  1976. .driver = {
  1977. .name = "sh-sci",
  1978. .owner = THIS_MODULE,
  1979. .pm = &sci_dev_pm_ops,
  1980. },
  1981. };
  1982. static int __init sci_init(void)
  1983. {
  1984. int ret;
  1985. printk(banner);
  1986. ret = uart_register_driver(&sci_uart_driver);
  1987. if (likely(ret == 0)) {
  1988. ret = platform_driver_register(&sci_driver);
  1989. if (unlikely(ret))
  1990. uart_unregister_driver(&sci_uart_driver);
  1991. }
  1992. return ret;
  1993. }
  1994. static void __exit sci_exit(void)
  1995. {
  1996. platform_driver_unregister(&sci_driver);
  1997. uart_unregister_driver(&sci_uart_driver);
  1998. }
  1999. #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
  2000. early_platform_init_buffer("earlyprintk", &sci_driver,
  2001. early_serial_buf, ARRAY_SIZE(early_serial_buf));
  2002. #endif
  2003. module_init(sci_init);
  2004. module_exit(sci_exit);
  2005. MODULE_LICENSE("GPL");
  2006. MODULE_ALIAS("platform:sh-sci");
  2007. MODULE_AUTHOR("Paul Mundt");
  2008. MODULE_DESCRIPTION("SuperH SCI(F) serial driver");