sh-sci.c 48 KB

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