serial.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. #ifdef 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. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  78. /*-----------------------------------------------------------------------------+
  79. | Fifo
  80. +-----------------------------------------------------------------------------*/
  81. typedef struct {
  82. char *rx_buffer;
  83. ulong rx_put;
  84. ulong rx_get;
  85. int cts;
  86. } serial_buffer_t;
  87. volatile serial_buffer_t buf_info;
  88. static int serial_buffer_active=0;
  89. #endif
  90. static int serial_div(int baudrate)
  91. {
  92. switch (baudrate) {
  93. case 1200:
  94. return 96;
  95. case 9600:
  96. return 12;
  97. case 19200:
  98. return 6;
  99. case 38400:
  100. return 3;
  101. case 57600:
  102. return 2;
  103. case 115200:
  104. return 1;
  105. }
  106. return 12;
  107. }
  108. /*
  109. * Minimal serial functions needed to use one of the SMC ports
  110. * as serial console interface.
  111. */
  112. int serial_init(void)
  113. {
  114. DECLARE_GLOBAL_DATA_PTR;
  115. volatile char val;
  116. int bdiv = serial_div(gd->baudrate);
  117. outb(0x80, UART0_BASE + UART_LCR); /* set DLAB bit */
  118. outb(bdiv, UART0_BASE + UART_DLL); /* set baudrate divisor */
  119. outb(bdiv >> 8, UART0_BASE + UART_DLM);/* set baudrate divisor */
  120. outb(0x03, UART0_BASE + UART_LCR); /* clear DLAB; set 8 bits, no parity */
  121. outb(0x01, UART0_BASE + UART_FCR); /* enable FIFO */
  122. outb(0x0b, UART0_BASE + UART_MCR); /* Set DTR and RTS active */
  123. val = inb(UART0_BASE + UART_LSR); /* clear line status */
  124. val = inb(UART0_BASE + UART_RBR); /* read receive buffer */
  125. outb(0x00, UART0_BASE + UART_SCR); /* set scratchpad */
  126. outb(0x00, UART0_BASE + UART_IER); /* set interrupt enable reg */
  127. return 0;
  128. }
  129. void serial_setbrg(void)
  130. {
  131. DECLARE_GLOBAL_DATA_PTR;
  132. unsigned short bdiv;
  133. bdiv = serial_div(gd->baudrate);
  134. outb(0x80, UART0_BASE + UART_LCR); /* set DLAB bit */
  135. outb(bdiv&0xff, UART0_BASE + UART_DLL); /* set baudrate divisor */
  136. outb(bdiv >> 8, UART0_BASE + UART_DLM);/* set baudrate divisor */
  137. outb(0x03, UART0_BASE + UART_LCR); /* clear DLAB; set 8 bits, no parity */
  138. }
  139. void serial_putc(const char c)
  140. {
  141. int i;
  142. if (c == '\n')
  143. serial_putc ('\r');
  144. /* check THRE bit, wait for transmiter available */
  145. for (i = 1; i < 3500; i++) {
  146. if ((inb (UART0_BASE + UART_LSR) & 0x20) == 0x20) {
  147. break;
  148. }
  149. udelay(100);
  150. }
  151. outb(c, UART0_BASE + UART_THR); /* put character out */
  152. }
  153. void serial_puts(const char *s)
  154. {
  155. while (*s) {
  156. serial_putc(*s++);
  157. }
  158. }
  159. int serial_getc(void)
  160. {
  161. unsigned char status = 0;
  162. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  163. if (serial_buffer_active) {
  164. return serial_buffered_getc();
  165. }
  166. #endif
  167. while (1) {
  168. #if defined(CONFIG_HW_WATCHDOG)
  169. WATCHDOG_RESET(); /* Reset HW Watchdog, if needed */
  170. #endif /* CONFIG_HW_WATCHDOG */
  171. status = inb(UART0_BASE + UART_LSR);
  172. if ((status & asyncLSRDataReady1) != 0x0) {
  173. break;
  174. }
  175. if ((status & ( asyncLSRFramingError1 |
  176. asyncLSROverrunError1 |
  177. asyncLSRParityError1 |
  178. asyncLSRBreakInterrupt1 )) != 0) {
  179. outb(asyncLSRFramingError1 |
  180. asyncLSROverrunError1 |
  181. asyncLSRParityError1 |
  182. asyncLSRBreakInterrupt1, UART0_BASE + UART_LSR);
  183. }
  184. }
  185. return (0x000000ff & (int) inb (UART0_BASE));
  186. }
  187. int serial_tstc(void)
  188. {
  189. unsigned char status;
  190. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  191. if (serial_buffer_active) {
  192. return serial_buffered_tstc();
  193. }
  194. #endif
  195. status = inb(UART0_BASE + UART_LSR);
  196. if ((status & asyncLSRDataReady1) != 0x0) {
  197. return (1);
  198. }
  199. if ((status & ( asyncLSRFramingError1 |
  200. asyncLSROverrunError1 |
  201. asyncLSRParityError1 |
  202. asyncLSRBreakInterrupt1 )) != 0) {
  203. outb(asyncLSRFramingError1 |
  204. asyncLSROverrunError1 |
  205. asyncLSRParityError1 |
  206. asyncLSRBreakInterrupt1, UART0_BASE + UART_LSR);
  207. }
  208. return 0;
  209. }
  210. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  211. void serial_isr(void *arg)
  212. {
  213. int space;
  214. int c;
  215. int rx_put = buf_info.rx_put;
  216. if (buf_info.rx_get <= rx_put) {
  217. space = CONFIG_SERIAL_SOFTWARE_FIFO - (rx_put - buf_info.rx_get);
  218. } else {
  219. space = buf_info.rx_get - rx_put;
  220. }
  221. while (inb(UART0_BASE + UART_LSR) & 1) {
  222. c = inb(UART0_BASE);
  223. if (space) {
  224. buf_info.rx_buffer[rx_put++] = c;
  225. space--;
  226. if (rx_put == buf_info.rx_get) {
  227. buf_info.rx_get++;
  228. if (rx_put == CONFIG_SERIAL_SOFTWARE_FIFO) {
  229. buf_info.rx_get = 0;
  230. }
  231. }
  232. if (rx_put == CONFIG_SERIAL_SOFTWARE_FIFO) {
  233. rx_put = 0;
  234. if (0 == buf_info.rx_get) {
  235. buf_info.rx_get = 1;
  236. }
  237. }
  238. }
  239. if (space < CONFIG_SERIAL_SOFTWARE_FIFO / 4) {
  240. /* Stop flow by setting RTS inactive */
  241. outb(inb(UART0_BASE + UART_MCR) & (0xFF ^ 0x02),
  242. UART0_BASE + UART_MCR);
  243. }
  244. }
  245. buf_info.rx_put = rx_put;
  246. }
  247. void serial_buffered_init(void)
  248. {
  249. serial_puts ("Switching to interrupt driven serial input mode.\n");
  250. buf_info.rx_buffer = malloc (CONFIG_SERIAL_SOFTWARE_FIFO);
  251. buf_info.rx_put = 0;
  252. buf_info.rx_get = 0;
  253. if (inb (UART0_BASE + UART_MSR) & 0x10) {
  254. serial_puts ("Check CTS signal present on serial port: OK.\n");
  255. buf_info.cts = 1;
  256. } else {
  257. serial_puts ("WARNING: CTS signal not present on serial port.\n");
  258. buf_info.cts = 0;
  259. }
  260. irq_install_handler ( VECNUM_U0 /*UART0 */ /*int vec */ ,
  261. serial_isr /*interrupt_handler_t *handler */ ,
  262. (void *) &buf_info /*void *arg */ );
  263. /* Enable "RX Data Available" Interrupt on UART */
  264. /* outb(inb(UART0_BASE + UART_IER) |0x01, UART0_BASE + UART_IER); */
  265. outb(0x01, UART0_BASE + UART_IER);
  266. /* Set DTR and RTS active, enable interrupts */
  267. outb(inb (UART0_BASE + UART_MCR) | 0x0b, UART0_BASE + UART_MCR);
  268. /* Setup UART FIFO: RX trigger level: 1 byte, Enable FIFO */
  269. outb( /*(1 << 6) |*/ 1, UART0_BASE + UART_FCR);
  270. serial_buffer_active = 1;
  271. }
  272. void serial_buffered_putc (const char c)
  273. {
  274. int i;
  275. /* Wait for CTS */
  276. #if defined(CONFIG_HW_WATCHDOG)
  277. while (!(inb (UART0_BASE + UART_MSR) & 0x10))
  278. WATCHDOG_RESET ();
  279. #else
  280. if (buf_info.cts) {
  281. for (i=0;i<1000;i++) {
  282. if ((inb (UART0_BASE + UART_MSR) & 0x10)) {
  283. break;
  284. }
  285. }
  286. if (i!=1000) {
  287. buf_info.cts = 0;
  288. }
  289. } else {
  290. if ((inb (UART0_BASE + UART_MSR) & 0x10)) {
  291. buf_info.cts = 1;
  292. }
  293. }
  294. #endif
  295. serial_putc (c);
  296. }
  297. void serial_buffered_puts(const char *s)
  298. {
  299. serial_puts (s);
  300. }
  301. int serial_buffered_getc(void)
  302. {
  303. int space;
  304. int c;
  305. int rx_get = buf_info.rx_get;
  306. int rx_put;
  307. #if defined(CONFIG_HW_WATCHDOG)
  308. while (rx_get == buf_info.rx_put)
  309. WATCHDOG_RESET ();
  310. #else
  311. while (rx_get == buf_info.rx_put);
  312. #endif
  313. c = buf_info.rx_buffer[rx_get++];
  314. if (rx_get == CONFIG_SERIAL_SOFTWARE_FIFO) {
  315. rx_get = 0;
  316. }
  317. buf_info.rx_get = rx_get;
  318. rx_put = buf_info.rx_put;
  319. if (rx_get <= rx_put) {
  320. space = CONFIG_SERIAL_SOFTWARE_FIFO - (rx_put - rx_get);
  321. } else {
  322. space = rx_get - rx_put;
  323. }
  324. if (space > CONFIG_SERIAL_SOFTWARE_FIFO / 2) {
  325. /* Start flow by setting RTS active */
  326. outb(inb (UART0_BASE + UART_MCR) | 0x02, UART0_BASE + UART_MCR);
  327. }
  328. return c;
  329. }
  330. int serial_buffered_tstc(void)
  331. {
  332. return (buf_info.rx_get != buf_info.rx_put) ? 1 : 0;
  333. }
  334. #endif /* CONFIG_SERIAL_SOFTWARE_FIFO */
  335. #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
  336. /*
  337. AS HARNOIS : according to CONFIG_KGDB_SER_INDEX kgdb uses serial port
  338. number 0 or number 1
  339. - if CONFIG_KGDB_SER_INDEX = 1 => serial port number 0 :
  340. configuration has been already done
  341. - if CONFIG_KGDB_SER_INDEX = 2 => serial port number 1 :
  342. configure port 1 for serial I/O with rate = CONFIG_KGDB_BAUDRATE
  343. */
  344. #if (CONFIG_KGDB_SER_INDEX & 2)
  345. void kgdb_serial_init(void)
  346. {
  347. DECLARE_GLOBAL_DATA_PTR;
  348. volatile char val;
  349. bdiv = serial_div (CONFIG_KGDB_BAUDRATE);
  350. /*
  351. * Init onboard 16550 UART
  352. */
  353. outb(0x80, UART1_BASE + UART_LCR); /* set DLAB bit */
  354. outb(bdiv & 0xff), UART1_BASE + UART_DLL); /* set divisor for 9600 baud */
  355. outb(bdiv >> 8), UART1_BASE + UART_DLM); /* set divisor for 9600 baud */
  356. outb(0x03, UART1_BASE + UART_LCR); /* line control 8 bits no parity */
  357. outb(0x00, UART1_BASE + UART_FCR); /* disable FIFO */
  358. outb(0x00, UART1_BASE + UART_MCR); /* no modem control DTR RTS */
  359. val = inb(UART1_BASE + UART_LSR); /* clear line status */
  360. val = inb(UART1_BASE + UART_RBR); /* read receive buffer */
  361. outb(0x00, UART1_BASE + UART_SCR); /* set scratchpad */
  362. outb(0x00, UART1_BASE + UART_IER); /* set interrupt enable reg */
  363. }
  364. void putDebugChar(const char c)
  365. {
  366. if (c == '\n')
  367. serial_putc ('\r');
  368. outb(c, UART1_BASE + UART_THR); /* put character out */
  369. /* check THRE bit, wait for transfer done */
  370. while ((inb(UART1_BASE + UART_LSR) & 0x20) != 0x20);
  371. }
  372. void putDebugStr(const char *s)
  373. {
  374. while (*s) {
  375. serial_putc(*s++);
  376. }
  377. }
  378. int getDebugChar(void)
  379. {
  380. unsigned char status = 0;
  381. while (1) {
  382. status = inb(UART1_BASE + UART_LSR);
  383. if ((status & asyncLSRDataReady1) != 0x0) {
  384. break;
  385. }
  386. if ((status & ( asyncLSRFramingError1 |
  387. asyncLSROverrunError1 |
  388. asyncLSRParityError1 |
  389. asyncLSRBreakInterrupt1 )) != 0) {
  390. outb(asyncLSRFramingError1 |
  391. asyncLSROverrunError1 |
  392. asyncLSRParityError1 |
  393. asyncLSRBreakInterrupt1, UART1_BASE + UART_LSR);
  394. }
  395. }
  396. return (0x000000ff & (int) inb(UART1_BASE));
  397. }
  398. void kgdb_interruptible(int yes)
  399. {
  400. return;
  401. }
  402. #else /* ! (CONFIG_KGDB_SER_INDEX & 2) */
  403. void kgdb_serial_init(void)
  404. {
  405. serial_printf ("[on serial] ");
  406. }
  407. void putDebugChar(int c)
  408. {
  409. serial_putc (c);
  410. }
  411. void putDebugStr(const char *str)
  412. {
  413. serial_puts (str);
  414. }
  415. int getDebugChar(void)
  416. {
  417. return serial_getc ();
  418. }
  419. void kgdb_interruptible(int yes)
  420. {
  421. return;
  422. }
  423. #endif /* (CONFIG_KGDB_SER_INDEX & 2) */
  424. #endif /* CFG_CMD_KGDB */