v850e_uartb.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * include/asm-v850/v850e_uartb.h -- V850E on-chip `UARTB' UART
  3. *
  4. * Copyright (C) 2001,02,03 NEC Electronics Corporation
  5. * Copyright (C) 2001,02,03 Miles Bader <miles@gnu.org>
  6. *
  7. * This file is subject to the terms and conditions of the GNU General
  8. * Public License. See the file COPYING in the main directory of this
  9. * archive for more details.
  10. *
  11. * Written by Miles Bader <miles@gnu.org>
  12. */
  13. /* The V850E UARTB is basically a superset of the original V850E UART, but
  14. even where it's the same, the names and details have changed a bit.
  15. It's similar enough to use the same driver (v850e_uart.c), but the
  16. details have been abstracted slightly to do so. */
  17. #ifndef __V850_V850E_UARTB_H__
  18. #define __V850_V850E_UARTB_H__
  19. /* Raw hardware interface. */
  20. #define V850E_UARTB_BASE_ADDR(n) (0xFFFFFA00 + 0x10 * (n))
  21. /* Addresses of specific UART control registers for channel N. */
  22. #define V850E_UARTB_CTL0_ADDR(n) (V850E_UARTB_BASE_ADDR(n) + 0x0)
  23. #define V850E_UARTB_CTL2_ADDR(n) (V850E_UARTB_BASE_ADDR(n) + 0x2)
  24. #define V850E_UARTB_STR_ADDR(n) (V850E_UARTB_BASE_ADDR(n) + 0x4)
  25. #define V850E_UARTB_RX_ADDR(n) (V850E_UARTB_BASE_ADDR(n) + 0x6)
  26. #define V850E_UARTB_RXAP_ADDR(n) (V850E_UARTB_BASE_ADDR(n) + 0x6)
  27. #define V850E_UARTB_TX_ADDR(n) (V850E_UARTB_BASE_ADDR(n) + 0x8)
  28. #define V850E_UARTB_FIC0_ADDR(n) (V850E_UARTB_BASE_ADDR(n) + 0xA)
  29. #define V850E_UARTB_FIC1_ADDR(n) (V850E_UARTB_BASE_ADDR(n) + 0xB)
  30. #define V850E_UARTB_FIC2_ADDR(n) (V850E_UARTB_BASE_ADDR(n) + 0xC)
  31. #define V850E_UARTB_FIS0_ADDR(n) (V850E_UARTB_BASE_ADDR(n) + 0xE)
  32. #define V850E_UARTB_FIS1_ADDR(n) (V850E_UARTB_BASE_ADDR(n) + 0xF)
  33. /* UARTB control register 0 (general config). */
  34. #define V850E_UARTB_CTL0(n) (*(volatile u8 *)V850E_UARTB_CTL0_ADDR(n))
  35. /* Control bits for config registers. */
  36. #define V850E_UARTB_CTL0_PWR 0x80 /* clock enable */
  37. #define V850E_UARTB_CTL0_TXE 0x40 /* transmit enable */
  38. #define V850E_UARTB_CTL0_RXE 0x20 /* receive enable */
  39. #define V850E_UARTB_CTL0_DIR 0x10 /* */
  40. #define V850E_UARTB_CTL0_PS1 0x08 /* parity */
  41. #define V850E_UARTB_CTL0_PS0 0x04 /* parity */
  42. #define V850E_UARTB_CTL0_CL 0x02 /* char len 1:8bit, 0:7bit */
  43. #define V850E_UARTB_CTL0_SL 0x01 /* stop bit 1:2bit, 0:1bit */
  44. #define V850E_UARTB_CTL0_PS_MASK 0x0C /* mask covering parity bits */
  45. #define V850E_UARTB_CTL0_PS_NONE 0x00 /* no parity */
  46. #define V850E_UARTB_CTL0_PS_ZERO 0x04 /* zero parity */
  47. #define V850E_UARTB_CTL0_PS_ODD 0x08 /* odd parity */
  48. #define V850E_UARTB_CTL0_PS_EVEN 0x0C /* even parity */
  49. #define V850E_UARTB_CTL0_CL_8 0x02 /* char len 1:8bit, 0:7bit */
  50. #define V850E_UARTB_CTL0_SL_2 0x01 /* stop bit 1:2bit, 0:1bit */
  51. /* UARTB control register 2 (clock divider). */
  52. #define V850E_UARTB_CTL2(n) (*(volatile u16 *)V850E_UARTB_CTL2_ADDR(n))
  53. #define V850E_UARTB_CTL2_MIN 4
  54. #define V850E_UARTB_CTL2_MAX 0xFFFF
  55. /* UARTB serial interface status register. */
  56. #define V850E_UARTB_STR(n) (*(volatile u8 *)V850E_UARTB_STR_ADDR(n))
  57. /* Control bits for status registers. */
  58. #define V850E_UARTB_STR_TSF 0x80 /* UBTX or FIFO exist data */
  59. #define V850E_UARTB_STR_OVF 0x08 /* overflow error */
  60. #define V850E_UARTB_STR_PE 0x04 /* parity error */
  61. #define V850E_UARTB_STR_FE 0x02 /* framing error */
  62. #define V850E_UARTB_STR_OVE 0x01 /* overrun error */
  63. /* UARTB receive data register. */
  64. #define V850E_UARTB_RX(n) (*(volatile u8 *)V850E_UARTB_RX_ADDR(n))
  65. #define V850E_UARTB_RXAP(n) (*(volatile u16 *)V850E_UARTB_RXAP_ADDR(n))
  66. /* Control bits for status registers. */
  67. #define V850E_UARTB_RXAP_PEF 0x0200 /* parity error */
  68. #define V850E_UARTB_RXAP_FEF 0x0100 /* framing error */
  69. /* UARTB transmit data register. */
  70. #define V850E_UARTB_TX(n) (*(volatile u8 *)V850E_UARTB_TX_ADDR(n))
  71. /* UARTB FIFO control register 0. */
  72. #define V850E_UARTB_FIC0(n) (*(volatile u8 *)V850E_UARTB_FIC0_ADDR(n))
  73. /* UARTB FIFO control register 1. */
  74. #define V850E_UARTB_FIC1(n) (*(volatile u8 *)V850E_UARTB_FIC1_ADDR(n))
  75. /* UARTB FIFO control register 2. */
  76. #define V850E_UARTB_FIC2(n) (*(volatile u16 *)V850E_UARTB_FIC2_ADDR(n))
  77. /* UARTB FIFO status register 0. */
  78. #define V850E_UARTB_FIS0(n) (*(volatile u8 *)V850E_UARTB_FIS0_ADDR(n))
  79. /* UARTB FIFO status register 1. */
  80. #define V850E_UARTB_FIS1(n) (*(volatile u8 *)V850E_UARTB_FIS1_ADDR(n))
  81. /* Slightly abstract interface used by driver. */
  82. /* Interrupts used by the UART. */
  83. /* Received when the most recently transmitted character has been sent. */
  84. #define V850E_UART_TX_IRQ(chan) IRQ_INTUBTIT (chan)
  85. /* Received when a new character has been received. */
  86. #define V850E_UART_RX_IRQ(chan) IRQ_INTUBTIR (chan)
  87. /* Use by serial driver for information purposes. */
  88. #define V850E_UART_BASE_ADDR(chan) V850E_UARTB_BASE_ADDR(chan)
  89. /* UART clock generator interface. */
  90. /* This type encapsulates a particular uart frequency. */
  91. typedef u16 v850e_uart_speed_t;
  92. /* Calculate a uart speed from BAUD for this uart. */
  93. static inline v850e_uart_speed_t v850e_uart_calc_speed (unsigned baud)
  94. {
  95. v850e_uart_speed_t speed;
  96. /*
  97. * V850E/ME2 UARTB baud rate is determined by the value of UBCTL2
  98. * fx = V850E_UARTB_BASE_FREQ = CPU_CLOCK_FREQ/4
  99. * baud = fx / 2*speed [ speed >= 4 ]
  100. */
  101. speed = V850E_UARTB_CTL2_MIN;
  102. while (((V850E_UARTB_BASE_FREQ / 2) / speed ) > baud)
  103. speed++;
  104. return speed;
  105. }
  106. /* Return the current speed of uart channel CHAN. */
  107. #define v850e_uart_speed(chan) V850E_UARTB_CTL2 (chan)
  108. /* Set the current speed of uart channel CHAN. */
  109. #define v850e_uart_set_speed(chan, speed) (V850E_UARTB_CTL2 (chan) = (speed))
  110. /* Return true if SPEED1 and SPEED2 are the same. */
  111. #define v850e_uart_speed_eq(speed1, speed2) ((speed1) == (speed2))
  112. /* Minimum baud rate possible. */
  113. #define v850e_uart_min_baud() \
  114. ((V850E_UARTB_BASE_FREQ / 2) / V850E_UARTB_CTL2_MAX)
  115. /* Maximum baud rate possible. The error is quite high at max, though. */
  116. #define v850e_uart_max_baud() \
  117. ((V850E_UARTB_BASE_FREQ / 2) / V850E_UARTB_CTL2_MIN)
  118. /* The `maximum' clock rate the uart can used, which is wanted (though not
  119. really used in any useful way) by the serial framework. */
  120. #define v850e_uart_max_clock() \
  121. (V850E_UARTB_BASE_FREQ / 2)
  122. /* UART configuration interface. */
  123. /* Type of the uart config register; must be a scalar. */
  124. typedef u16 v850e_uart_config_t;
  125. /* The uart hardware config register for channel CHAN. */
  126. #define V850E_UART_CONFIG(chan) V850E_UARTB_CTL0 (chan)
  127. /* This config bit set if the uart is enabled. */
  128. #define V850E_UART_CONFIG_ENABLED V850E_UARTB_CTL0_PWR
  129. /* If the uart _isn't_ enabled, store this value to it to do so. */
  130. #define V850E_UART_CONFIG_INIT V850E_UARTB_CTL0_PWR
  131. /* Store this config value to disable the uart channel completely. */
  132. #define V850E_UART_CONFIG_FINI 0
  133. /* Setting/clearing these bits enable/disable TX/RX, respectively (but
  134. otherwise generally leave things running). */
  135. #define V850E_UART_CONFIG_RX_ENABLE V850E_UARTB_CTL0_RXE
  136. #define V850E_UART_CONFIG_TX_ENABLE V850E_UARTB_CTL0_TXE
  137. /* These masks define which config bits affect TX/RX modes, respectively. */
  138. #define V850E_UART_CONFIG_RX_BITS \
  139. (V850E_UARTB_CTL0_PS_MASK | V850E_UARTB_CTL0_CL_8)
  140. #define V850E_UART_CONFIG_TX_BITS \
  141. (V850E_UARTB_CTL0_PS_MASK | V850E_UARTB_CTL0_CL_8 | V850E_UARTB_CTL0_SL_2)
  142. static inline v850e_uart_config_t v850e_uart_calc_config (unsigned cflags)
  143. {
  144. v850e_uart_config_t config = 0;
  145. /* Figure out new configuration of control register. */
  146. if (cflags & CSTOPB)
  147. /* Number of stop bits, 1 or 2. */
  148. config |= V850E_UARTB_CTL0_SL_2;
  149. if ((cflags & CSIZE) == CS8)
  150. /* Number of data bits, 7 or 8. */
  151. config |= V850E_UARTB_CTL0_CL_8;
  152. if (! (cflags & PARENB))
  153. /* No parity check/generation. */
  154. config |= V850E_UARTB_CTL0_PS_NONE;
  155. else if (cflags & PARODD)
  156. /* Odd parity check/generation. */
  157. config |= V850E_UARTB_CTL0_PS_ODD;
  158. else
  159. /* Even parity check/generation. */
  160. config |= V850E_UARTB_CTL0_PS_EVEN;
  161. if (cflags & CREAD)
  162. /* Reading enabled. */
  163. config |= V850E_UARTB_CTL0_RXE;
  164. config |= V850E_UARTB_CTL0_PWR;
  165. config |= V850E_UARTB_CTL0_TXE; /* Writing is always enabled. */
  166. config |= V850E_UARTB_CTL0_DIR; /* LSB first. */
  167. return config;
  168. }
  169. /* This should delay as long as necessary for a recently written config
  170. setting to settle, before we turn the uart back on. */
  171. static inline void
  172. v850e_uart_config_delay (v850e_uart_config_t config, v850e_uart_speed_t speed)
  173. {
  174. /* The UART may not be reset properly unless we wait at least 2
  175. `basic-clocks' until turning on the TXE/RXE bits again.
  176. A `basic clock' is the clock used by the baud-rate generator,
  177. i.e., the cpu clock divided by the 2^new_clk_divlog2.
  178. The loop takes 2 insns, so loop CYCLES / 2 times. */
  179. register unsigned count = 1 << speed;
  180. while (--count != 0)
  181. /* nothing */;
  182. }
  183. /* RX/TX interface. */
  184. /* Return true if all characters awaiting transmission on uart channel N
  185. have been transmitted. */
  186. #define v850e_uart_xmit_done(n) \
  187. (! (V850E_UARTB_STR(n) & V850E_UARTB_STR_TSF))
  188. /* Wait for this to be true. */
  189. #define v850e_uart_wait_for_xmit_done(n) \
  190. do { } while (! v850e_uart_xmit_done (n))
  191. /* Return true if uart channel N is ready to transmit a character. */
  192. #define v850e_uart_xmit_ok(n) \
  193. (v850e_uart_xmit_done(n) && v850e_uart_cts(n))
  194. /* Wait for this to be true. */
  195. #define v850e_uart_wait_for_xmit_ok(n) \
  196. do { } while (! v850e_uart_xmit_ok (n))
  197. /* Write character CH to uart channel CHAN. */
  198. #define v850e_uart_putc(chan, ch) (V850E_UARTB_TX(chan) = (ch))
  199. /* Return latest character read on channel CHAN. */
  200. #define v850e_uart_getc(chan) V850E_UARTB_RX (chan)
  201. /* Return bit-mask of uart error status. */
  202. #define v850e_uart_err(chan) V850E_UARTB_STR (chan)
  203. /* Various error bits set in the error result. */
  204. #define V850E_UART_ERR_OVERRUN V850E_UARTB_STR_OVE
  205. #define V850E_UART_ERR_FRAME V850E_UARTB_STR_FE
  206. #define V850E_UART_ERR_PARITY V850E_UARTB_STR_PE
  207. #endif /* __V850_V850E_UARTB_H__ */