uarths.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * SPDX-License-Identifier: Apache-2.0
  3. *
  4. * Copyright 2018 Canaan Inc.
  5. * Copyright (c) 2019 Western Digital Corporation or its affiliates.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "sysctl.h"
  20. #include "uarths.h"
  21. static volatile struct uarths *const uarths =
  22. (volatile struct uarths *)UARTHS_BASE_ADDR;
  23. void uarths_init(u32 baud_rate, enum uarths_stopbit stopbit)
  24. {
  25. u32 freq = sysctl_get_cpu_freq();
  26. u16 div = freq / baud_rate - 1;
  27. /* Set UART registers */
  28. uarths->div.div = div;
  29. uarths->txctrl.nstop = stopbit;
  30. uarths->txctrl.txen = 1;
  31. uarths->rxctrl.rxen = 1;
  32. uarths->txctrl.txcnt = 0;
  33. uarths->rxctrl.rxcnt = 0;
  34. uarths->ip.txwm = 1;
  35. uarths->ip.rxwm = 0;
  36. uarths->ie.txwm = 1;
  37. uarths->ie.rxwm = 0;
  38. /* Clear input */
  39. if (!uarths->rxdata.empty)
  40. (void)uarths_getc();
  41. }
  42. void uarths_putc(char c)
  43. {
  44. while (uarths->txdata.full)
  45. ;
  46. uarths->txdata.data = (u8)c;
  47. }
  48. int uarths_getc(void)
  49. {
  50. struct uarths_rxdata rx = uarths->rxdata;
  51. if (rx.empty)
  52. return -1;
  53. return rx.data;
  54. }