driver_extif.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Sonics Silicon Backplane
  3. * Broadcom EXTIF core driver
  4. *
  5. * Copyright 2005, Broadcom Corporation
  6. * Copyright 2006, 2007, Michael Buesch <m@bues.ch>
  7. * Copyright 2006, 2007, Felix Fietkau <nbd@openwrt.org>
  8. * Copyright 2007, Aurelien Jarno <aurelien@aurel32.net>
  9. *
  10. * Licensed under the GNU/GPL. See COPYING for details.
  11. */
  12. #include <linux/serial.h>
  13. #include <linux/serial_core.h>
  14. #include <linux/serial_reg.h>
  15. #include "ssb_private.h"
  16. static inline u32 extif_read32(struct ssb_extif *extif, u16 offset)
  17. {
  18. return ssb_read32(extif->dev, offset);
  19. }
  20. static inline void extif_write32(struct ssb_extif *extif, u16 offset, u32 value)
  21. {
  22. ssb_write32(extif->dev, offset, value);
  23. }
  24. static inline u32 extif_write32_masked(struct ssb_extif *extif, u16 offset,
  25. u32 mask, u32 value)
  26. {
  27. value &= mask;
  28. value |= extif_read32(extif, offset) & ~mask;
  29. extif_write32(extif, offset, value);
  30. return value;
  31. }
  32. #ifdef CONFIG_SSB_SERIAL
  33. static bool serial_exists(u8 *regs)
  34. {
  35. u8 save_mcr, msr = 0;
  36. if (regs) {
  37. save_mcr = regs[UART_MCR];
  38. regs[UART_MCR] = (UART_MCR_LOOP | UART_MCR_OUT2 | UART_MCR_RTS);
  39. msr = regs[UART_MSR] & (UART_MSR_DCD | UART_MSR_RI
  40. | UART_MSR_CTS | UART_MSR_DSR);
  41. regs[UART_MCR] = save_mcr;
  42. }
  43. return (msr == (UART_MSR_DCD | UART_MSR_CTS));
  44. }
  45. int ssb_extif_serial_init(struct ssb_extif *extif, struct ssb_serial_port *ports)
  46. {
  47. u32 i, nr_ports = 0;
  48. /* Disable GPIO interrupt initially */
  49. extif_write32(extif, SSB_EXTIF_GPIO_INTPOL, 0);
  50. extif_write32(extif, SSB_EXTIF_GPIO_INTMASK, 0);
  51. for (i = 0; i < 2; i++) {
  52. void __iomem *uart_regs;
  53. uart_regs = ioremap_nocache(SSB_EUART, 16);
  54. if (uart_regs) {
  55. uart_regs += (i * 8);
  56. if (serial_exists(uart_regs) && ports) {
  57. extif_write32(extif, SSB_EXTIF_GPIO_INTMASK, 2);
  58. nr_ports++;
  59. ports[i].regs = uart_regs;
  60. ports[i].irq = 2;
  61. ports[i].baud_base = 13500000;
  62. ports[i].reg_shift = 0;
  63. }
  64. iounmap(uart_regs);
  65. }
  66. }
  67. return nr_ports;
  68. }
  69. #endif /* CONFIG_SSB_SERIAL */
  70. void ssb_extif_timing_init(struct ssb_extif *extif, unsigned long ns)
  71. {
  72. u32 tmp;
  73. /* Initialize extif so we can get to the LEDs and external UART */
  74. extif_write32(extif, SSB_EXTIF_PROG_CFG, SSB_EXTCFG_EN);
  75. /* Set timing for the flash */
  76. tmp = DIV_ROUND_UP(10, ns) << SSB_PROG_WCNT_3_SHIFT;
  77. tmp |= DIV_ROUND_UP(40, ns) << SSB_PROG_WCNT_1_SHIFT;
  78. tmp |= DIV_ROUND_UP(120, ns);
  79. extif_write32(extif, SSB_EXTIF_PROG_WAITCNT, tmp);
  80. /* Set programmable interface timing for external uart */
  81. tmp = DIV_ROUND_UP(10, ns) << SSB_PROG_WCNT_3_SHIFT;
  82. tmp |= DIV_ROUND_UP(20, ns) << SSB_PROG_WCNT_2_SHIFT;
  83. tmp |= DIV_ROUND_UP(100, ns) << SSB_PROG_WCNT_1_SHIFT;
  84. tmp |= DIV_ROUND_UP(120, ns);
  85. extif_write32(extif, SSB_EXTIF_PROG_WAITCNT, tmp);
  86. }
  87. void ssb_extif_get_clockcontrol(struct ssb_extif *extif,
  88. u32 *pll_type, u32 *n, u32 *m)
  89. {
  90. *pll_type = SSB_PLLTYPE_1;
  91. *n = extif_read32(extif, SSB_EXTIF_CLOCK_N);
  92. *m = extif_read32(extif, SSB_EXTIF_CLOCK_SB);
  93. }
  94. u32 ssb_extif_watchdog_timer_set_wdt(struct bcm47xx_wdt *wdt, u32 ticks)
  95. {
  96. struct ssb_extif *extif = bcm47xx_wdt_get_drvdata(wdt);
  97. return ssb_extif_watchdog_timer_set(extif, ticks);
  98. }
  99. u32 ssb_extif_watchdog_timer_set_ms(struct bcm47xx_wdt *wdt, u32 ms)
  100. {
  101. struct ssb_extif *extif = bcm47xx_wdt_get_drvdata(wdt);
  102. u32 ticks = (SSB_EXTIF_WATCHDOG_CLK / 1000) * ms;
  103. ticks = ssb_extif_watchdog_timer_set(extif, ticks);
  104. return (ticks * 1000) / SSB_EXTIF_WATCHDOG_CLK;
  105. }
  106. u32 ssb_extif_watchdog_timer_set(struct ssb_extif *extif, u32 ticks)
  107. {
  108. if (ticks > SSB_EXTIF_WATCHDOG_MAX_TIMER)
  109. ticks = SSB_EXTIF_WATCHDOG_MAX_TIMER;
  110. extif_write32(extif, SSB_EXTIF_WATCHDOG, ticks);
  111. return ticks;
  112. }
  113. void ssb_extif_init(struct ssb_extif *extif)
  114. {
  115. if (!extif->dev)
  116. return; /* We don't have a Extif core */
  117. spin_lock_init(&extif->gpio_lock);
  118. }
  119. u32 ssb_extif_gpio_in(struct ssb_extif *extif, u32 mask)
  120. {
  121. return extif_read32(extif, SSB_EXTIF_GPIO_IN) & mask;
  122. }
  123. u32 ssb_extif_gpio_out(struct ssb_extif *extif, u32 mask, u32 value)
  124. {
  125. unsigned long flags;
  126. u32 res = 0;
  127. spin_lock_irqsave(&extif->gpio_lock, flags);
  128. res = extif_write32_masked(extif, SSB_EXTIF_GPIO_OUT(0),
  129. mask, value);
  130. spin_unlock_irqrestore(&extif->gpio_lock, flags);
  131. return res;
  132. }
  133. u32 ssb_extif_gpio_outen(struct ssb_extif *extif, u32 mask, u32 value)
  134. {
  135. unsigned long flags;
  136. u32 res = 0;
  137. spin_lock_irqsave(&extif->gpio_lock, flags);
  138. res = extif_write32_masked(extif, SSB_EXTIF_GPIO_OUTEN(0),
  139. mask, value);
  140. spin_unlock_irqrestore(&extif->gpio_lock, flags);
  141. return res;
  142. }
  143. u32 ssb_extif_gpio_polarity(struct ssb_extif *extif, u32 mask, u32 value)
  144. {
  145. unsigned long flags;
  146. u32 res = 0;
  147. spin_lock_irqsave(&extif->gpio_lock, flags);
  148. res = extif_write32_masked(extif, SSB_EXTIF_GPIO_INTPOL, mask, value);
  149. spin_unlock_irqrestore(&extif->gpio_lock, flags);
  150. return res;
  151. }
  152. u32 ssb_extif_gpio_intmask(struct ssb_extif *extif, u32 mask, u32 value)
  153. {
  154. unsigned long flags;
  155. u32 res = 0;
  156. spin_lock_irqsave(&extif->gpio_lock, flags);
  157. res = extif_write32_masked(extif, SSB_EXTIF_GPIO_INTMASK, mask, value);
  158. spin_unlock_irqrestore(&extif->gpio_lock, flags);
  159. return res;
  160. }