prom.c 36 KB

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