driver_chipcommon.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. 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. EXPORT_SYMBOL_GPL(bcma_chipco_get_alp_clock);
  31. static u32 bcma_chipco_watchdog_get_max_timer(struct bcma_drv_cc *cc)
  32. {
  33. struct bcma_bus *bus = cc->core->bus;
  34. u32 nb;
  35. if (cc->capabilities & BCMA_CC_CAP_PMU) {
  36. if (bus->chipinfo.id == BCMA_CHIP_ID_BCM4706)
  37. nb = 32;
  38. else if (cc->core->id.rev < 26)
  39. nb = 16;
  40. else
  41. nb = (cc->core->id.rev >= 37) ? 32 : 24;
  42. } else {
  43. nb = 28;
  44. }
  45. if (nb == 32)
  46. return 0xffffffff;
  47. else
  48. return (1 << nb) - 1;
  49. }
  50. static u32 bcma_chipco_watchdog_timer_set_wdt(struct bcm47xx_wdt *wdt,
  51. u32 ticks)
  52. {
  53. struct bcma_drv_cc *cc = bcm47xx_wdt_get_drvdata(wdt);
  54. return bcma_chipco_watchdog_timer_set(cc, ticks);
  55. }
  56. static u32 bcma_chipco_watchdog_timer_set_ms_wdt(struct bcm47xx_wdt *wdt,
  57. u32 ms)
  58. {
  59. struct bcma_drv_cc *cc = bcm47xx_wdt_get_drvdata(wdt);
  60. u32 ticks;
  61. ticks = bcma_chipco_watchdog_timer_set(cc, cc->ticks_per_ms * ms);
  62. return ticks / cc->ticks_per_ms;
  63. }
  64. static int bcma_chipco_watchdog_ticks_per_ms(struct bcma_drv_cc *cc)
  65. {
  66. struct bcma_bus *bus = cc->core->bus;
  67. if (cc->capabilities & BCMA_CC_CAP_PMU) {
  68. if (bus->chipinfo.id == BCMA_CHIP_ID_BCM4706)
  69. /* 4706 CC and PMU watchdogs are clocked at 1/4 of ALP clock */
  70. return bcma_chipco_get_alp_clock(cc) / 4000;
  71. else
  72. /* based on 32KHz ILP clock */
  73. return 32;
  74. } else {
  75. return bcma_chipco_get_alp_clock(cc) / 1000;
  76. }
  77. }
  78. int bcma_chipco_watchdog_register(struct bcma_drv_cc *cc)
  79. {
  80. struct bcm47xx_wdt wdt = {};
  81. struct platform_device *pdev;
  82. wdt.driver_data = cc;
  83. wdt.timer_set = bcma_chipco_watchdog_timer_set_wdt;
  84. wdt.timer_set_ms = bcma_chipco_watchdog_timer_set_ms_wdt;
  85. wdt.max_timer_ms = bcma_chipco_watchdog_get_max_timer(cc) / cc->ticks_per_ms;
  86. pdev = platform_device_register_data(NULL, "bcm47xx-wdt",
  87. cc->core->bus->num, &wdt,
  88. sizeof(wdt));
  89. if (IS_ERR(pdev))
  90. return PTR_ERR(pdev);
  91. cc->watchdog = pdev;
  92. return 0;
  93. }
  94. void bcma_core_chipcommon_early_init(struct bcma_drv_cc *cc)
  95. {
  96. if (cc->early_setup_done)
  97. return;
  98. spin_lock_init(&cc->gpio_lock);
  99. if (cc->core->id.rev >= 11)
  100. cc->status = bcma_cc_read32(cc, BCMA_CC_CHIPSTAT);
  101. cc->capabilities = bcma_cc_read32(cc, BCMA_CC_CAP);
  102. if (cc->core->id.rev >= 35)
  103. cc->capabilities_ext = bcma_cc_read32(cc, BCMA_CC_CAP_EXT);
  104. if (cc->capabilities & BCMA_CC_CAP_PMU)
  105. bcma_pmu_early_init(cc);
  106. cc->early_setup_done = true;
  107. }
  108. void bcma_core_chipcommon_init(struct bcma_drv_cc *cc)
  109. {
  110. u32 leddc_on = 10;
  111. u32 leddc_off = 90;
  112. if (cc->setup_done)
  113. return;
  114. bcma_core_chipcommon_early_init(cc);
  115. if (cc->core->id.rev >= 20) {
  116. bcma_cc_write32(cc, BCMA_CC_GPIOPULLUP, 0);
  117. bcma_cc_write32(cc, BCMA_CC_GPIOPULLDOWN, 0);
  118. }
  119. if (cc->capabilities & BCMA_CC_CAP_PMU)
  120. bcma_pmu_init(cc);
  121. if (cc->capabilities & BCMA_CC_CAP_PCTL)
  122. bcma_err(cc->core->bus, "Power control not implemented!\n");
  123. if (cc->core->id.rev >= 16) {
  124. if (cc->core->bus->sprom.leddc_on_time &&
  125. cc->core->bus->sprom.leddc_off_time) {
  126. leddc_on = cc->core->bus->sprom.leddc_on_time;
  127. leddc_off = cc->core->bus->sprom.leddc_off_time;
  128. }
  129. bcma_cc_write32(cc, BCMA_CC_GPIOTIMER,
  130. ((leddc_on << BCMA_CC_GPIOTIMER_ONTIME_SHIFT) |
  131. (leddc_off << BCMA_CC_GPIOTIMER_OFFTIME_SHIFT)));
  132. }
  133. cc->ticks_per_ms = bcma_chipco_watchdog_ticks_per_ms(cc);
  134. cc->setup_done = true;
  135. }
  136. /* Set chip watchdog reset timer to fire in 'ticks' backplane cycles */
  137. u32 bcma_chipco_watchdog_timer_set(struct bcma_drv_cc *cc, u32 ticks)
  138. {
  139. u32 maxt;
  140. enum bcma_clkmode clkmode;
  141. maxt = bcma_chipco_watchdog_get_max_timer(cc);
  142. if (cc->capabilities & BCMA_CC_CAP_PMU) {
  143. if (ticks == 1)
  144. ticks = 2;
  145. else if (ticks > maxt)
  146. ticks = maxt;
  147. bcma_cc_write32(cc, BCMA_CC_PMU_WATCHDOG, ticks);
  148. } else {
  149. clkmode = ticks ? BCMA_CLKMODE_FAST : BCMA_CLKMODE_DYNAMIC;
  150. bcma_core_set_clockmode(cc->core, clkmode);
  151. if (ticks > maxt)
  152. ticks = maxt;
  153. /* instant NMI */
  154. bcma_cc_write32(cc, BCMA_CC_WATCHDOG, ticks);
  155. }
  156. return ticks;
  157. }
  158. void bcma_chipco_irq_mask(struct bcma_drv_cc *cc, u32 mask, u32 value)
  159. {
  160. bcma_cc_write32_masked(cc, BCMA_CC_IRQMASK, mask, value);
  161. }
  162. u32 bcma_chipco_irq_status(struct bcma_drv_cc *cc, u32 mask)
  163. {
  164. return bcma_cc_read32(cc, BCMA_CC_IRQSTAT) & mask;
  165. }
  166. u32 bcma_chipco_gpio_in(struct bcma_drv_cc *cc, u32 mask)
  167. {
  168. return bcma_cc_read32(cc, BCMA_CC_GPIOIN) & mask;
  169. }
  170. u32 bcma_chipco_gpio_out(struct bcma_drv_cc *cc, u32 mask, u32 value)
  171. {
  172. unsigned long flags;
  173. u32 res;
  174. spin_lock_irqsave(&cc->gpio_lock, flags);
  175. res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOOUT, mask, value);
  176. spin_unlock_irqrestore(&cc->gpio_lock, flags);
  177. return res;
  178. }
  179. EXPORT_SYMBOL_GPL(bcma_chipco_gpio_out);
  180. u32 bcma_chipco_gpio_outen(struct bcma_drv_cc *cc, u32 mask, u32 value)
  181. {
  182. unsigned long flags;
  183. u32 res;
  184. spin_lock_irqsave(&cc->gpio_lock, flags);
  185. res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOOUTEN, mask, value);
  186. spin_unlock_irqrestore(&cc->gpio_lock, flags);
  187. return res;
  188. }
  189. EXPORT_SYMBOL_GPL(bcma_chipco_gpio_outen);
  190. /*
  191. * If the bit is set to 0, chipcommon controlls this GPIO,
  192. * if the bit is set to 1, it is used by some part of the chip and not our code.
  193. */
  194. u32 bcma_chipco_gpio_control(struct bcma_drv_cc *cc, u32 mask, u32 value)
  195. {
  196. unsigned long flags;
  197. u32 res;
  198. spin_lock_irqsave(&cc->gpio_lock, flags);
  199. res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOCTL, mask, value);
  200. spin_unlock_irqrestore(&cc->gpio_lock, flags);
  201. return res;
  202. }
  203. EXPORT_SYMBOL_GPL(bcma_chipco_gpio_control);
  204. u32 bcma_chipco_gpio_intmask(struct bcma_drv_cc *cc, u32 mask, u32 value)
  205. {
  206. unsigned long flags;
  207. u32 res;
  208. spin_lock_irqsave(&cc->gpio_lock, flags);
  209. res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOIRQ, mask, value);
  210. spin_unlock_irqrestore(&cc->gpio_lock, flags);
  211. return res;
  212. }
  213. u32 bcma_chipco_gpio_polarity(struct bcma_drv_cc *cc, u32 mask, u32 value)
  214. {
  215. unsigned long flags;
  216. u32 res;
  217. spin_lock_irqsave(&cc->gpio_lock, flags);
  218. res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOPOL, mask, value);
  219. spin_unlock_irqrestore(&cc->gpio_lock, flags);
  220. return res;
  221. }
  222. u32 bcma_chipco_gpio_pullup(struct bcma_drv_cc *cc, u32 mask, u32 value)
  223. {
  224. unsigned long flags;
  225. u32 res;
  226. if (cc->core->id.rev < 20)
  227. return 0;
  228. spin_lock_irqsave(&cc->gpio_lock, flags);
  229. res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOPULLUP, mask, value);
  230. spin_unlock_irqrestore(&cc->gpio_lock, flags);
  231. return res;
  232. }
  233. u32 bcma_chipco_gpio_pulldown(struct bcma_drv_cc *cc, u32 mask, u32 value)
  234. {
  235. unsigned long flags;
  236. u32 res;
  237. if (cc->core->id.rev < 20)
  238. return 0;
  239. spin_lock_irqsave(&cc->gpio_lock, flags);
  240. res = bcma_cc_write32_masked(cc, BCMA_CC_GPIOPULLDOWN, mask, value);
  241. spin_unlock_irqrestore(&cc->gpio_lock, flags);
  242. return res;
  243. }
  244. #ifdef CONFIG_BCMA_DRIVER_MIPS
  245. void bcma_chipco_serial_init(struct bcma_drv_cc *cc)
  246. {
  247. unsigned int irq;
  248. u32 baud_base;
  249. u32 i;
  250. unsigned int ccrev = cc->core->id.rev;
  251. struct bcma_serial_port *ports = cc->serial_ports;
  252. if (ccrev >= 11 && ccrev != 15) {
  253. baud_base = bcma_chipco_get_alp_clock(cc);
  254. if (ccrev >= 21) {
  255. /* Turn off UART clock before switching clocksource. */
  256. bcma_cc_write32(cc, BCMA_CC_CORECTL,
  257. bcma_cc_read32(cc, BCMA_CC_CORECTL)
  258. & ~BCMA_CC_CORECTL_UARTCLKEN);
  259. }
  260. /* Set the override bit so we don't divide it */
  261. bcma_cc_write32(cc, BCMA_CC_CORECTL,
  262. bcma_cc_read32(cc, BCMA_CC_CORECTL)
  263. | BCMA_CC_CORECTL_UARTCLK0);
  264. if (ccrev >= 21) {
  265. /* Re-enable the UART clock. */
  266. bcma_cc_write32(cc, BCMA_CC_CORECTL,
  267. bcma_cc_read32(cc, BCMA_CC_CORECTL)
  268. | BCMA_CC_CORECTL_UARTCLKEN);
  269. }
  270. } else {
  271. bcma_err(cc->core->bus, "serial not supported on this device ccrev: 0x%x\n", ccrev);
  272. return;
  273. }
  274. irq = bcma_core_irq(cc->core);
  275. /* Determine the registers of the UARTs */
  276. cc->nr_serial_ports = (cc->capabilities & BCMA_CC_CAP_NRUART);
  277. for (i = 0; i < cc->nr_serial_ports; i++) {
  278. ports[i].regs = cc->core->io_addr + BCMA_CC_UART0_DATA +
  279. (i * 256);
  280. ports[i].irq = irq;
  281. ports[i].baud_base = baud_base;
  282. ports[i].reg_shift = 0;
  283. }
  284. }
  285. #endif /* CONFIG_BCMA_DRIVER_MIPS */