prom.c 38 KB

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