serial.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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) || defined(CONFIG_405EP)
  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. #elif defined(CONFIG_405EP)
  238. #define UART0_BASE 0xef600300
  239. #define UART1_BASE 0xef600400
  240. #define UCR0_MASK 0x0000007f
  241. #define UCR1_MASK 0x00007f00
  242. #define UCR0_UDIV_POS 0
  243. #define UCR1_UDIV_POS 8
  244. #define UDIV_MAX 127
  245. #else /* CONFIG_405GP || CONFIG_405CR */
  246. #define UART0_BASE 0xef600300
  247. #define UART1_BASE 0xef600400
  248. #define CR0_MASK 0x00001fff
  249. #define CR0_EXTCLK_ENA 0x000000c0
  250. #define CR0_UDIV_POS 1
  251. #define UDIV_MAX 32
  252. #endif
  253. /* using serial port 0 or 1 as U-Boot console ? */
  254. #if defined(CONFIG_UART1_CONSOLE)
  255. #define ACTING_UART0_BASE UART1_BASE
  256. #define ACTING_UART1_BASE UART0_BASE
  257. #else
  258. #define ACTING_UART0_BASE UART0_BASE
  259. #define ACTING_UART1_BASE UART1_BASE
  260. #endif
  261. #if defined(CONFIG_405EP) && defined(CFG_EXT_SERIAL_CLOCK)
  262. #error "External serial clock not supported on IBM PPC405EP!"
  263. #endif
  264. #define UART_RBR 0x00
  265. #define UART_THR 0x00
  266. #define UART_IER 0x01
  267. #define UART_IIR 0x02
  268. #define UART_FCR 0x02
  269. #define UART_LCR 0x03
  270. #define UART_MCR 0x04
  271. #define UART_LSR 0x05
  272. #define UART_MSR 0x06
  273. #define UART_SCR 0x07
  274. #define UART_DLL 0x00
  275. #define UART_DLM 0x01
  276. /*-----------------------------------------------------------------------------+
  277. | Line Status Register.
  278. +-----------------------------------------------------------------------------*/
  279. /*#define asyncLSRport1 ACTING_UART0_BASE+0x05 */
  280. #define asyncLSRDataReady1 0x01
  281. #define asyncLSROverrunError1 0x02
  282. #define asyncLSRParityError1 0x04
  283. #define asyncLSRFramingError1 0x08
  284. #define asyncLSRBreakInterrupt1 0x10
  285. #define asyncLSRTxHoldEmpty1 0x20
  286. #define asyncLSRTxShiftEmpty1 0x40
  287. #define asyncLSRRxFifoError1 0x80
  288. /*-----------------------------------------------------------------------------+
  289. | Miscellanies defines.
  290. +-----------------------------------------------------------------------------*/
  291. /*#define asyncTxBufferport1 ACTING_UART0_BASE+0x00 */
  292. /*#define asyncRxBufferport1 ACTING_UART0_BASE+0x00 */
  293. #if CONFIG_SERIAL_SOFTWARE_FIFO
  294. /*-----------------------------------------------------------------------------+
  295. | Fifo
  296. +-----------------------------------------------------------------------------*/
  297. typedef struct {
  298. char *rx_buffer;
  299. ulong rx_put;
  300. ulong rx_get;
  301. } serial_buffer_t;
  302. volatile static serial_buffer_t buf_info;
  303. #endif
  304. #if defined(CONFIG_440) && !defined(CFG_EXT_SERIAL_CLOCK)
  305. static void serial_divs (int baudrate, unsigned long *pudiv,
  306. unsigned short *pbdiv )
  307. {
  308. sys_info_t sysinfo;
  309. unsigned long div; /* total divisor udiv * bdiv */
  310. unsigned long umin; /* minimum udiv */
  311. unsigned short diff; /* smallest diff */
  312. unsigned long udiv; /* best udiv */
  313. unsigned short idiff; /* current diff */
  314. unsigned short ibdiv; /* current bdiv */
  315. unsigned long i;
  316. unsigned long est; /* current estimate */
  317. get_sys_info( &sysinfo );
  318. udiv = 32; /* Assume lowest possible serial clk */
  319. div = sysinfo.freqPLB/(16*baudrate); /* total divisor */
  320. umin = sysinfo.pllOpbDiv<<1; /* 2 x OPB divisor */
  321. diff = 32; /* highest possible */
  322. /* i is the test udiv value -- start with the largest
  323. * possible (32) to minimize serial clock and constrain
  324. * search to umin.
  325. */
  326. for( i = 32; i > umin; i-- ){
  327. ibdiv = div/i;
  328. est = i * ibdiv;
  329. idiff = (est > div) ? (est-div) : (div-est);
  330. if( idiff == 0 ){
  331. udiv = i;
  332. break; /* can't do better */
  333. }
  334. else if( idiff < diff ){
  335. udiv = i; /* best so far */
  336. diff = idiff; /* update lowest diff*/
  337. }
  338. }
  339. *pudiv = udiv;
  340. *pbdiv = div/udiv;
  341. }
  342. #endif /* defined(CONFIG_440) && !defined(CFG_EXT_SERIAL_CLK */
  343. /*
  344. * Minimal serial functions needed to use one of the SMC ports
  345. * as serial console interface.
  346. */
  347. #if defined(CONFIG_440)
  348. int serial_init (void)
  349. {
  350. DECLARE_GLOBAL_DATA_PTR;
  351. unsigned long reg;
  352. unsigned long udiv;
  353. unsigned short bdiv;
  354. volatile char val;
  355. #ifdef CFG_EXT_SERIAL_CLOCK
  356. unsigned long tmp;
  357. #endif
  358. reg = mfdcr(cntrl0) & ~CR0_MASK;
  359. #ifdef CFG_EXT_SERIAL_CLOCK
  360. reg |= CR0_EXTCLK_ENA;
  361. udiv = 1;
  362. tmp = gd->baudrate * 16;
  363. bdiv = (CFG_EXT_SERIAL_CLOCK + tmp / 2) / tmp;
  364. #else
  365. /* For 440, the cpu clock is on divider chain A, UART on divider
  366. * chain B ... so cpu clock is irrelevant. Get the "optimized"
  367. * values that are subject to the 1/2 opb clock constraint
  368. */
  369. serial_divs (gd->baudrate, &udiv, &bdiv);
  370. #endif
  371. reg |= (udiv - 1) << CR0_UDIV_POS; /* set the UART divisor */
  372. mtdcr (cntrl0, reg);
  373. out8 (ACTING_UART0_BASE + UART_LCR, 0x80); /* set DLAB bit */
  374. out8 (ACTING_UART0_BASE + UART_DLL, bdiv); /* set baudrate divisor */
  375. out8 (ACTING_UART0_BASE + UART_DLM, bdiv >> 8);/* set baudrate divisor */
  376. out8 (ACTING_UART0_BASE + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  377. out8 (ACTING_UART0_BASE + UART_FCR, 0x00); /* disable FIFO */
  378. out8 (ACTING_UART0_BASE + UART_MCR, 0x00); /* no modem control DTR RTS */
  379. val = in8 (ACTING_UART0_BASE + UART_LSR); /* clear line status */
  380. val = in8 (ACTING_UART0_BASE + UART_RBR); /* read receive buffer */
  381. out8 (ACTING_UART0_BASE + UART_SCR, 0x00); /* set scratchpad */
  382. out8 (ACTING_UART0_BASE + UART_IER, 0x00); /* set interrupt enable reg */
  383. return (0);
  384. }
  385. #else /* !defined(CONFIG_440) */
  386. int serial_init (void)
  387. {
  388. DECLARE_GLOBAL_DATA_PTR;
  389. unsigned long reg;
  390. unsigned long tmp;
  391. unsigned long clk;
  392. unsigned long udiv;
  393. unsigned short bdiv;
  394. volatile char val;
  395. #ifdef CONFIG_405EP
  396. reg = mfdcr(cpc0_ucr) & ~(UCR0_MASK | UCR1_MASK);
  397. clk = gd->cpu_clk;
  398. tmp = CFG_BASE_BAUD * 16;
  399. udiv = (clk + tmp / 2) / tmp;
  400. if (udiv > UDIV_MAX) /* max. n bits for udiv */
  401. udiv = UDIV_MAX;
  402. reg |= (udiv) << UCR0_UDIV_POS; /* set the UART divisor */
  403. reg |= (udiv) << UCR1_UDIV_POS; /* set the UART divisor */
  404. mtdcr (cpc0_ucr, reg);
  405. #else /* CONFIG_405EP */
  406. reg = mfdcr(cntrl0) & ~CR0_MASK;
  407. #ifdef CFG_EXT_SERIAL_CLOCK
  408. clk = CFG_EXT_SERIAL_CLOCK;
  409. udiv = 1;
  410. reg |= CR0_EXTCLK_ENA;
  411. #else
  412. clk = gd->cpu_clk;
  413. #ifdef CFG_405_UART_ERRATA_59
  414. udiv = 31; /* Errata 59: stuck at 31 */
  415. #else
  416. tmp = CFG_BASE_BAUD * 16;
  417. udiv = (clk + tmp / 2) / tmp;
  418. if (udiv > UDIV_MAX) /* max. n bits for udiv */
  419. udiv = UDIV_MAX;
  420. #endif
  421. #endif
  422. reg |= (udiv - 1) << CR0_UDIV_POS; /* set the UART divisor */
  423. mtdcr (cntrl0, reg);
  424. #endif /* CONFIG_405EP */
  425. tmp = gd->baudrate * udiv * 16;
  426. bdiv = (clk + tmp / 2) / tmp;
  427. out8 (ACTING_UART0_BASE + UART_LCR, 0x80); /* set DLAB bit */
  428. out8 (ACTING_UART0_BASE + UART_DLL, bdiv); /* set baudrate divisor */
  429. out8 (ACTING_UART0_BASE + UART_DLM, bdiv >> 8);/* set baudrate divisor */
  430. out8 (ACTING_UART0_BASE + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  431. out8 (ACTING_UART0_BASE + UART_FCR, 0x00); /* disable FIFO */
  432. out8 (ACTING_UART0_BASE + UART_MCR, 0x00); /* no modem control DTR RTS */
  433. val = in8 (ACTING_UART0_BASE + UART_LSR); /* clear line status */
  434. val = in8 (ACTING_UART0_BASE + UART_RBR); /* read receive buffer */
  435. out8 (ACTING_UART0_BASE + UART_SCR, 0x00); /* set scratchpad */
  436. out8 (ACTING_UART0_BASE + UART_IER, 0x00); /* set interrupt enable reg */
  437. return (0);
  438. }
  439. #endif /* if defined(CONFIG_440) */
  440. void serial_setbrg (void)
  441. {
  442. DECLARE_GLOBAL_DATA_PTR;
  443. unsigned long tmp;
  444. unsigned long clk;
  445. unsigned long udiv;
  446. unsigned short bdiv;
  447. #ifdef CFG_EXT_SERIAL_CLOCK
  448. clk = CFG_EXT_SERIAL_CLOCK;
  449. #else
  450. clk = gd->cpu_clk;
  451. #endif
  452. #ifdef CONFIG_405EP
  453. udiv = ((mfdcr (cpc0_ucr) & UCR0_MASK) >> UCR0_UDIV_POS);
  454. #else
  455. udiv = ((mfdcr (cntrl0) & 0x3e) >> 1) + 1;
  456. #endif /* CONFIG_405EP */
  457. tmp = gd->baudrate * udiv * 16;
  458. bdiv = (clk + tmp / 2) / tmp;
  459. out8 (ACTING_UART0_BASE + UART_LCR, 0x80); /* set DLAB bit */
  460. out8 (ACTING_UART0_BASE + UART_DLL, bdiv); /* set baudrate divisor */
  461. out8 (ACTING_UART0_BASE + UART_DLM, bdiv >> 8);/* set baudrate divisor */
  462. out8 (ACTING_UART0_BASE + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  463. }
  464. void serial_putc (const char c)
  465. {
  466. int i;
  467. if (c == '\n')
  468. serial_putc ('\r');
  469. /* check THRE bit, wait for transmiter available */
  470. for (i = 1; i < 3500; i++) {
  471. if ((in8 (ACTING_UART0_BASE + UART_LSR) & 0x20) == 0x20)
  472. break;
  473. udelay (100);
  474. }
  475. out8 (ACTING_UART0_BASE + UART_THR, c); /* put character out */
  476. }
  477. void serial_puts (const char *s)
  478. {
  479. while (*s) {
  480. serial_putc (*s++);
  481. }
  482. }
  483. int serial_getc ()
  484. {
  485. unsigned char status = 0;
  486. while (1) {
  487. #if defined(CONFIG_HW_WATCHDOG)
  488. WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */
  489. #endif /* CONFIG_HW_WATCHDOG */
  490. status = in8 (ACTING_UART0_BASE + UART_LSR);
  491. if ((status & asyncLSRDataReady1) != 0x0) {
  492. break;
  493. }
  494. if ((status & ( asyncLSRFramingError1 |
  495. asyncLSROverrunError1 |
  496. asyncLSRParityError1 |
  497. asyncLSRBreakInterrupt1 )) != 0) {
  498. out8 (ACTING_UART0_BASE + UART_LSR,
  499. asyncLSRFramingError1 |
  500. asyncLSROverrunError1 |
  501. asyncLSRParityError1 |
  502. asyncLSRBreakInterrupt1);
  503. }
  504. }
  505. return (0x000000ff & (int) in8 (ACTING_UART0_BASE));
  506. }
  507. int serial_tstc ()
  508. {
  509. unsigned char status;
  510. status = in8 (ACTING_UART0_BASE + UART_LSR);
  511. if ((status & asyncLSRDataReady1) != 0x0) {
  512. return (1);
  513. }
  514. if ((status & ( asyncLSRFramingError1 |
  515. asyncLSROverrunError1 |
  516. asyncLSRParityError1 |
  517. asyncLSRBreakInterrupt1 )) != 0) {
  518. out8 (ACTING_UART0_BASE + UART_LSR,
  519. asyncLSRFramingError1 |
  520. asyncLSROverrunError1 |
  521. asyncLSRParityError1 |
  522. asyncLSRBreakInterrupt1);
  523. }
  524. return 0;
  525. }
  526. #if CONFIG_SERIAL_SOFTWARE_FIFO
  527. void serial_isr (void *arg)
  528. {
  529. int space;
  530. int c;
  531. const int rx_get = buf_info.rx_get;
  532. int rx_put = buf_info.rx_put;
  533. if (rx_get <= rx_put) {
  534. space = CONFIG_SERIAL_SOFTWARE_FIFO - (rx_put - rx_get);
  535. } else {
  536. space = rx_get - rx_put;
  537. }
  538. while (serial_tstc ()) {
  539. c = serial_getc ();
  540. if (space) {
  541. buf_info.rx_buffer[rx_put++] = c;
  542. space--;
  543. }
  544. if (rx_put == CONFIG_SERIAL_SOFTWARE_FIFO)
  545. rx_put = 0;
  546. if (space < CONFIG_SERIAL_SOFTWARE_FIFO / 4) {
  547. /* Stop flow by setting RTS inactive */
  548. out8 (ACTING_UART0_BASE + UART_MCR,
  549. in8 (ACTING_UART0_BASE + UART_MCR) & (0xFF ^ 0x02));
  550. }
  551. }
  552. buf_info.rx_put = rx_put;
  553. }
  554. void serial_buffered_init (void)
  555. {
  556. serial_puts ("Switching to interrupt driven serial input mode.\n");
  557. buf_info.rx_buffer = malloc (CONFIG_SERIAL_SOFTWARE_FIFO);
  558. buf_info.rx_put = 0;
  559. buf_info.rx_get = 0;
  560. if (in8 (ACTING_UART0_BASE + UART_MSR) & 0x10) {
  561. serial_puts ("Check CTS signal present on serial port: OK.\n");
  562. } else {
  563. serial_puts ("WARNING: CTS signal not present on serial port.\n");
  564. }
  565. irq_install_handler ( VECNUM_U0 /*UART0 */ /*int vec */ ,
  566. serial_isr /*interrupt_handler_t *handler */ ,
  567. (void *) &buf_info /*void *arg */ );
  568. /* Enable "RX Data Available" Interrupt on UART */
  569. /* out8(ACTING_UART0_BASE + UART_IER, in8(ACTING_UART0_BASE + UART_IER) |0x01); */
  570. out8 (ACTING_UART0_BASE + UART_IER, 0x01);
  571. /* Set DTR active */
  572. out8 (ACTING_UART0_BASE + UART_MCR, in8 (ACTING_UART0_BASE + UART_MCR) | 0x01);
  573. /* Start flow by setting RTS active */
  574. out8 (ACTING_UART0_BASE + UART_MCR, in8 (ACTING_UART0_BASE + UART_MCR) | 0x02);
  575. /* Setup UART FIFO: RX trigger level: 4 byte, Enable FIFO */
  576. out8 (ACTING_UART0_BASE + UART_FCR, (1 << 6) | 1);
  577. }
  578. void serial_buffered_putc (const char c)
  579. {
  580. /* Wait for CTS */
  581. #if defined(CONFIG_HW_WATCHDOG)
  582. while (!(in8 (ACTING_UART0_BASE + UART_MSR) & 0x10))
  583. WATCHDOG_RESET ();
  584. #else
  585. while (!(in8 (ACTING_UART0_BASE + UART_MSR) & 0x10));
  586. #endif
  587. serial_putc (c);
  588. }
  589. void serial_buffered_puts (const char *s)
  590. {
  591. serial_puts (s);
  592. }
  593. int serial_buffered_getc (void)
  594. {
  595. int space;
  596. int c;
  597. int rx_get = buf_info.rx_get;
  598. int rx_put;
  599. #if defined(CONFIG_HW_WATCHDOG)
  600. while (rx_get == buf_info.rx_put)
  601. WATCHDOG_RESET ();
  602. #else
  603. while (rx_get == buf_info.rx_put);
  604. #endif
  605. c = buf_info.rx_buffer[rx_get++];
  606. if (rx_get == CONFIG_SERIAL_SOFTWARE_FIFO)
  607. rx_get = 0;
  608. buf_info.rx_get = rx_get;
  609. rx_put = buf_info.rx_put;
  610. if (rx_get <= rx_put) {
  611. space = CONFIG_SERIAL_SOFTWARE_FIFO - (rx_put - rx_get);
  612. } else {
  613. space = rx_get - rx_put;
  614. }
  615. if (space > CONFIG_SERIAL_SOFTWARE_FIFO / 2) {
  616. /* Start flow by setting RTS active */
  617. out8 (ACTING_UART0_BASE + UART_MCR, in8 (ACTING_UART0_BASE + UART_MCR) | 0x02);
  618. }
  619. return c;
  620. }
  621. int serial_buffered_tstc (void)
  622. {
  623. return (buf_info.rx_get != buf_info.rx_put) ? 1 : 0;
  624. }
  625. #endif /* CONFIG_SERIAL_SOFTWARE_FIFO */
  626. #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
  627. /*
  628. AS HARNOIS : according to CONFIG_KGDB_SER_INDEX kgdb uses serial port
  629. number 0 or number 1
  630. - if CONFIG_KGDB_SER_INDEX = 1 => serial port number 0 :
  631. configuration has been already done
  632. - if CONFIG_KGDB_SER_INDEX = 2 => serial port number 1 :
  633. configure port 1 for serial I/O with rate = CONFIG_KGDB_BAUDRATE
  634. */
  635. #if (CONFIG_KGDB_SER_INDEX & 2)
  636. void kgdb_serial_init (void)
  637. {
  638. DECLARE_GLOBAL_DATA_PTR;
  639. volatile char val;
  640. unsigned short br_reg;
  641. get_clocks ();
  642. br_reg = (((((gd->cpu_clk / 16) / 18) * 10) / CONFIG_KGDB_BAUDRATE) +
  643. 5) / 10;
  644. /*
  645. * Init onboard 16550 UART
  646. */
  647. out8 (ACTING_UART1_BASE + UART_LCR, 0x80); /* set DLAB bit */
  648. out8 (ACTING_UART1_BASE + UART_DLL, (br_reg & 0x00ff)); /* set divisor for 9600 baud */
  649. out8 (ACTING_UART1_BASE + UART_DLM, ((br_reg & 0xff00) >> 8)); /* set divisor for 9600 baud */
  650. out8 (ACTING_UART1_BASE + UART_LCR, 0x03); /* line control 8 bits no parity */
  651. out8 (ACTING_UART1_BASE + UART_FCR, 0x00); /* disable FIFO */
  652. out8 (ACTING_UART1_BASE + UART_MCR, 0x00); /* no modem control DTR RTS */
  653. val = in8 (ACTING_UART1_BASE + UART_LSR); /* clear line status */
  654. val = in8 (ACTING_UART1_BASE + UART_RBR); /* read receive buffer */
  655. out8 (ACTING_UART1_BASE + UART_SCR, 0x00); /* set scratchpad */
  656. out8 (ACTING_UART1_BASE + UART_IER, 0x00); /* set interrupt enable reg */
  657. }
  658. void putDebugChar (const char c)
  659. {
  660. if (c == '\n')
  661. serial_putc ('\r');
  662. out8 (ACTING_UART1_BASE + UART_THR, c); /* put character out */
  663. /* check THRE bit, wait for transfer done */
  664. while ((in8 (ACTING_UART1_BASE + UART_LSR) & 0x20) != 0x20);
  665. }
  666. void putDebugStr (const char *s)
  667. {
  668. while (*s) {
  669. serial_putc (*s++);
  670. }
  671. }
  672. int getDebugChar (void)
  673. {
  674. unsigned char status = 0;
  675. while (1) {
  676. status = in8 (ACTING_UART1_BASE + UART_LSR);
  677. if ((status & asyncLSRDataReady1) != 0x0) {
  678. break;
  679. }
  680. if ((status & ( asyncLSRFramingError1 |
  681. asyncLSROverrunError1 |
  682. asyncLSRParityError1 |
  683. asyncLSRBreakInterrupt1 )) != 0) {
  684. out8 (ACTING_UART1_BASE + UART_LSR,
  685. asyncLSRFramingError1 |
  686. asyncLSROverrunError1 |
  687. asyncLSRParityError1 |
  688. asyncLSRBreakInterrupt1);
  689. }
  690. }
  691. return (0x000000ff & (int) in8 (ACTING_UART1_BASE));
  692. }
  693. void kgdb_interruptible (int yes)
  694. {
  695. return;
  696. }
  697. #else /* ! (CONFIG_KGDB_SER_INDEX & 2) */
  698. void kgdb_serial_init (void)
  699. {
  700. serial_printf ("[on serial] ");
  701. }
  702. void putDebugChar (int c)
  703. {
  704. serial_putc (c);
  705. }
  706. void putDebugStr (const char *str)
  707. {
  708. serial_puts (str);
  709. }
  710. int getDebugChar (void)
  711. {
  712. return serial_getc ();
  713. }
  714. void kgdb_interruptible (int yes)
  715. {
  716. return;
  717. }
  718. #endif /* (CONFIG_KGDB_SER_INDEX & 2) */
  719. #endif /* CFG_CMD_KGDB */
  720. #endif /* CONFIG_405GP || CONFIG_405CR */