sh-sci.c 49 KB

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