sh-sci.c 49 KB

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