prom.c 36 KB

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