8250-ofw.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-11-09 GuEe-GUI first version
  9. */
  10. #include <rtthread.h>
  11. #include "8250.h"
  12. struct ofw_platform_8250
  13. {
  14. struct serial8250 parent;
  15. struct rt_reset_control *rstc;
  16. };
  17. #define to_ofw_platform_8250(serial8250) rt_container_of(serial8250, struct ofw_platform_8250, parent)
  18. static void ofw_platform_8250_free_resource(struct ofw_platform_8250 *op8250)
  19. {
  20. struct serial8250 *serial = &op8250->parent;
  21. if (serial->base)
  22. {
  23. rt_iounmap(serial->base);
  24. }
  25. if (!rt_is_err_or_null(serial->clk))
  26. {
  27. rt_clk_disable_unprepare(serial->clk);
  28. rt_clk_put(serial->clk);
  29. }
  30. if (!rt_is_err_or_null(op8250->rstc))
  31. {
  32. rt_reset_control_put(op8250->rstc);
  33. }
  34. rt_free(op8250);
  35. }
  36. static void op8250_remove(struct serial8250 *serial)
  37. {
  38. struct ofw_platform_8250 *op8250 = to_ofw_platform_8250(serial);
  39. ofw_platform_8250_free_resource(op8250);
  40. }
  41. static rt_err_t ofw_platform_8250_probe(struct rt_platform_device *pdev)
  42. {
  43. rt_err_t err;
  44. rt_uint32_t val;
  45. struct serial8250 *serial;
  46. struct ofw_platform_8250 *op8250;
  47. struct rt_ofw_node *np = pdev->parent.ofw_node;
  48. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  49. if (rt_ofw_prop_read_bool(np, "used-by-rtas"))
  50. {
  51. return -RT_EBUSY;
  52. }
  53. op8250 = serial8250_alloc(op8250);
  54. if (!op8250)
  55. {
  56. return -RT_ENOMEM;
  57. }
  58. serial = &op8250->parent;
  59. serial->base = rt_ofw_iomap(np, 0);
  60. if (!serial->base)
  61. {
  62. err = -RT_EIO;
  63. goto _fail;
  64. }
  65. serial->irq = rt_ofw_get_irq(np, 0);
  66. if (serial->irq < 0)
  67. {
  68. err = serial->irq;
  69. goto _fail;
  70. }
  71. if (!rt_ofw_prop_read_u32(np, "clock-frequency", &val))
  72. {
  73. serial->freq = val;
  74. }
  75. else
  76. {
  77. serial->clk = rt_ofw_get_clk(np, 0);
  78. if (rt_is_err(serial->clk))
  79. {
  80. err = rt_ptr_err(serial->clk);
  81. goto _fail;
  82. }
  83. if ((err = rt_clk_prepare_enable(serial->clk)))
  84. {
  85. goto _fail;
  86. }
  87. serial->freq = rt_clk_get_rate(serial->clk);
  88. }
  89. if (!rt_ofw_prop_read_u32(np, "reg-shift", &val))
  90. {
  91. serial->regshift = val;
  92. }
  93. serial->iotype = PORT_MMIO;
  94. if (!rt_ofw_prop_read_u32(np, "reg-io-width", &val))
  95. {
  96. switch (val)
  97. {
  98. case 1:
  99. serial->iotype = PORT_MMIO;
  100. break;
  101. case 2:
  102. serial->iotype = PORT_MMIO16;
  103. break;
  104. case 4:
  105. serial->iotype = rt_ofw_prop_read_bool(np, "big-endian") ?
  106. PORT_MMIO32BE : PORT_MMIO32;
  107. break;
  108. }
  109. }
  110. op8250->rstc = rt_ofw_get_reset_control_by_index(np, 0);
  111. if (rt_is_err(op8250->rstc))
  112. {
  113. err = rt_ptr_err(op8250->rstc);
  114. goto _fail;
  115. }
  116. if (op8250->rstc && (err = rt_reset_control_deassert(op8250->rstc)))
  117. {
  118. goto _fail;
  119. }
  120. rt_dm_dev_bind_fwdata(&serial->parent.parent, pdev->parent.ofw_node, &serial->parent);
  121. pdev->parent.user_data = serial;
  122. serial->parent.ops = &serial8250_uart_ops;
  123. serial->parent.config = config;
  124. serial->remove = &op8250_remove;
  125. serial->data = op8250;
  126. if ((err = serial8250_setup(serial)))
  127. {
  128. goto _fail;
  129. }
  130. return RT_EOK;
  131. _fail:
  132. ofw_platform_8250_free_resource(op8250);
  133. return err;
  134. }
  135. static rt_err_t ofw_platform_8250_remove(struct rt_platform_device *pdev)
  136. {
  137. struct rt_device *dev = &pdev->parent;
  138. struct serial8250 *serial = dev->user_data;
  139. rt_dm_dev_unbind_fwdata(dev, RT_NULL);
  140. return serial8250_remove(serial);
  141. }
  142. static const struct rt_ofw_node_id ofw_platform_8250_ofw_ids[] =
  143. {
  144. { .type = "ttyS", .compatible = "ns16550a" },
  145. { .type = "ttyS", .compatible = "ns16550" },
  146. #ifndef RT_SERIAL_8250_BCM7271
  147. { .type = "ttyS", .compatible = "brcm,bcm7271-uart" },
  148. #endif
  149. { /* sentinel */ }
  150. };
  151. static struct rt_platform_driver ofw_platform_8250_driver =
  152. {
  153. .name = "8250-ofw",
  154. .ids = ofw_platform_8250_ofw_ids,
  155. .probe = ofw_platform_8250_probe,
  156. .remove = ofw_platform_8250_remove,
  157. };
  158. static int ofw_platform_8250_drv_register(void)
  159. {
  160. rt_platform_driver_register(&ofw_platform_8250_driver);
  161. return 0;
  162. }
  163. INIT_PLATFORM_EXPORT(ofw_platform_8250_drv_register);