sh-sci.c 54 KB

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