prom.c 40 KB

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