serial.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * (C) Copyright 2002
  3. * Daniel Engström, Omicron Ceti AB, daniel@omicron.se
  4. *
  5. * (C) Copyright 2000
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. /*------------------------------------------------------------------------------+ */
  27. /*
  28. * This source code has been made available to you by IBM on an AS-IS
  29. * basis. Anyone receiving this source is licensed under IBM
  30. * copyrights to use it in any way he or she deems fit, including
  31. * copying it, modifying it, compiling it, and redistributing it either
  32. * with or without modifications. No license under IBM patents or
  33. * patent applications is to be implied by the copyright license.
  34. *
  35. * Any user of this software should understand that IBM cannot provide
  36. * technical support for this software and will not be responsible for
  37. * any consequences resulting from the use of this software.
  38. *
  39. * Any person who transfers this source code or any derivative work
  40. * must include the IBM copyright notice, this paragraph, and the
  41. * preceding two paragraphs in the transferred software.
  42. *
  43. * COPYRIGHT I B M CORPORATION 1995
  44. * LICENSED MATERIAL - PROGRAM PROPERTY OF I B M
  45. */
  46. /*------------------------------------------------------------------------------- */
  47. #include <common.h>
  48. #include <watchdog.h>
  49. #include <asm/io.h>
  50. #include <asm/ibmpc.h>
  51. #if CONFIG_SERIAL_SOFTWARE_FIFO
  52. #include <malloc.h>
  53. #endif
  54. #define UART_RBR 0x00
  55. #define UART_THR 0x00
  56. #define UART_IER 0x01
  57. #define UART_IIR 0x02
  58. #define UART_FCR 0x02
  59. #define UART_LCR 0x03
  60. #define UART_MCR 0x04
  61. #define UART_LSR 0x05
  62. #define UART_MSR 0x06
  63. #define UART_SCR 0x07
  64. #define UART_DLL 0x00
  65. #define UART_DLM 0x01
  66. /*-----------------------------------------------------------------------------+
  67. | Line Status Register.
  68. +-----------------------------------------------------------------------------*/
  69. #define asyncLSRDataReady1 0x01
  70. #define asyncLSROverrunError1 0x02
  71. #define asyncLSRParityError1 0x04
  72. #define asyncLSRFramingError1 0x08
  73. #define asyncLSRBreakInterrupt1 0x10
  74. #define asyncLSRTxHoldEmpty1 0x20
  75. #define asyncLSRTxShiftEmpty1 0x40
  76. #define asyncLSRRxFifoError1 0x80
  77. #if CONFIG_SERIAL_SOFTWARE_FIFO
  78. /*-----------------------------------------------------------------------------+
  79. | Fifo
  80. +-----------------------------------------------------------------------------*/
  81. typedef struct {
  82. char *rx_buffer;
  83. ulong rx_put;
  84. ulong rx_get;
  85. } serial_buffer_t;
  86. volatile static serial_buffer_t buf_info;
  87. #endif
  88. static int serial_div (int baudrate )
  89. {
  90. switch (baudrate) {
  91. case 1200:
  92. return 96;
  93. case 9600:
  94. return 12;
  95. case 19200:
  96. return 6;
  97. case 38400:
  98. return 3;
  99. case 57600:
  100. return 2;
  101. case 115200:
  102. return 1;
  103. }
  104. hang ();
  105. }
  106. /*
  107. * Minimal serial functions needed to use one of the SMC ports
  108. * as serial console interface.
  109. */
  110. int serial_init (void)
  111. {
  112. DECLARE_GLOBAL_DATA_PTR;
  113. volatile char val;
  114. int bdiv = serial_div(gd->baudrate);
  115. outb(0x80, UART0_BASE + UART_LCR); /* set DLAB bit */
  116. outb(bdiv, UART0_BASE + UART_DLL); /* set baudrate divisor */
  117. outb(bdiv >> 8, UART0_BASE + UART_DLM);/* set baudrate divisor */
  118. outb(0x03, UART0_BASE + UART_LCR); /* clear DLAB; set 8 bits, no parity */
  119. outb(0x00, UART0_BASE + UART_FCR); /* disable FIFO */
  120. outb(0x00, UART0_BASE + UART_MCR); /* no modem control DTR RTS */
  121. val = inb(UART0_BASE + UART_LSR); /* clear line status */
  122. val = inb(UART0_BASE + UART_RBR); /* read receive buffer */
  123. outb(0x00, UART0_BASE + UART_SCR); /* set scratchpad */
  124. outb(0x00, UART0_BASE + UART_IER); /* set interrupt enable reg */
  125. return (0);
  126. }
  127. void serial_setbrg (void)
  128. {
  129. DECLARE_GLOBAL_DATA_PTR;
  130. unsigned short bdiv;
  131. bdiv = serial_div (gd->baudrate);
  132. outb(0x80, UART0_BASE + UART_LCR); /* set DLAB bit */
  133. outb(bdiv&0xff, UART0_BASE + UART_DLL); /* set baudrate divisor */
  134. outb(bdiv >> 8, UART0_BASE + UART_DLM);/* set baudrate divisor */
  135. outb(0x03, UART0_BASE + UART_LCR); /* clear DLAB; set 8 bits, no parity */
  136. }
  137. void serial_putc (const char c)
  138. {
  139. int i;
  140. if (c == '\n')
  141. serial_putc ('\r');
  142. /* check THRE bit, wait for transmiter available */
  143. for (i = 1; i < 3500; i++) {
  144. if ((inb (UART0_BASE + UART_LSR) & 0x20) == 0x20)
  145. break;
  146. udelay (100);
  147. }
  148. outb(c, UART0_BASE + UART_THR); /* put character out */
  149. }
  150. void serial_puts (const char *s)
  151. {
  152. while (*s) {
  153. serial_putc (*s++);
  154. }
  155. }
  156. int serial_getc ()
  157. {
  158. unsigned char status = 0;
  159. while (1) {
  160. #if defined(CONFIG_HW_WATCHDOG)
  161. WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */
  162. #endif /* CONFIG_HW_WATCHDOG */
  163. status = inb (UART0_BASE + UART_LSR);
  164. if ((status & asyncLSRDataReady1) != 0x0) {
  165. break;
  166. }
  167. if ((status & ( asyncLSRFramingError1 |
  168. asyncLSROverrunError1 |
  169. asyncLSRParityError1 |
  170. asyncLSRBreakInterrupt1 )) != 0) {
  171. outb(asyncLSRFramingError1 |
  172. asyncLSROverrunError1 |
  173. asyncLSRParityError1 |
  174. asyncLSRBreakInterrupt1, UART0_BASE + UART_LSR);
  175. }
  176. }
  177. return (0x000000ff & (int) inb (UART0_BASE));
  178. }
  179. int serial_tstc ()
  180. {
  181. unsigned char status;
  182. status = inb (UART0_BASE + UART_LSR);
  183. if ((status & asyncLSRDataReady1) != 0x0) {
  184. return (1);
  185. }
  186. if ((status & ( asyncLSRFramingError1 |
  187. asyncLSROverrunError1 |
  188. asyncLSRParityError1 |
  189. asyncLSRBreakInterrupt1 )) != 0) {
  190. outb(asyncLSRFramingError1 |
  191. asyncLSROverrunError1 |
  192. asyncLSRParityError1 |
  193. asyncLSRBreakInterrupt1, UART0_BASE + UART_LSR);
  194. }
  195. return 0;
  196. }
  197. #if CONFIG_SERIAL_SOFTWARE_FIFO
  198. void serial_isr (void *arg)
  199. {
  200. int space;
  201. int c;
  202. const int rx_get = buf_info.rx_get;
  203. int rx_put = buf_info.rx_put;
  204. if (rx_get <= rx_put) {
  205. space = CONFIG_SERIAL_SOFTWARE_FIFO - (rx_put - rx_get);
  206. } else {
  207. space = rx_get - rx_put;
  208. }
  209. while (serial_tstc ()) {
  210. c = serial_getc ();
  211. if (space) {
  212. buf_info.rx_buffer[rx_put++] = c;
  213. space--;
  214. }
  215. if (rx_put == CONFIG_SERIAL_SOFTWARE_FIFO)
  216. rx_put = 0;
  217. if (space < CONFIG_SERIAL_SOFTWARE_FIFO / 4) {
  218. /* Stop flow by setting RTS inactive */
  219. outb(inb (UART0_BASE + UART_MCR) & (0xFF ^ 0x02),
  220. UART0_BASE + UART_MCR);
  221. }
  222. }
  223. buf_info.rx_put = rx_put;
  224. }
  225. void serial_buffered_init (void)
  226. {
  227. serial_puts ("Switching to interrupt driven serial input mode.\n");
  228. buf_info.rx_buffer = malloc (CONFIG_SERIAL_SOFTWARE_FIFO);
  229. buf_info.rx_put = 0;
  230. buf_info.rx_get = 0;
  231. if (inb (UART0_BASE + UART_MSR) & 0x10) {
  232. serial_puts ("Check CTS signal present on serial port: OK.\n");
  233. } else {
  234. serial_puts ("WARNING: CTS signal not present on serial port.\n");
  235. }
  236. irq_install_handler ( VECNUM_U0 /*UART0 *//*int vec */ ,
  237. serial_isr /*interrupt_handler_t *handler */ ,
  238. (void *) &buf_info /*void *arg */ );
  239. /* Enable "RX Data Available" Interrupt on UART */
  240. /* outb(inb(UART0_BASE + UART_IER) |0x01, UART0_BASE + UART_IER); */
  241. outb(0x01, UART0_BASE + UART_IER);
  242. /* Set DTR active */
  243. outb(inb (UART0_BASE + UART_MCR) | 0x01, UART0_BASE + UART_MCR);
  244. /* Start flow by setting RTS active */
  245. outb(inb (UART0_BASE + UART_MCR) | 0x02, UART0_BASE + UART_MCR);
  246. /* Setup UART FIFO: RX trigger level: 4 byte, Enable FIFO */
  247. outb((1 << 6) | 1, UART0_BASE + UART_FCR);
  248. }
  249. void serial_buffered_putc (const char c)
  250. {
  251. /* Wait for CTS */
  252. #if defined(CONFIG_HW_WATCHDOG)
  253. while (!(inb (UART0_BASE + UART_MSR) & 0x10))
  254. WATCHDOG_RESET ();
  255. #else
  256. while (!(inb (UART0_BASE + UART_MSR) & 0x10));
  257. #endif
  258. serial_putc (c);
  259. }
  260. void serial_buffered_puts (const char *s)
  261. {
  262. serial_puts (s);
  263. }
  264. int serial_buffered_getc (void)
  265. {
  266. int space;
  267. int c;
  268. int rx_get = buf_info.rx_get;
  269. int rx_put;
  270. #if defined(CONFIG_HW_WATCHDOG)
  271. while (rx_get == buf_info.rx_put)
  272. WATCHDOG_RESET ();
  273. #else
  274. while (rx_get == buf_info.rx_put);
  275. #endif
  276. c = buf_info.rx_buffer[rx_get++];
  277. if (rx_get == CONFIG_SERIAL_SOFTWARE_FIFO)
  278. rx_get = 0;
  279. buf_info.rx_get = rx_get;
  280. rx_put = buf_info.rx_put;
  281. if (rx_get <= rx_put) {
  282. space = CONFIG_SERIAL_SOFTWARE_FIFO - (rx_put - rx_get);
  283. } else {
  284. space = rx_get - rx_put;
  285. }
  286. if (space > CONFIG_SERIAL_SOFTWARE_FIFO / 2) {
  287. /* Start flow by setting RTS active */
  288. outb(inb (UART0_BASE + UART_MCR) | 0x02, UART0_BASE + UART_MCR);
  289. }
  290. return c;
  291. }
  292. int serial_buffered_tstc (void)
  293. {
  294. return (buf_info.rx_get != buf_info.rx_put) ? 1 : 0;
  295. }
  296. #endif /* CONFIG_SERIAL_SOFTWARE_FIFO */
  297. #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
  298. /*
  299. AS HARNOIS : according to CONFIG_KGDB_SER_INDEX kgdb uses serial port
  300. number 0 or number 1
  301. - if CONFIG_KGDB_SER_INDEX = 1 => serial port number 0 :
  302. configuration has been already done
  303. - if CONFIG_KGDB_SER_INDEX = 2 => serial port number 1 :
  304. configure port 1 for serial I/O with rate = CONFIG_KGDB_BAUDRATE
  305. */
  306. #if (CONFIG_KGDB_SER_INDEX & 2)
  307. void kgdb_serial_init (void)
  308. {
  309. DECLARE_GLOBAL_DATA_PTR;
  310. volatile char val;
  311. bdiv = serial_div (CONFIG_KGDB_BAUDRATE);
  312. /*
  313. * Init onboard 16550 UART
  314. */
  315. outb(0x80, UART1_BASE + UART_LCR); /* set DLAB bit */
  316. outb(bdiv & 0xff), UART1_BASE + UART_DLL); /* set divisor for 9600 baud */
  317. outb(bdiv >> 8), UART1_BASE + UART_DLM); /* set divisor for 9600 baud */
  318. outb(0x03, UART1_BASE + UART_LCR); /* line control 8 bits no parity */
  319. outb(0x00, UART1_BASE + UART_FCR); /* disable FIFO */
  320. outb(0x00, UART1_BASE + UART_MCR); /* no modem control DTR RTS */
  321. val = inb(UART1_BASE + UART_LSR); /* clear line status */
  322. val = inb(UART1_BASE + UART_RBR); /* read receive buffer */
  323. outb(0x00, UART1_BASE + UART_SCR); /* set scratchpad */
  324. outb(0x00, UART1_BASE + UART_IER); /* set interrupt enable reg */
  325. }
  326. void putDebugChar (const char c)
  327. {
  328. if (c == '\n')
  329. serial_putc ('\r');
  330. outb(c, UART1_BASE + UART_THR); /* put character out */
  331. /* check THRE bit, wait for transfer done */
  332. while ((inb(UART1_BASE + UART_LSR) & 0x20) != 0x20);
  333. }
  334. void putDebugStr (const char *s)
  335. {
  336. while (*s) {
  337. serial_putc(*s++);
  338. }
  339. }
  340. int getDebugChar (void)
  341. {
  342. unsigned char status = 0;
  343. while (1) {
  344. status = inb(UART1_BASE + UART_LSR);
  345. if ((status & asyncLSRDataReady1) != 0x0) {
  346. break;
  347. }
  348. if ((status & ( asyncLSRFramingError1 |
  349. asyncLSROverrunError1 |
  350. asyncLSRParityError1 |
  351. asyncLSRBreakInterrupt1 )) != 0) {
  352. outb(asyncLSRFramingError1 |
  353. asyncLSROverrunError1 |
  354. asyncLSRParityError1 |
  355. asyncLSRBreakInterrupt1, UART1_BASE + UART_LSR);
  356. }
  357. }
  358. return (0x000000ff & (int) inb(UART1_BASE));
  359. }
  360. void kgdb_interruptible (int yes)
  361. {
  362. return;
  363. }
  364. #else /* ! (CONFIG_KGDB_SER_INDEX & 2) */
  365. void kgdb_serial_init (void)
  366. {
  367. serial_printf ("[on serial] ");
  368. }
  369. void putDebugChar (int c)
  370. {
  371. serial_putc (c);
  372. }
  373. void putDebugStr (const char *str)
  374. {
  375. serial_puts (str);
  376. }
  377. int getDebugChar (void)
  378. {
  379. return serial_getc ();
  380. }
  381. void kgdb_interruptible (int yes)
  382. {
  383. return;
  384. }
  385. #endif /* (CONFIG_KGDB_SER_INDEX & 2) */
  386. #endif /* CFG_CMD_KGDB */