950-0855-gpio_brcmstb-Allow-to-build-for-ARCH_BCM2835.patch 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. From fa18902ee1e53ad391a455a01be3ab2ea1c5af5f Mon Sep 17 00:00:00 2001
  2. From: Dom Cobley <[email protected]>
  3. Date: Fri, 21 May 2021 12:33:38 +0100
  4. Subject: [PATCH] gpio_brcmstb: Allow to build for ARCH_BCM2835
  5. gpio-brcmstb: Report the correct bank width
  6. gpio: brcmstb: Use bank address as gpiochip label
  7. If the path to the device node is used as gpiochip label then
  8. gpio-brcmstb instances with multiple banks end up with duplicated
  9. names. Instead, use a combination of the driver name with the physical
  10. address of the bank, which is both unique and helpful for devmem
  11. debugging.
  12. Signed-off-by: Phil Elwell <[email protected]>
  13. gpio: mmio: Add DIRECT mode for shared access
  14. The generic MMIO GPIO library uses shadow registers for efficiency,
  15. but this breaks attempts by raspi-gpio to change other GPIOs in the
  16. same bank. Add a DIRECT mode that makes fewer assumptions about the
  17. existing register contents, but note that genuinely simultaneous
  18. accesses are likely to lose updates.
  19. Signed-off-by: Phil Elwell <[email protected]>
  20. gpio: brcmstb: Don't always clear interrupt mask
  21. If the GPIO controller is not being used as an interrupt source
  22. leave the interrupt mask register alone. On BCM2712 it might be used
  23. to generate interrupts to the VPU firmware, and on other devices it
  24. doesn't matter since no interrupts will be generated.
  25. Signed-off-by: Phil Elwell <[email protected]>
  26. ---
  27. drivers/gpio/Kconfig | 2 +-
  28. drivers/gpio/gpio-brcmstb.c | 14 ++--
  29. drivers/gpio/gpio-mmio.c | 124 ++++++++++++++++++++++++++++++++++--
  30. include/linux/gpio/driver.h | 1 +
  31. 4 files changed, 131 insertions(+), 10 deletions(-)
  32. --- a/drivers/gpio/Kconfig
  33. +++ b/drivers/gpio/Kconfig
  34. @@ -203,7 +203,7 @@ config GPIO_BCM_VIRT
  35. config GPIO_BRCMSTB
  36. tristate "BRCMSTB GPIO support"
  37. default y if (ARCH_BRCMSTB || BMIPS_GENERIC)
  38. - depends on OF_GPIO && (ARCH_BRCMSTB || BMIPS_GENERIC || COMPILE_TEST)
  39. + depends on OF_GPIO && (ARCH_BRCMSTB || BMIPS_GENERIC || ARCH_BCM2835 || COMPILE_TEST)
  40. select GPIO_GENERIC
  41. select IRQ_DOMAIN
  42. help
  43. --- a/drivers/gpio/gpio-brcmstb.c
  44. +++ b/drivers/gpio/gpio-brcmstb.c
  45. @@ -640,6 +640,8 @@ static int brcmstb_gpio_probe(struct pla
  46. #if defined(CONFIG_MIPS) && defined(__BIG_ENDIAN)
  47. flags = BGPIOF_BIG_ENDIAN_BYTE_ORDER;
  48. #endif
  49. + if (of_property_read_bool(np, "brcm,gpio-direct"))
  50. + flags |= BGPIOF_REG_DIRECT;
  51. of_property_for_each_u32(np, "brcm,gpio-bank-widths", prop, p,
  52. bank_width) {
  53. @@ -689,7 +691,9 @@ static int brcmstb_gpio_probe(struct pla
  54. }
  55. gc->owner = THIS_MODULE;
  56. - gc->label = devm_kasprintf(dev, GFP_KERNEL, "%pOF", np);
  57. + gc->label = devm_kasprintf(dev, GFP_KERNEL, "gpio-brcmstb@%zx",
  58. + (size_t)res->start +
  59. + GIO_BANK_OFF(bank->id, 0));
  60. if (!gc->label) {
  61. err = -ENOMEM;
  62. goto fail;
  63. @@ -698,7 +702,7 @@ static int brcmstb_gpio_probe(struct pla
  64. gc->of_gpio_n_cells = 2;
  65. gc->of_xlate = brcmstb_gpio_of_xlate;
  66. /* not all ngpio lines are valid, will use bank width later */
  67. - gc->ngpio = MAX_GPIO_PER_BANK;
  68. + gc->ngpio = bank_width;
  69. gc->offset = bank->id * MAX_GPIO_PER_BANK;
  70. if (priv->parent_irq > 0)
  71. gc->to_irq = brcmstb_gpio_to_irq;
  72. @@ -707,8 +711,10 @@ static int brcmstb_gpio_probe(struct pla
  73. * Mask all interrupts by default, since wakeup interrupts may
  74. * be retained from S5 cold boot
  75. */
  76. - need_wakeup_event |= !!__brcmstb_gpio_get_active_irqs(bank);
  77. - gc->write_reg(reg_base + GIO_MASK(bank->id), 0);
  78. + if (priv->parent_irq > 0) {
  79. + need_wakeup_event |= !!__brcmstb_gpio_get_active_irqs(bank);
  80. + gc->write_reg(reg_base + GIO_MASK(bank->id), 0);
  81. + }
  82. err = gpiochip_add_data(gc, bank);
  83. if (err) {
  84. --- a/drivers/gpio/gpio-mmio.c
  85. +++ b/drivers/gpio/gpio-mmio.c
  86. @@ -232,6 +232,25 @@ static void bgpio_set(struct gpio_chip *
  87. raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  88. }
  89. +static void bgpio_set_direct(struct gpio_chip *gc, unsigned int gpio, int val)
  90. +{
  91. + unsigned long mask = bgpio_line2mask(gc, gpio);
  92. + unsigned long flags;
  93. +
  94. + raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
  95. +
  96. + gc->bgpio_data = gc->read_reg(gc->reg_dat);
  97. +
  98. + if (val)
  99. + gc->bgpio_data |= mask;
  100. + else
  101. + gc->bgpio_data &= ~mask;
  102. +
  103. + gc->write_reg(gc->reg_dat, gc->bgpio_data);
  104. +
  105. + raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  106. +}
  107. +
  108. static void bgpio_set_with_clear(struct gpio_chip *gc, unsigned int gpio,
  109. int val)
  110. {
  111. @@ -324,6 +343,27 @@ static void bgpio_set_multiple_with_clea
  112. gc->write_reg(gc->reg_clr, clear_mask);
  113. }
  114. +static void bgpio_set_multiple_direct(struct gpio_chip *gc,
  115. + unsigned long *mask,
  116. + unsigned long *bits)
  117. +{
  118. + unsigned long flags;
  119. + unsigned long set_mask, clear_mask;
  120. +
  121. + raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
  122. +
  123. + bgpio_multiple_get_masks(gc, mask, bits, &set_mask, &clear_mask);
  124. +
  125. + gc->bgpio_data = gc->read_reg(gc->reg_dat);
  126. +
  127. + gc->bgpio_data |= set_mask;
  128. + gc->bgpio_data &= ~clear_mask;
  129. +
  130. + gc->write_reg(gc->reg_dat, gc->bgpio_data);
  131. +
  132. + raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  133. +}
  134. +
  135. static int bgpio_simple_dir_in(struct gpio_chip *gc, unsigned int gpio)
  136. {
  137. return 0;
  138. @@ -361,6 +401,29 @@ static int bgpio_dir_in(struct gpio_chip
  139. return 0;
  140. }
  141. +static int bgpio_dir_in_direct(struct gpio_chip *gc, unsigned int gpio)
  142. +{
  143. + unsigned long flags;
  144. +
  145. + raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
  146. +
  147. + if (gc->reg_dir_in)
  148. + gc->bgpio_dir = ~gc->read_reg(gc->reg_dir_in);
  149. + if (gc->reg_dir_out)
  150. + gc->bgpio_dir = gc->read_reg(gc->reg_dir_out);
  151. +
  152. + gc->bgpio_dir &= ~bgpio_line2mask(gc, gpio);
  153. +
  154. + if (gc->reg_dir_in)
  155. + gc->write_reg(gc->reg_dir_in, ~gc->bgpio_dir);
  156. + if (gc->reg_dir_out)
  157. + gc->write_reg(gc->reg_dir_out, gc->bgpio_dir);
  158. +
  159. + raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  160. +
  161. + return 0;
  162. +}
  163. +
  164. static int bgpio_get_dir(struct gpio_chip *gc, unsigned int gpio)
  165. {
  166. /* Return 0 if output, 1 if input */
  167. @@ -399,6 +462,28 @@ static void bgpio_dir_out(struct gpio_ch
  168. raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  169. }
  170. +static void bgpio_dir_out_direct(struct gpio_chip *gc, unsigned int gpio,
  171. + int val)
  172. +{
  173. + unsigned long flags;
  174. +
  175. + raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
  176. +
  177. + if (gc->reg_dir_in)
  178. + gc->bgpio_dir = ~gc->read_reg(gc->reg_dir_in);
  179. + if (gc->reg_dir_out)
  180. + gc->bgpio_dir = gc->read_reg(gc->reg_dir_out);
  181. +
  182. + gc->bgpio_dir |= bgpio_line2mask(gc, gpio);
  183. +
  184. + if (gc->reg_dir_in)
  185. + gc->write_reg(gc->reg_dir_in, ~gc->bgpio_dir);
  186. + if (gc->reg_dir_out)
  187. + gc->write_reg(gc->reg_dir_out, gc->bgpio_dir);
  188. +
  189. + raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  190. +}
  191. +
  192. static int bgpio_dir_out_dir_first(struct gpio_chip *gc, unsigned int gpio,
  193. int val)
  194. {
  195. @@ -415,6 +500,22 @@ static int bgpio_dir_out_val_first(struc
  196. return 0;
  197. }
  198. +static int bgpio_dir_out_dir_first_direct(struct gpio_chip *gc,
  199. + unsigned int gpio, int val)
  200. +{
  201. + bgpio_dir_out_direct(gc, gpio, val);
  202. + gc->set(gc, gpio, val);
  203. + return 0;
  204. +}
  205. +
  206. +static int bgpio_dir_out_val_first_direct(struct gpio_chip *gc,
  207. + unsigned int gpio, int val)
  208. +{
  209. + gc->set(gc, gpio, val);
  210. + bgpio_dir_out_direct(gc, gpio, val);
  211. + return 0;
  212. +}
  213. +
  214. static int bgpio_setup_accessors(struct device *dev,
  215. struct gpio_chip *gc,
  216. bool byte_be)
  217. @@ -508,6 +609,9 @@ static int bgpio_setup_io(struct gpio_ch
  218. } else if (flags & BGPIOF_NO_OUTPUT) {
  219. gc->set = bgpio_set_none;
  220. gc->set_multiple = NULL;
  221. + } else if (flags & BGPIOF_REG_DIRECT) {
  222. + gc->set = bgpio_set_direct;
  223. + gc->set_multiple = bgpio_set_multiple_direct;
  224. } else {
  225. gc->set = bgpio_set;
  226. gc->set_multiple = bgpio_set_multiple;
  227. @@ -544,11 +648,21 @@ static int bgpio_setup_direction(struct
  228. if (dirout || dirin) {
  229. gc->reg_dir_out = dirout;
  230. gc->reg_dir_in = dirin;
  231. - if (flags & BGPIOF_NO_SET_ON_INPUT)
  232. - gc->direction_output = bgpio_dir_out_dir_first;
  233. - else
  234. - gc->direction_output = bgpio_dir_out_val_first;
  235. - gc->direction_input = bgpio_dir_in;
  236. + if (flags & BGPIOF_REG_DIRECT) {
  237. + if (flags & BGPIOF_NO_SET_ON_INPUT)
  238. + gc->direction_output =
  239. + bgpio_dir_out_dir_first_direct;
  240. + else
  241. + gc->direction_output =
  242. + bgpio_dir_out_val_first_direct;
  243. + gc->direction_input = bgpio_dir_in_direct;
  244. + } else {
  245. + if (flags & BGPIOF_NO_SET_ON_INPUT)
  246. + gc->direction_output = bgpio_dir_out_dir_first;
  247. + else
  248. + gc->direction_output = bgpio_dir_out_val_first;
  249. + gc->direction_input = bgpio_dir_in;
  250. + }
  251. gc->get_direction = bgpio_get_dir;
  252. } else {
  253. if (flags & BGPIOF_NO_OUTPUT)
  254. --- a/include/linux/gpio/driver.h
  255. +++ b/include/linux/gpio/driver.h
  256. @@ -690,6 +690,7 @@ int bgpio_init(struct gpio_chip *gc, str
  257. #define BGPIOF_READ_OUTPUT_REG_SET BIT(4) /* reg_set stores output value */
  258. #define BGPIOF_NO_OUTPUT BIT(5) /* only input */
  259. #define BGPIOF_NO_SET_ON_INPUT BIT(6)
  260. +#define BGPIOF_REG_DIRECT BIT(7) /* ignore shadow registers */
  261. int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
  262. irq_hw_number_t hwirq);