driver_mipscore.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Sonics Silicon Backplane
  3. * Broadcom MIPS core driver
  4. *
  5. * Copyright 2005, Broadcom Corporation
  6. * Copyright 2006, 2007, Michael Buesch <mb@bu3sch.de>
  7. *
  8. * Licensed under the GNU/GPL. See COPYING for details.
  9. */
  10. #include <linux/ssb/ssb.h>
  11. #include <linux/serial.h>
  12. #include <linux/serial_core.h>
  13. #include <linux/serial_reg.h>
  14. #include <linux/time.h>
  15. #include "ssb_private.h"
  16. static inline u32 mips_read32(struct ssb_mipscore *mcore,
  17. u16 offset)
  18. {
  19. return ssb_read32(mcore->dev, offset);
  20. }
  21. static inline void mips_write32(struct ssb_mipscore *mcore,
  22. u16 offset,
  23. u32 value)
  24. {
  25. ssb_write32(mcore->dev, offset, value);
  26. }
  27. static const u32 ipsflag_irq_mask[] = {
  28. 0,
  29. SSB_IPSFLAG_IRQ1,
  30. SSB_IPSFLAG_IRQ2,
  31. SSB_IPSFLAG_IRQ3,
  32. SSB_IPSFLAG_IRQ4,
  33. };
  34. static const u32 ipsflag_irq_shift[] = {
  35. 0,
  36. SSB_IPSFLAG_IRQ1_SHIFT,
  37. SSB_IPSFLAG_IRQ2_SHIFT,
  38. SSB_IPSFLAG_IRQ3_SHIFT,
  39. SSB_IPSFLAG_IRQ4_SHIFT,
  40. };
  41. static inline u32 ssb_irqflag(struct ssb_device *dev)
  42. {
  43. u32 tpsflag = ssb_read32(dev, SSB_TPSFLAG);
  44. if (tpsflag)
  45. return ssb_read32(dev, SSB_TPSFLAG) & SSB_TPSFLAG_BPFLAG;
  46. else
  47. /* not irq supported */
  48. return 0x3f;
  49. }
  50. static struct ssb_device *find_device(struct ssb_device *rdev, int irqflag)
  51. {
  52. struct ssb_bus *bus = rdev->bus;
  53. int i;
  54. for (i = 0; i < bus->nr_devices; i++) {
  55. struct ssb_device *dev;
  56. dev = &(bus->devices[i]);
  57. if (ssb_irqflag(dev) == irqflag)
  58. return dev;
  59. }
  60. return NULL;
  61. }
  62. /* Get the MIPS IRQ assignment for a specified device.
  63. * If unassigned, 0 is returned.
  64. * If disabled, 5 is returned.
  65. * If not supported, 6 is returned.
  66. */
  67. unsigned int ssb_mips_irq(struct ssb_device *dev)
  68. {
  69. struct ssb_bus *bus = dev->bus;
  70. struct ssb_device *mdev = bus->mipscore.dev;
  71. u32 irqflag;
  72. u32 ipsflag;
  73. u32 tmp;
  74. unsigned int irq;
  75. irqflag = ssb_irqflag(dev);
  76. if (irqflag == 0x3f)
  77. return 6;
  78. ipsflag = ssb_read32(bus->mipscore.dev, SSB_IPSFLAG);
  79. for (irq = 1; irq <= 4; irq++) {
  80. tmp = ((ipsflag & ipsflag_irq_mask[irq]) >> ipsflag_irq_shift[irq]);
  81. if (tmp == irqflag)
  82. break;
  83. }
  84. if (irq == 5) {
  85. if ((1 << irqflag) & ssb_read32(mdev, SSB_INTVEC))
  86. irq = 0;
  87. }
  88. return irq;
  89. }
  90. static void clear_irq(struct ssb_bus *bus, unsigned int irq)
  91. {
  92. struct ssb_device *dev = bus->mipscore.dev;
  93. /* Clear the IRQ in the MIPScore backplane registers */
  94. if (irq == 0) {
  95. ssb_write32(dev, SSB_INTVEC, 0);
  96. } else {
  97. ssb_write32(dev, SSB_IPSFLAG,
  98. ssb_read32(dev, SSB_IPSFLAG) |
  99. ipsflag_irq_mask[irq]);
  100. }
  101. }
  102. static void set_irq(struct ssb_device *dev, unsigned int irq)
  103. {
  104. unsigned int oldirq = ssb_mips_irq(dev);
  105. struct ssb_bus *bus = dev->bus;
  106. struct ssb_device *mdev = bus->mipscore.dev;
  107. u32 irqflag = ssb_irqflag(dev);
  108. BUG_ON(oldirq == 6);
  109. dev->irq = irq + 2;
  110. /* clear the old irq */
  111. if (oldirq == 0)
  112. ssb_write32(mdev, SSB_INTVEC, (~(1 << irqflag) & ssb_read32(mdev, SSB_INTVEC)));
  113. else if (oldirq != 5)
  114. clear_irq(bus, oldirq);
  115. /* assign the new one */
  116. if (irq == 0) {
  117. ssb_write32(mdev, SSB_INTVEC, ((1 << irqflag) | ssb_read32(mdev, SSB_INTVEC)));
  118. } else {
  119. u32 ipsflag = ssb_read32(mdev, SSB_IPSFLAG);
  120. if ((ipsflag & ipsflag_irq_mask[irq]) != ipsflag_irq_mask[irq]) {
  121. u32 oldipsflag = (ipsflag & ipsflag_irq_mask[irq]) >> ipsflag_irq_shift[irq];
  122. struct ssb_device *olddev = find_device(dev, oldipsflag);
  123. if (olddev)
  124. set_irq(olddev, 0);
  125. }
  126. irqflag <<= ipsflag_irq_shift[irq];
  127. irqflag |= (ipsflag & ~ipsflag_irq_mask[irq]);
  128. ssb_write32(mdev, SSB_IPSFLAG, irqflag);
  129. }
  130. ssb_dprintk(KERN_INFO PFX
  131. "set_irq: core 0x%04x, irq %d => %d\n",
  132. dev->id.coreid, oldirq+2, irq+2);
  133. }
  134. static void print_irq(struct ssb_device *dev, unsigned int irq)
  135. {
  136. int i;
  137. static const char *irq_name[] = {"2(S)", "3", "4", "5", "6", "D", "I"};
  138. ssb_dprintk(KERN_INFO PFX
  139. "core 0x%04x, irq :", dev->id.coreid);
  140. for (i = 0; i <= 6; i++) {
  141. ssb_dprintk(" %s%s", irq_name[i], i==irq?"*":" ");
  142. }
  143. ssb_dprintk("\n");
  144. }
  145. static void dump_irq(struct ssb_bus *bus)
  146. {
  147. int i;
  148. for (i = 0; i < bus->nr_devices; i++) {
  149. struct ssb_device *dev;
  150. dev = &(bus->devices[i]);
  151. print_irq(dev, ssb_mips_irq(dev));
  152. }
  153. }
  154. static void ssb_mips_serial_init(struct ssb_mipscore *mcore)
  155. {
  156. struct ssb_bus *bus = mcore->dev->bus;
  157. if (bus->extif.dev)
  158. mcore->nr_serial_ports = ssb_extif_serial_init(&bus->extif, mcore->serial_ports);
  159. else if (bus->chipco.dev)
  160. mcore->nr_serial_ports = ssb_chipco_serial_init(&bus->chipco, mcore->serial_ports);
  161. else
  162. mcore->nr_serial_ports = 0;
  163. }
  164. static void ssb_mips_flash_detect(struct ssb_mipscore *mcore)
  165. {
  166. struct ssb_bus *bus = mcore->dev->bus;
  167. mcore->flash_buswidth = 2;
  168. if (bus->chipco.dev) {
  169. mcore->flash_window = 0x1c000000;
  170. mcore->flash_window_size = 0x02000000;
  171. if ((ssb_read32(bus->chipco.dev, SSB_CHIPCO_FLASH_CFG)
  172. & SSB_CHIPCO_CFG_DS16) == 0)
  173. mcore->flash_buswidth = 1;
  174. } else {
  175. mcore->flash_window = 0x1fc00000;
  176. mcore->flash_window_size = 0x00400000;
  177. }
  178. }
  179. u32 ssb_cpu_clock(struct ssb_mipscore *mcore)
  180. {
  181. struct ssb_bus *bus = mcore->dev->bus;
  182. u32 pll_type, n, m, rate = 0;
  183. if (bus->extif.dev) {
  184. ssb_extif_get_clockcontrol(&bus->extif, &pll_type, &n, &m);
  185. } else if (bus->chipco.dev) {
  186. ssb_chipco_get_clockcpu(&bus->chipco, &pll_type, &n, &m);
  187. } else
  188. return 0;
  189. if ((pll_type == SSB_PLLTYPE_5) || (bus->chip_id == 0x5365)) {
  190. rate = 200000000;
  191. } else {
  192. rate = ssb_calc_clock_rate(pll_type, n, m);
  193. }
  194. if (pll_type == SSB_PLLTYPE_6) {
  195. rate *= 2;
  196. }
  197. return rate;
  198. }
  199. void ssb_mipscore_init(struct ssb_mipscore *mcore)
  200. {
  201. struct ssb_bus *bus;
  202. struct ssb_device *dev;
  203. unsigned long hz, ns;
  204. unsigned int irq, i;
  205. if (!mcore->dev)
  206. return; /* We don't have a MIPS core */
  207. ssb_dprintk(KERN_INFO PFX "Initializing MIPS core...\n");
  208. bus = mcore->dev->bus;
  209. hz = ssb_clockspeed(bus);
  210. if (!hz)
  211. hz = 100000000;
  212. ns = 1000000000 / hz;
  213. if (bus->extif.dev)
  214. ssb_extif_timing_init(&bus->extif, ns);
  215. else if (bus->chipco.dev)
  216. ssb_chipco_timing_init(&bus->chipco, ns);
  217. /* Assign IRQs to all cores on the bus, start with irq line 2, because serial usually takes 1 */
  218. for (irq = 2, i = 0; i < bus->nr_devices; i++) {
  219. int mips_irq;
  220. dev = &(bus->devices[i]);
  221. mips_irq = ssb_mips_irq(dev);
  222. if (mips_irq > 4)
  223. dev->irq = 0;
  224. else
  225. dev->irq = mips_irq + 2;
  226. if (dev->irq > 5)
  227. continue;
  228. switch (dev->id.coreid) {
  229. case SSB_DEV_USB11_HOST:
  230. /* shouldn't need a separate irq line for non-4710, most of them have a proper
  231. * external usb controller on the pci */
  232. if ((bus->chip_id == 0x4710) && (irq <= 4)) {
  233. set_irq(dev, irq++);
  234. }
  235. break;
  236. case SSB_DEV_PCI:
  237. case SSB_DEV_ETHERNET:
  238. case SSB_DEV_ETHERNET_GBIT:
  239. case SSB_DEV_80211:
  240. case SSB_DEV_USB20_HOST:
  241. /* These devices get their own IRQ line if available, the rest goes on IRQ0 */
  242. if (irq <= 4) {
  243. set_irq(dev, irq++);
  244. break;
  245. }
  246. /* fallthrough */
  247. case SSB_DEV_EXTIF:
  248. set_irq(dev, 0);
  249. break;
  250. }
  251. }
  252. ssb_dprintk(KERN_INFO PFX "after irq reconfiguration\n");
  253. dump_irq(bus);
  254. ssb_mips_serial_init(mcore);
  255. ssb_mips_flash_detect(mcore);
  256. }