prom.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684
  1. /*
  2. * Procedures for creating, accessing and interpreting the device tree.
  3. *
  4. * Paul Mackerras August 1996.
  5. * Copyright (C) 1996-2005 Paul Mackerras.
  6. *
  7. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  8. * {engebret|bergner}@us.ibm.com
  9. *
  10. * Adapted for sparc64 by David S. Miller davem@davemloft.net
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/types.h>
  19. #include <linux/string.h>
  20. #include <linux/mm.h>
  21. #include <linux/module.h>
  22. #include <linux/lmb.h>
  23. #include <linux/of_device.h>
  24. #include <asm/prom.h>
  25. #include <asm/oplib.h>
  26. #include <asm/irq.h>
  27. #include <asm/asi.h>
  28. #include <asm/upa.h>
  29. #include <asm/smp.h>
  30. extern struct device_node *allnodes; /* temporary while merging */
  31. extern rwlock_t devtree_lock; /* temporary while merging */
  32. struct device_node *of_find_node_by_phandle(phandle handle)
  33. {
  34. struct device_node *np;
  35. for (np = allnodes; np; np = np->allnext)
  36. if (np->node == handle)
  37. break;
  38. return np;
  39. }
  40. EXPORT_SYMBOL(of_find_node_by_phandle);
  41. int of_getintprop_default(struct device_node *np, const char *name, int def)
  42. {
  43. struct property *prop;
  44. int len;
  45. prop = of_find_property(np, name, &len);
  46. if (!prop || len != 4)
  47. return def;
  48. return *(int *) prop->value;
  49. }
  50. EXPORT_SYMBOL(of_getintprop_default);
  51. DEFINE_MUTEX(of_set_property_mutex);
  52. EXPORT_SYMBOL(of_set_property_mutex);
  53. int of_set_property(struct device_node *dp, const char *name, void *val, int len)
  54. {
  55. struct property **prevp;
  56. void *new_val;
  57. int err;
  58. new_val = kmalloc(len, GFP_KERNEL);
  59. if (!new_val)
  60. return -ENOMEM;
  61. memcpy(new_val, val, len);
  62. err = -ENODEV;
  63. write_lock(&devtree_lock);
  64. prevp = &dp->properties;
  65. while (*prevp) {
  66. struct property *prop = *prevp;
  67. if (!strcasecmp(prop->name, name)) {
  68. void *old_val = prop->value;
  69. int ret;
  70. mutex_lock(&of_set_property_mutex);
  71. ret = prom_setprop(dp->node, name, val, len);
  72. mutex_unlock(&of_set_property_mutex);
  73. err = -EINVAL;
  74. if (ret >= 0) {
  75. prop->value = new_val;
  76. prop->length = len;
  77. if (OF_IS_DYNAMIC(prop))
  78. kfree(old_val);
  79. OF_MARK_DYNAMIC(prop);
  80. err = 0;
  81. }
  82. break;
  83. }
  84. prevp = &(*prevp)->next;
  85. }
  86. write_unlock(&devtree_lock);
  87. /* XXX Upate procfs if necessary... */
  88. return err;
  89. }
  90. EXPORT_SYMBOL(of_set_property);
  91. int of_find_in_proplist(const char *list, const char *match, int len)
  92. {
  93. while (len > 0) {
  94. int l;
  95. if (!strcmp(list, match))
  96. return 1;
  97. l = strlen(list) + 1;
  98. list += l;
  99. len -= l;
  100. }
  101. return 0;
  102. }
  103. EXPORT_SYMBOL(of_find_in_proplist);
  104. static unsigned int prom_early_allocated __initdata;
  105. static void * __init prom_early_alloc(unsigned long size)
  106. {
  107. unsigned long paddr = lmb_alloc(size, SMP_CACHE_BYTES);
  108. void *ret;
  109. if (!paddr) {
  110. prom_printf("prom_early_alloc(%lu) failed\n");
  111. prom_halt();
  112. }
  113. ret = __va(paddr);
  114. memset(ret, 0, size);
  115. prom_early_allocated += size;
  116. return ret;
  117. }
  118. #ifdef CONFIG_PCI
  119. /* PSYCHO interrupt mapping support. */
  120. #define PSYCHO_IMAP_A_SLOT0 0x0c00UL
  121. #define PSYCHO_IMAP_B_SLOT0 0x0c20UL
  122. static unsigned long psycho_pcislot_imap_offset(unsigned long ino)
  123. {
  124. unsigned int bus = (ino & 0x10) >> 4;
  125. unsigned int slot = (ino & 0x0c) >> 2;
  126. if (bus == 0)
  127. return PSYCHO_IMAP_A_SLOT0 + (slot * 8);
  128. else
  129. return PSYCHO_IMAP_B_SLOT0 + (slot * 8);
  130. }
  131. #define PSYCHO_OBIO_IMAP_BASE 0x1000UL
  132. #define PSYCHO_ONBOARD_IRQ_BASE 0x20
  133. #define psycho_onboard_imap_offset(__ino) \
  134. (PSYCHO_OBIO_IMAP_BASE + (((__ino) & 0x1f) << 3))
  135. #define PSYCHO_ICLR_A_SLOT0 0x1400UL
  136. #define PSYCHO_ICLR_SCSI 0x1800UL
  137. #define psycho_iclr_offset(ino) \
  138. ((ino & 0x20) ? (PSYCHO_ICLR_SCSI + (((ino) & 0x1f) << 3)) : \
  139. (PSYCHO_ICLR_A_SLOT0 + (((ino) & 0x1f)<<3)))
  140. static unsigned int psycho_irq_build(struct device_node *dp,
  141. unsigned int ino,
  142. void *_data)
  143. {
  144. unsigned long controller_regs = (unsigned long) _data;
  145. unsigned long imap, iclr;
  146. unsigned long imap_off, iclr_off;
  147. int inofixup = 0;
  148. ino &= 0x3f;
  149. if (ino < PSYCHO_ONBOARD_IRQ_BASE) {
  150. /* PCI slot */
  151. imap_off = psycho_pcislot_imap_offset(ino);
  152. } else {
  153. /* Onboard device */
  154. imap_off = psycho_onboard_imap_offset(ino);
  155. }
  156. /* Now build the IRQ bucket. */
  157. imap = controller_regs + imap_off;
  158. iclr_off = psycho_iclr_offset(ino);
  159. iclr = controller_regs + iclr_off;
  160. if ((ino & 0x20) == 0)
  161. inofixup = ino & 0x03;
  162. return build_irq(inofixup, iclr, imap);
  163. }
  164. static void __init psycho_irq_trans_init(struct device_node *dp)
  165. {
  166. const struct linux_prom64_registers *regs;
  167. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  168. dp->irq_trans->irq_build = psycho_irq_build;
  169. regs = of_get_property(dp, "reg", NULL);
  170. dp->irq_trans->data = (void *) regs[2].phys_addr;
  171. }
  172. #define sabre_read(__reg) \
  173. ({ u64 __ret; \
  174. __asm__ __volatile__("ldxa [%1] %2, %0" \
  175. : "=r" (__ret) \
  176. : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
  177. : "memory"); \
  178. __ret; \
  179. })
  180. struct sabre_irq_data {
  181. unsigned long controller_regs;
  182. unsigned int pci_first_busno;
  183. };
  184. #define SABRE_CONFIGSPACE 0x001000000UL
  185. #define SABRE_WRSYNC 0x1c20UL
  186. #define SABRE_CONFIG_BASE(CONFIG_SPACE) \
  187. (CONFIG_SPACE | (1UL << 24))
  188. #define SABRE_CONFIG_ENCODE(BUS, DEVFN, REG) \
  189. (((unsigned long)(BUS) << 16) | \
  190. ((unsigned long)(DEVFN) << 8) | \
  191. ((unsigned long)(REG)))
  192. /* When a device lives behind a bridge deeper in the PCI bus topology
  193. * than APB, a special sequence must run to make sure all pending DMA
  194. * transfers at the time of IRQ delivery are visible in the coherency
  195. * domain by the cpu. This sequence is to perform a read on the far
  196. * side of the non-APB bridge, then perform a read of Sabre's DMA
  197. * write-sync register.
  198. */
  199. static void sabre_wsync_handler(unsigned int ino, void *_arg1, void *_arg2)
  200. {
  201. unsigned int phys_hi = (unsigned int) (unsigned long) _arg1;
  202. struct sabre_irq_data *irq_data = _arg2;
  203. unsigned long controller_regs = irq_data->controller_regs;
  204. unsigned long sync_reg = controller_regs + SABRE_WRSYNC;
  205. unsigned long config_space = controller_regs + SABRE_CONFIGSPACE;
  206. unsigned int bus, devfn;
  207. u16 _unused;
  208. config_space = SABRE_CONFIG_BASE(config_space);
  209. bus = (phys_hi >> 16) & 0xff;
  210. devfn = (phys_hi >> 8) & 0xff;
  211. config_space |= SABRE_CONFIG_ENCODE(bus, devfn, 0x00);
  212. __asm__ __volatile__("membar #Sync\n\t"
  213. "lduha [%1] %2, %0\n\t"
  214. "membar #Sync"
  215. : "=r" (_unused)
  216. : "r" ((u16 *) config_space),
  217. "i" (ASI_PHYS_BYPASS_EC_E_L)
  218. : "memory");
  219. sabre_read(sync_reg);
  220. }
  221. #define SABRE_IMAP_A_SLOT0 0x0c00UL
  222. #define SABRE_IMAP_B_SLOT0 0x0c20UL
  223. #define SABRE_ICLR_A_SLOT0 0x1400UL
  224. #define SABRE_ICLR_B_SLOT0 0x1480UL
  225. #define SABRE_ICLR_SCSI 0x1800UL
  226. #define SABRE_ICLR_ETH 0x1808UL
  227. #define SABRE_ICLR_BPP 0x1810UL
  228. #define SABRE_ICLR_AU_REC 0x1818UL
  229. #define SABRE_ICLR_AU_PLAY 0x1820UL
  230. #define SABRE_ICLR_PFAIL 0x1828UL
  231. #define SABRE_ICLR_KMS 0x1830UL
  232. #define SABRE_ICLR_FLPY 0x1838UL
  233. #define SABRE_ICLR_SHW 0x1840UL
  234. #define SABRE_ICLR_KBD 0x1848UL
  235. #define SABRE_ICLR_MS 0x1850UL
  236. #define SABRE_ICLR_SER 0x1858UL
  237. #define SABRE_ICLR_UE 0x1870UL
  238. #define SABRE_ICLR_CE 0x1878UL
  239. #define SABRE_ICLR_PCIERR 0x1880UL
  240. static unsigned long sabre_pcislot_imap_offset(unsigned long ino)
  241. {
  242. unsigned int bus = (ino & 0x10) >> 4;
  243. unsigned int slot = (ino & 0x0c) >> 2;
  244. if (bus == 0)
  245. return SABRE_IMAP_A_SLOT0 + (slot * 8);
  246. else
  247. return SABRE_IMAP_B_SLOT0 + (slot * 8);
  248. }
  249. #define SABRE_OBIO_IMAP_BASE 0x1000UL
  250. #define SABRE_ONBOARD_IRQ_BASE 0x20
  251. #define sabre_onboard_imap_offset(__ino) \
  252. (SABRE_OBIO_IMAP_BASE + (((__ino) & 0x1f) << 3))
  253. #define sabre_iclr_offset(ino) \
  254. ((ino & 0x20) ? (SABRE_ICLR_SCSI + (((ino) & 0x1f) << 3)) : \
  255. (SABRE_ICLR_A_SLOT0 + (((ino) & 0x1f)<<3)))
  256. static int sabre_device_needs_wsync(struct device_node *dp)
  257. {
  258. struct device_node *parent = dp->parent;
  259. const char *parent_model, *parent_compat;
  260. /* This traversal up towards the root is meant to
  261. * handle two cases:
  262. *
  263. * 1) non-PCI bus sitting under PCI, such as 'ebus'
  264. * 2) the PCI controller interrupts themselves, which
  265. * will use the sabre_irq_build but do not need
  266. * the DMA synchronization handling
  267. */
  268. while (parent) {
  269. if (!strcmp(parent->type, "pci"))
  270. break;
  271. parent = parent->parent;
  272. }
  273. if (!parent)
  274. return 0;
  275. parent_model = of_get_property(parent,
  276. "model", NULL);
  277. if (parent_model &&
  278. (!strcmp(parent_model, "SUNW,sabre") ||
  279. !strcmp(parent_model, "SUNW,simba")))
  280. return 0;
  281. parent_compat = of_get_property(parent,
  282. "compatible", NULL);
  283. if (parent_compat &&
  284. (!strcmp(parent_compat, "pci108e,a000") ||
  285. !strcmp(parent_compat, "pci108e,a001")))
  286. return 0;
  287. return 1;
  288. }
  289. static unsigned int sabre_irq_build(struct device_node *dp,
  290. unsigned int ino,
  291. void *_data)
  292. {
  293. struct sabre_irq_data *irq_data = _data;
  294. unsigned long controller_regs = irq_data->controller_regs;
  295. const struct linux_prom_pci_registers *regs;
  296. unsigned long imap, iclr;
  297. unsigned long imap_off, iclr_off;
  298. int inofixup = 0;
  299. int virt_irq;
  300. ino &= 0x3f;
  301. if (ino < SABRE_ONBOARD_IRQ_BASE) {
  302. /* PCI slot */
  303. imap_off = sabre_pcislot_imap_offset(ino);
  304. } else {
  305. /* onboard device */
  306. imap_off = sabre_onboard_imap_offset(ino);
  307. }
  308. /* Now build the IRQ bucket. */
  309. imap = controller_regs + imap_off;
  310. iclr_off = sabre_iclr_offset(ino);
  311. iclr = controller_regs + iclr_off;
  312. if ((ino & 0x20) == 0)
  313. inofixup = ino & 0x03;
  314. virt_irq = build_irq(inofixup, iclr, imap);
  315. /* If the parent device is a PCI<->PCI bridge other than
  316. * APB, we have to install a pre-handler to ensure that
  317. * all pending DMA is drained before the interrupt handler
  318. * is run.
  319. */
  320. regs = of_get_property(dp, "reg", NULL);
  321. if (regs && sabre_device_needs_wsync(dp)) {
  322. irq_install_pre_handler(virt_irq,
  323. sabre_wsync_handler,
  324. (void *) (long) regs->phys_hi,
  325. (void *) irq_data);
  326. }
  327. return virt_irq;
  328. }
  329. static void __init sabre_irq_trans_init(struct device_node *dp)
  330. {
  331. const struct linux_prom64_registers *regs;
  332. struct sabre_irq_data *irq_data;
  333. const u32 *busrange;
  334. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  335. dp->irq_trans->irq_build = sabre_irq_build;
  336. irq_data = prom_early_alloc(sizeof(struct sabre_irq_data));
  337. regs = of_get_property(dp, "reg", NULL);
  338. irq_data->controller_regs = regs[0].phys_addr;
  339. busrange = of_get_property(dp, "bus-range", NULL);
  340. irq_data->pci_first_busno = busrange[0];
  341. dp->irq_trans->data = irq_data;
  342. }
  343. /* SCHIZO interrupt mapping support. Unlike Psycho, for this controller the
  344. * imap/iclr registers are per-PBM.
  345. */
  346. #define SCHIZO_IMAP_BASE 0x1000UL
  347. #define SCHIZO_ICLR_BASE 0x1400UL
  348. static unsigned long schizo_imap_offset(unsigned long ino)
  349. {
  350. return SCHIZO_IMAP_BASE + (ino * 8UL);
  351. }
  352. static unsigned long schizo_iclr_offset(unsigned long ino)
  353. {
  354. return SCHIZO_ICLR_BASE + (ino * 8UL);
  355. }
  356. static unsigned long schizo_ino_to_iclr(unsigned long pbm_regs,
  357. unsigned int ino)
  358. {
  359. return pbm_regs + schizo_iclr_offset(ino);
  360. }
  361. static unsigned long schizo_ino_to_imap(unsigned long pbm_regs,
  362. unsigned int ino)
  363. {
  364. return pbm_regs + schizo_imap_offset(ino);
  365. }
  366. #define schizo_read(__reg) \
  367. ({ u64 __ret; \
  368. __asm__ __volatile__("ldxa [%1] %2, %0" \
  369. : "=r" (__ret) \
  370. : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
  371. : "memory"); \
  372. __ret; \
  373. })
  374. #define schizo_write(__reg, __val) \
  375. __asm__ __volatile__("stxa %0, [%1] %2" \
  376. : /* no outputs */ \
  377. : "r" (__val), "r" (__reg), \
  378. "i" (ASI_PHYS_BYPASS_EC_E) \
  379. : "memory")
  380. static void tomatillo_wsync_handler(unsigned int ino, void *_arg1, void *_arg2)
  381. {
  382. unsigned long sync_reg = (unsigned long) _arg2;
  383. u64 mask = 1UL << (ino & IMAP_INO);
  384. u64 val;
  385. int limit;
  386. schizo_write(sync_reg, mask);
  387. limit = 100000;
  388. val = 0;
  389. while (--limit) {
  390. val = schizo_read(sync_reg);
  391. if (!(val & mask))
  392. break;
  393. }
  394. if (limit <= 0) {
  395. printk("tomatillo_wsync_handler: DMA won't sync [%lx:%lx]\n",
  396. val, mask);
  397. }
  398. if (_arg1) {
  399. static unsigned char cacheline[64]
  400. __attribute__ ((aligned (64)));
  401. __asm__ __volatile__("rd %%fprs, %0\n\t"
  402. "or %0, %4, %1\n\t"
  403. "wr %1, 0x0, %%fprs\n\t"
  404. "stda %%f0, [%5] %6\n\t"
  405. "wr %0, 0x0, %%fprs\n\t"
  406. "membar #Sync"
  407. : "=&r" (mask), "=&r" (val)
  408. : "0" (mask), "1" (val),
  409. "i" (FPRS_FEF), "r" (&cacheline[0]),
  410. "i" (ASI_BLK_COMMIT_P));
  411. }
  412. }
  413. struct schizo_irq_data {
  414. unsigned long pbm_regs;
  415. unsigned long sync_reg;
  416. u32 portid;
  417. int chip_version;
  418. };
  419. static unsigned int schizo_irq_build(struct device_node *dp,
  420. unsigned int ino,
  421. void *_data)
  422. {
  423. struct schizo_irq_data *irq_data = _data;
  424. unsigned long pbm_regs = irq_data->pbm_regs;
  425. unsigned long imap, iclr;
  426. int ign_fixup;
  427. int virt_irq;
  428. int is_tomatillo;
  429. ino &= 0x3f;
  430. /* Now build the IRQ bucket. */
  431. imap = schizo_ino_to_imap(pbm_regs, ino);
  432. iclr = schizo_ino_to_iclr(pbm_regs, ino);
  433. /* On Schizo, no inofixup occurs. This is because each
  434. * INO has it's own IMAP register. On Psycho and Sabre
  435. * there is only one IMAP register for each PCI slot even
  436. * though four different INOs can be generated by each
  437. * PCI slot.
  438. *
  439. * But, for JBUS variants (essentially, Tomatillo), we have
  440. * to fixup the lowest bit of the interrupt group number.
  441. */
  442. ign_fixup = 0;
  443. is_tomatillo = (irq_data->sync_reg != 0UL);
  444. if (is_tomatillo) {
  445. if (irq_data->portid & 1)
  446. ign_fixup = (1 << 6);
  447. }
  448. virt_irq = build_irq(ign_fixup, iclr, imap);
  449. if (is_tomatillo) {
  450. irq_install_pre_handler(virt_irq,
  451. tomatillo_wsync_handler,
  452. ((irq_data->chip_version <= 4) ?
  453. (void *) 1 : (void *) 0),
  454. (void *) irq_data->sync_reg);
  455. }
  456. return virt_irq;
  457. }
  458. static void __init __schizo_irq_trans_init(struct device_node *dp,
  459. int is_tomatillo)
  460. {
  461. const struct linux_prom64_registers *regs;
  462. struct schizo_irq_data *irq_data;
  463. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  464. dp->irq_trans->irq_build = schizo_irq_build;
  465. irq_data = prom_early_alloc(sizeof(struct schizo_irq_data));
  466. regs = of_get_property(dp, "reg", NULL);
  467. dp->irq_trans->data = irq_data;
  468. irq_data->pbm_regs = regs[0].phys_addr;
  469. if (is_tomatillo)
  470. irq_data->sync_reg = regs[3].phys_addr + 0x1a18UL;
  471. else
  472. irq_data->sync_reg = 0UL;
  473. irq_data->portid = of_getintprop_default(dp, "portid", 0);
  474. irq_data->chip_version = of_getintprop_default(dp, "version#", 0);
  475. }
  476. static void __init schizo_irq_trans_init(struct device_node *dp)
  477. {
  478. __schizo_irq_trans_init(dp, 0);
  479. }
  480. static void __init tomatillo_irq_trans_init(struct device_node *dp)
  481. {
  482. __schizo_irq_trans_init(dp, 1);
  483. }
  484. static unsigned int pci_sun4v_irq_build(struct device_node *dp,
  485. unsigned int devino,
  486. void *_data)
  487. {
  488. u32 devhandle = (u32) (unsigned long) _data;
  489. return sun4v_build_irq(devhandle, devino);
  490. }
  491. static void __init pci_sun4v_irq_trans_init(struct device_node *dp)
  492. {
  493. const struct linux_prom64_registers *regs;
  494. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  495. dp->irq_trans->irq_build = pci_sun4v_irq_build;
  496. regs = of_get_property(dp, "reg", NULL);
  497. dp->irq_trans->data = (void *) (unsigned long)
  498. ((regs->phys_addr >> 32UL) & 0x0fffffff);
  499. }
  500. struct fire_irq_data {
  501. unsigned long pbm_regs;
  502. u32 portid;
  503. };
  504. #define FIRE_IMAP_BASE 0x001000
  505. #define FIRE_ICLR_BASE 0x001400
  506. static unsigned long fire_imap_offset(unsigned long ino)
  507. {
  508. return FIRE_IMAP_BASE + (ino * 8UL);
  509. }
  510. static unsigned long fire_iclr_offset(unsigned long ino)
  511. {
  512. return FIRE_ICLR_BASE + (ino * 8UL);
  513. }
  514. static unsigned long fire_ino_to_iclr(unsigned long pbm_regs,
  515. unsigned int ino)
  516. {
  517. return pbm_regs + fire_iclr_offset(ino);
  518. }
  519. static unsigned long fire_ino_to_imap(unsigned long pbm_regs,
  520. unsigned int ino)
  521. {
  522. return pbm_regs + fire_imap_offset(ino);
  523. }
  524. static unsigned int fire_irq_build(struct device_node *dp,
  525. unsigned int ino,
  526. void *_data)
  527. {
  528. struct fire_irq_data *irq_data = _data;
  529. unsigned long pbm_regs = irq_data->pbm_regs;
  530. unsigned long imap, iclr;
  531. unsigned long int_ctrlr;
  532. ino &= 0x3f;
  533. /* Now build the IRQ bucket. */
  534. imap = fire_ino_to_imap(pbm_regs, ino);
  535. iclr = fire_ino_to_iclr(pbm_regs, ino);
  536. /* Set the interrupt controller number. */
  537. int_ctrlr = 1 << 6;
  538. upa_writeq(int_ctrlr, imap);
  539. /* The interrupt map registers do not have an INO field
  540. * like other chips do. They return zero in the INO
  541. * field, and the interrupt controller number is controlled
  542. * in bits 6 to 9. So in order for build_irq() to get
  543. * the INO right we pass it in as part of the fixup
  544. * which will get added to the map register zero value
  545. * read by build_irq().
  546. */
  547. ino |= (irq_data->portid << 6);
  548. ino -= int_ctrlr;
  549. return build_irq(ino, iclr, imap);
  550. }
  551. static void __init fire_irq_trans_init(struct device_node *dp)
  552. {
  553. const struct linux_prom64_registers *regs;
  554. struct fire_irq_data *irq_data;
  555. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  556. dp->irq_trans->irq_build = fire_irq_build;
  557. irq_data = prom_early_alloc(sizeof(struct fire_irq_data));
  558. regs = of_get_property(dp, "reg", NULL);
  559. dp->irq_trans->data = irq_data;
  560. irq_data->pbm_regs = regs[0].phys_addr;
  561. irq_data->portid = of_getintprop_default(dp, "portid", 0);
  562. }
  563. #endif /* CONFIG_PCI */
  564. #ifdef CONFIG_SBUS
  565. /* INO number to IMAP register offset for SYSIO external IRQ's.
  566. * This should conform to both Sunfire/Wildfire server and Fusion
  567. * desktop designs.
  568. */
  569. #define SYSIO_IMAP_SLOT0 0x2c00UL
  570. #define SYSIO_IMAP_SLOT1 0x2c08UL
  571. #define SYSIO_IMAP_SLOT2 0x2c10UL
  572. #define SYSIO_IMAP_SLOT3 0x2c18UL
  573. #define SYSIO_IMAP_SCSI 0x3000UL
  574. #define SYSIO_IMAP_ETH 0x3008UL
  575. #define SYSIO_IMAP_BPP 0x3010UL
  576. #define SYSIO_IMAP_AUDIO 0x3018UL
  577. #define SYSIO_IMAP_PFAIL 0x3020UL
  578. #define SYSIO_IMAP_KMS 0x3028UL
  579. #define SYSIO_IMAP_FLPY 0x3030UL
  580. #define SYSIO_IMAP_SHW 0x3038UL
  581. #define SYSIO_IMAP_KBD 0x3040UL
  582. #define SYSIO_IMAP_MS 0x3048UL
  583. #define SYSIO_IMAP_SER 0x3050UL
  584. #define SYSIO_IMAP_TIM0 0x3060UL
  585. #define SYSIO_IMAP_TIM1 0x3068UL
  586. #define SYSIO_IMAP_UE 0x3070UL
  587. #define SYSIO_IMAP_CE 0x3078UL
  588. #define SYSIO_IMAP_SBERR 0x3080UL
  589. #define SYSIO_IMAP_PMGMT 0x3088UL
  590. #define SYSIO_IMAP_GFX 0x3090UL
  591. #define SYSIO_IMAP_EUPA 0x3098UL
  592. #define bogon ((unsigned long) -1)
  593. static unsigned long sysio_irq_offsets[] = {
  594. /* SBUS Slot 0 --> 3, level 1 --> 7 */
  595. SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0,
  596. SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0,
  597. SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1,
  598. SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1,
  599. SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2,
  600. SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2,
  601. SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3,
  602. SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3,
  603. /* Onboard devices (not relevant/used on SunFire). */
  604. SYSIO_IMAP_SCSI,
  605. SYSIO_IMAP_ETH,
  606. SYSIO_IMAP_BPP,
  607. bogon,
  608. SYSIO_IMAP_AUDIO,
  609. SYSIO_IMAP_PFAIL,
  610. bogon,
  611. bogon,
  612. SYSIO_IMAP_KMS,
  613. SYSIO_IMAP_FLPY,
  614. SYSIO_IMAP_SHW,
  615. SYSIO_IMAP_KBD,
  616. SYSIO_IMAP_MS,
  617. SYSIO_IMAP_SER,
  618. bogon,
  619. bogon,
  620. SYSIO_IMAP_TIM0,
  621. SYSIO_IMAP_TIM1,
  622. bogon,
  623. bogon,
  624. SYSIO_IMAP_UE,
  625. SYSIO_IMAP_CE,
  626. SYSIO_IMAP_SBERR,
  627. SYSIO_IMAP_PMGMT,
  628. SYSIO_IMAP_GFX,
  629. SYSIO_IMAP_EUPA,
  630. };
  631. #undef bogon
  632. #define NUM_SYSIO_OFFSETS ARRAY_SIZE(sysio_irq_offsets)
  633. /* Convert Interrupt Mapping register pointer to associated
  634. * Interrupt Clear register pointer, SYSIO specific version.
  635. */
  636. #define SYSIO_ICLR_UNUSED0 0x3400UL
  637. #define SYSIO_ICLR_SLOT0 0x3408UL
  638. #define SYSIO_ICLR_SLOT1 0x3448UL
  639. #define SYSIO_ICLR_SLOT2 0x3488UL
  640. #define SYSIO_ICLR_SLOT3 0x34c8UL
  641. static unsigned long sysio_imap_to_iclr(unsigned long imap)
  642. {
  643. unsigned long diff = SYSIO_ICLR_UNUSED0 - SYSIO_IMAP_SLOT0;
  644. return imap + diff;
  645. }
  646. static unsigned int sbus_of_build_irq(struct device_node *dp,
  647. unsigned int ino,
  648. void *_data)
  649. {
  650. unsigned long reg_base = (unsigned long) _data;
  651. const struct linux_prom_registers *regs;
  652. unsigned long imap, iclr;
  653. int sbus_slot = 0;
  654. int sbus_level = 0;
  655. ino &= 0x3f;
  656. regs = of_get_property(dp, "reg", NULL);
  657. if (regs)
  658. sbus_slot = regs->which_io;
  659. if (ino < 0x20)
  660. ino += (sbus_slot * 8);
  661. imap = sysio_irq_offsets[ino];
  662. if (imap == ((unsigned long)-1)) {
  663. prom_printf("get_irq_translations: Bad SYSIO INO[%x]\n",
  664. ino);
  665. prom_halt();
  666. }
  667. imap += reg_base;
  668. /* SYSIO inconsistency. For external SLOTS, we have to select
  669. * the right ICLR register based upon the lower SBUS irq level
  670. * bits.
  671. */
  672. if (ino >= 0x20) {
  673. iclr = sysio_imap_to_iclr(imap);
  674. } else {
  675. sbus_level = ino & 0x7;
  676. switch(sbus_slot) {
  677. case 0:
  678. iclr = reg_base + SYSIO_ICLR_SLOT0;
  679. break;
  680. case 1:
  681. iclr = reg_base + SYSIO_ICLR_SLOT1;
  682. break;
  683. case 2:
  684. iclr = reg_base + SYSIO_ICLR_SLOT2;
  685. break;
  686. default:
  687. case 3:
  688. iclr = reg_base + SYSIO_ICLR_SLOT3;
  689. break;
  690. };
  691. iclr += ((unsigned long)sbus_level - 1UL) * 8UL;
  692. }
  693. return build_irq(sbus_level, iclr, imap);
  694. }
  695. static void __init sbus_irq_trans_init(struct device_node *dp)
  696. {
  697. const struct linux_prom64_registers *regs;
  698. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  699. dp->irq_trans->irq_build = sbus_of_build_irq;
  700. regs = of_get_property(dp, "reg", NULL);
  701. dp->irq_trans->data = (void *) (unsigned long) regs->phys_addr;
  702. }
  703. #endif /* CONFIG_SBUS */
  704. static unsigned int central_build_irq(struct device_node *dp,
  705. unsigned int ino,
  706. void *_data)
  707. {
  708. struct device_node *central_dp = _data;
  709. struct of_device *central_op = of_find_device_by_node(central_dp);
  710. struct resource *res;
  711. unsigned long imap, iclr;
  712. u32 tmp;
  713. if (!strcmp(dp->name, "eeprom")) {
  714. res = &central_op->resource[5];
  715. } else if (!strcmp(dp->name, "zs")) {
  716. res = &central_op->resource[4];
  717. } else if (!strcmp(dp->name, "clock-board")) {
  718. res = &central_op->resource[3];
  719. } else {
  720. return ino;
  721. }
  722. imap = res->start + 0x00UL;
  723. iclr = res->start + 0x10UL;
  724. /* Set the INO state to idle, and disable. */
  725. upa_writel(0, iclr);
  726. upa_readl(iclr);
  727. tmp = upa_readl(imap);
  728. tmp &= ~0x80000000;
  729. upa_writel(tmp, imap);
  730. return build_irq(0, iclr, imap);
  731. }
  732. static void __init central_irq_trans_init(struct device_node *dp)
  733. {
  734. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  735. dp->irq_trans->irq_build = central_build_irq;
  736. dp->irq_trans->data = dp;
  737. }
  738. struct irq_trans {
  739. const char *name;
  740. void (*init)(struct device_node *);
  741. };
  742. #ifdef CONFIG_PCI
  743. static struct irq_trans __initdata pci_irq_trans_table[] = {
  744. { "SUNW,sabre", sabre_irq_trans_init },
  745. { "pci108e,a000", sabre_irq_trans_init },
  746. { "pci108e,a001", sabre_irq_trans_init },
  747. { "SUNW,psycho", psycho_irq_trans_init },
  748. { "pci108e,8000", psycho_irq_trans_init },
  749. { "SUNW,schizo", schizo_irq_trans_init },
  750. { "pci108e,8001", schizo_irq_trans_init },
  751. { "SUNW,schizo+", schizo_irq_trans_init },
  752. { "pci108e,8002", schizo_irq_trans_init },
  753. { "SUNW,tomatillo", tomatillo_irq_trans_init },
  754. { "pci108e,a801", tomatillo_irq_trans_init },
  755. { "SUNW,sun4v-pci", pci_sun4v_irq_trans_init },
  756. { "pciex108e,80f0", fire_irq_trans_init },
  757. };
  758. #endif
  759. static unsigned int sun4v_vdev_irq_build(struct device_node *dp,
  760. unsigned int devino,
  761. void *_data)
  762. {
  763. u32 devhandle = (u32) (unsigned long) _data;
  764. return sun4v_build_irq(devhandle, devino);
  765. }
  766. static void __init sun4v_vdev_irq_trans_init(struct device_node *dp)
  767. {
  768. const struct linux_prom64_registers *regs;
  769. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  770. dp->irq_trans->irq_build = sun4v_vdev_irq_build;
  771. regs = of_get_property(dp, "reg", NULL);
  772. dp->irq_trans->data = (void *) (unsigned long)
  773. ((regs->phys_addr >> 32UL) & 0x0fffffff);
  774. }
  775. static void __init irq_trans_init(struct device_node *dp)
  776. {
  777. #ifdef CONFIG_PCI
  778. const char *model;
  779. int i;
  780. #endif
  781. #ifdef CONFIG_PCI
  782. model = of_get_property(dp, "model", NULL);
  783. if (!model)
  784. model = of_get_property(dp, "compatible", NULL);
  785. if (model) {
  786. for (i = 0; i < ARRAY_SIZE(pci_irq_trans_table); i++) {
  787. struct irq_trans *t = &pci_irq_trans_table[i];
  788. if (!strcmp(model, t->name)) {
  789. t->init(dp);
  790. return;
  791. }
  792. }
  793. }
  794. #endif
  795. #ifdef CONFIG_SBUS
  796. if (!strcmp(dp->name, "sbus") ||
  797. !strcmp(dp->name, "sbi")) {
  798. sbus_irq_trans_init(dp);
  799. return;
  800. }
  801. #endif
  802. if (!strcmp(dp->name, "fhc") &&
  803. !strcmp(dp->parent->name, "central")) {
  804. central_irq_trans_init(dp);
  805. return;
  806. }
  807. if (!strcmp(dp->name, "virtual-devices") ||
  808. !strcmp(dp->name, "niu")) {
  809. sun4v_vdev_irq_trans_init(dp);
  810. return;
  811. }
  812. }
  813. static int is_root_node(const struct device_node *dp)
  814. {
  815. if (!dp)
  816. return 0;
  817. return (dp->parent == NULL);
  818. }
  819. /* The following routines deal with the black magic of fully naming a
  820. * node.
  821. *
  822. * Certain well known named nodes are just the simple name string.
  823. *
  824. * Actual devices have an address specifier appended to the base name
  825. * string, like this "foo@addr". The "addr" can be in any number of
  826. * formats, and the platform plus the type of the node determine the
  827. * format and how it is constructed.
  828. *
  829. * For children of the ROOT node, the naming convention is fixed and
  830. * determined by whether this is a sun4u or sun4v system.
  831. *
  832. * For children of other nodes, it is bus type specific. So
  833. * we walk up the tree until we discover a "device_type" property
  834. * we recognize and we go from there.
  835. *
  836. * As an example, the boot device on my workstation has a full path:
  837. *
  838. * /pci@1e,600000/ide@d/disk@0,0:c
  839. */
  840. static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
  841. {
  842. struct linux_prom64_registers *regs;
  843. struct property *rprop;
  844. u32 high_bits, low_bits, type;
  845. rprop = of_find_property(dp, "reg", NULL);
  846. if (!rprop)
  847. return;
  848. regs = rprop->value;
  849. if (!is_root_node(dp->parent)) {
  850. sprintf(tmp_buf, "%s@%x,%x",
  851. dp->name,
  852. (unsigned int) (regs->phys_addr >> 32UL),
  853. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  854. return;
  855. }
  856. type = regs->phys_addr >> 60UL;
  857. high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
  858. low_bits = (regs->phys_addr & 0xffffffffUL);
  859. if (type == 0 || type == 8) {
  860. const char *prefix = (type == 0) ? "m" : "i";
  861. if (low_bits)
  862. sprintf(tmp_buf, "%s@%s%x,%x",
  863. dp->name, prefix,
  864. high_bits, low_bits);
  865. else
  866. sprintf(tmp_buf, "%s@%s%x",
  867. dp->name,
  868. prefix,
  869. high_bits);
  870. } else if (type == 12) {
  871. sprintf(tmp_buf, "%s@%x",
  872. dp->name, high_bits);
  873. }
  874. }
  875. static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
  876. {
  877. struct linux_prom64_registers *regs;
  878. struct property *prop;
  879. prop = of_find_property(dp, "reg", NULL);
  880. if (!prop)
  881. return;
  882. regs = prop->value;
  883. if (!is_root_node(dp->parent)) {
  884. sprintf(tmp_buf, "%s@%x,%x",
  885. dp->name,
  886. (unsigned int) (regs->phys_addr >> 32UL),
  887. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  888. return;
  889. }
  890. prop = of_find_property(dp, "upa-portid", NULL);
  891. if (!prop)
  892. prop = of_find_property(dp, "portid", NULL);
  893. if (prop) {
  894. unsigned long mask = 0xffffffffUL;
  895. if (tlb_type >= cheetah)
  896. mask = 0x7fffff;
  897. sprintf(tmp_buf, "%s@%x,%x",
  898. dp->name,
  899. *(u32 *)prop->value,
  900. (unsigned int) (regs->phys_addr & mask));
  901. }
  902. }
  903. /* "name@slot,offset" */
  904. static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
  905. {
  906. struct linux_prom_registers *regs;
  907. struct property *prop;
  908. prop = of_find_property(dp, "reg", NULL);
  909. if (!prop)
  910. return;
  911. regs = prop->value;
  912. sprintf(tmp_buf, "%s@%x,%x",
  913. dp->name,
  914. regs->which_io,
  915. regs->phys_addr);
  916. }
  917. /* "name@devnum[,func]" */
  918. static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
  919. {
  920. struct linux_prom_pci_registers *regs;
  921. struct property *prop;
  922. unsigned int devfn;
  923. prop = of_find_property(dp, "reg", NULL);
  924. if (!prop)
  925. return;
  926. regs = prop->value;
  927. devfn = (regs->phys_hi >> 8) & 0xff;
  928. if (devfn & 0x07) {
  929. sprintf(tmp_buf, "%s@%x,%x",
  930. dp->name,
  931. devfn >> 3,
  932. devfn & 0x07);
  933. } else {
  934. sprintf(tmp_buf, "%s@%x",
  935. dp->name,
  936. devfn >> 3);
  937. }
  938. }
  939. /* "name@UPA_PORTID,offset" */
  940. static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
  941. {
  942. struct linux_prom64_registers *regs;
  943. struct property *prop;
  944. prop = of_find_property(dp, "reg", NULL);
  945. if (!prop)
  946. return;
  947. regs = prop->value;
  948. prop = of_find_property(dp, "upa-portid", NULL);
  949. if (!prop)
  950. return;
  951. sprintf(tmp_buf, "%s@%x,%x",
  952. dp->name,
  953. *(u32 *) prop->value,
  954. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  955. }
  956. /* "name@reg" */
  957. static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
  958. {
  959. struct property *prop;
  960. u32 *regs;
  961. prop = of_find_property(dp, "reg", NULL);
  962. if (!prop)
  963. return;
  964. regs = prop->value;
  965. sprintf(tmp_buf, "%s@%x", dp->name, *regs);
  966. }
  967. /* "name@addrhi,addrlo" */
  968. static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
  969. {
  970. struct linux_prom64_registers *regs;
  971. struct property *prop;
  972. prop = of_find_property(dp, "reg", NULL);
  973. if (!prop)
  974. return;
  975. regs = prop->value;
  976. sprintf(tmp_buf, "%s@%x,%x",
  977. dp->name,
  978. (unsigned int) (regs->phys_addr >> 32UL),
  979. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  980. }
  981. /* "name@bus,addr" */
  982. static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
  983. {
  984. struct property *prop;
  985. u32 *regs;
  986. prop = of_find_property(dp, "reg", NULL);
  987. if (!prop)
  988. return;
  989. regs = prop->value;
  990. /* This actually isn't right... should look at the #address-cells
  991. * property of the i2c bus node etc. etc.
  992. */
  993. sprintf(tmp_buf, "%s@%x,%x",
  994. dp->name, regs[0], regs[1]);
  995. }
  996. /* "name@reg0[,reg1]" */
  997. static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
  998. {
  999. struct property *prop;
  1000. u32 *regs;
  1001. prop = of_find_property(dp, "reg", NULL);
  1002. if (!prop)
  1003. return;
  1004. regs = prop->value;
  1005. if (prop->length == sizeof(u32) || regs[1] == 1) {
  1006. sprintf(tmp_buf, "%s@%x",
  1007. dp->name, regs[0]);
  1008. } else {
  1009. sprintf(tmp_buf, "%s@%x,%x",
  1010. dp->name, regs[0], regs[1]);
  1011. }
  1012. }
  1013. /* "name@reg0reg1[,reg2reg3]" */
  1014. static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
  1015. {
  1016. struct property *prop;
  1017. u32 *regs;
  1018. prop = of_find_property(dp, "reg", NULL);
  1019. if (!prop)
  1020. return;
  1021. regs = prop->value;
  1022. if (regs[2] || regs[3]) {
  1023. sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
  1024. dp->name, regs[0], regs[1], regs[2], regs[3]);
  1025. } else {
  1026. sprintf(tmp_buf, "%s@%08x%08x",
  1027. dp->name, regs[0], regs[1]);
  1028. }
  1029. }
  1030. static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
  1031. {
  1032. struct device_node *parent = dp->parent;
  1033. if (parent != NULL) {
  1034. if (!strcmp(parent->type, "pci") ||
  1035. !strcmp(parent->type, "pciex")) {
  1036. pci_path_component(dp, tmp_buf);
  1037. return;
  1038. }
  1039. if (!strcmp(parent->type, "sbus")) {
  1040. sbus_path_component(dp, tmp_buf);
  1041. return;
  1042. }
  1043. if (!strcmp(parent->type, "upa")) {
  1044. upa_path_component(dp, tmp_buf);
  1045. return;
  1046. }
  1047. if (!strcmp(parent->type, "ebus")) {
  1048. ebus_path_component(dp, tmp_buf);
  1049. return;
  1050. }
  1051. if (!strcmp(parent->name, "usb") ||
  1052. !strcmp(parent->name, "hub")) {
  1053. usb_path_component(dp, tmp_buf);
  1054. return;
  1055. }
  1056. if (!strcmp(parent->type, "i2c")) {
  1057. i2c_path_component(dp, tmp_buf);
  1058. return;
  1059. }
  1060. if (!strcmp(parent->type, "firewire")) {
  1061. ieee1394_path_component(dp, tmp_buf);
  1062. return;
  1063. }
  1064. if (!strcmp(parent->type, "virtual-devices")) {
  1065. vdev_path_component(dp, tmp_buf);
  1066. return;
  1067. }
  1068. /* "isa" is handled with platform naming */
  1069. }
  1070. /* Use platform naming convention. */
  1071. if (tlb_type == hypervisor) {
  1072. sun4v_path_component(dp, tmp_buf);
  1073. return;
  1074. } else {
  1075. sun4u_path_component(dp, tmp_buf);
  1076. }
  1077. }
  1078. static char * __init build_path_component(struct device_node *dp)
  1079. {
  1080. char tmp_buf[64], *n;
  1081. tmp_buf[0] = '\0';
  1082. __build_path_component(dp, tmp_buf);
  1083. if (tmp_buf[0] == '\0')
  1084. strcpy(tmp_buf, dp->name);
  1085. n = prom_early_alloc(strlen(tmp_buf) + 1);
  1086. strcpy(n, tmp_buf);
  1087. return n;
  1088. }
  1089. static char * __init build_full_name(struct device_node *dp)
  1090. {
  1091. int len, ourlen, plen;
  1092. char *n;
  1093. plen = strlen(dp->parent->full_name);
  1094. ourlen = strlen(dp->path_component_name);
  1095. len = ourlen + plen + 2;
  1096. n = prom_early_alloc(len);
  1097. strcpy(n, dp->parent->full_name);
  1098. if (!is_root_node(dp->parent)) {
  1099. strcpy(n + plen, "/");
  1100. plen++;
  1101. }
  1102. strcpy(n + plen, dp->path_component_name);
  1103. return n;
  1104. }
  1105. static unsigned int unique_id;
  1106. static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
  1107. {
  1108. static struct property *tmp = NULL;
  1109. struct property *p;
  1110. if (tmp) {
  1111. p = tmp;
  1112. memset(p, 0, sizeof(*p) + 32);
  1113. tmp = NULL;
  1114. } else {
  1115. p = prom_early_alloc(sizeof(struct property) + 32);
  1116. p->unique_id = unique_id++;
  1117. }
  1118. p->name = (char *) (p + 1);
  1119. if (special_name) {
  1120. strcpy(p->name, special_name);
  1121. p->length = special_len;
  1122. p->value = prom_early_alloc(special_len);
  1123. memcpy(p->value, special_val, special_len);
  1124. } else {
  1125. if (prev == NULL) {
  1126. prom_firstprop(node, p->name);
  1127. } else {
  1128. prom_nextprop(node, prev, p->name);
  1129. }
  1130. if (strlen(p->name) == 0) {
  1131. tmp = p;
  1132. return NULL;
  1133. }
  1134. p->length = prom_getproplen(node, p->name);
  1135. if (p->length <= 0) {
  1136. p->length = 0;
  1137. } else {
  1138. p->value = prom_early_alloc(p->length + 1);
  1139. prom_getproperty(node, p->name, p->value, p->length);
  1140. ((unsigned char *)p->value)[p->length] = '\0';
  1141. }
  1142. }
  1143. return p;
  1144. }
  1145. static struct property * __init build_prop_list(phandle node)
  1146. {
  1147. struct property *head, *tail;
  1148. head = tail = build_one_prop(node, NULL,
  1149. ".node", &node, sizeof(node));
  1150. tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
  1151. tail = tail->next;
  1152. while(tail) {
  1153. tail->next = build_one_prop(node, tail->name,
  1154. NULL, NULL, 0);
  1155. tail = tail->next;
  1156. }
  1157. return head;
  1158. }
  1159. static char * __init get_one_property(phandle node, const char *name)
  1160. {
  1161. char *buf = "<NULL>";
  1162. int len;
  1163. len = prom_getproplen(node, name);
  1164. if (len > 0) {
  1165. buf = prom_early_alloc(len);
  1166. prom_getproperty(node, name, buf, len);
  1167. }
  1168. return buf;
  1169. }
  1170. static struct device_node * __init create_node(phandle node, struct device_node *parent)
  1171. {
  1172. struct device_node *dp;
  1173. if (!node)
  1174. return NULL;
  1175. dp = prom_early_alloc(sizeof(*dp));
  1176. dp->unique_id = unique_id++;
  1177. dp->parent = parent;
  1178. kref_init(&dp->kref);
  1179. dp->name = get_one_property(node, "name");
  1180. dp->type = get_one_property(node, "device_type");
  1181. dp->node = node;
  1182. dp->properties = build_prop_list(node);
  1183. irq_trans_init(dp);
  1184. return dp;
  1185. }
  1186. static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
  1187. {
  1188. struct device_node *ret = NULL, *prev_sibling = NULL;
  1189. struct device_node *dp;
  1190. while (1) {
  1191. dp = create_node(node, parent);
  1192. if (!dp)
  1193. break;
  1194. if (prev_sibling)
  1195. prev_sibling->sibling = dp;
  1196. if (!ret)
  1197. ret = dp;
  1198. prev_sibling = dp;
  1199. *(*nextp) = dp;
  1200. *nextp = &dp->allnext;
  1201. dp->path_component_name = build_path_component(dp);
  1202. dp->full_name = build_full_name(dp);
  1203. dp->child = build_tree(dp, prom_getchild(node), nextp);
  1204. node = prom_getsibling(node);
  1205. }
  1206. return ret;
  1207. }
  1208. static const char *get_mid_prop(void)
  1209. {
  1210. return (tlb_type == spitfire ? "upa-portid" : "portid");
  1211. }
  1212. struct device_node *of_find_node_by_cpuid(int cpuid)
  1213. {
  1214. struct device_node *dp;
  1215. const char *mid_prop = get_mid_prop();
  1216. for_each_node_by_type(dp, "cpu") {
  1217. int id = of_getintprop_default(dp, mid_prop, -1);
  1218. const char *this_mid_prop = mid_prop;
  1219. if (id < 0) {
  1220. this_mid_prop = "cpuid";
  1221. id = of_getintprop_default(dp, this_mid_prop, -1);
  1222. }
  1223. if (id < 0) {
  1224. prom_printf("OF: Serious problem, cpu lacks "
  1225. "%s property", this_mid_prop);
  1226. prom_halt();
  1227. }
  1228. if (cpuid == id)
  1229. return dp;
  1230. }
  1231. return NULL;
  1232. }
  1233. static void __init of_fill_in_cpu_data(void)
  1234. {
  1235. struct device_node *dp;
  1236. const char *mid_prop = get_mid_prop();
  1237. ncpus_probed = 0;
  1238. for_each_node_by_type(dp, "cpu") {
  1239. int cpuid = of_getintprop_default(dp, mid_prop, -1);
  1240. const char *this_mid_prop = mid_prop;
  1241. struct device_node *portid_parent;
  1242. int portid = -1;
  1243. portid_parent = NULL;
  1244. if (cpuid < 0) {
  1245. this_mid_prop = "cpuid";
  1246. cpuid = of_getintprop_default(dp, this_mid_prop, -1);
  1247. if (cpuid >= 0) {
  1248. int limit = 2;
  1249. portid_parent = dp;
  1250. while (limit--) {
  1251. portid_parent = portid_parent->parent;
  1252. if (!portid_parent)
  1253. break;
  1254. portid = of_getintprop_default(portid_parent,
  1255. "portid", -1);
  1256. if (portid >= 0)
  1257. break;
  1258. }
  1259. }
  1260. }
  1261. if (cpuid < 0) {
  1262. prom_printf("OF: Serious problem, cpu lacks "
  1263. "%s property", this_mid_prop);
  1264. prom_halt();
  1265. }
  1266. ncpus_probed++;
  1267. #ifdef CONFIG_SMP
  1268. if (cpuid >= NR_CPUS) {
  1269. printk(KERN_WARNING "Ignoring CPU %d which is "
  1270. ">= NR_CPUS (%d)\n",
  1271. cpuid, NR_CPUS);
  1272. continue;
  1273. }
  1274. #else
  1275. /* On uniprocessor we only want the values for the
  1276. * real physical cpu the kernel booted onto, however
  1277. * cpu_data() only has one entry at index 0.
  1278. */
  1279. if (cpuid != real_hard_smp_processor_id())
  1280. continue;
  1281. cpuid = 0;
  1282. #endif
  1283. cpu_data(cpuid).clock_tick =
  1284. of_getintprop_default(dp, "clock-frequency", 0);
  1285. if (portid_parent) {
  1286. cpu_data(cpuid).dcache_size =
  1287. of_getintprop_default(dp, "l1-dcache-size",
  1288. 16 * 1024);
  1289. cpu_data(cpuid).dcache_line_size =
  1290. of_getintprop_default(dp, "l1-dcache-line-size",
  1291. 32);
  1292. cpu_data(cpuid).icache_size =
  1293. of_getintprop_default(dp, "l1-icache-size",
  1294. 8 * 1024);
  1295. cpu_data(cpuid).icache_line_size =
  1296. of_getintprop_default(dp, "l1-icache-line-size",
  1297. 32);
  1298. cpu_data(cpuid).ecache_size =
  1299. of_getintprop_default(dp, "l2-cache-size", 0);
  1300. cpu_data(cpuid).ecache_line_size =
  1301. of_getintprop_default(dp, "l2-cache-line-size", 0);
  1302. if (!cpu_data(cpuid).ecache_size ||
  1303. !cpu_data(cpuid).ecache_line_size) {
  1304. cpu_data(cpuid).ecache_size =
  1305. of_getintprop_default(portid_parent,
  1306. "l2-cache-size",
  1307. (4 * 1024 * 1024));
  1308. cpu_data(cpuid).ecache_line_size =
  1309. of_getintprop_default(portid_parent,
  1310. "l2-cache-line-size", 64);
  1311. }
  1312. cpu_data(cpuid).core_id = portid + 1;
  1313. cpu_data(cpuid).proc_id = portid;
  1314. #ifdef CONFIG_SMP
  1315. sparc64_multi_core = 1;
  1316. #endif
  1317. } else {
  1318. cpu_data(cpuid).dcache_size =
  1319. of_getintprop_default(dp, "dcache-size", 16 * 1024);
  1320. cpu_data(cpuid).dcache_line_size =
  1321. of_getintprop_default(dp, "dcache-line-size", 32);
  1322. cpu_data(cpuid).icache_size =
  1323. of_getintprop_default(dp, "icache-size", 16 * 1024);
  1324. cpu_data(cpuid).icache_line_size =
  1325. of_getintprop_default(dp, "icache-line-size", 32);
  1326. cpu_data(cpuid).ecache_size =
  1327. of_getintprop_default(dp, "ecache-size",
  1328. (4 * 1024 * 1024));
  1329. cpu_data(cpuid).ecache_line_size =
  1330. of_getintprop_default(dp, "ecache-line-size", 64);
  1331. cpu_data(cpuid).core_id = 0;
  1332. cpu_data(cpuid).proc_id = -1;
  1333. }
  1334. #ifdef CONFIG_SMP
  1335. cpu_set(cpuid, cpu_present_map);
  1336. cpu_set(cpuid, cpu_possible_map);
  1337. #endif
  1338. }
  1339. smp_fill_in_sib_core_maps();
  1340. }
  1341. struct device_node *of_console_device;
  1342. EXPORT_SYMBOL(of_console_device);
  1343. char *of_console_path;
  1344. EXPORT_SYMBOL(of_console_path);
  1345. char *of_console_options;
  1346. EXPORT_SYMBOL(of_console_options);
  1347. static void __init of_console_init(void)
  1348. {
  1349. char *msg = "OF stdout device is: %s\n";
  1350. struct device_node *dp;
  1351. const char *type;
  1352. phandle node;
  1353. of_console_path = prom_early_alloc(256);
  1354. if (prom_ihandle2path(prom_stdout, of_console_path, 256) < 0) {
  1355. prom_printf("Cannot obtain path of stdout.\n");
  1356. prom_halt();
  1357. }
  1358. of_console_options = strrchr(of_console_path, ':');
  1359. if (of_console_options) {
  1360. of_console_options++;
  1361. if (*of_console_options == '\0')
  1362. of_console_options = NULL;
  1363. }
  1364. node = prom_inst2pkg(prom_stdout);
  1365. if (!node) {
  1366. prom_printf("Cannot resolve stdout node from "
  1367. "instance %08x.\n", prom_stdout);
  1368. prom_halt();
  1369. }
  1370. dp = of_find_node_by_phandle(node);
  1371. type = of_get_property(dp, "device_type", NULL);
  1372. if (!type) {
  1373. prom_printf("Console stdout lacks device_type property.\n");
  1374. prom_halt();
  1375. }
  1376. if (strcmp(type, "display") && strcmp(type, "serial")) {
  1377. prom_printf("Console device_type is neither display "
  1378. "nor serial.\n");
  1379. prom_halt();
  1380. }
  1381. of_console_device = dp;
  1382. printk(msg, of_console_path);
  1383. }
  1384. void __init prom_build_devicetree(void)
  1385. {
  1386. struct device_node **nextp;
  1387. allnodes = create_node(prom_root_node, NULL);
  1388. allnodes->path_component_name = "";
  1389. allnodes->full_name = "/";
  1390. nextp = &allnodes->allnext;
  1391. allnodes->child = build_tree(allnodes,
  1392. prom_getchild(allnodes->node),
  1393. &nextp);
  1394. of_console_init();
  1395. printk("PROM: Built device tree with %u bytes of memory.\n",
  1396. prom_early_allocated);
  1397. if (tlb_type != hypervisor)
  1398. of_fill_in_cpu_data();
  1399. }