serial_sicc.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010
  1. /*
  2. * arch/ppc/4xx_io/serial_sicc.c
  3. *
  4. * Driver for IBM STB3xxx SICC serial port
  5. *
  6. * Based on drivers/char/serial_amba.c, by ARM Ltd.
  7. *
  8. * Copyright 2001 IBM Crop.
  9. * Author: IBM China Research Lab
  10. * Yudong Yang <yangyud@cn.ibm.com>
  11. * Yi Ge <geyi@cn.ibm.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. *
  28. * This is a driver for SICC serial port on IBM Redwood 4 evaluation board.
  29. * The driver support both as a console device and normal serial device and
  30. * is compatible with normal ttyS* devices.
  31. */
  32. #include <linux/config.h>
  33. #include <linux/module.h>
  34. #include <linux/kernel.h>
  35. #include <linux/errno.h>
  36. #include <linux/signal.h>
  37. #include <linux/sched.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/tty.h>
  40. #include <linux/tty_flip.h>
  41. #include <linux/major.h>
  42. #include <linux/string.h>
  43. #include <linux/fcntl.h>
  44. #include <linux/ptrace.h>
  45. #include <linux/ioport.h>
  46. #include <linux/mm.h>
  47. #include <linux/slab.h>
  48. #include <linux/init.h>
  49. #include <linux/capability.h>
  50. #include <linux/circ_buf.h>
  51. #include <linux/serial.h>
  52. #include <linux/console.h>
  53. #include <linux/sysrq.h>
  54. #include <linux/bitops.h>
  55. #include <asm/system.h>
  56. #include <asm/io.h>
  57. #include <asm/irq.h>
  58. #include <asm/uaccess.h>
  59. #include <asm/serial.h>
  60. #include <linux/serialP.h>
  61. /* -----------------------------------------------------------------------------
  62. * From STB03xxx SICC UART Specification
  63. * -----------------------------------------------------------------------------
  64. * UART Register Offsets.
  65. */
  66. #define BL_SICC_LSR 0x0000000 /* line status register read/clear */
  67. #define BL_SICC_LSRS 0x0000001 /* set line status register read/set */
  68. #define BL_SICC_HSR 0x0000002 /* handshake status register r/clear */
  69. #define BL_SICC_HSRS 0x0000003 /* set handshake status register r/set */
  70. #define BL_SICC_BRDH 0x0000004 /* baudrate divisor high reg r/w */
  71. #define BL_SICC_BRDL 0x0000005 /* baudrate divisor low reg r/w */
  72. #define BL_SICC_LCR 0x0000006 /* control register r/w */
  73. #define BL_SICC_RCR 0x0000007 /* receiver command register r/w */
  74. #define BL_SICC_TxCR 0x0000008 /* transmitter command register r/w */
  75. #define BL_SICC_RBR 0x0000009 /* receive buffer r */
  76. #define BL_SICC_TBR 0x0000009 /* transmit buffer w */
  77. #define BL_SICC_CTL2 0x000000A /* added for Vesta */
  78. #define BL_SICC_IrCR 0x000000B /* added for Vesta IR */
  79. /* masks and definitions for serial port control register */
  80. #define _LCR_LM_MASK 0xc0 /* loop back modes */
  81. #define _LCR_DTR_MASK 0x20 /* data terminal ready 0-inactive */
  82. #define _LCR_RTS_MASK 0x10 /* request to send 0-inactive */
  83. #define _LCR_DB_MASK 0x08 /* data bits mask */
  84. #define _LCR_PE_MASK 0x04 /* parity enable */
  85. #define _LCR_PTY_MASK 0x02 /* parity */
  86. #define _LCR_SB_MASK 0x01 /* stop bit mask */
  87. #define _LCR_LM_NORM 0x00 /* normal operation */
  88. #define _LCR_LM_LOOP 0x40 /* internal loopback mode */
  89. #define _LCR_LM_ECHO 0x80 /* automatic echo mode */
  90. #define _LCR_LM_RES 0xc0 /* reserved */
  91. #define _LCR_DTR_ACTIVE _LCR_DTR_MASK /* DTR is active */
  92. #define _LCR_RTS_ACTIVE _LCR_RTS_MASK /* RTS is active */
  93. #define _LCR_DB_8_BITS _LCR_DB_MASK /* 8 data bits */
  94. #define _LCR_DB_7_BITS 0x00 /* 7 data bits */
  95. #define _LCR_PE_ENABLE _LCR_PE_MASK /* parity enabled */
  96. #define _LCR_PE_DISABLE 0x00 /* parity disabled */
  97. #define _LCR_PTY_EVEN 0x00 /* even parity */
  98. #define _LCR_PTY_ODD _LCR_PTY_MASK /* odd parity */
  99. #define _LCR_SB_1_BIT 0x00 /* one stop bit */
  100. #define _LCR_SB_2_BIT _LCR_SB_MASK /* two stop bit */
  101. /* serial port handshake register */
  102. #define _HSR_DIS_MASK 0x80 /* DSR input inactive error mask */
  103. #define _HSR_CS_MASK 0x40 /* CTS input inactive error mask */
  104. #define _HSR_DIS_ACT 0x00 /* dsr input is active */
  105. #define _HSR_DIS_INACT _HSR_DIS_MASK /* dsr input is inactive */
  106. #define _HSR_CS_ACT 0x00 /* cts input is active */
  107. #define _HSR_CS_INACT _HSR_CS_MASK /* cts input is active */
  108. /* serial port line status register */
  109. #define _LSR_RBR_MASK 0x80 /* receive buffer ready mask */
  110. #define _LSR_FE_MASK 0x40 /* framing error */
  111. #define _LSR_OE_MASK 0x20 /* overrun error */
  112. #define _LSR_PE_MASK 0x10 /* parity error */
  113. #define _LSR_LB_MASK 0x08 /* line break */
  114. #define _LSR_TBR_MASK 0x04 /* transmit buffer ready */
  115. #define _LSR_TSR_MASK 0x02 /* transmit shift register ready */
  116. #define _LSR_RBR_FULL _LSR_RBR_MASK /* receive buffer is full */
  117. #define _LSR_FE_ERROR _LSR_FE_MASK /* framing error detected */
  118. #define _LSR_OE_ERROR _LSR_OE_MASK /* overrun error detected */
  119. #define _LSR_PE_ERROR _LSR_PE_MASK /* parity error detected */
  120. #define _LSR_LB_BREAK _LSR_LB_MASK /* line break detected */
  121. #define _LSR_TBR_EMPTY _LSR_TBR_MASK /* transmit buffer is ready */
  122. #define _LSR_TSR_EMPTY _LSR_TSR_MASK /* transmit shift register is empty */
  123. #define _LSR_TX_ALL 0x06 /* all physical transmit is done */
  124. #define _LSR_RX_ERR (_LSR_LB_BREAK | _LSR_FE_MASK | _LSR_OE_MASK | \
  125. _LSR_PE_MASK )
  126. /* serial port receiver command register */
  127. #define _RCR_ER_MASK 0x80 /* enable receiver mask */
  128. #define _RCR_DME_MASK 0x60 /* dma mode */
  129. #define _RCR_EIE_MASK 0x10 /* error interrupt enable mask */
  130. #define _RCR_PME_MASK 0x08 /* pause mode mask */
  131. #define _RCR_ER_ENABLE _RCR_ER_MASK /* receiver enabled */
  132. #define _RCR_DME_DISABLE 0x00 /* dma disabled */
  133. #define _RCR_DME_RXRDY 0x20 /* dma disabled, RxRDY interrupt enabled*/
  134. #define _RCR_DME_ENABLE2 0x40 /* dma enabled,receiver src channel 2 */
  135. #define _RCR_DME_ENABLE3 0x60 /* dma enabled,receiver src channel 3 */
  136. #define _RCR_PME_HARD _RCR_PME_MASK /* RTS controlled by hardware */
  137. #define _RCR_PME_SOFT 0x00 /* RTS controlled by software */
  138. /* serial port transmit command register */
  139. #define _TxCR_ET_MASK 0x80 /* transmiter enable mask */
  140. #define _TxCR_DME_MASK 0x60 /* dma mode mask */
  141. #define _TxCR_TIE_MASK 0x10 /* empty interrupt enable mask */
  142. #define _TxCR_EIE_MASK 0x08 /* error interrupt enable mask */
  143. #define _TxCR_SPE_MASK 0x04 /* stop/pause mask */
  144. #define _TxCR_TB_MASK 0x02 /* transmit break mask */
  145. #define _TxCR_ET_ENABLE _TxCR_ET_MASK /* transmiter enabled */
  146. #define _TxCR_DME_DISABLE 0x00 /* transmiter disabled, TBR intr disabled */
  147. #define _TxCR_DME_TBR 0x20 /* transmiter disabled, TBR intr enabled */
  148. #define _TxCR_DME_CHAN_2 0x40 /* dma enabled, destination chann 2 */
  149. #define _TxCR_DME_CHAN_3 0x60 /* dma enabled, destination chann 3 */
  150. /* serial ctl reg 2 - added for Vesta */
  151. #define _CTL2_EXTERN 0x80 /* */
  152. #define _CTL2_USEFIFO 0x40 /* */
  153. #define _CTL2_RESETRF 0x08 /* */
  154. #define _CTL2_RESETTF 0x04 /* */
  155. #define SERIAL_SICC_NAME "ttySICC"
  156. #define SERIAL_SICC_MAJOR 150
  157. #define SERIAL_SICC_MINOR 1
  158. #define SERIAL_SICC_NR 1
  159. #ifndef TRUE
  160. #define TRUE 1
  161. #endif
  162. #ifndef FALSE
  163. #define FALSE 0
  164. #endif
  165. /*
  166. * Things needed by tty driver
  167. */
  168. static struct tty_driver *siccnormal_driver;
  169. #if defined(CONFIG_SERIAL_SICC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  170. #define SUPPORT_SYSRQ
  171. #endif
  172. /*
  173. * Things needed internally to this driver
  174. */
  175. /*
  176. * tmp_buf is used as a temporary buffer by serial_write. We need to
  177. * lock it in case the copy_from_user blocks while swapping in a page,
  178. * and some other program tries to do a serial write at the same time.
  179. * Since the lock will only come under contention when the system is
  180. * swapping and available memory is low, it makes sense to share one
  181. * buffer across all the serial ports, since it significantly saves
  182. * memory if large numbers of serial ports are open.
  183. */
  184. static u_char *tmp_buf;
  185. static DECLARE_MUTEX(tmp_buf_sem);
  186. #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
  187. /* number of characters left in xmit buffer before we ask for more */
  188. #define WAKEUP_CHARS 256
  189. #define SICC_ISR_PASS_LIMIT 256
  190. #define EVT_WRITE_WAKEUP 0
  191. struct SICC_icount {
  192. __u32 cts;
  193. __u32 dsr;
  194. __u32 rng;
  195. __u32 dcd;
  196. __u32 rx;
  197. __u32 tx;
  198. __u32 frame;
  199. __u32 overrun;
  200. __u32 parity;
  201. __u32 brk;
  202. __u32 buf_overrun;
  203. };
  204. /*
  205. * Static information about the port
  206. */
  207. struct SICC_port {
  208. unsigned int uart_base;
  209. unsigned int uart_base_phys;
  210. unsigned int irqrx;
  211. unsigned int irqtx;
  212. unsigned int uartclk;
  213. unsigned int fifosize;
  214. unsigned int tiocm_support;
  215. void (*set_mctrl)(struct SICC_port *, u_int mctrl);
  216. };
  217. /*
  218. * This is the state information which is persistent across opens
  219. */
  220. struct SICC_state {
  221. struct SICC_icount icount;
  222. unsigned int line;
  223. unsigned int close_delay;
  224. unsigned int closing_wait;
  225. unsigned int custom_divisor;
  226. unsigned int flags;
  227. int count;
  228. struct SICC_info *info;
  229. spinlock_t sicc_lock;
  230. };
  231. #define SICC_XMIT_SIZE 1024
  232. /*
  233. * This is the state information which is only valid when the port is open.
  234. */
  235. struct SICC_info {
  236. struct SICC_port *port;
  237. struct SICC_state *state;
  238. struct tty_struct *tty;
  239. unsigned char x_char;
  240. unsigned char old_status;
  241. unsigned char read_status_mask;
  242. unsigned char ignore_status_mask;
  243. struct circ_buf xmit;
  244. unsigned int flags;
  245. #ifdef SUPPORT_SYSRQ
  246. unsigned long sysrq;
  247. #endif
  248. unsigned int event;
  249. unsigned int timeout;
  250. unsigned int lcr_h;
  251. unsigned int mctrl;
  252. int blocked_open;
  253. struct tasklet_struct tlet;
  254. wait_queue_head_t open_wait;
  255. wait_queue_head_t close_wait;
  256. wait_queue_head_t delta_msr_wait;
  257. };
  258. #ifdef CONFIG_SERIAL_SICC_CONSOLE
  259. static struct console siccuart_cons;
  260. #endif
  261. static void siccuart_change_speed(struct SICC_info *info, struct termios *old_termios);
  262. static void siccuart_wait_until_sent(struct tty_struct *tty, int timeout);
  263. static void powerpcMtcic_cr(unsigned long value)
  264. {
  265. mtdcr(DCRN_CICCR, value);
  266. }
  267. static unsigned long powerpcMfcic_cr(void)
  268. {
  269. return mfdcr(DCRN_CICCR);
  270. }
  271. static unsigned long powerpcMfclkgpcr(void)
  272. {
  273. return mfdcr(DCRN_SCCR);
  274. }
  275. static void sicc_set_mctrl_null(struct SICC_port *port, u_int mctrl)
  276. {
  277. }
  278. static struct SICC_port sicc_ports[SERIAL_SICC_NR] = {
  279. {
  280. .uart_base = 0,
  281. .uart_base_phys = SICC0_IO_BASE,
  282. .irqrx = SICC0_INTRX,
  283. .irqtx = SICC0_INTTX,
  284. // .uartclk = 0,
  285. .fifosize = 1,
  286. .set_mctrl = sicc_set_mctrl_null,
  287. }
  288. };
  289. static struct SICC_state sicc_state[SERIAL_SICC_NR];
  290. static void siccuart_enable_rx_interrupt(struct SICC_info *info)
  291. {
  292. unsigned char cr;
  293. cr = readb(info->port->uart_base+BL_SICC_RCR);
  294. cr &= ~_RCR_DME_MASK;
  295. cr |= _RCR_DME_RXRDY;
  296. writeb(cr, info->port->uart_base+BL_SICC_RCR);
  297. }
  298. static void siccuart_disable_rx_interrupt(struct SICC_info *info)
  299. {
  300. unsigned char cr;
  301. cr = readb(info->port->uart_base+BL_SICC_RCR);
  302. cr &= ~_RCR_DME_MASK;
  303. cr |= _RCR_DME_DISABLE;
  304. writeb(cr, info->port->uart_base+BL_SICC_RCR);
  305. }
  306. static void siccuart_enable_tx_interrupt(struct SICC_info *info)
  307. {
  308. unsigned char cr;
  309. cr = readb(info->port->uart_base+BL_SICC_TxCR);
  310. cr &= ~_TxCR_DME_MASK;
  311. cr |= _TxCR_DME_TBR;
  312. writeb(cr, info->port->uart_base+BL_SICC_TxCR);
  313. }
  314. static void siccuart_disable_tx_interrupt(struct SICC_info *info)
  315. {
  316. unsigned char cr;
  317. cr = readb(info->port->uart_base+BL_SICC_TxCR);
  318. cr &= ~_TxCR_DME_MASK;
  319. cr |= _TxCR_DME_DISABLE;
  320. writeb(cr, info->port->uart_base+BL_SICC_TxCR);
  321. }
  322. static void siccuart_stop(struct tty_struct *tty)
  323. {
  324. struct SICC_info *info = tty->driver_data;
  325. unsigned long flags;
  326. /* disable interrupts while stopping serial port interrupts */
  327. spin_lock_irqsave(&info->state->sicc_lock,flags);
  328. siccuart_disable_tx_interrupt(info);
  329. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  330. }
  331. static void siccuart_start(struct tty_struct *tty)
  332. {
  333. struct SICC_info *info = tty->driver_data;
  334. unsigned long flags;
  335. /* disable interrupts while starting serial port interrupts */
  336. spin_lock_irqsave(&info->state->sicc_lock,flags);
  337. if (info->xmit.head != info->xmit.tail
  338. && info->xmit.buf)
  339. siccuart_enable_tx_interrupt(info);
  340. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  341. }
  342. /*
  343. * This routine is used by the interrupt handler to schedule
  344. * processing in the software interrupt portion of the driver.
  345. */
  346. static void siccuart_event(struct SICC_info *info, int event)
  347. {
  348. info->event |= 1 << event;
  349. tasklet_schedule(&info->tlet);
  350. }
  351. static void
  352. siccuart_rx_chars(struct SICC_info *info, struct pt_regs *regs)
  353. {
  354. struct tty_struct *tty = info->tty;
  355. unsigned int status, ch, rsr, flg, ignored = 0;
  356. struct SICC_icount *icount = &info->state->icount;
  357. struct SICC_port *port = info->port;
  358. status = readb(port->uart_base+BL_SICC_LSR );
  359. while (status & _LSR_RBR_FULL) {
  360. ch = readb(port->uart_base+BL_SICC_RBR);
  361. if (tty->flip.count >= TTY_FLIPBUF_SIZE)
  362. goto ignore_char;
  363. icount->rx++;
  364. flg = TTY_NORMAL;
  365. /*
  366. * Note that the error handling code is
  367. * out of the main execution path
  368. */
  369. rsr = readb(port->uart_base+BL_SICC_LSR);
  370. if (rsr & _LSR_RX_ERR)
  371. goto handle_error;
  372. #ifdef SUPPORT_SYSRQ
  373. if (info->sysrq) {
  374. if (ch && time_before(jiffies, info->sysrq)) {
  375. handle_sysrq(ch, regs, NULL);
  376. info->sysrq = 0;
  377. goto ignore_char;
  378. }
  379. info->sysrq = 0;
  380. }
  381. #endif
  382. error_return:
  383. *tty->flip.flag_buf_ptr++ = flg;
  384. *tty->flip.char_buf_ptr++ = ch;
  385. tty->flip.count++;
  386. ignore_char:
  387. status = readb(port->uart_base+BL_SICC_LSR );
  388. }
  389. out:
  390. tty_flip_buffer_push(tty);
  391. return;
  392. handle_error:
  393. if (rsr & _LSR_LB_BREAK) {
  394. rsr &= ~(_LSR_FE_MASK | _LSR_PE_MASK);
  395. icount->brk++;
  396. #ifdef SUPPORT_SYSRQ
  397. if (info->state->line == siccuart_cons.index) {
  398. if (!info->sysrq) {
  399. info->sysrq = jiffies + HZ*5;
  400. goto ignore_char;
  401. }
  402. }
  403. #endif
  404. } else if (rsr & _LSR_PE_MASK)
  405. icount->parity++;
  406. else if (rsr & _LSR_FE_MASK)
  407. icount->frame++;
  408. if (rsr & _LSR_OE_MASK)
  409. icount->overrun++;
  410. if (rsr & info->ignore_status_mask) {
  411. if (++ignored > 100)
  412. goto out;
  413. goto ignore_char;
  414. }
  415. rsr &= info->read_status_mask;
  416. if (rsr & _LSR_LB_BREAK)
  417. flg = TTY_BREAK;
  418. else if (rsr & _LSR_PE_MASK)
  419. flg = TTY_PARITY;
  420. else if (rsr & _LSR_FE_MASK)
  421. flg = TTY_FRAME;
  422. if (rsr & _LSR_OE_MASK) {
  423. /*
  424. * CHECK: does overrun affect the current character?
  425. * ASSUMPTION: it does not.
  426. */
  427. *tty->flip.flag_buf_ptr++ = flg;
  428. *tty->flip.char_buf_ptr++ = ch;
  429. tty->flip.count++;
  430. if (tty->flip.count >= TTY_FLIPBUF_SIZE)
  431. goto ignore_char;
  432. ch = 0;
  433. flg = TTY_OVERRUN;
  434. }
  435. #ifdef SUPPORT_SYSRQ
  436. info->sysrq = 0;
  437. #endif
  438. goto error_return;
  439. }
  440. static void siccuart_tx_chars(struct SICC_info *info)
  441. {
  442. struct SICC_port *port = info->port;
  443. int count;
  444. unsigned char status;
  445. if (info->x_char) {
  446. writeb(info->x_char, port->uart_base+ BL_SICC_TBR);
  447. info->state->icount.tx++;
  448. info->x_char = 0;
  449. return;
  450. }
  451. if (info->xmit.head == info->xmit.tail
  452. || info->tty->stopped
  453. || info->tty->hw_stopped) {
  454. siccuart_disable_tx_interrupt(info);
  455. writeb(status&(~_LSR_RBR_MASK),port->uart_base+BL_SICC_LSR);
  456. return;
  457. }
  458. count = port->fifosize;
  459. do {
  460. writeb(info->xmit.buf[info->xmit.tail], port->uart_base+ BL_SICC_TBR);
  461. info->xmit.tail = (info->xmit.tail + 1) & (SICC_XMIT_SIZE - 1);
  462. info->state->icount.tx++;
  463. if (info->xmit.head == info->xmit.tail)
  464. break;
  465. } while (--count > 0);
  466. if (CIRC_CNT(info->xmit.head,
  467. info->xmit.tail,
  468. SICC_XMIT_SIZE) < WAKEUP_CHARS)
  469. siccuart_event(info, EVT_WRITE_WAKEUP);
  470. if (info->xmit.head == info->xmit.tail) {
  471. siccuart_disable_tx_interrupt(info);
  472. }
  473. }
  474. static irqreturn_t siccuart_int_rx(int irq, void *dev_id, struct pt_regs *regs)
  475. {
  476. struct SICC_info *info = dev_id;
  477. siccuart_rx_chars(info, regs);
  478. return IRQ_HANDLED;
  479. }
  480. static irqreturn_t siccuart_int_tx(int irq, void *dev_id, struct pt_regs *regs)
  481. {
  482. struct SICC_info *info = dev_id;
  483. siccuart_tx_chars(info);
  484. return IRQ_HANDLED;
  485. }
  486. static void siccuart_tasklet_action(unsigned long data)
  487. {
  488. struct SICC_info *info = (struct SICC_info *)data;
  489. struct tty_struct *tty;
  490. tty = info->tty;
  491. if (!tty || !test_and_clear_bit(EVT_WRITE_WAKEUP, &info->event))
  492. return;
  493. if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
  494. tty->ldisc.write_wakeup)
  495. (tty->ldisc.write_wakeup)(tty);
  496. wake_up_interruptible(&tty->write_wait);
  497. }
  498. static int siccuart_startup(struct SICC_info *info)
  499. {
  500. unsigned long flags;
  501. unsigned long page;
  502. int retval = 0;
  503. if (info->flags & ASYNC_INITIALIZED) {
  504. return 0;
  505. }
  506. page = get_zeroed_page(GFP_KERNEL);
  507. if (!page)
  508. return -ENOMEM;
  509. if (info->port->uart_base == 0)
  510. info->port->uart_base = (int)ioremap(info->port->uart_base_phys, PAGE_SIZE);
  511. if (info->port->uart_base == 0) {
  512. free_page(page);
  513. return -ENOMEM;
  514. }
  515. /* lock access to info while doing setup */
  516. spin_lock_irqsave(&info->state->sicc_lock,flags);
  517. if (info->xmit.buf)
  518. free_page(page);
  519. else
  520. info->xmit.buf = (unsigned char *) page;
  521. info->mctrl = 0;
  522. if (info->tty->termios->c_cflag & CBAUD)
  523. info->mctrl = TIOCM_RTS | TIOCM_DTR;
  524. info->port->set_mctrl(info->port, info->mctrl);
  525. /*
  526. * initialise the old status of the modem signals
  527. */
  528. info->old_status = 0; // UART_GET_FR(info->port) & AMBA_UARTFR_MODEM_ANY;
  529. if (info->tty)
  530. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  531. info->xmit.head = info->xmit.tail = 0;
  532. /*
  533. * Set up the tty->alt_speed kludge
  534. */
  535. if (info->tty) {
  536. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  537. info->tty->alt_speed = 57600;
  538. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  539. info->tty->alt_speed = 115200;
  540. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  541. info->tty->alt_speed = 230400;
  542. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  543. info->tty->alt_speed = 460800;
  544. }
  545. writeb( 0x00, info->port->uart_base + BL_SICC_IrCR ); // disable IrDA
  546. /*
  547. * and set the speed of the serial port
  548. */
  549. siccuart_change_speed(info, 0);
  550. // enable rx/tx ports
  551. writeb(_RCR_ER_ENABLE /*| _RCR_PME_HARD*/, info->port->uart_base + BL_SICC_RCR);
  552. writeb(_TxCR_ET_ENABLE , info->port->uart_base + BL_SICC_TxCR);
  553. readb(info->port->uart_base + BL_SICC_RBR); // clear rx port
  554. writeb(0xf8, info->port->uart_base + BL_SICC_LSR); /* reset bits 0-4 of LSR */
  555. /*
  556. * Finally, enable interrupts
  557. */
  558. /*
  559. * Allocate the IRQ
  560. */
  561. retval = request_irq(info->port->irqrx, siccuart_int_rx, 0, "SICC rx", info);
  562. if (retval) {
  563. if (capable(CAP_SYS_ADMIN)) {
  564. if (info->tty)
  565. set_bit(TTY_IO_ERROR, &info->tty->flags);
  566. retval = 0;
  567. }
  568. goto errout;
  569. }
  570. retval = request_irq(info->port->irqtx, siccuart_int_tx, 0, "SICC tx", info);
  571. if (retval) {
  572. if (capable(CAP_SYS_ADMIN)) {
  573. if (info->tty)
  574. set_bit(TTY_IO_ERROR, &info->tty->flags);
  575. retval = 0;
  576. }
  577. free_irq(info->port->irqrx, info);
  578. goto errout;
  579. }
  580. siccuart_enable_rx_interrupt(info);
  581. info->flags |= ASYNC_INITIALIZED;
  582. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  583. return 0;
  584. errout:
  585. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  586. return retval;
  587. }
  588. /*
  589. * This routine will shutdown a serial port; interrupts are disabled, and
  590. * DTR is dropped if the hangup on close termio flag is on.
  591. */
  592. static void siccuart_shutdown(struct SICC_info *info)
  593. {
  594. unsigned long flags;
  595. if (!(info->flags & ASYNC_INITIALIZED))
  596. return;
  597. /* lock while shutting down port */
  598. spin_lock_irqsave(&info->state->sicc_lock,flags); /* Disable interrupts */
  599. /*
  600. * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
  601. * here so the queue might never be woken up
  602. */
  603. wake_up_interruptible(&info->delta_msr_wait);
  604. /*
  605. * disable all interrupts, disable the port
  606. */
  607. siccuart_disable_rx_interrupt(info);
  608. siccuart_disable_tx_interrupt(info);
  609. /*
  610. * Free the IRQ
  611. */
  612. free_irq(info->port->irqtx, info);
  613. free_irq(info->port->irqrx, info);
  614. if (info->xmit.buf) {
  615. unsigned long pg = (unsigned long) info->xmit.buf;
  616. info->xmit.buf = NULL;
  617. free_page(pg);
  618. }
  619. if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
  620. info->mctrl &= ~(TIOCM_DTR|TIOCM_RTS);
  621. info->port->set_mctrl(info->port, info->mctrl);
  622. /* kill off our tasklet */
  623. tasklet_kill(&info->tlet);
  624. if (info->tty)
  625. set_bit(TTY_IO_ERROR, &info->tty->flags);
  626. info->flags &= ~ASYNC_INITIALIZED;
  627. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  628. }
  629. static void siccuart_change_speed(struct SICC_info *info, struct termios *old_termios)
  630. {
  631. unsigned int lcr_h, baud, quot, cflag, old_rcr, old_tcr, bits;
  632. unsigned long flags;
  633. if (!info->tty || !info->tty->termios)
  634. return;
  635. cflag = info->tty->termios->c_cflag;
  636. pr_debug("siccuart_set_cflag(0x%x) called\n", cflag);
  637. /* byte size and parity */
  638. switch (cflag & CSIZE) {
  639. case CS7: lcr_h = _LCR_PE_DISABLE | _LCR_DB_7_BITS | _LCR_SB_1_BIT; bits = 9; break;
  640. default: lcr_h = _LCR_PE_DISABLE | _LCR_DB_8_BITS | _LCR_SB_1_BIT; bits = 10; break; // CS8
  641. }
  642. if (cflag & CSTOPB) {
  643. lcr_h |= _LCR_SB_2_BIT;
  644. bits ++;
  645. }
  646. if (cflag & PARENB) {
  647. lcr_h |= _LCR_PE_ENABLE;
  648. bits++;
  649. if (!(cflag & PARODD))
  650. lcr_h |= _LCR_PTY_ODD;
  651. else
  652. lcr_h |= _LCR_PTY_EVEN;
  653. }
  654. do {
  655. /* Determine divisor based on baud rate */
  656. baud = tty_get_baud_rate(info->tty);
  657. if (!baud)
  658. baud = 9600;
  659. {
  660. // here is ppc403SetBaud(com_port, baud);
  661. unsigned long divisor, clockSource, temp;
  662. /* Ensure CICCR[7] is 0 to select Internal Baud Clock */
  663. powerpcMtcic_cr((unsigned long)(powerpcMfcic_cr() & 0xFEFFFFFF));
  664. /* Determine Internal Baud Clock Frequency */
  665. /* powerpcMfclkgpcr() reads DCR 0x120 - the*/
  666. /* SCCR (Serial Clock Control Register) on Vesta */
  667. temp = powerpcMfclkgpcr();
  668. if(temp & 0x00000080) {
  669. clockSource = 324000000;
  670. }
  671. else {
  672. clockSource = 216000000;
  673. }
  674. clockSource = clockSource/(unsigned long)((temp&0x00FC0000)>>18);
  675. divisor = clockSource/(16*baud) - 1;
  676. /* divisor has only 12 bits of resolution */
  677. if(divisor>0x00000FFF){
  678. divisor=0x00000FFF;
  679. }
  680. quot = divisor;
  681. }
  682. if (baud == 38400 &&
  683. ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
  684. quot = info->state->custom_divisor;
  685. if (!quot && old_termios) {
  686. info->tty->termios->c_cflag &= ~CBAUD;
  687. info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
  688. old_termios = NULL;
  689. }
  690. } while (quot == 0 && old_termios);
  691. /* As a last resort, if the quotient is zero, default to 9600 bps */
  692. if (!quot)
  693. quot = (info->port->uartclk / (16 * 9600)) - 1;
  694. info->timeout = info->port->fifosize * HZ * bits / baud;
  695. info->timeout += HZ/50; /* Add .02 seconds of slop */
  696. if (cflag & CRTSCTS)
  697. info->flags |= ASYNC_CTS_FLOW;
  698. else
  699. info->flags &= ~ASYNC_CTS_FLOW;
  700. if (cflag & CLOCAL)
  701. info->flags &= ~ASYNC_CHECK_CD;
  702. else
  703. info->flags |= ASYNC_CHECK_CD;
  704. /*
  705. * Set up parity check flag
  706. */
  707. #define RELEVENT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  708. info->read_status_mask = _LSR_OE_MASK;
  709. if (I_INPCK(info->tty))
  710. info->read_status_mask |= _LSR_FE_MASK | _LSR_PE_MASK;
  711. if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
  712. info->read_status_mask |= _LSR_LB_MASK;
  713. /*
  714. * Characters to ignore
  715. */
  716. info->ignore_status_mask = 0;
  717. if (I_IGNPAR(info->tty))
  718. info->ignore_status_mask |= _LSR_FE_MASK | _LSR_PE_MASK;
  719. if (I_IGNBRK(info->tty)) {
  720. info->ignore_status_mask |= _LSR_LB_MASK;
  721. /*
  722. * If we're ignoring parity and break indicators,
  723. * ignore overruns to (for real raw support).
  724. */
  725. if (I_IGNPAR(info->tty))
  726. info->ignore_status_mask |= _LSR_OE_MASK;
  727. }
  728. /* disable interrupts while reading and clearing registers */
  729. spin_lock_irqsave(&info->state->sicc_lock,flags);
  730. old_rcr = readb(info->port->uart_base + BL_SICC_RCR);
  731. old_tcr = readb(info->port->uart_base + BL_SICC_TxCR);
  732. writeb(0, info->port->uart_base + BL_SICC_RCR);
  733. writeb(0, info->port->uart_base + BL_SICC_TxCR);
  734. /*RLBtrace (&ppc403Chan0, 0x2000000c, 0, 0);*/
  735. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  736. /* Set baud rate */
  737. writeb((quot & 0x00000F00)>>8, info->port->uart_base + BL_SICC_BRDH );
  738. writeb( quot & 0x00000FF, info->port->uart_base + BL_SICC_BRDL );
  739. /* Set CTL2 reg to use external clock (ExtClk) and enable FIFOs. */
  740. /* For now, do NOT use FIFOs since 403 UART did not have this */
  741. /* capability and this driver was inherited from 403UART. */
  742. writeb(_CTL2_EXTERN, info->port->uart_base + BL_SICC_CTL2);
  743. writeb(lcr_h, info->port->uart_base + BL_SICC_LCR);
  744. writeb(old_rcr, info->port->uart_base + BL_SICC_RCR); // restore rcr
  745. writeb(old_tcr, info->port->uart_base + BL_SICC_TxCR); // restore txcr
  746. }
  747. static void siccuart_put_char(struct tty_struct *tty, u_char ch)
  748. {
  749. struct SICC_info *info = tty->driver_data;
  750. unsigned long flags;
  751. if (!tty || !info->xmit.buf)
  752. return;
  753. /* lock info->xmit while adding character to tx buffer */
  754. spin_lock_irqsave(&info->state->sicc_lock,flags);
  755. if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SICC_XMIT_SIZE) != 0) {
  756. info->xmit.buf[info->xmit.head] = ch;
  757. info->xmit.head = (info->xmit.head + 1) & (SICC_XMIT_SIZE - 1);
  758. }
  759. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  760. }
  761. static void siccuart_flush_chars(struct tty_struct *tty)
  762. {
  763. struct SICC_info *info = tty->driver_data;
  764. unsigned long flags;
  765. if (info->xmit.head == info->xmit.tail
  766. || tty->stopped
  767. || tty->hw_stopped
  768. || !info->xmit.buf)
  769. return;
  770. /* disable interrupts while transmitting characters */
  771. spin_lock_irqsave(&info->state->sicc_lock,flags);
  772. siccuart_enable_tx_interrupt(info);
  773. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  774. }
  775. static int siccuart_write(struct tty_struct *tty,
  776. const u_char * buf, int count)
  777. {
  778. struct SICC_info *info = tty->driver_data;
  779. unsigned long flags;
  780. int c, ret = 0;
  781. if (!tty || !info->xmit.buf || !tmp_buf)
  782. return 0;
  783. /* lock info->xmit while removing characters from buffer */
  784. spin_lock_irqsave(&info->state->sicc_lock,flags);
  785. while (1) {
  786. c = CIRC_SPACE_TO_END(info->xmit.head,
  787. info->xmit.tail,
  788. SICC_XMIT_SIZE);
  789. if (count < c)
  790. c = count;
  791. if (c <= 0)
  792. break;
  793. memcpy(info->xmit.buf + info->xmit.head, buf, c);
  794. info->xmit.head = (info->xmit.head + c) &
  795. (SICC_XMIT_SIZE - 1);
  796. buf += c;
  797. count -= c;
  798. ret += c;
  799. }
  800. if (info->xmit.head != info->xmit.tail
  801. && !tty->stopped
  802. && !tty->hw_stopped)
  803. siccuart_enable_tx_interrupt(info);
  804. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  805. return ret;
  806. }
  807. static int siccuart_write_room(struct tty_struct *tty)
  808. {
  809. struct SICC_info *info = tty->driver_data;
  810. return CIRC_SPACE(info->xmit.head, info->xmit.tail, SICC_XMIT_SIZE);
  811. }
  812. static int siccuart_chars_in_buffer(struct tty_struct *tty)
  813. {
  814. struct SICC_info *info = tty->driver_data;
  815. return CIRC_CNT(info->xmit.head, info->xmit.tail, SICC_XMIT_SIZE);
  816. }
  817. static void siccuart_flush_buffer(struct tty_struct *tty)
  818. {
  819. struct SICC_info *info = tty->driver_data;
  820. unsigned long flags;
  821. pr_debug("siccuart_flush_buffer(%d) called\n", tty->index);
  822. /* lock info->xmit while zeroing buffer counts */
  823. spin_lock_irqsave(&info->state->sicc_lock,flags);
  824. info->xmit.head = info->xmit.tail = 0;
  825. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  826. wake_up_interruptible(&tty->write_wait);
  827. if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
  828. tty->ldisc.write_wakeup)
  829. (tty->ldisc.write_wakeup)(tty);
  830. }
  831. /*
  832. * This function is used to send a high-priority XON/XOFF character to
  833. * the device
  834. */
  835. static void siccuart_send_xchar(struct tty_struct *tty, char ch)
  836. {
  837. struct SICC_info *info = tty->driver_data;
  838. info->x_char = ch;
  839. if (ch)
  840. siccuart_enable_tx_interrupt(info);
  841. }
  842. static void siccuart_throttle(struct tty_struct *tty)
  843. {
  844. struct SICC_info *info = tty->driver_data;
  845. unsigned long flags;
  846. if (I_IXOFF(tty))
  847. siccuart_send_xchar(tty, STOP_CHAR(tty));
  848. if (tty->termios->c_cflag & CRTSCTS) {
  849. /* disable interrupts while setting modem control lines */
  850. spin_lock_irqsave(&info->state->sicc_lock,flags);
  851. info->mctrl &= ~TIOCM_RTS;
  852. info->port->set_mctrl(info->port, info->mctrl);
  853. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  854. }
  855. }
  856. static void siccuart_unthrottle(struct tty_struct *tty)
  857. {
  858. struct SICC_info *info = (struct SICC_info *) tty->driver_data;
  859. unsigned long flags;
  860. if (I_IXOFF(tty)) {
  861. if (info->x_char)
  862. info->x_char = 0;
  863. else
  864. siccuart_send_xchar(tty, START_CHAR(tty));
  865. }
  866. if (tty->termios->c_cflag & CRTSCTS) {
  867. /* disable interrupts while setting modem control lines */
  868. spin_lock_irqsave(&info->state->sicc_lock,flags);
  869. info->mctrl |= TIOCM_RTS;
  870. info->port->set_mctrl(info->port, info->mctrl);
  871. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  872. }
  873. }
  874. static int get_serial_info(struct SICC_info *info, struct serial_struct *retinfo)
  875. {
  876. struct SICC_state *state = info->state;
  877. struct SICC_port *port = info->port;
  878. struct serial_struct tmp;
  879. memset(&tmp, 0, sizeof(tmp));
  880. tmp.type = 0;
  881. tmp.line = state->line;
  882. tmp.port = port->uart_base;
  883. if (HIGH_BITS_OFFSET)
  884. tmp.port_high = port->uart_base >> HIGH_BITS_OFFSET;
  885. tmp.irq = port->irqrx;
  886. tmp.flags = 0;
  887. tmp.xmit_fifo_size = port->fifosize;
  888. tmp.baud_base = port->uartclk / 16;
  889. tmp.close_delay = state->close_delay;
  890. tmp.closing_wait = state->closing_wait;
  891. tmp.custom_divisor = state->custom_divisor;
  892. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  893. return -EFAULT;
  894. return 0;
  895. }
  896. static int set_serial_info(struct SICC_info *info,
  897. struct serial_struct *newinfo)
  898. {
  899. struct serial_struct new_serial;
  900. struct SICC_state *state, old_state;
  901. struct SICC_port *port;
  902. unsigned long new_port;
  903. unsigned int i, change_irq, change_port;
  904. int retval = 0;
  905. if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
  906. return -EFAULT;
  907. state = info->state;
  908. old_state = *state;
  909. port = info->port;
  910. new_port = new_serial.port;
  911. if (HIGH_BITS_OFFSET)
  912. new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
  913. change_irq = new_serial.irq != port->irqrx;
  914. change_port = new_port != port->uart_base;
  915. if (!capable(CAP_SYS_ADMIN)) {
  916. if (change_irq || change_port ||
  917. (new_serial.baud_base != port->uartclk / 16) ||
  918. (new_serial.close_delay != state->close_delay) ||
  919. (new_serial.xmit_fifo_size != port->fifosize) ||
  920. ((new_serial.flags & ~ASYNC_USR_MASK) !=
  921. (state->flags & ~ASYNC_USR_MASK)))
  922. return -EPERM;
  923. state->flags = ((state->flags & ~ASYNC_USR_MASK) |
  924. (new_serial.flags & ASYNC_USR_MASK));
  925. info->flags = ((info->flags & ~ASYNC_USR_MASK) |
  926. (new_serial.flags & ASYNC_USR_MASK));
  927. state->custom_divisor = new_serial.custom_divisor;
  928. goto check_and_exit;
  929. }
  930. if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
  931. (new_serial.baud_base < 9600))
  932. return -EINVAL;
  933. if (new_serial.type && change_port) {
  934. for (i = 0; i < SERIAL_SICC_NR; i++)
  935. if ((port != sicc_ports + i) &&
  936. sicc_ports[i].uart_base != new_port)
  937. return -EADDRINUSE;
  938. }
  939. if ((change_port || change_irq) && (state->count > 1))
  940. return -EBUSY;
  941. /*
  942. * OK, past this point, all the error checking has been done.
  943. * At this point, we start making changes.....
  944. */
  945. port->uartclk = new_serial.baud_base * 16;
  946. state->flags = ((state->flags & ~ASYNC_FLAGS) |
  947. (new_serial.flags & ASYNC_FLAGS));
  948. info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
  949. (info->flags & ASYNC_INTERNAL_FLAGS));
  950. state->custom_divisor = new_serial.custom_divisor;
  951. state->close_delay = msecs_to_jiffies(10 * new_serial.close_delay);
  952. state->closing_wait = msecs_to_jiffies(10 * new_serial.closing_wait);
  953. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  954. port->fifosize = new_serial.xmit_fifo_size;
  955. if (change_port || change_irq) {
  956. /*
  957. * We need to shutdown the serial port at the old
  958. * port/irq combination.
  959. */
  960. siccuart_shutdown(info);
  961. port->irqrx = new_serial.irq;
  962. port->uart_base = new_port;
  963. }
  964. check_and_exit:
  965. if (!port->uart_base)
  966. return 0;
  967. if (info->flags & ASYNC_INITIALIZED) {
  968. if ((old_state.flags & ASYNC_SPD_MASK) !=
  969. (state->flags & ASYNC_SPD_MASK) ||
  970. (old_state.custom_divisor != state->custom_divisor)) {
  971. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  972. info->tty->alt_speed = 57600;
  973. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  974. info->tty->alt_speed = 115200;
  975. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  976. info->tty->alt_speed = 230400;
  977. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  978. info->tty->alt_speed = 460800;
  979. siccuart_change_speed(info, NULL);
  980. }
  981. } else
  982. retval = siccuart_startup(info);
  983. return retval;
  984. }
  985. /*
  986. * get_lsr_info - get line status register info
  987. */
  988. static int get_lsr_info(struct SICC_info *info, unsigned int *value)
  989. {
  990. unsigned int result, status;
  991. unsigned long flags;
  992. /* disable interrupts while reading status from port */
  993. spin_lock_irqsave(&info->state->sicc_lock,flags);
  994. status = readb(info->port->uart_base + BL_SICC_LSR);
  995. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  996. result = status & _LSR_TSR_EMPTY ? TIOCSER_TEMT : 0;
  997. /*
  998. * If we're about to load something into the transmit
  999. * register, we'll pretend the transmitter isn't empty to
  1000. * avoid a race condition (depending on when the transmit
  1001. * interrupt happens).
  1002. */
  1003. if (info->x_char ||
  1004. ((CIRC_CNT(info->xmit.head, info->xmit.tail,
  1005. SICC_XMIT_SIZE) > 0) &&
  1006. !info->tty->stopped && !info->tty->hw_stopped))
  1007. result &= TIOCSER_TEMT;
  1008. return put_user(result, value);
  1009. }
  1010. static int get_modem_info(struct SICC_info *info, unsigned int *value)
  1011. {
  1012. unsigned int result = info->mctrl;
  1013. return put_user(result, value);
  1014. }
  1015. static int set_modem_info(struct SICC_info *info, unsigned int cmd,
  1016. unsigned int *value)
  1017. {
  1018. unsigned int arg, old;
  1019. unsigned long flags;
  1020. if (get_user(arg, value))
  1021. return -EFAULT;
  1022. old = info->mctrl;
  1023. switch (cmd) {
  1024. case TIOCMBIS:
  1025. info->mctrl |= arg;
  1026. break;
  1027. case TIOCMBIC:
  1028. info->mctrl &= ~arg;
  1029. break;
  1030. case TIOCMSET:
  1031. info->mctrl = arg;
  1032. break;
  1033. default:
  1034. return -EINVAL;
  1035. }
  1036. /* disable interrupts while setting modem control lines */
  1037. spin_lock_irqsave(&info->state->sicc_lock,flags);
  1038. if (old != info->mctrl)
  1039. info->port->set_mctrl(info->port, info->mctrl);
  1040. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  1041. return 0;
  1042. }
  1043. static void siccuart_break_ctl(struct tty_struct *tty, int break_state)
  1044. {
  1045. struct SICC_info *info = tty->driver_data;
  1046. unsigned long flags;
  1047. unsigned int lcr_h;
  1048. /* disable interrupts while setting break state */
  1049. spin_lock_irqsave(&info->state->sicc_lock,flags);
  1050. lcr_h = readb(info->port + BL_SICC_LSR);
  1051. if (break_state == -1)
  1052. lcr_h |= _LSR_LB_MASK;
  1053. else
  1054. lcr_h &= ~_LSR_LB_MASK;
  1055. writeb(lcr_h, info->port + BL_SICC_LSRS);
  1056. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  1057. }
  1058. static int siccuart_ioctl(struct tty_struct *tty, struct file *file,
  1059. unsigned int cmd, unsigned long arg)
  1060. {
  1061. struct SICC_info *info = tty->driver_data;
  1062. struct SICC_icount cnow;
  1063. struct serial_icounter_struct icount;
  1064. unsigned long flags;
  1065. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  1066. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  1067. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  1068. if (tty->flags & (1 << TTY_IO_ERROR))
  1069. return -EIO;
  1070. }
  1071. switch (cmd) {
  1072. case TIOCMGET:
  1073. return get_modem_info(info, (unsigned int *)arg);
  1074. case TIOCMBIS:
  1075. case TIOCMBIC:
  1076. case TIOCMSET:
  1077. return set_modem_info(info, cmd, (unsigned int *)arg);
  1078. case TIOCGSERIAL:
  1079. return get_serial_info(info,
  1080. (struct serial_struct *)arg);
  1081. case TIOCSSERIAL:
  1082. return set_serial_info(info,
  1083. (struct serial_struct *)arg);
  1084. case TIOCSERGETLSR: /* Get line status register */
  1085. return get_lsr_info(info, (unsigned int *)arg);
  1086. /*
  1087. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1088. * - mask passed in arg for lines of interest
  1089. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1090. * Caller should use TIOCGICOUNT to see which one it was
  1091. */
  1092. case TIOCMIWAIT:
  1093. return 0;
  1094. /*
  1095. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1096. * Return: write counters to the user passed counter struct
  1097. * NB: both 1->0 and 0->1 transitions are counted except for
  1098. * RI where only 0->1 is counted.
  1099. */
  1100. case TIOCGICOUNT:
  1101. /* disable interrupts while getting interrupt count */
  1102. spin_lock_irqsave(&info->state->sicc_lock,flags);
  1103. cnow = info->state->icount;
  1104. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  1105. icount.cts = cnow.cts;
  1106. icount.dsr = cnow.dsr;
  1107. icount.rng = cnow.rng;
  1108. icount.dcd = cnow.dcd;
  1109. icount.rx = cnow.rx;
  1110. icount.tx = cnow.tx;
  1111. icount.frame = cnow.frame;
  1112. icount.overrun = cnow.overrun;
  1113. icount.parity = cnow.parity;
  1114. icount.brk = cnow.brk;
  1115. icount.buf_overrun = cnow.buf_overrun;
  1116. return copy_to_user((void *)arg, &icount, sizeof(icount))
  1117. ? -EFAULT : 0;
  1118. default:
  1119. return -ENOIOCTLCMD;
  1120. }
  1121. return 0;
  1122. }
  1123. static void siccuart_set_termios(struct tty_struct *tty, struct termios *old_termios)
  1124. {
  1125. struct SICC_info *info = tty->driver_data;
  1126. unsigned long flags;
  1127. unsigned int cflag = tty->termios->c_cflag;
  1128. if ((cflag ^ old_termios->c_cflag) == 0 &&
  1129. RELEVENT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
  1130. return;
  1131. siccuart_change_speed(info, old_termios);
  1132. /* Handle transition to B0 status */
  1133. if ((old_termios->c_cflag & CBAUD) &&
  1134. !(cflag & CBAUD)) {
  1135. /* disable interrupts while setting break state */
  1136. spin_lock_irqsave(&info->state->sicc_lock,flags);
  1137. info->mctrl &= ~(TIOCM_RTS | TIOCM_DTR);
  1138. info->port->set_mctrl(info->port, info->mctrl);
  1139. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  1140. }
  1141. /* Handle transition away from B0 status */
  1142. if (!(old_termios->c_cflag & CBAUD) &&
  1143. (cflag & CBAUD)) {
  1144. /* disable interrupts while setting break state */
  1145. spin_lock_irqsave(&info->state->sicc_lock,flags);
  1146. info->mctrl |= TIOCM_DTR;
  1147. if (!(cflag & CRTSCTS) ||
  1148. !test_bit(TTY_THROTTLED, &tty->flags))
  1149. info->mctrl |= TIOCM_RTS;
  1150. info->port->set_mctrl(info->port, info->mctrl);
  1151. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  1152. }
  1153. /* Handle turning off CRTSCTS */
  1154. if ((old_termios->c_cflag & CRTSCTS) &&
  1155. !(cflag & CRTSCTS)) {
  1156. tty->hw_stopped = 0;
  1157. siccuart_start(tty);
  1158. }
  1159. #if 0
  1160. /*
  1161. * No need to wake up processes in open wait, since they
  1162. * sample the CLOCAL flag once, and don't recheck it.
  1163. * XXX It's not clear whether the current behavior is correct
  1164. * or not. Hence, this may change.....
  1165. */
  1166. if (!(old_termios->c_cflag & CLOCAL) &&
  1167. (tty->termios->c_cflag & CLOCAL))
  1168. wake_up_interruptible(&info->open_wait);
  1169. #endif
  1170. }
  1171. static void siccuart_close(struct tty_struct *tty, struct file *filp)
  1172. {
  1173. struct SICC_info *info = tty->driver_data;
  1174. struct SICC_state *state;
  1175. unsigned long flags;
  1176. if (!info)
  1177. return;
  1178. state = info->state;
  1179. //pr_debug("siccuart_close() called\n");
  1180. /* lock tty->driver_data while closing port */
  1181. spin_lock_irqsave(&info->state->sicc_lock,flags);
  1182. if (tty_hung_up_p(filp)) {
  1183. goto quick_close;
  1184. }
  1185. if ((tty->count == 1) && (state->count != 1)) {
  1186. /*
  1187. * Uh, oh. tty->count is 1, which means that the tty
  1188. * structure will be freed. state->count should always
  1189. * be one in these conditions. If it's greater than
  1190. * one, we've got real problems, since it means the
  1191. * serial port won't be shutdown.
  1192. */
  1193. printk("siccuart_close: bad serial port count; tty->count is 1, state->count is %d\n", state->count);
  1194. state->count = 1;
  1195. }
  1196. if (--state->count < 0) {
  1197. printk("rs_close: bad serial port count for %s: %d\n", tty->name, state->count);
  1198. state->count = 0;
  1199. }
  1200. if (state->count) {
  1201. goto quick_close;
  1202. }
  1203. info->flags |= ASYNC_CLOSING;
  1204. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  1205. /*
  1206. * Now we wait for the transmit buffer to clear; and we notify
  1207. * the line discipline to only process XON/XOFF characters.
  1208. */
  1209. tty->closing = 1;
  1210. if (info->state->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  1211. tty_wait_until_sent(tty, info->state->closing_wait);
  1212. /*
  1213. * At this point, we stop accepting input. To do this, we
  1214. * disable the receive line status interrupts.
  1215. */
  1216. if (info->flags & ASYNC_INITIALIZED) {
  1217. siccuart_disable_rx_interrupt(info);
  1218. /*
  1219. * Before we drop DTR, make sure the UART transmitter
  1220. * has completely drained; this is especially
  1221. * important if there is a transmit FIFO!
  1222. */
  1223. siccuart_wait_until_sent(tty, info->timeout);
  1224. }
  1225. siccuart_shutdown(info);
  1226. if (tty->driver->flush_buffer)
  1227. tty->driver->flush_buffer(tty);
  1228. if (tty->ldisc.flush_buffer)
  1229. tty->ldisc.flush_buffer(tty);
  1230. tty->closing = 0;
  1231. info->event = 0;
  1232. info->tty = NULL;
  1233. if (info->blocked_open) {
  1234. if (info->state->close_delay)
  1235. schedule_timeout_interruptible(info->state->close_delay);
  1236. wake_up_interruptible(&info->open_wait);
  1237. }
  1238. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  1239. wake_up_interruptible(&info->close_wait);
  1240. return;
  1241. quick_close:
  1242. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  1243. return;
  1244. }
  1245. static void siccuart_wait_until_sent(struct tty_struct *tty, int timeout)
  1246. {
  1247. struct SICC_info *info = (struct SICC_info *) tty->driver_data;
  1248. unsigned long char_time, expire;
  1249. if (info->port->fifosize == 0)
  1250. return;
  1251. /*
  1252. * Set the check interval to be 1/5 of the estimated time to
  1253. * send a single character, and make it at least 1. The check
  1254. * interval should also be less than the timeout.
  1255. *
  1256. * Note: we have to use pretty tight timings here to satisfy
  1257. * the NIST-PCTS.
  1258. */
  1259. char_time = (info->timeout - msecs_to_jiffies(20)) / info->port->fifosize;
  1260. char_time = char_time / 5;
  1261. if (char_time == 0)
  1262. char_time = 1;
  1263. // Crazy!! sometimes the input arg 'timeout' can be negtive numbers :-(
  1264. if (timeout >= 0 && timeout < char_time)
  1265. char_time = timeout;
  1266. /*
  1267. * If the transmitter hasn't cleared in twice the approximate
  1268. * amount of time to send the entire FIFO, it probably won't
  1269. * ever clear. This assumes the UART isn't doing flow
  1270. * control, which is currently the case. Hence, if it ever
  1271. * takes longer than info->timeout, this is probably due to a
  1272. * UART bug of some kind. So, we clamp the timeout parameter at
  1273. * 2*info->timeout.
  1274. */
  1275. if (!timeout || timeout > 2 * info->timeout)
  1276. timeout = 2 * info->timeout;
  1277. expire = jiffies + timeout;
  1278. pr_debug("siccuart_wait_until_sent(%d), jiff=%lu, expire=%lu char_time=%lu...\n",
  1279. tty->index, jiffies,
  1280. expire, char_time);
  1281. while ((readb(info->port->uart_base + BL_SICC_LSR) & _LSR_TX_ALL) != _LSR_TX_ALL) {
  1282. schedule_timeout_interruptible(char_time);
  1283. if (signal_pending(current))
  1284. break;
  1285. if (timeout && time_after(jiffies, expire))
  1286. break;
  1287. }
  1288. set_current_state(TASK_RUNNING);
  1289. }
  1290. static void siccuart_hangup(struct tty_struct *tty)
  1291. {
  1292. struct SICC_info *info = tty->driver_data;
  1293. struct SICC_state *state = info->state;
  1294. siccuart_flush_buffer(tty);
  1295. if (info->flags & ASYNC_CLOSING)
  1296. return;
  1297. siccuart_shutdown(info);
  1298. info->event = 0;
  1299. state->count = 0;
  1300. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  1301. info->tty = NULL;
  1302. wake_up_interruptible(&info->open_wait);
  1303. }
  1304. static int block_til_ready(struct tty_struct *tty, struct file *filp,
  1305. struct SICC_info *info)
  1306. {
  1307. DECLARE_WAITQUEUE(wait, current);
  1308. struct SICC_state *state = info->state;
  1309. unsigned long flags;
  1310. int do_clocal = 0, extra_count = 0, retval;
  1311. /*
  1312. * If the device is in the middle of being closed, then block
  1313. * until it's done, and then try again.
  1314. */
  1315. if (tty_hung_up_p(filp) ||
  1316. (info->flags & ASYNC_CLOSING)) {
  1317. if (info->flags & ASYNC_CLOSING)
  1318. interruptible_sleep_on(&info->close_wait);
  1319. return (info->flags & ASYNC_HUP_NOTIFY) ?
  1320. -EAGAIN : -ERESTARTSYS;
  1321. }
  1322. /*
  1323. * If non-blocking mode is set, or the port is not enabled,
  1324. * then make the check up front and then exit.
  1325. */
  1326. if ((filp->f_flags & O_NONBLOCK) ||
  1327. (tty->flags & (1 << TTY_IO_ERROR))) {
  1328. info->flags |= ASYNC_NORMAL_ACTIVE;
  1329. return 0;
  1330. }
  1331. if (tty->termios->c_cflag & CLOCAL)
  1332. do_clocal = 1;
  1333. /*
  1334. * Block waiting for the carrier detect and the line to become
  1335. * free (i.e., not in use by the callout). While we are in
  1336. * this loop, state->count is dropped by one, so that
  1337. * rs_close() knows when to free things. We restore it upon
  1338. * exit, either normal or abnormal.
  1339. */
  1340. retval = 0;
  1341. add_wait_queue(&info->open_wait, &wait);
  1342. /* lock while decrementing state->count */
  1343. spin_lock_irqsave(&info->state->sicc_lock,flags);
  1344. if (!tty_hung_up_p(filp)) {
  1345. extra_count = 1;
  1346. state->count--;
  1347. }
  1348. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  1349. info->blocked_open++;
  1350. while (1) {
  1351. /* disable interrupts while setting modem control lines */
  1352. spin_lock_irqsave(&info->state->sicc_lock,flags);
  1353. if (tty->termios->c_cflag & CBAUD) {
  1354. info->mctrl = TIOCM_DTR | TIOCM_RTS;
  1355. info->port->set_mctrl(info->port, info->mctrl);
  1356. }
  1357. spin_unlock_irqrestore(&info->state->sicc_lock,flags);
  1358. set_current_state(TASK_INTERRUPTIBLE);
  1359. if (tty_hung_up_p(filp) ||
  1360. !(info->flags & ASYNC_INITIALIZED)) {
  1361. if (info->flags & ASYNC_HUP_NOTIFY)
  1362. retval = -EAGAIN;
  1363. else
  1364. retval = -ERESTARTSYS;
  1365. break;
  1366. }
  1367. if (!(info->flags & ASYNC_CLOSING) &&
  1368. (do_clocal /*|| (UART_GET_FR(info->port) & SICC_UARTFR_DCD)*/))
  1369. break;
  1370. if (signal_pending(current)) {
  1371. retval = -ERESTARTSYS;
  1372. break;
  1373. }
  1374. schedule();
  1375. }
  1376. set_current_state(TASK_RUNNING);
  1377. remove_wait_queue(&info->open_wait, &wait);
  1378. if (extra_count)
  1379. state->count++;
  1380. info->blocked_open--;
  1381. if (retval)
  1382. return retval;
  1383. info->flags |= ASYNC_NORMAL_ACTIVE;
  1384. return 0;
  1385. }
  1386. static struct SICC_info *siccuart_get(int line)
  1387. {
  1388. struct SICC_info *info;
  1389. struct SICC_state *state = sicc_state + line;
  1390. state->count++;
  1391. if (state->info)
  1392. return state->info;
  1393. info = kmalloc(sizeof(struct SICC_info), GFP_KERNEL);
  1394. if (info) {
  1395. memset(info, 0, sizeof(struct SICC_info));
  1396. init_waitqueue_head(&info->open_wait);
  1397. init_waitqueue_head(&info->close_wait);
  1398. init_waitqueue_head(&info->delta_msr_wait);
  1399. info->flags = state->flags;
  1400. info->state = state;
  1401. info->port = sicc_ports + line;
  1402. tasklet_init(&info->tlet, siccuart_tasklet_action,
  1403. (unsigned long)info);
  1404. }
  1405. if (state->info) {
  1406. kfree(info);
  1407. return state->info;
  1408. }
  1409. state->info = info;
  1410. return info;
  1411. }
  1412. static int siccuart_open(struct tty_struct *tty, struct file *filp)
  1413. {
  1414. struct SICC_info *info;
  1415. int retval, line = tty->index;
  1416. // is this a line that we've got?
  1417. if (line >= SERIAL_SICC_NR) {
  1418. return -ENODEV;
  1419. }
  1420. info = siccuart_get(line);
  1421. if (!info)
  1422. return -ENOMEM;
  1423. tty->driver_data = info;
  1424. info->tty = tty;
  1425. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  1426. /*
  1427. * Make sure we have the temporary buffer allocated
  1428. */
  1429. if (!tmp_buf) {
  1430. unsigned long page = get_zeroed_page(GFP_KERNEL);
  1431. if (tmp_buf)
  1432. free_page(page);
  1433. else if (!page) {
  1434. return -ENOMEM;
  1435. }
  1436. tmp_buf = (u_char *)page;
  1437. }
  1438. /*
  1439. * If the port is in the middle of closing, bail out now.
  1440. */
  1441. if (tty_hung_up_p(filp) ||
  1442. (info->flags & ASYNC_CLOSING)) {
  1443. if (info->flags & ASYNC_CLOSING)
  1444. interruptible_sleep_on(&info->close_wait);
  1445. return -EAGAIN;
  1446. }
  1447. /*
  1448. * Start up the serial port
  1449. */
  1450. retval = siccuart_startup(info);
  1451. if (retval) {
  1452. return retval;
  1453. }
  1454. retval = block_til_ready(tty, filp, info);
  1455. if (retval) {
  1456. return retval;
  1457. }
  1458. #ifdef CONFIG_SERIAL_SICC_CONSOLE
  1459. if (siccuart_cons.cflag && siccuart_cons.index == line) {
  1460. tty->termios->c_cflag = siccuart_cons.cflag;
  1461. siccuart_cons.cflag = 0;
  1462. siccuart_change_speed(info, NULL);
  1463. }
  1464. #endif
  1465. return 0;
  1466. }
  1467. static struct tty_operations sicc_ops = {
  1468. .open = siccuart_open,
  1469. .close = siccuart_close,
  1470. .write = siccuart_write,
  1471. .put_char = siccuart_put_char,
  1472. .flush_chars = siccuart_flush_chars,
  1473. .write_room = siccuart_write_room,
  1474. .chars_in_buffer = siccuart_chars_in_buffer,
  1475. .flush_buffer = siccuart_flush_buffer,
  1476. .ioctl = siccuart_ioctl,
  1477. .throttle = siccuart_throttle,
  1478. .unthrottle = siccuart_unthrottle,
  1479. .send_xchar = siccuart_send_xchar,
  1480. .set_termios = siccuart_set_termios,
  1481. .stop = siccuart_stop,
  1482. .start = siccuart_start,
  1483. .hangup = siccuart_hangup,
  1484. .break_ctl = siccuart_break_ctl,
  1485. .wait_until_sent = siccuart_wait_until_sent,
  1486. };
  1487. int __init siccuart_init(void)
  1488. {
  1489. int i;
  1490. siccnormal_driver = alloc_tty_driver(SERIAL_SICC_NR);
  1491. if (!siccnormal_driver)
  1492. return -ENOMEM;
  1493. printk("IBM Vesta SICC serial port driver V 0.1 by Yudong Yang and Yi Ge / IBM CRL .\n");
  1494. siccnormal_driver->driver_name = "serial_sicc";
  1495. siccnormal_driver->owner = THIS_MODULE;
  1496. siccnormal_driver->name = SERIAL_SICC_NAME;
  1497. siccnormal_driver->major = SERIAL_SICC_MAJOR;
  1498. siccnormal_driver->minor_start = SERIAL_SICC_MINOR;
  1499. siccnormal_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1500. siccnormal_driver->subtype = SERIAL_TYPE_NORMAL;
  1501. siccnormal_driver->init_termios = tty_std_termios;
  1502. siccnormal_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1503. siccnormal_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
  1504. tty_set_operations(siccnormal_driver, &sicc_ops);
  1505. if (tty_register_driver(siccnormal_driver))
  1506. panic("Couldn't register SICC serial driver\n");
  1507. for (i = 0; i < SERIAL_SICC_NR; i++) {
  1508. struct SICC_state *state = sicc_state + i;
  1509. state->line = i;
  1510. state->close_delay = msecs_to_jiffies(500);
  1511. state->closing_wait = 30 * HZ;
  1512. spin_lock_init(&state->sicc_lock);
  1513. }
  1514. return 0;
  1515. }
  1516. __initcall(siccuart_init);
  1517. #ifdef CONFIG_SERIAL_SICC_CONSOLE
  1518. /************** console driver *****************/
  1519. /*
  1520. * This code is currently never used; console->read is never called.
  1521. * Therefore, although we have an implementation, we don't use it.
  1522. * FIXME: the "const char *s" should be fixed to "char *s" some day.
  1523. * (when the definition in include/linux/console.h is also fixed)
  1524. */
  1525. #ifdef used_and_not_const_char_pointer
  1526. static int siccuart_console_read(struct console *co, const char *s, u_int count)
  1527. {
  1528. struct SICC_port *port = &sicc_ports[co->index];
  1529. unsigned int status;
  1530. char *w;
  1531. int c;
  1532. pr_debug("siccuart_console_read() called\n");
  1533. c = 0;
  1534. w = s;
  1535. while (c < count) {
  1536. if(readb(port->uart_base + BL_SICC_LSR) & _LSR_RBR_FULL) {
  1537. *w++ = readb(port->uart_base + BL_SICC_RBR);
  1538. c++;
  1539. } else {
  1540. // nothing more to get, return
  1541. return c;
  1542. }
  1543. }
  1544. // return the count
  1545. return c;
  1546. }
  1547. #endif
  1548. /*
  1549. * Print a string to the serial port trying not to disturb
  1550. * any possible real use of the port...
  1551. *
  1552. * The console_lock must be held when we get here.
  1553. */
  1554. static void siccuart_console_write(struct console *co, const char *s, u_int count)
  1555. {
  1556. struct SICC_port *port = &sicc_ports[co->index];
  1557. unsigned int old_cr;
  1558. int i;
  1559. /*
  1560. * First save the CR then disable the interrupts
  1561. */
  1562. old_cr = readb(port->uart_base + BL_SICC_TxCR);
  1563. writeb(old_cr & ~_TxCR_DME_MASK, port->uart_base + BL_SICC_TxCR);
  1564. /*
  1565. * Now, do each character
  1566. */
  1567. for (i = 0; i < count; i++) {
  1568. while ((readb(port->uart_base + BL_SICC_LSR)&_LSR_TX_ALL) != _LSR_TX_ALL);
  1569. writeb(s[i], port->uart_base + BL_SICC_TBR);
  1570. if (s[i] == '\n') {
  1571. while ((readb(port->uart_base + BL_SICC_LSR)&_LSR_TX_ALL) != _LSR_TX_ALL);
  1572. writeb('\r', port->uart_base + BL_SICC_TBR);
  1573. }
  1574. }
  1575. /*
  1576. * Finally, wait for transmitter to become empty
  1577. * and restore the TCR
  1578. */
  1579. while ((readb(port->uart_base + BL_SICC_LSR)&_LSR_TX_ALL) != _LSR_TX_ALL);
  1580. writeb(old_cr, port->uart_base + BL_SICC_TxCR);
  1581. }
  1582. /*
  1583. * Receive character from the serial port
  1584. */
  1585. static int siccuart_console_wait_key(struct console *co)
  1586. {
  1587. struct SICC_port *port = &sicc_ports[co->index];
  1588. int c;
  1589. while(!(readb(port->uart_base + BL_SICC_LSR) & _LSR_RBR_FULL));
  1590. c = readb(port->uart_base + BL_SICC_RBR);
  1591. return c;
  1592. }
  1593. static struct tty_driver *siccuart_console_device(struct console *c, int *index)
  1594. {
  1595. *index = c->index;
  1596. return siccnormal_driver;
  1597. }
  1598. static int __init siccuart_console_setup(struct console *co, char *options)
  1599. {
  1600. struct SICC_port *port;
  1601. int baud = 9600;
  1602. int bits = 8;
  1603. int parity = 'n';
  1604. u_int cflag = CREAD | HUPCL | CLOCAL;
  1605. u_int lcr_h, quot;
  1606. if (co->index >= SERIAL_SICC_NR)
  1607. co->index = 0;
  1608. port = &sicc_ports[co->index];
  1609. if (port->uart_base == 0)
  1610. port->uart_base = (int)ioremap(port->uart_base_phys, PAGE_SIZE);
  1611. if (options) {
  1612. char *s = options;
  1613. baud = simple_strtoul(s, NULL, 10);
  1614. while (*s >= '0' && *s <= '9')
  1615. s++;
  1616. if (*s) parity = *s++;
  1617. if (*s) bits = *s - '0';
  1618. }
  1619. /*
  1620. * Now construct a cflag setting.
  1621. */
  1622. switch (baud) {
  1623. case 1200: cflag |= B1200; break;
  1624. case 2400: cflag |= B2400; break;
  1625. case 4800: cflag |= B4800; break;
  1626. default: cflag |= B9600; baud = 9600; break;
  1627. case 19200: cflag |= B19200; break;
  1628. case 38400: cflag |= B38400; break;
  1629. case 57600: cflag |= B57600; break;
  1630. case 115200: cflag |= B115200; break;
  1631. }
  1632. switch (bits) {
  1633. case 7: cflag |= CS7; lcr_h = _LCR_PE_DISABLE | _LCR_DB_7_BITS | _LCR_SB_1_BIT; break;
  1634. default: cflag |= CS8; lcr_h = _LCR_PE_DISABLE | _LCR_DB_8_BITS | _LCR_SB_1_BIT; break;
  1635. }
  1636. switch (parity) {
  1637. case 'o':
  1638. case 'O': cflag |= PARODD; lcr_h |= _LCR_PTY_ODD; break;
  1639. case 'e':
  1640. case 'E': cflag |= PARENB; lcr_h |= _LCR_PE_ENABLE | _LCR_PTY_ODD; break;
  1641. }
  1642. co->cflag = cflag;
  1643. {
  1644. // a copy of is inserted here ppc403SetBaud(com_port, (int)9600);
  1645. unsigned long divisor, clockSource, temp;
  1646. unsigned int rate = baud;
  1647. /* Ensure CICCR[7] is 0 to select Internal Baud Clock */
  1648. powerpcMtcic_cr((unsigned long)(powerpcMfcic_cr() & 0xFEFFFFFF));
  1649. /* Determine Internal Baud Clock Frequency */
  1650. /* powerpcMfclkgpcr() reads DCR 0x120 - the*/
  1651. /* SCCR (Serial Clock Control Register) on Vesta */
  1652. temp = powerpcMfclkgpcr();
  1653. if(temp & 0x00000080) {
  1654. clockSource = 324000000;
  1655. }
  1656. else {
  1657. clockSource = 216000000;
  1658. }
  1659. clockSource = clockSource/(unsigned long)((temp&0x00FC0000)>>18);
  1660. divisor = clockSource/(16*rate) - 1;
  1661. /* divisor has only 12 bits of resolution */
  1662. if(divisor>0x00000FFF){
  1663. divisor=0x00000FFF;
  1664. }
  1665. quot = divisor;
  1666. }
  1667. writeb((quot & 0x00000F00)>>8, port->uart_base + BL_SICC_BRDH );
  1668. writeb( quot & 0x00000FF, port->uart_base + BL_SICC_BRDL );
  1669. /* Set CTL2 reg to use external clock (ExtClk) and enable FIFOs. */
  1670. /* For now, do NOT use FIFOs since 403 UART did not have this */
  1671. /* capability and this driver was inherited from 403UART. */
  1672. writeb(_CTL2_EXTERN, port->uart_base + BL_SICC_CTL2);
  1673. writeb(lcr_h, port->uart_base + BL_SICC_LCR);
  1674. writeb(_RCR_ER_ENABLE | _RCR_PME_HARD, port->uart_base + BL_SICC_RCR);
  1675. writeb( _TxCR_ET_ENABLE , port->uart_base + BL_SICC_TxCR);
  1676. // writeb(, info->port->uart_base + BL_SICC_RCR );
  1677. /*
  1678. * Transmitter Command Register: Transmitter enabled & DMA + TBR interrupt
  1679. * + Transmitter Empty interrupt + Transmitter error interrupt disabled &
  1680. * Stop mode when CTS active enabled & Transmit Break + Pattern Generation
  1681. * mode disabled.
  1682. */
  1683. writeb( 0x00, port->uart_base + BL_SICC_IrCR ); // disable IrDA
  1684. readb(port->uart_base + BL_SICC_RBR);
  1685. writeb(0xf8, port->uart_base + BL_SICC_LSR); /* reset bits 0-4 of LSR */
  1686. /* we will enable the port as we need it */
  1687. return 0;
  1688. }
  1689. static struct console siccuart_cons =
  1690. {
  1691. .name = SERIAL_SICC_NAME,
  1692. .write = siccuart_console_write,
  1693. #ifdef used_and_not_const_char_pointer
  1694. .read = siccuart_console_read,
  1695. #endif
  1696. .device = siccuart_console_device,
  1697. .wait_key = siccuart_console_wait_key,
  1698. .setup = siccuart_console_setup,
  1699. .flags = CON_PRINTBUFFER,
  1700. .index = -1,
  1701. };
  1702. void __init sicc_console_init(void)
  1703. {
  1704. register_console(&siccuart_cons);
  1705. }
  1706. #endif /* CONFIG_SERIAL_SICC_CONSOLE */