celleb_pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 <linux/slab.h>
  35. #include <asm/io.h>
  36. #include <asm/irq.h>
  37. #include <asm/prom.h>
  38. #include <asm/pci-bridge.h>
  39. #include <asm/ppc-pci.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 pci_controller *hose = pci_bus_to_host(bus);
  140. unsigned int devno = devfn >> 3;
  141. unsigned int fn = devfn & 0x7;
  142. /* allignment check */
  143. BUG_ON(where % size);
  144. pr_debug(" fake read: bus=0x%x, ", bus->number);
  145. config = get_fake_config_start(hose, devno, fn);
  146. pr_debug("devno=0x%x, where=0x%x, size=0x%x, ", devno, where, size);
  147. if (!config) {
  148. pr_debug("failed\n");
  149. return PCIBIOS_DEVICE_NOT_FOUND;
  150. }
  151. celleb_config_read_fake(config, where, size, val);
  152. pr_debug("val=0x%x\n", *val);
  153. return PCIBIOS_SUCCESSFUL;
  154. }
  155. static int celleb_fake_pci_write_config(struct pci_bus *bus,
  156. unsigned int devfn, int where, int size, u32 val)
  157. {
  158. char *config;
  159. struct pci_controller *hose = pci_bus_to_host(bus);
  160. struct celleb_pci_resource *res;
  161. unsigned int devno = devfn >> 3;
  162. unsigned int fn = devfn & 0x7;
  163. /* allignment check */
  164. BUG_ON(where % size);
  165. config = get_fake_config_start(hose, devno, fn);
  166. if (!config)
  167. return PCIBIOS_DEVICE_NOT_FOUND;
  168. if (val == ~0) {
  169. int i = (where - PCI_BASE_ADDRESS_0) >> 3;
  170. switch (where) {
  171. case PCI_BASE_ADDRESS_0:
  172. case PCI_BASE_ADDRESS_2:
  173. if (size != 4)
  174. return PCIBIOS_DEVICE_NOT_FOUND;
  175. res = get_resource_start(hose, devno, fn);
  176. if (!res)
  177. return PCIBIOS_DEVICE_NOT_FOUND;
  178. celleb_config_write_fake(config, where, size,
  179. (res->r[i].end - res->r[i].start));
  180. return PCIBIOS_SUCCESSFUL;
  181. case PCI_BASE_ADDRESS_1:
  182. case PCI_BASE_ADDRESS_3:
  183. case PCI_BASE_ADDRESS_4:
  184. case PCI_BASE_ADDRESS_5:
  185. break;
  186. default:
  187. break;
  188. }
  189. }
  190. celleb_config_write_fake(config, where, size, val);
  191. pr_debug(" fake write: where=%x, size=%d, val=%x\n",
  192. where, size, val);
  193. return PCIBIOS_SUCCESSFUL;
  194. }
  195. static struct pci_ops celleb_fake_pci_ops = {
  196. .read = celleb_fake_pci_read_config,
  197. .write = celleb_fake_pci_write_config,
  198. };
  199. static inline void celleb_setup_pci_base_addrs(struct pci_controller *hose,
  200. unsigned int devno, unsigned int fn,
  201. unsigned int num_base_addr)
  202. {
  203. u32 val;
  204. unsigned char *config;
  205. struct celleb_pci_resource *res;
  206. config = get_fake_config_start(hose, devno, fn);
  207. res = get_resource_start(hose, devno, fn);
  208. if (!config || !res)
  209. return;
  210. switch (num_base_addr) {
  211. case 3:
  212. val = (res->r[2].start & 0xfffffff0)
  213. | PCI_BASE_ADDRESS_MEM_TYPE_64;
  214. celleb_config_write_fake(config, PCI_BASE_ADDRESS_4, 4, val);
  215. val = res->r[2].start >> 32;
  216. celleb_config_write_fake(config, PCI_BASE_ADDRESS_5, 4, val);
  217. /* FALLTHROUGH */
  218. case 2:
  219. val = (res->r[1].start & 0xfffffff0)
  220. | PCI_BASE_ADDRESS_MEM_TYPE_64;
  221. celleb_config_write_fake(config, PCI_BASE_ADDRESS_2, 4, val);
  222. val = res->r[1].start >> 32;
  223. celleb_config_write_fake(config, PCI_BASE_ADDRESS_3, 4, val);
  224. /* FALLTHROUGH */
  225. case 1:
  226. val = (res->r[0].start & 0xfffffff0)
  227. | PCI_BASE_ADDRESS_MEM_TYPE_64;
  228. celleb_config_write_fake(config, PCI_BASE_ADDRESS_0, 4, val);
  229. val = res->r[0].start >> 32;
  230. celleb_config_write_fake(config, PCI_BASE_ADDRESS_1, 4, val);
  231. break;
  232. }
  233. val = PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
  234. celleb_config_write_fake(config, PCI_COMMAND, 2, val);
  235. }
  236. static int __init celleb_setup_fake_pci_device(struct device_node *node,
  237. struct pci_controller *hose)
  238. {
  239. unsigned int rlen;
  240. int num_base_addr = 0;
  241. u32 val;
  242. const u32 *wi0, *wi1, *wi2, *wi3, *wi4;
  243. unsigned int devno, fn;
  244. struct celleb_pci_private *private = hose->private_data;
  245. unsigned char **config = NULL;
  246. struct celleb_pci_resource **res = NULL;
  247. const char *name;
  248. const unsigned long *li;
  249. int size, result;
  250. if (private == NULL) {
  251. printk(KERN_ERR "PCI: "
  252. "memory space for pci controller is not assigned\n");
  253. goto error;
  254. }
  255. name = of_get_property(node, "model", &rlen);
  256. if (!name) {
  257. printk(KERN_ERR "PCI: model property not found.\n");
  258. goto error;
  259. }
  260. wi4 = of_get_property(node, "reg", &rlen);
  261. if (wi4 == NULL)
  262. goto error;
  263. devno = ((wi4[0] >> 8) & 0xff) >> 3;
  264. fn = (wi4[0] >> 8) & 0x7;
  265. pr_debug("PCI: celleb_setup_fake_pci() %s devno=%x fn=%x\n", name,
  266. devno, fn);
  267. size = 256;
  268. config = &private->fake_config[devno][fn];
  269. *config = zalloc_maybe_bootmem(size, GFP_KERNEL);
  270. if (*config == NULL) {
  271. printk(KERN_ERR "PCI: "
  272. "not enough memory for fake configuration space\n");
  273. goto error;
  274. }
  275. pr_debug("PCI: fake config area assigned 0x%016lx\n",
  276. (unsigned long)*config);
  277. size = sizeof(struct celleb_pci_resource);
  278. res = &private->res[devno][fn];
  279. *res = zalloc_maybe_bootmem(size, GFP_KERNEL);
  280. if (*res == NULL) {
  281. printk(KERN_ERR
  282. "PCI: not enough memory for resource data space\n");
  283. goto error;
  284. }
  285. pr_debug("PCI: res assigned 0x%016lx\n", (unsigned long)*res);
  286. wi0 = of_get_property(node, "device-id", NULL);
  287. wi1 = of_get_property(node, "vendor-id", NULL);
  288. wi2 = of_get_property(node, "class-code", NULL);
  289. wi3 = of_get_property(node, "revision-id", NULL);
  290. if (!wi0 || !wi1 || !wi2 || !wi3) {
  291. printk(KERN_ERR "PCI: Missing device tree properties.\n");
  292. goto error;
  293. }
  294. celleb_config_write_fake(*config, PCI_DEVICE_ID, 2, wi0[0] & 0xffff);
  295. celleb_config_write_fake(*config, PCI_VENDOR_ID, 2, wi1[0] & 0xffff);
  296. pr_debug("class-code = 0x%08x\n", wi2[0]);
  297. celleb_config_write_fake(*config, PCI_CLASS_PROG, 1, wi2[0] & 0xff);
  298. celleb_config_write_fake(*config, PCI_CLASS_DEVICE, 2,
  299. (wi2[0] >> 8) & 0xffff);
  300. celleb_config_write_fake(*config, PCI_REVISION_ID, 1, wi3[0]);
  301. while (num_base_addr < MAX_PCI_BASE_ADDRS) {
  302. result = of_address_to_resource(node,
  303. num_base_addr, &(*res)->r[num_base_addr]);
  304. if (result)
  305. break;
  306. num_base_addr++;
  307. }
  308. celleb_setup_pci_base_addrs(hose, devno, fn, num_base_addr);
  309. li = of_get_property(node, "interrupts", &rlen);
  310. if (!li) {
  311. printk(KERN_ERR "PCI: interrupts not found.\n");
  312. goto error;
  313. }
  314. val = li[0];
  315. celleb_config_write_fake(*config, PCI_INTERRUPT_PIN, 1, 1);
  316. celleb_config_write_fake(*config, PCI_INTERRUPT_LINE, 1, val);
  317. #ifdef DEBUG
  318. pr_debug("PCI: %s irq=%ld\n", name, li[0]);
  319. for (i = 0; i < 6; i++) {
  320. celleb_config_read_fake(*config,
  321. PCI_BASE_ADDRESS_0 + 0x4 * i, 4,
  322. &val);
  323. pr_debug("PCI: %s fn=%d base_address_%d=0x%x\n",
  324. name, fn, i, val);
  325. }
  326. #endif
  327. celleb_config_write_fake(*config, PCI_HEADER_TYPE, 1,
  328. PCI_HEADER_TYPE_NORMAL);
  329. return 0;
  330. error:
  331. if (mem_init_done) {
  332. if (config && *config)
  333. kfree(*config);
  334. if (res && *res)
  335. kfree(*res);
  336. } else {
  337. if (config && *config) {
  338. size = 256;
  339. free_bootmem((unsigned long)(*config), size);
  340. }
  341. if (res && *res) {
  342. size = sizeof(struct celleb_pci_resource);
  343. free_bootmem((unsigned long)(*res), size);
  344. }
  345. }
  346. return 1;
  347. }
  348. static int __init phb_set_bus_ranges(struct device_node *dev,
  349. struct pci_controller *phb)
  350. {
  351. const int *bus_range;
  352. unsigned int len;
  353. bus_range = of_get_property(dev, "bus-range", &len);
  354. if (bus_range == NULL || len < 2 * sizeof(int))
  355. return 1;
  356. phb->first_busno = bus_range[0];
  357. phb->last_busno = bus_range[1];
  358. return 0;
  359. }
  360. static void __init celleb_alloc_private_mem(struct pci_controller *hose)
  361. {
  362. hose->private_data =
  363. zalloc_maybe_bootmem(sizeof(struct celleb_pci_private),
  364. GFP_KERNEL);
  365. }
  366. static int __init celleb_setup_fake_pci(struct device_node *dev,
  367. struct pci_controller *phb)
  368. {
  369. struct device_node *node;
  370. phb->ops = &celleb_fake_pci_ops;
  371. celleb_alloc_private_mem(phb);
  372. for (node = of_get_next_child(dev, NULL);
  373. node != NULL; node = of_get_next_child(dev, node))
  374. celleb_setup_fake_pci_device(node, phb);
  375. return 0;
  376. }
  377. static struct celleb_phb_spec celleb_fake_pci_spec __initdata = {
  378. .setup = celleb_setup_fake_pci,
  379. };
  380. static struct of_device_id celleb_phb_match[] __initdata = {
  381. {
  382. .name = "pci-pseudo",
  383. .data = &celleb_fake_pci_spec,
  384. }, {
  385. .name = "epci",
  386. .data = &celleb_epci_spec,
  387. }, {
  388. .name = "pcie",
  389. .data = &celleb_pciex_spec,
  390. }, {
  391. },
  392. };
  393. int __init celleb_setup_phb(struct pci_controller *phb)
  394. {
  395. struct device_node *dev = phb->dn;
  396. const struct of_device_id *match;
  397. struct celleb_phb_spec *phb_spec;
  398. int rc;
  399. match = of_match_node(celleb_phb_match, dev);
  400. if (!match)
  401. return 1;
  402. phb_set_bus_ranges(dev, phb);
  403. phb->buid = 1;
  404. phb_spec = match->data;
  405. rc = (*phb_spec->setup)(dev, phb);
  406. if (rc)
  407. return 1;
  408. if (phb_spec->ops)
  409. iowa_register_bus(phb, phb_spec->ops,
  410. phb_spec->iowa_init,
  411. phb_spec->iowa_data);
  412. return 0;
  413. }
  414. int celleb_pci_probe_mode(struct pci_bus *bus)
  415. {
  416. return PCI_PROBE_DEVTREE;
  417. }