serial_sicc.c 61 KB

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