driver_chipcommon.c 10 KB

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