serial_sicc.c 61 KB

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