driver_mipscore.c 8.4 KB

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