driver_chipcommon.c 10 KB

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