sh-sci.c 51 KB

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