driver_chipcommon.c 8.9 KB

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