prom.c 38 KB

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