serial.c 25 KB

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