prom.c 37 KB

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