iop480_uart.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. #include <common.h>
  24. #include <commproc.h>
  25. #include <asm/processor.h>
  26. #include <asm/io.h>
  27. #include <watchdog.h>
  28. #include <serial.h>
  29. #include <linux/compiler.h>
  30. DECLARE_GLOBAL_DATA_PTR;
  31. #ifdef CONFIG_IOP480
  32. #define SPU_BASE 0x40000000
  33. #define spu_LineStat_rc 0x00 /* Line Status Register (Read/Clear) */
  34. #define spu_LineStat_w 0x04 /* Line Status Register (Set) */
  35. #define spu_Handshk_rc 0x08 /* Handshake Status Register (Read/Clear) */
  36. #define spu_Handshk_w 0x0c /* Handshake Status Register (Set) */
  37. #define spu_BRateDivh 0x10 /* Baud rate divisor high */
  38. #define spu_BRateDivl 0x14 /* Baud rate divisor low */
  39. #define spu_CtlReg 0x18 /* Control Register */
  40. #define spu_RxCmd 0x1c /* Rx Command Register */
  41. #define spu_TxCmd 0x20 /* Tx Command Register */
  42. #define spu_RxBuff 0x24 /* Rx data buffer */
  43. #define spu_TxBuff 0x24 /* Tx data buffer */
  44. /*-----------------------------------------------------------------------------+
  45. | Line Status Register.
  46. +-----------------------------------------------------------------------------*/
  47. #define asyncLSRport1 0x40000000
  48. #define asyncLSRport1set 0x40000004
  49. #define asyncLSRDataReady 0x80
  50. #define asyncLSRFramingError 0x40
  51. #define asyncLSROverrunError 0x20
  52. #define asyncLSRParityError 0x10
  53. #define asyncLSRBreakInterrupt 0x08
  54. #define asyncLSRTxHoldEmpty 0x04
  55. #define asyncLSRTxShiftEmpty 0x02
  56. /*-----------------------------------------------------------------------------+
  57. | Handshake Status Register.
  58. +-----------------------------------------------------------------------------*/
  59. #define asyncHSRport1 0x40000008
  60. #define asyncHSRport1set 0x4000000c
  61. #define asyncHSRDsr 0x80
  62. #define asyncLSRCts 0x40
  63. /*-----------------------------------------------------------------------------+
  64. | Control Register.
  65. +-----------------------------------------------------------------------------*/
  66. #define asyncCRport1 0x40000018
  67. #define asyncCRNormal 0x00
  68. #define asyncCRLoopback 0x40
  69. #define asyncCRAutoEcho 0x80
  70. #define asyncCRDtr 0x20
  71. #define asyncCRRts 0x10
  72. #define asyncCRWordLength7 0x00
  73. #define asyncCRWordLength8 0x08
  74. #define asyncCRParityDisable 0x00
  75. #define asyncCRParityEnable 0x04
  76. #define asyncCREvenParity 0x00
  77. #define asyncCROddParity 0x02
  78. #define asyncCRStopBitsOne 0x00
  79. #define asyncCRStopBitsTwo 0x01
  80. #define asyncCRDisableDtrRts 0x00
  81. /*-----------------------------------------------------------------------------+
  82. | Receiver Command Register.
  83. +-----------------------------------------------------------------------------*/
  84. #define asyncRCRport1 0x4000001c
  85. #define asyncRCRDisable 0x00
  86. #define asyncRCREnable 0x80
  87. #define asyncRCRIntDisable 0x00
  88. #define asyncRCRIntEnabled 0x20
  89. #define asyncRCRDMACh2 0x40
  90. #define asyncRCRDMACh3 0x60
  91. #define asyncRCRErrorInt 0x10
  92. #define asyncRCRPauseEnable 0x08
  93. /*-----------------------------------------------------------------------------+
  94. | Transmitter Command Register.
  95. +-----------------------------------------------------------------------------*/
  96. #define asyncTCRport1 0x40000020
  97. #define asyncTCRDisable 0x00
  98. #define asyncTCREnable 0x80
  99. #define asyncTCRIntDisable 0x00
  100. #define asyncTCRIntEnabled 0x20
  101. #define asyncTCRDMACh2 0x40
  102. #define asyncTCRDMACh3 0x60
  103. #define asyncTCRTxEmpty 0x10
  104. #define asyncTCRErrorInt 0x08
  105. #define asyncTCRStopPause 0x04
  106. #define asyncTCRBreakGen 0x02
  107. /*-----------------------------------------------------------------------------+
  108. | Miscellanies defines.
  109. +-----------------------------------------------------------------------------*/
  110. #define asyncTxBufferport1 0x40000024
  111. #define asyncRxBufferport1 0x40000024
  112. #define asyncDLABLsbport1 0x40000014
  113. #define asyncDLABMsbport1 0x40000010
  114. #define asyncXOFFchar 0x13
  115. #define asyncXONchar 0x11
  116. /*
  117. * Minimal serial functions needed to use one of the SMC ports
  118. * as serial console interface.
  119. */
  120. static int iop480_serial_init(void)
  121. {
  122. unsigned short br_reg;
  123. br_reg = ((((CONFIG_CPUCLOCK * 1000000) / 16) / gd->baudrate) - 1);
  124. /*
  125. * Init onboard UART
  126. */
  127. out_8((u8 *)SPU_BASE + spu_LineStat_rc, 0x78); /* Clear all bits in Line Status Reg */
  128. out_8((u8 *)SPU_BASE + spu_BRateDivl, (br_reg & 0x00ff)); /* Set baud rate divisor... */
  129. out_8((u8 *)SPU_BASE + spu_BRateDivh, ((br_reg & 0xff00) >> 8)); /* ... */
  130. out_8((u8 *)SPU_BASE + spu_CtlReg, 0x08); /* Set 8 bits, no parity and 1 stop bit */
  131. out_8((u8 *)SPU_BASE + spu_RxCmd, 0xb0); /* Enable Rx */
  132. out_8((u8 *)SPU_BASE + spu_TxCmd, 0x9c); /* Enable Tx */
  133. out_8((u8 *)SPU_BASE + spu_Handshk_rc, 0xff); /* Clear Handshake */
  134. in_8((u8 *)SPU_BASE + spu_RxBuff); /* Dummy read, to clear receiver */
  135. return (0);
  136. }
  137. static void iop480_serial_setbrg(void)
  138. {
  139. unsigned short br_reg;
  140. br_reg = ((((CONFIG_CPUCLOCK * 1000000) / 16) / gd->baudrate) - 1);
  141. out_8((u8 *)SPU_BASE + spu_BRateDivl,
  142. (br_reg & 0x00ff)); /* Set baud rate divisor... */
  143. out_8((u8 *)SPU_BASE + spu_BRateDivh,
  144. ((br_reg & 0xff00) >> 8)); /* ... */
  145. }
  146. static void iop480_serial_putc(const char c)
  147. {
  148. if (c == '\n')
  149. serial_putc ('\r');
  150. /* load status from handshake register */
  151. if (in_8((u8 *)SPU_BASE + spu_Handshk_rc) != 00)
  152. out_8((u8 *)SPU_BASE + spu_Handshk_rc, 0xff); /* Clear Handshake */
  153. out_8((u8 *)SPU_BASE + spu_TxBuff, c); /* Put char */
  154. while ((in_8((u8 *)SPU_BASE + spu_LineStat_rc) & 04) != 04) {
  155. if (in_8((u8 *)SPU_BASE + spu_Handshk_rc) != 00)
  156. out_8((u8 *)SPU_BASE + spu_Handshk_rc, 0xff); /* Clear Handshake */
  157. }
  158. }
  159. static void iop480_serial_puts(const char *s)
  160. {
  161. while (*s) {
  162. serial_putc (*s++);
  163. }
  164. }
  165. static int iop480_serial_getc(void)
  166. {
  167. unsigned char status = 0;
  168. while (1) {
  169. status = in_8((u8 *)asyncLSRport1);
  170. if ((status & asyncLSRDataReady) != 0x0) {
  171. break;
  172. }
  173. if ((status & ( asyncLSRFramingError |
  174. asyncLSROverrunError |
  175. asyncLSRParityError |
  176. asyncLSRBreakInterrupt )) != 0) {
  177. (void) out_8((u8 *)asyncLSRport1,
  178. asyncLSRFramingError |
  179. asyncLSROverrunError |
  180. asyncLSRParityError |
  181. asyncLSRBreakInterrupt );
  182. }
  183. }
  184. return (0x000000ff & (int) in_8((u8 *)asyncRxBufferport1));
  185. }
  186. static int iop480_serial_tstc(void)
  187. {
  188. unsigned char status;
  189. status = in_8((u8 *)asyncLSRport1);
  190. if ((status & asyncLSRDataReady) != 0x0) {
  191. return (1);
  192. }
  193. if ((status & ( asyncLSRFramingError |
  194. asyncLSROverrunError |
  195. asyncLSRParityError |
  196. asyncLSRBreakInterrupt )) != 0) {
  197. (void) out_8((u8 *)asyncLSRport1,
  198. asyncLSRFramingError |
  199. asyncLSROverrunError |
  200. asyncLSRParityError |
  201. asyncLSRBreakInterrupt);
  202. }
  203. return 0;
  204. }
  205. static struct serial_device iop480_serial_drv = {
  206. .name = "iop480_serial",
  207. .start = iop480_serial_init,
  208. .stop = NULL,
  209. .setbrg = iop480_serial_setbrg,
  210. .putc = iop480_serial_putc,
  211. .puts = iop480_serial_puts,
  212. .getc = iop480_serial_getc,
  213. .tstc = iop480_serial_tstc,
  214. };
  215. void iop480_serial_initialize(void)
  216. {
  217. serial_register(&iop480_serial_drv);
  218. }
  219. __weak struct serial_device *default_serial_console(void)
  220. {
  221. return &iop480_serial_drv;
  222. }
  223. #endif /* CONFIG_IOP480 */