serial_sa1100.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * (C) Copyright 2002
  3. * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
  4. *
  5. * (C) Copyright 2002
  6. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  7. * Marius Groeger <mgroeger@sysgo.de>
  8. *
  9. * (C) Copyright 2002
  10. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  11. * Alex Zuepke <azu@sysgo.de>
  12. *
  13. * Copyright (C) 1999 2000 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
  14. *
  15. * SPDX-License-Identifier: GPL-2.0+
  16. */
  17. #include <common.h>
  18. #include <SA-1100.h>
  19. #include <serial.h>
  20. #include <linux/compiler.h>
  21. DECLARE_GLOBAL_DATA_PTR;
  22. static void sa1100_serial_setbrg(void)
  23. {
  24. unsigned int reg = 0;
  25. if (gd->baudrate == 1200)
  26. reg = 191;
  27. else if (gd->baudrate == 9600)
  28. reg = 23;
  29. else if (gd->baudrate == 19200)
  30. reg = 11;
  31. else if (gd->baudrate == 38400)
  32. reg = 5;
  33. else if (gd->baudrate == 57600)
  34. reg = 3;
  35. else if (gd->baudrate == 115200)
  36. reg = 1;
  37. else
  38. hang ();
  39. #ifdef CONFIG_SERIAL1
  40. /* SA1110 uart function */
  41. Ser1SDCR0 |= SDCR0_SUS;
  42. /* Wait until port is ready ... */
  43. while(Ser1UTSR1 & UTSR1_TBY) {}
  44. /* init serial serial 1 */
  45. Ser1UTCR3 = 0x00;
  46. Ser1UTSR0 = 0xff;
  47. Ser1UTCR0 = ( UTCR0_1StpBit | UTCR0_8BitData );
  48. Ser1UTCR1 = 0;
  49. Ser1UTCR2 = (u32)reg;
  50. Ser1UTCR3 = ( UTCR3_RXE | UTCR3_TXE );
  51. #elif defined(CONFIG_SERIAL3)
  52. /* Wait until port is ready ... */
  53. while (Ser3UTSR1 & UTSR1_TBY) {
  54. }
  55. /* init serial serial 3 */
  56. Ser3UTCR3 = 0x00;
  57. Ser3UTSR0 = 0xff;
  58. Ser3UTCR0 = (UTCR0_1StpBit | UTCR0_8BitData);
  59. Ser3UTCR1 = 0;
  60. Ser3UTCR2 = (u32) reg;
  61. Ser3UTCR3 = (UTCR3_RXE | UTCR3_TXE);
  62. #else
  63. #error "Bad: you didn't configured serial ..."
  64. #endif
  65. }
  66. /*
  67. * Initialise the serial port with the given baudrate. The settings
  68. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  69. *
  70. */
  71. static int sa1100_serial_init(void)
  72. {
  73. serial_setbrg ();
  74. return (0);
  75. }
  76. /*
  77. * Output a single byte to the serial port.
  78. */
  79. static void sa1100_serial_putc(const char c)
  80. {
  81. #ifdef CONFIG_SERIAL1
  82. /* wait for room in the tx FIFO on SERIAL1 */
  83. while ((Ser1UTSR0 & UTSR0_TFS) == 0);
  84. Ser1UTDR = c;
  85. #elif defined(CONFIG_SERIAL3)
  86. /* wait for room in the tx FIFO on SERIAL3 */
  87. while ((Ser3UTSR0 & UTSR0_TFS) == 0);
  88. Ser3UTDR = c;
  89. #endif
  90. /* If \n, also do \r */
  91. if (c == '\n')
  92. serial_putc ('\r');
  93. }
  94. /*
  95. * Read a single byte from the serial port. Returns 1 on success, 0
  96. * otherwise. When the function is succesfull, the character read is
  97. * written into its argument c.
  98. */
  99. static int sa1100_serial_tstc(void)
  100. {
  101. #ifdef CONFIG_SERIAL1
  102. return Ser1UTSR1 & UTSR1_RNE;
  103. #elif defined(CONFIG_SERIAL3)
  104. return Ser3UTSR1 & UTSR1_RNE;
  105. #endif
  106. }
  107. /*
  108. * Read a single byte from the serial port. Returns 1 on success, 0
  109. * otherwise. When the function is succesfull, the character read is
  110. * written into its argument c.
  111. */
  112. static int sa1100_serial_getc(void)
  113. {
  114. #ifdef CONFIG_SERIAL1
  115. while (!(Ser1UTSR1 & UTSR1_RNE));
  116. return (char) Ser1UTDR & 0xff;
  117. #elif defined(CONFIG_SERIAL3)
  118. while (!(Ser3UTSR1 & UTSR1_RNE));
  119. return (char) Ser3UTDR & 0xff;
  120. #endif
  121. }
  122. static struct serial_device sa1100_serial_drv = {
  123. .name = "sa1100_serial",
  124. .start = sa1100_serial_init,
  125. .stop = NULL,
  126. .setbrg = sa1100_serial_setbrg,
  127. .putc = sa1100_serial_putc,
  128. .puts = default_serial_puts,
  129. .getc = sa1100_serial_getc,
  130. .tstc = sa1100_serial_tstc,
  131. };
  132. void sa1100_serial_initialize(void)
  133. {
  134. serial_register(&sa1100_serial_drv);
  135. }
  136. __weak struct serial_device *default_serial_console(void)
  137. {
  138. return &sa1100_serial_drv;
  139. }