prom.c 40 KB

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