prom.c 38 KB

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