pin-pl061.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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-26 GuEe-GUI first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include "dev_pin_dm.h"
  14. #define PL061_DIR 0x400
  15. #define PL061_IS 0x404
  16. #define PL061_IBE 0x408
  17. #define PL061_IEV 0x40c
  18. #define PL061_IE 0x410
  19. #define PL061_RIS 0x414
  20. #define PL061_MIS 0x418
  21. #define PL061_IC 0x41c
  22. #define PL061_GPIO_NR 8
  23. struct pl061
  24. {
  25. struct rt_device_pin parent;
  26. int irq;
  27. void *base;
  28. struct rt_clk *pclk;
  29. struct rt_spinlock spinlock;
  30. };
  31. #define raw_to_pl061(raw) rt_container_of(raw, struct pl061, parent)
  32. rt_inline rt_uint8_t pl061_read(struct pl061 *pl061, int offset)
  33. {
  34. return HWREG8(pl061->base + offset);
  35. }
  36. rt_inline void pl061_write(struct pl061 *pl061, int offset, rt_uint8_t value)
  37. {
  38. HWREG8(pl061->base + offset) = value;
  39. }
  40. static void pl061_isr(int irqno, void *param)
  41. {
  42. rt_uint8_t mask = 0;
  43. rt_ubase_t pending, level;
  44. struct pl061 *pl061 = (struct pl061 *)param;
  45. level = rt_spin_lock_irqsave(&pl061->spinlock);
  46. pending = pl061_read(pl061, PL061_MIS);
  47. rt_spin_unlock_irqrestore(&pl061->spinlock, level);
  48. if (pending)
  49. {
  50. for (int pin = 0; pin < PL061_GPIO_NR; ++pin)
  51. {
  52. if (pending & RT_BIT(pin))
  53. {
  54. mask |= RT_BIT(pin);
  55. pin_pic_handle_isr(&pl061->parent, pin);
  56. }
  57. }
  58. level = rt_spin_lock_irqsave(&pl061->spinlock);
  59. pl061_write(pl061, PL061_IC, mask);
  60. rt_spin_unlock_irqrestore(&pl061->spinlock, level);
  61. }
  62. }
  63. static void pl061_pin_mode(struct rt_device *device, rt_base_t pin, rt_uint8_t mode)
  64. {
  65. struct pl061 *pl061 = raw_to_pl061(device);
  66. if (pin >= 0 && pin < PL061_GPIO_NR)
  67. {
  68. rt_base_t level = rt_spin_lock_irqsave(&pl061->spinlock);
  69. switch (mode)
  70. {
  71. case PIN_MODE_OUTPUT:
  72. pl061_write(pl061, RT_BIT(pin + 2), 1 << pin);
  73. pl061_write(pl061, PL061_DIR, pl061_read(pl061, PL061_DIR) | RT_BIT(pin));
  74. /*
  75. * gpio value is set again, because pl061 doesn't allow to set value
  76. * of a gpio pin before configuring it in OUT mode.
  77. */
  78. pl061_write(pl061, RT_BIT(pin + 2), 1 << pin);
  79. break;
  80. case PIN_MODE_INPUT:
  81. pl061_write(pl061, PL061_DIR, pl061_read(pl061, PL061_DIR) & ~RT_BIT(pin));
  82. break;
  83. default:
  84. break;
  85. }
  86. rt_spin_unlock_irqrestore(&pl061->spinlock, level);
  87. }
  88. }
  89. static void pl061_pin_write(struct rt_device *device, rt_base_t pin, rt_uint8_t value)
  90. {
  91. struct pl061 *pl061 = raw_to_pl061(device);
  92. if (pin >= 0 && pin < PL061_GPIO_NR)
  93. {
  94. pl061_write(pl061, RT_BIT(pin + 2), !!value << pin);
  95. }
  96. }
  97. static rt_ssize_t pl061_pin_read(struct rt_device *device, rt_base_t pin)
  98. {
  99. rt_int8_t value = -RT_EINVAL;
  100. struct pl061 *pl061 = raw_to_pl061(device);
  101. if (pin >= 0 && pin < PL061_GPIO_NR)
  102. {
  103. value = !!pl061_read(pl061, RT_BIT(pin + 2));
  104. }
  105. return value;
  106. }
  107. static rt_err_t pl061_pin_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint8_t enabled)
  108. {
  109. rt_err_t err = RT_EOK;
  110. struct pl061 *pl061 = raw_to_pl061(device);
  111. if (pin >= 0 && pin < PL061_GPIO_NR)
  112. {
  113. rt_uint8_t gpioie, mask = RT_BIT(pin);
  114. rt_ubase_t level = rt_spin_lock_irqsave(&pl061->spinlock);
  115. if (enabled)
  116. {
  117. gpioie = pl061_read(pl061, PL061_IE) | mask;
  118. }
  119. else
  120. {
  121. gpioie = pl061_read(pl061, PL061_IE) & ~mask;
  122. }
  123. pl061_write(pl061, PL061_IE, gpioie);
  124. rt_spin_unlock_irqrestore(&pl061->spinlock, level);
  125. }
  126. else
  127. {
  128. err = -RT_EINVAL;
  129. }
  130. return err;
  131. }
  132. static rt_err_t pl061_pin_irq_mode(struct rt_device *device, rt_base_t pin, rt_uint8_t mode)
  133. {
  134. rt_err_t err = RT_EOK;
  135. struct pl061 *pl061 = raw_to_pl061(device);
  136. if (pin >= 0 && pin < PL061_GPIO_NR)
  137. {
  138. rt_uint8_t gpiois, gpioibe, gpioiev, bit = RT_BIT(pin);
  139. rt_ubase_t level = rt_spin_lock_irqsave(&pl061->spinlock);
  140. gpioiev = pl061_read(pl061, PL061_IEV);
  141. gpiois = pl061_read(pl061, PL061_IS);
  142. gpioibe = pl061_read(pl061, PL061_IBE);
  143. if (mode == PIN_IRQ_MODE_HIGH_LEVEL || mode == PIN_IRQ_MODE_LOW_LEVEL)
  144. {
  145. rt_bool_t polarity = (mode == PIN_IRQ_MODE_HIGH_LEVEL);
  146. /* Disable edge detection */
  147. gpioibe &= ~bit;
  148. /* Enable level detection */
  149. gpiois |= bit;
  150. /* Select polarity */
  151. if (polarity)
  152. {
  153. gpioiev |= bit;
  154. }
  155. else
  156. {
  157. gpioiev &= ~bit;
  158. }
  159. }
  160. else if (mode == PIN_IRQ_MODE_RISING_FALLING)
  161. {
  162. /* Disable level detection */
  163. gpiois &= ~bit;
  164. /* Select both edges, setting this makes PL061_EV be ignored */
  165. gpioibe |= bit;
  166. }
  167. else if (mode == PIN_IRQ_MODE_RISING || mode == PIN_IRQ_MODE_FALLING)
  168. {
  169. rt_bool_t rising = (mode == PIN_IRQ_MODE_RISING);
  170. /* Disable level detection */
  171. gpiois &= ~bit;
  172. /* Clear detection on both edges */
  173. gpioibe &= ~bit;
  174. /* Select edge */
  175. if (rising)
  176. {
  177. gpioiev |= bit;
  178. }
  179. else
  180. {
  181. gpioiev &= ~bit;
  182. }
  183. }
  184. else
  185. {
  186. /* No trigger: disable everything */
  187. gpiois &= ~bit;
  188. gpioibe &= ~bit;
  189. gpioiev &= ~bit;
  190. }
  191. pl061_write(pl061, PL061_IS, gpiois);
  192. pl061_write(pl061, PL061_IBE, gpioibe);
  193. pl061_write(pl061, PL061_IEV, gpioiev);
  194. rt_spin_unlock_irqrestore(&pl061->spinlock, level);
  195. }
  196. else
  197. {
  198. err = -RT_EINVAL;
  199. }
  200. return err;
  201. }
  202. static const struct rt_pin_ops pl061_pin_ops =
  203. {
  204. .pin_mode = pl061_pin_mode,
  205. .pin_write = pl061_pin_write,
  206. .pin_read = pl061_pin_read,
  207. .pin_irq_enable = pl061_pin_irq_enable,
  208. .pin_irq_mode = pl061_pin_irq_mode,
  209. };
  210. static rt_err_t pl061_probe(struct rt_platform_device *pdev)
  211. {
  212. rt_err_t err;
  213. struct rt_device *dev = &pdev->parent;
  214. struct pl061 *pl061 = rt_calloc(1, sizeof(*pl061));
  215. if (!pl061)
  216. {
  217. return -RT_ENOMEM;
  218. }
  219. pl061->base = rt_dm_dev_iomap(dev, 0);
  220. if (!pl061->base)
  221. {
  222. err = -RT_EIO;
  223. goto _fail;
  224. }
  225. pl061->irq = rt_dm_dev_get_irq(dev, 0);
  226. if (pl061->irq < 0)
  227. {
  228. err = pl061->irq;
  229. goto _fail;
  230. }
  231. pl061->pclk = rt_clk_get_by_name(dev, "apb_pclk");
  232. if (rt_is_err(pl061->pclk))
  233. {
  234. err = rt_ptr_err(pl061->pclk);
  235. goto _fail;
  236. }
  237. if ((err = rt_clk_prepare_enable(pl061->pclk)))
  238. {
  239. goto _fail;
  240. }
  241. rt_dm_dev_bind_fwdata(dev, RT_NULL, &pl061->parent);
  242. rt_spin_lock_init(&pl061->spinlock);
  243. pl061->parent.ops = &pl061_pin_ops;
  244. pin_api_init(&pl061->parent, PL061_GPIO_NR);
  245. pin_pic_init(&pl061->parent, pl061->irq);
  246. rt_hw_interrupt_install(pl061->irq, pl061_isr, pl061, "gpio-pl061");
  247. rt_hw_interrupt_umask(pl061->irq);
  248. return RT_EOK;
  249. _fail:
  250. if (pl061->base)
  251. {
  252. rt_iounmap(pl061->base);
  253. }
  254. if (!rt_is_err_or_null(pl061->pclk))
  255. {
  256. rt_clk_disable_unprepare(pl061->pclk);
  257. rt_clk_put(pl061->pclk);
  258. }
  259. rt_free(pl061);
  260. return err;
  261. }
  262. static const struct rt_ofw_node_id pl061_ofw_ids[] =
  263. {
  264. { .compatible = "arm,pl061" },
  265. { /* sentinel */ }
  266. };
  267. static struct rt_platform_driver pl061_driver =
  268. {
  269. .name = "pin-pl061",
  270. .ids = pl061_ofw_ids,
  271. .probe = pl061_probe,
  272. };
  273. static int pl061_drv_register(void)
  274. {
  275. rt_platform_driver_register(&pl061_driver);
  276. return 0;
  277. }
  278. INIT_SUBSYS_EXPORT(pl061_drv_register);