driver_chipcommon.c 8.8 KB

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