celleb_pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Support for PCI on Celleb platform.
  3. *
  4. * (C) Copyright 2006-2007 TOSHIBA CORPORATION
  5. *
  6. * This code is based on arch/powerpc/kernel/rtas_pci.c:
  7. * Copyright (C) 2001 Dave Engebretsen, IBM Corporation
  8. * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. */
  24. #undef DEBUG
  25. #include <linux/kernel.h>
  26. #include <linux/threads.h>
  27. #include <linux/pci.h>
  28. #include <linux/string.h>
  29. #include <linux/init.h>
  30. #include <linux/bootmem.h>
  31. #include <linux/pci_regs.h>
  32. #include <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <asm/io.h>
  35. #include <asm/irq.h>
  36. #include <asm/prom.h>
  37. #include <asm/pci-bridge.h>
  38. #include <asm/ppc-pci.h>
  39. #include "io-workarounds.h"
  40. #include "celleb_pci.h"
  41. #define MAX_PCI_DEVICES 32
  42. #define MAX_PCI_FUNCTIONS 8
  43. #define MAX_PCI_BASE_ADDRS 3 /* use 64 bit address */
  44. /* definition for fake pci configuration area for GbE, .... ,and etc. */
  45. struct celleb_pci_resource {
  46. struct resource r[MAX_PCI_BASE_ADDRS];
  47. };
  48. struct celleb_pci_private {
  49. unsigned char *fake_config[MAX_PCI_DEVICES][MAX_PCI_FUNCTIONS];
  50. struct celleb_pci_resource *res[MAX_PCI_DEVICES][MAX_PCI_FUNCTIONS];
  51. };
  52. static inline u8 celleb_fake_config_readb(void *addr)
  53. {
  54. u8 *p = addr;
  55. return *p;
  56. }
  57. static inline u16 celleb_fake_config_readw(void *addr)
  58. {
  59. __le16 *p = addr;
  60. return le16_to_cpu(*p);
  61. }
  62. static inline u32 celleb_fake_config_readl(void *addr)
  63. {
  64. __le32 *p = addr;
  65. return le32_to_cpu(*p);
  66. }
  67. static inline void celleb_fake_config_writeb(u32 val, void *addr)
  68. {
  69. u8 *p = addr;
  70. *p = val;
  71. }
  72. static inline void celleb_fake_config_writew(u32 val, void *addr)
  73. {
  74. __le16 val16;
  75. __le16 *p = addr;
  76. val16 = cpu_to_le16(val);
  77. *p = val16;
  78. }
  79. static inline void celleb_fake_config_writel(u32 val, void *addr)
  80. {
  81. __le32 val32;
  82. __le32 *p = addr;
  83. val32 = cpu_to_le32(val);
  84. *p = val32;
  85. }
  86. static unsigned char *get_fake_config_start(struct pci_controller *hose,
  87. int devno, int fn)
  88. {
  89. struct celleb_pci_private *private = hose->private_data;
  90. if (private == NULL)
  91. return NULL;
  92. return private->fake_config[devno][fn];
  93. }
  94. static struct celleb_pci_resource *get_resource_start(
  95. struct pci_controller *hose,
  96. int devno, int fn)
  97. {
  98. struct celleb_pci_private *private = hose->private_data;
  99. if (private == NULL)
  100. return NULL;
  101. return private->res[devno][fn];
  102. }
  103. static void celleb_config_read_fake(unsigned char *config, int where,
  104. int size, u32 *val)
  105. {
  106. char *p = config + where;
  107. switch (size) {
  108. case 1:
  109. *val = celleb_fake_config_readb(p);
  110. break;
  111. case 2:
  112. *val = celleb_fake_config_readw(p);
  113. break;
  114. case 4:
  115. *val = celleb_fake_config_readl(p);
  116. break;
  117. }
  118. }
  119. static void celleb_config_write_fake(unsigned char *config, int where,
  120. int size, u32 val)
  121. {
  122. char *p = config + where;
  123. switch (size) {
  124. case 1:
  125. celleb_fake_config_writeb(val, p);
  126. break;
  127. case 2:
  128. celleb_fake_config_writew(val, p);
  129. break;
  130. case 4:
  131. celleb_fake_config_writel(val, p);
  132. break;
  133. }
  134. }
  135. static int celleb_fake_pci_read_config(struct pci_bus *bus,
  136. unsigned int devfn, int where, int size, u32 *val)
  137. {
  138. char *config;
  139. struct device_node *node;
  140. struct pci_controller *hose;
  141. unsigned int devno = devfn >> 3;
  142. unsigned int fn = devfn & 0x7;
  143. /* allignment check */
  144. BUG_ON(where % size);
  145. pr_debug(" fake read: bus=0x%x, ", bus->number);
  146. node = (struct device_node *)bus->sysdata;
  147. hose = pci_find_hose_for_OF_device(node);
  148. config = get_fake_config_start(hose, devno, fn);
  149. pr_debug("devno=0x%x, where=0x%x, size=0x%x, ", devno, where, size);
  150. if (!config) {
  151. pr_debug("failed\n");
  152. return PCIBIOS_DEVICE_NOT_FOUND;
  153. }
  154. celleb_config_read_fake(config, where, size, val);
  155. pr_debug("val=0x%x\n", *val);
  156. return PCIBIOS_SUCCESSFUL;
  157. }
  158. static int celleb_fake_pci_write_config(struct pci_bus *bus,
  159. unsigned int devfn, int where, int size, u32 val)
  160. {
  161. char *config;
  162. struct device_node *node;
  163. struct pci_controller *hose;
  164. struct celleb_pci_resource *res;
  165. unsigned int devno = devfn >> 3;
  166. unsigned int fn = devfn & 0x7;
  167. /* allignment check */
  168. BUG_ON(where % size);
  169. node = (struct device_node *)bus->sysdata;
  170. hose = pci_find_hose_for_OF_device(node);
  171. config = get_fake_config_start(hose, devno, fn);
  172. if (!config)
  173. return PCIBIOS_DEVICE_NOT_FOUND;
  174. if (val == ~0) {
  175. int i = (where - PCI_BASE_ADDRESS_0) >> 3;
  176. switch (where) {
  177. case PCI_BASE_ADDRESS_0:
  178. case PCI_BASE_ADDRESS_2:
  179. if (size != 4)
  180. return PCIBIOS_DEVICE_NOT_FOUND;
  181. res = get_resource_start(hose, devno, fn);
  182. if (!res)
  183. return PCIBIOS_DEVICE_NOT_FOUND;
  184. celleb_config_write_fake(config, where, size,
  185. (res->r[i].end - res->r[i].start));
  186. return PCIBIOS_SUCCESSFUL;
  187. case PCI_BASE_ADDRESS_1:
  188. case PCI_BASE_ADDRESS_3:
  189. case PCI_BASE_ADDRESS_4:
  190. case PCI_BASE_ADDRESS_5:
  191. break;
  192. default:
  193. break;
  194. }
  195. }
  196. celleb_config_write_fake(config, where, size, val);
  197. pr_debug(" fake write: where=%x, size=%d, val=%x\n",
  198. where, size, val);
  199. return PCIBIOS_SUCCESSFUL;
  200. }
  201. static struct pci_ops celleb_fake_pci_ops = {
  202. .read = celleb_fake_pci_read_config,
  203. .write = celleb_fake_pci_write_config,
  204. };
  205. static inline void celleb_setup_pci_base_addrs(struct pci_controller *hose,
  206. unsigned int devno, unsigned int fn,
  207. unsigned int num_base_addr)
  208. {
  209. u32 val;
  210. unsigned char *config;
  211. struct celleb_pci_resource *res;
  212. config = get_fake_config_start(hose, devno, fn);
  213. res = get_resource_start(hose, devno, fn);
  214. if (!config || !res)
  215. return;
  216. switch (num_base_addr) {
  217. case 3:
  218. val = (res->r[2].start & 0xfffffff0)
  219. | PCI_BASE_ADDRESS_MEM_TYPE_64;
  220. celleb_config_write_fake(config, PCI_BASE_ADDRESS_4, 4, val);
  221. val = res->r[2].start >> 32;
  222. celleb_config_write_fake(config, PCI_BASE_ADDRESS_5, 4, val);
  223. /* FALLTHROUGH */
  224. case 2:
  225. val = (res->r[1].start & 0xfffffff0)
  226. | PCI_BASE_ADDRESS_MEM_TYPE_64;
  227. celleb_config_write_fake(config, PCI_BASE_ADDRESS_2, 4, val);
  228. val = res->r[1].start >> 32;
  229. celleb_config_write_fake(config, PCI_BASE_ADDRESS_3, 4, val);
  230. /* FALLTHROUGH */
  231. case 1:
  232. val = (res->r[0].start & 0xfffffff0)
  233. | PCI_BASE_ADDRESS_MEM_TYPE_64;
  234. celleb_config_write_fake(config, PCI_BASE_ADDRESS_0, 4, val);
  235. val = res->r[0].start >> 32;
  236. celleb_config_write_fake(config, PCI_BASE_ADDRESS_1, 4, val);
  237. break;
  238. }
  239. val = PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
  240. celleb_config_write_fake(config, PCI_COMMAND, 2, val);
  241. }
  242. static int __init celleb_setup_fake_pci_device(struct device_node *node,
  243. struct pci_controller *hose)
  244. {
  245. unsigned int rlen;
  246. int num_base_addr = 0;
  247. u32 val;
  248. const u32 *wi0, *wi1, *wi2, *wi3, *wi4;
  249. unsigned int devno, fn;
  250. struct celleb_pci_private *private = hose->private_data;
  251. unsigned char **config = NULL;
  252. struct celleb_pci_resource **res = NULL;
  253. const char *name;
  254. const unsigned long *li;
  255. int size, result;
  256. if (private == NULL) {
  257. printk(KERN_ERR "PCI: "
  258. "memory space for pci controller is not assigned\n");
  259. goto error;
  260. }
  261. name = of_get_property(node, "model", &rlen);
  262. if (!name) {
  263. printk(KERN_ERR "PCI: model property not found.\n");
  264. goto error;
  265. }
  266. wi4 = of_get_property(node, "reg", &rlen);
  267. if (wi4 == NULL)
  268. goto error;
  269. devno = ((wi4[0] >> 8) & 0xff) >> 3;
  270. fn = (wi4[0] >> 8) & 0x7;
  271. pr_debug("PCI: celleb_setup_fake_pci() %s devno=%x fn=%x\n", name,
  272. devno, fn);
  273. size = 256;
  274. config = &private->fake_config[devno][fn];
  275. *config = alloc_maybe_bootmem(size, GFP_KERNEL);
  276. if (*config == NULL) {
  277. printk(KERN_ERR "PCI: "
  278. "not enough memory for fake configuration space\n");
  279. goto error;
  280. }
  281. pr_debug("PCI: fake config area assigned 0x%016lx\n",
  282. (unsigned long)*config);
  283. size = sizeof(struct celleb_pci_resource);
  284. res = &private->res[devno][fn];
  285. *res = alloc_maybe_bootmem(size, GFP_KERNEL);
  286. if (*res == NULL) {
  287. printk(KERN_ERR
  288. "PCI: not enough memory for resource data space\n");
  289. goto error;
  290. }
  291. pr_debug("PCI: res assigned 0x%016lx\n", (unsigned long)*res);
  292. wi0 = of_get_property(node, "device-id", NULL);
  293. wi1 = of_get_property(node, "vendor-id", NULL);
  294. wi2 = of_get_property(node, "class-code", NULL);
  295. wi3 = of_get_property(node, "revision-id", NULL);
  296. if (!wi0 || !wi1 || !wi2 || !wi3) {
  297. printk(KERN_ERR "PCI: Missing device tree properties.\n");
  298. goto error;
  299. }
  300. celleb_config_write_fake(*config, PCI_DEVICE_ID, 2, wi0[0] & 0xffff);
  301. celleb_config_write_fake(*config, PCI_VENDOR_ID, 2, wi1[0] & 0xffff);
  302. pr_debug("class-code = 0x%08x\n", wi2[0]);
  303. celleb_config_write_fake(*config, PCI_CLASS_PROG, 1, wi2[0] & 0xff);
  304. celleb_config_write_fake(*config, PCI_CLASS_DEVICE, 2,
  305. (wi2[0] >> 8) & 0xffff);
  306. celleb_config_write_fake(*config, PCI_REVISION_ID, 1, wi3[0]);
  307. while (num_base_addr < MAX_PCI_BASE_ADDRS) {
  308. result = of_address_to_resource(node,
  309. num_base_addr, &(*res)->r[num_base_addr]);
  310. if (result)
  311. break;
  312. num_base_addr++;
  313. }
  314. celleb_setup_pci_base_addrs(hose, devno, fn, num_base_addr);
  315. li = of_get_property(node, "interrupts", &rlen);
  316. if (!li) {
  317. printk(KERN_ERR "PCI: interrupts not found.\n");
  318. goto error;
  319. }
  320. val = li[0];
  321. celleb_config_write_fake(*config, PCI_INTERRUPT_PIN, 1, 1);
  322. celleb_config_write_fake(*config, PCI_INTERRUPT_LINE, 1, val);
  323. #ifdef DEBUG
  324. pr_debug("PCI: %s irq=%ld\n", name, li[0]);
  325. for (i = 0; i < 6; i++) {
  326. celleb_config_read_fake(*config,
  327. PCI_BASE_ADDRESS_0 + 0x4 * i, 4,
  328. &val);
  329. pr_debug("PCI: %s fn=%d base_address_%d=0x%x\n",
  330. name, fn, i, val);
  331. }
  332. #endif
  333. celleb_config_write_fake(*config, PCI_HEADER_TYPE, 1,
  334. PCI_HEADER_TYPE_NORMAL);
  335. return 0;
  336. error:
  337. if (mem_init_done) {
  338. if (config && *config)
  339. kfree(*config);
  340. if (res && *res)
  341. kfree(*res);
  342. } else {
  343. if (config && *config) {
  344. size = 256;
  345. free_bootmem((unsigned long)(*config), size);
  346. }
  347. if (res && *res) {
  348. size = sizeof(struct celleb_pci_resource);
  349. free_bootmem((unsigned long)(*res), size);
  350. }
  351. }
  352. return 1;
  353. }
  354. static int __init phb_set_bus_ranges(struct device_node *dev,
  355. struct pci_controller *phb)
  356. {
  357. const int *bus_range;
  358. unsigned int len;
  359. bus_range = of_get_property(dev, "bus-range", &len);
  360. if (bus_range == NULL || len < 2 * sizeof(int))
  361. return 1;
  362. phb->first_busno = bus_range[0];
  363. phb->last_busno = bus_range[1];
  364. return 0;
  365. }
  366. static void __init celleb_alloc_private_mem(struct pci_controller *hose)
  367. {
  368. hose->private_data =
  369. alloc_maybe_bootmem(sizeof(struct celleb_pci_private),
  370. GFP_KERNEL);
  371. }
  372. static int __init celleb_setup_fake_pci(struct device_node *dev,
  373. struct pci_controller *phb)
  374. {
  375. struct device_node *node;
  376. phb->ops = &celleb_fake_pci_ops;
  377. celleb_alloc_private_mem(phb);
  378. for (node = of_get_next_child(dev, NULL);
  379. node != NULL; node = of_get_next_child(dev, node))
  380. celleb_setup_fake_pci_device(node, phb);
  381. return 0;
  382. }
  383. static struct celleb_phb_spec celleb_fake_pci_spec __initdata = {
  384. .setup = celleb_setup_fake_pci,
  385. };
  386. static struct of_device_id celleb_phb_match[] __initdata = {
  387. {
  388. .name = "pci-pseudo",
  389. .data = &celleb_fake_pci_spec,
  390. }, {
  391. .name = "epci",
  392. .data = &celleb_epci_spec,
  393. }, {
  394. .name = "pcie",
  395. .data = &celleb_pciex_spec,
  396. }, {
  397. },
  398. };
  399. static int __init celleb_io_workaround_init(struct pci_controller *phb,
  400. struct celleb_phb_spec *phb_spec)
  401. {
  402. if (phb_spec->ops) {
  403. iowa_register_bus(phb, phb_spec->ops, phb_spec->iowa_init,
  404. phb_spec->iowa_data);
  405. io_workaround_init();
  406. }
  407. return 0;
  408. }
  409. int __init celleb_setup_phb(struct pci_controller *phb)
  410. {
  411. struct device_node *dev = phb->dn;
  412. const struct of_device_id *match;
  413. struct celleb_phb_spec *phb_spec;
  414. int rc;
  415. match = of_match_node(celleb_phb_match, dev);
  416. if (!match)
  417. return 1;
  418. phb_set_bus_ranges(dev, phb);
  419. phb->buid = 1;
  420. phb_spec = match->data;
  421. rc = (*phb_spec->setup)(dev, phb);
  422. if (rc)
  423. return 1;
  424. return celleb_io_workaround_init(phb, phb_spec);
  425. }
  426. int celleb_pci_probe_mode(struct pci_bus *bus)
  427. {
  428. return PCI_PROBE_DEVTREE;
  429. }