driver_chipcommon.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Broadcom specific AMBA
  3. * ChipCommon core driver
  4. *
  5. * Copyright 2005, Broadcom Corporation
  6. * Copyright 2006, 2007, Michael Buesch <m@bues.ch>
  7. * Copyright 2012, Hauke Mehrtens <hauke@hauke-m.de>
  8. *
  9. * Licensed under the GNU/GPL. See COPYING for details.
  10. */
  11. #include "bcma_private.h"
  12. #include <linux/bcm47xx_wdt.h>
  13. #include <linux/export.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/bcma/bcma.h>
  16. static inline u32 bcma_cc_write32_masked(struct bcma_drv_cc *cc, u16 offset,
  17. u32 mask, u32 value)
  18. {
  19. value &= mask;
  20. value |= bcma_cc_read32(cc, offset) & ~mask;
  21. bcma_cc_write32(cc, offset, value);
  22. return value;
  23. }
  24. static u32 bcma_chipco_alp_clock(struct bcma_drv_cc *cc)
  25. {
  26. if (cc->capabilities & BCMA_CC_CAP_PMU)
  27. return bcma_pmu_alp_clock(cc);
  28. return 20000000;
  29. }
  30. static u32 bcma_chipco_watchdog_get_max_timer(struct bcma_drv_cc *cc)
  31. {
  32. struct bcma_bus *bus = cc->core->bus;
  33. u32 nb;
  34. if (cc->capabilities & BCMA_CC_CAP_PMU) {
  35. if (bus->chipinfo.id == BCMA_CHIP_ID_BCM4706)
  36. nb = 32;
  37. else if (cc->core->id.rev < 26)
  38. nb = 16;
  39. else
  40. nb = (cc->core->id.rev >= 37) ? 32 : 24;
  41. } else {
  42. nb = 28;
  43. }
  44. if (nb == 32)
  45. return 0xffffffff;
  46. else
  47. return (1 << nb) - 1;
  48. }
  49. static u32 bcma_chipco_watchdog_timer_set_wdt(struct bcm47xx_wdt *wdt,
  50. u32 ticks)
  51. {
  52. struct bcma_drv_cc *cc = bcm47xx_wdt_get_drvdata(wdt);
  53. return bcma_chipco_watchdog_timer_set(cc, ticks);
  54. }
  55. static u32 bcma_chipco_watchdog_timer_set_ms_wdt(struct bcm47xx_wdt *wdt,
  56. u32 ms)
  57. {
  58. struct bcma_drv_cc *cc = bcm47xx_wdt_get_drvdata(wdt);
  59. u32 ticks;
  60. ticks = bcma_chipco_watchdog_timer_set(cc, cc->ticks_per_ms * ms);
  61. return ticks / cc->ticks_per_ms;
  62. }
  63. static int bcma_chipco_watchdog_ticks_per_ms(struct bcma_drv_cc *cc)
  64. {
  65. struct bcma_bus *bus = cc->core->bus;
  66. if (cc->capabilities & BCMA_CC_CAP_PMU) {
  67. if (bus->chipinfo.id == BCMA_CHIP_ID_BCM4706)
  68. /* 4706 CC and PMU watchdogs are clocked at 1/4 of ALP clock */
  69. return bcma_chipco_alp_clock(cc) / 4000;
  70. else
  71. /* based on 32KHz ILP clock */
  72. return 32;
  73. } else {
  74. return bcma_chipco_alp_clock(cc) / 1000;
  75. }
  76. }
  77. int bcma_chipco_watchdog_register(struct bcma_drv_cc *cc)
  78. {
  79. struct bcm47xx_wdt wdt = {};
  80. struct platform_device *pdev;
  81. wdt.driver_data = cc;
  82. wdt.timer_set = bcma_chipco_watchdog_timer_set_wdt;
  83. wdt.timer_set_ms = bcma_chipco_watchdog_timer_set_ms_wdt;
  84. wdt.max_timer_ms = bcma_chipco_watchdog_get_max_timer(cc) / cc->ticks_per_ms;
  85. pdev = platform_device_register_data(NULL, "bcm47xx-wdt",
  86. cc->core->bus->num, &wdt,
  87. sizeof(wdt));
  88. if (IS_ERR(pdev))
  89. return PTR_ERR(pdev);
  90. cc->watchdog = pdev;
  91. return 0;
  92. }
  93. void bcma_core_chipcommon_early_init(struct bcma_drv_cc *cc)
  94. {
  95. if (cc->early_setup_done)
  96. return;
  97. if (cc->core->id.rev >= 11)
  98. cc->status = bcma_cc_read32(cc, BCMA_CC_CHIPSTAT);
  99. cc->capabilities = bcma_cc_read32(cc, BCMA_CC_CAP);
  100. if (cc->core->id.rev >= 35)
  101. cc->capabilities_ext = bcma_cc_read32(cc, BCMA_CC_CAP_EXT);
  102. if (cc->capabilities & BCMA_CC_CAP_PMU)
  103. bcma_pmu_early_init(cc);
  104. cc->early_setup_done = true;
  105. }
  106. void bcma_core_chipcommon_init(struct bcma_drv_cc *cc)
  107. {
  108. u32 leddc_on = 10;
  109. u32 leddc_off = 90;
  110. if (cc->setup_done)
  111. return;
  112. bcma_core_chipcommon_early_init(cc);
  113. if (cc->core->id.rev >= 20) {
  114. bcma_cc_write32(cc, BCMA_CC_GPIOPULLUP, 0);
  115. bcma_cc_write32(cc, BCMA_CC_GPIOPULLDOWN, 0);
  116. }
  117. if (cc->capabilities & BCMA_CC_CAP_PMU)
  118. bcma_pmu_init(cc);
  119. if (cc->capabilities & BCMA_CC_CAP_PCTL)
  120. bcma_err(cc->core->bus, "Power control not implemented!\n");
  121. if (cc->core->id.rev >= 16) {
  122. if (cc->core->bus->sprom.leddc_on_time &&
  123. cc->core->bus->sprom.leddc_off_time) {
  124. leddc_on = cc->core->bus->sprom.leddc_on_time;
  125. leddc_off = cc->core->bus->sprom.leddc_off_time;
  126. }
  127. bcma_cc_write32(cc, BCMA_CC_GPIOTIMER,
  128. ((leddc_on << BCMA_CC_GPIOTIMER_ONTIME_SHIFT) |
  129. (leddc_off << BCMA_CC_GPIOTIMER_OFFTIME_SHIFT)));
  130. }
  131. cc->ticks_per_ms = bcma_chipco_watchdog_ticks_per_ms(cc);
  132. cc->setup_done = true;
  133. }
  134. /* Set chip watchdog reset timer to fire in 'ticks' backplane cycles */
  135. u32 bcma_chipco_watchdog_timer_set(struct bcma_drv_cc *cc, u32 ticks)
  136. {
  137. u32 maxt;
  138. enum bcma_clkmode clkmode;
  139. maxt = bcma_chipco_watchdog_get_max_timer(cc);
  140. if (cc->capabilities & BCMA_CC_CAP_PMU) {
  141. if (ticks == 1)
  142. ticks = 2;
  143. else if (ticks > maxt)
  144. ticks = maxt;
  145. bcma_cc_write32(cc, BCMA_CC_PMU_WATCHDOG, ticks);
  146. } else {
  147. clkmode = ticks ? BCMA_CLKMODE_FAST : BCMA_CLKMODE_DYNAMIC;
  148. bcma_core_set_clockmode(cc->core, clkmode);
  149. if (ticks > maxt)
  150. ticks = maxt;
  151. /* instant NMI */
  152. bcma_cc_write32(cc, BCMA_CC_WATCHDOG, ticks);
  153. }
  154. return ticks;
  155. }
  156. void bcma_chipco_irq_mask(struct bcma_drv_cc *cc, u32 mask, u32 value)
  157. {
  158. bcma_cc_write32_masked(cc, BCMA_CC_IRQMASK, mask, value);
  159. }
  160. u32 bcma_chipco_irq_status(struct bcma_drv_cc *cc, u32 mask)
  161. {
  162. return bcma_cc_read32(cc, BCMA_CC_IRQSTAT) & mask;
  163. }
  164. u32 bcma_chipco_gpio_in(struct bcma_drv_cc *cc, u32 mask)
  165. {
  166. return bcma_cc_read32(cc, BCMA_CC_GPIOIN) & mask;
  167. }
  168. u32 bcma_chipco_gpio_out(struct bcma_drv_cc *cc, u32 mask, u32 value)
  169. {
  170. return bcma_cc_write32_masked(cc, BCMA_CC_GPIOOUT, mask, value);
  171. }
  172. u32 bcma_chipco_gpio_outen(struct bcma_drv_cc *cc, u32 mask, u32 value)
  173. {
  174. return bcma_cc_write32_masked(cc, BCMA_CC_GPIOOUTEN, mask, value);
  175. }
  176. u32 bcma_chipco_gpio_control(struct bcma_drv_cc *cc, u32 mask, u32 value)
  177. {
  178. return bcma_cc_write32_masked(cc, BCMA_CC_GPIOCTL, mask, value);
  179. }
  180. EXPORT_SYMBOL_GPL(bcma_chipco_gpio_control);
  181. u32 bcma_chipco_gpio_intmask(struct bcma_drv_cc *cc, u32 mask, u32 value)
  182. {
  183. return bcma_cc_write32_masked(cc, BCMA_CC_GPIOIRQ, mask, value);
  184. }
  185. u32 bcma_chipco_gpio_polarity(struct bcma_drv_cc *cc, u32 mask, u32 value)
  186. {
  187. return bcma_cc_write32_masked(cc, BCMA_CC_GPIOPOL, mask, value);
  188. }
  189. #ifdef CONFIG_BCMA_DRIVER_MIPS
  190. void bcma_chipco_serial_init(struct bcma_drv_cc *cc)
  191. {
  192. unsigned int irq;
  193. u32 baud_base;
  194. u32 i;
  195. unsigned int ccrev = cc->core->id.rev;
  196. struct bcma_serial_port *ports = cc->serial_ports;
  197. if (ccrev >= 11 && ccrev != 15) {
  198. baud_base = bcma_chipco_alp_clock(cc);
  199. if (ccrev >= 21) {
  200. /* Turn off UART clock before switching clocksource. */
  201. bcma_cc_write32(cc, BCMA_CC_CORECTL,
  202. bcma_cc_read32(cc, BCMA_CC_CORECTL)
  203. & ~BCMA_CC_CORECTL_UARTCLKEN);
  204. }
  205. /* Set the override bit so we don't divide it */
  206. bcma_cc_write32(cc, BCMA_CC_CORECTL,
  207. bcma_cc_read32(cc, BCMA_CC_CORECTL)
  208. | BCMA_CC_CORECTL_UARTCLK0);
  209. if (ccrev >= 21) {
  210. /* Re-enable the UART clock. */
  211. bcma_cc_write32(cc, BCMA_CC_CORECTL,
  212. bcma_cc_read32(cc, BCMA_CC_CORECTL)
  213. | BCMA_CC_CORECTL_UARTCLKEN);
  214. }
  215. } else {
  216. bcma_err(cc->core->bus, "serial not supported on this device ccrev: 0x%x\n", ccrev);
  217. return;
  218. }
  219. irq = bcma_core_mips_irq(cc->core);
  220. /* Determine the registers of the UARTs */
  221. cc->nr_serial_ports = (cc->capabilities & BCMA_CC_CAP_NRUART);
  222. for (i = 0; i < cc->nr_serial_ports; i++) {
  223. ports[i].regs = cc->core->io_addr + BCMA_CC_UART0_DATA +
  224. (i * 256);
  225. ports[i].irq = irq;
  226. ports[i].baud_base = baud_base;
  227. ports[i].reg_shift = 0;
  228. }
  229. }
  230. #endif /* CONFIG_BCMA_DRIVER_MIPS */