serial_sicc.c 61 KB

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