4xx_uart.c 22 KB

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