v850e_uarta.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * include/asm-v850/v850e_uarta.h -- original V850E on-chip 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. /* This is the original V850E UART implementation is called just `UART' in
  14. the docs, but we name this header file <asm/v850e_uarta.h> because the
  15. name <asm/v850e_uart.h> is used for the common driver that handles both
  16. `UART' and `UARTB' implementations. */
  17. #ifndef __V850_V850E_UARTA_H__
  18. #define __V850_V850E_UARTA_H__
  19. /* Raw hardware interface. */
  20. /* The base address of the UART control registers for channel N.
  21. The default is the address used on the V850E/MA1. */
  22. #ifndef V850E_UART_BASE_ADDR
  23. #define V850E_UART_BASE_ADDR(n) (0xFFFFFA00 + 0x10 * (n))
  24. #endif
  25. /* Addresses of specific UART control registers for channel N.
  26. The defaults are the addresses used on the V850E/MA1; if a platform
  27. wants to redefine any of these, it must redefine them all. */
  28. #ifndef V850E_UART_ASIM_ADDR
  29. #define V850E_UART_ASIM_ADDR(n) (V850E_UART_BASE_ADDR(n) + 0x0)
  30. #define V850E_UART_RXB_ADDR(n) (V850E_UART_BASE_ADDR(n) + 0x2)
  31. #define V850E_UART_ASIS_ADDR(n) (V850E_UART_BASE_ADDR(n) + 0x3)
  32. #define V850E_UART_TXB_ADDR(n) (V850E_UART_BASE_ADDR(n) + 0x4)
  33. #define V850E_UART_ASIF_ADDR(n) (V850E_UART_BASE_ADDR(n) + 0x5)
  34. #define V850E_UART_CKSR_ADDR(n) (V850E_UART_BASE_ADDR(n) + 0x6)
  35. #define V850E_UART_BRGC_ADDR(n) (V850E_UART_BASE_ADDR(n) + 0x7)
  36. #endif
  37. /* UART config registers. */
  38. #define V850E_UART_ASIM(n) (*(volatile u8 *)V850E_UART_ASIM_ADDR(n))
  39. /* Control bits for config registers. */
  40. #define V850E_UART_ASIM_CAE 0x80 /* clock enable */
  41. #define V850E_UART_ASIM_TXE 0x40 /* transmit enable */
  42. #define V850E_UART_ASIM_RXE 0x20 /* receive enable */
  43. #define V850E_UART_ASIM_PS_MASK 0x18 /* mask covering parity-select bits */
  44. #define V850E_UART_ASIM_PS_NONE 0x00 /* no parity */
  45. #define V850E_UART_ASIM_PS_ZERO 0x08 /* zero parity */
  46. #define V850E_UART_ASIM_PS_ODD 0x10 /* odd parity */
  47. #define V850E_UART_ASIM_PS_EVEN 0x18 /* even parity */
  48. #define V850E_UART_ASIM_CL_8 0x04 /* char len is 8 bits (otherwise, 7) */
  49. #define V850E_UART_ASIM_SL_2 0x02 /* 2 stop bits (otherwise, 1) */
  50. #define V850E_UART_ASIM_ISRM 0x01 /* generate INTSR interrupt on errors
  51. (otherwise, generate INTSER) */
  52. /* UART serial interface status registers. */
  53. #define V850E_UART_ASIS(n) (*(volatile u8 *)V850E_UART_ASIS_ADDR(n))
  54. /* Control bits for status registers. */
  55. #define V850E_UART_ASIS_PE 0x04 /* parity error */
  56. #define V850E_UART_ASIS_FE 0x02 /* framing error */
  57. #define V850E_UART_ASIS_OVE 0x01 /* overrun error */
  58. /* UART serial interface transmission status registers. */
  59. #define V850E_UART_ASIF(n) (*(volatile u8 *)V850E_UART_ASIF_ADDR(n))
  60. #define V850E_UART_ASIF_TXBF 0x02 /* transmit buffer flag (data in TXB) */
  61. #define V850E_UART_ASIF_TXSF 0x01 /* transmit shift flag (sending data) */
  62. /* UART receive buffer register. */
  63. #define V850E_UART_RXB(n) (*(volatile u8 *)V850E_UART_RXB_ADDR(n))
  64. /* UART transmit buffer register. */
  65. #define V850E_UART_TXB(n) (*(volatile u8 *)V850E_UART_TXB_ADDR(n))
  66. /* UART baud-rate generator control registers. */
  67. #define V850E_UART_CKSR(n) (*(volatile u8 *)V850E_UART_CKSR_ADDR(n))
  68. #define V850E_UART_CKSR_MAX 11
  69. #define V850E_UART_BRGC(n) (*(volatile u8 *)V850E_UART_BRGC_ADDR(n))
  70. #define V850E_UART_BRGC_MIN 8
  71. #ifndef V850E_UART_CKSR_MAX_FREQ
  72. #define V850E_UART_CKSR_MAX_FREQ (25*1000*1000)
  73. #endif
  74. /* Calculate the minimum value for CKSR on this processor. */
  75. static inline unsigned v850e_uart_cksr_min (void)
  76. {
  77. int min = 0;
  78. unsigned freq = V850E_UART_BASE_FREQ;
  79. while (freq > V850E_UART_CKSR_MAX_FREQ) {
  80. freq >>= 1;
  81. min++;
  82. }
  83. return min;
  84. }
  85. /* Slightly abstract interface used by driver. */
  86. /* Interrupts used by the UART. */
  87. /* Received when the most recently transmitted character has been sent. */
  88. #define V850E_UART_TX_IRQ(chan) IRQ_INTST (chan)
  89. /* Received when a new character has been received. */
  90. #define V850E_UART_RX_IRQ(chan) IRQ_INTSR (chan)
  91. /* UART clock generator interface. */
  92. /* This type encapsulates a particular uart frequency. */
  93. typedef struct {
  94. unsigned clk_divlog2;
  95. unsigned brgen_count;
  96. } v850e_uart_speed_t;
  97. /* Calculate a uart speed from BAUD for this uart. */
  98. static inline v850e_uart_speed_t v850e_uart_calc_speed (unsigned baud)
  99. {
  100. v850e_uart_speed_t speed;
  101. /* Calculate the log2 clock divider and baud-rate counter values
  102. (note that the UART divides the resulting clock by 2, so
  103. multiply BAUD by 2 here to compensate). */
  104. calc_counter_params (V850E_UART_BASE_FREQ, baud * 2,
  105. v850e_uart_cksr_min(),
  106. V850E_UART_CKSR_MAX, 8/*bits*/,
  107. &speed.clk_divlog2, &speed.brgen_count);
  108. return speed;
  109. }
  110. /* Return the current speed of uart channel CHAN. */
  111. static inline v850e_uart_speed_t v850e_uart_speed (unsigned chan)
  112. {
  113. v850e_uart_speed_t speed;
  114. speed.clk_divlog2 = V850E_UART_CKSR (chan);
  115. speed.brgen_count = V850E_UART_BRGC (chan);
  116. return speed;
  117. }
  118. /* Set the current speed of uart channel CHAN. */
  119. static inline void v850e_uart_set_speed(unsigned chan,v850e_uart_speed_t speed)
  120. {
  121. V850E_UART_CKSR (chan) = speed.clk_divlog2;
  122. V850E_UART_BRGC (chan) = speed.brgen_count;
  123. }
  124. static inline int
  125. v850e_uart_speed_eq (v850e_uart_speed_t speed1, v850e_uart_speed_t speed2)
  126. {
  127. return speed1.clk_divlog2 == speed2.clk_divlog2
  128. && speed1.brgen_count == speed2.brgen_count;
  129. }
  130. /* Minimum baud rate possible. */
  131. #define v850e_uart_min_baud() \
  132. ((V850E_UART_BASE_FREQ >> V850E_UART_CKSR_MAX) / (2 * 255) + 1)
  133. /* Maximum baud rate possible. The error is quite high at max, though. */
  134. #define v850e_uart_max_baud() \
  135. ((V850E_UART_BASE_FREQ >> v850e_uart_cksr_min()) / (2 *V850E_UART_BRGC_MIN))
  136. /* The `maximum' clock rate the uart can used, which is wanted (though not
  137. really used in any useful way) by the serial framework. */
  138. #define v850e_uart_max_clock() \
  139. ((V850E_UART_BASE_FREQ >> v850e_uart_cksr_min()) / 2)
  140. /* UART configuration interface. */
  141. /* Type of the uart config register; must be a scalar. */
  142. typedef u16 v850e_uart_config_t;
  143. /* The uart hardware config register for channel CHAN. */
  144. #define V850E_UART_CONFIG(chan) V850E_UART_ASIM (chan)
  145. /* This config bit set if the uart is enabled. */
  146. #define V850E_UART_CONFIG_ENABLED V850E_UART_ASIM_CAE
  147. /* If the uart _isn't_ enabled, store this value to it to do so. */
  148. #define V850E_UART_CONFIG_INIT V850E_UART_ASIM_CAE
  149. /* Store this config value to disable the uart channel completely. */
  150. #define V850E_UART_CONFIG_FINI 0
  151. /* Setting/clearing these bits enable/disable TX/RX, respectively (but
  152. otherwise generally leave things running). */
  153. #define V850E_UART_CONFIG_RX_ENABLE V850E_UART_ASIM_RXE
  154. #define V850E_UART_CONFIG_TX_ENABLE V850E_UART_ASIM_TXE
  155. /* These masks define which config bits affect TX/RX modes, respectively. */
  156. #define V850E_UART_CONFIG_RX_BITS \
  157. (V850E_UART_ASIM_PS_MASK | V850E_UART_ASIM_CL_8 | V850E_UART_ASIM_ISRM)
  158. #define V850E_UART_CONFIG_TX_BITS \
  159. (V850E_UART_ASIM_PS_MASK | V850E_UART_ASIM_CL_8 | V850E_UART_ASIM_SL_2)
  160. static inline v850e_uart_config_t v850e_uart_calc_config (unsigned cflags)
  161. {
  162. v850e_uart_config_t config = 0;
  163. /* Figure out new configuration of control register. */
  164. if (cflags & CSTOPB)
  165. /* Number of stop bits, 1 or 2. */
  166. config |= V850E_UART_ASIM_SL_2;
  167. if ((cflags & CSIZE) == CS8)
  168. /* Number of data bits, 7 or 8. */
  169. config |= V850E_UART_ASIM_CL_8;
  170. if (! (cflags & PARENB))
  171. /* No parity check/generation. */
  172. config |= V850E_UART_ASIM_PS_NONE;
  173. else if (cflags & PARODD)
  174. /* Odd parity check/generation. */
  175. config |= V850E_UART_ASIM_PS_ODD;
  176. else
  177. /* Even parity check/generation. */
  178. config |= V850E_UART_ASIM_PS_EVEN;
  179. if (cflags & CREAD)
  180. /* Reading enabled. */
  181. config |= V850E_UART_ASIM_RXE;
  182. config |= V850E_UART_ASIM_CAE;
  183. config |= V850E_UART_ASIM_TXE; /* Writing is always enabled. */
  184. config |= V850E_UART_ASIM_ISRM; /* Errors generate a read-irq. */
  185. return config;
  186. }
  187. /* This should delay as long as necessary for a recently written config
  188. setting to settle, before we turn the uart back on. */
  189. static inline void
  190. v850e_uart_config_delay (v850e_uart_config_t config, v850e_uart_speed_t speed)
  191. {
  192. /* The UART may not be reset properly unless we wait at least 2
  193. `basic-clocks' until turning on the TXE/RXE bits again.
  194. A `basic clock' is the clock used by the baud-rate generator,
  195. i.e., the cpu clock divided by the 2^new_clk_divlog2.
  196. The loop takes 2 insns, so loop CYCLES / 2 times. */
  197. register unsigned count = 1 << speed.clk_divlog2;
  198. while (--count != 0)
  199. /* nothing */;
  200. }
  201. /* RX/TX interface. */
  202. /* Return true if all characters awaiting transmission on uart channel N
  203. have been transmitted. */
  204. #define v850e_uart_xmit_done(n) \
  205. (! (V850E_UART_ASIF(n) & V850E_UART_ASIF_TXBF))
  206. /* Wait for this to be true. */
  207. #define v850e_uart_wait_for_xmit_done(n) \
  208. do { } while (! v850e_uart_xmit_done (n))
  209. /* Return true if uart channel N is ready to transmit a character. */
  210. #define v850e_uart_xmit_ok(n) \
  211. (v850e_uart_xmit_done(n) && v850e_uart_cts(n))
  212. /* Wait for this to be true. */
  213. #define v850e_uart_wait_for_xmit_ok(n) \
  214. do { } while (! v850e_uart_xmit_ok (n))
  215. /* Write character CH to uart channel CHAN. */
  216. #define v850e_uart_putc(chan, ch) (V850E_UART_TXB(chan) = (ch))
  217. /* Return latest character read on channel CHAN. */
  218. #define v850e_uart_getc(chan) V850E_UART_RXB (chan)
  219. /* Return bit-mask of uart error status. */
  220. #define v850e_uart_err(chan) V850E_UART_ASIS (chan)
  221. /* Various error bits set in the error result. */
  222. #define V850E_UART_ERR_OVERRUN V850E_UART_ASIS_OVE
  223. #define V850E_UART_ERR_FRAME V850E_UART_ASIS_FE
  224. #define V850E_UART_ERR_PARITY V850E_UART_ASIS_PE
  225. #endif /* __V850_V850E_UARTA_H__ */