sh-sci.c 57 KB

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