driver_mipscore.c 8.7 KB

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