serial.c 12 KB

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