serial.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*------------------------------------------------------------------------------+ */
  24. /*
  25. * This source code has been made available to you by IBM on an AS-IS
  26. * basis. Anyone receiving this source is licensed under IBM
  27. * copyrights to use it in any way he or she deems fit, including
  28. * copying it, modifying it, compiling it, and redistributing it either
  29. * with or without modifications. No license under IBM patents or
  30. * patent applications is to be implied by the copyright license.
  31. *
  32. * Any user of this software should understand that IBM cannot provide
  33. * technical support for this software and will not be responsible for
  34. * any consequences resulting from the use of this software.
  35. *
  36. * Any person who transfers this source code or any derivative work
  37. * must include the IBM copyright notice, this paragraph, and the
  38. * preceding two paragraphs in the transferred software.
  39. *
  40. * COPYRIGHT I B M CORPORATION 1995
  41. * LICENSED MATERIAL - PROGRAM PROPERTY OF I B M
  42. */
  43. /*------------------------------------------------------------------------------- */
  44. #include <common.h>
  45. #include <commproc.h>
  46. #include <asm/processor.h>
  47. #include <watchdog.h>
  48. #include "vecnum.h"
  49. #if CONFIG_SERIAL_SOFTWARE_FIFO
  50. #include <malloc.h>
  51. #endif
  52. /*****************************************************************************/
  53. #ifdef CONFIG_IOP480
  54. #define SPU_BASE 0x40000000
  55. #define spu_LineStat_rc 0x00 /* Line Status Register (Read/Clear) */
  56. #define spu_LineStat_w 0x04 /* Line Status Register (Set) */
  57. #define spu_Handshk_rc 0x08 /* Handshake Status Register (Read/Clear) */
  58. #define spu_Handshk_w 0x0c /* Handshake Status Register (Set) */
  59. #define spu_BRateDivh 0x10 /* Baud rate divisor high */
  60. #define spu_BRateDivl 0x14 /* Baud rate divisor low */
  61. #define spu_CtlReg 0x18 /* Control Register */
  62. #define spu_RxCmd 0x1c /* Rx Command Register */
  63. #define spu_TxCmd 0x20 /* Tx Command Register */
  64. #define spu_RxBuff 0x24 /* Rx data buffer */
  65. #define spu_TxBuff 0x24 /* Tx data buffer */
  66. /*-----------------------------------------------------------------------------+
  67. | Line Status Register.
  68. +-----------------------------------------------------------------------------*/
  69. #define asyncLSRport1 0x40000000
  70. #define asyncLSRport1set 0x40000004
  71. #define asyncLSRDataReady 0x80
  72. #define asyncLSRFramingError 0x40
  73. #define asyncLSROverrunError 0x20
  74. #define asyncLSRParityError 0x10
  75. #define asyncLSRBreakInterrupt 0x08
  76. #define asyncLSRTxHoldEmpty 0x04
  77. #define asyncLSRTxShiftEmpty 0x02
  78. /*-----------------------------------------------------------------------------+
  79. | Handshake Status Register.
  80. +-----------------------------------------------------------------------------*/
  81. #define asyncHSRport1 0x40000008
  82. #define asyncHSRport1set 0x4000000c
  83. #define asyncHSRDsr 0x80
  84. #define asyncLSRCts 0x40
  85. /*-----------------------------------------------------------------------------+
  86. | Control Register.
  87. +-----------------------------------------------------------------------------*/
  88. #define asyncCRport1 0x40000018
  89. #define asyncCRNormal 0x00
  90. #define asyncCRLoopback 0x40
  91. #define asyncCRAutoEcho 0x80
  92. #define asyncCRDtr 0x20
  93. #define asyncCRRts 0x10
  94. #define asyncCRWordLength7 0x00
  95. #define asyncCRWordLength8 0x08
  96. #define asyncCRParityDisable 0x00
  97. #define asyncCRParityEnable 0x04
  98. #define asyncCREvenParity 0x00
  99. #define asyncCROddParity 0x02
  100. #define asyncCRStopBitsOne 0x00
  101. #define asyncCRStopBitsTwo 0x01
  102. #define asyncCRDisableDtrRts 0x00
  103. /*-----------------------------------------------------------------------------+
  104. | Receiver Command Register.
  105. +-----------------------------------------------------------------------------*/
  106. #define asyncRCRport1 0x4000001c
  107. #define asyncRCRDisable 0x00
  108. #define asyncRCREnable 0x80
  109. #define asyncRCRIntDisable 0x00
  110. #define asyncRCRIntEnabled 0x20
  111. #define asyncRCRDMACh2 0x40
  112. #define asyncRCRDMACh3 0x60
  113. #define asyncRCRErrorInt 0x10
  114. #define asyncRCRPauseEnable 0x08
  115. /*-----------------------------------------------------------------------------+
  116. | Transmitter Command Register.
  117. +-----------------------------------------------------------------------------*/
  118. #define asyncTCRport1 0x40000020
  119. #define asyncTCRDisable 0x00
  120. #define asyncTCREnable 0x80
  121. #define asyncTCRIntDisable 0x00
  122. #define asyncTCRIntEnabled 0x20
  123. #define asyncTCRDMACh2 0x40
  124. #define asyncTCRDMACh3 0x60
  125. #define asyncTCRTxEmpty 0x10
  126. #define asyncTCRErrorInt 0x08
  127. #define asyncTCRStopPause 0x04
  128. #define asyncTCRBreakGen 0x02
  129. /*-----------------------------------------------------------------------------+
  130. | Miscellanies defines.
  131. +-----------------------------------------------------------------------------*/
  132. #define asyncTxBufferport1 0x40000024
  133. #define asyncRxBufferport1 0x40000024
  134. #define asyncDLABLsbport1 0x40000014
  135. #define asyncDLABMsbport1 0x40000010
  136. #define asyncXOFFchar 0x13
  137. #define asyncXONchar 0x11
  138. /*
  139. * Minimal serial functions needed to use one of the SMC ports
  140. * as serial console interface.
  141. */
  142. int serial_init (void)
  143. {
  144. DECLARE_GLOBAL_DATA_PTR;
  145. volatile char val;
  146. unsigned short br_reg;
  147. br_reg = ((((CONFIG_CPUCLOCK * 1000000) / 16) / gd->baudrate) - 1);
  148. /*
  149. * Init onboard UART
  150. */
  151. out8 (SPU_BASE + spu_LineStat_rc, 0x78); /* Clear all bits in Line Status Reg */
  152. out8 (SPU_BASE + spu_BRateDivl, (br_reg & 0x00ff)); /* Set baud rate divisor... */
  153. out8 (SPU_BASE + spu_BRateDivh, ((br_reg & 0xff00) >> 8)); /* ... */
  154. out8 (SPU_BASE + spu_CtlReg, 0x08); /* Set 8 bits, no parity and 1 stop bit */
  155. out8 (SPU_BASE + spu_RxCmd, 0xb0); /* Enable Rx */
  156. out8 (SPU_BASE + spu_TxCmd, 0x9c); /* Enable Tx */
  157. out8 (SPU_BASE + spu_Handshk_rc, 0xff); /* Clear Handshake */
  158. val = in8 (SPU_BASE + spu_RxBuff); /* Dummy read, to clear receiver */
  159. return (0);
  160. }
  161. void serial_setbrg (void)
  162. {
  163. DECLARE_GLOBAL_DATA_PTR;
  164. unsigned short br_reg;
  165. br_reg = ((((CONFIG_CPUCLOCK * 1000000) / 16) / gd->baudrate) - 1);
  166. out8 (SPU_BASE + spu_BRateDivl, (br_reg & 0x00ff)); /* Set baud rate divisor... */
  167. out8 (SPU_BASE + spu_BRateDivh, ((br_reg & 0xff00) >> 8)); /* ... */
  168. }
  169. void serial_putc (const char c)
  170. {
  171. if (c == '\n')
  172. serial_putc ('\r');
  173. /* load status from handshake register */
  174. if (in8 (SPU_BASE + spu_Handshk_rc) != 00)
  175. out8 (SPU_BASE + spu_Handshk_rc, 0xff); /* Clear Handshake */
  176. out8 (SPU_BASE + spu_TxBuff, c); /* Put char */
  177. while ((in8 (SPU_BASE + spu_LineStat_rc) & 04) != 04) {
  178. if (in8 (SPU_BASE + spu_Handshk_rc) != 00)
  179. out8 (SPU_BASE + spu_Handshk_rc, 0xff); /* Clear Handshake */
  180. }
  181. }
  182. void serial_puts (const char *s)
  183. {
  184. while (*s) {
  185. serial_putc (*s++);
  186. }
  187. }
  188. int serial_getc ()
  189. {
  190. unsigned char status = 0;
  191. while (1) {
  192. status = in8 (asyncLSRport1);
  193. if ((status & asyncLSRDataReady) != 0x0) {
  194. break;
  195. }
  196. if ((status & ( asyncLSRFramingError |
  197. asyncLSROverrunError |
  198. asyncLSRParityError |
  199. asyncLSRBreakInterrupt )) != 0) {
  200. (void) out8 (asyncLSRport1,
  201. asyncLSRFramingError |
  202. asyncLSROverrunError |
  203. asyncLSRParityError |
  204. asyncLSRBreakInterrupt );
  205. }
  206. }
  207. return (0x000000ff & (int) in8 (asyncRxBufferport1));
  208. }
  209. int serial_tstc ()
  210. {
  211. unsigned char status;
  212. status = in8 (asyncLSRport1);
  213. if ((status & asyncLSRDataReady) != 0x0) {
  214. return (1);
  215. }
  216. if ((status & ( asyncLSRFramingError |
  217. asyncLSROverrunError |
  218. asyncLSRParityError |
  219. asyncLSRBreakInterrupt )) != 0) {
  220. (void) out8 (asyncLSRport1,
  221. asyncLSRFramingError |
  222. asyncLSROverrunError |
  223. asyncLSRParityError |
  224. asyncLSRBreakInterrupt);
  225. }
  226. return 0;
  227. }
  228. #endif /* CONFIG_IOP480 */
  229. /*****************************************************************************/
  230. #if defined(CONFIG_405GP) || defined(CONFIG_405CR) || defined(CONFIG_440)
  231. #if defined(CONFIG_440)
  232. #define UART0_BASE CFG_PERIPHERAL_BASE + 0x00000200
  233. #define UART1_BASE CFG_PERIPHERAL_BASE + 0x00000300
  234. #define CR0_MASK 0x3fff0000
  235. #define CR0_EXTCLK_ENA 0x00600000
  236. #define CR0_UDIV_POS 16
  237. #else
  238. #define UART_BASE_PTR 0xF800FFFC; /* pointer to uart base */
  239. #define UART0_BASE 0xef600300
  240. #define UART1_BASE 0xef600400
  241. #define CR0_MASK 0x00001fff
  242. #define CR0_EXTCLK_ENA 0x00000c00
  243. #define CR0_UDIV_POS 1
  244. #endif
  245. #define UART_RBR 0x00
  246. #define UART_THR 0x00
  247. #define UART_IER 0x01
  248. #define UART_IIR 0x02
  249. #define UART_FCR 0x02
  250. #define UART_LCR 0x03
  251. #define UART_MCR 0x04
  252. #define UART_LSR 0x05
  253. #define UART_MSR 0x06
  254. #define UART_SCR 0x07
  255. #define UART_DLL 0x00
  256. #define UART_DLM 0x01
  257. /*-----------------------------------------------------------------------------+
  258. | Line Status Register.
  259. +-----------------------------------------------------------------------------*/
  260. /*#define asyncLSRport1 UART0_BASE+0x05 */
  261. #define asyncLSRDataReady1 0x01
  262. #define asyncLSROverrunError1 0x02
  263. #define asyncLSRParityError1 0x04
  264. #define asyncLSRFramingError1 0x08
  265. #define asyncLSRBreakInterrupt1 0x10
  266. #define asyncLSRTxHoldEmpty1 0x20
  267. #define asyncLSRTxShiftEmpty1 0x40
  268. #define asyncLSRRxFifoError1 0x80
  269. /*-----------------------------------------------------------------------------+
  270. | Miscellanies defines.
  271. +-----------------------------------------------------------------------------*/
  272. /*#define asyncTxBufferport1 UART0_BASE+0x00 */
  273. /*#define asyncRxBufferport1 UART0_BASE+0x00 */
  274. #if CONFIG_SERIAL_SOFTWARE_FIFO
  275. /*-----------------------------------------------------------------------------+
  276. | Fifo
  277. +-----------------------------------------------------------------------------*/
  278. typedef struct {
  279. char *rx_buffer;
  280. ulong rx_put;
  281. ulong rx_get;
  282. } serial_buffer_t;
  283. volatile static serial_buffer_t buf_info;
  284. #endif
  285. #if defined(CONFIG_440) && !defined(CFG_EXT_SERIAL_CLOCK)
  286. static void serial_divs (int baudrate, unsigned long *pudiv,
  287. unsigned short *pbdiv )
  288. {
  289. sys_info_t sysinfo;
  290. unsigned long div; /* total divisor udiv * bdiv */
  291. unsigned long umin; /* minimum udiv */
  292. unsigned short diff; /* smallest diff */
  293. unsigned long udiv; /* best udiv */
  294. unsigned short idiff; /* current diff */
  295. unsigned short ibdiv; /* current bdiv */
  296. unsigned long i;
  297. unsigned long est; /* current estimate */
  298. get_sys_info( &sysinfo );
  299. udiv = 32; /* Assume lowest possible serial clk */
  300. div = sysinfo.freqPLB/(16*baudrate); /* total divisor */
  301. umin = sysinfo.pllOpbDiv<<1; /* 2 x OPB divisor */
  302. diff = 32; /* highest possible */
  303. /* i is the test udiv value -- start with the largest
  304. * possible (32) to minimize serial clock and constrain
  305. * search to umin.
  306. */
  307. for( i = 32; i > umin; i-- ){
  308. ibdiv = div/i;
  309. est = i * ibdiv;
  310. idiff = (est > div) ? (est-div) : (div-est);
  311. if( idiff == 0 ){
  312. udiv = i;
  313. break; /* can't do better */
  314. }
  315. else if( idiff < diff ){
  316. udiv = i; /* best so far */
  317. diff = idiff; /* update lowest diff*/
  318. }
  319. }
  320. *pudiv = udiv;
  321. *pbdiv = div/udiv;
  322. }
  323. #endif /* defined(CONFIG_440) && !defined(CFG_EXT_SERIAL_CLK */
  324. /*
  325. * Minimal serial functions needed to use one of the SMC ports
  326. * as serial console interface.
  327. */
  328. #if defined(CONFIG_440)
  329. int serial_init (void)
  330. {
  331. DECLARE_GLOBAL_DATA_PTR;
  332. unsigned long reg;
  333. unsigned long udiv;
  334. unsigned short bdiv;
  335. volatile char val;
  336. #ifdef CFG_EXT_SERIAL_CLOCK
  337. unsigned long tmp;
  338. #endif
  339. reg = mfdcr(cntrl0) & ~CR0_MASK;
  340. #ifdef CFG_EXT_SERIAL_CLOCK
  341. reg |= CR0_EXTCLK_ENA;
  342. udiv = 1;
  343. tmp = gd->baudrate * 16;
  344. bdiv = (CFG_EXT_SERIAL_CLOCK + tmp / 2) / tmp;
  345. #else
  346. /* For 440, the cpu clock is on divider chain A, UART on divider
  347. * chain B ... so cpu clock is irrelevant. Get the "optimized"
  348. * values that are subject to the 1/2 opb clock constraint
  349. */
  350. serial_divs (gd->baudrate, &udiv, &bdiv);
  351. #endif
  352. reg |= (udiv - 1) << CR0_UDIV_POS; /* set the UART divisor */
  353. mtdcr (cntrl0, reg);
  354. out8 (UART0_BASE + UART_LCR, 0x80); /* set DLAB bit */
  355. out8 (UART0_BASE + UART_DLL, bdiv); /* set baudrate divisor */
  356. out8 (UART0_BASE + UART_DLM, bdiv >> 8);/* set baudrate divisor */
  357. out8 (UART0_BASE + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  358. out8 (UART0_BASE + UART_FCR, 0x00); /* disable FIFO */
  359. out8 (UART0_BASE + UART_MCR, 0x00); /* no modem control DTR RTS */
  360. val = in8 (UART0_BASE + UART_LSR); /* clear line status */
  361. val = in8 (UART0_BASE + UART_RBR); /* read receive buffer */
  362. out8 (UART0_BASE + UART_SCR, 0x00); /* set scratchpad */
  363. out8 (UART0_BASE + UART_IER, 0x00); /* set interrupt enable reg */
  364. return (0);
  365. }
  366. #else /* !defined(CONFIG_440) */
  367. int serial_init (void)
  368. {
  369. DECLARE_GLOBAL_DATA_PTR;
  370. unsigned long reg;
  371. unsigned long tmp;
  372. unsigned long clk;
  373. unsigned long udiv;
  374. unsigned short bdiv;
  375. volatile char val;
  376. reg = mfdcr(cntrl0) & ~CR0_MASK;
  377. #ifdef CFG_EXT_SERIAL_CLOCK
  378. clk = CFG_EXT_SERIAL_CLOCK;
  379. udiv = 1;
  380. reg |= CR0_EXTCLK_ENA;
  381. #else
  382. clk = gd->cpu_clk;
  383. #ifdef CFG_405_UART_ERRATA_59
  384. udiv = 31; /* Errata 59: stuck at 31 */
  385. #else
  386. tmp = CFG_BASE_BAUD * 16;
  387. udiv = (clk + tmp / 2) / tmp;
  388. #endif
  389. #endif
  390. reg |= (udiv - 1) << CR0_UDIV_POS; /* set the UART divisor */
  391. mtdcr (cntrl0, reg);
  392. tmp = gd->baudrate * udiv * 16;
  393. bdiv = (clk + tmp / 2) / tmp;
  394. out8 (UART0_BASE + UART_LCR, 0x80); /* set DLAB bit */
  395. out8 (UART0_BASE + UART_DLL, bdiv); /* set baudrate divisor */
  396. out8 (UART0_BASE + UART_DLM, bdiv >> 8);/* set baudrate divisor */
  397. out8 (UART0_BASE + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  398. out8 (UART0_BASE + UART_FCR, 0x00); /* disable FIFO */
  399. out8 (UART0_BASE + UART_MCR, 0x00); /* no modem control DTR RTS */
  400. val = in8 (UART0_BASE + UART_LSR); /* clear line status */
  401. val = in8 (UART0_BASE + UART_RBR); /* read receive buffer */
  402. out8 (UART0_BASE + UART_SCR, 0x00); /* set scratchpad */
  403. out8 (UART0_BASE + UART_IER, 0x00); /* set interrupt enable reg */
  404. return (0);
  405. }
  406. #endif /* if defined(CONFIG_440) */
  407. void serial_setbrg (void)
  408. {
  409. DECLARE_GLOBAL_DATA_PTR;
  410. unsigned long tmp;
  411. unsigned long clk;
  412. unsigned long udiv;
  413. unsigned short bdiv;
  414. #ifdef CFG_EXT_SERIAL_CLOCK
  415. clk = CFG_EXT_SERIAL_CLOCK;
  416. #else
  417. clk = gd->cpu_clk;
  418. #endif
  419. udiv = ((mfdcr (cntrl0) & 0x3e) >> 1) + 1;
  420. tmp = gd->baudrate * udiv * 16;
  421. bdiv = (clk + tmp / 2) / tmp;
  422. out8 (UART0_BASE + UART_LCR, 0x80); /* set DLAB bit */
  423. out8 (UART0_BASE + UART_DLL, bdiv); /* set baudrate divisor */
  424. out8 (UART0_BASE + UART_DLM, bdiv >> 8);/* set baudrate divisor */
  425. out8 (UART0_BASE + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  426. }
  427. void serial_putc (const char c)
  428. {
  429. int i;
  430. if (c == '\n')
  431. serial_putc ('\r');
  432. /* check THRE bit, wait for transmiter available */
  433. for (i = 1; i < 3500; i++) {
  434. if ((in8 (UART0_BASE + UART_LSR) & 0x20) == 0x20)
  435. break;
  436. udelay (100);
  437. }
  438. out8 (UART0_BASE + UART_THR, c); /* put character out */
  439. }
  440. void serial_puts (const char *s)
  441. {
  442. while (*s) {
  443. serial_putc (*s++);
  444. }
  445. }
  446. int serial_getc ()
  447. {
  448. unsigned char status = 0;
  449. while (1) {
  450. #if defined(CONFIG_HW_WATCHDOG)
  451. WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */
  452. #endif /* CONFIG_HW_WATCHDOG */
  453. status = in8 (UART0_BASE + UART_LSR);
  454. if ((status & asyncLSRDataReady1) != 0x0) {
  455. break;
  456. }
  457. if ((status & ( asyncLSRFramingError1 |
  458. asyncLSROverrunError1 |
  459. asyncLSRParityError1 |
  460. asyncLSRBreakInterrupt1 )) != 0) {
  461. out8 (UART0_BASE + UART_LSR,
  462. asyncLSRFramingError1 |
  463. asyncLSROverrunError1 |
  464. asyncLSRParityError1 |
  465. asyncLSRBreakInterrupt1);
  466. }
  467. }
  468. return (0x000000ff & (int) in8 (UART0_BASE));
  469. }
  470. int serial_tstc ()
  471. {
  472. unsigned char status;
  473. status = in8 (UART0_BASE + UART_LSR);
  474. if ((status & asyncLSRDataReady1) != 0x0) {
  475. return (1);
  476. }
  477. if ((status & ( asyncLSRFramingError1 |
  478. asyncLSROverrunError1 |
  479. asyncLSRParityError1 |
  480. asyncLSRBreakInterrupt1 )) != 0) {
  481. out8 (UART0_BASE + UART_LSR,
  482. asyncLSRFramingError1 |
  483. asyncLSROverrunError1 |
  484. asyncLSRParityError1 |
  485. asyncLSRBreakInterrupt1);
  486. }
  487. return 0;
  488. }
  489. #if CONFIG_SERIAL_SOFTWARE_FIFO
  490. void serial_isr (void *arg)
  491. {
  492. int space;
  493. int c;
  494. const int rx_get = buf_info.rx_get;
  495. int rx_put = buf_info.rx_put;
  496. if (rx_get <= rx_put) {
  497. space = CONFIG_SERIAL_SOFTWARE_FIFO - (rx_put - rx_get);
  498. } else {
  499. space = rx_get - rx_put;
  500. }
  501. while (serial_tstc ()) {
  502. c = serial_getc ();
  503. if (space) {
  504. buf_info.rx_buffer[rx_put++] = c;
  505. space--;
  506. }
  507. if (rx_put == CONFIG_SERIAL_SOFTWARE_FIFO)
  508. rx_put = 0;
  509. if (space < CONFIG_SERIAL_SOFTWARE_FIFO / 4) {
  510. /* Stop flow by setting RTS inactive */
  511. out8 (UART0_BASE + UART_MCR,
  512. in8 (UART0_BASE + UART_MCR) & (0xFF ^ 0x02));
  513. }
  514. }
  515. buf_info.rx_put = rx_put;
  516. }
  517. void serial_buffered_init (void)
  518. {
  519. serial_puts ("Switching to interrupt driven serial input mode.\n");
  520. buf_info.rx_buffer = malloc (CONFIG_SERIAL_SOFTWARE_FIFO);
  521. buf_info.rx_put = 0;
  522. buf_info.rx_get = 0;
  523. if (in8 (UART0_BASE + UART_MSR) & 0x10) {
  524. serial_puts ("Check CTS signal present on serial port: OK.\n");
  525. } else {
  526. serial_puts ("WARNING: CTS signal not present on serial port.\n");
  527. }
  528. irq_install_handler ( VECNUM_U0 /*UART0 */ /*int vec */ ,
  529. serial_isr /*interrupt_handler_t *handler */ ,
  530. (void *) &buf_info /*void *arg */ );
  531. /* Enable "RX Data Available" Interrupt on UART */
  532. /* out8(UART0_BASE + UART_IER, in8(UART0_BASE + UART_IER) |0x01); */
  533. out8 (UART0_BASE + UART_IER, 0x01);
  534. /* Set DTR active */
  535. out8 (UART0_BASE + UART_MCR, in8 (UART0_BASE + UART_MCR) | 0x01);
  536. /* Start flow by setting RTS active */
  537. out8 (UART0_BASE + UART_MCR, in8 (UART0_BASE + UART_MCR) | 0x02);
  538. /* Setup UART FIFO: RX trigger level: 4 byte, Enable FIFO */
  539. out8 (UART0_BASE + UART_FCR, (1 << 6) | 1);
  540. }
  541. void serial_buffered_putc (const char c)
  542. {
  543. /* Wait for CTS */
  544. #if defined(CONFIG_HW_WATCHDOG)
  545. while (!(in8 (UART0_BASE + UART_MSR) & 0x10))
  546. WATCHDOG_RESET ();
  547. #else
  548. while (!(in8 (UART0_BASE + UART_MSR) & 0x10));
  549. #endif
  550. serial_putc (c);
  551. }
  552. void serial_buffered_puts (const char *s)
  553. {
  554. serial_puts (s);
  555. }
  556. int serial_buffered_getc (void)
  557. {
  558. int space;
  559. int c;
  560. int rx_get = buf_info.rx_get;
  561. int rx_put;
  562. #if defined(CONFIG_HW_WATCHDOG)
  563. while (rx_get == buf_info.rx_put)
  564. WATCHDOG_RESET ();
  565. #else
  566. while (rx_get == buf_info.rx_put);
  567. #endif
  568. c = buf_info.rx_buffer[rx_get++];
  569. if (rx_get == CONFIG_SERIAL_SOFTWARE_FIFO)
  570. rx_get = 0;
  571. buf_info.rx_get = rx_get;
  572. rx_put = buf_info.rx_put;
  573. if (rx_get <= rx_put) {
  574. space = CONFIG_SERIAL_SOFTWARE_FIFO - (rx_put - rx_get);
  575. } else {
  576. space = rx_get - rx_put;
  577. }
  578. if (space > CONFIG_SERIAL_SOFTWARE_FIFO / 2) {
  579. /* Start flow by setting RTS active */
  580. out8 (UART0_BASE + UART_MCR, in8 (UART0_BASE + UART_MCR) | 0x02);
  581. }
  582. return c;
  583. }
  584. int serial_buffered_tstc (void)
  585. {
  586. return (buf_info.rx_get != buf_info.rx_put) ? 1 : 0;
  587. }
  588. #endif /* CONFIG_SERIAL_SOFTWARE_FIFO */
  589. #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
  590. /*
  591. AS HARNOIS : according to CONFIG_KGDB_SER_INDEX kgdb uses serial port
  592. number 0 or number 1
  593. - if CONFIG_KGDB_SER_INDEX = 1 => serial port number 0 :
  594. configuration has been already done
  595. - if CONFIG_KGDB_SER_INDEX = 2 => serial port number 1 :
  596. configure port 1 for serial I/O with rate = CONFIG_KGDB_BAUDRATE
  597. */
  598. #if (CONFIG_KGDB_SER_INDEX & 2)
  599. void kgdb_serial_init (void)
  600. {
  601. DECLARE_GLOBAL_DATA_PTR;
  602. volatile char val;
  603. unsigned short br_reg;
  604. get_clocks ();
  605. br_reg = (((((gd->cpu_clk / 16) / 18) * 10) / CONFIG_KGDB_BAUDRATE) +
  606. 5) / 10;
  607. /*
  608. * Init onboard 16550 UART
  609. */
  610. out8 (UART1_BASE + UART_LCR, 0x80); /* set DLAB bit */
  611. out8 (UART1_BASE + UART_DLL, (br_reg & 0x00ff)); /* set divisor for 9600 baud */
  612. out8 (UART1_BASE + UART_DLM, ((br_reg & 0xff00) >> 8)); /* set divisor for 9600 baud */
  613. out8 (UART1_BASE + UART_LCR, 0x03); /* line control 8 bits no parity */
  614. out8 (UART1_BASE + UART_FCR, 0x00); /* disable FIFO */
  615. out8 (UART1_BASE + UART_MCR, 0x00); /* no modem control DTR RTS */
  616. val = in8 (UART1_BASE + UART_LSR); /* clear line status */
  617. val = in8 (UART1_BASE + UART_RBR); /* read receive buffer */
  618. out8 (UART1_BASE + UART_SCR, 0x00); /* set scratchpad */
  619. out8 (UART1_BASE + UART_IER, 0x00); /* set interrupt enable reg */
  620. }
  621. void putDebugChar (const char c)
  622. {
  623. if (c == '\n')
  624. serial_putc ('\r');
  625. out8 (UART1_BASE + UART_THR, c); /* put character out */
  626. /* check THRE bit, wait for transfer done */
  627. while ((in8 (UART1_BASE + UART_LSR) & 0x20) != 0x20);
  628. }
  629. void putDebugStr (const char *s)
  630. {
  631. while (*s) {
  632. serial_putc (*s++);
  633. }
  634. }
  635. int getDebugChar (void)
  636. {
  637. unsigned char status = 0;
  638. while (1) {
  639. status = in8 (UART1_BASE + UART_LSR);
  640. if ((status & asyncLSRDataReady1) != 0x0) {
  641. break;
  642. }
  643. if ((status & ( asyncLSRFramingError1 |
  644. asyncLSROverrunError1 |
  645. asyncLSRParityError1 |
  646. asyncLSRBreakInterrupt1 )) != 0) {
  647. out8 (UART1_BASE + UART_LSR,
  648. asyncLSRFramingError1 |
  649. asyncLSROverrunError1 |
  650. asyncLSRParityError1 |
  651. asyncLSRBreakInterrupt1);
  652. }
  653. }
  654. return (0x000000ff & (int) in8 (UART1_BASE));
  655. }
  656. void kgdb_interruptible (int yes)
  657. {
  658. return;
  659. }
  660. #else /* ! (CONFIG_KGDB_SER_INDEX & 2) */
  661. void kgdb_serial_init (void)
  662. {
  663. serial_printf ("[on serial] ");
  664. }
  665. void putDebugChar (int c)
  666. {
  667. serial_putc (c);
  668. }
  669. void putDebugStr (const char *str)
  670. {
  671. serial_puts (str);
  672. }
  673. int getDebugChar (void)
  674. {
  675. return serial_getc ();
  676. }
  677. void kgdb_interruptible (int yes)
  678. {
  679. return;
  680. }
  681. #endif /* (CONFIG_KGDB_SER_INDEX & 2) */
  682. #endif /* CFG_CMD_KGDB */
  683. #endif /* CONFIG_405GP || CONFIG_405CR */