prom.c 38 KB

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