4xx_uart.c 22 KB

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