4xx_uart.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /*
  2. * (C) Copyright 2000-2006
  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. /*
  45. * Travis Sawyer 15 September 2004
  46. * Added CONFIG_SERIAL_MULTI support
  47. */
  48. #include <common.h>
  49. #include <commproc.h>
  50. #include <asm/processor.h>
  51. #include <watchdog.h>
  52. #include "vecnum.h"
  53. #ifdef CONFIG_SERIAL_MULTI
  54. #include <serial.h>
  55. #endif
  56. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  57. #include <malloc.h>
  58. #endif
  59. DECLARE_GLOBAL_DATA_PTR;
  60. #if defined(CONFIG_405GP) || defined(CONFIG_405CR) || \
  61. defined(CONFIG_405EP) || defined(CONFIG_405EZ) || \
  62. defined(CONFIG_405EX) || defined(CONFIG_440)
  63. #if defined(CONFIG_440)
  64. #if defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
  65. defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
  66. #define UART0_BASE CFG_PERIPHERAL_BASE + 0x00000300
  67. #define UART1_BASE CFG_PERIPHERAL_BASE + 0x00000400
  68. #else
  69. #define UART0_BASE CFG_PERIPHERAL_BASE + 0x00000200
  70. #define UART1_BASE CFG_PERIPHERAL_BASE + 0x00000300
  71. #endif
  72. #if defined(CONFIG_440SP) || defined(CONFIG_440SPE)
  73. #define UART2_BASE CFG_PERIPHERAL_BASE + 0x00000600
  74. #endif
  75. #if defined(CONFIG_440GP)
  76. #define CR0_MASK 0x3fff0000
  77. #define CR0_EXTCLK_ENA 0x00600000
  78. #define CR0_UDIV_POS 16
  79. #define UDIV_SUBTRACT 1
  80. #define UART0_SDR cntrl0
  81. #define MFREG(a, d) d = mfdcr(a)
  82. #define MTREG(a, d) mtdcr(a, d)
  83. #else /* #if defined(CONFIG_440GP) */
  84. /* all other 440 PPC's access clock divider via sdr register */
  85. #define CR0_MASK 0xdfffffff
  86. #define CR0_EXTCLK_ENA 0x00800000
  87. #define CR0_UDIV_POS 0
  88. #define UDIV_SUBTRACT 0
  89. #define UART0_SDR sdr_uart0
  90. #define UART1_SDR sdr_uart1
  91. #if defined(CONFIG_440EP) || defined(CONFIG_440EPx) || \
  92. defined(CONFIG_440GR) || defined(CONFIG_440GRx) || \
  93. defined(CONFIG_440SP) || defined(CONFIG_440SPe)
  94. #define UART2_SDR sdr_uart2
  95. #endif
  96. #if defined(CONFIG_440EP) || defined(CONFIG_440EPx) || \
  97. defined(CONFIG_440GR) || defined(CONFIG_440GRx)
  98. #define UART3_SDR sdr_uart3
  99. #endif
  100. #define MFREG(a, d) mfsdr(a, d)
  101. #define MTREG(a, d) mtsdr(a, d)
  102. #endif /* #if defined(CONFIG_440GP) */
  103. #elif defined(CONFIG_405EP) || defined(CONFIG_405EZ)
  104. #define UART0_BASE 0xef600300
  105. #define UART1_BASE 0xef600400
  106. #define UCR0_MASK 0x0000007f
  107. #define UCR1_MASK 0x00007f00
  108. #define UCR0_UDIV_POS 0
  109. #define UCR1_UDIV_POS 8
  110. #define UDIV_MAX 127
  111. #elif defined(CONFIG_405EX)
  112. #define UART0_BASE 0xef600200
  113. #define UART1_BASE 0xef600300
  114. #define CR0_MASK 0x000000ff
  115. #define CR0_EXTCLK_ENA 0x00800000
  116. #define CR0_UDIV_POS 0
  117. #define UDIV_SUBTRACT 0
  118. #define UART0_SDR sdr_uart0
  119. #define UART1_SDR sdr_uart1
  120. #else /* CONFIG_405GP || CONFIG_405CR */
  121. #define UART0_BASE 0xef600300
  122. #define UART1_BASE 0xef600400
  123. #define CR0_MASK 0x00001fff
  124. #define CR0_EXTCLK_ENA 0x000000c0
  125. #define CR0_UDIV_POS 1
  126. #define UDIV_MAX 32
  127. #endif
  128. /* using serial port 0 or 1 as U-Boot console ? */
  129. #if defined(CONFIG_UART1_CONSOLE)
  130. #define ACTING_UART0_BASE UART1_BASE
  131. #define ACTING_UART1_BASE UART0_BASE
  132. #else
  133. #define ACTING_UART0_BASE UART0_BASE
  134. #define ACTING_UART1_BASE UART1_BASE
  135. #endif
  136. #if defined(CONFIG_SERIAL_MULTI)
  137. #define UART_BASE dev_base
  138. #else
  139. #define UART_BASE ACTING_UART0_BASE
  140. #endif
  141. #if defined(CONFIG_405EP) && defined(CFG_EXT_SERIAL_CLOCK)
  142. #error "External serial clock not supported on AMCC PPC405EP!"
  143. #endif
  144. #define UART_RBR 0x00
  145. #define UART_THR 0x00
  146. #define UART_IER 0x01
  147. #define UART_IIR 0x02
  148. #define UART_FCR 0x02
  149. #define UART_LCR 0x03
  150. #define UART_MCR 0x04
  151. #define UART_LSR 0x05
  152. #define UART_MSR 0x06
  153. #define UART_SCR 0x07
  154. #define UART_DLL 0x00
  155. #define UART_DLM 0x01
  156. /*-----------------------------------------------------------------------------+
  157. | Line Status Register.
  158. +-----------------------------------------------------------------------------*/
  159. /*#define asyncLSRport1 ACTING_UART0_BASE+0x05 */
  160. #define asyncLSRDataReady1 0x01
  161. #define asyncLSROverrunError1 0x02
  162. #define asyncLSRParityError1 0x04
  163. #define asyncLSRFramingError1 0x08
  164. #define asyncLSRBreakInterrupt1 0x10
  165. #define asyncLSRTxHoldEmpty1 0x20
  166. #define asyncLSRTxShiftEmpty1 0x40
  167. #define asyncLSRRxFifoError1 0x80
  168. /*-----------------------------------------------------------------------------+
  169. | Miscellanies defines.
  170. +-----------------------------------------------------------------------------*/
  171. /*#define asyncTxBufferport1 ACTING_UART0_BASE+0x00 */
  172. /*#define asyncRxBufferport1 ACTING_UART0_BASE+0x00 */
  173. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  174. /*-----------------------------------------------------------------------------+
  175. | Fifo
  176. +-----------------------------------------------------------------------------*/
  177. typedef struct {
  178. char *rx_buffer;
  179. ulong rx_put;
  180. ulong rx_get;
  181. } serial_buffer_t;
  182. volatile static serial_buffer_t buf_info;
  183. #endif
  184. #if (defined(CONFIG_440) || defined(CONFIG_405EX)) && \
  185. !defined(CFG_EXT_SERIAL_CLOCK)
  186. static void serial_divs (int baudrate, unsigned long *pudiv,
  187. unsigned short *pbdiv)
  188. {
  189. sys_info_t sysinfo;
  190. unsigned long div; /* total divisor udiv * bdiv */
  191. unsigned long umin; /* minimum udiv */
  192. unsigned short diff; /* smallest diff */
  193. unsigned long udiv; /* best udiv */
  194. unsigned short idiff; /* current diff */
  195. unsigned short ibdiv; /* current bdiv */
  196. unsigned long i;
  197. unsigned long est; /* current estimate */
  198. get_sys_info(&sysinfo);
  199. udiv = 32; /* Assume lowest possible serial clk */
  200. div = sysinfo.freqPLB / (16 * baudrate); /* total divisor */
  201. umin = sysinfo.pllOpbDiv << 1; /* 2 x OPB divisor */
  202. diff = 32; /* highest possible */
  203. /* i is the test udiv value -- start with the largest
  204. * possible (32) to minimize serial clock and constrain
  205. * search to umin.
  206. */
  207. for (i = 32; i > umin; i--) {
  208. ibdiv = div / i;
  209. est = i * ibdiv;
  210. idiff = (est > div) ? (est-div) : (div-est);
  211. if (idiff == 0) {
  212. udiv = i;
  213. break; /* can't do better */
  214. } else if (idiff < diff) {
  215. udiv = i; /* best so far */
  216. diff = idiff; /* update lowest diff*/
  217. }
  218. }
  219. *pudiv = udiv;
  220. *pbdiv = div / udiv;
  221. }
  222. #elif defined(CONFIG_405EZ)
  223. static void serial_divs (int baudrate, unsigned long *pudiv,
  224. unsigned short *pbdiv)
  225. {
  226. sys_info_t sysinfo;
  227. unsigned long div; /* total divisor udiv * bdiv */
  228. unsigned long umin; /* minimum udiv */
  229. unsigned short diff; /* smallest diff */
  230. unsigned long udiv; /* best udiv */
  231. unsigned short idiff; /* current diff */
  232. unsigned short ibdiv; /* current bdiv */
  233. unsigned long i;
  234. unsigned long est; /* current estimate */
  235. unsigned long plloutb;
  236. unsigned long cpr_pllc;
  237. u32 reg;
  238. /* check the pll feedback source */
  239. mfcpr(cprpllc, cpr_pllc);
  240. get_sys_info(&sysinfo);
  241. plloutb = ((CONFIG_SYS_CLK_FREQ * ((cpr_pllc & PLLC_SRC_MASK) ?
  242. sysinfo.pllFwdDivB : sysinfo.pllFwdDiv) * sysinfo.pllFbkDiv) /
  243. sysinfo.pllFwdDivB);
  244. udiv = 256; /* Assume lowest possible serial clk */
  245. div = plloutb / (16 * baudrate); /* total divisor */
  246. umin = (plloutb / get_OPB_freq()) << 1; /* 2 x OPB divisor */
  247. diff = 256; /* highest possible */
  248. /* i is the test udiv value -- start with the largest
  249. * possible (256) to minimize serial clock and constrain
  250. * search to umin.
  251. */
  252. for (i = 256; i > umin; i--) {
  253. ibdiv = div / i;
  254. est = i * ibdiv;
  255. idiff = (est > div) ? (est-div) : (div-est);
  256. if (idiff == 0) {
  257. udiv = i;
  258. break; /* can't do better */
  259. } else if (idiff < diff) {
  260. udiv = i; /* best so far */
  261. diff = idiff; /* update lowest diff*/
  262. }
  263. }
  264. *pudiv = udiv;
  265. mfcpr(cprperd0, reg);
  266. reg &= ~0x0000ffff;
  267. reg |= ((udiv - 0) << 8) | (udiv - 0);
  268. mtcpr(cprperd0, reg);
  269. *pbdiv = div / udiv;
  270. }
  271. #endif /* defined(CONFIG_440) && !defined(CFG_EXT_SERIAL_CLK) */
  272. /*
  273. * Minimal serial functions needed to use one of the SMC ports
  274. * as serial console interface.
  275. */
  276. #if defined(CONFIG_440)
  277. #if defined(CONFIG_SERIAL_MULTI)
  278. int serial_init_dev (unsigned long dev_base)
  279. #else
  280. int serial_init(void)
  281. #endif
  282. {
  283. unsigned long reg;
  284. unsigned long udiv;
  285. unsigned short bdiv;
  286. volatile char val;
  287. #ifdef CFG_EXT_SERIAL_CLOCK
  288. unsigned long tmp;
  289. #endif
  290. MFREG(UART0_SDR, reg);
  291. reg &= ~CR0_MASK;
  292. #ifdef CFG_EXT_SERIAL_CLOCK
  293. reg |= CR0_EXTCLK_ENA;
  294. udiv = 1;
  295. tmp = gd->baudrate * 16;
  296. bdiv = (CFG_EXT_SERIAL_CLOCK + tmp / 2) / tmp;
  297. gd->freqUART = CFG_EXT_SERIAL_CLOCK;
  298. #else
  299. /* For 440, the cpu clock is on divider chain A, UART on divider
  300. * chain B ... so cpu clock is irrelevant. Get the "optimized"
  301. * values that are subject to the 1/2 opb clock constraint
  302. */
  303. serial_divs (gd->baudrate, &udiv, &bdiv);
  304. /* Correct UART frequency in bd-info struct now that
  305. * the UART divisor is available
  306. */
  307. gd->freqUART = gd->freqUART / udiv;
  308. #endif
  309. reg |= (udiv - UDIV_SUBTRACT) << CR0_UDIV_POS; /* set the UART divisor */
  310. /*
  311. * Configure input clock to baudrate generator for all
  312. * available serial ports here
  313. */
  314. MTREG(UART0_SDR, reg);
  315. #if defined(UART1_SDR)
  316. MTREG(UART1_SDR, reg);
  317. #endif
  318. #if defined(UART2_SDR)
  319. MTREG(UART2_SDR, reg);
  320. #endif
  321. #if defined(UART3_SDR)
  322. MTREG(UART3_SDR, reg);
  323. #endif
  324. out8(UART_BASE + UART_LCR, 0x80); /* set DLAB bit */
  325. out8(UART_BASE + UART_DLL, bdiv); /* set baudrate divisor */
  326. out8(UART_BASE + UART_DLM, bdiv >> 8); /* set baudrate divisor */
  327. out8(UART_BASE + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  328. out8(UART_BASE + UART_FCR, 0x00); /* disable FIFO */
  329. out8(UART_BASE + UART_MCR, 0x00); /* no modem control DTR RTS */
  330. val = in8(UART_BASE + UART_LSR); /* clear line status */
  331. val = in8(UART_BASE + UART_RBR); /* read receive buffer */
  332. out8(UART_BASE + UART_SCR, 0x00); /* set scratchpad */
  333. out8(UART_BASE + UART_IER, 0x00); /* set interrupt enable reg */
  334. return (0);
  335. }
  336. #else /* !defined(CONFIG_440) */
  337. #if defined(CONFIG_SERIAL_MULTI)
  338. int serial_init_dev (unsigned long dev_base)
  339. #else
  340. int serial_init (void)
  341. #endif
  342. {
  343. unsigned long reg;
  344. unsigned long tmp;
  345. unsigned long clk;
  346. unsigned long udiv;
  347. unsigned short bdiv;
  348. volatile char val;
  349. #ifdef CONFIG_405EX
  350. clk = tmp = 0;
  351. mfsdr(UART0_SDR, reg);
  352. reg &= ~CR0_MASK;
  353. #ifdef CFG_EXT_SERIAL_CLOCK
  354. reg |= CR0_EXTCLK_ENA;
  355. udiv = 1;
  356. tmp = gd->baudrate * 16;
  357. bdiv = (CFG_EXT_SERIAL_CLOCK + tmp / 2) / tmp;
  358. #else
  359. serial_divs(gd->baudrate, &udiv, &bdiv);
  360. #endif
  361. reg |= (udiv - UDIV_SUBTRACT) << CR0_UDIV_POS; /* set the UART divisor */
  362. /*
  363. * Configure input clock to baudrate generator for all
  364. * available serial ports here
  365. */
  366. mtsdr(UART0_SDR, reg);
  367. #if defined(UART1_SDR)
  368. mtsdr(UART1_SDR, reg);
  369. #endif
  370. #elif defined(CONFIG_405EZ)
  371. serial_divs(gd->baudrate, &udiv, &bdiv);
  372. clk = tmp = reg = 0;
  373. #else
  374. #ifdef CONFIG_405EP
  375. reg = mfdcr(cpc0_ucr) & ~(UCR0_MASK | UCR1_MASK);
  376. clk = gd->cpu_clk;
  377. tmp = CFG_BASE_BAUD * 16;
  378. udiv = (clk + tmp / 2) / tmp;
  379. if (udiv > UDIV_MAX) /* max. n bits for udiv */
  380. udiv = UDIV_MAX;
  381. reg |= (udiv) << UCR0_UDIV_POS; /* set the UART divisor */
  382. reg |= (udiv) << UCR1_UDIV_POS; /* set the UART divisor */
  383. mtdcr (cpc0_ucr, reg);
  384. #else /* CONFIG_405EP */
  385. reg = mfdcr(cntrl0) & ~CR0_MASK;
  386. #ifdef CFG_EXT_SERIAL_CLOCK
  387. clk = CFG_EXT_SERIAL_CLOCK;
  388. udiv = 1;
  389. reg |= CR0_EXTCLK_ENA;
  390. #else
  391. clk = gd->cpu_clk;
  392. #ifdef CFG_405_UART_ERRATA_59
  393. udiv = 31; /* Errata 59: stuck at 31 */
  394. #else
  395. tmp = CFG_BASE_BAUD * 16;
  396. udiv = (clk + tmp / 2) / tmp;
  397. if (udiv > UDIV_MAX) /* max. n bits for udiv */
  398. udiv = UDIV_MAX;
  399. #endif
  400. #endif
  401. reg |= (udiv - 1) << CR0_UDIV_POS; /* set the UART divisor */
  402. mtdcr (cntrl0, reg);
  403. #endif /* CONFIG_405EP */
  404. tmp = gd->baudrate * udiv * 16;
  405. bdiv = (clk + tmp / 2) / tmp;
  406. #endif /* CONFIG_405EX */
  407. /* Correct UART frequency in bd-info struct now that
  408. * the UART divisor is available
  409. */
  410. #ifdef CFG_EXT_SERIAL_CLOCK
  411. gd->freqUART = CFG_EXT_SERIAL_CLOCK;
  412. #else
  413. gd->freqUART = gd->freqUART / udiv;
  414. #endif
  415. out8(UART_BASE + UART_LCR, 0x80); /* set DLAB bit */
  416. out8(UART_BASE + UART_DLL, bdiv); /* set baudrate divisor */
  417. out8(UART_BASE + UART_DLM, bdiv >> 8); /* set baudrate divisor */
  418. out8(UART_BASE + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  419. out8(UART_BASE + UART_FCR, 0x00); /* disable FIFO */
  420. out8(UART_BASE + UART_MCR, 0x00); /* no modem control DTR RTS */
  421. val = in8(UART_BASE + UART_LSR); /* clear line status */
  422. val = in8(UART_BASE + UART_RBR); /* read receive buffer */
  423. out8(UART_BASE + UART_SCR, 0x00); /* set scratchpad */
  424. out8(UART_BASE + UART_IER, 0x00); /* set interrupt enable reg */
  425. return (0);
  426. }
  427. #endif /* if defined(CONFIG_440) */
  428. #if defined(CONFIG_SERIAL_MULTI)
  429. void serial_setbrg_dev (unsigned long dev_base)
  430. #else
  431. void serial_setbrg (void)
  432. #endif
  433. {
  434. #if defined(CONFIG_SERIAL_MULTI)
  435. serial_init_dev(dev_base);
  436. #else
  437. serial_init();
  438. #endif
  439. }
  440. #if defined(CONFIG_SERIAL_MULTI)
  441. void serial_putc_dev (unsigned long dev_base, const char c)
  442. #else
  443. void serial_putc (const char c)
  444. #endif
  445. {
  446. int i;
  447. if (c == '\n')
  448. #if defined(CONFIG_SERIAL_MULTI)
  449. serial_putc_dev (dev_base, '\r');
  450. #else
  451. serial_putc ('\r');
  452. #endif
  453. /* check THRE bit, wait for transmiter available */
  454. for (i = 1; i < 3500; i++) {
  455. if ((in8 (UART_BASE + UART_LSR) & 0x20) == 0x20)
  456. break;
  457. udelay (100);
  458. }
  459. out8 (UART_BASE + UART_THR, c); /* put character out */
  460. }
  461. #if defined(CONFIG_SERIAL_MULTI)
  462. void serial_puts_dev (unsigned long dev_base, const char *s)
  463. #else
  464. void serial_puts (const char *s)
  465. #endif
  466. {
  467. while (*s) {
  468. #if defined(CONFIG_SERIAL_MULTI)
  469. serial_putc_dev (dev_base, *s++);
  470. #else
  471. serial_putc (*s++);
  472. #endif
  473. }
  474. }
  475. #if defined(CONFIG_SERIAL_MULTI)
  476. int serial_getc_dev (unsigned long dev_base)
  477. #else
  478. int serial_getc (void)
  479. #endif
  480. {
  481. unsigned char status = 0;
  482. while (1) {
  483. #if defined(CONFIG_HW_WATCHDOG)
  484. WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */
  485. #endif /* CONFIG_HW_WATCHDOG */
  486. status = in8 (UART_BASE + UART_LSR);
  487. if ((status & asyncLSRDataReady1) != 0x0) {
  488. break;
  489. }
  490. if ((status & ( asyncLSRFramingError1 |
  491. asyncLSROverrunError1 |
  492. asyncLSRParityError1 |
  493. asyncLSRBreakInterrupt1 )) != 0) {
  494. out8 (UART_BASE + UART_LSR,
  495. asyncLSRFramingError1 |
  496. asyncLSROverrunError1 |
  497. asyncLSRParityError1 |
  498. asyncLSRBreakInterrupt1);
  499. }
  500. }
  501. return (0x000000ff & (int) in8 (UART_BASE));
  502. }
  503. #if defined(CONFIG_SERIAL_MULTI)
  504. int serial_tstc_dev (unsigned long dev_base)
  505. #else
  506. int serial_tstc (void)
  507. #endif
  508. {
  509. unsigned char status;
  510. status = in8 (UART_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 (UART_BASE + UART_LSR,
  519. asyncLSRFramingError1 |
  520. asyncLSROverrunError1 |
  521. asyncLSRParityError1 |
  522. asyncLSRBreakInterrupt1);
  523. }
  524. return 0;
  525. }
  526. #ifdef 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_dev (ACTING_UART0_BASE)) {
  539. c = serial_getc_dev (ACTING_UART0_BASE);
  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 defined(CONFIG_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. volatile char val;
  639. unsigned short br_reg;
  640. get_clocks ();
  641. br_reg = (((((gd->cpu_clk / 16) / 18) * 10) / CONFIG_KGDB_BAUDRATE) +
  642. 5) / 10;
  643. /*
  644. * Init onboard 16550 UART
  645. */
  646. out8 (ACTING_UART1_BASE + UART_LCR, 0x80); /* set DLAB bit */
  647. out8 (ACTING_UART1_BASE + UART_DLL, (br_reg & 0x00ff)); /* set divisor for 9600 baud */
  648. out8 (ACTING_UART1_BASE + UART_DLM, ((br_reg & 0xff00) >> 8)); /* set divisor for 9600 baud */
  649. out8 (ACTING_UART1_BASE + UART_LCR, 0x03); /* line control 8 bits no parity */
  650. out8 (ACTING_UART1_BASE + UART_FCR, 0x00); /* disable FIFO */
  651. out8 (ACTING_UART1_BASE + UART_MCR, 0x00); /* no modem control DTR RTS */
  652. val = in8 (ACTING_UART1_BASE + UART_LSR); /* clear line status */
  653. val = in8 (ACTING_UART1_BASE + UART_RBR); /* read receive buffer */
  654. out8 (ACTING_UART1_BASE + UART_SCR, 0x00); /* set scratchpad */
  655. out8 (ACTING_UART1_BASE + UART_IER, 0x00); /* set interrupt enable reg */
  656. }
  657. void putDebugChar (const char c)
  658. {
  659. if (c == '\n')
  660. serial_putc ('\r');
  661. out8 (ACTING_UART1_BASE + UART_THR, c); /* put character out */
  662. /* check THRE bit, wait for transfer done */
  663. while ((in8 (ACTING_UART1_BASE + UART_LSR) & 0x20) != 0x20);
  664. }
  665. void putDebugStr (const char *s)
  666. {
  667. while (*s) {
  668. serial_putc (*s++);
  669. }
  670. }
  671. int getDebugChar (void)
  672. {
  673. unsigned char status = 0;
  674. while (1) {
  675. status = in8 (ACTING_UART1_BASE + UART_LSR);
  676. if ((status & asyncLSRDataReady1) != 0x0) {
  677. break;
  678. }
  679. if ((status & ( asyncLSRFramingError1 |
  680. asyncLSROverrunError1 |
  681. asyncLSRParityError1 |
  682. asyncLSRBreakInterrupt1 )) != 0) {
  683. out8 (ACTING_UART1_BASE + UART_LSR,
  684. asyncLSRFramingError1 |
  685. asyncLSROverrunError1 |
  686. asyncLSRParityError1 |
  687. asyncLSRBreakInterrupt1);
  688. }
  689. }
  690. return (0x000000ff & (int) in8 (ACTING_UART1_BASE));
  691. }
  692. void kgdb_interruptible (int yes)
  693. {
  694. return;
  695. }
  696. #else /* ! (CONFIG_KGDB_SER_INDEX & 2) */
  697. void kgdb_serial_init (void)
  698. {
  699. serial_printf ("[on serial] ");
  700. }
  701. void putDebugChar (int c)
  702. {
  703. serial_putc (c);
  704. }
  705. void putDebugStr (const char *str)
  706. {
  707. serial_puts (str);
  708. }
  709. int getDebugChar (void)
  710. {
  711. return serial_getc ();
  712. }
  713. void kgdb_interruptible (int yes)
  714. {
  715. return;
  716. }
  717. #endif /* (CONFIG_KGDB_SER_INDEX & 2) */
  718. #endif
  719. #if defined(CONFIG_SERIAL_MULTI)
  720. int serial0_init(void)
  721. {
  722. return (serial_init_dev(UART0_BASE));
  723. }
  724. int serial1_init(void)
  725. {
  726. return (serial_init_dev(UART1_BASE));
  727. }
  728. void serial0_setbrg (void)
  729. {
  730. serial_setbrg_dev(UART0_BASE);
  731. }
  732. void serial1_setbrg (void)
  733. {
  734. serial_setbrg_dev(UART1_BASE);
  735. }
  736. void serial0_putc(const char c)
  737. {
  738. serial_putc_dev(UART0_BASE,c);
  739. }
  740. void serial1_putc(const char c)
  741. {
  742. serial_putc_dev(UART1_BASE, c);
  743. }
  744. void serial0_puts(const char *s)
  745. {
  746. serial_puts_dev(UART0_BASE, s);
  747. }
  748. void serial1_puts(const char *s)
  749. {
  750. serial_puts_dev(UART1_BASE, s);
  751. }
  752. int serial0_getc(void)
  753. {
  754. return(serial_getc_dev(UART0_BASE));
  755. }
  756. int serial1_getc(void)
  757. {
  758. return(serial_getc_dev(UART1_BASE));
  759. }
  760. int serial0_tstc(void)
  761. {
  762. return (serial_tstc_dev(UART0_BASE));
  763. }
  764. int serial1_tstc(void)
  765. {
  766. return (serial_tstc_dev(UART1_BASE));
  767. }
  768. struct serial_device serial0_device =
  769. {
  770. "serial0",
  771. "UART0",
  772. serial0_init,
  773. serial0_setbrg,
  774. serial0_getc,
  775. serial0_tstc,
  776. serial0_putc,
  777. serial0_puts,
  778. };
  779. struct serial_device serial1_device =
  780. {
  781. "serial1",
  782. "UART1",
  783. serial1_init,
  784. serial1_setbrg,
  785. serial1_getc,
  786. serial1_tstc,
  787. serial1_putc,
  788. serial1_puts,
  789. };
  790. #endif /* CONFIG_SERIAL_MULTI */
  791. #endif /* CONFIG_405GP || CONFIG_405CR */