4xx_uart.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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. * This source code has been made available to you by IBM on an AS-IS
  25. * basis. Anyone receiving this source is licensed under IBM
  26. * copyrights to use it in any way he or she deems fit, including
  27. * copying it, modifying it, compiling it, and redistributing it either
  28. * with or without modifications. No license under IBM patents or
  29. * patent applications is to be implied by the copyright license.
  30. *
  31. * Any user of this software should understand that IBM cannot provide
  32. * technical support for this software and will not be responsible for
  33. * any consequences resulting from the use of this software.
  34. *
  35. * Any person who transfers this source code or any derivative work
  36. * must include the IBM copyright notice, this paragraph, and the
  37. * preceding two paragraphs in the transferred software.
  38. *
  39. * COPYRIGHT I B M CORPORATION 1995
  40. * LICENSED MATERIAL - PROGRAM PROPERTY OF I B M
  41. */
  42. #include <common.h>
  43. #include <commproc.h>
  44. #include <asm/processor.h>
  45. #include <asm/io.h>
  46. #include <watchdog.h>
  47. #include <asm/ppc4xx-intvec.h>
  48. #ifdef CONFIG_SERIAL_MULTI
  49. #include <serial.h>
  50. #endif
  51. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  52. #include <malloc.h>
  53. #endif
  54. DECLARE_GLOBAL_DATA_PTR;
  55. #if defined(CONFIG_405GP) || defined(CONFIG_405CR) || \
  56. defined(CONFIG_405EP) || defined(CONFIG_405EZ) || \
  57. defined(CONFIG_405EX) || defined(CONFIG_440)
  58. #if defined(CONFIG_440)
  59. #if defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
  60. defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
  61. #define UART0_BASE CFG_PERIPHERAL_BASE + 0x00000300
  62. #define UART1_BASE CFG_PERIPHERAL_BASE + 0x00000400
  63. #else
  64. #define UART0_BASE CFG_PERIPHERAL_BASE + 0x00000200
  65. #define UART1_BASE CFG_PERIPHERAL_BASE + 0x00000300
  66. #endif
  67. #if defined(CONFIG_440SP) || defined(CONFIG_440SPE)
  68. #define UART2_BASE CFG_PERIPHERAL_BASE + 0x00000600
  69. #endif
  70. #if defined(CONFIG_440GP)
  71. #define CR0_MASK 0x3fff0000
  72. #define CR0_EXTCLK_ENA 0x00600000
  73. #define CR0_UDIV_POS 16
  74. #define UDIV_SUBTRACT 1
  75. #define UART0_SDR cntrl0
  76. #define MFREG(a, d) d = mfdcr(a)
  77. #define MTREG(a, d) mtdcr(a, d)
  78. #else /* #if defined(CONFIG_440GP) */
  79. /* all other 440 PPC's access clock divider via sdr register */
  80. #define CR0_MASK 0xdfffffff
  81. #define CR0_EXTCLK_ENA 0x00800000
  82. #define CR0_UDIV_POS 0
  83. #define UDIV_SUBTRACT 0
  84. #define UART0_SDR sdr_uart0
  85. #define UART1_SDR sdr_uart1
  86. #if defined(CONFIG_440EP) || defined(CONFIG_440EPx) || \
  87. defined(CONFIG_440GR) || defined(CONFIG_440GRx) || \
  88. defined(CONFIG_440SP) || defined(CONFIG_440SPe)
  89. #define UART2_SDR sdr_uart2
  90. #endif
  91. #if defined(CONFIG_440EP) || defined(CONFIG_440EPx) || \
  92. defined(CONFIG_440GR) || defined(CONFIG_440GRx)
  93. #define UART3_SDR sdr_uart3
  94. #endif
  95. #define MFREG(a, d) mfsdr(a, d)
  96. #define MTREG(a, d) mtsdr(a, d)
  97. #endif /* #if defined(CONFIG_440GP) */
  98. #elif defined(CONFIG_405EP) || defined(CONFIG_405EZ)
  99. #define UART0_BASE 0xef600300
  100. #define UART1_BASE 0xef600400
  101. #define UCR0_MASK 0x0000007f
  102. #define UCR1_MASK 0x00007f00
  103. #define UCR0_UDIV_POS 0
  104. #define UCR1_UDIV_POS 8
  105. #define UDIV_MAX 127
  106. #elif defined(CONFIG_405EX)
  107. #define UART0_BASE 0xef600200
  108. #define UART1_BASE 0xef600300
  109. #define CR0_MASK 0x000000ff
  110. #define CR0_EXTCLK_ENA 0x00800000
  111. #define CR0_UDIV_POS 0
  112. #define UDIV_SUBTRACT 0
  113. #define UART0_SDR sdr_uart0
  114. #define UART1_SDR sdr_uart1
  115. #else /* CONFIG_405GP || CONFIG_405CR */
  116. #define UART0_BASE 0xef600300
  117. #define UART1_BASE 0xef600400
  118. #define CR0_MASK 0x00001fff
  119. #define CR0_EXTCLK_ENA 0x000000c0
  120. #define CR0_UDIV_POS 1
  121. #define UDIV_MAX 32
  122. #endif
  123. /* using serial port 0 or 1 as U-Boot console ? */
  124. #if defined(CONFIG_UART1_CONSOLE)
  125. #define ACTING_UART0_BASE UART1_BASE
  126. #define ACTING_UART1_BASE UART0_BASE
  127. #else
  128. #define ACTING_UART0_BASE UART0_BASE
  129. #define ACTING_UART1_BASE UART1_BASE
  130. #endif
  131. #if defined(CONFIG_405EP) && defined(CFG_EXT_SERIAL_CLOCK)
  132. #error "External serial clock not supported on AMCC PPC405EP!"
  133. #endif
  134. #define UART_RBR 0x00
  135. #define UART_THR 0x00
  136. #define UART_IER 0x01
  137. #define UART_IIR 0x02
  138. #define UART_FCR 0x02
  139. #define UART_LCR 0x03
  140. #define UART_MCR 0x04
  141. #define UART_LSR 0x05
  142. #define UART_MSR 0x06
  143. #define UART_SCR 0x07
  144. #define UART_DLL 0x00
  145. #define UART_DLM 0x01
  146. /*-----------------------------------------------------------------------------+
  147. | Line Status Register.
  148. +-----------------------------------------------------------------------------*/
  149. #define asyncLSRDataReady1 0x01
  150. #define asyncLSROverrunError1 0x02
  151. #define asyncLSRParityError1 0x04
  152. #define asyncLSRFramingError1 0x08
  153. #define asyncLSRBreakInterrupt1 0x10
  154. #define asyncLSRTxHoldEmpty1 0x20
  155. #define asyncLSRTxShiftEmpty1 0x40
  156. #define asyncLSRRxFifoError1 0x80
  157. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  158. /*-----------------------------------------------------------------------------+
  159. | Fifo
  160. +-----------------------------------------------------------------------------*/
  161. typedef struct {
  162. char *rx_buffer;
  163. ulong rx_put;
  164. ulong rx_get;
  165. } serial_buffer_t;
  166. volatile static serial_buffer_t buf_info;
  167. #endif
  168. static void serial_init_common(u32 base, u32 udiv, u16 bdiv)
  169. {
  170. PPC4xx_SYS_INFO sys_info;
  171. u8 val;
  172. get_sys_info(&sys_info);
  173. /* Correct UART frequency in bd-info struct now that
  174. * the UART divisor is available
  175. */
  176. #ifdef CFG_EXT_SERIAL_CLOCK
  177. gd->uart_clk = CFG_EXT_SERIAL_CLOCK;
  178. #else
  179. gd->uart_clk = sys_info.freqUART / udiv;
  180. #endif
  181. out_8((u8 *)base + UART_LCR, 0x80); /* set DLAB bit */
  182. out_8((u8 *)base + UART_DLL, bdiv); /* set baudrate divisor */
  183. out_8((u8 *)base + UART_DLM, bdiv >> 8); /* set baudrate divisor */
  184. out_8((u8 *)base + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  185. out_8((u8 *)base + UART_FCR, 0x00); /* disable FIFO */
  186. out_8((u8 *)base + UART_MCR, 0x00); /* no modem control DTR RTS */
  187. val = in_8((u8 *)base + UART_LSR); /* clear line status */
  188. val = in_8((u8 *)base + UART_RBR); /* read receive buffer */
  189. out_8((u8 *)base + UART_SCR, 0x00); /* set scratchpad */
  190. out_8((u8 *)base + UART_IER, 0x00); /* set interrupt enable reg */
  191. }
  192. #if (defined(CONFIG_440) || defined(CONFIG_405EX)) && \
  193. !defined(CFG_EXT_SERIAL_CLOCK)
  194. static void serial_divs (int baudrate, unsigned long *pudiv,
  195. unsigned short *pbdiv)
  196. {
  197. sys_info_t sysinfo;
  198. unsigned long div; /* total divisor udiv * bdiv */
  199. unsigned long umin; /* minimum udiv */
  200. unsigned short diff; /* smallest diff */
  201. unsigned long udiv; /* best udiv */
  202. unsigned short idiff; /* current diff */
  203. unsigned short ibdiv; /* current bdiv */
  204. unsigned long i;
  205. unsigned long est; /* current estimate */
  206. get_sys_info(&sysinfo);
  207. udiv = 32; /* Assume lowest possible serial clk */
  208. div = sysinfo.freqPLB / (16 * baudrate); /* total divisor */
  209. umin = sysinfo.pllOpbDiv << 1; /* 2 x OPB divisor */
  210. diff = 32; /* highest possible */
  211. /* i is the test udiv value -- start with the largest
  212. * possible (32) to minimize serial clock and constrain
  213. * search to umin.
  214. */
  215. for (i = 32; i > umin; i--) {
  216. ibdiv = div / i;
  217. est = i * ibdiv;
  218. idiff = (est > div) ? (est-div) : (div-est);
  219. if (idiff == 0) {
  220. udiv = i;
  221. break; /* can't do better */
  222. } else if (idiff < diff) {
  223. udiv = i; /* best so far */
  224. diff = idiff; /* update lowest diff*/
  225. }
  226. }
  227. *pudiv = udiv;
  228. *pbdiv = div / udiv;
  229. }
  230. #elif defined(CONFIG_405EZ)
  231. static void serial_divs (int baudrate, unsigned long *pudiv,
  232. unsigned short *pbdiv)
  233. {
  234. sys_info_t sysinfo;
  235. unsigned long div; /* total divisor udiv * bdiv */
  236. unsigned long umin; /* minimum udiv */
  237. unsigned short diff; /* smallest diff */
  238. unsigned long udiv; /* best udiv */
  239. unsigned short idiff; /* current diff */
  240. unsigned short ibdiv; /* current bdiv */
  241. unsigned long i;
  242. unsigned long est; /* current estimate */
  243. unsigned long plloutb;
  244. unsigned long cpr_pllc;
  245. u32 reg;
  246. /* check the pll feedback source */
  247. mfcpr(cprpllc, cpr_pllc);
  248. get_sys_info(&sysinfo);
  249. plloutb = ((CONFIG_SYS_CLK_FREQ * ((cpr_pllc & PLLC_SRC_MASK) ?
  250. sysinfo.pllFwdDivB : sysinfo.pllFwdDiv) *
  251. sysinfo.pllFbkDiv) / sysinfo.pllFwdDivB);
  252. udiv = 256; /* Assume lowest possible serial clk */
  253. div = plloutb / (16 * baudrate); /* total divisor */
  254. umin = (plloutb / get_OPB_freq()) << 1; /* 2 x OPB divisor */
  255. diff = 256; /* highest possible */
  256. /* i is the test udiv value -- start with the largest
  257. * possible (256) to minimize serial clock and constrain
  258. * search to umin.
  259. */
  260. for (i = 256; i > umin; i--) {
  261. ibdiv = div / i;
  262. est = i * ibdiv;
  263. idiff = (est > div) ? (est-div) : (div-est);
  264. if (idiff == 0) {
  265. udiv = i;
  266. break; /* can't do better */
  267. } else if (idiff < diff) {
  268. udiv = i; /* best so far */
  269. diff = idiff; /* update lowest diff*/
  270. }
  271. }
  272. *pudiv = udiv;
  273. mfcpr(cprperd0, reg);
  274. reg &= ~0x0000ffff;
  275. reg |= ((udiv - 0) << 8) | (udiv - 0);
  276. mtcpr(cprperd0, reg);
  277. *pbdiv = div / udiv;
  278. }
  279. #endif /* defined(CONFIG_440) && !defined(CFG_EXT_SERIAL_CLK) */
  280. /*
  281. * Minimal serial functions needed to use one of the SMC ports
  282. * as serial console interface.
  283. */
  284. #if defined(CONFIG_440)
  285. int serial_init_dev(unsigned long base)
  286. {
  287. unsigned long reg;
  288. unsigned long udiv;
  289. unsigned short bdiv;
  290. #ifdef CFG_EXT_SERIAL_CLOCK
  291. unsigned long tmp;
  292. #endif
  293. MFREG(UART0_SDR, reg);
  294. reg &= ~CR0_MASK;
  295. #ifdef CFG_EXT_SERIAL_CLOCK
  296. reg |= CR0_EXTCLK_ENA;
  297. udiv = 1;
  298. tmp = gd->baudrate * 16;
  299. bdiv = (CFG_EXT_SERIAL_CLOCK + tmp / 2) / tmp;
  300. #else
  301. /* For 440, the cpu clock is on divider chain A, UART on divider
  302. * chain B ... so cpu clock is irrelevant. Get the "optimized"
  303. * values that are subject to the 1/2 opb clock constraint
  304. */
  305. serial_divs (gd->baudrate, &udiv, &bdiv);
  306. #endif
  307. reg |= (udiv - UDIV_SUBTRACT) << CR0_UDIV_POS; /* set the UART divisor */
  308. /*
  309. * Configure input clock to baudrate generator for all
  310. * available serial ports here
  311. */
  312. MTREG(UART0_SDR, reg);
  313. #if defined(UART1_SDR)
  314. MTREG(UART1_SDR, reg);
  315. #endif
  316. #if defined(UART2_SDR)
  317. MTREG(UART2_SDR, reg);
  318. #endif
  319. #if defined(UART3_SDR)
  320. MTREG(UART3_SDR, reg);
  321. #endif
  322. serial_init_common(base, udiv, bdiv);
  323. return (0);
  324. }
  325. #else /* !defined(CONFIG_440) */
  326. int serial_init_dev (unsigned long base)
  327. {
  328. unsigned long reg;
  329. unsigned long tmp;
  330. unsigned long clk;
  331. unsigned long udiv;
  332. unsigned short bdiv;
  333. #ifdef CONFIG_405EX
  334. clk = tmp = 0;
  335. mfsdr(UART0_SDR, reg);
  336. reg &= ~CR0_MASK;
  337. #ifdef CFG_EXT_SERIAL_CLOCK
  338. reg |= CR0_EXTCLK_ENA;
  339. udiv = 1;
  340. tmp = gd->baudrate * 16;
  341. bdiv = (CFG_EXT_SERIAL_CLOCK + tmp / 2) / tmp;
  342. #else
  343. serial_divs(gd->baudrate, &udiv, &bdiv);
  344. #endif
  345. reg |= (udiv - UDIV_SUBTRACT) << CR0_UDIV_POS; /* set the UART divisor */
  346. /*
  347. * Configure input clock to baudrate generator for all
  348. * available serial ports here
  349. */
  350. mtsdr(UART0_SDR, reg);
  351. #if defined(UART1_SDR)
  352. mtsdr(UART1_SDR, reg);
  353. #endif
  354. #elif defined(CONFIG_405EZ)
  355. serial_divs(gd->baudrate, &udiv, &bdiv);
  356. clk = tmp = reg = 0;
  357. #else
  358. #ifdef CONFIG_405EP
  359. reg = mfdcr(cpc0_ucr) & ~(UCR0_MASK | UCR1_MASK);
  360. clk = gd->cpu_clk;
  361. tmp = CFG_BASE_BAUD * 16;
  362. udiv = (clk + tmp / 2) / tmp;
  363. if (udiv > UDIV_MAX) /* max. n bits for udiv */
  364. udiv = UDIV_MAX;
  365. reg |= (udiv) << UCR0_UDIV_POS; /* set the UART divisor */
  366. reg |= (udiv) << UCR1_UDIV_POS; /* set the UART divisor */
  367. mtdcr (cpc0_ucr, reg);
  368. #else /* CONFIG_405EP */
  369. reg = mfdcr(cntrl0) & ~CR0_MASK;
  370. #ifdef CFG_EXT_SERIAL_CLOCK
  371. clk = CFG_EXT_SERIAL_CLOCK;
  372. udiv = 1;
  373. reg |= CR0_EXTCLK_ENA;
  374. #else
  375. clk = gd->cpu_clk;
  376. #ifdef CFG_405_UART_ERRATA_59
  377. udiv = 31; /* Errata 59: stuck at 31 */
  378. #else
  379. tmp = CFG_BASE_BAUD * 16;
  380. udiv = (clk + tmp / 2) / tmp;
  381. if (udiv > UDIV_MAX) /* max. n bits for udiv */
  382. udiv = UDIV_MAX;
  383. #endif
  384. #endif
  385. reg |= (udiv - 1) << CR0_UDIV_POS; /* set the UART divisor */
  386. mtdcr (cntrl0, reg);
  387. #endif /* CONFIG_405EP */
  388. tmp = gd->baudrate * udiv * 16;
  389. bdiv = (clk + tmp / 2) / tmp;
  390. #endif /* CONFIG_405EX */
  391. serial_init_common(base, udiv, bdiv);
  392. return (0);
  393. }
  394. #endif /* if defined(CONFIG_440) */
  395. void serial_setbrg_dev(unsigned long base)
  396. {
  397. serial_init_dev(base);
  398. }
  399. void serial_putc_dev(unsigned long base, const char c)
  400. {
  401. int i;
  402. if (c == '\n')
  403. serial_putc_dev(base, '\r');
  404. /* check THRE bit, wait for transmiter available */
  405. for (i = 1; i < 3500; i++) {
  406. if ((in_8((u8 *)base + UART_LSR) & 0x20) == 0x20)
  407. break;
  408. udelay (100);
  409. }
  410. out_8((u8 *)base + UART_THR, c); /* put character out */
  411. }
  412. void serial_puts_dev (unsigned long base, const char *s)
  413. {
  414. while (*s)
  415. serial_putc_dev (base, *s++);
  416. }
  417. int serial_getc_dev (unsigned long base)
  418. {
  419. unsigned char status = 0;
  420. while (1) {
  421. #if defined(CONFIG_HW_WATCHDOG)
  422. WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */
  423. #endif /* CONFIG_HW_WATCHDOG */
  424. status = in_8((u8 *)base + UART_LSR);
  425. if ((status & asyncLSRDataReady1) != 0x0)
  426. break;
  427. if ((status & ( asyncLSRFramingError1 |
  428. asyncLSROverrunError1 |
  429. asyncLSRParityError1 |
  430. asyncLSRBreakInterrupt1 )) != 0) {
  431. out_8((u8 *)base + UART_LSR,
  432. asyncLSRFramingError1 |
  433. asyncLSROverrunError1 |
  434. asyncLSRParityError1 |
  435. asyncLSRBreakInterrupt1);
  436. }
  437. }
  438. return (0x000000ff & (int) in_8((u8 *)base));
  439. }
  440. int serial_tstc_dev (unsigned long base)
  441. {
  442. unsigned char status;
  443. status = in_8((u8 *)base + UART_LSR);
  444. if ((status & asyncLSRDataReady1) != 0x0)
  445. return (1);
  446. if ((status & ( asyncLSRFramingError1 |
  447. asyncLSROverrunError1 |
  448. asyncLSRParityError1 |
  449. asyncLSRBreakInterrupt1 )) != 0) {
  450. out_8((u8 *)base + UART_LSR,
  451. asyncLSRFramingError1 |
  452. asyncLSROverrunError1 |
  453. asyncLSRParityError1 |
  454. asyncLSRBreakInterrupt1);
  455. }
  456. return 0;
  457. }
  458. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  459. void serial_isr (void *arg)
  460. {
  461. int space;
  462. int c;
  463. const int rx_get = buf_info.rx_get;
  464. int rx_put = buf_info.rx_put;
  465. if (rx_get <= rx_put)
  466. space = CONFIG_SERIAL_SOFTWARE_FIFO - (rx_put - rx_get);
  467. else
  468. space = rx_get - rx_put;
  469. while (serial_tstc_dev (ACTING_UART0_BASE)) {
  470. c = serial_getc_dev (ACTING_UART0_BASE);
  471. if (space) {
  472. buf_info.rx_buffer[rx_put++] = c;
  473. space--;
  474. }
  475. if (rx_put == CONFIG_SERIAL_SOFTWARE_FIFO)
  476. rx_put = 0;
  477. if (space < CONFIG_SERIAL_SOFTWARE_FIFO / 4) {
  478. /* Stop flow by setting RTS inactive */
  479. out_8((u8 *)ACTING_UART0_BASE + UART_MCR,
  480. in_8((u8 *)ACTING_UART0_BASE + UART_MCR) &
  481. (0xFF ^ 0x02));
  482. }
  483. }
  484. buf_info.rx_put = rx_put;
  485. }
  486. void serial_buffered_init (void)
  487. {
  488. serial_puts ("Switching to interrupt driven serial input mode.\n");
  489. buf_info.rx_buffer = malloc (CONFIG_SERIAL_SOFTWARE_FIFO);
  490. buf_info.rx_put = 0;
  491. buf_info.rx_get = 0;
  492. if (in_8((u8 *)ACTING_UART0_BASE + UART_MSR) & 0x10)
  493. serial_puts ("Check CTS signal present on serial port: OK.\n");
  494. else
  495. serial_puts ("WARNING: CTS signal not present on serial port.\n");
  496. irq_install_handler ( VECNUM_U0 /*UART0 */ /*int vec */ ,
  497. serial_isr /*interrupt_handler_t *handler */ ,
  498. (void *) &buf_info /*void *arg */ );
  499. /* Enable "RX Data Available" Interrupt on UART */
  500. out_8(ACTING_UART0_BASE + UART_IER, 0x01);
  501. /* Set DTR active */
  502. out_8(ACTING_UART0_BASE + UART_MCR,
  503. in_8((u8 *)ACTING_UART0_BASE + UART_MCR) | 0x01);
  504. /* Start flow by setting RTS active */
  505. out_8(ACTING_UART0_BASE + UART_MCR,
  506. in_8((u8 *)ACTING_UART0_BASE + UART_MCR) | 0x02);
  507. /* Setup UART FIFO: RX trigger level: 4 byte, Enable FIFO */
  508. out_8(ACTING_UART0_BASE + UART_FCR, (1 << 6) | 1);
  509. }
  510. void serial_buffered_putc (const char c)
  511. {
  512. /* Wait for CTS */
  513. #if defined(CONFIG_HW_WATCHDOG)
  514. while (!(in_8((u8 *)ACTING_UART0_BASE + UART_MSR) & 0x10))
  515. WATCHDOG_RESET ();
  516. #else
  517. while (!(in_8((u8 *)ACTING_UART0_BASE + UART_MSR) & 0x10));
  518. #endif
  519. serial_putc (c);
  520. }
  521. void serial_buffered_puts (const char *s)
  522. {
  523. serial_puts (s);
  524. }
  525. int serial_buffered_getc (void)
  526. {
  527. int space;
  528. int c;
  529. int rx_get = buf_info.rx_get;
  530. int rx_put;
  531. #if defined(CONFIG_HW_WATCHDOG)
  532. while (rx_get == buf_info.rx_put)
  533. WATCHDOG_RESET ();
  534. #else
  535. while (rx_get == buf_info.rx_put);
  536. #endif
  537. c = buf_info.rx_buffer[rx_get++];
  538. if (rx_get == CONFIG_SERIAL_SOFTWARE_FIFO)
  539. rx_get = 0;
  540. buf_info.rx_get = rx_get;
  541. rx_put = buf_info.rx_put;
  542. if (rx_get <= rx_put)
  543. space = CONFIG_SERIAL_SOFTWARE_FIFO - (rx_put - rx_get);
  544. else
  545. space = rx_get - rx_put;
  546. if (space > CONFIG_SERIAL_SOFTWARE_FIFO / 2) {
  547. /* Start flow by setting RTS active */
  548. out_8(ACTING_UART0_BASE + UART_MCR,
  549. in_8((u8 *)ACTING_UART0_BASE + UART_MCR) | 0x02);
  550. }
  551. return c;
  552. }
  553. int serial_buffered_tstc (void)
  554. {
  555. return (buf_info.rx_get != buf_info.rx_put) ? 1 : 0;
  556. }
  557. #endif /* CONFIG_SERIAL_SOFTWARE_FIFO */
  558. #if defined(CONFIG_CMD_KGDB)
  559. /*
  560. AS HARNOIS : according to CONFIG_KGDB_SER_INDEX kgdb uses serial port
  561. number 0 or number 1
  562. - if CONFIG_KGDB_SER_INDEX = 1 => serial port number 0 :
  563. configuration has been already done
  564. - if CONFIG_KGDB_SER_INDEX = 2 => serial port number 1 :
  565. configure port 1 for serial I/O with rate = CONFIG_KGDB_BAUDRATE
  566. */
  567. #if (CONFIG_KGDB_SER_INDEX & 2)
  568. void kgdb_serial_init (void)
  569. {
  570. u8 val;
  571. u16 br_reg;
  572. get_clocks ();
  573. br_reg = (((((gd->cpu_clk / 16) / 18) * 10) / CONFIG_KGDB_BAUDRATE) +
  574. 5) / 10;
  575. /*
  576. * Init onboard 16550 UART
  577. */
  578. out_8((u8 *)ACTING_UART1_BASE + UART_LCR, 0x80); /* set DLAB bit */
  579. out_8((u8 *)ACTING_UART1_BASE + UART_DLL, (br_reg & 0x00ff)); /* set divisor for 9600 baud */
  580. out_8((u8 *)ACTING_UART1_BASE + UART_DLM, ((br_reg & 0xff00) >> 8)); /* set divisor for 9600 baud */
  581. out_8((u8 *)ACTING_UART1_BASE + UART_LCR, 0x03); /* line control 8 bits no parity */
  582. out_8((u8 *)ACTING_UART1_BASE + UART_FCR, 0x00); /* disable FIFO */
  583. out_8((u8 *)ACTING_UART1_BASE + UART_MCR, 0x00); /* no modem control DTR RTS */
  584. val = in_8((u8 *)ACTING_UART1_BASE + UART_LSR); /* clear line status */
  585. val = in_8((u8 *)ACTING_UART1_BASE + UART_RBR); /* read receive buffer */
  586. out_8((u8 *)ACTING_UART1_BASE + UART_SCR, 0x00); /* set scratchpad */
  587. out_8((u8 *)ACTING_UART1_BASE + UART_IER, 0x00); /* set interrupt enable reg */
  588. }
  589. void putDebugChar (const char c)
  590. {
  591. if (c == '\n')
  592. serial_putc ('\r');
  593. out_8((u8 *)ACTING_UART1_BASE + UART_THR, c); /* put character out */
  594. /* check THRE bit, wait for transfer done */
  595. while ((in_8((u8 *)ACTING_UART1_BASE + UART_LSR) & 0x20) != 0x20);
  596. }
  597. void putDebugStr (const char *s)
  598. {
  599. while (*s)
  600. serial_putc (*s++);
  601. }
  602. int getDebugChar (void)
  603. {
  604. unsigned char status = 0;
  605. while (1) {
  606. status = in_8((u8 *)ACTING_UART1_BASE + UART_LSR);
  607. if ((status & asyncLSRDataReady1) != 0x0)
  608. break;
  609. if ((status & (asyncLSRFramingError1 |
  610. asyncLSROverrunError1 |
  611. asyncLSRParityError1 |
  612. asyncLSRBreakInterrupt1 )) != 0) {
  613. out_8((u8 *)ACTING_UART1_BASE + UART_LSR,
  614. asyncLSRFramingError1 |
  615. asyncLSROverrunError1 |
  616. asyncLSRParityError1 |
  617. asyncLSRBreakInterrupt1);
  618. }
  619. }
  620. return (0x000000ff & (int) in_8((u8 *)ACTING_UART1_BASE));
  621. }
  622. void kgdb_interruptible (int yes)
  623. {
  624. return;
  625. }
  626. #else /* ! (CONFIG_KGDB_SER_INDEX & 2) */
  627. void kgdb_serial_init (void)
  628. {
  629. serial_printf ("[on serial] ");
  630. }
  631. void putDebugChar (int c)
  632. {
  633. serial_putc (c);
  634. }
  635. void putDebugStr (const char *str)
  636. {
  637. serial_puts (str);
  638. }
  639. int getDebugChar (void)
  640. {
  641. return serial_getc ();
  642. }
  643. void kgdb_interruptible (int yes)
  644. {
  645. return;
  646. }
  647. #endif /* (CONFIG_KGDB_SER_INDEX & 2) */
  648. #endif
  649. #if defined(CONFIG_SERIAL_MULTI)
  650. int serial0_init(void)
  651. {
  652. return (serial_init_dev(UART0_BASE));
  653. }
  654. int serial1_init(void)
  655. {
  656. return (serial_init_dev(UART1_BASE));
  657. }
  658. void serial0_setbrg (void)
  659. {
  660. serial_setbrg_dev(UART0_BASE);
  661. }
  662. void serial1_setbrg (void)
  663. {
  664. serial_setbrg_dev(UART1_BASE);
  665. }
  666. void serial0_putc(const char c)
  667. {
  668. serial_putc_dev(UART0_BASE,c);
  669. }
  670. void serial1_putc(const char c)
  671. {
  672. serial_putc_dev(UART1_BASE, c);
  673. }
  674. void serial0_puts(const char *s)
  675. {
  676. serial_puts_dev(UART0_BASE, s);
  677. }
  678. void serial1_puts(const char *s)
  679. {
  680. serial_puts_dev(UART1_BASE, s);
  681. }
  682. int serial0_getc(void)
  683. {
  684. return(serial_getc_dev(UART0_BASE));
  685. }
  686. int serial1_getc(void)
  687. {
  688. return(serial_getc_dev(UART1_BASE));
  689. }
  690. int serial0_tstc(void)
  691. {
  692. return (serial_tstc_dev(UART0_BASE));
  693. }
  694. int serial1_tstc(void)
  695. {
  696. return (serial_tstc_dev(UART1_BASE));
  697. }
  698. struct serial_device serial0_device =
  699. {
  700. "serial0",
  701. "UART0",
  702. serial0_init,
  703. serial0_setbrg,
  704. serial0_getc,
  705. serial0_tstc,
  706. serial0_putc,
  707. serial0_puts,
  708. };
  709. struct serial_device serial1_device =
  710. {
  711. "serial1",
  712. "UART1",
  713. serial1_init,
  714. serial1_setbrg,
  715. serial1_getc,
  716. serial1_tstc,
  717. serial1_putc,
  718. serial1_puts,
  719. };
  720. #else
  721. /*
  722. * Wrapper functions
  723. */
  724. int serial_init(void)
  725. {
  726. return serial_init_dev(ACTING_UART0_BASE);
  727. }
  728. void serial_setbrg(void)
  729. {
  730. serial_setbrg_dev(ACTING_UART0_BASE);
  731. }
  732. void serial_putc(const char c)
  733. {
  734. serial_putc_dev(ACTING_UART0_BASE, c);
  735. }
  736. void serial_puts(const char *s)
  737. {
  738. serial_puts_dev(ACTING_UART0_BASE, s);
  739. }
  740. int serial_getc(void)
  741. {
  742. return serial_getc_dev(ACTING_UART0_BASE);
  743. }
  744. int serial_tstc(void)
  745. {
  746. return serial_tstc_dev(ACTING_UART0_BASE);
  747. }
  748. #endif /* CONFIG_SERIAL_MULTI */
  749. #endif /* CONFIG_405GP || CONFIG_405CR */