driver_chipcommon.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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_get_alp_clock(struct bcma_drv_cc *cc)
  25. {
  26. if (cc->capabilities & BCMA_CC_CAP_PMU)
  27. return bcma_pmu_get_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_get_alp_clock(cc) / 4000;
  70. else
  71. /* based on 32KHz ILP clock */
  72. return 32;
  73. } else {
  74. return bcma_chipco_get_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. spin_lock_init(&cc->gpio_lock);
  98. if (cc->core->id.rev >= 11)
  99. cc->status = bcma_cc_read32(cc, BCMA_CC_CHIPSTAT);
  100. cc->capabilities = bcma_cc_read32(cc, BCMA_CC_CAP);
  101. if (cc->core->id.rev >= 35)
  102. cc->capabilities_ext = bcma_cc_read32(cc, BCMA_CC_CAP_EXT);
  103. if (cc->capabilities & BCMA_CC_CAP_PMU)
  104. bcma_pmu_early_init(cc);
  105. cc->early_setup_done = true;
  106. }
  107. void bcma_core_chipcommon_init(struct bcma_drv_cc *cc)
  108. {
  109. u32 leddc_on = 10;
  110. u32 leddc_off = 90;
  111. if (cc->setup_done)
  112. return;
  113. bcma_core_chipcommon_early_init(cc);
  114. if (cc->core->id.rev >= 20) {
  115. bcma_cc_write32(cc, BCMA_CC_GPIOPULLUP, 0);
  116. bcma_cc_write32(cc, BCMA_CC_GPIOPULLDOWN, 0);
  117. }
  118. if (cc->capabilities & BCMA_CC_CAP_PMU)
  119. bcma_pmu_init(cc);
  120. if (cc->capabilities & BCMA_CC_CAP_PCTL)
  121. bcma_err(cc->core->bus, "Power control not implemented!\n");
  122. if (cc->core->id.rev >= 16) {
  123. if (cc->core->bus->sprom.leddc_on_time &&
  124. cc->core->bus->sprom.leddc_off_time) {
  125. leddc_on = cc->core->bus->sprom.leddc_on_time;
  126. leddc_off = cc->core->bus->sprom.leddc_off_time;
  127. }
  128. bcma_cc_write32(cc, BCMA_CC_GPIOTIMER,
  129. ((leddc_on << BCMA_CC_GPIOTIMER_ONTIME_SHIFT) |
  130. (leddc_off << BCMA_CC_GPIOTIMER_OFFTIME_SHIFT)));
  131. }
  132. cc->ticks_per_ms = bcma_chipco_watchdog_ticks_per_ms(cc);
  133. cc->setup_done = true;
  134. }
  135. /* Set chip watchdog reset timer to fire in 'ticks' backplane cycles */
  136. u32 bcma_chipco_watchdog_timer_set(struct bcma_drv_cc *cc, u32 ticks)
  137. {
  138. u32 maxt;
  139. enum bcma_clkmode clkmode;
  140. maxt = bcma_chipco_watchdog_get_max_timer(cc);
  141. if (cc->capabilities & BCMA_CC_CAP_PMU) {
  142. if (ticks == 1)
  143. ticks = 2;
  144. else if (ticks > maxt)
  145. ticks = maxt;
  146. bcma_cc_write32(cc, BCMA_CC_PMU_WATCHDOG, ticks);
  147. } else {
  148. clkmode = ticks ? BCMA_CLKMODE_FAST : BCMA_CLKMODE_DYNAMIC;
  149. bcma_core_set_clockmode(cc->core, clkmode);
  150. if (ticks > maxt)
  151. ticks = maxt;
  152. /* instant NMI */
  153. bcma_cc_write32(cc, BCMA_CC_WATCHDOG, ticks);
  154. }
  155. return ticks;
  156. }
  157. void bcma_chipco_irq_mask(struct bcma_drv_cc *cc, u32 mask, u32 value)
  158. {
  159. bcma_cc_write32_masked(cc, BCMA_CC_IRQMASK, mask, value);
  160. }
  161. u32 bcma_chipco_irq_status(struct bcma_drv_cc *cc, u32 mask)
  162. {
  163. return bcma_cc_read32(cc, BCMA_CC_IRQSTAT) & mask;
  164. }
  165. u32 bcma_chipco_gpio_in(struct bcma_drv_cc *cc, u32 mask)
  166. {
  167. return bcma_cc_read32(cc, BCMA_CC_GPIOIN) & mask;
  168. }
  169. u32 bcma_chipco_gpio_out(struct bcma_drv_cc *cc, u32 mask, u32 value)
  170. {
  171. unsigned long flags;
  172. u32 res;
  173. spin_lock_irqsave(&cc->gpio_lock, flags);
  174. res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOOUT, mask, value);
  175. spin_unlock_irqrestore(&cc->gpio_lock, flags);
  176. return res;
  177. }
  178. u32 bcma_chipco_gpio_outen(struct bcma_drv_cc *cc, u32 mask, u32 value)
  179. {
  180. unsigned long flags;
  181. u32 res;
  182. spin_lock_irqsave(&cc->gpio_lock, flags);
  183. res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOOUTEN, mask, value);
  184. spin_unlock_irqrestore(&cc->gpio_lock, flags);
  185. return res;
  186. }
  187. /*
  188. * If the bit is set to 0, chipcommon controlls this GPIO,
  189. * if the bit is set to 1, it is used by some part of the chip and not our code.
  190. */
  191. u32 bcma_chipco_gpio_control(struct bcma_drv_cc *cc, u32 mask, u32 value)
  192. {
  193. unsigned long flags;
  194. u32 res;
  195. spin_lock_irqsave(&cc->gpio_lock, flags);
  196. res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOCTL, mask, value);
  197. spin_unlock_irqrestore(&cc->gpio_lock, flags);
  198. return res;
  199. }
  200. EXPORT_SYMBOL_GPL(bcma_chipco_gpio_control);
  201. u32 bcma_chipco_gpio_intmask(struct bcma_drv_cc *cc, u32 mask, u32 value)
  202. {
  203. unsigned long flags;
  204. u32 res;
  205. spin_lock_irqsave(&cc->gpio_lock, flags);
  206. res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOIRQ, mask, value);
  207. spin_unlock_irqrestore(&cc->gpio_lock, flags);
  208. return res;
  209. }
  210. u32 bcma_chipco_gpio_polarity(struct bcma_drv_cc *cc, u32 mask, u32 value)
  211. {
  212. unsigned long flags;
  213. u32 res;
  214. spin_lock_irqsave(&cc->gpio_lock, flags);
  215. res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOPOL, mask, value);
  216. spin_unlock_irqrestore(&cc->gpio_lock, flags);
  217. return res;
  218. }
  219. u32 bcma_chipco_gpio_pullup(struct bcma_drv_cc *cc, u32 mask, u32 value)
  220. {
  221. unsigned long flags;
  222. u32 res;
  223. if (cc->core->id.rev < 20)
  224. return 0;
  225. spin_lock_irqsave(&cc->gpio_lock, flags);
  226. res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOPULLUP, mask, value);
  227. spin_unlock_irqrestore(&cc->gpio_lock, flags);
  228. return res;
  229. }
  230. u32 bcma_chipco_gpio_pulldown(struct bcma_drv_cc *cc, u32 mask, u32 value)
  231. {
  232. unsigned long flags;
  233. u32 res;
  234. if (cc->core->id.rev < 20)
  235. return 0;
  236. spin_lock_irqsave(&cc->gpio_lock, flags);
  237. res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOPULLDOWN, mask, value);
  238. spin_unlock_irqrestore(&cc->gpio_lock, flags);
  239. return res;
  240. }
  241. #ifdef CONFIG_BCMA_DRIVER_MIPS
  242. void bcma_chipco_serial_init(struct bcma_drv_cc *cc)
  243. {
  244. unsigned int irq;
  245. u32 baud_base;
  246. u32 i;
  247. unsigned int ccrev = cc->core->id.rev;
  248. struct bcma_serial_port *ports = cc->serial_ports;
  249. if (ccrev >= 11 && ccrev != 15) {
  250. baud_base = bcma_chipco_get_alp_clock(cc);
  251. if (ccrev >= 21) {
  252. /* Turn off UART clock before switching clocksource. */
  253. bcma_cc_write32(cc, BCMA_CC_CORECTL,
  254. bcma_cc_read32(cc, BCMA_CC_CORECTL)
  255. & ~BCMA_CC_CORECTL_UARTCLKEN);
  256. }
  257. /* Set the override bit so we don't divide it */
  258. bcma_cc_write32(cc, BCMA_CC_CORECTL,
  259. bcma_cc_read32(cc, BCMA_CC_CORECTL)
  260. | BCMA_CC_CORECTL_UARTCLK0);
  261. if (ccrev >= 21) {
  262. /* Re-enable the UART clock. */
  263. bcma_cc_write32(cc, BCMA_CC_CORECTL,
  264. bcma_cc_read32(cc, BCMA_CC_CORECTL)
  265. | BCMA_CC_CORECTL_UARTCLKEN);
  266. }
  267. } else {
  268. bcma_err(cc->core->bus, "serial not supported on this device ccrev: 0x%x\n", ccrev);
  269. return;
  270. }
  271. irq = bcma_core_irq(cc->core);
  272. /* Determine the registers of the UARTs */
  273. cc->nr_serial_ports = (cc->capabilities & BCMA_CC_CAP_NRUART);
  274. for (i = 0; i < cc->nr_serial_ports; i++) {
  275. ports[i].regs = cc->core->io_addr + BCMA_CC_UART0_DATA +
  276. (i * 256);
  277. ports[i].irq = irq;
  278. ports[i].baud_base = baud_base;
  279. ports[i].reg_shift = 0;
  280. }
  281. }
  282. #endif /* CONFIG_BCMA_DRIVER_MIPS */